Chromium Code Reviews| 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 // An Image wraps an image any flavor, be it platform-native GdkBitmap/NSImage, | 5 // An Image wraps an image any flavor, be it platform-native GdkBitmap/NSImage, |
| 6 // or a SkBitmap. This also provides easy conversion to other image types | 6 // or a SkBitmap. This also provides easy conversion to other image types |
| 7 // through operator overloading. It will cache the converted representations | 7 // through operator overloading. It will cache the converted representations |
| 8 // internally to prevent double-conversion. | 8 // internally to prevent double-conversion. |
| 9 // | 9 // |
| 10 // The lifetime of both the initial representation and any converted ones are | 10 // The lifetime of both the initial representation and any converted ones are |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 45 namespace internal { | 45 namespace internal { |
| 46 class ImageRep; | 46 class ImageRep; |
| 47 class ImageStorage; | 47 class ImageStorage; |
| 48 } | 48 } |
| 49 | 49 |
| 50 class UI_EXPORT Image { | 50 class UI_EXPORT Image { |
| 51 public: | 51 public: |
| 52 enum RepresentationType { | 52 enum RepresentationType { |
| 53 kImageRepGdk, | 53 kImageRepGdk, |
| 54 kImageRepCocoa, | 54 kImageRepCocoa, |
| 55 kImageRepCocoaTouch, | |
| 55 kImageRepCairo, | 56 kImageRepCairo, |
| 56 kImageRepSkia, | 57 kImageRepSkia, |
| 57 kImageRepPNG, | 58 kImageRepPNG, |
| 58 }; | 59 }; |
| 59 | 60 |
| 60 typedef std::map<RepresentationType, internal::ImageRep*> RepresentationMap; | 61 typedef std::map<RepresentationType, internal::ImageRep*> RepresentationMap; |
| 61 | 62 |
| 62 // Creates an empty image with no representations. | 63 // Creates an empty image with no representations. |
| 63 Image(); | 64 Image(); |
| 64 | 65 |
| 65 // Creates a new image by copying the PNG-encoded input for use as the default | 66 // Creates a new image by copying the PNG-encoded input for use as the default |
| 66 // representation. For example (from an std::vector): | 67 // representation. For example (from an std::vector): |
| 67 // std::vector<unsigned char> png = ...; | 68 // std::vector<unsigned char> png = ...; |
| 68 // gfx::Image image(&png.front(), png.size()); | 69 // gfx::Image image(&png.front(), png.size()); |
| 69 Image(const unsigned char* png, size_t input_size); | 70 Image(const unsigned char* png, size_t input_size); |
| 70 | 71 |
| 71 // Creates a new image by copying the ImageSkia for use as the default | 72 // Creates a new image by copying the ImageSkia for use as the default |
| 72 // representation. | 73 // representation. |
| 73 explicit Image(const ImageSkia& image); | 74 explicit Image(const ImageSkia& image); |
| 74 | 75 |
| 75 // Creates a new image by copying the bitmap for use as the default | 76 // Creates a new image by copying the bitmap for use as the default |
| 76 // representation. | 77 // representation. |
| 77 // TODO(pkotwicz): Get rid of this constructor. | 78 // TODO(pkotwicz): Get rid of this constructor. |
| 78 explicit Image(const SkBitmap& bitmap); | 79 explicit Image(const SkBitmap& bitmap); |
| 79 | 80 |
| 80 #if defined(TOOLKIT_GTK) | 81 #if defined(TOOLKIT_GTK) |
| 81 // Does not increase |pixbuf|'s reference count; expects to take ownership. | 82 // Does not increase |pixbuf|'s reference count; expects to take ownership. |
| 82 explicit Image(GdkPixbuf* pixbuf); | 83 explicit Image(GdkPixbuf* pixbuf); |
| 84 #elif defined(OS_IOS) | |
| 85 // Does not retain |image|; expects to take ownership. | |
| 86 explicit Image(UIImage* image); | |
| 83 #elif defined(OS_MACOSX) | 87 #elif defined(OS_MACOSX) |
| 84 // Does not retain |image|; expects to take ownership. | 88 // Does not retain |image|; expects to take ownership. |
| 85 // A single NSImage object can contain multiple bitmaps so there's no reason | 89 // A single NSImage object can contain multiple bitmaps so there's no reason |
| 86 // to pass a vector of these. | 90 // to pass a vector of these. |
| 87 explicit Image(NSImage* image); | 91 explicit Image(NSImage* image); |
| 88 #endif | 92 #endif |
| 89 | 93 |
| 90 // Initializes a new Image by AddRef()ing |other|'s internal storage. | 94 // Initializes a new Image by AddRef()ing |other|'s internal storage. |
| 91 Image(const Image& other); | 95 Image(const Image& other); |
| 92 | 96 |
| 93 // Copies a reference to |other|'s storage. | 97 // Copies a reference to |other|'s storage. |
| 94 Image& operator=(const Image& other); | 98 Image& operator=(const Image& other); |
| 95 | 99 |
| 96 // Deletes the image and, if the only owner of the storage, all of its cached | 100 // Deletes the image and, if the only owner of the storage, all of its cached |
| 97 // representations. | 101 // representations. |
| 98 ~Image(); | 102 ~Image(); |
| 99 | 103 |
| 100 // Converts the Image to the desired representation and stores it internally. | 104 // Converts the Image to the desired representation and stores it internally. |
| 101 // The returned result is a weak pointer owned by and scoped to the life of | 105 // The returned result is a weak pointer owned by and scoped to the life of |
| 102 // the Image. Must only be called if IsEmpty() is false. | 106 // the Image. Must only be called if IsEmpty() is false. |
| 103 const std::vector<unsigned char>* ToImagePNG() const; | 107 const std::vector<unsigned char>* ToImagePNG() const; |
| 104 const SkBitmap* ToSkBitmap() const; | 108 const SkBitmap* ToSkBitmap() const; |
| 105 const ImageSkia* ToImageSkia() const; | 109 const ImageSkia* ToImageSkia() const; |
| 106 #if defined(TOOLKIT_GTK) | 110 #if defined(TOOLKIT_GTK) |
| 107 GdkPixbuf* ToGdkPixbuf() const; | 111 GdkPixbuf* ToGdkPixbuf() const; |
| 108 CairoCachedSurface* const ToCairo() const; | 112 CairoCachedSurface* const ToCairo() const; |
| 113 #elif defined(OS_IOS) | |
| 114 UIImage* ToUIImage() const; | |
| 109 #elif defined(OS_MACOSX) | 115 #elif defined(OS_MACOSX) |
| 110 NSImage* ToNSImage() const; | 116 NSImage* ToNSImage() const; |
| 111 #endif | 117 #endif |
| 112 | 118 |
| 113 // Same as ToSkBitmap(), but returns a null SkBitmap if this image is empty. | 119 // Same as ToSkBitmap(), but returns a null SkBitmap if this image is empty. |
| 114 SkBitmap AsBitmap() const; | 120 SkBitmap AsBitmap() const; |
| 115 | 121 |
| 116 // Same as ToImageSkia(), but returns an empty ImageSkia if this | 122 // Same as ToImageSkia(), but returns an empty ImageSkia if this |
| 117 // image is empty. | 123 // image is empty. |
| 118 ImageSkia AsImageSkia() const; | 124 ImageSkia AsImageSkia() const; |
| 119 | 125 |
| 120 #if defined(OS_MACOSX) | 126 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 121 // Same as ToSkBitmap(), but returns nil if this image is empty. | 127 // Same as ToSkBitmap(), but returns nil if this image is empty. |
| 122 NSImage* AsNSImage() const; | 128 NSImage* AsNSImage() const; |
| 123 #endif | 129 #endif |
| 124 | 130 |
| 125 // Performs a conversion, like above, but returns a copy of the result rather | 131 // Performs a conversion, like above, but returns a copy of the result rather |
| 126 // than a weak pointer. The caller is responsible for deleting the result. | 132 // than a weak pointer. The caller is responsible for deleting the result. |
| 127 // Note that the result is only a copy in terms of memory management; the | 133 // Note that the result is only a copy in terms of memory management; the |
| 128 // backing pixels are shared amongst all copies (a fact of each of the | 134 // backing pixels are shared amongst all copies (a fact of each of the |
| 129 // converted representations, rather than a limitation imposed by Image) and | 135 // converted representations, rather than a limitation imposed by Image) and |
| 130 // so the result should be considered immutable. | 136 // so the result should be considered immutable. |
| 131 std::vector<unsigned char>* CopyImagePNG() const; | 137 std::vector<unsigned char>* CopyImagePNG() const; |
| 132 ImageSkia* CopyImageSkia() const; | 138 ImageSkia* CopyImageSkia() const; |
| 133 SkBitmap* CopySkBitmap() const; | 139 SkBitmap* CopySkBitmap() const; |
| 134 #if defined(TOOLKIT_GTK) | 140 #if defined(TOOLKIT_GTK) |
| 135 GdkPixbuf* CopyGdkPixbuf() const; | 141 GdkPixbuf* CopyGdkPixbuf() const; |
| 142 #elif defined(OS_IOS) | |
| 143 UIImage* CopyUIImage() const; | |
| 136 #elif defined(OS_MACOSX) | 144 #elif defined(OS_MACOSX) |
| 137 NSImage* CopyNSImage() const; | 145 NSImage* CopyNSImage() const; |
| 138 #endif | 146 #endif |
| 139 | 147 |
| 140 // DEPRECATED ---------------------------------------------------------------- | 148 // DEPRECATED ---------------------------------------------------------------- |
| 141 // Conversion handlers. These wrap the ToType() variants. | 149 // Conversion handlers. These wrap the ToType() variants. |
| 142 #if defined(OS_MACOSX) | 150 #if defined(OS_IOS) |
| 151 operator UIImage*() const; | |
|
Robert Sesek
2012/09/13 16:14:24
Remove. We're trying to kill the operators.
rohitrao (ping after 24h)
2012/09/13 17:57:57
I probably need to keep this in the downstream tre
| |
| 152 #elif defined(OS_MACOSX) | |
| 143 operator NSImage*() const; | 153 operator NSImage*() const; |
| 144 #endif | 154 #endif |
| 145 // --------------------------------------------------------------------------- | 155 // --------------------------------------------------------------------------- |
| 146 | 156 |
| 147 // Inspects the representations map to see if the given type exists. | 157 // Inspects the representations map to see if the given type exists. |
| 148 bool HasRepresentation(RepresentationType type) const; | 158 bool HasRepresentation(RepresentationType type) const; |
| 149 | 159 |
| 150 // Returns the number of representations. | 160 // Returns the number of representations. |
| 151 size_t RepresentationCount() const; | 161 size_t RepresentationCount() const; |
| 152 | 162 |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 172 // be cheaply copied. | 182 // be cheaply copied. |
| 173 scoped_refptr<internal::ImageStorage> storage_; | 183 scoped_refptr<internal::ImageStorage> storage_; |
| 174 | 184 |
| 175 friend class ::ImageTest; | 185 friend class ::ImageTest; |
| 176 friend class ::ImageMacTest; | 186 friend class ::ImageMacTest; |
| 177 }; | 187 }; |
| 178 | 188 |
| 179 } // namespace gfx | 189 } // namespace gfx |
| 180 | 190 |
| 181 #endif // UI_GFX_IMAGE_IMAGE_H_ | 191 #endif // UI_GFX_IMAGE_IMAGE_H_ |
| OLD | NEW |