From d1ae85447a12e97b0758b0d113a12e2d6c6d09b4 Mon Sep 17 00:00:00 2001 From: "glen@chromium.org" Date: Tue, 7 Jul 2009 17:36:23 +0000 Subject: Fix retarded bitmath. << 8 != * 255 BUG=13360 Review URL: http://codereview.chromium.org/149164 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20040 0039d316-1c4b-4281-b951-d872f2087c98 --- base/gfx/png_decoder.cc | 8 +++++--- base/gfx/png_encoder.cc | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/base/gfx/png_decoder.cc b/base/gfx/png_decoder.cc index bd6beb4..fe0bf893 100644 --- a/base/gfx/png_decoder.cc +++ b/base/gfx/png_decoder.cc @@ -329,10 +329,12 @@ bool PNGDecoder::Decode(const std::vector* data, for (int i = width * height * 4 - 4; i >= 0; i -= 4) { unsigned char alpha = decoded_data[i + 3]; if (alpha != 0 && alpha != 255) { + SkColor premultiplied = SkPreMultiplyARGB(alpha, + decoded_data[i], decoded_data[i + 1], decoded_data[i + 2]); bitmap_data[i + 3] = alpha; - bitmap_data[i] = (decoded_data[i] * alpha) >> 8; - bitmap_data[i + 1] = (decoded_data[i + 1] * alpha) >> 8; - bitmap_data[i + 2] = (decoded_data[i + 2] * alpha) >> 8; + bitmap_data[i] = SkColorGetR(premultiplied); + bitmap_data[i + 1] = SkColorGetG(premultiplied); + bitmap_data[i + 2] = SkColorGetB(premultiplied); } else { bitmap_data[i + 3] = alpha; bitmap_data[i] = decoded_data[i]; diff --git a/base/gfx/png_encoder.cc b/base/gfx/png_encoder.cc index f8b4bf0..c342736 100644 --- a/base/gfx/png_encoder.cc +++ b/base/gfx/png_encoder.cc @@ -7,6 +7,7 @@ #include "base/logging.h" #include "base/scoped_ptr.h" #include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/skia/include/core/SkUnPreMultiply.h" extern "C" { #include "third_party/libpng/png.h" @@ -214,9 +215,10 @@ bool PNGEncoder::EncodeBGRASkBitmap(const SkBitmap& input, int alpha = SkColorGetA(pixel); if (alpha != 0 && alpha != 255) { - divided[i + 0] = (SkColorGetR(pixel) << 8) / alpha; - divided[i + 1] = (SkColorGetG(pixel) << 8) / alpha; - divided[i + 2] = (SkColorGetB(pixel) << 8) / alpha; + SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel); + divided[i + 0] = SkColorGetR(unmultiplied); + divided[i + 1] = SkColorGetG(unmultiplied); + divided[i + 2] = SkColorGetB(unmultiplied); divided[i + 3] = alpha; } else { divided[i + 0] = SkColorGetR(pixel); -- cgit v1.1