1 /*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "AvoidXfermode.h"
9 #include "SkColorPriv.h"
10 #include "SkReadBuffer.h"
11 #include "SkWriteBuffer.h"
12 #include "SkString.h"
13
AvoidXfermode(SkColor opColor,U8CPU tolerance,Mode mode)14 AvoidXfermode::AvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode) {
15 if (tolerance > 255) {
16 tolerance = 255;
17 }
18 fTolerance = SkToU8(tolerance);
19 fOpColor = opColor;
20 fDistMul = (256 << 14) / (tolerance + 1);
21 fMode = mode;
22 }
23
CreateProc(SkReadBuffer & buffer)24 SkFlattenable* AvoidXfermode::CreateProc(SkReadBuffer& buffer) {
25 const SkColor color = buffer.readColor();
26 const unsigned tolerance = buffer.readUInt();
27 const unsigned mode = buffer.readUInt();
28 return Create(color, tolerance, (Mode)mode);
29 }
30
flatten(SkWriteBuffer & buffer) const31 void AvoidXfermode::flatten(SkWriteBuffer& buffer) const {
32 buffer.writeColor(fOpColor);
33 buffer.writeUInt(fTolerance);
34 buffer.writeUInt(fMode);
35 }
36
37 // returns 0..31
color_dist16(uint16_t c,unsigned r,unsigned g,unsigned b)38 static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b) {
39 SkASSERT(r <= SK_R16_MASK);
40 SkASSERT(g <= SK_G16_MASK);
41 SkASSERT(b <= SK_B16_MASK);
42
43 unsigned dr = SkAbs32(SkGetPackedR16(c) - r);
44 unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS);
45 unsigned db = SkAbs32(SkGetPackedB16(c) - b);
46
47 return SkMax32(dr, SkMax32(dg, db));
48 }
49
50 // returns 0..255
color_dist32(SkPMColor c,U8CPU r,U8CPU g,U8CPU b)51 static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b) {
52 SkASSERT(r <= 0xFF);
53 SkASSERT(g <= 0xFF);
54 SkASSERT(b <= 0xFF);
55
56 unsigned dr = SkAbs32(SkGetPackedR32(c) - r);
57 unsigned dg = SkAbs32(SkGetPackedG32(c) - g);
58 unsigned db = SkAbs32(SkGetPackedB32(c) - b);
59
60 return SkMax32(dr, SkMax32(dg, db));
61 }
62
scale_dist_14(int dist,uint32_t mul,uint32_t sub)63 static int scale_dist_14(int dist, uint32_t mul, uint32_t sub) {
64 int tmp = dist * mul - sub;
65 int result = (tmp + (1 << 13)) >> 14;
66
67 return result;
68 }
69
Accurate255To256(unsigned x)70 static inline unsigned Accurate255To256(unsigned x) {
71 return x + (x >> 7);
72 }
73
xfer32(SkPMColor dst[],const SkPMColor src[],int count,const SkAlpha aa[]) const74 void AvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count,
75 const SkAlpha aa[]) const {
76 unsigned opR = SkColorGetR(fOpColor);
77 unsigned opG = SkColorGetG(fOpColor);
78 unsigned opB = SkColorGetB(fOpColor);
79 uint32_t mul = fDistMul;
80 uint32_t sub = (fDistMul - (1 << 14)) << 8;
81
82 int MAX, mask;
83
84 if (kTargetColor_Mode == fMode) {
85 mask = -1;
86 MAX = 255;
87 } else {
88 mask = 0;
89 MAX = 0;
90 }
91
92 for (int i = 0; i < count; i++) {
93 int d = color_dist32(dst[i], opR, opG, opB);
94 // now reverse d if we need to
95 d = MAX + (d ^ mask) - mask;
96 SkASSERT((unsigned)d <= 255);
97 d = Accurate255To256(d);
98
99 d = scale_dist_14(d, mul, sub);
100 SkASSERT(d <= 256);
101
102 if (d > 0) {
103 if (aa) {
104 d = SkAlphaMul(d, Accurate255To256(*aa++));
105 if (0 == d) {
106 continue;
107 }
108 }
109 dst[i] = SkFourByteInterp256(src[i], dst[i], d);
110 }
111 }
112 }
113
SkBlend3216(SkPMColor src,U16CPU dst,unsigned scale)114 static inline U16CPU SkBlend3216(SkPMColor src, U16CPU dst, unsigned scale) {
115 SkASSERT(scale <= 32);
116 scale <<= 3;
117
118 return SkPackRGB16( SkAlphaBlend(SkPacked32ToR16(src), SkGetPackedR16(dst), scale),
119 SkAlphaBlend(SkPacked32ToG16(src), SkGetPackedG16(dst), scale),
120 SkAlphaBlend(SkPacked32ToB16(src), SkGetPackedB16(dst), scale));
121 }
122
xfer16(uint16_t dst[],const SkPMColor src[],int count,const SkAlpha aa[]) const123 void AvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count,
124 const SkAlpha aa[]) const {
125 unsigned opR = SkColorGetR(fOpColor) >> (8 - SK_R16_BITS);
126 unsigned opG = SkColorGetG(fOpColor) >> (8 - SK_G16_BITS);
127 unsigned opB = SkColorGetB(fOpColor) >> (8 - SK_R16_BITS);
128 uint32_t mul = fDistMul;
129 uint32_t sub = (fDistMul - (1 << 14)) << SK_R16_BITS;
130
131 int MAX, mask;
132
133 if (kTargetColor_Mode == fMode) {
134 mask = -1;
135 MAX = 31;
136 } else {
137 mask = 0;
138 MAX = 0;
139 }
140
141 for (int i = 0; i < count; i++) {
142 int d = color_dist16(dst[i], opR, opG, opB);
143 // now reverse d if we need to
144 d = MAX + (d ^ mask) - mask;
145 SkASSERT((unsigned)d <= 31);
146 // convert from 0..31 to 0..32
147 d += d >> 4;
148 d = scale_dist_14(d, mul, sub);
149 SkASSERT(d <= 32);
150
151 if (d > 0) {
152 if (aa) {
153 d = SkAlphaMul(d, Accurate255To256(*aa++));
154 if (0 == d) {
155 continue;
156 }
157 }
158 dst[i] = SkBlend3216(src[i], dst[i], d);
159 }
160 }
161 }
162
xferA8(SkAlpha dst[],const SkPMColor src[],int count,const SkAlpha aa[]) const163 void AvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count,
164 const SkAlpha aa[]) const {
165 }
166
167 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const168 void AvoidXfermode::toString(SkString* str) const {
169 str->append("AvoidXfermode: opColor: ");
170 str->appendHex(fOpColor);
171 str->appendf("distMul: %d ", fDistMul);
172
173 static const char* gModeStrings[] = { "Avoid", "Target" };
174
175 str->appendf("mode: %s", gModeStrings[fMode]);
176 }
177 #endif
178