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 | |
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
-rwxr-xr-x | base/gfx/gtk_util.cc | 39 | ||||
-rwxr-xr-x | base/gfx/gtk_util.h | 6 | ||||
-rw-r--r-- | chrome/browser/gtk/menu_gtk.cc | 42 |
3 files changed, 47 insertions, 40 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_ diff --git a/chrome/browser/gtk/menu_gtk.cc b/chrome/browser/gtk/menu_gtk.cc index 8937a3a..3f8f7a9 100644 --- a/chrome/browser/gtk/menu_gtk.cc +++ b/chrome/browser/gtk/menu_gtk.cc @@ -4,6 +4,7 @@ #include "chrome/browser/gtk/menu_gtk.h" +#include "base/gfx/gtk_util.h" #include "base/logging.h" #include "base/string_util.h" #include "chrome/common/l10n_util.h" @@ -31,45 +32,6 @@ std::string ConvertAcceleratorsFromWindowsStyle(const std::string& label) { return ret; } -void FreePixels(guchar* pixels, gpointer data) { - free(data); -} - -// We have to copy the pixels and reverse their order manually. -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)); - - // 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 MenuGtk::MenuGtk(MenuGtk::Delegate* delegate, @@ -203,7 +165,7 @@ void MenuGtk::BuildMenuFromDelegate() { menu_item = gtk_image_menu_item_new_with_label( delegate_->GetLabel(i).c_str()); const SkBitmap* icon = delegate_->GetIcon(i); - GdkPixbuf* pixbuf = GdkPixbufFromSkBitmap(icon); + GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(icon); GtkWidget* widget = gtk_image_new_from_pixbuf(pixbuf); gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menu_item), widget); g_object_unref(pixbuf); |