OLD | NEW |
---|---|
1 #include "SkBitmap.h" | |
scroggo
2014/07/10 15:09:48
Could you add the copyright header as well?
reed1
2014/07/10 15:32:16
Done.
| |
2 #include "SkCanvas.h" | |
1 #include "SkConfig8888.h" | 3 #include "SkConfig8888.h" |
2 #include "SkColorPriv.h" | 4 #include "SkColorPriv.h" |
5 #include "SkDither.h" | |
3 #include "SkMathPriv.h" | 6 #include "SkMathPriv.h" |
4 #include "SkUnPreMultiply.h" | 7 #include "SkUnPreMultiply.h" |
5 | 8 |
6 enum AlphaVerb { | 9 enum AlphaVerb { |
7 kNothing_AlphaVerb, | 10 kNothing_AlphaVerb, |
8 kPremul_AlphaVerb, | 11 kPremul_AlphaVerb, |
9 kUnpremul_AlphaVerb, | 12 kUnpremul_AlphaVerb, |
10 }; | 13 }; |
11 | 14 |
12 template <bool doSwapRB, AlphaVerb doAlpha> uint32_t convert32(uint32_t c) { | 15 template <bool doSwapRB, AlphaVerb doAlpha> uint32_t convert32(uint32_t c) { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 const uint32_t* srcP = static_cast<const uint32_t*>(fPixels); | 111 const uint32_t* srcP = static_cast<const uint32_t*>(fPixels); |
109 size_t srcInc = fRowBytes >> 2; | 112 size_t srcInc = fRowBytes >> 2; |
110 size_t dstInc = dst->fRowBytes >> 2; | 113 size_t dstInc = dst->fRowBytes >> 2; |
111 for (int y = 0; y < height; ++y) { | 114 for (int y = 0; y < height; ++y) { |
112 proc(dstP, srcP, width); | 115 proc(dstP, srcP, width); |
113 dstP += dstInc; | 116 dstP += dstInc; |
114 srcP += srcInc; | 117 srcP += srcInc; |
115 } | 118 } |
116 return true; | 119 return true; |
117 } | 120 } |
121 | |
122 static void rect_memcpy(void* dst, size_t dstRB, const void* src, size_t srcRB, size_t bytesPerRow, | |
123 int rowCount) { | |
124 SkASSERT(bytesPerRow <= srcRB); | |
125 SkASSERT(bytesPerRow <= dstRB); | |
126 for (int i = 0; i < rowCount; ++i) { | |
127 memcpy(dst, src, bytesPerRow); | |
128 dst = (char*)dst + dstRB; | |
129 src = (const char*)src + srcRB; | |
130 } | |
131 } | |
132 | |
133 bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRB, | |
134 const SkImageInfo& srcInfo, const void* srcPixels, size_t srcRB, | |
135 SkColorTable* ctable) { | |
136 if (srcInfo.dimensions() != dstInfo.dimensions()) { | |
137 return false; | |
138 } | |
139 | |
140 const int width = srcInfo.width(); | |
141 const int height = srcInfo.height(); | |
142 | |
143 // Handle fancy alpha swizzling if both are ARGB32 | |
144 if (4 == srcInfo.bytesPerPixel() && 4 == dstInfo.bytesPerPixel()) { | |
145 SkDstPixelInfo dstPI; | |
146 dstPI.fColorType = dstInfo.colorType(); | |
147 dstPI.fAlphaType = dstInfo.alphaType(); | |
148 dstPI.fPixels = dstPixels; | |
149 dstPI.fRowBytes = dstRB; | |
150 | |
151 SkSrcPixelInfo srcPI; | |
152 srcPI.fColorType = srcInfo.colorType(); | |
153 srcPI.fAlphaType = srcInfo.alphaType(); | |
154 srcPI.fPixels = srcPixels; | |
155 srcPI.fRowBytes = srcRB; | |
156 | |
157 return srcPI.convertPixelsTo(&dstPI, width, height); | |
158 } | |
159 | |
160 // If they agree on colorType, then we ignore alphaType and just memcpy. | |
scroggo
2014/07/10 15:09:48
I'm not sure this is the correct behavior (it's al
reed1
2014/07/10 15:32:16
This is what we did in the old code for both copyT
| |
161 // Note: we've already taken care of 32bit colortypes above. | |
162 if (srcInfo.colorType() == dstInfo.colorType()) { | |
163 switch (srcInfo.colorType()) { | |
164 case kRGB_565_SkColorType: | |
165 case kAlpha_8_SkColorType: | |
166 case kIndex_8_SkColorType: | |
167 break; | |
168 case kARGB_4444_SkColorType: | |
169 if (srcInfo.alphaType() != dstInfo.alphaType()) { | |
170 return false; | |
171 } | |
172 break; | |
173 default: | |
174 return false; | |
175 } | |
176 rect_memcpy(dstPixels, dstRB, srcPixels, srcRB, width * srcInfo.bytesPer Pixel(), height); | |
177 return true; | |
178 } | |
179 | |
180 /* | |
181 * Begin section where we try to change colorTypes along the way. Not all c ombinations | |
182 * are supported. | |
183 */ | |
184 | |
185 // Can no longer draw directly into 4444, but we can manually wack it for a few combinations | |
scroggo
2014/07/10 15:09:48
whack*
reed1
2014/07/10 15:32:16
Done.
| |
186 if (kARGB_4444_SkColorType == dstInfo.colorType() && | |
187 (kN32_SkColorType == srcInfo.colorType() || kIndex_8_SkColorType == srcI nfo.colorType())) { | |
188 if (srcInfo.alphaType() == kUnpremul_SkAlphaType) { | |
189 // Our method for converting to 4444 assumes premultiplied. | |
190 return false; | |
191 } | |
192 | |
193 const SkPMColor* table = NULL; | |
194 if (kIndex_8_SkColorType == srcInfo.colorType()) { | |
195 if (NULL == ctable) { | |
196 return false; | |
197 } | |
198 table = ctable->lockColors(); | |
199 } | |
200 | |
201 for (int y = 0; y < height; ++y) { | |
202 DITHER_4444_SCAN(y); | |
203 SkPMColor16* SK_RESTRICT dstRow = (SkPMColor16*)dstPixels; | |
204 if (table) { | |
205 const uint8_t* SK_RESTRICT srcRow = (const uint8_t*)srcPixels; | |
206 for (int x = 0; x < width; ++x) { | |
207 dstRow[x] = SkDitherARGB32To4444(table[srcRow[x]], DITHER_VA LUE(x)); | |
208 } | |
209 } else { | |
210 const SkPMColor* SK_RESTRICT srcRow = (const SkPMColor*)srcPixel s; | |
211 for (int x = 0; x < width; ++x) { | |
212 dstRow[x] = SkDitherARGB32To4444(srcRow[x], DITHER_VALUE(x)) ; | |
213 } | |
214 } | |
215 dstPixels = (char*)dstPixels + dstRB; | |
216 srcPixels = (const char*)srcPixels + srcRB; | |
217 } | |
218 | |
219 if (table) { | |
220 ctable->unlockColors(); | |
221 } | |
222 return true; | |
223 } | |
224 | |
225 if (dstInfo.alphaType() == kUnpremul_SkAlphaType) { | |
226 // We do not support drawing to unpremultiplied bitmaps. | |
227 return false; | |
228 } | |
229 | |
230 // Final fall-back, draw with a canvas | |
231 // | |
232 // Always clear the dest in case one of the blitters accesses it | |
233 // TODO: switch the allocation of tmpDst to call sk_calloc_throw | |
234 { | |
235 SkBitmap bm; | |
236 if (!bm.installPixels(srcInfo, const_cast<void*>(srcPixels), srcRB, ctab le, NULL, NULL)) { | |
237 return false; | |
238 } | |
239 SkAutoTUnref<SkCanvas> canvas(SkCanvas::NewRasterDirect(dstInfo, dstPixe ls, dstRB)); | |
240 if (NULL == canvas.get()) { | |
241 return false; | |
242 } | |
243 | |
244 SkPaint paint; | |
245 paint.setDither(true); | |
246 | |
247 canvas->clear(0); | |
248 canvas->drawBitmap(bm, 0, 0, &paint); | |
249 return true; | |
250 } | |
251 } | |
252 | |
OLD | NEW |