gfx/skia/trunk/src/images/SkScaledBitmapSampler.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2007 The Android Open Source Project
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8
michael@0 9 #include "SkScaledBitmapSampler.h"
michael@0 10 #include "SkBitmap.h"
michael@0 11 #include "SkColorPriv.h"
michael@0 12 #include "SkDither.h"
michael@0 13 #include "SkTypes.h"
michael@0 14
michael@0 15 // 8888
michael@0 16
michael@0 17 static bool Sample_Gray_D8888(void* SK_RESTRICT dstRow,
michael@0 18 const uint8_t* SK_RESTRICT src,
michael@0 19 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 20 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
michael@0 21 for (int x = 0; x < width; x++) {
michael@0 22 dst[x] = SkPackARGB32(0xFF, src[0], src[0], src[0]);
michael@0 23 src += deltaSrc;
michael@0 24 }
michael@0 25 return false;
michael@0 26 }
michael@0 27
michael@0 28 static SkScaledBitmapSampler::RowProc get_gray_to_8888_proc(const SkImageDecoder& decoder) {
michael@0 29 // Dither, unpremul, and skipZeroes have no effect
michael@0 30 return Sample_Gray_D8888;
michael@0 31 }
michael@0 32
michael@0 33 static bool Sample_RGBx_D8888(void* SK_RESTRICT dstRow,
michael@0 34 const uint8_t* SK_RESTRICT src,
michael@0 35 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 36 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
michael@0 37 for (int x = 0; x < width; x++) {
michael@0 38 dst[x] = SkPackARGB32(0xFF, src[0], src[1], src[2]);
michael@0 39 src += deltaSrc;
michael@0 40 }
michael@0 41 return false;
michael@0 42 }
michael@0 43
michael@0 44 static SkScaledBitmapSampler::RowProc get_RGBx_to_8888_proc(const SkImageDecoder& decoder) {
michael@0 45 // Dither, unpremul, and skipZeroes have no effect
michael@0 46 return Sample_RGBx_D8888;
michael@0 47 }
michael@0 48
michael@0 49 static bool Sample_RGBA_D8888(void* SK_RESTRICT dstRow,
michael@0 50 const uint8_t* SK_RESTRICT src,
michael@0 51 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 52 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
michael@0 53 unsigned alphaMask = 0xFF;
michael@0 54 for (int x = 0; x < width; x++) {
michael@0 55 unsigned alpha = src[3];
michael@0 56 dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]);
michael@0 57 src += deltaSrc;
michael@0 58 alphaMask &= alpha;
michael@0 59 }
michael@0 60 return alphaMask != 0xFF;
michael@0 61 }
michael@0 62
michael@0 63 static bool Sample_RGBA_D8888_Unpremul(void* SK_RESTRICT dstRow,
michael@0 64 const uint8_t* SK_RESTRICT src,
michael@0 65 int width, int deltaSrc, int,
michael@0 66 const SkPMColor[]) {
michael@0 67 uint32_t* SK_RESTRICT dst = reinterpret_cast<uint32_t*>(dstRow);
michael@0 68 unsigned alphaMask = 0xFF;
michael@0 69 for (int x = 0; x < width; x++) {
michael@0 70 unsigned alpha = src[3];
michael@0 71 dst[x] = SkPackARGB32NoCheck(alpha, src[0], src[1], src[2]);
michael@0 72 src += deltaSrc;
michael@0 73 alphaMask &= alpha;
michael@0 74 }
michael@0 75 return alphaMask != 0xFF;
michael@0 76 }
michael@0 77
michael@0 78 static bool Sample_RGBA_D8888_SkipZ(void* SK_RESTRICT dstRow,
michael@0 79 const uint8_t* SK_RESTRICT src,
michael@0 80 int width, int deltaSrc, int,
michael@0 81 const SkPMColor[]) {
michael@0 82 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
michael@0 83 unsigned alphaMask = 0xFF;
michael@0 84 for (int x = 0; x < width; x++) {
michael@0 85 unsigned alpha = src[3];
michael@0 86 if (0 != alpha) {
michael@0 87 dst[x] = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]);
michael@0 88 }
michael@0 89 src += deltaSrc;
michael@0 90 alphaMask &= alpha;
michael@0 91 }
michael@0 92 return alphaMask != 0xFF;
michael@0 93 }
michael@0 94
michael@0 95 static SkScaledBitmapSampler::RowProc get_RGBA_to_8888_proc(const SkImageDecoder& decoder) {
michael@0 96 // Dither has no effect.
michael@0 97 if (decoder.getRequireUnpremultipliedColors()) {
michael@0 98 // We could check each component for a zero, at the expense of extra checks.
michael@0 99 // For now, just return unpremul.
michael@0 100 return Sample_RGBA_D8888_Unpremul;
michael@0 101 }
michael@0 102 // Supply the versions that premultiply the colors
michael@0 103 if (decoder.getSkipWritingZeroes()) {
michael@0 104 return Sample_RGBA_D8888_SkipZ;
michael@0 105 }
michael@0 106 return Sample_RGBA_D8888;
michael@0 107 }
michael@0 108
michael@0 109 // 565
michael@0 110
michael@0 111 static bool Sample_Gray_D565(void* SK_RESTRICT dstRow,
michael@0 112 const uint8_t* SK_RESTRICT src,
michael@0 113 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 114 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
michael@0 115 for (int x = 0; x < width; x++) {
michael@0 116 dst[x] = SkPack888ToRGB16(src[0], src[0], src[0]);
michael@0 117 src += deltaSrc;
michael@0 118 }
michael@0 119 return false;
michael@0 120 }
michael@0 121
michael@0 122 static bool Sample_Gray_D565_D(void* SK_RESTRICT dstRow,
michael@0 123 const uint8_t* SK_RESTRICT src,
michael@0 124 int width, int deltaSrc, int y, const SkPMColor[]) {
michael@0 125 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
michael@0 126 DITHER_565_SCAN(y);
michael@0 127 for (int x = 0; x < width; x++) {
michael@0 128 dst[x] = SkDitherRGBTo565(src[0], src[0], src[0], DITHER_VALUE(x));
michael@0 129 src += deltaSrc;
michael@0 130 }
michael@0 131 return false;
michael@0 132 }
michael@0 133
michael@0 134 static SkScaledBitmapSampler::RowProc get_gray_to_565_proc(const SkImageDecoder& decoder) {
michael@0 135 // Unpremul and skip zeroes make no difference
michael@0 136 if (decoder.getDitherImage()) {
michael@0 137 return Sample_Gray_D565_D;
michael@0 138 }
michael@0 139 return Sample_Gray_D565;
michael@0 140 }
michael@0 141
michael@0 142 static bool Sample_RGBx_D565(void* SK_RESTRICT dstRow,
michael@0 143 const uint8_t* SK_RESTRICT src,
michael@0 144 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 145 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
michael@0 146 for (int x = 0; x < width; x++) {
michael@0 147 dst[x] = SkPack888ToRGB16(src[0], src[1], src[2]);
michael@0 148 src += deltaSrc;
michael@0 149 }
michael@0 150 return false;
michael@0 151 }
michael@0 152
michael@0 153 static bool Sample_RGBx_D565_D(void* SK_RESTRICT dstRow,
michael@0 154 const uint8_t* SK_RESTRICT src,
michael@0 155 int width, int deltaSrc, int y,
michael@0 156 const SkPMColor[]) {
michael@0 157 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
michael@0 158 DITHER_565_SCAN(y);
michael@0 159 for (int x = 0; x < width; x++) {
michael@0 160 dst[x] = SkDitherRGBTo565(src[0], src[1], src[2], DITHER_VALUE(x));
michael@0 161 src += deltaSrc;
michael@0 162 }
michael@0 163 return false;
michael@0 164 }
michael@0 165
michael@0 166 static SkScaledBitmapSampler::RowProc get_RGBx_to_565_proc(const SkImageDecoder& decoder) {
michael@0 167 // Unpremul and skip zeroes make no difference
michael@0 168 if (decoder.getDitherImage()) {
michael@0 169 return Sample_RGBx_D565_D;
michael@0 170 }
michael@0 171 return Sample_RGBx_D565;
michael@0 172 }
michael@0 173
michael@0 174
michael@0 175 static bool Sample_D565_D565(void* SK_RESTRICT dstRow,
michael@0 176 const uint8_t* SK_RESTRICT src,
michael@0 177 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 178 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
michael@0 179 uint16_t* SK_RESTRICT castedSrc = (uint16_t*) src;
michael@0 180 for (int x = 0; x < width; x++) {
michael@0 181 dst[x] = castedSrc[0];
michael@0 182 castedSrc += deltaSrc >> 1;
michael@0 183 }
michael@0 184 return false;
michael@0 185 }
michael@0 186
michael@0 187 static SkScaledBitmapSampler::RowProc get_565_to_565_proc(const SkImageDecoder& decoder) {
michael@0 188 // Unpremul, dither, and skip zeroes have no effect
michael@0 189 return Sample_D565_D565;
michael@0 190 }
michael@0 191
michael@0 192 // 4444
michael@0 193
michael@0 194 static bool Sample_Gray_D4444(void* SK_RESTRICT dstRow,
michael@0 195 const uint8_t* SK_RESTRICT src,
michael@0 196 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 197 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 198 for (int x = 0; x < width; x++) {
michael@0 199 unsigned gray = src[0] >> 4;
michael@0 200 dst[x] = SkPackARGB4444(0xF, gray, gray, gray);
michael@0 201 src += deltaSrc;
michael@0 202 }
michael@0 203 return false;
michael@0 204 }
michael@0 205
michael@0 206 static bool Sample_Gray_D4444_D(void* SK_RESTRICT dstRow,
michael@0 207 const uint8_t* SK_RESTRICT src,
michael@0 208 int width, int deltaSrc, int y, const SkPMColor[]) {
michael@0 209 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 210 DITHER_4444_SCAN(y);
michael@0 211 for (int x = 0; x < width; x++) {
michael@0 212 dst[x] = SkDitherARGB32To4444(0xFF, src[0], src[0], src[0],
michael@0 213 DITHER_VALUE(x));
michael@0 214 src += deltaSrc;
michael@0 215 }
michael@0 216 return false;
michael@0 217 }
michael@0 218
michael@0 219 static SkScaledBitmapSampler::RowProc get_gray_to_4444_proc(const SkImageDecoder& decoder) {
michael@0 220 // Skip zeroes and unpremul make no difference
michael@0 221 if (decoder.getDitherImage()) {
michael@0 222 return Sample_Gray_D4444_D;
michael@0 223 }
michael@0 224 return Sample_Gray_D4444;
michael@0 225 }
michael@0 226
michael@0 227 static bool Sample_RGBx_D4444(void* SK_RESTRICT dstRow,
michael@0 228 const uint8_t* SK_RESTRICT src,
michael@0 229 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 230 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 231 for (int x = 0; x < width; x++) {
michael@0 232 dst[x] = SkPackARGB4444(0xF, src[0] >> 4, src[1] >> 4, src[2] >> 4);
michael@0 233 src += deltaSrc;
michael@0 234 }
michael@0 235 return false;
michael@0 236 }
michael@0 237
michael@0 238 static bool Sample_RGBx_D4444_D(void* SK_RESTRICT dstRow,
michael@0 239 const uint8_t* SK_RESTRICT src,
michael@0 240 int width, int deltaSrc, int y, const SkPMColor[]) {
michael@0 241 SkPMColor16* dst = (SkPMColor16*)dstRow;
michael@0 242 DITHER_4444_SCAN(y);
michael@0 243
michael@0 244 for (int x = 0; x < width; x++) {
michael@0 245 dst[x] = SkDitherARGB32To4444(0xFF, src[0], src[1], src[2],
michael@0 246 DITHER_VALUE(x));
michael@0 247 src += deltaSrc;
michael@0 248 }
michael@0 249 return false;
michael@0 250 }
michael@0 251
michael@0 252 static SkScaledBitmapSampler::RowProc get_RGBx_to_4444_proc(const SkImageDecoder& decoder) {
michael@0 253 // Skip zeroes and unpremul make no difference
michael@0 254 if (decoder.getDitherImage()) {
michael@0 255 return Sample_RGBx_D4444_D;
michael@0 256 }
michael@0 257 return Sample_RGBx_D4444;
michael@0 258 }
michael@0 259
michael@0 260 static bool Sample_RGBA_D4444(void* SK_RESTRICT dstRow,
michael@0 261 const uint8_t* SK_RESTRICT src,
michael@0 262 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 263 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 264 unsigned alphaMask = 0xFF;
michael@0 265
michael@0 266 for (int x = 0; x < width; x++) {
michael@0 267 unsigned alpha = src[3];
michael@0 268 SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]);
michael@0 269 dst[x] = SkPixel32ToPixel4444(c);
michael@0 270 src += deltaSrc;
michael@0 271 alphaMask &= alpha;
michael@0 272 }
michael@0 273 return alphaMask != 0xFF;
michael@0 274 }
michael@0 275
michael@0 276 static bool Sample_RGBA_D4444_SkipZ(void* SK_RESTRICT dstRow,
michael@0 277 const uint8_t* SK_RESTRICT src,
michael@0 278 int width, int deltaSrc, int,
michael@0 279 const SkPMColor[]) {
michael@0 280 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 281 unsigned alphaMask = 0xFF;
michael@0 282
michael@0 283 for (int x = 0; x < width; x++) {
michael@0 284 unsigned alpha = src[3];
michael@0 285 if (alpha != 0) {
michael@0 286 SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]);
michael@0 287 dst[x] = SkPixel32ToPixel4444(c);
michael@0 288 }
michael@0 289 src += deltaSrc;
michael@0 290 alphaMask &= alpha;
michael@0 291 }
michael@0 292 return alphaMask != 0xFF;
michael@0 293 }
michael@0 294
michael@0 295
michael@0 296 static bool Sample_RGBA_D4444_D(void* SK_RESTRICT dstRow,
michael@0 297 const uint8_t* SK_RESTRICT src,
michael@0 298 int width, int deltaSrc, int y,
michael@0 299 const SkPMColor[]) {
michael@0 300 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 301 unsigned alphaMask = 0xFF;
michael@0 302 DITHER_4444_SCAN(y);
michael@0 303
michael@0 304 for (int x = 0; x < width; x++) {
michael@0 305 unsigned alpha = src[3];
michael@0 306 SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]);
michael@0 307 dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x));
michael@0 308 src += deltaSrc;
michael@0 309 alphaMask &= alpha;
michael@0 310 }
michael@0 311 return alphaMask != 0xFF;
michael@0 312 }
michael@0 313
michael@0 314 static bool Sample_RGBA_D4444_D_SkipZ(void* SK_RESTRICT dstRow,
michael@0 315 const uint8_t* SK_RESTRICT src,
michael@0 316 int width, int deltaSrc, int y,
michael@0 317 const SkPMColor[]) {
michael@0 318 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 319 unsigned alphaMask = 0xFF;
michael@0 320 DITHER_4444_SCAN(y);
michael@0 321
michael@0 322 for (int x = 0; x < width; x++) {
michael@0 323 unsigned alpha = src[3];
michael@0 324 if (alpha != 0) {
michael@0 325 SkPMColor c = SkPreMultiplyARGB(alpha, src[0], src[1], src[2]);
michael@0 326 dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x));
michael@0 327 }
michael@0 328 src += deltaSrc;
michael@0 329 alphaMask &= alpha;
michael@0 330 }
michael@0 331 return alphaMask != 0xFF;
michael@0 332 }
michael@0 333
michael@0 334 static SkScaledBitmapSampler::RowProc get_RGBA_to_4444_proc(const SkImageDecoder& decoder) {
michael@0 335 if (decoder.getRequireUnpremultipliedColors()) {
michael@0 336 // Unpremultiplied is not supported for 4444
michael@0 337 return NULL;
michael@0 338 }
michael@0 339 const bool dither = decoder.getDitherImage();
michael@0 340 if (decoder.getSkipWritingZeroes()) {
michael@0 341 if (dither) {
michael@0 342 return Sample_RGBA_D4444_D_SkipZ;
michael@0 343 }
michael@0 344 return Sample_RGBA_D4444_SkipZ;
michael@0 345 }
michael@0 346 if (dither) {
michael@0 347 return Sample_RGBA_D4444_D;
michael@0 348 }
michael@0 349 return Sample_RGBA_D4444;
michael@0 350 }
michael@0 351
michael@0 352 // Index
michael@0 353
michael@0 354 #define A32_MASK_IN_PLACE (SkPMColor)(SK_A32_MASK << SK_A32_SHIFT)
michael@0 355
michael@0 356 static bool Sample_Index_D8888(void* SK_RESTRICT dstRow,
michael@0 357 const uint8_t* SK_RESTRICT src,
michael@0 358 int width, int deltaSrc, int, const SkPMColor ctable[]) {
michael@0 359
michael@0 360 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
michael@0 361 SkPMColor cc = A32_MASK_IN_PLACE;
michael@0 362 for (int x = 0; x < width; x++) {
michael@0 363 SkPMColor c = ctable[*src];
michael@0 364 cc &= c;
michael@0 365 dst[x] = c;
michael@0 366 src += deltaSrc;
michael@0 367 }
michael@0 368 return cc != A32_MASK_IN_PLACE;
michael@0 369 }
michael@0 370
michael@0 371 static bool Sample_Index_D8888_SkipZ(void* SK_RESTRICT dstRow,
michael@0 372 const uint8_t* SK_RESTRICT src,
michael@0 373 int width, int deltaSrc, int,
michael@0 374 const SkPMColor ctable[]) {
michael@0 375
michael@0 376 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
michael@0 377 SkPMColor cc = A32_MASK_IN_PLACE;
michael@0 378 for (int x = 0; x < width; x++) {
michael@0 379 SkPMColor c = ctable[*src];
michael@0 380 cc &= c;
michael@0 381 if (c != 0) {
michael@0 382 dst[x] = c;
michael@0 383 }
michael@0 384 src += deltaSrc;
michael@0 385 }
michael@0 386 return cc != A32_MASK_IN_PLACE;
michael@0 387 }
michael@0 388
michael@0 389 static SkScaledBitmapSampler::RowProc get_index_to_8888_proc(const SkImageDecoder& decoder) {
michael@0 390 if (decoder.getRequireUnpremultipliedColors()) {
michael@0 391 // Unpremultiplied is not supported for an index source.
michael@0 392 return NULL;
michael@0 393 }
michael@0 394 // Dither makes no difference
michael@0 395 if (decoder.getSkipWritingZeroes()) {
michael@0 396 return Sample_Index_D8888_SkipZ;
michael@0 397 }
michael@0 398 return Sample_Index_D8888;
michael@0 399 }
michael@0 400
michael@0 401 static bool Sample_Index_D565(void* SK_RESTRICT dstRow,
michael@0 402 const uint8_t* SK_RESTRICT src,
michael@0 403 int width, int deltaSrc, int, const SkPMColor ctable[]) {
michael@0 404
michael@0 405 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
michael@0 406 for (int x = 0; x < width; x++) {
michael@0 407 dst[x] = SkPixel32ToPixel16(ctable[*src]);
michael@0 408 src += deltaSrc;
michael@0 409 }
michael@0 410 return false;
michael@0 411 }
michael@0 412
michael@0 413 static bool Sample_Index_D565_D(void* SK_RESTRICT dstRow,
michael@0 414 const uint8_t* SK_RESTRICT src, int width,
michael@0 415 int deltaSrc, int y, const SkPMColor ctable[]) {
michael@0 416
michael@0 417 uint16_t* SK_RESTRICT dst = (uint16_t*)dstRow;
michael@0 418 DITHER_565_SCAN(y);
michael@0 419
michael@0 420 for (int x = 0; x < width; x++) {
michael@0 421 SkPMColor c = ctable[*src];
michael@0 422 dst[x] = SkDitherRGBTo565(SkGetPackedR32(c), SkGetPackedG32(c),
michael@0 423 SkGetPackedB32(c), DITHER_VALUE(x));
michael@0 424 src += deltaSrc;
michael@0 425 }
michael@0 426 return false;
michael@0 427 }
michael@0 428
michael@0 429 static SkScaledBitmapSampler::RowProc get_index_to_565_proc(const SkImageDecoder& decoder) {
michael@0 430 // Unpremultiplied and skip zeroes make no difference
michael@0 431 if (decoder.getDitherImage()) {
michael@0 432 return Sample_Index_D565_D;
michael@0 433 }
michael@0 434 return Sample_Index_D565;
michael@0 435 }
michael@0 436
michael@0 437 static bool Sample_Index_D4444(void* SK_RESTRICT dstRow,
michael@0 438 const uint8_t* SK_RESTRICT src, int width,
michael@0 439 int deltaSrc, int y, const SkPMColor ctable[]) {
michael@0 440
michael@0 441 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 442 SkPMColor cc = A32_MASK_IN_PLACE;
michael@0 443 for (int x = 0; x < width; x++) {
michael@0 444 SkPMColor c = ctable[*src];
michael@0 445 cc &= c;
michael@0 446 dst[x] = SkPixel32ToPixel4444(c);
michael@0 447 src += deltaSrc;
michael@0 448 }
michael@0 449 return cc != A32_MASK_IN_PLACE;
michael@0 450 }
michael@0 451
michael@0 452 static bool Sample_Index_D4444_D(void* SK_RESTRICT dstRow,
michael@0 453 const uint8_t* SK_RESTRICT src, int width,
michael@0 454 int deltaSrc, int y, const SkPMColor ctable[]) {
michael@0 455
michael@0 456 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 457 SkPMColor cc = A32_MASK_IN_PLACE;
michael@0 458 DITHER_4444_SCAN(y);
michael@0 459
michael@0 460 for (int x = 0; x < width; x++) {
michael@0 461 SkPMColor c = ctable[*src];
michael@0 462 cc &= c;
michael@0 463 dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x));
michael@0 464 src += deltaSrc;
michael@0 465 }
michael@0 466 return cc != A32_MASK_IN_PLACE;
michael@0 467 }
michael@0 468
michael@0 469 static bool Sample_Index_D4444_SkipZ(void* SK_RESTRICT dstRow,
michael@0 470 const uint8_t* SK_RESTRICT src, int width,
michael@0 471 int deltaSrc, int y, const SkPMColor ctable[]) {
michael@0 472
michael@0 473 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 474 SkPMColor cc = A32_MASK_IN_PLACE;
michael@0 475 for (int x = 0; x < width; x++) {
michael@0 476 SkPMColor c = ctable[*src];
michael@0 477 cc &= c;
michael@0 478 if (c != 0) {
michael@0 479 dst[x] = SkPixel32ToPixel4444(c);
michael@0 480 }
michael@0 481 src += deltaSrc;
michael@0 482 }
michael@0 483 return cc != A32_MASK_IN_PLACE;
michael@0 484 }
michael@0 485
michael@0 486 static bool Sample_Index_D4444_D_SkipZ(void* SK_RESTRICT dstRow,
michael@0 487 const uint8_t* SK_RESTRICT src, int width,
michael@0 488 int deltaSrc, int y, const SkPMColor ctable[]) {
michael@0 489
michael@0 490 SkPMColor16* SK_RESTRICT dst = (SkPMColor16*)dstRow;
michael@0 491 SkPMColor cc = A32_MASK_IN_PLACE;
michael@0 492 DITHER_4444_SCAN(y);
michael@0 493
michael@0 494 for (int x = 0; x < width; x++) {
michael@0 495 SkPMColor c = ctable[*src];
michael@0 496 cc &= c;
michael@0 497 if (c != 0) {
michael@0 498 dst[x] = SkDitherARGB32To4444(c, DITHER_VALUE(x));
michael@0 499 }
michael@0 500 src += deltaSrc;
michael@0 501 }
michael@0 502 return cc != A32_MASK_IN_PLACE;
michael@0 503 }
michael@0 504
michael@0 505 static SkScaledBitmapSampler::RowProc get_index_to_4444_proc(const SkImageDecoder& decoder) {
michael@0 506 // Unpremul not allowed
michael@0 507 if (decoder.getRequireUnpremultipliedColors()) {
michael@0 508 return NULL;
michael@0 509 }
michael@0 510 const bool dither = decoder.getDitherImage();
michael@0 511 if (decoder.getSkipWritingZeroes()) {
michael@0 512 if (dither) {
michael@0 513 return Sample_Index_D4444_D_SkipZ;
michael@0 514 }
michael@0 515 return Sample_Index_D4444_SkipZ;
michael@0 516 }
michael@0 517 if (dither) {
michael@0 518 return Sample_Index_D4444_D;
michael@0 519 }
michael@0 520 return Sample_Index_D4444;
michael@0 521 }
michael@0 522
michael@0 523 static bool Sample_Index_DI(void* SK_RESTRICT dstRow,
michael@0 524 const uint8_t* SK_RESTRICT src,
michael@0 525 int width, int deltaSrc, int, const SkPMColor[]) {
michael@0 526 if (1 == deltaSrc) {
michael@0 527 memcpy(dstRow, src, width);
michael@0 528 } else {
michael@0 529 uint8_t* SK_RESTRICT dst = (uint8_t*)dstRow;
michael@0 530 for (int x = 0; x < width; x++) {
michael@0 531 dst[x] = src[0];
michael@0 532 src += deltaSrc;
michael@0 533 }
michael@0 534 }
michael@0 535 return false;
michael@0 536 }
michael@0 537
michael@0 538 static SkScaledBitmapSampler::RowProc get_index_to_index_proc(const SkImageDecoder& decoder) {
michael@0 539 // Unpremul not allowed
michael@0 540 if (decoder.getRequireUnpremultipliedColors()) {
michael@0 541 return NULL;
michael@0 542 }
michael@0 543 // Ignore dither and skip zeroes
michael@0 544 return Sample_Index_DI;
michael@0 545 }
michael@0 546
michael@0 547 // A8
michael@0 548 static bool Sample_Gray_DA8(void* SK_RESTRICT dstRow,
michael@0 549 const uint8_t* SK_RESTRICT src,
michael@0 550 int width, int deltaSrc, int,
michael@0 551 const SkPMColor[]) {
michael@0 552 // Sampling Gray to A8 uses the same function as Index to Index8,
michael@0 553 // except we assume that there is alpha for speed, since an A8
michael@0 554 // bitmap with no alpha is not interesting.
michael@0 555 (void) Sample_Index_DI(dstRow, src, width, deltaSrc, /* y unused */ 0,
michael@0 556 /* ctable unused */ NULL);
michael@0 557 return true;
michael@0 558 }
michael@0 559
michael@0 560 static SkScaledBitmapSampler::RowProc get_gray_to_A8_proc(const SkImageDecoder& decoder) {
michael@0 561 if (decoder.getRequireUnpremultipliedColors()) {
michael@0 562 return NULL;
michael@0 563 }
michael@0 564 // Ignore skip and dither.
michael@0 565 return Sample_Gray_DA8;
michael@0 566 }
michael@0 567
michael@0 568 typedef SkScaledBitmapSampler::RowProc (*RowProcChooser)(const SkImageDecoder& decoder);
michael@0 569 ///////////////////////////////////////////////////////////////////////////////
michael@0 570
michael@0 571 #include "SkScaledBitmapSampler.h"
michael@0 572
michael@0 573 SkScaledBitmapSampler::SkScaledBitmapSampler(int width, int height,
michael@0 574 int sampleSize) {
michael@0 575 fCTable = NULL;
michael@0 576 fDstRow = NULL;
michael@0 577 fRowProc = NULL;
michael@0 578
michael@0 579 if (width <= 0 || height <= 0) {
michael@0 580 sk_throw();
michael@0 581 }
michael@0 582
michael@0 583 SkDEBUGCODE(fSampleMode = kUninitialized_SampleMode);
michael@0 584
michael@0 585 if (sampleSize <= 1) {
michael@0 586 fScaledWidth = width;
michael@0 587 fScaledHeight = height;
michael@0 588 fX0 = fY0 = 0;
michael@0 589 fDX = fDY = 1;
michael@0 590 return;
michael@0 591 }
michael@0 592
michael@0 593 int dx = SkMin32(sampleSize, width);
michael@0 594 int dy = SkMin32(sampleSize, height);
michael@0 595
michael@0 596 fScaledWidth = width / dx;
michael@0 597 fScaledHeight = height / dy;
michael@0 598
michael@0 599 SkASSERT(fScaledWidth > 0);
michael@0 600 SkASSERT(fScaledHeight > 0);
michael@0 601
michael@0 602 fX0 = dx >> 1;
michael@0 603 fY0 = dy >> 1;
michael@0 604
michael@0 605 SkASSERT(fX0 >= 0 && fX0 < width);
michael@0 606 SkASSERT(fY0 >= 0 && fY0 < height);
michael@0 607
michael@0 608 fDX = dx;
michael@0 609 fDY = dy;
michael@0 610
michael@0 611 SkASSERT(fDX > 0 && (fX0 + fDX * (fScaledWidth - 1)) < width);
michael@0 612 SkASSERT(fDY > 0 && (fY0 + fDY * (fScaledHeight - 1)) < height);
michael@0 613 }
michael@0 614
michael@0 615 bool SkScaledBitmapSampler::begin(SkBitmap* dst, SrcConfig sc,
michael@0 616 const SkImageDecoder& decoder,
michael@0 617 const SkPMColor ctable[]) {
michael@0 618 static const RowProcChooser gProcChoosers[] = {
michael@0 619 get_gray_to_8888_proc,
michael@0 620 get_RGBx_to_8888_proc,
michael@0 621 get_RGBA_to_8888_proc,
michael@0 622 get_index_to_8888_proc,
michael@0 623 NULL, // 565 to 8888
michael@0 624
michael@0 625 get_gray_to_565_proc,
michael@0 626 get_RGBx_to_565_proc,
michael@0 627 get_RGBx_to_565_proc, // The source alpha will be ignored.
michael@0 628 get_index_to_565_proc,
michael@0 629 get_565_to_565_proc,
michael@0 630
michael@0 631 get_gray_to_4444_proc,
michael@0 632 get_RGBx_to_4444_proc,
michael@0 633 get_RGBA_to_4444_proc,
michael@0 634 get_index_to_4444_proc,
michael@0 635 NULL, // 565 to 4444
michael@0 636
michael@0 637 NULL, // gray to index
michael@0 638 NULL, // rgbx to index
michael@0 639 NULL, // rgba to index
michael@0 640 get_index_to_index_proc,
michael@0 641 NULL, // 565 to index
michael@0 642
michael@0 643 get_gray_to_A8_proc,
michael@0 644 NULL, // rgbx to a8
michael@0 645 NULL, // rgba to a8
michael@0 646 NULL, // index to a8
michael@0 647 NULL, // 565 to a8
michael@0 648 };
michael@0 649
michael@0 650 // The jump between dst configs in the table
michael@0 651 static const int gProcDstConfigSpan = 5;
michael@0 652 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gProcChoosers) == 5 * gProcDstConfigSpan,
michael@0 653 gProcs_has_the_wrong_number_of_entries);
michael@0 654
michael@0 655 fCTable = ctable;
michael@0 656
michael@0 657 int index = 0;
michael@0 658 switch (sc) {
michael@0 659 case SkScaledBitmapSampler::kGray:
michael@0 660 fSrcPixelSize = 1;
michael@0 661 index += 0;
michael@0 662 break;
michael@0 663 case SkScaledBitmapSampler::kRGB:
michael@0 664 fSrcPixelSize = 3;
michael@0 665 index += 1;
michael@0 666 break;
michael@0 667 case SkScaledBitmapSampler::kRGBX:
michael@0 668 fSrcPixelSize = 4;
michael@0 669 index += 1;
michael@0 670 break;
michael@0 671 case SkScaledBitmapSampler::kRGBA:
michael@0 672 fSrcPixelSize = 4;
michael@0 673 index += 2;
michael@0 674 break;
michael@0 675 case SkScaledBitmapSampler::kIndex:
michael@0 676 fSrcPixelSize = 1;
michael@0 677 index += 3;
michael@0 678 break;
michael@0 679 case SkScaledBitmapSampler::kRGB_565:
michael@0 680 fSrcPixelSize = 2;
michael@0 681 index += 4;
michael@0 682 break;
michael@0 683 default:
michael@0 684 return false;
michael@0 685 }
michael@0 686
michael@0 687 switch (dst->config()) {
michael@0 688 case SkBitmap::kARGB_8888_Config:
michael@0 689 index += 0 * gProcDstConfigSpan;
michael@0 690 break;
michael@0 691 case SkBitmap::kRGB_565_Config:
michael@0 692 index += 1 * gProcDstConfigSpan;
michael@0 693 break;
michael@0 694 case SkBitmap::kARGB_4444_Config:
michael@0 695 index += 2 * gProcDstConfigSpan;
michael@0 696 break;
michael@0 697 case SkBitmap::kIndex8_Config:
michael@0 698 index += 3 * gProcDstConfigSpan;
michael@0 699 break;
michael@0 700 case SkBitmap::kA8_Config:
michael@0 701 index += 4 * gProcDstConfigSpan;
michael@0 702 break;
michael@0 703 default:
michael@0 704 return false;
michael@0 705 }
michael@0 706
michael@0 707 RowProcChooser chooser = gProcChoosers[index];
michael@0 708 if (NULL == chooser) {
michael@0 709 fRowProc = NULL;
michael@0 710 } else {
michael@0 711 fRowProc = chooser(decoder);
michael@0 712 }
michael@0 713 fDstRow = (char*)dst->getPixels();
michael@0 714 fDstRowBytes = dst->rowBytes();
michael@0 715 fCurrY = 0;
michael@0 716 return fRowProc != NULL;
michael@0 717 }
michael@0 718
michael@0 719 bool SkScaledBitmapSampler::next(const uint8_t* SK_RESTRICT src) {
michael@0 720 SkASSERT(kInterlaced_SampleMode != fSampleMode);
michael@0 721 SkDEBUGCODE(fSampleMode = kConsecutive_SampleMode);
michael@0 722 SkASSERT((unsigned)fCurrY < (unsigned)fScaledHeight);
michael@0 723
michael@0 724 bool hadAlpha = fRowProc(fDstRow, src + fX0 * fSrcPixelSize, fScaledWidth,
michael@0 725 fDX * fSrcPixelSize, fCurrY, fCTable);
michael@0 726 fDstRow += fDstRowBytes;
michael@0 727 fCurrY += 1;
michael@0 728 return hadAlpha;
michael@0 729 }
michael@0 730
michael@0 731 bool SkScaledBitmapSampler::sampleInterlaced(const uint8_t* SK_RESTRICT src, int srcY) {
michael@0 732 SkASSERT(kConsecutive_SampleMode != fSampleMode);
michael@0 733 SkDEBUGCODE(fSampleMode = kInterlaced_SampleMode);
michael@0 734 // Any line that should be a part of the destination can be created by the formula:
michael@0 735 // fY0 + (some multiplier) * fDY
michael@0 736 // so if srcY - fY0 is not an integer multiple of fDY that srcY will be skipped.
michael@0 737 const int srcYMinusY0 = srcY - fY0;
michael@0 738 if (srcYMinusY0 % fDY != 0) {
michael@0 739 // This line is not part of the output, so return false for alpha, since we have
michael@0 740 // not added an alpha to the output.
michael@0 741 return false;
michael@0 742 }
michael@0 743 // Unlike in next(), where the data is used sequentially, this function skips around,
michael@0 744 // so fDstRow and fCurrY are never updated. fDstRow must always be the starting point
michael@0 745 // of the destination bitmap's pixels, which is used to calculate the destination row
michael@0 746 // each time this function is called.
michael@0 747 const int dstY = srcYMinusY0 / fDY;
michael@0 748 SkASSERT(dstY < fScaledHeight);
michael@0 749 char* dstRow = fDstRow + dstY * fDstRowBytes;
michael@0 750 return fRowProc(dstRow, src + fX0 * fSrcPixelSize, fScaledWidth,
michael@0 751 fDX * fSrcPixelSize, dstY, fCTable);
michael@0 752 }
michael@0 753
michael@0 754 #ifdef SK_DEBUG
michael@0 755 // The following code is for a test to ensure that changing the method to get the right row proc
michael@0 756 // did not change the row proc unintentionally. Tested by ImageDecodingTest.cpp
michael@0 757
michael@0 758 // friend of SkScaledBitmapSampler solely for the purpose of accessing fRowProc.
michael@0 759 class RowProcTester {
michael@0 760 public:
michael@0 761 static SkScaledBitmapSampler::RowProc getRowProc(const SkScaledBitmapSampler& sampler) {
michael@0 762 return sampler.fRowProc;
michael@0 763 }
michael@0 764 };
michael@0 765
michael@0 766
michael@0 767 // Table showing the expected RowProc for each combination of inputs.
michael@0 768 // Table formated as follows:
michael@0 769 // Each group of 5 consecutive rows represents sampling from a single
michael@0 770 // SkScaledBitmapSampler::SrcConfig.
michael@0 771 // Within each set, each row represents a different destination SkBitmap::Config
michael@0 772 // Each column represents a different combination of dither and unpremul.
michael@0 773 // D = dither ~D = no dither
michael@0 774 // U = unpremul ~U = no unpremul
michael@0 775 // ~D~U D~U ~DU DU
michael@0 776 SkScaledBitmapSampler::RowProc gTestProcs[] = {
michael@0 777 // Gray
michael@0 778 Sample_Gray_DA8, Sample_Gray_DA8, NULL, NULL, // to A8
michael@0 779 NULL, NULL, NULL, NULL, // to Index8
michael@0 780 Sample_Gray_D565, Sample_Gray_D565_D, Sample_Gray_D565, Sample_Gray_D565_D, // to 565
michael@0 781 Sample_Gray_D4444, Sample_Gray_D4444_D, Sample_Gray_D4444, Sample_Gray_D4444_D, // to 4444
michael@0 782 Sample_Gray_D8888, Sample_Gray_D8888, Sample_Gray_D8888, Sample_Gray_D8888, // to 8888
michael@0 783 // Index
michael@0 784 NULL, NULL, NULL, NULL, // to A8
michael@0 785 Sample_Index_DI, Sample_Index_DI, NULL, NULL, // to Index8
michael@0 786 Sample_Index_D565, Sample_Index_D565_D, Sample_Index_D565, Sample_Index_D565_D, // to 565
michael@0 787 Sample_Index_D4444, Sample_Index_D4444_D, NULL, NULL, // to 4444
michael@0 788 Sample_Index_D8888, Sample_Index_D8888, NULL, NULL, // to 8888
michael@0 789 // RGB
michael@0 790 NULL, NULL, NULL, NULL, // to A8
michael@0 791 NULL, NULL, NULL, NULL, // to Index8
michael@0 792 Sample_RGBx_D565, Sample_RGBx_D565_D, Sample_RGBx_D565, Sample_RGBx_D565_D, // to 565
michael@0 793 Sample_RGBx_D4444, Sample_RGBx_D4444_D, Sample_RGBx_D4444, Sample_RGBx_D4444_D, // to 4444
michael@0 794 Sample_RGBx_D8888, Sample_RGBx_D8888, Sample_RGBx_D8888, Sample_RGBx_D8888, // to 8888
michael@0 795 // RGBx is the same as RGB
michael@0 796 NULL, NULL, NULL, NULL, // to A8
michael@0 797 NULL, NULL, NULL, NULL, // to Index8
michael@0 798 Sample_RGBx_D565, Sample_RGBx_D565_D, Sample_RGBx_D565, Sample_RGBx_D565_D, // to 565
michael@0 799 Sample_RGBx_D4444, Sample_RGBx_D4444_D, Sample_RGBx_D4444, Sample_RGBx_D4444_D, // to 4444
michael@0 800 Sample_RGBx_D8888, Sample_RGBx_D8888, Sample_RGBx_D8888, Sample_RGBx_D8888, // to 8888
michael@0 801 // RGBA
michael@0 802 NULL, NULL, NULL, NULL, // to A8
michael@0 803 NULL, NULL, NULL, NULL, // to Index8
michael@0 804 Sample_RGBx_D565, Sample_RGBx_D565_D, Sample_RGBx_D565, Sample_RGBx_D565_D, // to 565
michael@0 805 Sample_RGBA_D4444, Sample_RGBA_D4444_D, NULL, NULL, // to 4444
michael@0 806 Sample_RGBA_D8888, Sample_RGBA_D8888, Sample_RGBA_D8888_Unpremul, Sample_RGBA_D8888_Unpremul, // to 8888
michael@0 807 // RGB_565
michael@0 808 NULL, NULL, NULL, NULL, // to A8
michael@0 809 NULL, NULL, NULL, NULL, // to Index8
michael@0 810 Sample_D565_D565, Sample_D565_D565, Sample_D565_D565, Sample_D565_D565, // to 565
michael@0 811 NULL, NULL, NULL, NULL, // to 4444
michael@0 812 NULL, NULL, NULL, NULL, // to 8888
michael@0 813 };
michael@0 814
michael@0 815 // Dummy class that allows instantiation of an ImageDecoder, so begin can query its fields.
michael@0 816 class DummyDecoder : public SkImageDecoder {
michael@0 817 public:
michael@0 818 DummyDecoder() {}
michael@0 819 protected:
michael@0 820 virtual bool onDecode(SkStream*, SkBitmap*, SkImageDecoder::Mode) SK_OVERRIDE {
michael@0 821 return false;
michael@0 822 }
michael@0 823 };
michael@0 824
michael@0 825 void test_row_proc_choice();
michael@0 826 void test_row_proc_choice() {
michael@0 827 SkBitmap dummyBitmap;
michael@0 828 DummyDecoder dummyDecoder;
michael@0 829 size_t procCounter = 0;
michael@0 830 for (int sc = SkScaledBitmapSampler::kGray; sc <= SkScaledBitmapSampler::kRGB_565; ++sc) {
michael@0 831 for (int c = SkBitmap::kA8_Config; c <= SkBitmap::kARGB_8888_Config; ++c) {
michael@0 832 for (int unpremul = 0; unpremul <= 1; ++unpremul) {
michael@0 833 for (int dither = 0; dither <= 1; ++dither) {
michael@0 834 // Arbitrary width/height/sampleSize to allow SkScaledBitmapSampler to
michael@0 835 // be considered valid.
michael@0 836 SkScaledBitmapSampler sampler(10, 10, 1);
michael@0 837 dummyBitmap.setConfig((SkBitmap::Config) c, 10, 10);
michael@0 838 dummyDecoder.setDitherImage(SkToBool(dither));
michael@0 839 dummyDecoder.setRequireUnpremultipliedColors(SkToBool(unpremul));
michael@0 840 sampler.begin(&dummyBitmap, (SkScaledBitmapSampler::SrcConfig) sc,
michael@0 841 dummyDecoder);
michael@0 842 SkScaledBitmapSampler::RowProc expected = gTestProcs[procCounter];
michael@0 843 SkScaledBitmapSampler::RowProc actual = RowProcTester::getRowProc(sampler);
michael@0 844 SkASSERT(expected == actual);
michael@0 845 procCounter++;
michael@0 846 }
michael@0 847 }
michael@0 848 }
michael@0 849 }
michael@0 850 SkASSERT(SK_ARRAY_COUNT(gTestProcs) == procCounter);
michael@0 851 }
michael@0 852 #endif // SK_DEBUG

mercurial