Unpremultiply SkBitmaps for PDF output

BUG=skia:236, chromium:175548
R=vandebo@chromium.org, edisonn@google.com, reed@google.com

Author: richardlin@chromium.org

Review URL: https://chromiumcodereview.appspot.com/22329003

git-svn-id: http://skia.googlecode.com/svn/trunk@10725 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
commit-bot@chromium.org 2013-08-14 22:02:23 +00:00
parent ab882bf205
commit 5d18cee176
3 changed files with 200 additions and 20 deletions

131
gm/bitmappremul.cpp Normal file
View File

@ -0,0 +1,131 @@
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
#include "SkBitmap.h"
#include "SkCanvas.h"
#include "SkColorPriv.h"
/**
* This GM checks that bitmap pixels are unpremultiplied before being exported
* to other formats. If unpremultiplication is implemented properly, this
* GM should come out completely white. If not, this GM looks like a row of two
* greyscale gradients above a row of grey lines.
* This tests both the ARGB4444 and ARGB8888 bitmap configurations.
*/
static const int SLIDE_SIZE = 256;
static const int PIXEL_SIZE_8888 = SLIDE_SIZE / 256;
static const int PIXEL_SIZE_4444 = SLIDE_SIZE / 16;
static SkBitmap init_bitmap(SkBitmap::Config config) {
SkBitmap bitmap;
bitmap.setConfig(config, SLIDE_SIZE, SLIDE_SIZE);
bitmap.allocPixels();
bitmap.eraseColor(SK_ColorWHITE);
return bitmap;
}
static SkBitmap make_argb8888_gradient() {
SkBitmap bitmap = init_bitmap(SkBitmap::kARGB_8888_Config);
uint8_t rowColor = 0;
for (int y = 0; y < SLIDE_SIZE; y++) {
uint32_t* dst = bitmap.getAddr32(0, y);
for (int x = 0; x < SLIDE_SIZE; x++) {
dst[x] = SkPackARGB32(rowColor, rowColor,
rowColor, rowColor);
}
if (y % PIXEL_SIZE_8888 == PIXEL_SIZE_8888 - 1) {
rowColor++;
}
}
return bitmap;
}
static SkBitmap make_argb4444_gradient() {
SkBitmap bitmap = init_bitmap(SkBitmap::kARGB_4444_Config);
uint8_t rowColor = 0;
for (int y = 0; y < SLIDE_SIZE; y++) {
uint16_t* dst = bitmap.getAddr16(0, y);
for (int x = 0; x < SLIDE_SIZE; x++) {
dst[x] = SkPackARGB4444(rowColor, rowColor,
rowColor, rowColor);
}
if (y % PIXEL_SIZE_4444 == PIXEL_SIZE_4444 - 1) {
rowColor++;
}
}
return bitmap;
}
static SkBitmap make_argb8888_stripes() {
SkBitmap bitmap = init_bitmap(SkBitmap::kARGB_8888_Config);
uint8_t rowColor = 0;
for (int y = 0; y < SLIDE_SIZE; y++) {
uint32_t* dst = bitmap.getAddr32(0, y);
for (int x = 0; x < SLIDE_SIZE; x++) {
dst[x] = SkPackARGB32(rowColor, rowColor,
rowColor, rowColor);
}
if (rowColor == 0) {
rowColor = 255;
} else {
rowColor = 0;
}
}
return bitmap;
}
static SkBitmap make_argb4444_stripes() {
SkBitmap bitmap = init_bitmap(SkBitmap::kARGB_4444_Config);
uint8_t rowColor = 0;;
for (int y = 0; y < SLIDE_SIZE; y++) {
uint16_t* dst = bitmap.getAddr16(0, y);
for (int x = 0; x < SLIDE_SIZE; x++) {
dst[x] = SkPackARGB4444(rowColor, rowColor,
rowColor, rowColor);
}
if (rowColor == 0) {
rowColor = 15;
} else {
rowColor = 0;
}
}
return bitmap;
}
namespace skiagm {
class BitmapPremulGM : public GM {
public:
BitmapPremulGM() {
this->setBGColor(0xFFFFFFFF);
}
protected:
SkString onShortName() SK_OVERRIDE {
return SkString("bitmap_premul");
}
virtual SkISize onISize() SK_OVERRIDE {
return SkISize::Make(512, 512);
}
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
SkScalar slideSize = SLIDE_SIZE;
canvas->drawBitmap(make_argb8888_gradient(), 0, 0);
canvas->drawBitmap(make_argb4444_gradient(), slideSize, 0);
canvas->drawBitmap(make_argb8888_stripes(), 0, slideSize);
canvas->drawBitmap(make_argb4444_stripes(), slideSize, slideSize);
}
private:
typedef GM INHERITED;
};
DEF_GM( return new BitmapPremulGM; )
}

