OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/gfx/gtk_util.h" | 5 #include "base/gfx/gtk_util.h" |
6 | 6 |
7 #include <gdk/gdk.h> | 7 #include <gdk/gdk.h> |
8 | 8 |
9 #include "base/gfx/rect.h" | 9 #include "base/gfx/rect.h" |
| 10 #include "skia/include/SkBitmap.h" |
10 | 11 |
11 namespace gfx { | 12 namespace gfx { |
12 | 13 |
13 void SubtractRectanglesFromRegion(GdkRegion* region, | 14 void SubtractRectanglesFromRegion(GdkRegion* region, |
14 const std::vector<gfx::Rect>& cutouts) { | 15 const std::vector<gfx::Rect>& cutouts) { |
15 for (size_t i = 0; i < cutouts.size(); ++i) { | 16 for (size_t i = 0; i < cutouts.size(); ++i) { |
16 GdkRectangle rect = cutouts[i].ToGdkRectangle(); | 17 GdkRectangle rect = cutouts[i].ToGdkRectangle(); |
17 GdkRegion* rect_region = gdk_region_rectangle(&rect); | 18 GdkRegion* rect_region = gdk_region_rectangle(&rect); |
18 gdk_region_subtract(region, rect_region); | 19 gdk_region_subtract(region, rect_region); |
19 // TODO(deanm): It would be nice to be able to reuse the GdkRegion here. | 20 // TODO(deanm): It would be nice to be able to reuse the GdkRegion here. |
20 gdk_region_destroy(rect_region); | 21 gdk_region_destroy(rect_region); |
21 } | 22 } |
22 } | 23 } |
23 | 24 |
| 25 static void FreePixels(guchar* pixels, gpointer data) { |
| 26 free(data); |
| 27 } |
| 28 |
| 29 GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap* bitmap) { |
| 30 bitmap->lockPixels(); |
| 31 int width = bitmap->width(); |
| 32 int height = bitmap->height(); |
| 33 int stride = bitmap->rowBytes(); |
| 34 const guchar* orig_data = static_cast<guchar*>(bitmap->getPixels()); |
| 35 guchar* data = static_cast<guchar*>(malloc(height * stride)); |
| 36 |
| 37 // We have to copy the pixels and swap from BGRA to RGBA. |
| 38 for (int i = 0; i < height; ++i) { |
| 39 for (int j = 0; j < width; ++j) { |
| 40 int idx = i * stride + j * 4; |
| 41 data[idx] = orig_data[idx + 2]; |
| 42 data[idx + 1] = orig_data[idx + 1]; |
| 43 data[idx + 2] = orig_data[idx]; |
| 44 data[idx + 3] = orig_data[idx + 3]; |
| 45 } |
| 46 } |
| 47 |
| 48 // This pixbuf takes ownership of our malloc()ed data and will |
| 49 // free it for us when it is destroyed. |
| 50 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( |
| 51 data, |
| 52 GDK_COLORSPACE_RGB, // The only colorspace gtk supports. |
| 53 true, // There is an alpha channel. |
| 54 8, |
| 55 width, height, stride, &FreePixels, data); |
| 56 |
| 57 // Assume ownership of pixbuf. |
| 58 g_object_ref_sink(pixbuf); |
| 59 bitmap->unlockPixels(); |
| 60 return pixbuf; |
| 61 } |
| 62 |
24 } // namespace gfx | 63 } // namespace gfx |
OLD | NEW |