michael@0: michael@0: /* michael@0: * Copyright 2012 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: * Functions to transform scanlines between packed-pixel formats. michael@0: */ michael@0: michael@0: #include "SkBitmap.h" michael@0: #include "SkColor.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkPreConfig.h" michael@0: #include "SkUnPreMultiply.h" michael@0: michael@0: /** michael@0: * Function template for transforming scanlines. michael@0: * Transform 'width' pixels from 'src' buffer into 'dst' buffer, michael@0: * repacking color channel data as appropriate for the given transformation. michael@0: */ michael@0: typedef void (*transform_scanline_proc)(const char* SK_RESTRICT src, michael@0: int width, char* SK_RESTRICT dst); michael@0: michael@0: /** michael@0: * Identity transformation: just copy bytes from src to dst. michael@0: */ michael@0: static void transform_scanline_memcpy(const char* SK_RESTRICT src, int width, michael@0: char* SK_RESTRICT dst) { michael@0: memcpy(dst, src, width); michael@0: } michael@0: michael@0: /** michael@0: * Transform from kRGB_565_Config to 3-bytes-per-pixel RGB. michael@0: * Alpha channel data is not present in kRGB_565_Config format, so there is no michael@0: * alpha channel data to preserve. michael@0: */ michael@0: static void transform_scanline_565(const char* SK_RESTRICT src, int width, michael@0: char* SK_RESTRICT dst) { michael@0: const uint16_t* SK_RESTRICT srcP = (const uint16_t*)src; michael@0: for (int i = 0; i < width; i++) { michael@0: unsigned c = *srcP++; michael@0: *dst++ = SkPacked16ToR32(c); michael@0: *dst++ = SkPacked16ToG32(c); michael@0: *dst++ = SkPacked16ToB32(c); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Transform from kARGB_8888_Config to 3-bytes-per-pixel RGB. michael@0: * Alpha channel data, if any, is abandoned. michael@0: */ michael@0: static void transform_scanline_888(const char* SK_RESTRICT src, int width, michael@0: char* SK_RESTRICT dst) { michael@0: const SkPMColor* SK_RESTRICT srcP = (const SkPMColor*)src; michael@0: for (int i = 0; i < width; i++) { michael@0: SkPMColor c = *srcP++; michael@0: *dst++ = SkGetPackedR32(c); michael@0: *dst++ = SkGetPackedG32(c); michael@0: *dst++ = SkGetPackedB32(c); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Transform from kARGB_4444_Config to 3-bytes-per-pixel RGB. michael@0: * Alpha channel data, if any, is abandoned. michael@0: */ michael@0: static void transform_scanline_444(const char* SK_RESTRICT src, int width, michael@0: char* SK_RESTRICT dst) { michael@0: const SkPMColor16* SK_RESTRICT srcP = (const SkPMColor16*)src; michael@0: for (int i = 0; i < width; i++) { michael@0: SkPMColor16 c = *srcP++; michael@0: *dst++ = SkPacked4444ToR32(c); michael@0: *dst++ = SkPacked4444ToG32(c); michael@0: *dst++ = SkPacked4444ToB32(c); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Transform from kARGB_8888_Config to 4-bytes-per-pixel RGBA. michael@0: * (This would be the identity transformation, except for byte-order and michael@0: * scaling of RGB based on alpha channel). michael@0: */ michael@0: static void transform_scanline_8888(const char* SK_RESTRICT src, int width, michael@0: char* SK_RESTRICT dst) { michael@0: const SkPMColor* SK_RESTRICT srcP = (const SkPMColor*)src; michael@0: const SkUnPreMultiply::Scale* SK_RESTRICT table = michael@0: SkUnPreMultiply::GetScaleTable(); michael@0: michael@0: for (int i = 0; i < width; i++) { michael@0: SkPMColor c = *srcP++; michael@0: unsigned a = SkGetPackedA32(c); michael@0: unsigned r = SkGetPackedR32(c); michael@0: unsigned g = SkGetPackedG32(c); michael@0: unsigned b = SkGetPackedB32(c); michael@0: michael@0: if (0 != a && 255 != a) { michael@0: SkUnPreMultiply::Scale scale = table[a]; michael@0: r = SkUnPreMultiply::ApplyScale(scale, r); michael@0: g = SkUnPreMultiply::ApplyScale(scale, g); michael@0: b = SkUnPreMultiply::ApplyScale(scale, b); michael@0: } michael@0: *dst++ = r; michael@0: *dst++ = g; michael@0: *dst++ = b; michael@0: *dst++ = a; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Transform from kARGB_8888_Config to 4-bytes-per-pixel RGBA, michael@0: * with scaling of RGB based on alpha channel. michael@0: */ michael@0: static void transform_scanline_4444(const char* SK_RESTRICT src, int width, michael@0: char* SK_RESTRICT dst) { michael@0: const SkPMColor16* SK_RESTRICT srcP = (const SkPMColor16*)src; michael@0: const SkUnPreMultiply::Scale* SK_RESTRICT table = michael@0: SkUnPreMultiply::GetScaleTable(); michael@0: michael@0: for (int i = 0; i < width; i++) { michael@0: SkPMColor16 c = *srcP++; michael@0: unsigned a = SkPacked4444ToA32(c); michael@0: unsigned r = SkPacked4444ToR32(c); michael@0: unsigned g = SkPacked4444ToG32(c); michael@0: unsigned b = SkPacked4444ToB32(c); michael@0: michael@0: if (0 != a && 255 != a) { michael@0: SkUnPreMultiply::Scale scale = table[a]; michael@0: r = SkUnPreMultiply::ApplyScale(scale, r); michael@0: g = SkUnPreMultiply::ApplyScale(scale, g); michael@0: b = SkUnPreMultiply::ApplyScale(scale, b); michael@0: } michael@0: *dst++ = r; michael@0: *dst++ = g; michael@0: *dst++ = b; michael@0: *dst++ = a; michael@0: } michael@0: }