| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/libgtkui/skia_utils_gtk2.h" | 5 #include "chrome/browser/ui/libgtkui/skia_utils_gtk.h" |
| 6 | 6 |
| 7 #include <gdk/gdk.h> | 7 #include <gdk/gdk.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 11 #include "third_party/skia/include/core/SkUnPreMultiply.h" | 11 #include "third_party/skia/include/core/SkUnPreMultiply.h" |
| 12 | 12 |
| 13 namespace libgtkui { | 13 namespace libgtkui { |
| 14 | 14 |
| 15 // GDK_COLOR_RGB multiplies by 257 (= 0x10001) to distribute the bits evenly | 15 // GDK_COLOR_RGB multiplies by 257 (= 0x10001) to distribute the bits evenly |
| 16 // See: http://www.mindcontrol.org/~hplus/graphics/expand-bits.html | 16 // See: http://www.mindcontrol.org/~hplus/graphics/expand-bits.html |
| 17 // To get back, we can just right shift by eight | 17 // To get back, we can just right shift by eight |
| 18 // (or, formulated differently, i == (i*257)/256 for all i < 256). | 18 // (or, formulated differently, i == (i*257)/256 for all i < 256). |
| 19 | 19 |
| 20 SkColor GdkColorToSkColor(GdkColor color) { | 20 SkColor GdkColorToSkColor(GdkColor color) { |
| 21 return SkColorSetRGB(color.red >> 8, color.green >> 8, color.blue >> 8); | 21 return SkColorSetRGB(color.red >> 8, color.green >> 8, color.blue >> 8); |
| 22 } | 22 } |
| 23 | 23 |
| 24 GdkColor SkColorToGdkColor(SkColor color) { | 24 GdkColor SkColorToGdkColor(SkColor color) { |
| 25 GdkColor gdk_color = { | 25 GdkColor gdk_color = { |
| 26 0, | 26 0, static_cast<guint16>(SkColorGetR(color) * kSkiaToGDKMultiplier), |
| 27 static_cast<guint16>(SkColorGetR(color) * kSkiaToGDKMultiplier), | |
| 28 static_cast<guint16>(SkColorGetG(color) * kSkiaToGDKMultiplier), | 27 static_cast<guint16>(SkColorGetG(color) * kSkiaToGDKMultiplier), |
| 29 static_cast<guint16>(SkColorGetB(color) * kSkiaToGDKMultiplier) | 28 static_cast<guint16>(SkColorGetB(color) * kSkiaToGDKMultiplier)}; |
| 30 }; | |
| 31 return gdk_color; | 29 return gdk_color; |
| 32 } | 30 } |
| 33 | 31 |
| 34 const SkBitmap GdkPixbufToImageSkia(GdkPixbuf* pixbuf) { | 32 const SkBitmap GdkPixbufToImageSkia(GdkPixbuf* pixbuf) { |
| 35 // TODO(erg): What do we do in the case where the pixbuf fails these dchecks? | 33 // TODO(erg): What do we do in the case where the pixbuf fails these dchecks? |
| 36 // I would prefer to use our gtk based canvas, but that would require | 34 // I would prefer to use our gtk based canvas, but that would require |
| 37 // recompiling half of our skia extensions with gtk support, which we can't | 35 // recompiling half of our skia extensions with gtk support, which we can't |
| 38 // do in this build. | 36 // do in this build. |
| 39 DCHECK_EQ(GDK_COLORSPACE_RGB, gdk_pixbuf_get_colorspace(pixbuf)); | 37 DCHECK_EQ(GDK_COLORSPACE_RGB, gdk_pixbuf_get_colorspace(pixbuf)); |
| 40 | 38 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 return NULL; | 89 return NULL; |
| 92 | 90 |
| 93 SkAutoLockPixels lock_pixels(bitmap); | 91 SkAutoLockPixels lock_pixels(bitmap); |
| 94 | 92 |
| 95 int width = bitmap.width(); | 93 int width = bitmap.width(); |
| 96 int height = bitmap.height(); | 94 int height = bitmap.height(); |
| 97 | 95 |
| 98 GdkPixbuf* pixbuf = | 96 GdkPixbuf* pixbuf = |
| 99 gdk_pixbuf_new(GDK_COLORSPACE_RGB, // The only colorspace gtk supports. | 97 gdk_pixbuf_new(GDK_COLORSPACE_RGB, // The only colorspace gtk supports. |
| 100 TRUE, // There is an alpha channel. | 98 TRUE, // There is an alpha channel. |
| 101 8, | 99 8, width, height); |
| 102 width, | |
| 103 height); | |
| 104 | 100 |
| 105 // SkBitmaps are premultiplied, we need to unpremultiply them. | 101 // SkBitmaps are premultiplied, we need to unpremultiply them. |
| 106 const int kBytesPerPixel = 4; | 102 const int kBytesPerPixel = 4; |
| 107 uint8_t* divided = gdk_pixbuf_get_pixels(pixbuf); | 103 uint8_t* divided = gdk_pixbuf_get_pixels(pixbuf); |
| 108 | 104 |
| 109 for (int y = 0, i = 0; y < height; y++) { | 105 for (int y = 0, i = 0; y < height; y++) { |
| 110 for (int x = 0; x < width; x++) { | 106 for (int x = 0; x < width; x++) { |
| 111 uint32_t pixel = bitmap.getAddr32(0, y)[x]; | 107 uint32_t pixel = bitmap.getAddr32(0, y)[x]; |
| 112 | 108 |
| 113 int alpha = SkColorGetA(pixel); | 109 int alpha = SkColorGetA(pixel); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 124 divided[i + 3] = alpha; | 120 divided[i + 3] = alpha; |
| 125 } | 121 } |
| 126 i += kBytesPerPixel; | 122 i += kBytesPerPixel; |
| 127 } | 123 } |
| 128 } | 124 } |
| 129 | 125 |
| 130 return pixbuf; | 126 return pixbuf; |
| 131 } | 127 } |
| 132 | 128 |
| 133 } // namespace libgtkui | 129 } // namespace libgtkui |
| OLD | NEW |