OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkConfig8888.h" | 10 #include "SkConfig8888.h" |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 int rowCount) { | 130 int rowCount) { |
131 SkASSERT(bytesPerRow <= srcRB); | 131 SkASSERT(bytesPerRow <= srcRB); |
132 SkASSERT(bytesPerRow <= dstRB); | 132 SkASSERT(bytesPerRow <= dstRB); |
133 for (int i = 0; i < rowCount; ++i) { | 133 for (int i = 0; i < rowCount; ++i) { |
134 memcpy(dst, src, bytesPerRow); | 134 memcpy(dst, src, bytesPerRow); |
135 dst = (char*)dst + dstRB; | 135 dst = (char*)dst + dstRB; |
136 src = (const char*)src + srcRB; | 136 src = (const char*)src + srcRB; |
137 } | 137 } |
138 } | 138 } |
139 | 139 |
| 140 static void copy_g8_to_32(void* dst, size_t dstRB, const void* src, size_t srcRB
, int w, int h) { |
| 141 uint32_t* dst32 = (uint32_t*)dst; |
| 142 const uint8_t* src8 = (const uint8_t*)src; |
| 143 |
| 144 for (int y = 0; y < h; ++y) { |
| 145 for (int x = 0; x < w; ++x) { |
| 146 dst32[x] = SkPackARGB32(0xFF, src8[x], src8[x], src8[x]); |
| 147 } |
| 148 dst32 = (uint32_t*)((char*)dst32 + dstRB); |
| 149 src8 += srcRB; |
| 150 } |
| 151 } |
| 152 |
| 153 static void copy_32_to_g8(void* dst, size_t dstRB, const void* src, size_t srcRB
, |
| 154 const SkImageInfo& srcInfo) { |
| 155 uint8_t* dst8 = (uint8_t*)dst; |
| 156 const uint32_t* src32 = (const uint32_t*)src; |
| 157 |
| 158 const int w = srcInfo.width(); |
| 159 const int h = srcInfo.height(); |
| 160 const bool isBGRA = (kBGRA_8888_SkColorType == srcInfo.colorType()); |
| 161 |
| 162 for (int y = 0; y < h; ++y) { |
| 163 if (isBGRA) { |
| 164 // BGRA |
| 165 for (int x = 0; x < w; ++x) { |
| 166 uint32_t s = src32[x]; |
| 167 dst8[x] = SkComputeLuminance((s >> 16) & 0xFF, (s >> 8) & 0xFF,
s & 0xFF); |
| 168 } |
| 169 } else { |
| 170 // RGBA |
| 171 for (int x = 0; x < w; ++x) { |
| 172 uint32_t s = src32[x]; |
| 173 dst8[x] = SkComputeLuminance(s & 0xFF, (s >> 8) & 0xFF, (s >> 16
) & 0xFF); |
| 174 } |
| 175 } |
| 176 src32 = (const uint32_t*)((const char*)src32 + srcRB); |
| 177 dst8 += dstRB; |
| 178 } |
| 179 } |
| 180 |
140 bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t
dstRB, | 181 bool SkPixelInfo::CopyPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t
dstRB, |
141 const SkImageInfo& srcInfo, const void* srcPixels,
size_t srcRB, | 182 const SkImageInfo& srcInfo, const void* srcPixels,
size_t srcRB, |
142 SkColorTable* ctable) { | 183 SkColorTable* ctable) { |
143 if (srcInfo.dimensions() != dstInfo.dimensions()) { | 184 if (srcInfo.dimensions() != dstInfo.dimensions()) { |
144 return false; | 185 return false; |
145 } | 186 } |
146 | 187 |
147 const int width = srcInfo.width(); | 188 const int width = srcInfo.width(); |
148 const int height = srcInfo.height(); | 189 const int height = srcInfo.height(); |
149 | 190 |
(...skipping 13 matching lines...) Expand all Loading... |
163 | 204 |
164 return srcPI.convertPixelsTo(&dstPI, width, height); | 205 return srcPI.convertPixelsTo(&dstPI, width, height); |
165 } | 206 } |
166 | 207 |
167 // If they agree on colorType and the alphaTypes are compatible, then we jus
t memcpy. | 208 // 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. | 209 // Note: we've already taken care of 32bit colortypes above. |
169 if (srcInfo.colorType() == dstInfo.colorType()) { | 210 if (srcInfo.colorType() == dstInfo.colorType()) { |
170 switch (srcInfo.colorType()) { | 211 switch (srcInfo.colorType()) { |
171 case kRGB_565_SkColorType: | 212 case kRGB_565_SkColorType: |
172 case kAlpha_8_SkColorType: | 213 case kAlpha_8_SkColorType: |
| 214 case kGray_8_SkColorType: |
173 break; | 215 break; |
174 case kIndex_8_SkColorType: | 216 case kIndex_8_SkColorType: |
175 case kARGB_4444_SkColorType: | 217 case kARGB_4444_SkColorType: |
176 if (srcInfo.alphaType() != dstInfo.alphaType()) { | 218 if (srcInfo.alphaType() != dstInfo.alphaType()) { |
177 return false; | 219 return false; |
178 } | 220 } |
179 break; | 221 break; |
180 default: | 222 default: |
181 return false; | 223 return false; |
182 } | 224 } |
183 rect_memcpy(dstPixels, dstRB, srcPixels, srcRB, width * srcInfo.bytesPer
Pixel(), height); | 225 rect_memcpy(dstPixels, dstRB, srcPixels, srcRB, width * srcInfo.bytesPer
Pixel(), height); |
184 return true; | 226 return true; |
185 } | 227 } |
186 | 228 |
187 /* | 229 /* |
188 * Begin section where we try to change colorTypes along the way. Not all c
ombinations | 230 * Begin section where we try to change colorTypes along the way. Not all c
ombinations |
189 * are supported. | 231 * are supported. |
190 */ | 232 */ |
191 | 233 |
| 234 if (kGray_8_SkColorType == srcInfo.colorType() && 4 == dstInfo.bytesPerPixel
()) { |
| 235 copy_g8_to_32(dstPixels, dstRB, srcPixels, srcRB, width, height); |
| 236 return true; |
| 237 } |
| 238 if (kGray_8_SkColorType == dstInfo.colorType() && 4 == srcInfo.bytesPerPixel
()) { |
| 239 copy_32_to_g8(dstPixels, dstRB, srcPixels, srcRB, srcInfo); |
| 240 return true; |
| 241 } |
| 242 |
192 // Can no longer draw directly into 4444, but we can manually whack it for a
few combinations | 243 // Can no longer draw directly into 4444, but we can manually whack it for a
few combinations |
193 if (kARGB_4444_SkColorType == dstInfo.colorType() && | 244 if (kARGB_4444_SkColorType == dstInfo.colorType() && |
194 (kN32_SkColorType == srcInfo.colorType() || kIndex_8_SkColorType == srcI
nfo.colorType())) { | 245 (kN32_SkColorType == srcInfo.colorType() || kIndex_8_SkColorType == srcI
nfo.colorType())) { |
195 if (srcInfo.alphaType() == kUnpremul_SkAlphaType) { | 246 if (srcInfo.alphaType() == kUnpremul_SkAlphaType) { |
196 // Our method for converting to 4444 assumes premultiplied. | 247 // Our method for converting to 4444 assumes premultiplied. |
197 return false; | 248 return false; |
198 } | 249 } |
199 | 250 |
200 const SkPMColor* table = NULL; | 251 const SkPMColor* table = NULL; |
201 if (kIndex_8_SkColorType == srcInfo.colorType()) { | 252 if (kIndex_8_SkColorType == srcInfo.colorType()) { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 | 297 |
247 SkPaint paint; | 298 SkPaint paint; |
248 paint.setDither(true); | 299 paint.setDither(true); |
249 | 300 |
250 canvas->clear(0); | 301 canvas->clear(0); |
251 canvas->drawBitmap(bm, 0, 0, &paint); | 302 canvas->drawBitmap(bm, 0, 0, &paint); |
252 return true; | 303 return true; |
253 } | 304 } |
254 } | 305 } |
255 | 306 |
OLD | NEW |