OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 Google Inc. | |
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 "SkColorPriv.h" | |
9 #include "SkMaskSwizzler.h" | |
10 | |
11 #define UPDATE_ALPHA(alpha, zeroAlpha, maxAlpha) \ | |
scroggo
2015/03/12 19:58:40
As stated elsewhere, since we share it, I think it
msarett
2015/03/12 21:59:41
Done.
| |
12 zeroAlpha |= (alpha); \ | |
13 maxAlpha &= (alpha); | |
14 | |
15 /* | |
16 * | |
17 * Row procedure for masked color components with 16 bits per pixel | |
18 * | |
19 */ | |
20 static SkSwizzler::ResultAlpha swizzle_mask16_to_n32( | |
21 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { | |
22 | |
23 // Use the masks to decode to the destination | |
24 uint16_t* srcPtr = (uint16_t*) srcRow; | |
25 SkPMColor* dstPtr = (SkPMColor*) dstRow; | |
26 for (int i = 0; i < width; i++) { | |
27 uint16_t p = srcPtr[i]; | |
28 uint8_t red = masks->getRed(p); | |
29 uint8_t green = masks->getGreen(p); | |
30 uint8_t blue = masks->getBlue(p); | |
31 dstPtr[i] = SkPackARGB32NoCheck(0xFF, red, green, blue); | |
32 } | |
33 return SkSwizzler::kOpaque_ResultAlpha; | |
34 } | |
35 | |
36 /* | |
37 * | |
38 * Row procedure for masked color components with 16 bits per pixel with alpha | |
39 * | |
40 */ | |
41 static SkSwizzler::ResultAlpha swizzle_mask16_alpha_to_n32( | |
42 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { | |
43 | |
44 // Use the masks to decode to the destination | |
45 uint16_t* srcPtr = (uint16_t*) srcRow; | |
46 SkPMColor* dstPtr = (SkPMColor*) dstRow; | |
47 uint8_t zeroAlpha = SkSwizzler::kZeroAlphaInit; | |
48 uint8_t maxAlpha = SkSwizzler::kMaxAlphaInit; | |
49 for (int i = 0; i < width; i++) { | |
50 uint16_t p = srcPtr[i]; | |
51 uint8_t red = masks->getRed(p); | |
52 uint8_t green = masks->getGreen(p); | |
53 uint8_t blue = masks->getBlue(p); | |
54 uint8_t alpha = masks->getAlpha(p); | |
55 UPDATE_ALPHA(alpha, zeroAlpha, maxAlpha); | |
56 dstPtr[i] = SkPackARGB32NoCheck(alpha, red, green, blue); | |
57 } | |
58 return SkSwizzler::GetResult(zeroAlpha, maxAlpha); | |
59 } | |
60 | |
61 /* | |
62 * | |
63 * Row procedure for masked color components with 24 bits per pixel | |
64 * | |
65 */ | |
66 static SkSwizzler::ResultAlpha swizzle_mask24_to_n32( | |
67 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { | |
68 | |
69 // Use the masks to decode to the destination | |
70 SkPMColor* dstPtr = (SkPMColor*) dstRow; | |
71 for (int i = 0; i < 3*width; i += 3) { | |
72 uint32_t p = srcRow[i] | (srcRow[i + 1] << 8) | srcRow[i + 2] << 16; | |
73 uint8_t red = masks->getRed(p); | |
74 uint8_t green = masks->getGreen(p); | |
75 uint8_t blue = masks->getBlue(p); | |
76 dstPtr[i/3] = SkPackARGB32NoCheck(0xFF, red, green, blue); | |
77 } | |
78 return SkSwizzler::kOpaque_ResultAlpha; | |
79 } | |
80 | |
81 /* | |
82 * | |
83 * Row procedure for masked color components with 24 bits per pixel with alpha | |
84 * | |
85 */ | |
86 static SkSwizzler::ResultAlpha swizzle_mask24_alpha_to_n32( | |
87 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { | |
88 | |
89 // Use the masks to decode to the destination | |
90 SkPMColor* dstPtr = (SkPMColor*) dstRow; | |
91 uint8_t zeroAlpha = SkSwizzler::kZeroAlphaInit; | |
92 uint8_t maxAlpha = SkSwizzler::kMaxAlphaInit; | |
93 for (int i = 0; i < 3*width; i += 3) { | |
94 uint32_t p = srcRow[i] | (srcRow[i + 1] << 8) | srcRow[i + 2] << 16; | |
95 uint8_t red = masks->getRed(p); | |
96 uint8_t green = masks->getGreen(p); | |
97 uint8_t blue = masks->getBlue(p); | |
98 uint8_t alpha = masks->getAlpha(p); | |
99 UPDATE_ALPHA(alpha, zeroAlpha, maxAlpha); | |
100 dstPtr[i/3] = SkPackARGB32NoCheck(alpha, red, green, blue); | |
101 } | |
102 return SkSwizzler::GetResult(zeroAlpha, maxAlpha); | |
103 } | |
104 | |
105 /* | |
106 * | |
107 * Row procedure for masked color components with 32 bits per pixel | |
108 * | |
109 */ | |
110 static SkSwizzler::ResultAlpha swizzle_mask32_to_n32( | |
111 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { | |
112 | |
113 // Use the masks to decode to the destination | |
114 uint32_t* srcPtr = (uint32_t*) srcRow; | |
115 SkPMColor* dstPtr = (SkPMColor*) dstRow; | |
116 for (int i = 0; i < width; i++) { | |
117 uint32_t p = srcPtr[i]; | |
118 uint8_t red = masks->getRed(p); | |
119 uint8_t green = masks->getGreen(p); | |
120 uint8_t blue = masks->getBlue(p); | |
121 dstPtr[i] = SkPackARGB32NoCheck(0xFF, red, green, blue); | |
122 } | |
123 return SkSwizzler::kOpaque_ResultAlpha; | |
124 } | |
125 | |
126 /* | |
127 * | |
128 * Row procedure for masked color components with 32 bits per pixel | |
129 * | |
130 */ | |
131 static SkSwizzler::ResultAlpha swizzle_mask32_alpha_to_n32( | |
132 void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { | |
133 | |
134 // Use the masks to decode to the destination | |
135 uint32_t* srcPtr = (uint32_t*) srcRow; | |
136 SkPMColor* dstPtr = (SkPMColor*) dstRow; | |
137 uint8_t zeroAlpha = SkSwizzler::kZeroAlphaInit; | |
138 uint8_t maxAlpha = SkSwizzler::kMaxAlphaInit; | |
139 for (int i = 0; i < width; i++) { | |
140 uint32_t p = srcPtr[i]; | |
141 uint8_t red = masks->getRed(p); | |
142 uint8_t green = masks->getGreen(p); | |
143 uint8_t blue = masks->getBlue(p); | |
144 uint8_t alpha = masks->getAlpha(p); | |
145 UPDATE_ALPHA(alpha, zeroAlpha, maxAlpha); | |
146 dstPtr[i] = SkPackARGB32NoCheck(alpha, red, green, blue); | |
147 } | |
148 return SkSwizzler::GetResult(zeroAlpha, maxAlpha); | |
149 } | |
150 | |
151 #undef UPDATE_ALPHA | |
152 | |
153 /* | |
154 * | |
155 * Create a new mask swizzler | |
156 * | |
157 */ | |
158 SkMaskSwizzler* SkMaskSwizzler::CreateMaskSwizzler( | |
159 const SkImageInfo& imageInfo, SkMasks* masks, uint32_t bitsPerPixel) { | |
160 | |
161 // Choose the appropriate row procedure | |
162 RowProc proc = NULL; | |
163 uint32_t alphaMask = masks->getAlphaMask(); | |
164 switch (bitsPerPixel) { | |
165 case 16: | |
166 if (0 == alphaMask) { | |
167 proc = &swizzle_mask16_to_n32; | |
168 } else { | |
169 proc = &swizzle_mask16_alpha_to_n32; | |
170 } | |
171 break; | |
172 case 24: | |
173 if (0 == alphaMask) { | |
174 proc = &swizzle_mask24_to_n32; | |
175 } else { | |
176 proc = &swizzle_mask24_alpha_to_n32; | |
177 } | |
178 break; | |
179 case 32: | |
180 if (0 == alphaMask) { | |
181 proc = &swizzle_mask32_to_n32; | |
182 } else { | |
183 proc = &swizzle_mask32_alpha_to_n32; | |
184 } | |
185 break; | |
186 default: | |
187 SkASSERT(false); | |
188 return NULL; | |
189 } | |
190 return SkNEW_ARGS(SkMaskSwizzler, (imageInfo, masks, proc)); | |
191 } | |
192 | |
193 /* | |
194 * | |
195 * Constructor for mask swizzler | |
196 * | |
197 */ | |
198 SkMaskSwizzler::SkMaskSwizzler(const SkImageInfo& imageInfo, | |
199 SkMasks* masks, RowProc proc) | |
200 : fImageInfo(imageInfo) | |
201 , fMasks(masks) | |
202 , fRowProc(proc) | |
203 {} | |
204 | |
205 /* | |
206 * | |
207 * Swizzle the next row | |
208 * | |
209 */ | |
210 SkSwizzler::ResultAlpha SkMaskSwizzler::next(void* dst, | |
211 const uint8_t* src) { | |
212 return fRowProc(dst, src, fImageInfo.width(), fMasks); | |
213 } | |
OLD | NEW |