msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkCodecPriv.h" |
| 9 | #include "SkColorPriv.h" |
| 10 | #include "SkMaskSwizzler.h" |
| 11 | |
| 12 | /* |
| 13 | * |
| 14 | * Row procedure for masked color components with 16 bits per pixel |
| 15 | * |
| 16 | */ |
| 17 | static SkSwizzler::ResultAlpha swizzle_mask16_to_n32( |
| 18 | void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { |
| 19 | |
| 20 | // Use the masks to decode to the destination |
| 21 | uint16_t* srcPtr = (uint16_t*) srcRow; |
| 22 | SkPMColor* dstPtr = (SkPMColor*) dstRow; |
| 23 | for (int i = 0; i < width; i++) { |
| 24 | uint16_t p = srcPtr[i]; |
| 25 | uint8_t red = masks->getRed(p); |
| 26 | uint8_t green = masks->getGreen(p); |
| 27 | uint8_t blue = masks->getBlue(p); |
| 28 | dstPtr[i] = SkPackARGB32NoCheck(0xFF, red, green, blue); |
| 29 | } |
| 30 | return SkSwizzler::kOpaque_ResultAlpha; |
| 31 | } |
| 32 | |
| 33 | /* |
| 34 | * |
| 35 | * Row procedure for masked color components with 16 bits per pixel with alpha |
| 36 | * |
| 37 | */ |
| 38 | static SkSwizzler::ResultAlpha swizzle_mask16_alpha_to_n32( |
| 39 | void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { |
| 40 | |
| 41 | // Use the masks to decode to the destination |
| 42 | uint16_t* srcPtr = (uint16_t*) srcRow; |
| 43 | SkPMColor* dstPtr = (SkPMColor*) dstRow; |
| 44 | INIT_RESULT_ALPHA; |
| 45 | for (int i = 0; i < width; i++) { |
| 46 | uint16_t p = srcPtr[i]; |
| 47 | uint8_t red = masks->getRed(p); |
| 48 | uint8_t green = masks->getGreen(p); |
| 49 | uint8_t blue = masks->getBlue(p); |
| 50 | uint8_t alpha = masks->getAlpha(p); |
| 51 | UPDATE_RESULT_ALPHA(alpha); |
| 52 | dstPtr[i] = SkPackARGB32NoCheck(alpha, red, green, blue); |
| 53 | } |
| 54 | return COMPUTE_RESULT_ALPHA; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * |
| 59 | * Row procedure for masked color components with 24 bits per pixel |
| 60 | * |
| 61 | */ |
| 62 | static SkSwizzler::ResultAlpha swizzle_mask24_to_n32( |
| 63 | void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { |
| 64 | |
| 65 | // Use the masks to decode to the destination |
| 66 | SkPMColor* dstPtr = (SkPMColor*) dstRow; |
| 67 | for (int i = 0; i < 3*width; i += 3) { |
| 68 | uint32_t p = srcRow[i] | (srcRow[i + 1] << 8) | srcRow[i + 2] << 16; |
| 69 | uint8_t red = masks->getRed(p); |
| 70 | uint8_t green = masks->getGreen(p); |
| 71 | uint8_t blue = masks->getBlue(p); |
| 72 | dstPtr[i/3] = SkPackARGB32NoCheck(0xFF, red, green, blue); |
| 73 | } |
| 74 | return SkSwizzler::kOpaque_ResultAlpha; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * |
| 79 | * Row procedure for masked color components with 24 bits per pixel with alpha |
| 80 | * |
| 81 | */ |
| 82 | static SkSwizzler::ResultAlpha swizzle_mask24_alpha_to_n32( |
| 83 | void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { |
| 84 | |
| 85 | // Use the masks to decode to the destination |
| 86 | SkPMColor* dstPtr = (SkPMColor*) dstRow; |
| 87 | INIT_RESULT_ALPHA; |
| 88 | for (int i = 0; i < 3*width; i += 3) { |
| 89 | uint32_t p = srcRow[i] | (srcRow[i + 1] << 8) | srcRow[i + 2] << 16; |
| 90 | uint8_t red = masks->getRed(p); |
| 91 | uint8_t green = masks->getGreen(p); |
| 92 | uint8_t blue = masks->getBlue(p); |
| 93 | uint8_t alpha = masks->getAlpha(p); |
| 94 | UPDATE_RESULT_ALPHA(alpha); |
| 95 | dstPtr[i/3] = SkPackARGB32NoCheck(alpha, red, green, blue); |
| 96 | } |
| 97 | return COMPUTE_RESULT_ALPHA; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * |
| 102 | * Row procedure for masked color components with 32 bits per pixel |
| 103 | * |
| 104 | */ |
| 105 | static SkSwizzler::ResultAlpha swizzle_mask32_to_n32( |
| 106 | void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { |
| 107 | |
| 108 | // Use the masks to decode to the destination |
| 109 | uint32_t* srcPtr = (uint32_t*) srcRow; |
| 110 | SkPMColor* dstPtr = (SkPMColor*) dstRow; |
| 111 | for (int i = 0; i < width; i++) { |
| 112 | uint32_t p = srcPtr[i]; |
| 113 | uint8_t red = masks->getRed(p); |
| 114 | uint8_t green = masks->getGreen(p); |
| 115 | uint8_t blue = masks->getBlue(p); |
| 116 | dstPtr[i] = SkPackARGB32NoCheck(0xFF, red, green, blue); |
| 117 | } |
| 118 | return SkSwizzler::kOpaque_ResultAlpha; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * |
| 123 | * Row procedure for masked color components with 32 bits per pixel |
| 124 | * |
| 125 | */ |
| 126 | static SkSwizzler::ResultAlpha swizzle_mask32_alpha_to_n32( |
| 127 | void* dstRow, const uint8_t* srcRow, int width, SkMasks* masks) { |
| 128 | |
| 129 | // Use the masks to decode to the destination |
| 130 | uint32_t* srcPtr = (uint32_t*) srcRow; |
| 131 | SkPMColor* dstPtr = (SkPMColor*) dstRow; |
| 132 | INIT_RESULT_ALPHA; |
| 133 | for (int i = 0; i < width; i++) { |
| 134 | uint32_t p = srcPtr[i]; |
| 135 | uint8_t red = masks->getRed(p); |
| 136 | uint8_t green = masks->getGreen(p); |
| 137 | uint8_t blue = masks->getBlue(p); |
| 138 | uint8_t alpha = masks->getAlpha(p); |
| 139 | UPDATE_RESULT_ALPHA(alpha); |
| 140 | dstPtr[i] = SkPackARGB32NoCheck(alpha, red, green, blue); |
| 141 | } |
| 142 | return COMPUTE_RESULT_ALPHA; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * |
| 147 | * Create a new mask swizzler |
| 148 | * |
| 149 | */ |
| 150 | SkMaskSwizzler* SkMaskSwizzler::CreateMaskSwizzler( |
| 151 | const SkImageInfo& imageInfo, SkMasks* masks, uint32_t bitsPerPixel) { |
| 152 | |
| 153 | // Choose the appropriate row procedure |
| 154 | RowProc proc = NULL; |
| 155 | uint32_t alphaMask = masks->getAlphaMask(); |
| 156 | switch (bitsPerPixel) { |
| 157 | case 16: |
| 158 | if (0 == alphaMask) { |
| 159 | proc = &swizzle_mask16_to_n32; |
| 160 | } else { |
| 161 | proc = &swizzle_mask16_alpha_to_n32; |
| 162 | } |
| 163 | break; |
| 164 | case 24: |
| 165 | if (0 == alphaMask) { |
| 166 | proc = &swizzle_mask24_to_n32; |
| 167 | } else { |
| 168 | proc = &swizzle_mask24_alpha_to_n32; |
| 169 | } |
| 170 | break; |
| 171 | case 32: |
| 172 | if (0 == alphaMask) { |
| 173 | proc = &swizzle_mask32_to_n32; |
| 174 | } else { |
| 175 | proc = &swizzle_mask32_alpha_to_n32; |
| 176 | } |
| 177 | break; |
| 178 | default: |
| 179 | SkASSERT(false); |
| 180 | return NULL; |
| 181 | } |
| 182 | return SkNEW_ARGS(SkMaskSwizzler, (imageInfo, masks, proc)); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * |
| 187 | * Constructor for mask swizzler |
| 188 | * |
| 189 | */ |
| 190 | SkMaskSwizzler::SkMaskSwizzler(const SkImageInfo& imageInfo, |
| 191 | SkMasks* masks, RowProc proc) |
| 192 | : fImageInfo(imageInfo) |
| 193 | , fMasks(masks) |
| 194 | , fRowProc(proc) |
| 195 | {} |
| 196 | |
| 197 | /* |
| 198 | * |
| 199 | * Swizzle the next row |
| 200 | * |
| 201 | */ |
| 202 | SkSwizzler::ResultAlpha SkMaskSwizzler::next(void* dst, |
| 203 | const uint8_t* src) { |
| 204 | return fRowProc(dst, src, fImageInfo.width(), fMasks); |
| 205 | } |