reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2007 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkScaledBitmapSampler.h" |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkDither.h" |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
| 15 | // 8888 |
| 16 | |
| 17 | static bool Sample_Gray_D8888(void* SK_RESTRICT dstRow, |
| 18 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 19 | int width, int deltaSrc, int, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 21 | for (int x = 0; x < width; x++) { |
| 22 | dst[x] = SkPackARGB32(0xFF, src[0], src[0], src[0]); |
| 23 | src += deltaSrc; |
| 24 | } |
| 25 | return false; |
| 26 | } |
| 27 | |
| 28 | static bool Sample_RGBx_D8888(void* SK_RESTRICT dstRow, |
| 29 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 30 | int width, int deltaSrc, int, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 32 | for (int x = 0; x < width; x++) { |
| 33 | dst[x] = SkPackARGB32(0xFF, src[0], src[1], src[2]); |
| 34 | src += deltaSrc; |
| 35 | } |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | static bool Sample_RGBA_D8888(void* SK_RESTRICT dstRow, |
| 40 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 41 | int width, int deltaSrc, int, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 43 | unsigned alphaMask = 0xFF; |
| 44 | for (int x = 0; x < width; x++) { |
| 45 | unsigned alpha = src[3]; |
| 46 | dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); |
| 47 | src += deltaSrc; |
| 48 | alphaMask &= alpha; |
| 49 | } |
| 50 | return alphaMask != 0xFF; |
| 51 | } |
| 52 | |
| 53 | // 565 |
| 54 | |
| 55 | static bool Sample_Gray_D565(void* SK_RESTRICT dstRow, |
| 56 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 57 | int width, int deltaSrc, int, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
| 59 | for (int x = 0; x < width; x++) { |
| 60 | dst[x] = SkPack888ToRGB16(src[0], src[0], src[0]); |
| 61 | src += deltaSrc; |
| 62 | } |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | static bool Sample_Gray_D565_D(void* SK_RESTRICT dstRow, |
| 67 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 68 | int width, int deltaSrc, int y, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
| 70 | DITHER_565_SCAN(y); |
| 71 | for (int x = 0; x < width; x++) { |
| 72 | dst[x] = SkDitherRGBTo565(src[0], src[0], src[0], DITHER_VALUE(x)); |
| 73 | src += deltaSrc; |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | static bool Sample_RGBx_D565(void* SK_RESTRICT dstRow, |
| 79 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 80 | int width, int deltaSrc, int, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
| 82 | for (int x = 0; x < width; x++) { |
| 83 | dst[x] = SkPack888ToRGB16(src[0], src[1], src[2]); |
| 84 | src += deltaSrc; |
| 85 | } |
| 86 | return false; |
| 87 | } |
| 88 | |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 89 | static bool Sample_D565_D565(void* SK_RESTRICT dstRow, |
| 90 | const uint8_t* SK_RESTRICT src, |
| 91 | int width, int deltaSrc, int, const SkPMColor[]) { |
| 92 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
| 93 | uint16_t* SK_RESTRICT castedSrc = (uint16_t*) src; |
| 94 | for (int x = 0; x < width; x++) { |
| 95 | dst[x] = castedSrc[0]; |
| 96 | castedSrc += deltaSrc >> 1; |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | static bool Sample_RGBx_D565_D(void* SK_RESTRICT dstRow, |
| 102 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 103 | int width, int deltaSrc, int y, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
| 105 | DITHER_565_SCAN(y); |
| 106 | for (int x = 0; x < width; x++) { |
| 107 | dst[x] = SkDitherRGBTo565(src[0], src[1], src[2], DITHER_VALUE(x)); |
| 108 | src += deltaSrc; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | // 4444 |
| 114 | |
| 115 | static bool Sample_Gray_D4444(void* SK_RESTRICT dstRow, |
| 116 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 117 | int width, int deltaSrc, int, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; |
| 119 | for (int x = 0; x < width; x++) { |
| 120 | unsigned gray = src[0] >> 4; |
| 121 | dst[x] = SkPackARGB4444(0xF, gray, gray, gray); |
| 122 | src += deltaSrc; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | static bool Sample_Gray_D4444_D(void* SK_RESTRICT dstRow, |
| 128 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 129 | int width, int deltaSrc, int y, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; |
| 131 | DITHER_4444_SCAN(y); |
| 132 | for (int x = 0; x < width; x++) { |
| 133 | dst[x] = SkDitherARGB32To4444(0xFF, src[0], src[0], src[0], |
| 134 | DITHER_VALUE(x)); |
| 135 | src += deltaSrc; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | static bool Sample_RGBx_D4444(void* SK_RESTRICT dstRow, |
| 141 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 142 | int width, int deltaSrc, int, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; |
| 144 | for (int x = 0; x < width; x++) { |
| 145 | dst[x] = SkPackARGB4444(0xF, src[0] >> 4, src[1] >> 4, src[2] >> 4); |
| 146 | src += deltaSrc; |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | static bool Sample_RGBx_D4444_D(void* SK_RESTRICT dstRow, |
| 152 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 153 | int width, int deltaSrc, int y, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | SkPMColor16* dst = (SkPMColor16*)dstRow; |
| 155 | DITHER_4444_SCAN(y); |
| 156 | |
| 157 | for (int x = 0; x < width; x++) { |
| 158 | dst[x] = SkDitherARGB32To4444(0xFF, src[0], src[1], src[2], |
| 159 | DITHER_VALUE(x)); |
| 160 | src += deltaSrc; |
| 161 | } |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | static bool Sample_RGBA_D4444(void* SK_RESTRICT dstRow, |
| 166 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 167 | int width, int deltaSrc, int, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; |
| 169 | unsigned alphaMask = 0xFF; |
| 170 | |
| 171 | for (int x = 0; x < width; x++) { |
| 172 | unsigned alpha = src[3]; |
| 173 | SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); |
| 174 | dst[x] = SkPixel32ToPixel4444(c); |
| 175 | src += deltaSrc; |
| 176 | alphaMask &= alpha; |
| 177 | } |
| 178 | return alphaMask != 0xFF; |
| 179 | } |
| 180 | |
| 181 | static bool Sample_RGBA_D4444_D(void* SK_RESTRICT dstRow, |
| 182 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 183 | int width, int deltaSrc, int y, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; |
| 185 | unsigned alphaMask = 0xFF; |
| 186 | DITHER_4444_SCAN(y); |
| 187 | |
| 188 | for (int x = 0; x < width; x++) { |
| 189 | unsigned alpha = src[3]; |
| 190 | SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]); |
| 191 | dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x)); |
| 192 | src += deltaSrc; |
| 193 | alphaMask &= alpha; |
| 194 | } |
| 195 | return alphaMask != 0xFF; |
| 196 | } |
| 197 | |
| 198 | // Index |
| 199 | |
reed@android.com | 1cdcb51 | 2009-08-24 19:11:00 +0000 | [diff] [blame] | 200 | #define A32_MASK_IN_PLACE (SkPMColor)(SK_A32_MASK << SK_A32_SHIFT) |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 201 | |
| 202 | static bool Sample_Index_D8888(void* SK_RESTRICT dstRow, |
| 203 | const uint8_t* SK_RESTRICT src, |
| 204 | int width, int deltaSrc, int, const SkPMColor ctable[]) { |
| 205 | |
| 206 | SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; |
| 207 | SkPMColor cc = A32_MASK_IN_PLACE; |
| 208 | for (int x = 0; x < width; x++) { |
| 209 | SkPMColor c = ctable[*src]; |
| 210 | cc &= c; |
| 211 | dst[x] = c; |
| 212 | src += deltaSrc; |
| 213 | } |
| 214 | return cc != A32_MASK_IN_PLACE; |
| 215 | } |
| 216 | |
| 217 | static bool Sample_Index_D565(void* SK_RESTRICT dstRow, |
| 218 | const uint8_t* SK_RESTRICT src, |
| 219 | int width, int deltaSrc, int, const SkPMColor ctable[]) { |
| 220 | |
| 221 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
| 222 | for (int x = 0; x < width; x++) { |
| 223 | dst[x] = SkPixel32ToPixel16(ctable[*src]); |
| 224 | src += deltaSrc; |
| 225 | } |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | static bool Sample_Index_D565_D(void* SK_RESTRICT dstRow, |
| 230 | const uint8_t* SK_RESTRICT src, int width, |
| 231 | int deltaSrc, int y, const SkPMColor ctable[]) { |
| 232 | |
| 233 | uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow; |
| 234 | DITHER_565_SCAN(y); |
| 235 | |
| 236 | for (int x = 0; x < width; x++) { |
| 237 | SkPMColor c = ctable[*src]; |
| 238 | dst[x] = SkDitherRGBTo565(SkGetPackedR32(c), SkGetPackedG32(c), |
| 239 | SkGetPackedB32(c), DITHER_VALUE(x)); |
| 240 | src += deltaSrc; |
| 241 | } |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | static bool Sample_Index_D4444(void* SK_RESTRICT dstRow, |
| 246 | const uint8_t* SK_RESTRICT src, int width, |
| 247 | int deltaSrc, int y, const SkPMColor ctable[]) { |
| 248 | |
| 249 | SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; |
| 250 | SkPMColor cc = A32_MASK_IN_PLACE; |
| 251 | for (int x = 0; x < width; x++) { |
| 252 | SkPMColor c = ctable[*src]; |
| 253 | cc &= c; |
| 254 | dst[x] = SkPixel32ToPixel4444(c); |
| 255 | src += deltaSrc; |
| 256 | } |
| 257 | return cc != A32_MASK_IN_PLACE; |
| 258 | } |
| 259 | |
| 260 | static bool Sample_Index_D4444_D(void* SK_RESTRICT dstRow, |
| 261 | const uint8_t* SK_RESTRICT src, int width, |
| 262 | int deltaSrc, int y, const SkPMColor ctable[]) { |
| 263 | |
| 264 | SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow; |
| 265 | SkPMColor cc = A32_MASK_IN_PLACE; |
| 266 | DITHER_4444_SCAN(y); |
| 267 | |
| 268 | for (int x = 0; x < width; x++) { |
| 269 | SkPMColor c = ctable[*src]; |
| 270 | cc &= c; |
| 271 | dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x)); |
| 272 | src += deltaSrc; |
| 273 | } |
| 274 | return cc != A32_MASK_IN_PLACE; |
| 275 | } |
| 276 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | static bool Sample_Index_DI(void* SK_RESTRICT dstRow, |
| 278 | const uint8_t* SK_RESTRICT src, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 279 | int width, int deltaSrc, int, const SkPMColor[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 280 | if (1 == deltaSrc) { |
| 281 | memcpy(dstRow, src, width); |
| 282 | } else { |
| 283 | uint8_t* SK_RESTRICT dst = (uint8_t*)dstRow; |
| 284 | for (int x = 0; x < width; x++) { |
| 285 | dst[x] = src[0]; |
| 286 | src += deltaSrc; |
| 287 | } |
| 288 | } |
| 289 | return false; |
| 290 | } |
| 291 | |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 292 | // 8888 Unpremul |
| 293 | |
| 294 | static bool Sample_Gray_D8888_Unpremul(void* SK_RESTRICT dstRow, |
| 295 | const uint8_t* SK_RESTRICT src, |
| 296 | int width, int deltaSrc, int, |
| 297 | const SkPMColor[]) { |
| 298 | uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow); |
| 299 | for (int x = 0; x < width; x++) { |
| 300 | dst[x] = SkPackARGB32NoCheck(0xFF, src[0], src[0], src[0]); |
| 301 | src += deltaSrc; |
| 302 | } |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | // Sample_RGBx_D8888_Unpremul is no different from Sample_RGBx_D8888, since alpha |
| 307 | // is 0xFF |
| 308 | |
| 309 | static bool Sample_RGBA_D8888_Unpremul(void* SK_RESTRICT dstRow, |
| 310 | const uint8_t* SK_RESTRICT src, |
| 311 | int width, int deltaSrc, int, |
| 312 | const SkPMColor[]) { |
| 313 | uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow); |
| 314 | unsigned alphaMask = 0xFF; |
| 315 | for (int x = 0; x < width; x++) { |
| 316 | unsigned alpha = src[3]; |
| 317 | dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]); |
| 318 | src += deltaSrc; |
| 319 | alphaMask &= alpha; |
| 320 | } |
| 321 | return alphaMask != 0xFF; |
| 322 | } |
| 323 | |
| 324 | // Sample_Index_D8888_Unpremul is the same as Sample_Index_D8888, since the |
| 325 | // color table has its colors inserted unpremultiplied. |
| 326 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 327 | /////////////////////////////////////////////////////////////////////////////// |
| 328 | |
| 329 | #include "SkScaledBitmapSampler.h" |
| 330 | |
| 331 | SkScaledBitmapSampler::SkScaledBitmapSampler(int width, int height, |
| 332 | int sampleSize) { |
vandebo@chromium.org | a728e35 | 2012-03-28 20:29:38 +0000 | [diff] [blame] | 333 | fCTable = NULL; |
| 334 | fDstRow = NULL; |
| 335 | fRowProc = NULL; |
| 336 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 337 | if (width <= 0 || height <= 0) { |
| 338 | sk_throw(); |
| 339 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 340 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 341 | if (sampleSize <= 1) { |
| 342 | fScaledWidth = width; |
| 343 | fScaledHeight = height; |
| 344 | fX0 = fY0 = 0; |
| 345 | fDX = fDY = 1; |
| 346 | return; |
| 347 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 348 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | int dx = SkMin32(sampleSize, width); |
| 350 | int dy = SkMin32(sampleSize, height); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 351 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 352 | fScaledWidth = width / dx; |
| 353 | fScaledHeight = height / dy; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 354 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 355 | SkASSERT(fScaledWidth > 0); |
| 356 | SkASSERT(fScaledHeight > 0); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 357 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 358 | fX0 = dx >> 1; |
| 359 | fY0 = dy >> 1; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 360 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 361 | SkASSERT(fX0 >= 0 && fX0 < width); |
| 362 | SkASSERT(fY0 >= 0 && fY0 < height); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 363 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 364 | fDX = dx; |
| 365 | fDY = dy; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 366 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 367 | SkASSERT(fDX > 0 && (fX0 + fDX * (fScaledWidth - 1)) < width); |
| 368 | SkASSERT(fDY > 0 && (fY0 + fDY * (fScaledHeight - 1)) < height); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | } |
| 370 | |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 371 | bool SkScaledBitmapSampler::begin(SkBitmap* dst, SrcConfig sc, bool dither, |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 372 | const SkPMColor ctable[], |
| 373 | bool requireUnpremul) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 374 | static const RowProc gProcs[] = { |
| 375 | // 8888 (no dither distinction) |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 376 | Sample_Gray_D8888, Sample_Gray_D8888, |
| 377 | Sample_RGBx_D8888, Sample_RGBx_D8888, |
| 378 | Sample_RGBA_D8888, Sample_RGBA_D8888, |
| 379 | Sample_Index_D8888, Sample_Index_D8888, |
| 380 | NULL, NULL, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 381 | // 565 (no alpha distinction) |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 382 | Sample_Gray_D565, Sample_Gray_D565_D, |
| 383 | Sample_RGBx_D565, Sample_RGBx_D565_D, |
| 384 | Sample_RGBx_D565, Sample_RGBx_D565_D, |
| 385 | Sample_Index_D565, Sample_Index_D565_D, |
| 386 | Sample_D565_D565, Sample_D565_D565, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 387 | // 4444 |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 388 | Sample_Gray_D4444, Sample_Gray_D4444_D, |
| 389 | Sample_RGBx_D4444, Sample_RGBx_D4444_D, |
| 390 | Sample_RGBA_D4444, Sample_RGBA_D4444_D, |
| 391 | Sample_Index_D4444, Sample_Index_D4444_D, |
| 392 | NULL, NULL, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 393 | // Index8 |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 394 | NULL, NULL, |
| 395 | NULL, NULL, |
| 396 | NULL, NULL, |
| 397 | Sample_Index_DI, Sample_Index_DI, |
| 398 | NULL, NULL, |
| 399 | // 8888 Unpremul (no dither distinction) |
| 400 | Sample_Gray_D8888_Unpremul, Sample_Gray_D8888_Unpremul, |
| 401 | Sample_RGBx_D8888, Sample_RGBx_D8888, |
| 402 | Sample_RGBA_D8888_Unpremul, Sample_RGBA_D8888_Unpremul, |
| 403 | Sample_Index_D8888, Sample_Index_D8888, |
| 404 | NULL, NULL, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | }; |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 406 | // The jump between dst configs in the table |
| 407 | static const int gProcDstConfigSpan = 10; |
| 408 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gProcs) == 5 * gProcDstConfigSpan, |
| 409 | gProcs_has_the_wrong_number_of_entries); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 410 | |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 411 | fCTable = ctable; |
| 412 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 413 | int index = 0; |
| 414 | if (dither) { |
| 415 | index += 1; |
| 416 | } |
| 417 | switch (sc) { |
| 418 | case SkScaledBitmapSampler::kGray: |
| 419 | fSrcPixelSize = 1; |
| 420 | index += 0; |
| 421 | break; |
| 422 | case SkScaledBitmapSampler::kRGB: |
| 423 | fSrcPixelSize = 3; |
| 424 | index += 2; |
| 425 | break; |
| 426 | case SkScaledBitmapSampler::kRGBX: |
| 427 | fSrcPixelSize = 4; |
| 428 | index += 2; |
| 429 | break; |
| 430 | case SkScaledBitmapSampler::kRGBA: |
| 431 | fSrcPixelSize = 4; |
| 432 | index += 4; |
| 433 | break; |
| 434 | case SkScaledBitmapSampler::kIndex: |
| 435 | fSrcPixelSize = 1; |
| 436 | index += 6; |
| 437 | break; |
djsollen@google.com | 57f4969 | 2011-02-23 20:46:31 +0000 | [diff] [blame] | 438 | case SkScaledBitmapSampler::kRGB_565: |
| 439 | fSrcPixelSize = 2; |
| 440 | index += 8; |
| 441 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 442 | default: |
| 443 | return false; |
| 444 | } |
| 445 | |
| 446 | switch (dst->config()) { |
| 447 | case SkBitmap::kARGB_8888_Config: |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 448 | index += 0 * gProcDstConfigSpan; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 449 | break; |
| 450 | case SkBitmap::kRGB_565_Config: |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 451 | index += 1 * gProcDstConfigSpan; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 452 | break; |
| 453 | case SkBitmap::kARGB_4444_Config: |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 454 | index += 2 * gProcDstConfigSpan; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 455 | break; |
| 456 | case SkBitmap::kIndex8_Config: |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 457 | index += 3 * gProcDstConfigSpan; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 458 | break; |
| 459 | default: |
| 460 | return false; |
| 461 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 462 | |
scroggo@google.com | 2bbc2c9 | 2013-06-14 15:33:20 +0000 | [diff] [blame] | 463 | if (requireUnpremul) { |
| 464 | if (dst->config() != SkBitmap::kARGB_8888_Config) { |
| 465 | return false; |
| 466 | } |
| 467 | index += 4 * gProcDstConfigSpan; |
| 468 | } |
| 469 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 470 | fRowProc = gProcs[index]; |
| 471 | fDstRow = (char*)dst->getPixels(); |
| 472 | fDstRowBytes = dst->rowBytes(); |
| 473 | fCurrY = 0; |
| 474 | return fRowProc != NULL; |
| 475 | } |
| 476 | |
| 477 | bool SkScaledBitmapSampler::next(const uint8_t* SK_RESTRICT src) { |
| 478 | SkASSERT((unsigned)fCurrY < (unsigned)fScaledHeight); |
| 479 | |
| 480 | bool hadAlpha = fRowProc(fDstRow, src + fX0 * fSrcPixelSize, fScaledWidth, |
reed@android.com | 1134426 | 2009-07-08 20:09:23 +0000 | [diff] [blame] | 481 | fDX * fSrcPixelSize, fCurrY, fCTable); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 482 | fDstRow += fDstRowBytes; |
| 483 | fCurrY += 1; |
| 484 | return hadAlpha; |
| 485 | } |