OLD | NEW |
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" | 1 #include "SkConfig8888.h" |
11 #include "SkColorPriv.h" | 2 #include "SkColorPriv.h" |
12 #include "SkDither.h" | |
13 #include "SkMathPriv.h" | 3 #include "SkMathPriv.h" |
14 #include "SkUnPreMultiply.h" | 4 #include "SkUnPreMultiply.h" |
15 | 5 |
16 enum AlphaVerb { | 6 enum AlphaVerb { |
17 kNothing_AlphaVerb, | 7 kNothing_AlphaVerb, |
18 kPremul_AlphaVerb, | 8 kPremul_AlphaVerb, |
19 kUnpremul_AlphaVerb, | 9 kUnpremul_AlphaVerb, |
20 }; | 10 }; |
21 | 11 |
22 template <bool doSwapRB, AlphaVerb doAlpha> uint32_t convert32(uint32_t c) { | 12 template <bool doSwapRB, AlphaVerb doAlpha> uint32_t convert32(uint32_t c) { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 const uint32_t* srcP = static_cast<const uint32_t*>(fPixels); | 108 const uint32_t* srcP = static_cast<const uint32_t*>(fPixels); |
119 size_t srcInc = fRowBytes >> 2; | 109 size_t srcInc = fRowBytes >> 2; |
120 size_t dstInc = dst->fRowBytes >> 2; | 110 size_t dstInc = dst->fRowBytes >> 2; |
121 for (int y = 0; y < height; ++y) { | 111 for (int y = 0; y < height; ++y) { |
122 proc(dstP, srcP, width); | 112 proc(dstP, srcP, width); |
123 dstP += dstInc; | 113 dstP += dstInc; |
124 srcP += srcInc; | 114 srcP += srcInc; |
125 } | 115 } |
126 return true; | 116 return true; |
127 } | 117 } |
128 | |
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 | |
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 jus
t 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.bytesPer
Pixel(), height); | |
184 return true; | |
185 } | |
186 | |
187 /* | |
188 * Begin section where we try to change colorTypes along the way. Not all c
ombinations | |
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 == srcI
nfo.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_VA
LUE(x)); | |
215 } | |
216 } else { | |
217 const SkPMColor* SK_RESTRICT srcRow = (const SkPMColor*)srcPixel
s; | |
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, ctab
le, NULL, NULL)) { | |
244 return false; | |
245 } | |
246 SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterDirect(dstInfo, dstPixe
ls, 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 | |
OLD | NEW |