Chromium Code Reviews| Index: src/codec/SkSwizzler.cpp |
| diff --git a/src/codec/SkSwizzler.cpp b/src/codec/SkSwizzler.cpp |
| index 71cb82a08bc5803fd6f8877044b452a3d1591cd3..8726158a71c1bcc4d3f962194125032266472699 100644 |
| --- a/src/codec/SkSwizzler.cpp |
| +++ b/src/codec/SkSwizzler.cpp |
| @@ -7,6 +7,7 @@ |
| #include "SkCodecPriv.h" |
| #include "SkColorPriv.h" |
| +#include "SkScaledCodec.h" |
| #include "SkSwizzler.h" |
| #include "SkTemplates.h" |
| #include "SkUtils.h" |
| @@ -19,6 +20,16 @@ SkSwizzler::ResultAlpha SkSwizzler::GetResult(uint8_t zeroAlpha, |
| return (((uint16_t) maxAlpha) << 8) | zeroAlpha; |
| } |
| +// samples the row. Does not do anything else but sampling |
| +static SkSwizzler::ResultAlpha sample(void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, |
| + int width, int deltaSrc, int y, const SkPMColor ctable[]){ |
| + uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow; |
| + for (int x = 0; x < width; x++) { |
| + dst[x] = src[0]; |
| + src += deltaSrc; |
| + } |
| + return 0xFF; |
|
scroggo
2015/07/31 13:35:33
Since this is opaque, what you want is to return 0
emmaleer
2015/07/31 18:41:56
I don't understand what you mean by "will compute
scroggo
2015/07/31 19:31:58
No, I mean the expression that is passed to the ma
emmaleer
2015/07/31 20:43:09
Acknowledged.
|
| +} |
| // kIndex1, kIndex2, kIndex4 |
| static SkSwizzler::ResultAlpha swizzle_small_index_to_index( |
| @@ -72,10 +83,17 @@ static SkSwizzler::ResultAlpha swizzle_small_index_to_n32( |
| static SkSwizzler::ResultAlpha swizzle_index_to_index( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow; |
| - memcpy(dst, src, width); |
| + if (1 == deltaSrc) { |
| + memcpy(dst, src, width); |
| + } else { |
| + for (int x = 0; x < width; x++) { |
| + dst[x] = src[0]; |
| + src += deltaSrc; |
| + } |
| + } |
| // TODO (msarett): Should we skip the loop here and guess that the row is opaque/not opaque? |
| // SkScaledBitmap sampler just guesses that it is opaque. This is dangerous |
| // and probably wrong since gif and bmp (rarely) may have alpha. |
| @@ -88,30 +106,32 @@ static SkSwizzler::ResultAlpha swizzle_index_to_index( |
| static SkSwizzler::ResultAlpha swizzle_index_to_n32( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| INIT_RESULT_ALPHA; |
| for (int x = 0; x < width; x++) { |
| - SkPMColor c = ctable[src[x]]; |
| + SkPMColor c = ctable[*src]; |
| UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); |
| dst[x] = c; |
| + src += deltaSrc; |
| } |
| return COMPUTE_RESULT_ALPHA; |
| } |
| static SkSwizzler::ResultAlpha swizzle_index_to_n32_skipZ( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| INIT_RESULT_ALPHA; |
| for (int x = 0; x < width; x++) { |
| - SkPMColor c = ctable[src[x]]; |
| + SkPMColor c = ctable[*src]; |
| UPDATE_RESULT_ALPHA(c >> SK_A32_SHIFT); |
| if (c != 0) { |
| dst[x] = c; |
| } |
| + src += deltaSrc; |
| } |
| return COMPUTE_RESULT_ALPHA; |
| } |
| @@ -122,19 +142,29 @@ static SkSwizzler::ResultAlpha swizzle_index_to_n32_skipZ( |
| static SkSwizzler::ResultAlpha swizzle_gray_to_n32( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| for (int x = 0; x < width; x++) { |
| - dst[x] = SkPackARGB32NoCheck(0xFF, src[x], src[x], src[x]); |
| + dst[x] = SkPackARGB32NoCheck(0xFF, *src, *src, *src); |
| + src += deltaSrc; |
| } |
| return SkSwizzler::kOpaque_ResultAlpha; |
| } |
| static SkSwizzler::ResultAlpha swizzle_gray_to_gray( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| - memcpy(dstRow, src, width); |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| + |
| + uint8_t* SK_RESTRICT dst = (uint8_t*) dstRow; |
| + if (1 == deltaSrc) { |
| + memcpy(dstRow, src, width); |
| + } else { |
| + for (int x = 0; x < width; x++) { |
| + dst[x] = src[0]; |
| + src += deltaSrc; |
| + } |
| + } |
| return SkSwizzler::kOpaque_ResultAlpha; |
| } |
| @@ -142,12 +172,12 @@ static SkSwizzler::ResultAlpha swizzle_gray_to_gray( |
| static SkSwizzler::ResultAlpha swizzle_bgrx_to_n32( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| for (int x = 0; x < width; x++) { |
| dst[x] = SkPackARGB32NoCheck(0xFF, src[2], src[1], src[0]); |
| - src += bytesPerPixel; |
| + src += deltaSrc; |
| } |
| return SkSwizzler::kOpaque_ResultAlpha; |
| } |
| @@ -156,7 +186,7 @@ static SkSwizzler::ResultAlpha swizzle_bgrx_to_n32( |
| static SkSwizzler::ResultAlpha swizzle_bgra_to_n32_unpremul( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| INIT_RESULT_ALPHA; |
| @@ -164,14 +194,14 @@ static SkSwizzler::ResultAlpha swizzle_bgra_to_n32_unpremul( |
| uint8_t alpha = src[3]; |
| UPDATE_RESULT_ALPHA(alpha); |
| dst[x] = SkPackARGB32NoCheck(alpha, src[2], src[1], src[0]); |
| - src += bytesPerPixel; |
| + src += deltaSrc; |
| } |
| return COMPUTE_RESULT_ALPHA; |
| } |
| static SkSwizzler::ResultAlpha swizzle_bgra_to_n32_premul( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| INIT_RESULT_ALPHA; |
| @@ -179,7 +209,7 @@ static SkSwizzler::ResultAlpha swizzle_bgra_to_n32_premul( |
| uint8_t alpha = src[3]; |
| UPDATE_RESULT_ALPHA(alpha); |
| dst[x] = SkPreMultiplyARGB(alpha, src[2], src[1], src[0]); |
| - src += bytesPerPixel; |
| + src += deltaSrc; |
| } |
| return COMPUTE_RESULT_ALPHA; |
| } |
| @@ -187,19 +217,19 @@ static SkSwizzler::ResultAlpha swizzle_bgra_to_n32_premul( |
| // n32 |
| static SkSwizzler::ResultAlpha swizzle_rgbx_to_n32( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| for (int x = 0; x < width; x++) { |
| dst[x] = SkPackARGB32(0xFF, src[0], src[1], src[2]); |
| - src += bytesPerPixel; |
| + src += deltaSrc; |
| } |
| return SkSwizzler::kOpaque_ResultAlpha; |
| } |
| static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_premul( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| INIT_RESULT_ALPHA; |
| @@ -207,14 +237,14 @@ static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_premul( |
| unsigned alpha = src[3]; |
| UPDATE_RESULT_ALPHA(alpha); |
| dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); |
| - src += bytesPerPixel; |
| + src += deltaSrc; |
| } |
| return COMPUTE_RESULT_ALPHA; |
| } |
| static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_unpremul( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow); |
| INIT_RESULT_ALPHA; |
| @@ -222,14 +252,14 @@ static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_unpremul( |
| unsigned alpha = src[3]; |
| UPDATE_RESULT_ALPHA(alpha); |
| dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); |
| - src += bytesPerPixel; |
| + src += deltaSrc; |
| } |
| return COMPUTE_RESULT_ALPHA; |
| } |
| static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_premul_skipZ( |
| void* SK_RESTRICT dstRow, const uint8_t* SK_RESTRICT src, int width, |
| - int bytesPerPixel, int y, const SkPMColor ctable[]) { |
| + int deltaSrc, int y, const SkPMColor ctable[]) { |
| SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| INIT_RESULT_ALPHA; |
| @@ -239,7 +269,7 @@ static SkSwizzler::ResultAlpha swizzle_rgba_to_n32_premul_skipZ( |
| if (0 != alpha) { |
| dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); |
| } |
| - src += bytesPerPixel; |
| + src += deltaSrc; |
| } |
| return COMPUTE_RESULT_ALPHA; |
| } |
| @@ -273,25 +303,28 @@ static bool swizzle_rgba_to_n32_unpremul_skipZ(void* SK_RESTRICT dstRow, |
| SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| const SkPMColor* ctable, |
| - const SkImageInfo& info, void* dst, |
| + const SkImageInfo& dstInfo, void* dst, |
| size_t dstRowBytes, |
| - SkCodec::ZeroInitialized zeroInit) { |
| - if (info.colorType() == kUnknown_SkColorType || kUnknown == sc) { |
| + SkCodec::ZeroInitialized zeroInit, |
| + int srcWidth) { |
| + if (dstInfo.colorType() == kUnknown_SkColorType || kUnknown == sc) { |
| return NULL; |
| } |
| - if (info.minRowBytes() > dstRowBytes) { |
| + if (dstInfo.minRowBytes() > dstRowBytes) { |
| return NULL; |
| } |
| if ((kIndex == sc || kIndex4 == sc || kIndex2 == sc || kIndex1 == sc) |
| && NULL == ctable) { |
| return NULL; |
| } |
| + |
| RowProc proc = NULL; |
| + |
| switch (sc) { |
| case kIndex1: |
| case kIndex2: |
| case kIndex4: |
| - switch (info.colorType()) { |
| + switch (dstInfo.colorType()) { |
| case kN32_SkColorType: |
| proc = &swizzle_small_index_to_n32; |
| break; |
| @@ -303,7 +336,7 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| } |
| break; |
| case kIndex: |
| - switch (info.colorType()) { |
| + switch (dstInfo.colorType()) { |
| case kN32_SkColorType: |
| // We assume the color premultiplied ctable (or not) as desired. |
| if (SkCodec::kYes_ZeroInitialized == zeroInit) { |
| @@ -322,7 +355,7 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| } |
| break; |
| case kGray: |
| - switch (info.colorType()) { |
| + switch (dstInfo.colorType()) { |
| case kN32_SkColorType: |
| proc = &swizzle_gray_to_n32; |
| break; |
| @@ -334,7 +367,7 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| break; |
| case kBGR: |
| case kBGRX: |
| - switch (info.colorType()) { |
| + switch (dstInfo.colorType()) { |
| case kN32_SkColorType: |
| proc = &swizzle_bgrx_to_n32; |
| break; |
| @@ -343,9 +376,9 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| } |
| break; |
| case kBGRA: |
| - switch (info.colorType()) { |
| + switch (dstInfo.colorType()) { |
| case kN32_SkColorType: |
| - switch (info.alphaType()) { |
| + switch (dstInfo.alphaType()) { |
| case kUnpremul_SkAlphaType: |
| proc = &swizzle_bgra_to_n32_unpremul; |
| break; |
| @@ -362,7 +395,7 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| break; |
| case kRGBX: |
| // TODO: Support other swizzles. |
| - switch (info.colorType()) { |
| + switch (dstInfo.colorType()) { |
| case kN32_SkColorType: |
| proc = &swizzle_rgbx_to_n32; |
| break; |
| @@ -371,9 +404,9 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| } |
| break; |
| case kRGBA: |
| - switch (info.colorType()) { |
| + switch (dstInfo.colorType()) { |
| case kN32_SkColorType: |
| - if (info.alphaType() == kUnpremul_SkAlphaType) { |
| + if (dstInfo.alphaType() == kUnpremul_SkAlphaType) { |
| // Respect zeroInit? |
| proc = &swizzle_rgba_to_n32_unpremul; |
| } else { |
| @@ -389,7 +422,7 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| } |
| break; |
| case kRGB: |
| - switch (info.colorType()) { |
| + switch (dstInfo.colorType()) { |
| case kN32_SkColorType: |
| proc = &swizzle_rgbx_to_n32; |
| break; |
| @@ -397,6 +430,14 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| break; |
| } |
| break; |
| + case kRGB_565: |
| + switch (dstInfo.colorType()) { |
| + case kRGB_565_SkColorType: |
| + proc = &sample; |
| + break; |
| + default: |
| + break; |
| + } |
| default: |
| break; |
| } |
| @@ -407,13 +448,16 @@ SkSwizzler* SkSwizzler::CreateSwizzler(SkSwizzler::SrcConfig sc, |
| // Store deltaSrc in bytes if it is an even multiple, otherwise use bits |
| int deltaSrc = SkIsAlign8(BitsPerPixel(sc)) ? BytesPerPixel(sc) : |
| BitsPerPixel(sc); |
| - return SkNEW_ARGS(SkSwizzler, (proc, ctable, deltaSrc, info, dst, |
| - dstRowBytes)); |
| + |
| + int sampleX = SkScaledCodec::get_sample_size(srcWidth, dstInfo.width()); |
| + |
| + return SkNEW_ARGS(SkSwizzler, (proc, ctable, deltaSrc, dstInfo, dst, |
| + dstRowBytes, sampleX)); |
| } |
| SkSwizzler::SkSwizzler(RowProc proc, const SkPMColor* ctable, |
| int deltaSrc, const SkImageInfo& info, void* dst, |
| - size_t rowBytes) |
| + size_t rowBytes, int sampleX) |
| : fRowProc(proc) |
| , fColorTable(ctable) |
| , fDeltaSrc(deltaSrc) |
| @@ -421,7 +465,11 @@ SkSwizzler::SkSwizzler(RowProc proc, const SkPMColor* ctable, |
| , fDstRow(dst) |
| , fDstRowBytes(rowBytes) |
| , fCurrY(0) |
| + , fSampleX(sampleX) |
| + , fX0(sampleX == 1 ? 0 : sampleX >> 1) |
| { |
| + // check that fX0 is less than original width |
| + SkASSERT(fX0 >= 0 && fX0 < fDstInfo.width() * fSampleX); |
| SkDEBUGCODE(fNextMode = kUninitialized_NextMode); |
| } |
| @@ -432,8 +480,8 @@ SkSwizzler::ResultAlpha SkSwizzler::next(const uint8_t* SK_RESTRICT src) { |
| SkDEBUGCODE(fNextMode = kConsecutive_NextMode); |
| // Decode a row |
| - const ResultAlpha result = fRowProc(fDstRow, src, fDstInfo.width(), |
| - fDeltaSrc, fCurrY, fColorTable); |
| + const ResultAlpha result = fRowProc(fDstRow, src + fX0 * fDeltaSrc, fDstInfo.width(), |
| + fSampleX * fDeltaSrc, fCurrY, fColorTable); |
| // Move to the next row and return the result |
| fCurrY++; |
| @@ -451,7 +499,7 @@ SkSwizzler::ResultAlpha SkSwizzler::next(const uint8_t* SK_RESTRICT src, |
| void* row = SkTAddOffset<void>(fDstRow, y*fDstRowBytes); |
| // Decode the row |
| - return fRowProc(row, src, fDstInfo.width(), fDeltaSrc, fCurrY, |
| + return fRowProc(row, src + fX0 * fDeltaSrc, fDstInfo.width(), fSampleX * fDeltaSrc, fCurrY, |
| fColorTable); |
| } |