| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/gfx/gtk_util.h" | |
| 6 | |
| 7 #include <gdk/gdk.h> | |
| 8 #include <gtk/gtk.h> | |
| 9 #include <stdlib.h> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/gfx/rect.h" | |
| 13 #include "base/linux_util.h" | |
| 14 #include "third_party/skia/include/core/SkBitmap.h" | |
| 15 #include "third_party/skia/include/core/SkUnPreMultiply.h" | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 void FreePixels(guchar* pixels, gpointer data) { | |
| 20 free(data); | |
| 21 } | |
| 22 | |
| 23 } // namespace | |
| 24 | |
| 25 namespace gfx { | |
| 26 | |
| 27 const GdkColor kGdkWhite = GDK_COLOR_RGB(0xff, 0xff, 0xff); | |
| 28 const GdkColor kGdkBlack = GDK_COLOR_RGB(0x00, 0x00, 0x00); | |
| 29 const GdkColor kGdkGreen = GDK_COLOR_RGB(0x00, 0xff, 0x00); | |
| 30 | |
| 31 GdkPixbuf* GdkPixbufFromSkBitmap(const SkBitmap* bitmap) { | |
| 32 bitmap->lockPixels(); | |
| 33 int width = bitmap->width(); | |
| 34 int height = bitmap->height(); | |
| 35 int stride = bitmap->rowBytes(); | |
| 36 | |
| 37 // SkBitmaps are premultiplied, we need to unpremultiply them. | |
| 38 const int kBytesPerPixel = 4; | |
| 39 uint8* divided = static_cast<uint8*>(malloc(height * stride)); | |
| 40 | |
| 41 for (int y = 0, i = 0; y < height; y++) { | |
| 42 for (int x = 0; x < width; x++) { | |
| 43 uint32 pixel = bitmap->getAddr32(0, y)[x]; | |
| 44 | |
| 45 int alpha = SkColorGetA(pixel); | |
| 46 if (alpha != 0 && alpha != 255) { | |
| 47 SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel); | |
| 48 divided[i + 0] = SkColorGetR(unmultiplied); | |
| 49 divided[i + 1] = SkColorGetG(unmultiplied); | |
| 50 divided[i + 2] = SkColorGetB(unmultiplied); | |
| 51 divided[i + 3] = alpha; | |
| 52 } else { | |
| 53 divided[i + 0] = SkColorGetR(pixel); | |
| 54 divided[i + 1] = SkColorGetG(pixel); | |
| 55 divided[i + 2] = SkColorGetB(pixel); | |
| 56 divided[i + 3] = alpha; | |
| 57 } | |
| 58 i += kBytesPerPixel; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 // This pixbuf takes ownership of our malloc()ed data and will | |
| 63 // free it for us when it is destroyed. | |
| 64 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data( | |
| 65 divided, | |
| 66 GDK_COLORSPACE_RGB, // The only colorspace gtk supports. | |
| 67 true, // There is an alpha channel. | |
| 68 8, | |
| 69 width, height, stride, &FreePixels, divided); | |
| 70 | |
| 71 bitmap->unlockPixels(); | |
| 72 return pixbuf; | |
| 73 } | |
| 74 | |
| 75 void SubtractRectanglesFromRegion(GdkRegion* region, | |
| 76 const std::vector<Rect>& cutouts) { | |
| 77 for (size_t i = 0; i < cutouts.size(); ++i) { | |
| 78 GdkRectangle rect = cutouts[i].ToGdkRectangle(); | |
| 79 GdkRegion* rect_region = gdk_region_rectangle(&rect); | |
| 80 gdk_region_subtract(region, rect_region); | |
| 81 // TODO(deanm): It would be nice to be able to reuse the GdkRegion here. | |
| 82 gdk_region_destroy(rect_region); | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 } // namespace gfx | |
| OLD | NEW |