OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/gfx/image.h" | 5 #include "ui/gfx/image.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
12 | 12 |
13 #if defined(OS_LINUX) | 13 #if defined(TOOLKIT_USES_GTK) |
14 #include <gdk-pixbuf/gdk-pixbuf.h> | 14 #include <gdk-pixbuf/gdk-pixbuf.h> |
15 #include <glib-object.h> | 15 #include <glib-object.h> |
16 #include "ui/gfx/canvas_skia.h" | 16 #include "ui/gfx/canvas_skia.h" |
17 #include "ui/gfx/gtk_util.h" | 17 #include "ui/gfx/gtk_util.h" |
18 #elif defined(OS_MACOSX) | 18 #elif defined(OS_MACOSX) |
19 #include "base/mac/mac_util.h" | 19 #include "base/mac/mac_util.h" |
20 #include "skia/ext/skia_utils_mac.h" | 20 #include "skia/ext/skia_utils_mac.h" |
21 #endif | 21 #endif |
22 | 22 |
23 namespace gfx { | 23 namespace gfx { |
24 | 24 |
25 namespace internal { | 25 namespace internal { |
26 | 26 |
27 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
28 // This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform | 28 // This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform |
29 // file cannot include the [square brackets] of ObjC. | 29 // file cannot include the [square brackets] of ObjC. |
30 bool NSImageToSkBitmaps(NSImage* image, std::vector<const SkBitmap*>* bitmaps); | 30 bool NSImageToSkBitmaps(NSImage* image, std::vector<const SkBitmap*>* bitmaps); |
31 #endif | 31 #endif |
32 | 32 |
33 #if defined(OS_LINUX) | 33 #if defined(TOOLKIT_USES_GTK) |
34 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { | 34 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { |
35 gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf), | 35 gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf), |
36 gdk_pixbuf_get_height(pixbuf), | 36 gdk_pixbuf_get_height(pixbuf), |
37 /*is_opaque=*/false); | 37 /*is_opaque=*/false); |
38 canvas.DrawGdkPixbuf(pixbuf, 0, 0); | 38 canvas.DrawGdkPixbuf(pixbuf, 0, 0); |
39 return new SkBitmap(canvas.ExtractBitmap()); | 39 return new SkBitmap(canvas.ExtractBitmap()); |
40 } | 40 } |
41 #endif | 41 #endif |
42 | 42 |
43 class ImageRepSkia; | 43 class ImageRepSkia; |
(...skipping 11 matching lines...) Expand all Loading... |
55 | 55 |
56 // Deletes the associated pixels of an ImageRep. | 56 // Deletes the associated pixels of an ImageRep. |
57 virtual ~ImageRep() {} | 57 virtual ~ImageRep() {} |
58 | 58 |
59 // Cast helpers ("fake RTTI"). | 59 // Cast helpers ("fake RTTI"). |
60 ImageRepSkia* AsImageRepSkia() { | 60 ImageRepSkia* AsImageRepSkia() { |
61 CHECK_EQ(type_, Image::kImageRepSkia); | 61 CHECK_EQ(type_, Image::kImageRepSkia); |
62 return reinterpret_cast<ImageRepSkia*>(this); | 62 return reinterpret_cast<ImageRepSkia*>(this); |
63 } | 63 } |
64 | 64 |
65 #if defined(OS_LINUX) | 65 #if defined(TOOLKIT_USES_GTK) |
66 ImageRepGdk* AsImageRepGdk() { | 66 ImageRepGdk* AsImageRepGdk() { |
67 CHECK_EQ(type_, Image::kImageRepGdk); | 67 CHECK_EQ(type_, Image::kImageRepGdk); |
68 return reinterpret_cast<ImageRepGdk*>(this); | 68 return reinterpret_cast<ImageRepGdk*>(this); |
69 } | 69 } |
70 #endif | 70 #endif |
71 | 71 |
72 #if defined(OS_MACOSX) | 72 #if defined(OS_MACOSX) |
73 ImageRepCocoa* AsImageRepCocoa() { | 73 ImageRepCocoa* AsImageRepCocoa() { |
74 CHECK_EQ(type_, Image::kImageRepCocoa); | 74 CHECK_EQ(type_, Image::kImageRepCocoa); |
75 return reinterpret_cast<ImageRepCocoa*>(this); | 75 return reinterpret_cast<ImageRepCocoa*>(this); |
(...skipping 27 matching lines...) Expand all Loading... |
103 const SkBitmap* bitmap() const { return bitmaps_[0]; } | 103 const SkBitmap* bitmap() const { return bitmaps_[0]; } |
104 | 104 |
105 const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; } | 105 const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; } |
106 | 106 |
107 private: | 107 private: |
108 std::vector<const SkBitmap*> bitmaps_; | 108 std::vector<const SkBitmap*> bitmaps_; |
109 | 109 |
110 DISALLOW_COPY_AND_ASSIGN(ImageRepSkia); | 110 DISALLOW_COPY_AND_ASSIGN(ImageRepSkia); |
111 }; | 111 }; |
112 | 112 |
113 #if defined(OS_LINUX) | 113 #if defined(TOOLKIT_USES_GTK) |
114 class ImageRepGdk : public ImageRep { | 114 class ImageRepGdk : public ImageRep { |
115 public: | 115 public: |
116 explicit ImageRepGdk(GdkPixbuf* pixbuf) | 116 explicit ImageRepGdk(GdkPixbuf* pixbuf) |
117 : ImageRep(Image::kImageRepGdk), | 117 : ImageRep(Image::kImageRepGdk), |
118 pixbuf_(pixbuf) { | 118 pixbuf_(pixbuf) { |
119 CHECK(pixbuf); | 119 CHECK(pixbuf); |
120 } | 120 } |
121 | 121 |
122 virtual ~ImageRepGdk() { | 122 virtual ~ImageRepGdk() { |
123 if (pixbuf_) { | 123 if (pixbuf_) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmap); | 200 internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmap); |
201 AddRepresentation(rep); | 201 AddRepresentation(rep); |
202 } | 202 } |
203 | 203 |
204 Image::Image(const std::vector<const SkBitmap*>& bitmaps) | 204 Image::Image(const std::vector<const SkBitmap*>& bitmaps) |
205 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { | 205 : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { |
206 internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmaps); | 206 internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmaps); |
207 AddRepresentation(rep); | 207 AddRepresentation(rep); |
208 } | 208 } |
209 | 209 |
210 #if defined(OS_LINUX) | 210 #if defined(TOOLKIT_USES_GTK) |
211 Image::Image(GdkPixbuf* pixbuf) | 211 Image::Image(GdkPixbuf* pixbuf) |
212 : storage_(new internal::ImageStorage(Image::kImageRepGdk)) { | 212 : storage_(new internal::ImageStorage(Image::kImageRepGdk)) { |
213 internal::ImageRepGdk* rep = new internal::ImageRepGdk(pixbuf); | 213 internal::ImageRepGdk* rep = new internal::ImageRepGdk(pixbuf); |
214 AddRepresentation(rep); | 214 AddRepresentation(rep); |
215 } | 215 } |
216 #endif | 216 #endif |
217 | 217 |
218 #if defined(OS_MACOSX) | 218 #if defined(OS_MACOSX) |
219 Image::Image(NSImage* image) | 219 Image::Image(NSImage* image) |
220 : storage_(new internal::ImageStorage(Image::kImageRepCocoa)) { | 220 : storage_(new internal::ImageStorage(Image::kImageRepCocoa)) { |
(...skipping 15 matching lines...) Expand all Loading... |
236 | 236 |
237 Image::operator const SkBitmap*() const { | 237 Image::operator const SkBitmap*() const { |
238 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); | 238 internal::ImageRep* rep = GetRepresentation(Image::kImageRepSkia); |
239 return rep->AsImageRepSkia()->bitmap(); | 239 return rep->AsImageRepSkia()->bitmap(); |
240 } | 240 } |
241 | 241 |
242 Image::operator const SkBitmap&() const { | 242 Image::operator const SkBitmap&() const { |
243 return *(this->operator const SkBitmap*()); | 243 return *(this->operator const SkBitmap*()); |
244 } | 244 } |
245 | 245 |
246 #if defined(OS_LINUX) | 246 #if defined(TOOLKIT_USES_GTK) |
247 Image::operator GdkPixbuf*() const { | 247 Image::operator GdkPixbuf*() const { |
248 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); | 248 internal::ImageRep* rep = GetRepresentation(Image::kImageRepGdk); |
249 return rep->AsImageRepGdk()->pixbuf(); | 249 return rep->AsImageRepGdk()->pixbuf(); |
250 } | 250 } |
251 #endif | 251 #endif |
252 | 252 |
253 #if defined(OS_MACOSX) | 253 #if defined(OS_MACOSX) |
254 Image::operator NSImage*() const { | 254 Image::operator NSImage*() const { |
255 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); | 255 internal::ImageRep* rep = GetRepresentation(Image::kImageRepCocoa); |
256 return rep->AsImageRepCocoa()->image(); | 256 return rep->AsImageRepCocoa()->image(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 RepresentationMap::iterator it = storage_->representations().find(rep_type); | 288 RepresentationMap::iterator it = storage_->representations().find(rep_type); |
289 if (it != storage_->representations().end()) | 289 if (it != storage_->representations().end()) |
290 return it->second; | 290 return it->second; |
291 | 291 |
292 // At this point, the requested rep does not exist, so it must be converted | 292 // At this point, the requested rep does not exist, so it must be converted |
293 // from the default rep. | 293 // from the default rep. |
294 | 294 |
295 // Handle native-to-Skia conversion. | 295 // Handle native-to-Skia conversion. |
296 if (rep_type == Image::kImageRepSkia) { | 296 if (rep_type == Image::kImageRepSkia) { |
297 internal::ImageRepSkia* rep = NULL; | 297 internal::ImageRepSkia* rep = NULL; |
298 #if defined(OS_LINUX) | 298 #if defined(TOOLKIT_USES_GTK) |
299 if (storage_->default_representation_type() == Image::kImageRepGdk) { | 299 if (storage_->default_representation_type() == Image::kImageRepGdk) { |
300 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); | 300 internal::ImageRepGdk* pixbuf_rep = default_rep->AsImageRepGdk(); |
301 rep = new internal::ImageRepSkia( | 301 rep = new internal::ImageRepSkia( |
302 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); | 302 internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); |
303 } | 303 } |
304 #elif defined(OS_MACOSX) | 304 #elif defined(OS_MACOSX) |
305 if (storage_->default_representation_type() == Image::kImageRepCocoa) { | 305 if (storage_->default_representation_type() == Image::kImageRepCocoa) { |
306 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); | 306 internal::ImageRepCocoa* nsimage_rep = default_rep->AsImageRepCocoa(); |
307 std::vector<const SkBitmap*> bitmaps; | 307 std::vector<const SkBitmap*> bitmaps; |
308 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); | 308 CHECK(internal::NSImageToSkBitmaps(nsimage_rep->image(), &bitmaps)); |
309 rep = new internal::ImageRepSkia(bitmaps); | 309 rep = new internal::ImageRepSkia(bitmaps); |
310 } | 310 } |
311 #endif | 311 #endif |
312 CHECK(rep); | 312 CHECK(rep); |
313 AddRepresentation(rep); | 313 AddRepresentation(rep); |
314 return rep; | 314 return rep; |
315 } | 315 } |
316 | 316 |
317 // Handle Skia-to-native conversions. | 317 // Handle Skia-to-native conversions. |
318 if (default_rep->type() == Image::kImageRepSkia) { | 318 if (default_rep->type() == Image::kImageRepSkia) { |
319 internal::ImageRepSkia* skia_rep = default_rep->AsImageRepSkia(); | 319 internal::ImageRepSkia* skia_rep = default_rep->AsImageRepSkia(); |
320 internal::ImageRep* native_rep = NULL; | 320 internal::ImageRep* native_rep = NULL; |
321 #if defined(OS_LINUX) | 321 #if defined(TOOLKIT_USES_GTK) |
322 if (rep_type == Image::kImageRepGdk) { | 322 if (rep_type == Image::kImageRepGdk) { |
323 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap()); | 323 GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap()); |
324 native_rep = new internal::ImageRepGdk(pixbuf); | 324 native_rep = new internal::ImageRepGdk(pixbuf); |
325 } | 325 } |
326 #elif defined(OS_MACOSX) | 326 #elif defined(OS_MACOSX) |
327 if (rep_type == Image::kImageRepCocoa) { | 327 if (rep_type == Image::kImageRepCocoa) { |
328 NSImage* image = gfx::SkBitmapsToNSImage(skia_rep->bitmaps()); | 328 NSImage* image = gfx::SkBitmapsToNSImage(skia_rep->bitmaps()); |
329 base::mac::NSObjectRetain(image); | 329 base::mac::NSObjectRetain(image); |
330 native_rep = new internal::ImageRepCocoa(image); | 330 native_rep = new internal::ImageRepCocoa(image); |
331 } | 331 } |
(...skipping 15 matching lines...) Expand all Loading... |
347 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> | 347 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> |
348 bitmaps().size(); | 348 bitmaps().size(); |
349 } | 349 } |
350 | 350 |
351 const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const { | 351 const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) const { |
352 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> | 352 return GetRepresentation(Image::kImageRepSkia)->AsImageRepSkia()-> |
353 bitmaps()[index]; | 353 bitmaps()[index]; |
354 } | 354 } |
355 | 355 |
356 } // namespace gfx | 356 } // namespace gfx |
OLD | NEW |