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