1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/*
* Copyright 2006 The Android Open Source Project
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkAvoidXfermode.h"
#include "SkColorPriv.h"
#include "SkFlattenableBuffers.h"
SkAvoidXfermode::SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode)
{
if (tolerance > 255) {
tolerance = 255;
}
fOpColor = opColor;
fDistMul = (256 << 14) / (tolerance + 1);
fMode = mode;
}
SkAvoidXfermode::SkAvoidXfermode(SkFlattenableReadBuffer& buffer)
: INHERITED(buffer)
{
fOpColor = buffer.readColor();
fDistMul = buffer.readUInt();
fMode = (Mode)buffer.readUInt();
}
void SkAvoidXfermode::flatten(SkFlattenableWriteBuffer& buffer) const
{
this->INHERITED::flatten(buffer);
buffer.writeColor(fOpColor);
buffer.writeUInt(fDistMul);
buffer.writeUInt(fMode);
}
// returns 0..31
static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b)
{
SkASSERT(r <= SK_R16_MASK);
SkASSERT(g <= SK_G16_MASK);
SkASSERT(b <= SK_B16_MASK);
unsigned dr = SkAbs32(SkGetPackedR16(c) - r);
unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS);
unsigned db = SkAbs32(SkGetPackedB16(c) - b);
return SkMax32(dr, SkMax32(dg, db));
}
// returns 0..15
static unsigned color_dist4444(uint16_t c, unsigned r, unsigned g, unsigned b)
{
SkASSERT(r <= 0xF);
SkASSERT(g <= 0xF);
SkASSERT(b <= 0xF);
unsigned dr = SkAbs32(SkGetPackedR4444(c) - r);
unsigned dg = SkAbs32(SkGetPackedG4444(c) - g);
unsigned db = SkAbs32(SkGetPackedB4444(c) - b);
return SkMax32(dr, SkMax32(dg, db));
}
// returns 0..255
static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b)
{
SkASSERT(r <= 0xFF);
SkASSERT(g <= 0xFF);
SkASSERT(b <= 0xFF);
unsigned dr = SkAbs32(SkGetPackedR32(c) - r);
unsigned dg = SkAbs32(SkGetPackedG32(c) - g);
unsigned db = SkAbs32(SkGetPackedB32(c) - b);
return SkMax32(dr, SkMax32(dg, db));
}
static int scale_dist_14(int dist, uint32_t mul, uint32_t sub)
{
int tmp = dist * mul - sub;
int result = (tmp + (1 << 13)) >> 14;
return result;
}
static inline unsigned Accurate255To256(unsigned x) {
return x + (x >> 7);
}
void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count,
const SkAlpha aa[])
{
unsigned opR = SkColorGetR(fOpColor);
unsigned opG = SkColorGetG(fOpColor);
unsigned opB = SkColorGetB(fOpColor);
uint32_t mul = fDistMul;
uint32_t sub = (fDistMul - (1 << 14)) << 8;
int MAX, mask;
if (kTargetColor_Mode == fMode) {
mask = -1;
MAX = 255;
} else {
mask = 0;
MAX = 0;
}
for (int i = 0; i < count; i++) {
int d = color_dist32(dst[i], opR, opG, opB);
// now reverse d if we need to
d = MAX + (d ^ mask) - mask;
SkASSERT((unsigned)d <= 255);
d = Accurate255To256(d);
d = scale_dist_14(d, mul, sub);
SkASSERT(d <= 256);
if (d > 0) {
if (NULL != aa) {
d = SkAlphaMul(d, Accurate255To256(*aa++));
if (0 == d) {
continue;
}
}
dst[i] = SkFourByteInterp256(src[i], dst[i], d);
}
}
}
static inline U16CPU SkBlend3216(SkPMColor src, U16CPU dst, unsigned scale)
{
SkASSERT(scale <= 32);
scale <<= 3;
return SkPackRGB16( SkAlphaBlend(SkPacked32ToR16(src), SkGetPackedR16(dst), scale),
SkAlphaBlend(SkPacked32ToG16(src), SkGetPackedG16(dst), scale),
SkAlphaBlend(SkPacked32ToB16(src), SkGetPackedB16(dst), scale));
}
void SkAvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
const SkAlpha aa[])
{
unsigned opR = SkColorGetR(fOpColor) >> (8 - SK_R16_BITS);
unsigned opG = SkColorGetG(fOpColor) >> (8 - SK_G16_BITS);
unsigned opB = SkColorGetB(fOpColor) >> (8 - SK_R16_BITS);
uint32_t mul = fDistMul;
uint32_t sub = (fDistMul - (1 << 14)) << SK_R16_BITS;
int MAX, mask;
if (kTargetColor_Mode == fMode) {
mask = -1;
MAX = 31;
} else {
mask = 0;
MAX = 0;
}
for (int i = 0; i < count; i++) {
int d = color_dist16(dst[i], opR, opG, opB);
// now reverse d if we need to
d = MAX + (d ^ mask) - mask;
SkASSERT((unsigned)d <= 31);
// convert from 0..31 to 0..32
d += d >> 4;
d = scale_dist_14(d, mul, sub);
SkASSERT(d <= 32);
if (d > 0) {
if (NULL != aa) {
d = SkAlphaMul(d, Accurate255To256(*aa++));
if (0 == d) {
continue;
}
}
dst[i] = SkBlend3216(src[i], dst[i], d);
}
}
}
void SkAvoidXfermode::xfer4444(uint16_t dst[], const SkPMColor src[], int count,
const SkAlpha aa[])
{
unsigned opR = SkColorGetR(fOpColor) >> 4;
unsigned opG = SkColorGetG(fOpColor) >> 4;
unsigned opB = SkColorGetB(fOpColor) >> 4;
uint32_t mul = fDistMul;
uint32_t sub = (fDistMul - (1 << 14)) << 4;
int MAX, mask;
if (kTargetColor_Mode == fMode) {
mask = -1;
MAX = 15;
} else {
mask = 0;
MAX = 0;
}
for (int i = 0; i < count; i++) {
int d = color_dist4444(dst[i], opR, opG, opB);
// now reverse d if we need to
d = MAX + (d ^ mask) - mask;
SkASSERT((unsigned)d <= 15);
// convert from 0..15 to 0..16
d += d >> 3;
d = scale_dist_14(d, mul, sub);
SkASSERT(d <= 16);
if (d > 0) {
if (NULL != aa) {
d = SkAlphaMul(d, Accurate255To256(*aa++));
if (0 == d) {
continue;
}
}
dst[i] = SkBlend4444(SkPixel32ToPixel4444(src[i]), dst[i], d);
}
}
}
void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count, const SkAlpha aa[])
{
// override in subclass
}
SK_DEFINE_FLATTENABLE_REGISTRAR(SkAvoidXfermode)
|