From 59bfb1240666b0f70109ac8ab04a280765ff8563 Mon Sep 17 00:00:00 2001 From: "robertphillips@google.com" Date: Thu, 8 Nov 2012 15:06:52 +0000 Subject: Update WIC Image decoder to unpremul colors before save https://codereview.appspot.com/6826069/ git-svn-id: http://skia.googlecode.com/svn/trunk@6349 2bbb7eff-a529-9590-31e7-b0007b416f81 --- src/ports/SkImageDecoder_WIC.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/ports/SkImageDecoder_WIC.cpp b/src/ports/SkImageDecoder_WIC.cpp index 79d107adec..5a36ace56a 100644 --- a/src/ports/SkImageDecoder_WIC.cpp +++ b/src/ports/SkImageDecoder_WIC.cpp @@ -17,6 +17,7 @@ #include "SkMovie.h" #include "SkStream.h" #include "SkTScopedComPtr.h" +#include "SkUnPreMultiply.h" class SkImageDecoder_WIC : public SkImageDecoder { protected: @@ -134,6 +135,9 @@ bool SkImageDecoder_WIC::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { stride * height, reinterpret_cast(bm->getPixels()) ); + + // Note: we don't need to premultiply here since we specified PBGRA + bm->computeAndSetOpaquePredicate(); } return SUCCEEDED(hr); @@ -183,7 +187,7 @@ bool SkImageEncoder_WIC::onEncode(SkWStream* stream //Convert to 8888 if needed. const SkBitmap* bitmap; SkBitmap bitmapCopy; - if (SkBitmap::kARGB_8888_Config == bitmapOrig.config()) { + if (SkBitmap::kARGB_8888_Config == bitmapOrig.config() && bitmapOrig.isOpaque()) { bitmap = &bitmapOrig; } else { if (!bitmapOrig.copyTo(&bitmapCopy, SkBitmap::kARGB_8888_Config)) { @@ -192,6 +196,23 @@ bool SkImageEncoder_WIC::onEncode(SkWStream* stream bitmap = &bitmapCopy; } + // We cannot use PBGRA so we need to unpremultiply ourselves + if (!bitmap->isOpaque()) { + SkAutoLockPixels alp(*bitmap); + + uint8_t* pixels = reinterpret_cast(bitmap->getPixels()); + for (int y = 0; y < bitmap->height(); ++y) { + for (int x = 0; x < bitmap->width(); ++x) { + uint8_t* bytes = pixels + y * bitmap->rowBytes() + x * bitmap->bytesPerPixel(); + + SkPMColor* src = reinterpret_cast(bytes); + SkColor* dst = reinterpret_cast(bytes); + + *dst = SkUnPreMultiply::PMColorToColor(*src); + } + } + } + //Initialize COM. SkAutoCoInitialize scopedCo; if (!scopedCo.succeeded()) { -- cgit v1.2.3