Index: src/codec/SkMaskSwizzler.cpp |
diff --git a/src/codec/SkMaskSwizzler.cpp b/src/codec/SkMaskSwizzler.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9f54fd089966fec76c985dbd208d3653642f59e1 |
--- /dev/null |
+++ b/src/codec/SkMaskSwizzler.cpp |
@@ -0,0 +1,338 @@ |
+/* |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkColorPriv.h" |
+#include "SkMaskSwizzler.h" |
+ |
+/* |
+ * |
+ * Used to convert 1-7 bit color components into 8-bit color components |
+ * |
+ */ |
+const static uint8_t n_bit_to_8_bit_lookup_table[] = { |
+ // 1 bit |
+ 0, 255, |
+ // 2 bits |
+ 0, 85, 170, 255, |
+ // 3 bits |
+ 0, 36, 73, 109, 146, 182, 219, 255, |
+ // 4 bits |
+ 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255, |
+ // 5 bits |
+ 0, 8, 16, 25, 33, 41, 49, 58, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, |
+ 148, 156, 165, 173, 181, 189, 197, 206, 214, 222, 230, 239, 247, 255, |
+ // 6 bits |
+ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 45, 49, 53, 57, 61, 65, 69, 73, |
+ 77, 81, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 130, 134, 138, |
+ 142, 146, 150, 154, 158, 162, 166, 170, 174, 178, 182, 186, 190, 194, 198, |
+ 202, 206, 210, 215, 219, 223, 227, 231, 235, 239, 243, 247, 251, 255, |
+ // 7 bits |
+ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, |
+ 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, |
+ 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, |
+ 112, 114, 116, 118, 120, 122, 124, 126, 129, 131, 133, 135, 137, 139, 141, |
+ 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, |
+ 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199, 201, |
+ 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, |
+ 233, 235, 237, 239, 241, 243, 245, 247, 249, 251, 253, 255 |
+}; |
+ |
+/* |
+ * |
+ * Convert an n bit component to an 8-bit component |
+ * |
+ */ |
+static uint8_t convert_to_8(uint32_t component, uint32_t n) { |
+ if (0 == n) { |
+ return 0; |
+ } else if (8 > n) { |
+ return n_bit_to_8_bit_lookup_table[(1 << n) - 2 + component]; |
+ } else { |
+ SkASSERT(8 == n); |
+ return component; |
+ } |
+} |
+ |
+/* |
+ * |
+ * Get the color component |
+ * |
+ */ |
+static uint8_t get_comp(uint32_t pixel, uint32_t mask, uint32_t shift, |
+ uint32_t size) { |
+ return convert_to_8((pixel & mask) >> shift, size); |
+} |
+ |
+/* |
+ * |
+ * Row procedure for masked color components with 16 bits per pixel |
+ * |
+ */ |
+static SkSwizzler::ResultAlpha swizzle_mask16_to_n32( |
+ void* dstRow, const uint8_t* srcRow, int width, |
+ const SkMaskSwizzler::Masks& masks) { |
+ |
+ // Use the masks to decode to the destination |
+ uint16_t* srcPtr = (uint16_t*) srcRow; |
+ SkPMColor* dstPtr = (SkPMColor*) dstRow; |
+ for (uint32_t i = 0; i < width; i++) { |
+ uint16_t p = srcPtr[i]; |
+ uint8_t red = get_comp(p, masks.red.mask, masks.red.shift, |
scroggo
2015/03/11 14:23:10
get_comp is a step in the right direction.
What i
msarett
2015/03/11 18:53:05
Agreed this is done.
|
+ masks.red.size); |
+ uint8_t green = get_comp(p, masks.green.mask, masks.green.shift, |
+ masks.green.size); |
+ uint8_t blue = get_comp(p, masks.blue.mask, masks.blue.shift, |
+ masks.blue.size); |
+ dstPtr[i] = SkPackARGB32NoCheck(0xFF, red, green, blue); |
+ } |
+ return SkSwizzler::kOpaque_ResultAlpha; |
+} |
+ |
+/* |
+ * |
+ * Row procedure for masked color components with 16 bits per pixel with alpha |
+ * |
+ */ |
+static SkSwizzler::ResultAlpha swizzle_mask16_alpha_to_n32( |
+ void* dstRow, const uint8_t* srcRow, int width, |
+ const SkMaskSwizzler::Masks& masks) { |
+ |
+ // Use the masks to decode to the destination |
+ uint16_t* srcPtr = (uint16_t*) srcRow; |
+ SkPMColor* dstPtr = (SkPMColor*) dstRow; |
+ uint8_t zeroAlpha = 0; |
+ uint8_t maxAlpha = 0xFF; |
+ for (uint32_t i = 0; i < width; i++) { |
+ uint16_t p = srcPtr[i]; |
+ uint8_t red = get_comp(p, masks.red.mask, masks.red.shift, |
+ masks.red.size); |
+ uint8_t green = get_comp(p, masks.green.mask, masks.green.shift, |
+ masks.green.size); |
+ uint8_t blue = get_comp(p, masks.blue.mask, masks.blue.shift, |
+ masks.blue.size); |
+ uint8_t alpha = get_comp(p, masks.alpha.mask, masks.alpha.shift, |
+ masks.alpha.size); |
+ zeroAlpha |= alpha; |
+ maxAlpha &= alpha; |
+ dstPtr[i] = SkPackARGB32NoCheck(alpha, red, green, blue); |
+ } |
+ return SkSwizzler::GetResult(zeroAlpha, maxAlpha); |
+} |
+ |
+/* |
+ * |
+ * Row procedure for masked color components with 24 bits per pixel |
+ * |
+ */ |
+static SkSwizzler::ResultAlpha swizzle_mask24_to_n32( |
+ void* dstRow, const uint8_t* srcRow, int width, |
+ const SkMaskSwizzler::Masks& masks) { |
+ |
+ // Use the masks to decode to the destination |
+ SkPMColor* dstPtr = (SkPMColor*) dstRow; |
+ for (uint32_t i = 0; i < 3*width; i += 3) { |
+ uint32_t p = srcRow[i] | (srcRow[i + 1] << 8) | srcRow[i + 2] << 16; |
+ uint8_t red = get_comp(p, masks.red.mask, masks.red.shift, |
+ masks.red.size); |
+ uint8_t green = get_comp(p, masks.green.mask, masks.green.shift, |
+ masks.green.size); |
+ uint8_t blue = get_comp(p, masks.blue.mask, masks.blue.shift, |
+ masks.blue.size); |
+ dstPtr[i/3] = SkPackARGB32NoCheck(0xFF, red, green, blue); |
+ } |
+ return SkSwizzler::kOpaque_ResultAlpha; |
+} |
+ |
+/* |
+ * |
+ * Row procedure for masked color components with 24 bits per pixel with alpha |
+ * |
+ */ |
+static SkSwizzler::ResultAlpha swizzle_mask24_alpha_to_n32( |
+ void* dstRow, const uint8_t* srcRow, int width, |
+ const SkMaskSwizzler::Masks& masks) { |
+ |
+ // Use the masks to decode to the destination |
+ SkPMColor* dstPtr = (SkPMColor*) dstRow; |
+ uint8_t zeroAlpha = 0; |
+ uint8_t maxAlpha = 0xFF; |
+ for (uint32_t i = 0; i < 3*width; i += 3) { |
+ uint32_t p = srcRow[i] | (srcRow[i + 1] << 8) | srcRow[i + 2] << 16; |
+ uint8_t red = get_comp(p, masks.red.mask, masks.red.shift, |
+ masks.red.size); |
+ uint8_t green = get_comp(p, masks.green.mask, masks.green.shift, |
+ masks.green.size); |
+ uint8_t blue = get_comp(p, masks.blue.mask, masks.blue.shift, |
+ masks.blue.size); |
+ uint8_t alpha = get_comp(p, masks.alpha.mask, masks.alpha.shift, |
+ masks.alpha.size); |
+ zeroAlpha |= alpha; |
+ maxAlpha &= alpha; |
+ dstPtr[i/3] = SkPackARGB32NoCheck(alpha, red, green, blue); |
+ } |
+ return SkSwizzler::GetResult(zeroAlpha, maxAlpha); |
+} |
+ |
+/* |
+ * |
+ * Row procedure for masked color components with 32 bits per pixel |
+ * |
+ */ |
+static SkSwizzler::ResultAlpha swizzle_mask32_to_n32( |
+ void* dstRow, const uint8_t* srcRow, int width, |
+ const SkMaskSwizzler::Masks& masks) { |
+ |
+ // Use the masks to decode to the destination |
+ uint32_t* srcPtr = (uint32_t*) srcRow; |
+ SkPMColor* dstPtr = (SkPMColor*) dstRow; |
+ for (uint32_t i = 0; i < width; i++) { |
+ uint32_t p = srcPtr[i]; |
+ uint8_t red = get_comp(p, masks.red.mask, masks.red.shift, |
+ masks.red.size); |
+ uint8_t green = get_comp(p, masks.green.mask, masks.green.shift, |
+ masks.green.size); |
+ uint8_t blue = get_comp(p, masks.blue.mask, masks.blue.shift, |
+ masks.blue.size); |
+ dstPtr[i] = SkPackARGB32NoCheck(0xFF, red, green, blue); |
+ } |
+ return SkSwizzler::kOpaque_ResultAlpha; |
+} |
+ |
+/* |
+ * |
+ * Row procedure for masked color components with 32 bits per pixel |
+ * |
+ */ |
+static SkSwizzler::ResultAlpha swizzle_mask32_alpha_to_n32( |
+ void* dstRow, const uint8_t* srcRow, int width, |
+ const SkMaskSwizzler::Masks& masks) { |
+ |
+ // Use the masks to decode to the destination |
+ uint32_t* srcPtr = (uint32_t*) srcRow; |
+ SkPMColor* dstPtr = (SkPMColor*) dstRow; |
+ uint8_t zeroAlpha = 0; |
+ uint8_t maxAlpha = 0xFF; |
+ for (uint32_t i = 0; i < width; i++) { |
+ uint32_t p = srcPtr[i]; |
+ uint8_t red = get_comp(p, masks.red.mask, masks.red.shift, |
+ masks.red.size); |
+ uint8_t green = get_comp(p, masks.green.mask, masks.green.shift, |
+ masks.green.size); |
+ uint8_t blue = get_comp(p, masks.blue.mask, masks.blue.shift, |
+ masks.blue.size); |
+ uint8_t alpha = get_comp(p, masks.alpha.mask, masks.alpha.shift, |
+ masks.alpha.size); |
+ zeroAlpha |= alpha; |
+ maxAlpha &= alpha; |
+ dstPtr[i] = SkPackARGB32NoCheck(alpha, red, green, blue); |
+ } |
+ return SkSwizzler::GetResult(zeroAlpha, maxAlpha); |
+} |
+ |
+/* |
+ * |
+ * Process an input mask to obtain the necessary information |
+ * |
+ */ |
+const SkMaskSwizzler::MaskInfo& SkMaskSwizzler::ProcessMask(uint32_t mask, |
+ uint32_t bpp) { |
+ // Trim the masks to the allowed number of bits |
+ if (bpp < 32) { |
+ mask &= (1 << bpp) - 1; |
+ } |
+ |
+ // Determine properties of the mask |
+ uint32_t tempMask = mask; |
+ uint32_t shift = 0; |
+ uint32_t size = 0; |
+ if (tempMask != 0) { |
+ // Count trailing zeros on masks |
+ for (; (tempMask & 1) == 0; tempMask >>= 1) { |
+ shift++; |
+ } |
+ // Count the size of the mask |
+ for (; tempMask & 1; tempMask >>= 1) { |
+ size++; |
+ } |
+ // Truncate masks greater than 8 bits |
+ if (size > 8) { |
+ shift += size - 8; |
+ size = 8; |
+ } |
+ } |
+ |
+ // Save the calculated values |
+ const MaskInfo info = { mask, shift, size }; |
+ return info; |
+} |
+ |
+/* |
+ * |
+ * Create a new mask swizzler |
+ * |
+ */ |
+SkMaskSwizzler* SkMaskSwizzler::CreateMaskSwizzler( |
+ const SkImageInfo& imageInfo, InputMasks input, uint32_t bitsPerPixel) { |
+ // Process input masks |
+ const MaskInfo red = ProcessMask(input.red, bitsPerPixel); |
+ const MaskInfo green = ProcessMask(input.green, bitsPerPixel); |
+ const MaskInfo blue = ProcessMask(input.blue, bitsPerPixel); |
+ const MaskInfo alpha = ProcessMask(input.alpha, bitsPerPixel); |
+ const Masks masks = { red, green, blue, alpha }; |
+ |
+ // Choose the appropriate row procedure |
+ RowProc proc = NULL; |
+ switch (bitsPerPixel) { |
+ case 16: |
+ if (masks.alpha.mask == 0) { |
+ proc = &swizzle_mask16_to_n32; |
+ } else { |
+ proc = &swizzle_mask16_alpha_to_n32; |
+ } |
+ break; |
+ case 24: |
+ if (masks.alpha.mask == 0) { |
+ proc = &swizzle_mask24_to_n32; |
+ } else { |
+ proc = &swizzle_mask24_alpha_to_n32; |
+ } |
+ break; |
+ case 32: |
+ if (masks.alpha.mask == 0) { |
+ proc = &swizzle_mask32_to_n32; |
+ } else { |
+ proc = &swizzle_mask32_alpha_to_n32; |
+ } |
+ break; |
+ default: |
+ SkASSERT(false); |
+ return NULL; |
+ } |
+ return SkNEW_ARGS(SkMaskSwizzler, (imageInfo, masks, proc)); |
+} |
+ |
+/* |
+ * |
+ * Constructor for mask swizzler |
+ * |
+ */ |
+SkMaskSwizzler::SkMaskSwizzler(const SkImageInfo& imageInfo, const Masks& masks, |
+ RowProc proc) |
+ : fImageInfo(imageInfo) |
+ , fMasks(masks) |
+ , fRowProc(proc) |
+{} |
+ |
+/* |
+ * |
+ * Swizzle the next row |
+ * |
+ */ |
+SkSwizzler::ResultAlpha SkMaskSwizzler::next(void* dst, |
+ const uint8_t* src) { |
+ return fRowProc(dst, src, fImageInfo.width(), fMasks); |
+} |