| 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 "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" | 
| 11 | 12 | 
| 12 #if defined(OS_LINUX) | 13 #if defined(OS_LINUX) | 
| 13 #include <gdk-pixbuf/gdk-pixbuf.h> | 14 #include <gdk-pixbuf/gdk-pixbuf.h> | 
| 14 #include <glib-object.h> | 15 #include <glib-object.h> | 
| 15 #include "ui/gfx/canvas_skia.h" | 16 #include "ui/gfx/canvas_skia.h" | 
| 16 #include "ui/gfx/gtk_util.h" | 17 #include "ui/gfx/gtk_util.h" | 
| 17 #elif defined(OS_MACOSX) | 18 #elif defined(OS_MACOSX) | 
| 18 #include "base/mac/mac_util.h" | 19 #include "base/mac/mac_util.h" | 
| 19 #include "skia/ext/skia_utils_mac.h" | 20 #include "skia/ext/skia_utils_mac.h" | 
| 20 #endif | 21 #endif | 
| 21 | 22 | 
| 22 namespace gfx { | 23 namespace gfx { | 
| 23 | 24 | 
| 24 namespace internal { | 25 namespace internal { | 
| 25 | 26 | 
| 26 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) | 
| 27 // This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform | 28 // This is a wrapper around gfx::NSImageToSkBitmap() because this cross-platform | 
| 28 // file cannot include the [square brackets] of ObjC. | 29 // file cannot include the [square brackets] of ObjC. | 
| 29 const SkBitmap* NSImageToSkBitmap(NSImage* image); | 30 bool NSImageToSkBitmaps(NSImage* image, std::vector<const SkBitmap*>& bitmaps); | 
| 30 #endif | 31 #endif | 
| 31 | 32 | 
| 32 #if defined(OS_LINUX) | 33 #if defined(OS_LINUX) | 
| 33 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { | 34 const SkBitmap* GdkPixbufToSkBitmap(GdkPixbuf* pixbuf) { | 
| 34   gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf), | 35   gfx::CanvasSkia canvas(gdk_pixbuf_get_width(pixbuf), | 
| 35                          gdk_pixbuf_get_height(pixbuf), | 36                          gdk_pixbuf_get_height(pixbuf), | 
| 36                          /*is_opaque=*/false); | 37                          /*is_opaque=*/false); | 
| 37   canvas.DrawGdkPixbuf(pixbuf, 0, 0); | 38   canvas.DrawGdkPixbuf(pixbuf, 0, 0); | 
| 38   return new SkBitmap(canvas.ExtractBitmap()); | 39   return new SkBitmap(canvas.ExtractBitmap()); | 
| 39 } | 40 } | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 77 | 78 | 
| 78   Image::RepresentationType type() const { return type_; } | 79   Image::RepresentationType type() const { return type_; } | 
| 79 | 80 | 
| 80  private: | 81  private: | 
| 81   Image::RepresentationType type_; | 82   Image::RepresentationType type_; | 
| 82 }; | 83 }; | 
| 83 | 84 | 
| 84 class SkBitmapRep : public ImageRep { | 85 class SkBitmapRep : public ImageRep { | 
| 85  public: | 86  public: | 
| 86   explicit SkBitmapRep(const SkBitmap* bitmap) | 87   explicit SkBitmapRep(const SkBitmap* bitmap) | 
|  | 88       : ImageRep(Image::kSkBitmapRep) { | 
|  | 89     CHECK(bitmap); | 
|  | 90     bitmaps_.push_back(bitmap); | 
|  | 91   } | 
|  | 92 | 
|  | 93   explicit SkBitmapRep(const std::vector<const SkBitmap*>& bitmaps) | 
| 87       : ImageRep(Image::kSkBitmapRep), | 94       : ImageRep(Image::kSkBitmapRep), | 
| 88         bitmap_(bitmap) { | 95         bitmaps_(bitmaps) { | 
| 89     CHECK(bitmap); | 96     CHECK(!bitmaps_.empty()); | 
| 90   } | 97   } | 
| 91 | 98 | 
| 92   virtual ~SkBitmapRep() { | 99   virtual ~SkBitmapRep() { | 
| 93     delete bitmap_; | 100     STLDeleteElements(&bitmaps_); | 
| 94     bitmap_ = NULL; |  | 
| 95   } | 101   } | 
| 96 | 102 | 
| 97   const SkBitmap* bitmap() const { return bitmap_; } | 103   const SkBitmap* bitmap() const { return bitmaps_[0]; } | 
|  | 104 | 
|  | 105   const std::vector<const SkBitmap*>& bitmaps() const { return bitmaps_; } | 
| 98 | 106 | 
| 99  private: | 107  private: | 
| 100   const SkBitmap* bitmap_; | 108   std::vector<const SkBitmap*> bitmaps_; | 
| 101 | 109 | 
| 102   DISALLOW_COPY_AND_ASSIGN(SkBitmapRep); | 110   DISALLOW_COPY_AND_ASSIGN(SkBitmapRep); | 
| 103 }; | 111 }; | 
| 104 | 112 | 
| 105 #if defined(OS_LINUX) | 113 #if defined(OS_LINUX) | 
| 106 class GdkPixbufRep : public ImageRep { | 114 class GdkPixbufRep : public ImageRep { | 
| 107  public: | 115  public: | 
| 108   explicit GdkPixbufRep(GdkPixbuf* pixbuf) | 116   explicit GdkPixbufRep(GdkPixbuf* pixbuf) | 
| 109       : ImageRep(Image::kGdkPixbufRep), | 117       : ImageRep(Image::kGdkPixbufRep), | 
| 110         pixbuf_(pixbuf) { | 118         pixbuf_(pixbuf) { | 
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 151 #endif | 159 #endif | 
| 152 | 160 | 
| 153 }  // namespace internal | 161 }  // namespace internal | 
| 154 | 162 | 
| 155 Image::Image(const SkBitmap* bitmap) | 163 Image::Image(const SkBitmap* bitmap) | 
| 156     : default_representation_(Image::kSkBitmapRep) { | 164     : default_representation_(Image::kSkBitmapRep) { | 
| 157   internal::SkBitmapRep* rep = new internal::SkBitmapRep(bitmap); | 165   internal::SkBitmapRep* rep = new internal::SkBitmapRep(bitmap); | 
| 158   AddRepresentation(rep); | 166   AddRepresentation(rep); | 
| 159 } | 167 } | 
| 160 | 168 | 
|  | 169 Image::Image(const std::vector<const SkBitmap*>& bitmaps) | 
|  | 170     : default_representation_(Image::kSkBitmapRep) { | 
|  | 171   internal::SkBitmapRep* rep = new internal::SkBitmapRep(bitmaps); | 
|  | 172   AddRepresentation(rep); | 
|  | 173 } | 
|  | 174 | 
| 161 #if defined(OS_LINUX) | 175 #if defined(OS_LINUX) | 
| 162 Image::Image(GdkPixbuf* pixbuf) | 176 Image::Image(GdkPixbuf* pixbuf) | 
| 163     : default_representation_(Image::kGdkPixbufRep) { | 177     : default_representation_(Image::kGdkPixbufRep) { | 
| 164   internal::GdkPixbufRep* rep = new internal::GdkPixbufRep(pixbuf); | 178   internal::GdkPixbufRep* rep = new internal::GdkPixbufRep(pixbuf); | 
| 165   AddRepresentation(rep); | 179   AddRepresentation(rep); | 
| 166 } | 180 } | 
| 167 #endif | 181 #endif | 
| 168 | 182 | 
| 169 #if defined(OS_MACOSX) | 183 #if defined(OS_MACOSX) | 
| 170 Image::Image(NSImage* image) : default_representation_(Image::kNSImageRep) { | 184 Image::Image(NSImage* image) : default_representation_(Image::kNSImageRep) { | 
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 239     internal::SkBitmapRep* rep = NULL; | 253     internal::SkBitmapRep* rep = NULL; | 
| 240 #if defined(OS_LINUX) | 254 #if defined(OS_LINUX) | 
| 241     if (default_representation_ == Image::kGdkPixbufRep) { | 255     if (default_representation_ == Image::kGdkPixbufRep) { | 
| 242       internal::GdkPixbufRep* pixbuf_rep = default_rep->AsGdkPixbufRep(); | 256       internal::GdkPixbufRep* pixbuf_rep = default_rep->AsGdkPixbufRep(); | 
| 243       rep = new internal::SkBitmapRep( | 257       rep = new internal::SkBitmapRep( | 
| 244           internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); | 258           internal::GdkPixbufToSkBitmap(pixbuf_rep->pixbuf())); | 
| 245     } | 259     } | 
| 246 #elif defined(OS_MACOSX) | 260 #elif defined(OS_MACOSX) | 
| 247     if (default_representation_ == Image::kNSImageRep) { | 261     if (default_representation_ == Image::kNSImageRep) { | 
| 248       internal::NSImageRep* nsimage_rep = default_rep->AsNSImageRep(); | 262       internal::NSImageRep* nsimage_rep = default_rep->AsNSImageRep(); | 
| 249       rep = new internal::SkBitmapRep( | 263       std::vector<const SkBitmap*> bitmaps; | 
| 250           internal::NSImageToSkBitmap(nsimage_rep->image())); | 264       if (internal::NSImageToSkBitmaps(nsimage_rep->image(), bitmaps)) | 
|  | 265         rep = new internal::SkBitmapRep(bitmaps); | 
| 251     } | 266     } | 
| 252 #endif | 267 #endif | 
| 253     CHECK(rep); | 268     CHECK(rep); | 
| 254     AddRepresentation(rep); | 269     AddRepresentation(rep); | 
| 255     return rep; | 270     return rep; | 
| 256   } | 271   } | 
| 257 | 272 | 
| 258   // Handle Skia-to-native conversions. | 273   // Handle Skia-to-native conversions. | 
| 259   if (default_rep->type() == Image::kSkBitmapRep) { | 274   if (default_rep->type() == Image::kSkBitmapRep) { | 
| 260     internal::SkBitmapRep* skia_rep = default_rep->AsSkBitmapRep(); | 275     internal::SkBitmapRep* skia_rep = default_rep->AsSkBitmapRep(); | 
| 261     internal::ImageRep* native_rep = NULL; | 276     internal::ImageRep* native_rep = NULL; | 
| 262 #if defined(OS_LINUX) | 277 #if defined(OS_LINUX) | 
| 263     if (rep_type == Image::kGdkPixbufRep) { | 278     if (rep_type == Image::kGdkPixbufRep) { | 
| 264       GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap()); | 279       GdkPixbuf* pixbuf = gfx::GdkPixbufFromSkBitmap(skia_rep->bitmap()); | 
| 265       native_rep = new internal::GdkPixbufRep(pixbuf); | 280       native_rep = new internal::GdkPixbufRep(pixbuf); | 
| 266     } | 281     } | 
| 267 #elif defined(OS_MACOSX) | 282 #elif defined(OS_MACOSX) | 
| 268     if (rep_type == Image::kNSImageRep) { | 283     if (rep_type == Image::kNSImageRep) { | 
| 269       NSImage* image = gfx::SkBitmapToNSImage(*(skia_rep->bitmap())); | 284       NSImage* image = gfx::SkBitmapsToNSImage(skia_rep->bitmaps()); | 
| 270       base::mac::NSObjectRetain(image); | 285       base::mac::NSObjectRetain(image); | 
| 271       native_rep = new internal::NSImageRep(image); | 286       native_rep = new internal::NSImageRep(image); | 
| 272     } | 287     } | 
| 273 #endif | 288 #endif | 
| 274     CHECK(native_rep); | 289     CHECK(native_rep); | 
| 275     AddRepresentation(native_rep); | 290     AddRepresentation(native_rep); | 
| 276     return native_rep; | 291     return native_rep; | 
| 277   } | 292   } | 
| 278 | 293 | 
| 279   // Something went seriously wrong... | 294   // Something went seriously wrong... | 
| 280   return NULL; | 295   return NULL; | 
| 281 } | 296 } | 
| 282 | 297 | 
| 283 void Image::AddRepresentation(internal::ImageRep* rep) { | 298 void Image::AddRepresentation(internal::ImageRep* rep) { | 
| 284   representations_.insert(std::make_pair(rep->type(), rep)); | 299   representations_.insert(std::make_pair(rep->type(), rep)); | 
| 285 } | 300 } | 
| 286 | 301 | 
|  | 302 size_t Image::GetNumberOfSkBitmaps() { | 
|  | 303   return GetRepresentation(Image::kSkBitmapRep)->AsSkBitmapRep()-> | 
|  | 304       bitmaps().size(); | 
|  | 305 } | 
|  | 306 | 
|  | 307 const SkBitmap* Image::GetSkBitmapAtIndex(size_t index) { | 
|  | 308   return GetRepresentation(Image::kSkBitmapRep)->AsSkBitmapRep()-> | 
|  | 309       bitmaps()[index]; | 
|  | 310 } | 
|  | 311 | 
| 287 }  // namespace gfx | 312 }  // namespace gfx | 
| OLD | NEW | 
|---|