aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-08-14 22:02:23 +0000
committerGravatar commit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>2013-08-14 22:02:23 +0000
commit5d18cee176b8a8c01138a43fd561881090ebc810 (patch)
tree6369854951d0db95bf126d6441b661e3a8c5317b
parentab882bf205affcd29f62f764681bfdf09fde004e (diff)
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
-rw-r--r--gm/bitmappremul.cpp131
-rw-r--r--gyp/gmslides.gypi1
-rw-r--r--src/pdf/SkPDFImage.cpp88
3 files changed, 200 insertions, 20 deletions
diff --git a/gm/bitmappremul.cpp b/gm/bitmappremul.cpp
new file mode 100644
index 0000000000..9f0c1e8cb9
--- /dev/null
+++ b/gm/bitmappremul.cpp
@@ -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; )
+}
diff --git a/gyp/gmslides.gypi b/gyp/gmslides.gypi
index bc0fc65415..524d63a2a7 100644
--- a/gyp/gmslides.gypi
+++ b/gyp/gmslides.gypi
@@ -12,6 +12,7 @@
'../gm/bitmapcopy.cpp',
'../gm/bitmapmatrix.cpp',
'../gm/bitmapfilters.cpp',
+ '../gm/bitmappremul.cpp',
'../gm/bitmaprect.cpp',
'../gm/bitmaprecttest.cpp',
'../gm/bitmapscroll.cpp',
diff --git a/src/pdf/SkPDFImage.cpp b/src/pdf/SkPDFImage.cpp
index a5cb4c20d1..a4872e25f8 100644
--- a/src/pdf/SkPDFImage.cpp
+++ b/src/pdf/SkPDFImage.cpp
@@ -18,6 +18,57 @@
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,
SkStream** imageData, SkStream** alphaData) {
SkMemoryStream* image = NULL;
@@ -49,36 +100,35 @@ void extractImageData(const SkBitmap& bitmap, const SkIRect& srcRect,
uint16_t* src = bitmap.getAddr16(0, y);
int x;
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) |
- SkGetPackedA4444(src[x + 1]);
- if (alphaDst[0] != 0xFF) {
+ SkGetPackedA4444(src[x + 1]);
+ if (alphaDst[0] != SK_AlphaOPAQUE) {
hasAlpha = true;
}
if (alphaDst[0]) {
isTransparent = false;
}
+ unpremultiply_and_pack_argb4444(src[x], src[x + 1], dst);
alphaDst++;
+ dst += 3;
}
if (srcRect.width() & 1) {
- dst[0] = (SkGetPackedR4444(src[x]) << 4) |
- SkGetPackedG4444(src[x]);
- dst[1] = (SkGetPackedB4444(src[x]) << 4);
- dst += 2;
- alphaDst[0] = (SkGetPackedA4444(src[x]) << 4);
+ alphaDst[0] = SkGetPackedA4444(src[x]) << 4;
+ // Use a buffer to translate from the usual 2 4444 values
+ // in 3 bytes to the single 4444 value in 2 bytes.
+ uint8_t buffer[3];
if (alphaDst[0] != 0xF0) {
hasAlpha = true;
}
if (alphaDst[0] & 0xF0) {
isTransparent = false;
}
+ unpremultiply_and_pack_argb4444(src[x], 0x00, buffer);
+ dst[0] = buffer[0];
+ dst[1] = buffer[1];
+
alphaDst++;
+ dst += 2;
}
}
break;
@@ -108,18 +158,16 @@ void extractImageData(const SkBitmap& bitmap, const SkIRect& srcRect,
for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
uint32_t* src = bitmap.getAddr32(0, y);
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]);
- if (alphaDst[0] != 0xFF) {
+ if (alphaDst[0] != SK_AlphaOPAQUE) {
hasAlpha = true;
}
- if (alphaDst[0]) {
+ if (alphaDst[0] != SK_AlphaTRANSPARENT) {
isTransparent = false;
}
+ unpremultiply_and_pack_argb8888(src[x], dst);
alphaDst++;
+ dst += 3;
}
}
break;