| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkCodecPriv.h" | 8 #include "SkCodecPriv.h" |
| 9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
| 10 #include "SkSwizzler.h" | 10 #include "SkSwizzler.h" |
| 11 #include "SkTemplates.h" | 11 #include "SkTemplates.h" |
| 12 #include "SkUtils.h" |
| 12 | 13 |
| 13 SkSwizzler::ResultAlpha SkSwizzler::GetResult(uint8_t zeroAlpha, | 14 SkSwizzler::ResultAlpha SkSwizzler::GetResult(uint8_t zeroAlpha, |
| 14 uint8_t maxAlpha) { | 15 uint8_t maxAlpha) { |
| 15 // In the transparent case, this returns 0x0000 | 16 // In the transparent case, this returns 0x0000 |
| 16 // In the opaque case, this returns 0xFFFF | 17 // In the opaque case, this returns 0xFFFF |
| 17 // If the row is neither transparent nor opaque, returns something else | 18 // If the row is neither transparent nor opaque, returns something else |
| 18 return (((uint16_t) maxAlpha) << 8) | zeroAlpha; | 19 return (((uint16_t) maxAlpha) << 8) | zeroAlpha; |
| 19 } | 20 } |
| 20 | 21 |
| 21 // kIndex1, kIndex2, kIndex4 | 22 // kIndex1, kIndex2, kIndex4 |
| 22 | 23 |
| 23 static SkSwizzler::ResultAlpha swizzle_small_index_to_n32( | 24 static SkSwizzler::ResultAlpha swizzle_small_index_to_index( |
| 24 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | 25 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| 25 int bitsPerPixel, int y, const SkPMColor ctable[]) { | 26 int bitsPerPixel, int y, const SkPMColor ctable[]) { |
| 26 | 27 |
| 27 SkPMColor* SK_RESTRICT dst = (SkPMColor*) dstRow; | 28 uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow; |
| 28 INIT_RESULT_ALPHA; | 29 INIT_RESULT_ALPHA; |
| 29 const uint32_t pixelsPerByte = 8 / bitsPerPixel; | 30 const uint32_t pixelsPerByte = 8 / bitsPerPixel; |
| 30 const size_t rowBytes = compute_row_bytes_ppb(width, pixelsPerByte); | 31 const size_t rowBytes = compute_row_bytes_ppb(width, pixelsPerByte); |
| 31 const uint8_t mask = (1 << bitsPerPixel) - 1; | 32 const uint8_t mask = (1 << bitsPerPixel) - 1; |
| 32 int x = 0; | 33 int x = 0; |
| 33 for (uint32_t byte = 0; byte < rowBytes; byte++) { | 34 for (uint32_t byte = 0; byte < rowBytes; byte++) { |
| 34 uint8_t pixelData = src[byte]; | 35 uint8_t pixelData = src[byte]; |
| 35 for (uint32_t p = 0; p < pixelsPerByte && x < width; p++) { | 36 for (uint32_t p = 0; p < pixelsPerByte && x < width; p++) { |
| 36 uint8_t index = (pixelData >> (8 - bitsPerPixel)) & mask; | 37 uint8_t index = (pixelData >> (8 - bitsPerPixel)) & mask; |
| 37 SkPMColor c = ctable[index]; | 38 UPDATE_RESULT_ALPHA(ctable[index] >> SK_A32_SHIFT); |
| 38 UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); | 39 dst[x] = index; |
| 39 dst[x] = c; | |
| 40 pixelData <<= bitsPerPixel; | 40 pixelData <<= bitsPerPixel; |
| 41 x++; | 41 x++; |
| 42 } | 42 } |
| 43 } | 43 } |
| 44 return COMPUTE_RESULT_ALPHA; | 44 return COMPUTE_RESULT_ALPHA; |
| 45 } | 45 } |
| 46 | 46 |
| 47 static SkSwizzler::ResultAlpha swizzle_small_index_to_565( | 47 static SkSwizzler::ResultAlpha swizzle_small_index_to_n32( |
| 48 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | 48 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| 49 int bitsPerPixel, int y, const SkPMColor ctable[]) { | 49 int bitsPerPixel, int y, const SkPMColor ctable[]) { |
| 50 | 50 |
| 51 uint16_t* SK_RESTRICT dst = (uint16_t*) dstRow; | 51 SkPMColor* SK_RESTRICT dst = (SkPMColor*) dstRow; |
| 52 INIT_RESULT_ALPHA; |
| 52 const uint32_t pixelsPerByte = 8 / bitsPerPixel; | 53 const uint32_t pixelsPerByte = 8 / bitsPerPixel; |
| 53 const size_t rowBytes = compute_row_bytes_ppb(width, pixelsPerByte); | 54 const size_t rowBytes = compute_row_bytes_ppb(width, pixelsPerByte); |
| 54 const uint8_t mask = (1 << bitsPerPixel) - 1; | 55 const uint8_t mask = (1 << bitsPerPixel) - 1; |
| 55 int x = 0; | 56 int x = 0; |
| 56 for (uint32_t byte = 0; byte < rowBytes; byte++) { | 57 for (uint32_t byte = 0; byte < rowBytes; byte++) { |
| 57 uint8_t pixelData = src[byte]; | 58 uint8_t pixelData = src[byte]; |
| 58 for (uint32_t p = 0; p < pixelsPerByte && x < width; p++) { | 59 for (uint32_t p = 0; p < pixelsPerByte && x < width; p++) { |
| 59 uint8_t index = (pixelData >> (8 - bitsPerPixel)) & mask; | 60 uint8_t index = (pixelData >> (8 - bitsPerPixel)) & mask; |
| 60 uint16_t c = SkPixel32ToPixel16(ctable[index]); | 61 SkPMColor c = ctable[index]; |
| 62 UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); |
| 61 dst[x] = c; | 63 dst[x] = c; |
| 62 pixelData <<= bitsPerPixel; | 64 pixelData <<= bitsPerPixel; |
| 63 x++; | 65 x++; |
| 64 } | 66 } |
| 65 } | 67 } |
| 66 return SkSwizzler::kOpaque_ResultAlpha; | 68 return COMPUTE_RESULT_ALPHA; |
| 69 } |
| 70 |
| 71 static SkSwizzler::ResultAlpha swizzle_index_to_index( |
| 72 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| 73 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| 74 |
| 75 uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow; |
| 76 memcpy(dst, src, width); |
| 77 // TODO (msarett): Should we skip the loop here and guess that the row is op
aque/not opaque? |
| 78 // SkScaledBitmap sampler just guesses that it is opaque. T
his is dangerous |
| 79 // and probably wrong since gif and bmp (rarely) may have al
pha. |
| 80 INIT_RESULT_ALPHA; |
| 81 for (int x = 0; x < width; x++) { |
| 82 UPDATE_RESULT_ALPHA(ctable[src[x]] >> SK_A32_SHIFT); |
| 83 } |
| 84 return COMPUTE_RESULT_ALPHA; |
| 67 } | 85 } |
| 68 | 86 |
| 69 // kIndex | 87 // kIndex |
| 70 | 88 |
| 71 static SkSwizzler::ResultAlpha swizzle_index_to_n32( | 89 static SkSwizzler::ResultAlpha swizzle_index_to_n32( |
| 72 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | 90 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| 73 int bytesPerPixel, int y, const SkPMColor ctable[]) { | 91 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| 74 | 92 |
| 75 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 93 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 76 INIT_RESULT_ALPHA; | 94 INIT_RESULT_ALPHA; |
| 77 for (int x = 0; x < width; x++) { | 95 for (int x = 0; x < width; x++) { |
| 78 SkPMColor c = ctable[*src]; | 96 SkPMColor c = ctable[src[x]]; |
| 79 UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); | 97 UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); |
| 80 dst[x] = c; | 98 dst[x] = c; |
| 81 src++; | |
| 82 } | 99 } |
| 83 return COMPUTE_RESULT_ALPHA; | 100 return COMPUTE_RESULT_ALPHA; |
| 84 } | 101 } |
| 85 | 102 |
| 86 static SkSwizzler::ResultAlpha swizzle_index_to_n32_skipZ( | 103 static SkSwizzler::ResultAlpha swizzle_index_to_n32_skipZ( |
| 87 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | 104 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| 88 int bytesPerPixel, int y, const SkPMColor ctable[]) { | 105 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| 89 | 106 |
| 90 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 107 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 91 INIT_RESULT_ALPHA; | 108 INIT_RESULT_ALPHA; |
| 92 for (int x = 0; x < width; x++) { | 109 for (int x = 0; x < width; x++) { |
| 93 SkPMColor c = ctable[*src]; | 110 SkPMColor c = ctable[src[x]]; |
| 94 UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); | 111 UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); |
| 95 if (c != 0) { | 112 if (c != 0) { |
| 96 dst[x] = c; | 113 dst[x] = c; |
| 97 } | 114 } |
| 98 src++; | |
| 99 } | 115 } |
| 100 return COMPUTE_RESULT_ALPHA; | 116 return COMPUTE_RESULT_ALPHA; |
| 101 } | 117 } |
| 102 | 118 |
| 103 static SkSwizzler::ResultAlpha swizzle_index_to_565( | |
| 104 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | |
| 105 int bytesPerPixel, int y, const SkPMColor ctable[]) { | |
| 106 | |
| 107 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; | |
| 108 for (int x = 0; x < width; x++) { | |
| 109 uint16_t c = SkPixel32ToPixel16(ctable[*src]); | |
| 110 dst[x] = c; | |
| 111 src++; | |
| 112 } | |
| 113 return SkSwizzler::kOpaque_ResultAlpha; | |
| 114 } | |
| 115 | |
| 116 #undef A32_MASK_IN_PLACE | 119 #undef A32_MASK_IN_PLACE |
| 117 | 120 |
| 118 static SkSwizzler::ResultAlpha swizzle_bgrx_to_n32( | 121 static SkSwizzler::ResultAlpha swizzle_bgrx_to_n32( |
| 119 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | 122 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| 120 int bytesPerPixel, int y, const SkPMColor ctable[]) { | 123 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| 121 | 124 |
| 122 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 125 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 123 for (int x = 0; x < width; x++) { | 126 for (int x = 0; x < width; x++) { |
| 124 dst[x] = SkPackARGB32NoCheck(0xFF, src[2], src[1], src[0]); | 127 dst[x] = SkPackARGB32NoCheck(0xFF, src[2], src[1], src[0]); |
| 125 src += bytesPerPixel; | 128 src += bytesPerPixel; |
| 126 } | 129 } |
| 127 return SkSwizzler::kOpaque_ResultAlpha; | 130 return SkSwizzler::kOpaque_ResultAlpha; |
| 128 } | 131 } |
| 129 | 132 |
| 130 static SkSwizzler::ResultAlpha swizzle_bgrx_to_565( | |
| 131 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | |
| 132 int bytesPerPixel, int y, const SkPMColor ctable[]) { | |
| 133 | |
| 134 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; | |
| 135 for (int x = 0; x < width; x++) { | |
| 136 dst[x] = SkPack888ToRGB16(src[2], src[1], src[0]); | |
| 137 src += bytesPerPixel; | |
| 138 } | |
| 139 return SkSwizzler::kOpaque_ResultAlpha; | |
| 140 } | |
| 141 | |
| 142 // kBGRA | 133 // kBGRA |
| 143 | 134 |
| 144 static SkSwizzler::ResultAlpha swizzle_bgra_to_n32_unpremul( | 135 static SkSwizzler::ResultAlpha swizzle_bgra_to_n32_unpremul( |
| 145 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, | 136 void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| 146 int bytesPerPixel, int y, const SkPMColor ctable[]) { | 137 int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| 147 | 138 |
| 148 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; | 139 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 149 INIT_RESULT_ALPHA; | 140 INIT_RESULT_ALPHA; |
| 150 for (int x = 0; x < width; x++) { | 141 for (int x = 0; x < width; x++) { |
| 151 uint8_t alpha = src[3]; | 142 uint8_t alpha = src[3]; |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 } | 266 } |
| 276 RowProc proc = NULL; | 267 RowProc proc = NULL; |
| 277 switch (sc) { | 268 switch (sc) { |
| 278 case kIndex1: | 269 case kIndex1: |
| 279 case kIndex2: | 270 case kIndex2: |
| 280 case kIndex4: | 271 case kIndex4: |
| 281 switch (info.colorType()) { | 272 switch (info.colorType()) { |
| 282 case kN32_SkColorType: | 273 case kN32_SkColorType: |
| 283 proc = &swizzle_small_index_to_n32; | 274 proc = &swizzle_small_index_to_n32; |
| 284 break; | 275 break; |
| 285 case kRGB_565_SkColorType: | 276 case kIndex_8_SkColorType: |
| 286 proc = &swizzle_small_index_to_565; | 277 proc = &swizzle_small_index_to_index; |
| 287 break; | 278 break; |
| 288 default: | 279 default: |
| 289 break; | 280 break; |
| 290 } | 281 } |
| 291 break; | 282 break; |
| 292 case kIndex: | 283 case kIndex: |
| 293 switch (info.colorType()) { | 284 switch (info.colorType()) { |
| 294 case kN32_SkColorType: | 285 case kN32_SkColorType: |
| 295 // We assume the color premultiplied ctable (or not) as desi
red. | 286 // We assume the color premultiplied ctable (or not) as desi
red. |
| 296 if (SkImageGenerator::kYes_ZeroInitialized == zeroInit) { | 287 if (SkImageGenerator::kYes_ZeroInitialized == zeroInit) { |
| 297 proc = &swizzle_index_to_n32_skipZ; | 288 proc = &swizzle_index_to_n32_skipZ; |
| 298 break; | 289 break; |
| 299 } else { | 290 } else { |
| 300 proc = &swizzle_index_to_n32; | 291 proc = &swizzle_index_to_n32; |
| 301 break; | 292 break; |
| 302 } | 293 } |
| 303 break; | 294 break; |
| 304 case kRGB_565_SkColorType: | 295 case kIndex_8_SkColorType: |
| 305 proc = &swizzle_index_to_565; | 296 proc = &swizzle_index_to_index; |
| 306 break; | 297 break; |
| 307 default: | 298 default: |
| 308 break; | 299 break; |
| 309 } | 300 } |
| 310 break; | 301 break; |
| 311 case kBGR: | 302 case kBGR: |
| 312 case kBGRX: | 303 case kBGRX: |
| 313 switch (info.colorType()) { | 304 switch (info.colorType()) { |
| 314 case kN32_SkColorType: | 305 case kN32_SkColorType: |
| 315 proc = &swizzle_bgrx_to_n32; | 306 proc = &swizzle_bgrx_to_n32; |
| 316 break; | 307 break; |
| 317 case kRGB_565_SkColorType: | |
| 318 proc = &swizzle_bgrx_to_565; | |
| 319 break; | |
| 320 default: | 308 default: |
| 321 break; | 309 break; |
| 322 } | 310 } |
| 323 break; | 311 break; |
| 324 case kBGRA: | 312 case kBGRA: |
| 325 switch (info.colorType()) { | 313 switch (info.colorType()) { |
| 326 case kN32_SkColorType: | 314 case kN32_SkColorType: |
| 327 switch (info.alphaType()) { | 315 switch (info.alphaType()) { |
| 328 case kUnpremul_SkAlphaType: | 316 case kUnpremul_SkAlphaType: |
| 329 proc = &swizzle_bgra_to_n32_unpremul; | 317 proc = &swizzle_bgra_to_n32_unpremul; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 SkASSERT(kConsecutive_NextMode != fNextMode); | 414 SkASSERT(kConsecutive_NextMode != fNextMode); |
| 427 SkDEBUGCODE(fNextMode = kDesignateRow_NextMode); | 415 SkDEBUGCODE(fNextMode = kDesignateRow_NextMode); |
| 428 | 416 |
| 429 // Choose the row | 417 // Choose the row |
| 430 void* row = SkTAddOffset<void>(fDstRow, y*fDstRowBytes); | 418 void* row = SkTAddOffset<void>(fDstRow, y*fDstRowBytes); |
| 431 | 419 |
| 432 // Decode the row | 420 // Decode the row |
| 433 return fRowProc(row, src, fDstInfo.width(), fDeltaSrc, fCurrY, | 421 return fRowProc(row, src, fDstInfo.width(), fDeltaSrc, fCurrY, |
| 434 fColorTable); | 422 fColorTable); |
| 435 } | 423 } |
| 424 |
| 425 void SkSwizzler::Fill(void* dst, const SkImageInfo& dstInfo, size_t dstRowBytes,
uint32_t y, |
| 426 uint32_t colorOrIndex, SkPMColor* colorTable) { |
| 427 SkASSERT(dst != NULL); |
| 428 SkASSERT(y < (uint32_t) dstInfo.height()); |
| 429 |
| 430 // Get dst start row |
| 431 void* dstRow = SkTAddOffset<void*>(dst, y * dstRowBytes); |
| 432 |
| 433 // Calculate remaining bytes. This is tricky since the final row may not be
padded. |
| 434 const size_t totalBytes = dstInfo.getSafeSize(dstRowBytes); |
| 435 const size_t remainingBytes = totalBytes - y * dstRowBytes; |
| 436 |
| 437 // Use the proper memset routine to fill the remaining bytes |
| 438 switch(dstInfo.colorType()) { |
| 439 case kN32_SkColorType: |
| 440 // Assume input is an index if we have a color table |
| 441 uint32_t color; |
| 442 if (NULL != colorTable) { |
| 443 SkASSERT(colorOrIndex == (uint8_t) colorOrIndex); |
| 444 color = colorTable[colorOrIndex]; |
| 445 // Otherwise, assume the input is a color |
| 446 } else { |
| 447 color = colorOrIndex; |
| 448 } |
| 449 |
| 450 // We must fill row by row in the case of unaligned row bytes |
| 451 if (SkIsAlign4((size_t) dstRow) && SkIsAlign4(dstRowBytes)) { |
| 452 sk_memset32((uint32_t*) dstRow, color, |
| 453 (uint32_t) remainingBytes / sizeof(SkPMColor)); |
| 454 } else { |
| 455 // This is an unlikely, slow case |
| 456 SkCodecPrintf("Warning: Strange number of row bytes, fill will b
e slow.\n"); |
| 457 for (int32_t row = y; row < dstInfo.height(); row++) { |
| 458 uint32_t* dstPtr = (uint32_t*) dstRow; |
| 459 for (int32_t col = 0; col < dstInfo.width(); col++) { |
| 460 dstPtr[col] = color; |
| 461 } |
| 462 dstRow = SkTAddOffset<void*>(dstRow, dstRowBytes); |
| 463 } |
| 464 } |
| 465 break; |
| 466 // On an index destination color type, always assume the input is an ind
ex |
| 467 case kIndex_8_SkColorType: |
| 468 SkASSERT(colorOrIndex == (uint8_t) colorOrIndex); |
| 469 memset(dstRow, colorOrIndex, remainingBytes); |
| 470 break; |
| 471 default: |
| 472 SkCodecPrintf("Error: Unsupported dst color type for fill(). Doing
nothing.\n"); |
| 473 SkASSERT(false); |
| 474 break; |
| 475 } |
| 476 } |
| OLD | NEW |