summaryrefslogtreecommitdiffstats
path: root/ui
diff options
context:
space:
mode:
authordcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-20 02:00:08 +0000
committerdcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-20 02:00:08 +0000
commitaa0d77af73798a141846310e5d57487c8b7ab55d (patch)
tree6bc8e70bc868902684e950d92e72201a09631235 /ui
parent000bed6be3f6ccf8497096ff9f0cc31ec83c24d3 (diff)
downloadchromium_src-aa0d77af73798a141846310e5d57487c8b7ab55d.zip
chromium_src-aa0d77af73798a141846310e5d57487c8b7ab55d.tar.gz
chromium_src-aa0d77af73798a141846310e5d57487c8b7ab55d.tar.bz2
Fix ClipboardGtk::WriteBitmap not unpremultiplying images.
We share code with GdkPixbufFromSkBitmap now, which has also been updated to remove the need to pass in our own function to free memory. BUG=154573 Review URL: https://chromiumcodereview.appspot.com/11187047 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@163125 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r--ui/base/clipboard/clipboard_gtk.cc41
-rw-r--r--ui/gfx/gtk_util.cc22
2 files changed, 14 insertions, 49 deletions
diff --git a/ui/base/clipboard/clipboard_gtk.cc b/ui/base/clipboard/clipboard_gtk.cc
index 0d23645..b3666be 100644
--- a/ui/base/clipboard/clipboard_gtk.cc
+++ b/ui/base/clipboard/clipboard_gtk.cc
@@ -23,6 +23,7 @@
#include "ui/base/gtk/scoped_gobject.h"
#include "ui/base/x/x11_util.h"
#include "ui/gfx/canvas.h"
+#include "ui/gfx/gtk_util.h"
#include "ui/gfx/size.h"
namespace ui {
@@ -174,34 +175,6 @@ void ClearData(GtkClipboard* clipboard,
delete map;
}
-// Called on GdkPixbuf destruction; see WriteBitmap().
-void GdkPixbufFree(guchar* pixels, gpointer data) {
- free(pixels);
-}
-
-// Makes a copy of |pixels| with the ordering changed from BGRA to RGBA.
-// The caller is responsible for free()ing the data. If |stride| is 0, it's
-// assumed to be 4 * |width|.
-uint8_t* BGRAToRGBA(const uint8_t* pixels, int width, int height, int stride) {
- if (stride == 0)
- stride = width * 4;
-
- uint8_t* new_pixels = static_cast<uint8_t*>(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;
- new_pixels[idx] = pixels[idx + 2];
- new_pixels[idx + 1] = pixels[idx + 1];
- new_pixels[idx + 2] = pixels[idx];
- new_pixels[idx + 3] = pixels[idx + 3];
- }
- }
-
- return new_pixels;
-}
-
} // namespace
Clipboard::FormatType::FormatType() : data_(GDK_NONE) {
@@ -335,13 +308,13 @@ void Clipboard::WriteWebSmartPaste() {
void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) {
const gfx::Size* size = reinterpret_cast<const gfx::Size*>(size_data);
- guchar* data = BGRAToRGBA(reinterpret_cast<const uint8_t*>(pixel_data),
- size->width(), size->height(), 0);
+ // Adopt the pixels into a SkBitmap. Note that the pixel order in memory is
+ // actually BGRA.
+ SkBitmap bitmap;
+ bitmap.setConfig(SkBitmap::kARGB_8888_Config, size->width(), size->height());
+ bitmap.setPixels(const_cast<char*>(pixel_data));
+ GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(bitmap);
- GdkPixbuf* pixbuf =
- gdk_pixbuf_new_from_data(data, GDK_COLORSPACE_RGB, TRUE,
- 8, size->width(), size->height(),
- size->width() * 4, GdkPixbufFree, NULL);
// We store the GdkPixbuf*, and the size_t half of the pair is meaningless.
// Note that this contrasts with the vast majority of entries in our target
// map, which directly store the data and its length.
diff --git a/ui/gfx/gtk_util.cc b/ui/gfx/gtk_util.cc
index 4c39e1c..bc34224 100644
--- a/ui/gfx/gtk_util.cc
+++ b/ui/gfx/gtk_util.cc
@@ -52,10 +52,6 @@ class GdkCursorCache {
DISALLOW_COPY_AND_ASSIGN(GdkCursorCache);
};
-void FreePixels(guchar* pixels, gpointer data) {
- free(data);
-}
-
} // namespace
namespace gfx {
@@ -95,11 +91,16 @@ GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap& bitmap) {
int width = bitmap.width();
int height = bitmap.height();
- int stride = bitmap.rowBytes();
+
+ GdkPixbuf* pixbuf = gdk_pixbuf_new(
+ GDK_COLORSPACE_RGB, // The only colorspace gtk supports.
+ TRUE, // There is an alpha channel.
+ 8,
+ width, height);
// SkBitmaps are premultiplied, we need to unpremultiply them.
const int kBytesPerPixel = 4;
- uint8* divided = static_cast<uint8*>(malloc(height * stride));
+ uint8* divided = gdk_pixbuf_get_pixels(pixbuf);
for (int y = 0, i = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
@@ -122,15 +123,6 @@ GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap& bitmap) {
}
}
- // 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(
- divided,
- GDK_COLORSPACE_RGB, // The only colorspace gtk supports.
- true, // There is an alpha channel.
- 8,
- width, height, stride, &FreePixels, divided);
-
return pixbuf;
}