Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: src/images/SkScaledBitmapSampler.cpp

Issue 16410009: Add an option to create unpremultiplied bitmaps. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1
2 /* 1 /*
3 * Copyright 2007 The Android Open Source Project 2 * Copyright 2007 The Android Open Source Project
4 * 3 *
5 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file. 5 * found in the LICENSE file.
7 */ 6 */
8 7
9 8
10 #include "SkScaledBitmapSampler.h" 9 #include "SkScaledBitmapSampler.h"
11 #include "SkBitmap.h" 10 #include "SkBitmap.h"
12 #include "SkColorPriv.h" 11 #include "SkColorPriv.h"
13 #include "SkDither.h" 12 #include "SkDither.h"
13 #include "SkTypes.h"
14 14
15 // 8888 15 // 8888
16 16
17 static bool Sample_Gray_D8888(void* SK_RESTRICT dstRow, 17 static bool Sample_Gray_D8888(void* SK_RESTRICT dstRow,
18 const uint8_t* SK_RESTRICT src, 18 const uint8_t* SK_RESTRICT src,
19 int width, int deltaSrc, int, const SkPMColor[]) { 19 int width, int deltaSrc, int, const SkPMColor[]) {
20 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow; 20 SkPMColor* SK_RESTRICT dst = (SkPMColor*)dstRow;
21 for (int x = 0; x < width; x++) { 21 for (int x = 0; x < width; x++) {
22 dst[x] = SkPackARGB32(0xFF, src[0], src[0], src[0]); 22 dst[x] = SkPackARGB32(0xFF, src[0], src[0], src[0]);
23 src += deltaSrc; 23 src += deltaSrc;
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 } else { 282 } else {
283 uint8_t* SK_RESTRICT dst = (uint8_t*)dstRow; 283 uint8_t* SK_RESTRICT dst = (uint8_t*)dstRow;
284 for (int x = 0; x < width; x++) { 284 for (int x = 0; x < width; x++) {
285 dst[x] = src[0]; 285 dst[x] = src[0];
286 src += deltaSrc; 286 src += deltaSrc;
287 } 287 }
288 } 288 }
289 return false; 289 return false;
290 } 290 }
291 291
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 SkUnPMColor* SK_RESTRICT dst = reinterpret_cast<SkUnPMColor*>(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 alph a
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 SkUnPMColor* SK_RESTRICT dst = reinterpret_cast<SkUnPMColor*>(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
292 /////////////////////////////////////////////////////////////////////////////// 327 ///////////////////////////////////////////////////////////////////////////////
293 328
294 #include "SkScaledBitmapSampler.h" 329 #include "SkScaledBitmapSampler.h"
295 330
296 SkScaledBitmapSampler::SkScaledBitmapSampler(int width, int height, 331 SkScaledBitmapSampler::SkScaledBitmapSampler(int width, int height,
297 int sampleSize) { 332 int sampleSize) {
298 fCTable = NULL; 333 fCTable = NULL;
299 fDstRow = NULL; 334 fDstRow = NULL;
300 fRowProc = NULL; 335 fRowProc = NULL;
301 336
(...skipping 28 matching lines...) Expand all
330 fDY = dy; 365 fDY = dy;
331 366
332 SkASSERT(fDX > 0 && (fX0 + fDX * (fScaledWidth - 1)) < width); 367 SkASSERT(fDX > 0 && (fX0 + fDX * (fScaledWidth - 1)) < width);
333 SkASSERT(fDY > 0 && (fY0 + fDY * (fScaledHeight - 1)) < height); 368 SkASSERT(fDY > 0 && (fY0 + fDY * (fScaledHeight - 1)) < height);
334 } 369 }
335 370
336 bool SkScaledBitmapSampler::begin(SkBitmap* dst, SrcConfig sc, bool dither, 371 bool SkScaledBitmapSampler::begin(SkBitmap* dst, SrcConfig sc, bool dither,
337 const SkPMColor ctable[]) { 372 const SkPMColor ctable[]) {
338 static const RowProc gProcs[] = { 373 static const RowProc gProcs[] = {
339 // 8888 (no dither distinction) 374 // 8888 (no dither distinction)
340 Sample_Gray_D8888, Sample_Gray_D8888, 375 Sample_Gray_D8888, Sample_Gray_D8888,
341 Sample_RGBx_D8888, Sample_RGBx_D8888, 376 Sample_RGBx_D8888, Sample_RGBx_D8888,
342 Sample_RGBA_D8888, Sample_RGBA_D8888, 377 Sample_RGBA_D8888, Sample_RGBA_D8888,
343 Sample_Index_D8888, Sample_Index_D8888, 378 Sample_Index_D8888, Sample_Index_D8888,
344 NULL, NULL, 379 NULL, NULL,
345 // 565 (no alpha distinction) 380 // 565 (no alpha distinction)
346 Sample_Gray_D565, Sample_Gray_D565_D, 381 Sample_Gray_D565, Sample_Gray_D565_D,
347 Sample_RGBx_D565, Sample_RGBx_D565_D, 382 Sample_RGBx_D565, Sample_RGBx_D565_D,
348 Sample_RGBx_D565, Sample_RGBx_D565_D, 383 Sample_RGBx_D565, Sample_RGBx_D565_D,
349 Sample_Index_D565, Sample_Index_D565_D, 384 Sample_Index_D565, Sample_Index_D565_D,
350 Sample_D565_D565, Sample_D565_D565, 385 Sample_D565_D565, Sample_D565_D565,
351 // 4444 386 // 4444
352 Sample_Gray_D4444, Sample_Gray_D4444_D, 387 Sample_Gray_D4444, Sample_Gray_D4444_D,
353 Sample_RGBx_D4444, Sample_RGBx_D4444_D, 388 Sample_RGBx_D4444, Sample_RGBx_D4444_D,
354 Sample_RGBA_D4444, Sample_RGBA_D4444_D, 389 Sample_RGBA_D4444, Sample_RGBA_D4444_D,
355 Sample_Index_D4444, Sample_Index_D4444_D, 390 Sample_Index_D4444, Sample_Index_D4444_D,
356 NULL, NULL, 391 NULL, NULL,
357 // Index8 392 // Index8
358 NULL, NULL, 393 NULL, NULL,
359 NULL, NULL, 394 NULL, NULL,
360 NULL, NULL, 395 NULL, NULL,
361 Sample_Index_DI, Sample_Index_DI, 396 Sample_Index_DI, Sample_Index_DI,
362 NULL, NULL, 397 NULL, NULL,
398 // 8888 Unpremul (no dither distinction)
399 Sample_Gray_D8888_Unpremul, Sample_Gray_D8888_Unpremul,
400 Sample_RGBx_D8888, Sample_RGBx_D8888,
401 Sample_RGBA_D8888_Unpremul, Sample_RGBA_D8888_Unpremul,
402 Sample_Index_D8888, Sample_Index_D8888,
403 NULL, NULL,
363 }; 404 };
405 // The jump between dst configs in the table
406 static const int gProcDstConfigSpan = 10;
407 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(gProcs) == 5 * gProcDstConfigSpan,
408 gProcs_has_the_wrong_number_of_entries);
364 409
365 fCTable = ctable; 410 fCTable = ctable;
366 411
367 int index = 0; 412 int index = 0;
368 if (dither) { 413 if (dither) {
369 index += 1; 414 index += 1;
370 } 415 }
371 switch (sc) { 416 switch (sc) {
372 case SkScaledBitmapSampler::kGray: 417 case SkScaledBitmapSampler::kGray:
373 fSrcPixelSize = 1; 418 fSrcPixelSize = 1;
(...skipping 18 matching lines...) Expand all
392 case SkScaledBitmapSampler::kRGB_565: 437 case SkScaledBitmapSampler::kRGB_565:
393 fSrcPixelSize = 2; 438 fSrcPixelSize = 2;
394 index += 8; 439 index += 8;
395 break; 440 break;
396 default: 441 default:
397 return false; 442 return false;
398 } 443 }
399 444
400 switch (dst->config()) { 445 switch (dst->config()) {
401 case SkBitmap::kARGB_8888_Config: 446 case SkBitmap::kARGB_8888_Config:
402 index += 0; 447 index += 0 * gProcDstConfigSpan;
403 break; 448 break;
404 case SkBitmap::kRGB_565_Config: 449 case SkBitmap::kRGB_565_Config:
405 index += 10; 450 index += 1 * gProcDstConfigSpan;
406 break; 451 break;
407 case SkBitmap::kARGB_4444_Config: 452 case SkBitmap::kARGB_4444_Config:
408 index += 20; 453 index += 2 * gProcDstConfigSpan;
409 break; 454 break;
410 case SkBitmap::kIndex8_Config: 455 case SkBitmap::kIndex8_Config:
411 index += 30; 456 index += 3 * gProcDstConfigSpan;
412 break; 457 break;
413 default: 458 default:
414 return false; 459 return false;
415 } 460 }
416 461
462 if (!dst->premultiplied()) {
463 SkASSERT(dst->config() == SkBitmap::kARGB_8888_Config);
464 index += 4 * gProcDstConfigSpan;
465 }
466
417 fRowProc = gProcs[index]; 467 fRowProc = gProcs[index];
418 fDstRow = (char*)dst->getPixels(); 468 fDstRow = (char*)dst->getPixels();
419 fDstRowBytes = dst->rowBytes(); 469 fDstRowBytes = dst->rowBytes();
420 fCurrY = 0; 470 fCurrY = 0;
421 return fRowProc != NULL; 471 return fRowProc != NULL;
422 } 472 }
423 473
424 bool SkScaledBitmapSampler::next(const uint8_t* SK_RESTRICT src) { 474 bool SkScaledBitmapSampler::next(const uint8_t* SK_RESTRICT src) {
425 SkASSERT((unsigned)fCurrY < (unsigned)fScaledHeight); 475 SkASSERT((unsigned)fCurrY < (unsigned)fScaledHeight);
426 476
427 bool hadAlpha = fRowProc(fDstRow, src + fX0 * fSrcPixelSize, fScaledWidth, 477 bool hadAlpha = fRowProc(fDstRow, src + fX0 * fSrcPixelSize, fScaledWidth,
428 fDX * fSrcPixelSize, fCurrY, fCTable); 478 fDX * fSrcPixelSize, fCurrY, fCTable);
429 fDstRow += fDstRowBytes; 479 fDstRow += fDstRowBytes;
430 fCurrY += 1; 480 fCurrY += 1;
431 return hadAlpha; 481 return hadAlpha;
432 } 482 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698