View File

@ -12,6 +12,7 @@
'../gm/bitmapcopy.cpp', '../gm/bitmapcopy.cpp',
'../gm/bitmapmatrix.cpp', '../gm/bitmapmatrix.cpp',
'../gm/bitmapfilters.cpp', '../gm/bitmapfilters.cpp',
'../gm/bitmappremul.cpp',
'../gm/bitmaprect.cpp', '../gm/bitmaprect.cpp',
'../gm/bitmaprecttest.cpp', '../gm/bitmaprecttest.cpp',
'../gm/bitmapscroll.cpp', '../gm/bitmapscroll.cpp',

View File

@ -18,6 +18,57 @@
namespace { namespace {
static void unpremultiply_and_pack_argb8888(uint32_t src, uint8_t dst[3]) {
uint8_t alpha = SkGetPackedA32(src);
if (alpha != SK_AlphaOPAQUE) {
SkColor unpremul = SkUnPreMultiply::PMColorToColor(src);
dst[0] = SkColorGetR(unpremul);
dst[1] = SkColorGetG(unpremul);
dst[2] = SkColorGetB(unpremul);
} else {
dst[0] = SkGetPackedR32(src);
dst[1] = SkGetPackedG32(src);
dst[2] = SkGetPackedB32(src);
}
}
static void unpremultiply_and_pack_argb4444(uint16_t src0, uint16_t src1,
uint8_t dst[3]) {
// Unpack and transform the alpha values from 4 bits to 8 bits.
// This is necessary since the unpremultiply functions expect to work in
// 8-bit space, but we are passing in 4-bit values. Since we scale up
// the alpha, we scale down the amount the value is increased by, so that
// the results are correct for 4-bit color components.
uint8_t alpha0 = SkGetPackedA4444(src0);
alpha0 = alpha0 | (alpha0 << 4);
if (alpha0 != SK_AlphaOPAQUE) {
SkUnPreMultiply::Scale scale0 = SkUnPreMultiply::GetScale(alpha0);
dst[0] = SkUnPreMultiply::ApplyScale(scale0,
SkGetPackedR4444(src0)) << 4;
dst[0] |= SkUnPreMultiply::ApplyScale(scale0, SkGetPackedG4444(src0));
dst[1] = SkUnPreMultiply::ApplyScale(scale0,
SkGetPackedB4444(src0)) << 4;
} else {
dst[0] = SkGetPackedR4444(src0) << 4;
dst[0] |= SkGetPackedG4444(src0);
dst[1] = SkGetPackedB4444(src0) << 4;
}
uint8_t alpha1 = SkGetPackedA4444(src1);
alpha1 = alpha1 | (alpha1 << 4);
if (alpha1 != SK_AlphaOPAQUE) {
SkUnPreMultiply::Scale scale1 = SkUnPreMultiply::GetScale(alpha1);
dst[1] |= SkUnPreMultiply::ApplyScale(scale1, SkGetPackedR4444(src1));
dst[2] = SkUnPreMultiply::ApplyScale(scale1,
SkGetPackedG4444(src1)) << 4;
dst[2] |= SkUnPreMultiply::ApplyScale(scale1, SkGetPackedB4444(src1));
} else {
dst[1] |= SkGetPackedR4444(src1);
dst[2] = SkGetPackedG4444(src1) << 4;
dst[2] |= SkGetPackedB4444(src1);
}
}
void extractImageData(const SkBitmap& bitmap, const SkIRect& srcRect, void extractImageData(const SkBitmap& bitmap, const SkIRect& srcRect,
SkStream** imageData, SkStream** alphaData) { SkStream** imageData, SkStream** alphaData) {
SkMemoryStream* image = NULL; SkMemoryStream* image = NULL;
@ -49,36 +100,35 @@ void extractImageData(const SkBitmap& bitmap, const SkIRect& srcRect,
uint16_t* src = bitmap.getAddr16(0, y); uint16_t* src = bitmap.getAddr16(0, y);
int x; int x;
for (x = srcRect.fLeft; x + 1 < srcRect.fRight; x += 2) { for (x = srcRect.fLeft; x + 1 < srcRect.fRight; x += 2) {
dst[0] = (SkGetPackedR4444(src[x]) << 4) |
SkGetPackedG4444(src[x]);
dst[1] = (SkGetPackedB4444(src[x]) << 4) |
SkGetPackedR4444(src[x + 1]);
dst[2] = (SkGetPackedG4444(src[x + 1]) << 4) |
SkGetPackedB4444(src[x + 1]);
dst += 3;
alphaDst[0] = (SkGetPackedA4444(src[x]) << 4) | alphaDst[0] = (SkGetPackedA4444(src[x]) << 4) |
SkGetPackedA4444(src[x + 1]); SkGetPackedA4444(src[x + 1]);
if (alphaDst[0] != 0xFF) { if (alphaDst[0] != SK_AlphaOPAQUE) {
hasAlpha = true; hasAlpha = true;
} }
if (alphaDst[0]) { if (alphaDst[0]) {
isTransparent = false; isTransparent = false;
} }
unpremultiply_and_pack_argb4444(src[x], src[x + 1], dst);
alphaDst++; alphaDst++;
dst += 3;
} }
if (srcRect.width() & 1) { if (srcRect.width() & 1) {
dst[0] = (SkGetPackedR4444(src[x]) << 4) | alphaDst[0] = SkGetPackedA4444(src[x]) << 4;
SkGetPackedG4444(src[x]); // Use a buffer to translate from the usual 2 4444 values
dst[1] = (SkGetPackedB4444(src[x]) << 4); // in 3 bytes to the single 4444 value in 2 bytes.
dst += 2; uint8_t buffer[3];
alphaDst[0] = (SkGetPackedA4444(src[x]) << 4);
if (alphaDst[0] != 0xF0) { if (alphaDst[0] != 0xF0) {
hasAlpha = true; hasAlpha = true;
} }
if (alphaDst[0] & 0xF0) { if (alphaDst[0] & 0xF0) {
isTransparent = false; isTransparent = false;
} }
unpremultiply_and_pack_argb4444(src[x], 0x00, buffer);
dst[0] = buffer[0];
dst[1] = buffer[1];
alphaDst++; alphaDst++;
dst += 2;
} }
} }
break; break;
@ -108,18 +158,16 @@ void extractImageData(const SkBitmap& bitmap, const SkIRect& srcRect,
for (int y = srcRect.fTop; y < srcRect.fBottom; y++) { for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
uint32_t* src = bitmap.getAddr32(0, y); uint32_t* src = bitmap.getAddr32(0, y);
for (int x = srcRect.fLeft; x < srcRect.fRight; x++) { for (int x = srcRect.fLeft; x < srcRect.fRight; x++) {
dst[0] = SkGetPackedR32(src[x]);
dst[1] = SkGetPackedG32(src[x]);
dst[2] = SkGetPackedB32(src[x]);
dst += 3;
alphaDst[0] = SkGetPackedA32(src[x]); alphaDst[0] = SkGetPackedA32(src[x]);
if (alphaDst[0] != 0xFF) { if (alphaDst[0] != SK_AlphaOPAQUE) {
hasAlpha = true; hasAlpha = true;
} }
if (alphaDst[0]) { if (alphaDst[0] != SK_AlphaTRANSPARENT) {
isTransparent = false; isTransparent = false;
} }
unpremultiply_and_pack_argb8888(src[x], dst);
alphaDst++; alphaDst++;
dst += 3;
} }
} }
break; break;