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

Unified Diff: src/pdf/SkPDFImage.cpp

Issue 22831039: Add unpremultiply support and GM (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Review fixes and rebase from refactor Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « gyp/gmslides.gypi ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pdf/SkPDFImage.cpp
diff --git a/src/pdf/SkPDFImage.cpp b/src/pdf/SkPDFImage.cpp
index 6ede7aaf515d54b5155ade6f8b9ee6033eb01cec..318a45929dea08f87b3569745fa7965899287876 100644
--- a/src/pdf/SkPDFImage.cpp
+++ b/src/pdf/SkPDFImage.cpp
@@ -336,6 +336,157 @@ static SkPDFArray* make_indexed_color_space(SkColorTable* table) {
return result;
}
+/**
+ * Removes the alpha component of an ARGB color (including unpremultiply) while
+ * keeping the output in the same format as the input.
+ */
+static uint32_t remove_alpha_argb8888(uint32_t pmColor) {
+ SkColor color = SkUnPreMultiply::PMColorToColor(pmColor);
+ return SkPackARGB32NoCheck(SK_AlphaOPAQUE,
+ SkColorGetR(color),
+ SkColorGetG(color),
+ SkColorGetB(color));
+}
+
+static uint16_t remove_alpha_argb4444(uint16_t pmColor) {
+ return SkPixel32ToPixel4444(
+ remove_alpha_argb8888(SkPixel4444ToPixel32(pmColor)));
+}
+
+static uint32_t get_argb8888_neighbor_avg_color(const SkBitmap& bitmap,
+ int xOrig, int yOrig) {
+ uint8_t count = 0;
+ uint16_t r = 0;
+ uint16_t g = 0;
+ uint16_t b = 0;
+
+ for (int y = yOrig - 1; y <= yOrig + 1; y++) {
+ if (y < 0 || y >= bitmap.height()) {
+ continue;
+ }
+ uint32_t* src = bitmap.getAddr32(0, y);
+ for (int x = xOrig - 1; x <= xOrig + 1; x++) {
+ if (x < 0 || x >= bitmap.width()) {
+ continue;
+ }
+ if (SkGetPackedA32(src[x]) != SK_AlphaTRANSPARENT) {
+ uint32_t color = remove_alpha_argb8888(src[x]);
+ r += SkGetPackedR32(color);
+ g += SkGetPackedG32(color);
+ b += SkGetPackedB32(color);
+ count++;
+ }
+ }
+ }
+
+ if (count == 0) {
+ return SkPackARGB32NoCheck(SK_AlphaOPAQUE, 0, 0, 0);
+ } else {
+ return SkPackARGB32NoCheck(SK_AlphaOPAQUE,
+ r / count, g / count, b / count);
+ }
+}
+
+static uint16_t get_argb4444_neighbor_avg_color(const SkBitmap& bitmap,
+ int xOrig, int yOrig) {
+ uint8_t count = 0;
+ uint8_t r = 0;
+ uint8_t g = 0;
+ uint8_t b = 0;
+
+ for (int y = yOrig - 1; y <= yOrig + 1; y++) {
+ if (y < 0 || y >= bitmap.height()) {
+ continue;
+ }
+ uint16_t* src = bitmap.getAddr16(0, y);
+ for (int x = xOrig - 1; x <= xOrig + 1; x++) {
+ if (x < 0 || x >= bitmap.width()) {
+ continue;
+ }
+ if ((SkGetPackedA4444(src[x]) & 0x0F) != SK_AlphaTRANSPARENT) {
+ uint16_t color = remove_alpha_argb4444(src[x]);
+ r += SkGetPackedR4444(color);
+ g += SkGetPackedG4444(color);
+ b += SkGetPackedB4444(color);
+ count++;
+ }
+ }
+ }
+
+ if (count == 0) {
+ return SkPackARGB4444(SK_AlphaOPAQUE & 0x0F, 0, 0, 0);
+ } else {
+ return SkPackARGB4444(SK_AlphaOPAQUE & 0x0F,
+ r / count, g / count, b / count);
+ }
+}
+
+static SkBitmap unpremultiply_bitmap(const SkBitmap& bitmap,
+ const SkIRect& srcRect) {
+ SkBitmap outBitmap;
+ outBitmap.setConfig(bitmap.config(), srcRect.width(), srcRect.height());
+ SkASSERT(outBitmap.allocPixels());
+ size_t dstRow = 0;
+
+ outBitmap.lockPixels();
+ bitmap.lockPixels();
+ switch (bitmap.config()) {
+ case SkBitmap::kARGB_4444_Config: {
+ for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
+ uint16_t* dst = outBitmap.getAddr16(0, dstRow);
+ uint16_t* src = bitmap.getAddr16(0, y);
+ for (int x = srcRect.fLeft; x < srcRect.fRight; x++) {
+ uint8_t a = SkGetPackedA4444(src[x]);
+ // It is necessary to average the color component of
+ // transparent pixels with their surrounding neighbors
+ // since the PDF renderer may separately re-sample the
+ // alpha and color channels when the image is not
+ // displayed at its native resolution. Since an alpha of
+ // zero gives no information about the color component,
+ // the pathological case is a white image with sharp
+ // transparency bounds - the color channel goes to black,
+ // and the should-be-transparent pixels are rendered
+ // as grey because of the separate soft mask and color
+ // resizing.
+ if (a == (SK_AlphaTRANSPARENT & 0x0F)) {
+ *dst = get_argb4444_neighbor_avg_color(bitmap, x, y);
+ } else {
+ *dst = remove_alpha_argb4444(src[x]);
+ }
+ dst++;
+ }
+ dstRow++;
+ }
+ break;
+ }
+ case SkBitmap::kARGB_8888_Config: {
+ for (int y = srcRect.fTop; y < srcRect.fBottom; y++) {
+ uint32_t* dst = outBitmap.getAddr32(0, dstRow);
+ uint32_t* src = bitmap.getAddr32(0, y);
+ for (int x = srcRect.fLeft; x < srcRect.fRight; x++) {
+ uint8_t a = SkGetPackedA32(src[x]);
+ if (a == SK_AlphaTRANSPARENT) {
+ *dst = get_argb8888_neighbor_avg_color(bitmap, x, y);
+ } else {
+ *dst = remove_alpha_argb8888(src[x]);
+ }
+ dst++;
+ }
+ dstRow++;
+ }
+ break;
+ }
+ default:
+ SkASSERT(false);
+ }
+ bitmap.unlockPixels();
+ outBitmap.unlockPixels();
+
+ outBitmap.setImmutable();
+
+ return outBitmap;
+}
+
// static
SkPDFImage* SkPDFImage::CreateImage(const SkBitmap& bitmap,
const SkIRect& srcRect,
@@ -358,8 +509,18 @@ SkPDFImage* SkPDFImage::CreateImage(const SkBitmap& bitmap,
return NULL;
}
- SkPDFImage* image = SkNEW_ARGS(SkPDFImage, (NULL, bitmap,
- false, srcRect, encoder));
+ SkPDFImage* image;
+ SkBitmap::Config config = bitmap.config();
+ if (alphaData.get() != NULL && (config == SkBitmap::kARGB_8888_Config ||
+ config == SkBitmap::kARGB_4444_Config)) {
+ SkBitmap unpremulBitmap = unpremultiply_bitmap(bitmap, srcRect);
+ SkIRect newSrcRect = srcRect;
+ newSrcRect.offset(-srcRect.left(), -srcRect.top());
+ image = SkNEW_ARGS(SkPDFImage, (NULL, unpremulBitmap,
+ false, newSrcRect, encoder));
+ } else {
+ image = SkNEW_ARGS(SkPDFImage, (NULL, bitmap, false, srcRect, encoder));
+ }
if (alphaData.get() != NULL) {
SkAutoTUnref<SkPDFImage> mask(
SkNEW_ARGS(SkPDFImage, (alphaData.get(), bitmap,
@@ -391,11 +552,17 @@ SkPDFImage::SkPDFImage(SkStream* stream,
bool isAlpha,
const SkIRect& srcRect,
EncodeToDCTStream encoder)
- : fBitmap(bitmap),
- fIsAlpha(isAlpha),
+ : fIsAlpha(isAlpha),
fSrcRect(srcRect),
fEncoder(encoder) {
+ if (bitmap.isImmutable()) {
+ fBitmap = bitmap;
+ } else {
+ bitmap.deepCopyTo(&fBitmap, bitmap.config());
+ fBitmap.setImmutable();
+ }
+
if (stream != NULL) {
setData(stream);
fStreamValid = true;
« no previous file with comments | « gyp/gmslides.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698