1 /*
2 * Copyright 2014 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 "SkBitmap.h"
9 #include "SkCanvas.h"
10 #include "SkConfig8888.h"
11 #include "SkColorPriv.h"
12 #include "SkDither.h"
13 #include "SkMathPriv.h"
14 #include "SkUnPreMultiply.h"
15
16 enum AlphaVerb {
17 kNothing_AlphaVerb,
18 kPremul_AlphaVerb,
19 kUnpremul_AlphaVerb,
20 };
21
convert32(uint32_t c)22 template <bool doSwapRB, AlphaVerb doAlpha> uint32_t convert32(uint32_t c) {
23 if (doSwapRB) {
24 c = SkSwizzle_RB(c);
25 }
26
27 // Lucky for us, in both RGBA and BGRA, the alpha component is always in the same place, so
28 // we can perform premul or unpremul the same way without knowing the swizzles for RGB.
29 switch (doAlpha) {
30 case kNothing_AlphaVerb:
31 // no change
32 break;
33 case kPremul_AlphaVerb:
34 c = SkPreMultiplyARGB(SkGetPackedA32(c), SkGetPackedR32(c),
35 SkGetPackedG32(c), SkGetPackedB32(c));
36 break;
37 case kUnpremul_AlphaVerb:
38 c = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(c);
39 break;
40 }
41 return c;
42 }
43
44 template <bool doSwapRB, AlphaVerb doAlpha>
convert32_row(uint32_t * dst,const uint32_t * src,int count)45 void convert32_row(uint32_t* dst, const uint32_t* src, int count) {
46 // This has to be correct if src == dst (but not partial overlap)
47 for (int i = 0; i < count; ++i) {
48 dst[i] = convert32<doSwapRB, doAlpha>(src[i]);
49 }
50 }
51
is_32bit_colortype(SkColorType ct)52 static bool is_32bit_colortype(SkColorType ct) {
53 return kRGBA_8888_SkColorType == ct || kBGRA_8888_SkColorType == ct;
54 }
55
compute_AlphaVerb(SkAlphaType src,SkAlphaType dst)56 static AlphaVerb compute_AlphaVerb(SkAlphaType src, SkAlphaType dst) {
57 SkASSERT(kUnknown_SkAlphaType != src);
58 SkASSERT(kUnknown_SkAlphaType != dst);
59
60 if (kOpaque_SkAlphaType == src || kOpaque_SkAlphaType == dst || src == dst) {
61 return kNothing_AlphaVerb;
62 }
63 if (kPremul_SkAlphaType == dst) {
64 SkASSERT(kUnpremul_SkAlphaType == src);
65 return kPremul_AlphaVerb;
66 } else {
67 SkASSERT(kPremul_SkAlphaType == src);
68 SkASSERT(kUnpremul_SkAlphaType == dst);
69 return kUnpremul_AlphaVerb;
70 }
71 }
72
memcpy32_row(uint32_t * dst,const uint32_t * src,int count)73 static void memcpy32_row(uint32_t* dst, const uint32_t* src, int count) {
74 memcpy(dst, src, count * 4);
75 }
76
convertPixelsTo(SkDstPixelInfo * dst,int width,int height) const77 bool SkSrcPixelInfo::convertPixelsTo(SkDstPixelInfo* dst, int width, int height) const {
78 if (width <= 0 || height <= 0) {
79 return false;
80 }
81
82 if (!is_32bit_colortype(fColorType) || !is_32bit_colortype(dst->fColorType)) {
83 return false;
84 }
85
86 void (*proc)(uint32_t* dst, const uint32_t* src, int count);
87 AlphaVerb doAlpha = compute_AlphaVerb(fAlphaType, dst->fAlphaType);
88 bool doSwapRB = fColorType != dst->fColorType;
89
90 switch (doAlpha) {
91 case kNothing_AlphaVerb:
92 if (doSwapRB) {
93 proc = convert32_row<true, kNothing_AlphaVerb>;
94 } else {
95 if (fPixels == dst->fPixels) {
96 return true;
97 }
98 proc = memcpy32_row;
99 }
100 break;
101 case kPremul_AlphaVerb:
102 if (doSwapRB) {
103 proc = convert32_row<true, kPremul_AlphaVerb>;
104 } else {
105 proc = convert32_row<false, kPremul_AlphaVerb>;
106 }
107 break;
108 case kUnpremul_AlphaVerb:
109 if (doSwapRB) {
110 proc = convert32_row<true, kUnpremul_AlphaVerb>;
111 } else {
112 proc = convert32_row<false, kUnpremul_AlphaVerb>;
113 }
114 break;
115 }
116
117 uint32_t* dstP = static_cast<uint32_t*>(dst->fPixels);
118 const uint32_t* srcP = static_cast<const uint32_t*>(fPixels);
119 size_t srcInc = fRowBytes >> 2;
120 size_t dstInc = dst->fRowBytes >> 2;
121 for (int y = 0; y < height; ++y) {
122 proc(dstP, srcP, width);
123 dstP += dstInc;
124 srcP += srcInc;
125 }
126 return true;
127 }
128
copy_g8_to_32(void * dst,size_t dstRB,const void * src,size_t srcRB,int w,int h)129 static void copy_g8_to_32(void* dst, size_t dstRB, const void* src, size_t srcRB, int w, int h) {
130 uint32_t* dst32 = (uint32_t*)dst;
131 const uint8_t* src8 = (const uint8_t*)src;
132
133 for (int y = 0; y < h; ++y) {
134 for (int x = 0; x < w; ++x) {
135 dst32[x] = SkPackARGB32(0xFF, src8[x], src8[x], src8[x]);
136 }
137 dst32 = (uint32_t*)((char*)dst32 + dstRB);
138 src8 += srcRB;
139 }
140 }
141
copy_32_to_g8(void * dst,size_t dstRB,const void * src,size_t srcRB,const SkImageInfo & srcInfo)142 static void copy_32_to_g8(void* dst, size_t dstRB, const void* src, size_t srcRB,
143 const SkImageInfo& srcInfo) {
144 uint8_t* dst8 = (uint8_t*)dst;
145 const uint32_t* src32 = (const uint32_t*)src;
146
147 const int w = srcInfo.width();
148 const int h = srcInfo.height();
149 const bool isBGRA = (kBGRA_8888_SkColorType == srcInfo.colorType());
150
151 for (int y = 0; y < h; ++y) {
152 if (isBGRA) {
153 // BGRA
154 for (int x = 0; x < w; ++x) {
155 uint32_t s = src32[x];
156 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF, s & 0xFF);
157 }
158 } else {
159 // RGBA
160 for (int x = 0; x < w; ++x) {
161 uint32_t s = src32[x];
162 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16) & 0xFF);
163 }
164 }
165 src32 = (const uint32_t*)((const char*)src32 + srcRB);
166 dst8 += dstRB;
167 }
168 }
169
extract_alpha(void * dst,size_t dstRB,const void * src,size_t srcRB,const SkImageInfo & srcInfo,SkColorTable * ctable)170 static bool extract_alpha(void* dst, size_t dstRB, const void* src, size_t srcRB,
171 const SkImageInfo& srcInfo, SkColorTable* ctable) {
172 uint8_t* SK_RESTRICT dst8 = (uint8_t*)dst;
173
174 const int w = srcInfo.width();
175 const int h = srcInfo.height();
176 if (srcInfo.isOpaque()) {
177 // src is opaque, so just fill alpha with 0xFF
178 for (int y = 0; y < h; ++y) {
179 memset(dst8, 0xFF, w);
180 dst8 += dstRB;
181 }
182 return true;
183 }
184 switch (srcInfo.colorType()) {
185 case kN32_SkColorType: {
186 const SkPMColor* SK_RESTRICT src32 = (const SkPMColor*)src;
187 for (int y = 0; y < h; ++y) {
188 for (int x = 0; x < w; ++x) {
189 dst8[x] = SkGetPackedA32(src32[x]);
190 }
191 dst8 += dstRB;
192 src32 = (const SkPMColor*)((const char*)src32 + srcRB);
193 }
194 break;
195 }
196 case kARGB_4444_SkColorType: {
197 const SkPMColor16* SK_RESTRICT src16 = (const SkPMColor16*)src;
198 for (int y = 0; y < h; ++y) {
199 for (int x = 0; x < w; ++x) {
200 dst8[x] = SkPacked4444ToA32(src16[x]);
201 }
202 dst8 += dstRB;
203 src16 = (const SkPMColor16*)((const char*)src16 + srcRB);
204 }
205 break;
206 }
207 case kIndex_8_SkColorType: {
208 if (nullptr == ctable) {
209 return false;
210 }
211 const SkPMColor* SK_RESTRICT table = ctable->readColors();
212 const uint8_t* SK_RESTRICT src8 = (const uint8_t*)src;
213 for (int y = 0; y < h; ++y) {
214 for (int x = 0; x < w; ++x) {
215 dst8[x] = SkGetPackedA32(table[src8[x]]);
216 }
217 dst8 += dstRB;
218 src8 += srcRB;
219 }
220 break;
221 }
222 default:
223 return false;
224 }
225 return true;
226 }
227
CopyPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRB,const SkImageInfo & srcInfo,const void * srcPixels,size_t srcRB,SkColorTable * ctable)228 bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
229 const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB,
230 SkColorTable* ctable) {
231 if (srcInfo.dimensions() != dstInfo.dimensions()) {
232 return false;
233 }
234
235 const int width = srcInfo.width();
236 const int height = srcInfo.height();
237
238 // Do the easiest one first : both configs are equal
239 if ((srcInfo == dstInfo) && !ctable) {
240 size_t bytes = width * srcInfo.bytesPerPixel();
241 for (int y = 0; y < height; ++y) {
242 memcpy(dstPixels, srcPixels, bytes);
243 srcPixels = (const char*)srcPixels + srcRB;
244 dstPixels = (char*)dstPixels + dstRB;
245 }
246 return true;
247 }
248
249 // Handle fancy alpha swizzling if both are ARGB32
250 if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel()) {
251 SkDstPixelInfo dstPI;
252 dstPI.fColorType = dstInfo.colorType();
253 dstPI.fAlphaType = dstInfo.alphaType();
254 dstPI.fPixels = dstPixels;
255 dstPI.fRowBytes = dstRB;
256
257 SkSrcPixelInfo srcPI;
258 srcPI.fColorType = srcInfo.colorType();
259 srcPI.fAlphaType = srcInfo.alphaType();
260 srcPI.fPixels = srcPixels;
261 srcPI.fRowBytes = srcRB;
262
263 return srcPI.convertPixelsTo(&dstPI, width, height);
264 }
265
266 // If they agree on colorType and the alphaTypes are compatible, then we just memcpy.
267 // Note: we've already taken care of 32bit colortypes above.
268 if (srcInfo.colorType() == dstInfo.colorType()) {
269 switch (srcInfo.colorType()) {
270 case kRGB_565_SkColorType:
271 case kAlpha_8_SkColorType:
272 case kGray_8_SkColorType:
273 break;
274 case kIndex_8_SkColorType:
275 case kARGB_4444_SkColorType:
276 case kRGBA_F16_SkColorType:
277 if (srcInfo.alphaType() != dstInfo.alphaType()) {
278 return false;
279 }
280 break;
281 default:
282 return false;
283 }
284 SkRectMemcpy(dstPixels, dstRB, srcPixels, srcRB, width * srcInfo.bytesPerPixel(), height);
285 return true;
286 }
287
288 /*
289 * Begin section where we try to change colorTypes along the way. Not all combinations
290 * are supported.
291 */
292
293 if (kGray_8_SkColorType == srcInfo.colorType() && 4 == dstInfo.bytesPerPixel()) {
294 copy_g8_to_32(dstPixels, dstRB, srcPixels, srcRB, width, height);
295 return true;
296 }
297 if (kGray_8_SkColorType == dstInfo.colorType() && 4 == srcInfo.bytesPerPixel()) {
298 copy_32_to_g8(dstPixels, dstRB, srcPixels, srcRB, srcInfo);
299 return true;
300 }
301
302 if (kAlpha_8_SkColorType == dstInfo.colorType() &&
303 extract_alpha(dstPixels, dstRB, srcPixels, srcRB, srcInfo, ctable)) {
304 return true;
305 }
306
307 // Can no longer draw directly into 4444, but we can manually whack it for a few combinations
308 if (kARGB_4444_SkColorType == dstInfo.colorType() &&
309 (kN32_SkColorType == srcInfo.colorType() || kIndex_8_SkColorType == srcInfo.colorType())) {
310 if (srcInfo.alphaType() == kUnpremul_SkAlphaType) {
311 // Our method for converting to 4444 assumes premultiplied.
312 return false;
313 }
314
315 const SkPMColor* table = nullptr;
316 if (kIndex_8_SkColorType == srcInfo.colorType()) {
317 if (nullptr == ctable) {
318 return false;
319 }
320 table = ctable->readColors();
321 }
322
323 for (int y = 0; y < height; ++y) {
324 DITHER_4444_SCAN(y);
325 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*)dstPixels;
326 if (table) {
327 const uint8_t* SK_RESTRICT srcRow = (const uint8_t*)srcPixels;
328 for (int x = 0; x < width; ++x) {
329 dstRow[x] = SkDitherARGB32To4444(table[srcRow[x]], DITHER_VALUE(x));
330 }
331 } else {
332 const SkPMColor* SK_RESTRICT srcRow = (const SkPMColor*)srcPixels;
333 for (int x = 0; x < width; ++x) {
334 dstRow[x] = SkDitherARGB32To4444(srcRow[x], DITHER_VALUE(x));
335 }
336 }
337 dstPixels = (char*)dstPixels + dstRB;
338 srcPixels = (const char*)srcPixels + srcRB;
339 }
340 return true;
341 }
342
343 if (dstInfo.alphaType() == kUnpremul_SkAlphaType) {
344 // We do not support drawing to unpremultiplied bitmaps.
345 return false;
346 }
347
348 // Final fall-back, draw with a canvas
349 //
350 // Always clear the dest in case one of the blitters accesses it
351 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
352 {
353 SkBitmap bm;
354 if (!bm.installPixels(srcInfo, const_cast<void*>(srcPixels), srcRB, ctable, nullptr, nullptr)) {
355 return false;
356 }
357 SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterDirect(dstInfo, dstPixels, dstRB));
358 if (nullptr == canvas.get()) {
359 return false;
360 }
361
362 SkPaint paint;
363 paint.setDither(true);
364
365 canvas->clear(0);
366 canvas->drawBitmap(bm, 0, 0, &paint);
367 return true;
368 }
369 }
370