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(kIgnore_SkAlphaType != src);
58 SkASSERT(kIgnore_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
rect_memcpy(void * dst,size_t dstRB,const void * src,size_t srcRB,size_t bytesPerRow,int rowCount)129 static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB, size_t bytesPerRow,
130 int rowCount) {
131 SkASSERT(bytesPerRow <= srcRB);
132 SkASSERT(bytesPerRow <= dstRB);
133 for (int i = 0; i < rowCount; ++i) {
134 memcpy(dst, src, bytesPerRow);
135 dst = (char*)dst + dstRB;
136 src = (const char*)src + srcRB;
137 }
138 }
139
CopyPixels(const SkImageInfo & dstInfo,void * dstPixels,size_t dstRB,const SkImageInfo & srcInfo,const void * srcPixels,size_t srcRB,SkColorTable * ctable)140 bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB,
141 const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB,
142 SkColorTable* ctable) {
143 if (srcInfo.dimensions() != dstInfo.dimensions()) {
144 return false;
145 }
146
147 const int width = srcInfo.width();
148 const int height = srcInfo.height();
149
150 // Handle fancy alpha swizzling if both are ARGB32
151 if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel()) {
152 SkDstPixelInfo dstPI;
153 dstPI.fColorType = dstInfo.colorType();
154 dstPI.fAlphaType = dstInfo.alphaType();
155 dstPI.fPixels = dstPixels;
156 dstPI.fRowBytes = dstRB;
157
158 SkSrcPixelInfo srcPI;
159 srcPI.fColorType = srcInfo.colorType();
160 srcPI.fAlphaType = srcInfo.alphaType();
161 srcPI.fPixels = srcPixels;
162 srcPI.fRowBytes = srcRB;
163
164 return srcPI.convertPixelsTo(&dstPI, width, height);
165 }
166
167 // If they agree on colorType and the alphaTypes are compatible, then we just memcpy.
168 // Note: we've already taken care of 32bit colortypes above.
169 if (srcInfo.colorType() == dstInfo.colorType()) {
170 switch (srcInfo.colorType()) {
171 case kRGB_565_SkColorType:
172 case kAlpha_8_SkColorType:
173 break;
174 case kIndex_8_SkColorType:
175 case kARGB_4444_SkColorType:
176 if (srcInfo.alphaType() != dstInfo.alphaType()) {
177 return false;
178 }
179 break;
180 default:
181 return false;
182 }
183 rect_memcpy(dstPixels, dstRB, srcPixels, srcRB, width * srcInfo.bytesPerPixel(), height);
184 return true;
185 }
186
187 /*
188 * Begin section where we try to change colorTypes along the way. Not all combinations
189 * are supported.
190 */
191
192 // Can no longer draw directly into 4444, but we can manually whack it for a few combinations
193 if (kARGB_4444_SkColorType == dstInfo.colorType() &&
194 (kN32_SkColorType == srcInfo.colorType() || kIndex_8_SkColorType == srcInfo.colorType())) {
195 if (srcInfo.alphaType() == kUnpremul_SkAlphaType) {
196 // Our method for converting to 4444 assumes premultiplied.
197 return false;
198 }
199
200 const SkPMColor* table = NULL;
201 if (kIndex_8_SkColorType == srcInfo.colorType()) {
202 if (NULL == ctable) {
203 return false;
204 }
205 table = ctable->lockColors();
206 }
207
208 for (int y = 0; y < height; ++y) {
209 DITHER_4444_SCAN(y);
210 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*)dstPixels;
211 if (table) {
212 const uint8_t* SK_RESTRICT srcRow = (const uint8_t*)srcPixels;
213 for (int x = 0; x < width; ++x) {
214 dstRow[x] = SkDitherARGB32To4444(table[srcRow[x]], DITHER_VALUE(x));
215 }
216 } else {
217 const SkPMColor* SK_RESTRICT srcRow = (const SkPMColor*)srcPixels;
218 for (int x = 0; x < width; ++x) {
219 dstRow[x] = SkDitherARGB32To4444(srcRow[x], DITHER_VALUE(x));
220 }
221 }
222 dstPixels = (char*)dstPixels + dstRB;
223 srcPixels = (const char*)srcPixels + srcRB;
224 }
225
226 if (table) {
227 ctable->unlockColors();
228 }
229 return true;
230 }
231
232 if (dstInfo.alphaType() == kUnpremul_SkAlphaType) {
233 // We do not support drawing to unpremultiplied bitmaps.
234 return false;
235 }
236
237 // Final fall-back, draw with a canvas
238 //
239 // Always clear the dest in case one of the blitters accesses it
240 // TODO: switch the allocation of tmpDst to call sk_calloc_throw
241 {
242 SkBitmap bm;
243 if (!bm.installPixels(srcInfo, const_cast<void*>(srcPixels), srcRB, ctable, NULL, NULL)) {
244 return false;
245 }
246 SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterDirect(dstInfo, dstPixels, dstRB));
247 if (NULL == canvas.get()) {
248 return false;
249 }
250
251 SkPaint paint;
252 paint.setDither(true);
253
254 canvas->clear(0);
255 canvas->drawBitmap(bm, 0, 0, &paint);
256 return true;
257 }
258 }
259
260