michael@0: /* michael@0: * Copyright 2007 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkScaledBitmapSampler.h" michael@0: #include "SkBitmap.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkDither.h" michael@0: #include "SkTypes.h" michael@0: michael@0: // 8888 michael@0: michael@0: static bool Sample_Gray_D8888(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkPackARGB32(0xFF, src[0], src[0], src[0]); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_gray_to_8888_proc(const SkImageDecoder& decoder) { michael@0: // Dither, unpremul, and skipZeroes have no effect michael@0: return Sample_Gray_D8888; michael@0: } michael@0: michael@0: static bool Sample_RGBx_D8888(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkPackARGB32(0xFF, src[0], src[1], src[2]); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_RGBx_to_8888_proc(const SkImageDecoder& decoder) { michael@0: // Dither, unpremul, and skipZeroes have no effect michael@0: return Sample_RGBx_D8888; michael@0: } michael@0: michael@0: static bool Sample_RGBA_D8888(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; michael@0: unsigned alphaMask = 0xFF; michael@0: for (int x = 0; x < width; x++) { michael@0: unsigned alpha = src[3]; michael@0: dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); michael@0: src += deltaSrc; michael@0: alphaMask &= alpha; michael@0: } michael@0: return alphaMask != 0xFF; michael@0: } michael@0: michael@0: static bool Sample_RGBA_D8888_Unpremul(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, michael@0: const SkPMColor[]) { michael@0: uint32_t* SK_RESTRICT dst = reinterpret_cast(dstRow); michael@0: unsigned alphaMask = 0xFF; michael@0: for (int x = 0; x < width; x++) { michael@0: unsigned alpha = src[3]; michael@0: dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); michael@0: src += deltaSrc; michael@0: alphaMask &= alpha; michael@0: } michael@0: return alphaMask != 0xFF; michael@0: } michael@0: michael@0: static bool Sample_RGBA_D8888_SkipZ(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, michael@0: const SkPMColor[]) { michael@0: SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; michael@0: unsigned alphaMask = 0xFF; michael@0: for (int x = 0; x < width; x++) { michael@0: unsigned alpha = src[3]; michael@0: if (0 != alpha) { michael@0: dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); michael@0: } michael@0: src += deltaSrc; michael@0: alphaMask &= alpha; michael@0: } michael@0: return alphaMask != 0xFF; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_RGBA_to_8888_proc(const SkImageDecoder& decoder) { michael@0: // Dither has no effect. michael@0: if (decoder.getRequireUnpremultipliedColors()) { michael@0: // We could check each component for a zero, at the expense of extra checks. michael@0: // For now, just return unpremul. michael@0: return Sample_RGBA_D8888_Unpremul; michael@0: } michael@0: // Supply the versions that premultiply the colors michael@0: if (decoder.getSkipWritingZeroes()) { michael@0: return Sample_RGBA_D8888_SkipZ; michael@0: } michael@0: return Sample_RGBA_D8888; michael@0: } michael@0: michael@0: // 565 michael@0: michael@0: static bool Sample_Gray_D565(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkPack888ToRGB16(src[0], src[0], src[0]); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static bool Sample_Gray_D565_D(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int y, const SkPMColor[]) { michael@0: uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; michael@0: DITHER_565_SCAN(y); michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkDitherRGBTo565(src[0], src[0], src[0], DITHER_VALUE(x)); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_gray_to_565_proc(const SkImageDecoder& decoder) { michael@0: // Unpremul and skip zeroes make no difference michael@0: if (decoder.getDitherImage()) { michael@0: return Sample_Gray_D565_D; michael@0: } michael@0: return Sample_Gray_D565; michael@0: } michael@0: michael@0: static bool Sample_RGBx_D565(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkPack888ToRGB16(src[0], src[1], src[2]); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static bool Sample_RGBx_D565_D(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int y, michael@0: const SkPMColor[]) { michael@0: uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; michael@0: DITHER_565_SCAN(y); michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkDitherRGBTo565(src[0], src[1], src[2], DITHER_VALUE(x)); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_RGBx_to_565_proc(const SkImageDecoder& decoder) { michael@0: // Unpremul and skip zeroes make no difference michael@0: if (decoder.getDitherImage()) { michael@0: return Sample_RGBx_D565_D; michael@0: } michael@0: return Sample_RGBx_D565; michael@0: } michael@0: michael@0: michael@0: static bool Sample_D565_D565(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; michael@0: uint16_t* SK_RESTRICT castedSrc = (uint16_t*) src; michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = castedSrc[0]; michael@0: castedSrc += deltaSrc >> 1; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_565_to_565_proc(const SkImageDecoder& decoder) { michael@0: // Unpremul, dither, and skip zeroes have no effect michael@0: return Sample_D565_D565; michael@0: } michael@0: michael@0: // 4444 michael@0: michael@0: static bool Sample_Gray_D4444(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: for (int x = 0; x < width; x++) { michael@0: unsigned gray = src[0] >> 4; michael@0: dst[x] = SkPackARGB4444(0xF, gray, gray, gray); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static bool Sample_Gray_D4444_D(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int y, const SkPMColor[]) { michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: DITHER_4444_SCAN(y); michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkDitherARGB32To4444(0xFF, src[0], src[0], src[0], michael@0: DITHER_VALUE(x)); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_gray_to_4444_proc(const SkImageDecoder& decoder) { michael@0: // Skip zeroes and unpremul make no difference michael@0: if (decoder.getDitherImage()) { michael@0: return Sample_Gray_D4444_D; michael@0: } michael@0: return Sample_Gray_D4444; michael@0: } michael@0: michael@0: static bool Sample_RGBx_D4444(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkPackARGB4444(0xF, src[0] >> 4, src[1] >> 4, src[2] >> 4); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static bool Sample_RGBx_D4444_D(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int y, const SkPMColor[]) { michael@0: SkPMColor16* dst = (SkPMColor16*)dstRow; michael@0: DITHER_4444_SCAN(y); michael@0: michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkDitherARGB32To4444(0xFF, src[0], src[1], src[2], michael@0: DITHER_VALUE(x)); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_RGBx_to_4444_proc(const SkImageDecoder& decoder) { michael@0: // Skip zeroes and unpremul make no difference michael@0: if (decoder.getDitherImage()) { michael@0: return Sample_RGBx_D4444_D; michael@0: } michael@0: return Sample_RGBx_D4444; michael@0: } michael@0: michael@0: static bool Sample_RGBA_D4444(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: unsigned alphaMask = 0xFF; michael@0: michael@0: for (int x = 0; x < width; x++) { michael@0: unsigned alpha = src[3]; michael@0: SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); michael@0: dst[x] = SkPixel32ToPixel4444(c); michael@0: src += deltaSrc; michael@0: alphaMask &= alpha; michael@0: } michael@0: return alphaMask != 0xFF; michael@0: } michael@0: michael@0: static bool Sample_RGBA_D4444_SkipZ(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, michael@0: const SkPMColor[]) { michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: unsigned alphaMask = 0xFF; michael@0: michael@0: for (int x = 0; x < width; x++) { michael@0: unsigned alpha = src[3]; michael@0: if (alpha != 0) { michael@0: SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); michael@0: dst[x] = SkPixel32ToPixel4444(c); michael@0: } michael@0: src += deltaSrc; michael@0: alphaMask &= alpha; michael@0: } michael@0: return alphaMask != 0xFF; michael@0: } michael@0: michael@0: michael@0: static bool Sample_RGBA_D4444_D(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int y, michael@0: const SkPMColor[]) { michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: unsigned alphaMask = 0xFF; michael@0: DITHER_4444_SCAN(y); michael@0: michael@0: for (int x = 0; x < width; x++) { michael@0: unsigned alpha = src[3]; michael@0: SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); michael@0: dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x)); michael@0: src += deltaSrc; michael@0: alphaMask &= alpha; michael@0: } michael@0: return alphaMask != 0xFF; michael@0: } michael@0: michael@0: static bool Sample_RGBA_D4444_D_SkipZ(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int y, michael@0: const SkPMColor[]) { michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: unsigned alphaMask = 0xFF; michael@0: DITHER_4444_SCAN(y); michael@0: michael@0: for (int x = 0; x < width; x++) { michael@0: unsigned alpha = src[3]; michael@0: if (alpha != 0) { michael@0: SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); michael@0: dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x)); michael@0: } michael@0: src += deltaSrc; michael@0: alphaMask &= alpha; michael@0: } michael@0: return alphaMask != 0xFF; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_RGBA_to_4444_proc(const SkImageDecoder& decoder) { michael@0: if (decoder.getRequireUnpremultipliedColors()) { michael@0: // Unpremultiplied is not supported for 4444 michael@0: return NULL; michael@0: } michael@0: const bool dither = decoder.getDitherImage(); michael@0: if (decoder.getSkipWritingZeroes()) { michael@0: if (dither) { michael@0: return Sample_RGBA_D4444_D_SkipZ; michael@0: } michael@0: return Sample_RGBA_D4444_SkipZ; michael@0: } michael@0: if (dither) { michael@0: return Sample_RGBA_D4444_D; michael@0: } michael@0: return Sample_RGBA_D4444; michael@0: } michael@0: michael@0: // Index michael@0: michael@0: #define A32_MASK_IN_PLACE (SkPMColor)(SK_A32_MASK << SK_A32_SHIFT) michael@0: michael@0: static bool Sample_Index_D8888(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor ctable[]) { michael@0: michael@0: SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; michael@0: SkPMColor cc = A32_MASK_IN_PLACE; michael@0: for (int x = 0; x < width; x++) { michael@0: SkPMColor c = ctable[*src]; michael@0: cc &= c; michael@0: dst[x] = c; michael@0: src += deltaSrc; michael@0: } michael@0: return cc != A32_MASK_IN_PLACE; michael@0: } michael@0: michael@0: static bool Sample_Index_D8888_SkipZ(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, michael@0: const SkPMColor ctable[]) { michael@0: michael@0: SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; michael@0: SkPMColor cc = A32_MASK_IN_PLACE; michael@0: for (int x = 0; x < width; x++) { michael@0: SkPMColor c = ctable[*src]; michael@0: cc &= c; michael@0: if (c != 0) { michael@0: dst[x] = c; michael@0: } michael@0: src += deltaSrc; michael@0: } michael@0: return cc != A32_MASK_IN_PLACE; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_index_to_8888_proc(const SkImageDecoder& decoder) { michael@0: if (decoder.getRequireUnpremultipliedColors()) { michael@0: // Unpremultiplied is not supported for an index source. michael@0: return NULL; michael@0: } michael@0: // Dither makes no difference michael@0: if (decoder.getSkipWritingZeroes()) { michael@0: return Sample_Index_D8888_SkipZ; michael@0: } michael@0: return Sample_Index_D8888; michael@0: } michael@0: michael@0: static bool Sample_Index_D565(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor ctable[]) { michael@0: michael@0: uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = SkPixel32ToPixel16(ctable[*src]); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static bool Sample_Index_D565_D(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, int width, michael@0: int deltaSrc, int y, const SkPMColor ctable[]) { michael@0: michael@0: uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; michael@0: DITHER_565_SCAN(y); michael@0: michael@0: for (int x = 0; x < width; x++) { michael@0: SkPMColor c = ctable[*src]; michael@0: dst[x] = SkDitherRGBTo565(SkGetPackedR32(c), SkGetPackedG32(c), michael@0: SkGetPackedB32(c), DITHER_VALUE(x)); michael@0: src += deltaSrc; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_index_to_565_proc(const SkImageDecoder& decoder) { michael@0: // Unpremultiplied and skip zeroes make no difference michael@0: if (decoder.getDitherImage()) { michael@0: return Sample_Index_D565_D; michael@0: } michael@0: return Sample_Index_D565; michael@0: } michael@0: michael@0: static bool Sample_Index_D4444(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, int width, michael@0: int deltaSrc, int y, const SkPMColor ctable[]) { michael@0: michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: SkPMColor cc = A32_MASK_IN_PLACE; michael@0: for (int x = 0; x < width; x++) { michael@0: SkPMColor c = ctable[*src]; michael@0: cc &= c; michael@0: dst[x] = SkPixel32ToPixel4444(c); michael@0: src += deltaSrc; michael@0: } michael@0: return cc != A32_MASK_IN_PLACE; michael@0: } michael@0: michael@0: static bool Sample_Index_D4444_D(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, int width, michael@0: int deltaSrc, int y, const SkPMColor ctable[]) { michael@0: michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: SkPMColor cc = A32_MASK_IN_PLACE; michael@0: DITHER_4444_SCAN(y); michael@0: michael@0: for (int x = 0; x < width; x++) { michael@0: SkPMColor c = ctable[*src]; michael@0: cc &= c; michael@0: dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x)); michael@0: src += deltaSrc; michael@0: } michael@0: return cc != A32_MASK_IN_PLACE; michael@0: } michael@0: michael@0: static bool Sample_Index_D4444_SkipZ(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, int width, michael@0: int deltaSrc, int y, const SkPMColor ctable[]) { michael@0: michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: SkPMColor cc = A32_MASK_IN_PLACE; michael@0: for (int x = 0; x < width; x++) { michael@0: SkPMColor c = ctable[*src]; michael@0: cc &= c; michael@0: if (c != 0) { michael@0: dst[x] = SkPixel32ToPixel4444(c); michael@0: } michael@0: src += deltaSrc; michael@0: } michael@0: return cc != A32_MASK_IN_PLACE; michael@0: } michael@0: michael@0: static bool Sample_Index_D4444_D_SkipZ(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, int width, michael@0: int deltaSrc, int y, const SkPMColor ctable[]) { michael@0: michael@0: SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; michael@0: SkPMColor cc = A32_MASK_IN_PLACE; michael@0: DITHER_4444_SCAN(y); michael@0: michael@0: for (int x = 0; x < width; x++) { michael@0: SkPMColor c = ctable[*src]; michael@0: cc &= c; michael@0: if (c != 0) { michael@0: dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x)); michael@0: } michael@0: src += deltaSrc; michael@0: } michael@0: return cc != A32_MASK_IN_PLACE; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_index_to_4444_proc(const SkImageDecoder& decoder) { michael@0: // Unpremul not allowed michael@0: if (decoder.getRequireUnpremultipliedColors()) { michael@0: return NULL; michael@0: } michael@0: const bool dither = decoder.getDitherImage(); michael@0: if (decoder.getSkipWritingZeroes()) { michael@0: if (dither) { michael@0: return Sample_Index_D4444_D_SkipZ; michael@0: } michael@0: return Sample_Index_D4444_SkipZ; michael@0: } michael@0: if (dither) { michael@0: return Sample_Index_D4444_D; michael@0: } michael@0: return Sample_Index_D4444; michael@0: } michael@0: michael@0: static bool Sample_Index_DI(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, const SkPMColor[]) { michael@0: if (1 == deltaSrc) { michael@0: memcpy(dstRow, src, width); michael@0: } else { michael@0: uint8_t* SK_RESTRICT dst = (uint8_t*)dstRow; michael@0: for (int x = 0; x < width; x++) { michael@0: dst[x] = src[0]; michael@0: src += deltaSrc; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_index_to_index_proc(const SkImageDecoder& decoder) { michael@0: // Unpremul not allowed michael@0: if (decoder.getRequireUnpremultipliedColors()) { michael@0: return NULL; michael@0: } michael@0: // Ignore dither and skip zeroes michael@0: return Sample_Index_DI; michael@0: } michael@0: michael@0: // A8 michael@0: static bool Sample_Gray_DA8(void* SK_RESTRICT dstRow, michael@0: const uint8_t* SK_RESTRICT src, michael@0: int width, int deltaSrc, int, michael@0: const SkPMColor[]) { michael@0: // Sampling Gray to A8 uses the same function as Index to Index8, michael@0: // except we assume that there is alpha for speed, since an A8 michael@0: // bitmap with no alpha is not interesting. michael@0: (void) Sample_Index_DI(dstRow, src, width, deltaSrc, /* y unused */ 0, michael@0: /* ctable unused */ NULL); michael@0: return true; michael@0: } michael@0: michael@0: static SkScaledBitmapSampler::RowProc get_gray_to_A8_proc(const SkImageDecoder& decoder) { michael@0: if (decoder.getRequireUnpremultipliedColors()) { michael@0: return NULL; michael@0: } michael@0: // Ignore skip and dither. michael@0: return Sample_Gray_DA8; michael@0: } michael@0: michael@0: typedef SkScaledBitmapSampler::RowProc (*RowProcChooser)(const SkImageDecoder& decoder); michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkScaledBitmapSampler.h" michael@0: michael@0: SkScaledBitmapSampler::SkScaledBitmapSampler(int width, int height, michael@0: int sampleSize) { michael@0: fCTable = NULL; michael@0: fDstRow = NULL; michael@0: fRowProc = NULL; michael@0: michael@0: if (width <= 0 || height <= 0) { michael@0: sk_throw(); michael@0: } michael@0: michael@0: SkDEBUGCODE(fSampleMode = kUninitialized_SampleMode); michael@0: michael@0: if (sampleSize <= 1) { michael@0: fScaledWidth = width; michael@0: fScaledHeight = height; michael@0: fX0 = fY0 = 0; michael@0: fDX = fDY = 1; michael@0: return; michael@0: } michael@0: michael@0: int dx = SkMin32(sampleSize, width); michael@0: int dy = SkMin32(sampleSize, height); michael@0: michael@0: fScaledWidth = width / dx; michael@0: fScaledHeight = height / dy; michael@0: michael@0: SkASSERT(fScaledWidth > 0); michael@0: SkASSERT(fScaledHeight > 0); michael@0: michael@0: fX0 = dx >> 1; michael@0: fY0 = dy >> 1; michael@0: michael@0: SkASSERT(fX0 >= 0 && fX0 < width); michael@0: SkASSERT(fY0 >= 0 && fY0 < height); michael@0: michael@0: fDX = dx; michael@0: fDY = dy; michael@0: michael@0: SkASSERT(fDX > 0 && (fX0 + fDX * (fScaledWidth - 1)) < width); michael@0: SkASSERT(fDY > 0 && (fY0 + fDY * (fScaledHeight - 1)) < height); michael@0: } michael@0: michael@0: bool SkScaledBitmapSampler::begin(SkBitmap* dst, SrcConfig sc, michael@0: const SkImageDecoder& decoder, michael@0: const SkPMColor ctable[]) { michael@0: static const RowProcChooser gProcChoosers[] = { michael@0: get_gray_to_8888_proc, michael@0: get_RGBx_to_8888_proc, michael@0: get_RGBA_to_8888_proc, michael@0: get_index_to_8888_proc, michael@0: NULL, // 565 to 8888 michael@0: michael@0: get_gray_to_565_proc, michael@0: get_RGBx_to_565_proc, michael@0: get_RGBx_to_565_proc, // The source alpha will be ignored. michael@0: get_index_to_565_proc, michael@0: get_565_to_565_proc, michael@0: michael@0: get_gray_to_4444_proc, michael@0: get_RGBx_to_4444_proc, michael@0: get_RGBA_to_4444_proc, michael@0: get_index_to_4444_proc, michael@0: NULL, // 565 to 4444 michael@0: michael@0: NULL, // gray to index michael@0: NULL, // rgbx to index michael@0: NULL, // rgba to index michael@0: get_index_to_index_proc, michael@0: NULL, // 565 to index michael@0: michael@0: get_gray_to_A8_proc, michael@0: NULL, // rgbx to a8 michael@0: NULL, // rgba to a8 michael@0: NULL, // index to a8 michael@0: NULL, // 565 to a8 michael@0: }; michael@0: michael@0: // The jump between dst configs in the table michael@0: static const int gProcDstConfigSpan = 5; michael@0: SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gProcChoosers) == 5 * gProcDstConfigSpan, michael@0: gProcs_has_the_wrong_number_of_entries); michael@0: michael@0: fCTable = ctable; michael@0: michael@0: int index = 0; michael@0: switch (sc) { michael@0: case SkScaledBitmapSampler::kGray: michael@0: fSrcPixelSize = 1; michael@0: index += 0; michael@0: break; michael@0: case SkScaledBitmapSampler::kRGB: michael@0: fSrcPixelSize = 3; michael@0: index += 1; michael@0: break; michael@0: case SkScaledBitmapSampler::kRGBX: michael@0: fSrcPixelSize = 4; michael@0: index += 1; michael@0: break; michael@0: case SkScaledBitmapSampler::kRGBA: michael@0: fSrcPixelSize = 4; michael@0: index += 2; michael@0: break; michael@0: case SkScaledBitmapSampler::kIndex: michael@0: fSrcPixelSize = 1; michael@0: index += 3; michael@0: break; michael@0: case SkScaledBitmapSampler::kRGB_565: michael@0: fSrcPixelSize = 2; michael@0: index += 4; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: switch (dst->config()) { michael@0: case SkBitmap::kARGB_8888_Config: michael@0: index += 0 * gProcDstConfigSpan; michael@0: break; michael@0: case SkBitmap::kRGB_565_Config: michael@0: index += 1 * gProcDstConfigSpan; michael@0: break; michael@0: case SkBitmap::kARGB_4444_Config: michael@0: index += 2 * gProcDstConfigSpan; michael@0: break; michael@0: case SkBitmap::kIndex8_Config: michael@0: index += 3 * gProcDstConfigSpan; michael@0: break; michael@0: case SkBitmap::kA8_Config: michael@0: index += 4 * gProcDstConfigSpan; michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: michael@0: RowProcChooser chooser = gProcChoosers[index]; michael@0: if (NULL == chooser) { michael@0: fRowProc = NULL; michael@0: } else { michael@0: fRowProc = chooser(decoder); michael@0: } michael@0: fDstRow = (char*)dst->getPixels(); michael@0: fDstRowBytes = dst->rowBytes(); michael@0: fCurrY = 0; michael@0: return fRowProc != NULL; michael@0: } michael@0: michael@0: bool SkScaledBitmapSampler::next(const uint8_t* SK_RESTRICT src) { michael@0: SkASSERT(kInterlaced_SampleMode != fSampleMode); michael@0: SkDEBUGCODE(fSampleMode = kConsecutive_SampleMode); michael@0: SkASSERT((unsigned)fCurrY < (unsigned)fScaledHeight); michael@0: michael@0: bool hadAlpha = fRowProc(fDstRow, src + fX0 * fSrcPixelSize, fScaledWidth, michael@0: fDX * fSrcPixelSize, fCurrY, fCTable); michael@0: fDstRow += fDstRowBytes; michael@0: fCurrY += 1; michael@0: return hadAlpha; michael@0: } michael@0: michael@0: bool SkScaledBitmapSampler::sampleInterlaced(const uint8_t* SK_RESTRICT src, int srcY) { michael@0: SkASSERT(kConsecutive_SampleMode != fSampleMode); michael@0: SkDEBUGCODE(fSampleMode = kInterlaced_SampleMode); michael@0: // Any line that should be a part of the destination can be created by the formula: michael@0: // fY0 + (some multiplier) * fDY michael@0: // so if srcY - fY0 is not an integer multiple of fDY that srcY will be skipped. michael@0: const int srcYMinusY0 = srcY - fY0; michael@0: if (srcYMinusY0 % fDY != 0) { michael@0: // This line is not part of the output, so return false for alpha, since we have michael@0: // not added an alpha to the output. michael@0: return false; michael@0: } michael@0: // Unlike in next(), where the data is used sequentially, this function skips around, michael@0: // so fDstRow and fCurrY are never updated. fDstRow must always be the starting point michael@0: // of the destination bitmap's pixels, which is used to calculate the destination row michael@0: // each time this function is called. michael@0: const int dstY = srcYMinusY0 / fDY; michael@0: SkASSERT(dstY < fScaledHeight); michael@0: char* dstRow = fDstRow + dstY * fDstRowBytes; michael@0: return fRowProc(dstRow, src + fX0 * fSrcPixelSize, fScaledWidth, michael@0: fDX * fSrcPixelSize, dstY, fCTable); michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: // The following code is for a test to ensure that changing the method to get the right row proc michael@0: // did not change the row proc unintentionally. Tested by ImageDecodingTest.cpp michael@0: michael@0: // friend of SkScaledBitmapSampler solely for the purpose of accessing fRowProc. michael@0: class RowProcTester { michael@0: public: michael@0: static SkScaledBitmapSampler::RowProc getRowProc(const SkScaledBitmapSampler& sampler) { michael@0: return sampler.fRowProc; michael@0: } michael@0: }; michael@0: michael@0: michael@0: // Table showing the expected RowProc for each combination of inputs. michael@0: // Table formated as follows: michael@0: // Each group of 5 consecutive rows represents sampling from a single michael@0: // SkScaledBitmapSampler::SrcConfig. michael@0: // Within each set, each row represents a different destination SkBitmap::Config michael@0: // Each column represents a different combination of dither and unpremul. michael@0: // D = dither ~D = no dither michael@0: // U = unpremul ~U = no unpremul michael@0: // ~D~U D~U ~DU DU michael@0: SkScaledBitmapSampler::RowProc gTestProcs[] = { michael@0: // Gray michael@0: Sample_Gray_DA8, Sample_Gray_DA8, NULL, NULL, // to A8 michael@0: NULL, NULL, NULL, NULL, // to Index8 michael@0: Sample_Gray_D565, Sample_Gray_D565_D, Sample_Gray_D565, Sample_Gray_D565_D, // to 565 michael@0: Sample_Gray_D4444, Sample_Gray_D4444_D, Sample_Gray_D4444, Sample_Gray_D4444_D, // to 4444 michael@0: Sample_Gray_D8888, Sample_Gray_D8888, Sample_Gray_D8888, Sample_Gray_D8888, // to 8888 michael@0: // Index michael@0: NULL, NULL, NULL, NULL, // to A8 michael@0: Sample_Index_DI, Sample_Index_DI, NULL, NULL, // to Index8 michael@0: Sample_Index_D565, Sample_Index_D565_D, Sample_Index_D565, Sample_Index_D565_D, // to 565 michael@0: Sample_Index_D4444, Sample_Index_D4444_D, NULL, NULL, // to 4444 michael@0: Sample_Index_D8888, Sample_Index_D8888, NULL, NULL, // to 8888 michael@0: // RGB michael@0: NULL, NULL, NULL, NULL, // to A8 michael@0: NULL, NULL, NULL, NULL, // to Index8 michael@0: Sample_RGBx_D565, Sample_RGBx_D565_D, Sample_RGBx_D565, Sample_RGBx_D565_D, // to 565 michael@0: Sample_RGBx_D4444, Sample_RGBx_D4444_D, Sample_RGBx_D4444, Sample_RGBx_D4444_D, // to 4444 michael@0: Sample_RGBx_D8888, Sample_RGBx_D8888, Sample_RGBx_D8888, Sample_RGBx_D8888, // to 8888 michael@0: // RGBx is the same as RGB michael@0: NULL, NULL, NULL, NULL, // to A8 michael@0: NULL, NULL, NULL, NULL, // to Index8 michael@0: Sample_RGBx_D565, Sample_RGBx_D565_D, Sample_RGBx_D565, Sample_RGBx_D565_D, // to 565 michael@0: Sample_RGBx_D4444, Sample_RGBx_D4444_D, Sample_RGBx_D4444, Sample_RGBx_D4444_D, // to 4444 michael@0: Sample_RGBx_D8888, Sample_RGBx_D8888, Sample_RGBx_D8888, Sample_RGBx_D8888, // to 8888 michael@0: // RGBA michael@0: NULL, NULL, NULL, NULL, // to A8 michael@0: NULL, NULL, NULL, NULL, // to Index8 michael@0: Sample_RGBx_D565, Sample_RGBx_D565_D, Sample_RGBx_D565, Sample_RGBx_D565_D, // to 565 michael@0: Sample_RGBA_D4444, Sample_RGBA_D4444_D, NULL, NULL, // to 4444 michael@0: Sample_RGBA_D8888, Sample_RGBA_D8888, Sample_RGBA_D8888_Unpremul, Sample_RGBA_D8888_Unpremul, // to 8888 michael@0: // RGB_565 michael@0: NULL, NULL, NULL, NULL, // to A8 michael@0: NULL, NULL, NULL, NULL, // to Index8 michael@0: Sample_D565_D565, Sample_D565_D565, Sample_D565_D565, Sample_D565_D565, // to 565 michael@0: NULL, NULL, NULL, NULL, // to 4444 michael@0: NULL, NULL, NULL, NULL, // to 8888 michael@0: }; michael@0: michael@0: // Dummy class that allows instantiation of an ImageDecoder, so begin can query its fields. michael@0: class DummyDecoder : public SkImageDecoder { michael@0: public: michael@0: DummyDecoder() {} michael@0: protected: michael@0: virtual bool onDecode(SkStream*, SkBitmap*, SkImageDecoder::Mode) SK_OVERRIDE { michael@0: return false; michael@0: } michael@0: }; michael@0: michael@0: void test_row_proc_choice(); michael@0: void test_row_proc_choice() { michael@0: SkBitmap dummyBitmap; michael@0: DummyDecoder dummyDecoder; michael@0: size_t procCounter = 0; michael@0: for (int sc = SkScaledBitmapSampler::kGray; sc <= SkScaledBitmapSampler::kRGB_565; ++sc) { michael@0: for (int c = SkBitmap::kA8_Config; c <= SkBitmap::kARGB_8888_Config; ++c) { michael@0: for (int unpremul = 0; unpremul <= 1; ++unpremul) { michael@0: for (int dither = 0; dither <= 1; ++dither) { michael@0: // Arbitrary width/height/sampleSize to allow SkScaledBitmapSampler to michael@0: // be considered valid. michael@0: SkScaledBitmapSampler sampler(10, 10, 1); michael@0: dummyBitmap.setConfig((SkBitmap::Config) c, 10, 10); michael@0: dummyDecoder.setDitherImage(SkToBool(dither)); michael@0: dummyDecoder.setRequireUnpremultipliedColors(SkToBool(unpremul)); michael@0: sampler.begin(&dummyBitmap, (SkScaledBitmapSampler::SrcConfig) sc, michael@0: dummyDecoder); michael@0: SkScaledBitmapSampler::RowProc expected = gTestProcs[procCounter]; michael@0: SkScaledBitmapSampler::RowProc actual = RowProcTester::getRowProc(sampler); michael@0: SkASSERT(expected == actual); michael@0: procCounter++; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: SkASSERT(SK_ARRAY_COUNT(gTestProcs) == procCounter); michael@0: } michael@0: #endif // SK_DEBUG