diff options
author | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-08 17:52:36 +0000 |
---|---|---|
committer | tc@google.com <tc@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-08 17:52:36 +0000 |
commit | 1e9d6d550c8b6acb7885b5ec628934fa8b7f1e4a (patch) | |
tree | 92e08062d4024aeae644ab6a2ca0cea1485c2dc3 /base/gfx | |
parent | 3161eec0578ab99cf7c03ccacfef26b9de1ca919 (diff) | |
download | chromium_src-1e9d6d550c8b6acb7885b5ec628934fa8b7f1e4a.zip chromium_src-1e9d6d550c8b6acb7885b5ec628934fa8b7f1e4a.tar.gz chromium_src-1e9d6d550c8b6acb7885b5ec628934fa8b7f1e4a.tar.bz2 |
GdkPixbufFromSkBitmap needs to un-premultiply the alpha before
passing off the data to gdk_pixbuf. This is required in the theme
code for tinted bitmaps.
Review URL: http://codereview.chromium.org/150151
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20164 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/gfx')
-rw-r--r-- | base/gfx/gtk_util.cc | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/base/gfx/gtk_util.cc b/base/gfx/gtk_util.cc index 005ec54..1611c94 100644 --- a/base/gfx/gtk_util.cc +++ b/base/gfx/gtk_util.cc @@ -8,9 +8,11 @@ #include <gtk/gtk.h> #include <stdlib.h> +#include "base/basictypes.h" #include "base/gfx/rect.h" #include "base/linux_util.h" #include "third_party/skia/include/core/SkBitmap.h" +#include "third_party/skia/include/core/SkUnPreMultiply.h" namespace { @@ -19,6 +21,7 @@ void FreePixels(guchar* pixels, gpointer data) { } } // namespace + namespace gfx { const GdkColor kGdkWhite = GDK_COLOR_RGB(0xff, 0xff, 0xff); @@ -30,17 +33,40 @@ GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap* bitmap) { int width = bitmap->width(); int height = bitmap->height(); int stride = bitmap->rowBytes(); - const guchar* orig_data = static_cast<guchar*>(bitmap->getPixels()); - guchar* data = base::BGRAToRGBA(orig_data, width, height, stride); + + // SkBitmaps are premultiplied, we need to unpremultiply them. + const int kBytesPerPixel = 4; + uint8* divided = static_cast<uint8*>(malloc(height * stride)); + + for (int y = 0, i = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + uint32 pixel = bitmap->getAddr32(0, y)[x]; + + int alpha = SkColorGetA(pixel); + if (alpha != 0 && alpha != 255) { + 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); + divided[i + 1] = SkColorGetG(pixel); + divided[i + 2] = SkColorGetB(pixel); + divided[i + 3] = alpha; + } + i += kBytesPerPixel; + } + } // This pixbuf takes ownership of our malloc()ed data and will // free it for us when it is destroyed. GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( - data, + divided, GDK_COLORSPACE_RGB, // The only colorspace gtk supports. true, // There is an alpha channel. 8, - width, height, stride, &FreePixels, data); + width, height, stride, &FreePixels, divided); bitmap->unlockPixels(); return pixbuf; |