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
3 // found in the LICENSE file.
5 #include "ui/gfx/image/image.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/gfx/image/image_png_rep.h"
14 #include "ui/gfx/image/image_skia.h"
15 #include "ui/gfx/size.h"
18 #include "ui/gfx/codec/png_codec.h"
22 #include "base/mac/foundation_util.h"
23 #include "ui/gfx/image/image_skia_util_ios.h"
24 #elif defined(OS_MACOSX)
25 #include "base/mac/mac_util.h"
26 #include "ui/gfx/image/image_skia_util_mac.h"
34 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage(
36 // Caller takes ownership of the returned UIImage.
37 UIImage* CreateUIImageFromPNG(
38 const std::vector<gfx::ImagePNGRep>& image_png_reps);
39 gfx::Size UIImageSize(UIImage* image);
40 #elif defined(OS_MACOSX)
41 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage(
43 // Caller takes ownership of the returned NSImage.
44 NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps,
45 CGColorSpaceRef color_space);
46 gfx::Size NSImageSize(NSImage* image);
47 #endif // defined(OS_MACOSX)
50 ImageSkia* ImageSkiaFromPNG(
51 const std::vector<gfx::ImagePNGRep>& image_png_reps);
52 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia(
53 const ImageSkia* skia);
55 // Returns a 16x16 red image to visually show error in decoding PNG.
56 // Caller takes ownership of returned ImageSkia.
57 ImageSkia* GetErrorImageSkia() {
59 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16);
61 bitmap.eraseRGB(0xff, 0, 0);
62 return new gfx::ImageSkia(gfx::ImageSkiaRep(bitmap, 1.0f));
65 ImageSkia* ImageSkiaFromPNG(
66 const std::vector<gfx::ImagePNGRep>& image_png_reps) {
67 if (image_png_reps.empty())
68 return GetErrorImageSkia();
70 scoped_ptr<gfx::ImageSkia> image_skia(new ImageSkia());
71 for (size_t i = 0; i < image_png_reps.size(); ++i) {
72 scoped_refptr<base::RefCountedMemory> raw_data =
73 image_png_reps[i].raw_data;
74 CHECK(raw_data.get());
76 if (!gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(),
78 LOG(ERROR) << "Unable to decode PNG for "
79 << image_png_reps[i].scale
81 return GetErrorImageSkia();
83 image_skia->AddRepresentation(gfx::ImageSkiaRep(
84 bitmap, image_png_reps[i].scale));
86 return image_skia.release();
89 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia(
90 const ImageSkia* image_skia) {
91 ImageSkiaRep image_skia_rep = image_skia->GetRepresentation(1.0f);
93 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes());
94 if (image_skia_rep.scale() != 1.0f ||
95 !gfx::PNGCodec::EncodeBGRASkBitmap(image_skia_rep.sk_bitmap(), false,
96 &png_bytes->data())) {
107 class ImageRepCocoaTouch;
109 // An ImageRep is the object that holds the backing memory for an Image. Each
110 // RepresentationType has an ImageRep subclass that is responsible for freeing
111 // the memory that the ImageRep holds. When an ImageRep is created, it expects
112 // to take ownership of the image, without having to retain it or increase its
116 explicit ImageRep(Image::RepresentationType rep) : type_(rep) {}
118 // Deletes the associated pixels of an ImageRep.
119 virtual ~ImageRep() {}
121 // Cast helpers ("fake RTTI").
122 ImageRepPNG* AsImageRepPNG() {
123 CHECK_EQ(type_, Image::kImageRepPNG);
124 return reinterpret_cast<ImageRepPNG*>(this);
127 ImageRepSkia* AsImageRepSkia() {
128 CHECK_EQ(type_, Image::kImageRepSkia);
129 return reinterpret_cast<ImageRepSkia*>(this);
133 ImageRepCocoaTouch* AsImageRepCocoaTouch() {
134 CHECK_EQ(type_, Image::kImageRepCocoaTouch);
135 return reinterpret_cast<ImageRepCocoaTouch*>(this);
137 #elif defined(OS_MACOSX)
138 ImageRepCocoa* AsImageRepCocoa() {
139 CHECK_EQ(type_, Image::kImageRepCocoa);
140 return reinterpret_cast<ImageRepCocoa*>(this);
144 Image::RepresentationType type() const { return type_; }
146 virtual int Width() const = 0;
147 virtual int Height() const = 0;
148 virtual gfx::Size Size() const = 0;
151 Image::RepresentationType type_;
154 class ImageRepPNG : public ImageRep {
156 ImageRepPNG() : ImageRep(Image::kImageRepPNG) {
159 ImageRepPNG(const std::vector<ImagePNGRep>& image_png_reps)
160 : ImageRep(Image::kImageRepPNG),
161 image_png_reps_(image_png_reps) {
164 virtual ~ImageRepPNG() {
167 virtual int Width() const OVERRIDE {
168 return Size().width();
171 virtual int Height() const OVERRIDE {
172 return Size().height();
175 virtual gfx::Size Size() const OVERRIDE {
176 // Read the PNG data to get the image size, caching it.
178 for (std::vector<ImagePNGRep>::const_iterator it = image_reps().begin();
179 it != image_reps().end(); ++it) {
180 if (it->scale == 1.0f) {
181 size_cache_.reset(new gfx::Size(it->Size()));
185 size_cache_.reset(new gfx::Size);
191 const std::vector<ImagePNGRep>& image_reps() const { return image_png_reps_; }
194 std::vector<ImagePNGRep> image_png_reps_;
196 // Cached to avoid having to parse the raw data multiple times.
197 mutable scoped_ptr<gfx::Size> size_cache_;
199 DISALLOW_COPY_AND_ASSIGN(ImageRepPNG);
202 class ImageRepSkia : public ImageRep {
204 // Takes ownership of |image|.
205 explicit ImageRepSkia(ImageSkia* image)
206 : ImageRep(Image::kImageRepSkia),
210 virtual ~ImageRepSkia() {
213 virtual int Width() const OVERRIDE {
214 return image_->width();
217 virtual int Height() const OVERRIDE {
218 return image_->height();
221 virtual gfx::Size Size() const OVERRIDE {
222 return image_->size();
225 ImageSkia* image() { return image_.get(); }
228 scoped_ptr<ImageSkia> image_;
230 DISALLOW_COPY_AND_ASSIGN(ImageRepSkia);
234 class ImageRepCocoaTouch : public ImageRep {
236 explicit ImageRepCocoaTouch(UIImage* image)
237 : ImageRep(Image::kImageRepCocoaTouch),
242 virtual ~ImageRepCocoaTouch() {
243 base::mac::NSObjectRelease(image_);
247 virtual int Width() const OVERRIDE {
248 return Size().width();
251 virtual int Height() const OVERRIDE {
252 return Size().height();
255 virtual gfx::Size Size() const OVERRIDE {
256 return internal::UIImageSize(image_);
259 UIImage* image() const { return image_; }
264 DISALLOW_COPY_AND_ASSIGN(ImageRepCocoaTouch);
266 #elif defined(OS_MACOSX)
267 class ImageRepCocoa : public ImageRep {
269 explicit ImageRepCocoa(NSImage* image)
270 : ImageRep(Image::kImageRepCocoa),
275 virtual ~ImageRepCocoa() {
276 base::mac::NSObjectRelease(image_);
280 virtual int Width() const OVERRIDE {
281 return Size().width();
284 virtual int Height() const OVERRIDE {
285 return Size().height();
288 virtual gfx::Size Size() const OVERRIDE {
289 return internal::NSImageSize(image_);
292 NSImage* image() const { return image_; }
297 DISALLOW_COPY_AND_ASSIGN(ImageRepCocoa);
299 #endif // defined(OS_MACOSX)
301 // The Storage class acts similarly to the pixels in a SkBitmap: the Image
302 // class holds a refptr instance of Storage, which in turn holds all the
303 // ImageReps. This way, the Image can be cheaply copied.
304 class ImageStorage : public base::RefCounted<ImageStorage> {
306 ImageStorage(gfx::Image::RepresentationType default_type)
307 : default_representation_type_(default_type),
308 #if defined(OS_MACOSX) && !defined(OS_IOS)
309 default_representation_color_space_(
310 base::mac::GetGenericRGBColorSpace()),
311 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
312 representations_deleter_(&representations_) {
315 gfx::Image::RepresentationType default_representation_type() {
316 return default_representation_type_;
318 gfx::Image::RepresentationMap& representations() { return representations_; }
320 #if defined(OS_MACOSX) && !defined(OS_IOS)
321 void set_default_representation_color_space(CGColorSpaceRef color_space) {
322 default_representation_color_space_ = color_space;
324 CGColorSpaceRef default_representation_color_space() {
325 return default_representation_color_space_;
327 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
330 friend class base::RefCounted<ImageStorage>;
334 // The type of image that was passed to the constructor. This key will always
335 // exist in the |representations_| map.
336 gfx::Image::RepresentationType default_representation_type_;
338 #if defined(OS_MACOSX) && !defined(OS_IOS)
339 // The default representation's colorspace. This is used for converting to
340 // NSImage. This field exists to compensate for PNGCodec not writing or
341 // reading colorspace ancillary chunks. (sRGB, iCCP).
343 CGColorSpaceRef default_representation_color_space_;
344 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
346 // All the representations of an Image. Size will always be at least one, with
347 // more for any converted representations.
348 gfx::Image::RepresentationMap representations_;
350 STLValueDeleter<Image::RepresentationMap> representations_deleter_;
352 DISALLOW_COPY_AND_ASSIGN(ImageStorage);
355 } // namespace internal
358 // |storage_| is NULL for empty Images.
361 Image::Image(const std::vector<ImagePNGRep>& image_reps) {
362 // Do not store obviously invalid ImagePNGReps.
363 std::vector<ImagePNGRep> filtered;
364 for (size_t i = 0; i < image_reps.size(); ++i) {
365 if (image_reps[i].raw_data.get() && image_reps[i].raw_data->size())
366 filtered.push_back(image_reps[i]);
369 if (filtered.empty())
372 storage_ = new internal::ImageStorage(Image::kImageRepPNG);
373 internal::ImageRepPNG* rep = new internal::ImageRepPNG(filtered);
374 AddRepresentation(rep);
377 Image::Image(const ImageSkia& image) {
378 if (!image.isNull()) {
379 storage_ = new internal::ImageStorage(Image::kImageRepSkia);
380 internal::ImageRepSkia* rep = new internal::ImageRepSkia(
381 new ImageSkia(image));
382 AddRepresentation(rep);
387 Image::Image(UIImage* image)
388 : storage_(new internal::ImageStorage(Image::kImageRepCocoaTouch)) {
390 internal::ImageRepCocoaTouch* rep = new internal::ImageRepCocoaTouch(image);
391 AddRepresentation(rep);
394 #elif defined(OS_MACOSX)
395 Image::Image(NSImage* image) {
397 storage_ = new internal::ImageStorage(Image::kImageRepCocoa);
398 internal::ImageRepCocoa* rep = new internal::ImageRepCocoa(image);
399 AddRepresentation(rep);
404 Image::Image(const Image& other) : storage_(other.storage_) {
407 Image& Image::operator=(const Image& other) {
408 storage_ = other.storage_;
416 Image Image::CreateFrom1xBitmap(const SkBitmap& bitmap) {
417 return gfx::Image(ImageSkia::CreateFrom1xBitmap(bitmap));
421 Image Image::CreateFrom1xPNGBytes(const unsigned char* input,
423 if (input_size == 0u)
426 scoped_refptr<base::RefCountedBytes> raw_data(new base::RefCountedBytes());
427 raw_data->data().assign(input, input + input_size);
429 return CreateFrom1xPNGBytes(raw_data);
432 Image Image::CreateFrom1xPNGBytes(
433 const scoped_refptr<base::RefCountedMemory>& input) {
434 if (!input.get() || input->size() == 0u)
437 std::vector<gfx::ImagePNGRep> image_reps;
438 image_reps.push_back(ImagePNGRep(input, 1.0f));
439 return gfx::Image(image_reps);
442 const SkBitmap* Image::ToSkBitmap() const {
443 // Possibly create and cache an intermediate ImageRepSkia.
444 return ToImageSkia()->bitmap();
447 const ImageSkia* Image::ToImageSkia() const {
448 internal::ImageRep* rep = GetRepresentation(kImageRepSkia, false);
450 switch (DefaultRepresentationType()) {
452 internal::ImageRepPNG* png_rep =
453 GetRepresentation(kImageRepPNG, true)->AsImageRepPNG();
454 rep = new internal::ImageRepSkia(
455 internal::ImageSkiaFromPNG(png_rep->image_reps()));
459 case kImageRepCocoaTouch: {
460 internal::ImageRepCocoaTouch* native_rep =
461 GetRepresentation(kImageRepCocoaTouch, true)
462 ->AsImageRepCocoaTouch();
463 rep = new internal::ImageRepSkia(new ImageSkia(
464 ImageSkiaFromUIImage(native_rep->image())));
467 #elif defined(OS_MACOSX)
468 case kImageRepCocoa: {
469 internal::ImageRepCocoa* native_rep =
470 GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa();
471 rep = new internal::ImageRepSkia(new ImageSkia(
472 ImageSkiaFromNSImage(native_rep->image())));
480 AddRepresentation(rep);
482 return rep->AsImageRepSkia()->image();
486 UIImage* Image::ToUIImage() const {
487 internal::ImageRep* rep = GetRepresentation(kImageRepCocoaTouch, false);
489 switch (DefaultRepresentationType()) {
491 internal::ImageRepPNG* png_rep =
492 GetRepresentation(kImageRepPNG, true)->AsImageRepPNG();
493 rep = new internal::ImageRepCocoaTouch(internal::CreateUIImageFromPNG(
494 png_rep->image_reps()));
497 case kImageRepSkia: {
498 internal::ImageRepSkia* skia_rep =
499 GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
500 UIImage* image = UIImageFromImageSkia(*skia_rep->image());
501 base::mac::NSObjectRetain(image);
502 rep = new internal::ImageRepCocoaTouch(image);
509 AddRepresentation(rep);
511 return rep->AsImageRepCocoaTouch()->image();
513 #elif defined(OS_MACOSX)
514 NSImage* Image::ToNSImage() const {
515 internal::ImageRep* rep = GetRepresentation(kImageRepCocoa, false);
517 CGColorSpaceRef default_representation_color_space =
518 storage_->default_representation_color_space();
520 switch (DefaultRepresentationType()) {
522 internal::ImageRepPNG* png_rep =
523 GetRepresentation(kImageRepPNG, true)->AsImageRepPNG();
524 rep = new internal::ImageRepCocoa(internal::NSImageFromPNG(
525 png_rep->image_reps(), default_representation_color_space));
528 case kImageRepSkia: {
529 internal::ImageRepSkia* skia_rep =
530 GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
531 NSImage* image = NSImageFromImageSkiaWithColorSpace(*skia_rep->image(),
532 default_representation_color_space);
533 base::mac::NSObjectRetain(image);
534 rep = new internal::ImageRepCocoa(image);
541 AddRepresentation(rep);
543 return rep->AsImageRepCocoa()->image();
547 scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const {
549 return new base::RefCountedBytes();
551 internal::ImageRep* rep = GetRepresentation(kImageRepPNG, false);
554 const std::vector<gfx::ImagePNGRep>& image_png_reps =
555 rep->AsImageRepPNG()->image_reps();
556 for (size_t i = 0; i < image_png_reps.size(); ++i) {
557 if (image_png_reps[i].scale == 1.0f)
558 return image_png_reps[i].raw_data;
560 return new base::RefCountedBytes();
563 scoped_refptr<base::RefCountedMemory> png_bytes(NULL);
564 switch (DefaultRepresentationType()) {
566 case kImageRepCocoaTouch: {
567 internal::ImageRepCocoaTouch* cocoa_touch_rep =
568 GetRepresentation(kImageRepCocoaTouch, true)
569 ->AsImageRepCocoaTouch();
570 png_bytes = internal::Get1xPNGBytesFromUIImage(
571 cocoa_touch_rep->image());
574 #elif defined(OS_MACOSX)
575 case kImageRepCocoa: {
576 internal::ImageRepCocoa* cocoa_rep =
577 GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa();
578 png_bytes = internal::Get1xPNGBytesFromNSImage(cocoa_rep->image());
582 case kImageRepSkia: {
583 internal::ImageRepSkia* skia_rep =
584 GetRepresentation(kImageRepSkia, true)->AsImageRepSkia();
585 png_bytes = internal::Get1xPNGBytesFromImageSkia(skia_rep->image());
591 if (!png_bytes.get() || !png_bytes->size()) {
592 // Add an ImageRepPNG with no data such that the conversion is not
593 // attempted each time we want the PNG bytes.
594 AddRepresentation(new internal::ImageRepPNG());
595 return new base::RefCountedBytes();
598 // Do not insert representations for scale factors other than 1x even if
599 // they are available because:
600 // - Only the 1x PNG bytes can be accessed.
601 // - ImageRepPNG is not used as an intermediate type in converting to a
602 // final type eg (converting from ImageRepSkia to ImageRepPNG to get an
604 std::vector<ImagePNGRep> image_png_reps;
605 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, 1.0f));
606 rep = new internal::ImageRepPNG(image_png_reps);
607 AddRepresentation(rep);
611 SkBitmap Image::AsBitmap() const {
612 return IsEmpty() ? SkBitmap() : *ToSkBitmap();
615 ImageSkia Image::AsImageSkia() const {
616 return IsEmpty() ? ImageSkia() : *ToImageSkia();
619 #if defined(OS_MACOSX) && !defined(OS_IOS)
620 NSImage* Image::AsNSImage() const {
621 return IsEmpty() ? nil : ToNSImage();
625 scoped_refptr<base::RefCountedMemory> Image::Copy1xPNGBytes() const {
626 scoped_refptr<base::RefCountedMemory> original = As1xPNGBytes();
627 scoped_refptr<base::RefCountedBytes> copy(new base::RefCountedBytes());
628 copy->data().assign(original->front(), original->front() + original->size());
632 ImageSkia* Image::CopyImageSkia() const {
633 return new ImageSkia(*ToImageSkia());
636 SkBitmap* Image::CopySkBitmap() const {
637 return new SkBitmap(*ToSkBitmap());
641 UIImage* Image::CopyUIImage() const {
642 UIImage* image = ToUIImage();
643 base::mac::NSObjectRetain(image);
646 #elif defined(OS_MACOSX)
647 NSImage* Image::CopyNSImage() const {
648 NSImage* image = ToNSImage();
649 base::mac::NSObjectRetain(image);
654 bool Image::HasRepresentation(RepresentationType type) const {
655 return storage_.get() && storage_->representations().count(type) != 0;
658 size_t Image::RepresentationCount() const {
662 return storage_->representations().size();
665 bool Image::IsEmpty() const {
666 return RepresentationCount() == 0;
669 int Image::Width() const {
672 return GetRepresentation(DefaultRepresentationType(), true)->Width();
675 int Image::Height() const {
678 return GetRepresentation(DefaultRepresentationType(), true)->Height();
681 gfx::Size Image::Size() const {
684 return GetRepresentation(DefaultRepresentationType(), true)->Size();
687 void Image::SwapRepresentations(gfx::Image* other) {
688 storage_.swap(other->storage_);
691 #if defined(OS_MACOSX) && !defined(OS_IOS)
692 void Image::SetSourceColorSpace(CGColorSpaceRef color_space) {
694 storage_->set_default_representation_color_space(color_space);
696 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
698 Image::RepresentationType Image::DefaultRepresentationType() const {
699 CHECK(storage_.get());
700 RepresentationType default_type = storage_->default_representation_type();
701 // The conversions above assume that the default representation type is never
703 DCHECK_NE(default_type, kImageRepCairo);
707 internal::ImageRep* Image::GetRepresentation(
708 RepresentationType rep_type, bool must_exist) const {
709 CHECK(storage_.get());
710 RepresentationMap::iterator it = storage_->representations().find(rep_type);
711 if (it == storage_->representations().end()) {
718 void Image::AddRepresentation(internal::ImageRep* rep) const {
719 CHECK(storage_.get());
720 storage_->representations().insert(std::make_pair(rep->type(), rep));