diff options
author | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-10 13:03:19 +0000 |
---|---|---|
committer | deanm@chromium.org <deanm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-10 13:03:19 +0000 |
commit | c6c32dee8da128970df06b74ab7fc53dee9eb6d1 (patch) | |
tree | a0a25f443991e8f13906fd359a89fccd77ba7043 /base/gfx | |
parent | f09c718082535c0de7f066cb63f95809070f35a6 (diff) | |
download | chromium_src-c6c32dee8da128970df06b74ab7fc53dee9eb6d1.zip chromium_src-c6c32dee8da128970df06b74ab7fc53dee9eb6d1.tar.gz chromium_src-c6c32dee8da128970df06b74ab7fc53dee9eb6d1.tar.bz2 |
Move GdkPixbufFromSkBitmap to gtk_util so it can easily be shared.
Review URL: http://codereview.chromium.org/38009
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@11327 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/gfx')
-rwxr-xr-x | base/gfx/gtk_util.cc | 39 | ||||
-rwxr-xr-x | base/gfx/gtk_util.h | 6 |
2 files changed, 45 insertions, 0 deletions
diff --git a/base/gfx/gtk_util.cc b/base/gfx/gtk_util.cc index 26601ca..e12bb6e 100755 --- a/base/gfx/gtk_util.cc +++ b/base/gfx/gtk_util.cc @@ -7,6 +7,7 @@ #include <gdk/gdk.h> #include "base/gfx/rect.h" +#include "skia/include/SkBitmap.h" namespace gfx { @@ -21,4 +22,42 @@ void SubtractRectanglesFromRegion(GdkRegion* region, } } +static void FreePixels(guchar* pixels, gpointer data) { + free(data); +} + +GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap* bitmap) { + bitmap->lockPixels(); + int width = bitmap->width(); + int height = bitmap->height(); + int stride = bitmap->rowBytes(); + const guchar* orig_data = static_cast<guchar*>(bitmap->getPixels()); + guchar* data = static_cast<guchar*>(malloc(height * stride)); + + // We have to copy the pixels and swap from BGRA to RGBA. + for (int i = 0; i < height; ++i) { + for (int j = 0; j < width; ++j) { + int idx = i * stride + j * 4; + data[idx] = orig_data[idx + 2]; + data[idx + 1] = orig_data[idx + 1]; + data[idx + 2] = orig_data[idx]; + data[idx + 3] = orig_data[idx + 3]; + } + } + + // 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, + GDK_COLORSPACE_RGB, // The only colorspace gtk supports. + true, // There is an alpha channel. + 8, + width, height, stride, &FreePixels, data); + + // Assume ownership of pixbuf. + g_object_ref_sink(pixbuf); + bitmap->unlockPixels(); + return pixbuf; +} + } // namespace gfx diff --git a/base/gfx/gtk_util.h b/base/gfx/gtk_util.h index 7062d59..bf6e6aa 100755 --- a/base/gfx/gtk_util.h +++ b/base/gfx/gtk_util.h @@ -7,7 +7,9 @@ #include <vector> +typedef struct _GdkPixbuf GdkPixbuf; typedef struct _GdkRegion GdkRegion; +class SkBitmap; namespace gfx { @@ -17,6 +19,10 @@ class Rect; void SubtractRectanglesFromRegion(GdkRegion* region, const std::vector<gfx::Rect>& cutouts); +// Convert and copy a SkBitmap to a GdkPixbuf. NOTE: This is an expensive +// operation, all of the pixels must be copied and their order swapped. +GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap* bitmap); + } // namespace gfx #endif // BASE_GFX_GTK_UTIL_H_ |