| 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 "ui/gfx/image/image.h" | 5 #include "ui/gfx/image/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/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage( | 117 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage( |
| 118 UIImage* uiimage); | 118 UIImage* uiimage); |
| 119 // Caller takes ownership of the returned UIImage. | 119 // Caller takes ownership of the returned UIImage. |
| 120 UIImage* CreateUIImageFromPNG( | 120 UIImage* CreateUIImageFromPNG( |
| 121 const std::vector<gfx::ImagePNGRep>& image_png_reps); | 121 const std::vector<gfx::ImagePNGRep>& image_png_reps); |
| 122 gfx::Size UIImageSize(UIImage* image); | 122 gfx::Size UIImageSize(UIImage* image); |
| 123 #elif defined(OS_MACOSX) | 123 #elif defined(OS_MACOSX) |
| 124 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage( | 124 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage( |
| 125 NSImage* nsimage); | 125 NSImage* nsimage); |
| 126 // Caller takes ownership of the returned NSImage. | 126 // Caller takes ownership of the returned NSImage. |
| 127 NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps); | 127 NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps, |
| 128 CGColorSpaceRef color_space); |
| 128 gfx::Size NSImageSize(NSImage* image); | 129 gfx::Size NSImageSize(NSImage* image); |
| 129 #endif // defined(OS_MACOSX) | 130 #endif // defined(OS_MACOSX) |
| 130 | 131 |
| 131 #if defined(OS_IOS) | 132 #if defined(OS_IOS) |
| 132 ImageSkia* ImageSkiaFromPNG( | 133 ImageSkia* ImageSkiaFromPNG( |
| 133 const std::vector<gfx::ImagePNGRep>& image_png_reps); | 134 const std::vector<gfx::ImagePNGRep>& image_png_reps); |
| 134 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( | 135 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( |
| 135 const ImageSkia* skia); | 136 const ImageSkia* skia); |
| 136 #else | 137 #else |
| 137 // Returns a 16x16 red image to visually show error in decoding PNG. | 138 // Returns a 16x16 red image to visually show error in decoding PNG. |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 }; | 466 }; |
| 466 #endif // defined(OS_MACOSX) | 467 #endif // defined(OS_MACOSX) |
| 467 | 468 |
| 468 // The Storage class acts similarly to the pixels in a SkBitmap: the Image | 469 // The Storage class acts similarly to the pixels in a SkBitmap: the Image |
| 469 // class holds a refptr instance of Storage, which in turn holds all the | 470 // class holds a refptr instance of Storage, which in turn holds all the |
| 470 // ImageReps. This way, the Image can be cheaply copied. | 471 // ImageReps. This way, the Image can be cheaply copied. |
| 471 class ImageStorage : public base::RefCounted<ImageStorage> { | 472 class ImageStorage : public base::RefCounted<ImageStorage> { |
| 472 public: | 473 public: |
| 473 ImageStorage(gfx::Image::RepresentationType default_type) | 474 ImageStorage(gfx::Image::RepresentationType default_type) |
| 474 : default_representation_type_(default_type), | 475 : default_representation_type_(default_type), |
| 476 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 477 default_representation_color_space_( |
| 478 base::mac::GetGenericRGBColorSpace()), |
| 479 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 475 representations_deleter_(&representations_) { | 480 representations_deleter_(&representations_) { |
| 476 } | 481 } |
| 477 | 482 |
| 478 gfx::Image::RepresentationType default_representation_type() { | 483 gfx::Image::RepresentationType default_representation_type() { |
| 479 return default_representation_type_; | 484 return default_representation_type_; |
| 480 } | 485 } |
| 481 gfx::Image::RepresentationMap& representations() { return representations_; } | 486 gfx::Image::RepresentationMap& representations() { return representations_; } |
| 482 | 487 |
| 488 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 489 void set_default_representation_color_space(CGColorSpaceRef color_space) { |
| 490 default_representation_color_space_ = color_space; |
| 491 } |
| 492 CGColorSpaceRef default_representation_color_space() { |
| 493 return default_representation_color_space_; |
| 494 } |
| 495 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 496 |
| 483 private: | 497 private: |
| 484 friend class base::RefCounted<ImageStorage>; | 498 friend class base::RefCounted<ImageStorage>; |
| 485 | 499 |
| 486 ~ImageStorage() {} | 500 ~ImageStorage() {} |
| 487 | 501 |
| 488 // The type of image that was passed to the constructor. This key will always | 502 // The type of image that was passed to the constructor. This key will always |
| 489 // exist in the |representations_| map. | 503 // exist in the |representations_| map. |
| 490 gfx::Image::RepresentationType default_representation_type_; | 504 gfx::Image::RepresentationType default_representation_type_; |
| 491 | 505 |
| 506 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 507 // The default representation's colorspace. This is used for converting to |
| 508 // NSImage. This field exists to compensate for PNGCodec not writing or |
| 509 // reading colorspace ancillary chunks. (sRGB, iCCP). |
| 510 // Not owned. |
| 511 CGColorSpaceRef default_representation_color_space_; |
| 512 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 513 |
| 492 // All the representations of an Image. Size will always be at least one, with | 514 // All the representations of an Image. Size will always be at least one, with |
| 493 // more for any converted representations. | 515 // more for any converted representations. |
| 494 gfx::Image::RepresentationMap representations_; | 516 gfx::Image::RepresentationMap representations_; |
| 495 | 517 |
| 496 STLValueDeleter<Image::RepresentationMap> representations_deleter_; | 518 STLValueDeleter<Image::RepresentationMap> representations_deleter_; |
| 497 | 519 |
| 498 DISALLOW_COPY_AND_ASSIGN(ImageStorage); | 520 DISALLOW_COPY_AND_ASSIGN(ImageStorage); |
| 499 }; | 521 }; |
| 500 | 522 |
| 501 } // namespace internal | 523 } // namespace internal |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 703 } | 725 } |
| 704 CHECK(rep); | 726 CHECK(rep); |
| 705 AddRepresentation(rep); | 727 AddRepresentation(rep); |
| 706 } | 728 } |
| 707 return rep->AsImageRepCocoaTouch()->image(); | 729 return rep->AsImageRepCocoaTouch()->image(); |
| 708 } | 730 } |
| 709 #elif defined(OS_MACOSX) | 731 #elif defined(OS_MACOSX) |
| 710 NSImage* Image::ToNSImage() const { | 732 NSImage* Image::ToNSImage() const { |
| 711 internal::ImageRep* rep = GetRepresentation(kImageRepCocoa, false); | 733 internal::ImageRep* rep = GetRepresentation(kImageRepCocoa, false); |
| 712 if (!rep) { | 734 if (!rep) { |
| 735 CGColorSpaceRef default_representation_color_space = |
| 736 storage_->default_representation_color_space(); |
| 737 |
| 713 switch (DefaultRepresentationType()) { | 738 switch (DefaultRepresentationType()) { |
| 714 case kImageRepPNG: { | 739 case kImageRepPNG: { |
| 715 internal::ImageRepPNG* png_rep = | 740 internal::ImageRepPNG* png_rep = |
| 716 GetRepresentation(kImageRepPNG, true)->AsImageRepPNG(); | 741 GetRepresentation(kImageRepPNG, true)->AsImageRepPNG(); |
| 717 rep = new internal::ImageRepCocoa(internal::NSImageFromPNG( | 742 rep = new internal::ImageRepCocoa(internal::NSImageFromPNG( |
| 718 png_rep->image_reps())); | 743 png_rep->image_reps(), default_representation_color_space)); |
| 719 break; | 744 break; |
| 720 } | 745 } |
| 721 case kImageRepSkia: { | 746 case kImageRepSkia: { |
| 722 internal::ImageRepSkia* skia_rep = | 747 internal::ImageRepSkia* skia_rep = |
| 723 GetRepresentation(kImageRepSkia, true)->AsImageRepSkia(); | 748 GetRepresentation(kImageRepSkia, true)->AsImageRepSkia(); |
| 724 NSImage* image = NSImageFromImageSkia(*skia_rep->image()); | 749 NSImage* image = NSImageFromImageSkiaWithColorSpace(*skia_rep->image(), |
| 750 default_representation_color_space); |
| 725 base::mac::NSObjectRetain(image); | 751 base::mac::NSObjectRetain(image); |
| 726 rep = new internal::ImageRepCocoa(image); | 752 rep = new internal::ImageRepCocoa(image); |
| 727 break; | 753 break; |
| 728 } | 754 } |
| 729 default: | 755 default: |
| 730 NOTREACHED(); | 756 NOTREACHED(); |
| 731 } | 757 } |
| 732 CHECK(rep); | 758 CHECK(rep); |
| 733 AddRepresentation(rep); | 759 AddRepresentation(rep); |
| 734 } | 760 } |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 889 gfx::Size Image::Size() const { | 915 gfx::Size Image::Size() const { |
| 890 if (IsEmpty()) | 916 if (IsEmpty()) |
| 891 return gfx::Size(); | 917 return gfx::Size(); |
| 892 return GetRepresentation(DefaultRepresentationType(), true)->Size(); | 918 return GetRepresentation(DefaultRepresentationType(), true)->Size(); |
| 893 } | 919 } |
| 894 | 920 |
| 895 void Image::SwapRepresentations(gfx::Image* other) { | 921 void Image::SwapRepresentations(gfx::Image* other) { |
| 896 storage_.swap(other->storage_); | 922 storage_.swap(other->storage_); |
| 897 } | 923 } |
| 898 | 924 |
| 925 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 926 void Image::SetSourceColorSpace(CGColorSpaceRef color_space) { |
| 927 if (storage_.get()) |
| 928 storage_->set_default_representation_color_space(color_space); |
| 929 } |
| 930 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 931 |
| 899 Image::RepresentationType Image::DefaultRepresentationType() const { | 932 Image::RepresentationType Image::DefaultRepresentationType() const { |
| 900 CHECK(storage_.get()); | 933 CHECK(storage_.get()); |
| 901 RepresentationType default_type = storage_->default_representation_type(); | 934 RepresentationType default_type = storage_->default_representation_type(); |
| 902 // The conversions above assume that the default representation type is never | 935 // The conversions above assume that the default representation type is never |
| 903 // kImageRepCairo. | 936 // kImageRepCairo. |
| 904 DCHECK_NE(default_type, kImageRepCairo); | 937 DCHECK_NE(default_type, kImageRepCairo); |
| 905 return default_type; | 938 return default_type; |
| 906 } | 939 } |
| 907 | 940 |
| 908 internal::ImageRep* Image::GetRepresentation( | 941 internal::ImageRep* Image::GetRepresentation( |
| 909 RepresentationType rep_type, bool must_exist) const { | 942 RepresentationType rep_type, bool must_exist) const { |
| 910 CHECK(storage_.get()); | 943 CHECK(storage_.get()); |
| 911 RepresentationMap::iterator it = storage_->representations().find(rep_type); | 944 RepresentationMap::iterator it = storage_->representations().find(rep_type); |
| 912 if (it == storage_->representations().end()) { | 945 if (it == storage_->representations().end()) { |
| 913 CHECK(!must_exist); | 946 CHECK(!must_exist); |
| 914 return NULL; | 947 return NULL; |
| 915 } | 948 } |
| 916 return it->second; | 949 return it->second; |
| 917 } | 950 } |
| 918 | 951 |
| 919 void Image::AddRepresentation(internal::ImageRep* rep) const { | 952 void Image::AddRepresentation(internal::ImageRep* rep) const { |
| 920 CHECK(storage_.get()); | 953 CHECK(storage_.get()); |
| 921 storage_->representations().insert(std::make_pair(rep->type(), rep)); | 954 storage_->representations().insert(std::make_pair(rep->type(), rep)); |
| 922 } | 955 } |
| 923 | 956 |
| 924 } // namespace gfx | 957 } // namespace gfx |
| OLD | NEW |