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 // 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
7 // through operator overloading. It will cache the converted representations
8 // internally to prevent double-conversion.
10 // The lifetime of both the initial representation and any converted ones are
11 // tied to the lifetime of the Image's internal storage. To allow Images to be
12 // cheaply passed around by value, the actual image data is stored in a ref-
13 // counted member. When all Images referencing this storage are deleted, the
14 // actual representations are deleted, too.
16 // Images can be empty, in which case they have no backing representation.
17 // Attempting to use an empty Image will result in a crash.
19 #ifndef UI_GFX_IMAGE_IMAGE_H_
20 #define UI_GFX_IMAGE_IMAGE_H_
24 #include "base/basictypes.h"
25 #include "base/containers/scoped_ptr_map.h"
26 #include "base/gtest_prod_util.h"
27 #include "base/memory/ref_counted_memory.h"
28 #include "base/memory/scoped_ptr.h"
29 #include "ui/gfx/gfx_export.h"
30 #include "ui/gfx/native_widget_types.h"
32 #if defined(OS_MACOSX) && !defined(OS_IOS)
33 typedef struct CGColorSpace
* CGColorSpaceRef
;
53 class GFX_EXPORT Image
{
55 enum RepresentationType
{
62 typedef base::ScopedPtrMap
<RepresentationType
, scoped_ptr
<internal::ImageRep
>>
65 // Creates an empty image with no representations.
68 // Creates a new image by copying the raw PNG-encoded input for use as the
69 // default representation.
70 explicit Image(const std::vector
<ImagePNGRep
>& image_reps
);
72 // Creates a new image by copying the ImageSkia for use as the default
74 explicit Image(const ImageSkia
& image
);
77 // Does not retain |image|; expects to take ownership.
78 explicit Image(UIImage
* image
);
79 #elif defined(OS_MACOSX)
80 // Does not retain |image|; expects to take ownership.
81 // A single NSImage object can contain multiple bitmaps so there's no reason
82 // to pass a vector of these.
83 explicit Image(NSImage
* image
);
86 // Initializes a new Image by AddRef()ing |other|'s internal storage.
87 Image(const Image
& other
);
89 // Copies a reference to |other|'s storage.
90 Image
& operator=(const Image
& other
);
92 // Deletes the image and, if the only owner of the storage, all of its cached
96 // Creates an image from the passed in 1x bitmap.
97 // WARNING: The resulting image will be pixelated when painted on a high
99 static Image
CreateFrom1xBitmap(const SkBitmap
& bitmap
);
101 // Creates an image from the PNG encoded input.
102 // For example (from an std::vector):
103 // std::vector<unsigned char> png = ...;
104 // gfx::Image image =
105 // Image::CreateFrom1xPNGBytes(&png.front(), png.size());
106 static Image
CreateFrom1xPNGBytes(const unsigned char* input
,
109 // Creates an image from the PNG encoded input.
110 static Image
CreateFrom1xPNGBytes(
111 const scoped_refptr
<base::RefCountedMemory
>& input
);
113 // Converts the Image to the desired representation and stores it internally.
114 // The returned result is a weak pointer owned by and scoped to the life of
115 // the Image. Must only be called if IsEmpty() is false.
116 const SkBitmap
* ToSkBitmap() const;
117 const ImageSkia
* ToImageSkia() const;
119 UIImage
* ToUIImage() const;
120 #elif defined(OS_MACOSX)
121 NSImage
* ToNSImage() const;
124 // Returns the raw PNG-encoded data for the bitmap at 1x. If the data is
125 // unavailable, either because the image has no data for 1x or because it is
126 // empty, an empty RefCountedBytes object is returned. NULL is never
128 scoped_refptr
<base::RefCountedMemory
> As1xPNGBytes() const;
130 // Same as ToSkBitmap(), but returns a null SkBitmap if this image is empty.
131 SkBitmap
AsBitmap() const;
133 // Same as ToImageSkia(), but returns an empty ImageSkia if this
135 ImageSkia
AsImageSkia() const;
137 #if defined(OS_MACOSX) && !defined(OS_IOS)
138 // Same as ToSkBitmap(), but returns nil if this image is empty.
139 NSImage
* AsNSImage() const;
142 // Performs a conversion, like above, but returns a copy of the result rather
143 // than a weak pointer. The caller is responsible for deleting the result.
144 // Note that the result is only a copy in terms of memory management; the
145 // backing pixels are shared amongst all copies (a fact of each of the
146 // converted representations, rather than a limitation imposed by Image) and
147 // so the result should be considered immutable.
148 scoped_refptr
<base::RefCountedMemory
> Copy1xPNGBytes() const;
149 ImageSkia
* CopyImageSkia() const;
150 SkBitmap
* CopySkBitmap() const;
152 UIImage
* CopyUIImage() const;
153 #elif defined(OS_MACOSX)
154 NSImage
* CopyNSImage() const;
157 // Inspects the representations map to see if the given type exists.
158 bool HasRepresentation(RepresentationType type
) const;
160 // Returns the number of representations.
161 size_t RepresentationCount() const;
163 // Returns true if this Image has no representations.
164 bool IsEmpty() const;
166 // Width and height of image in DIP coordinate system.
169 gfx::Size
Size() const;
171 // Swaps this image's internal representations with |other|.
172 void SwapRepresentations(gfx::Image
* other
);
174 #if defined(OS_MACOSX) && !defined(OS_IOS)
175 // Set the default representation's color space. This is used for converting
176 // to NSImage. This is used to compensate for PNGCodec not writing or reading
177 // colorspace ancillary chunks. (sRGB, iCCP).
178 void SetSourceColorSpace(CGColorSpaceRef color_space
);
179 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
182 // Returns the type of the default representation.
183 RepresentationType
DefaultRepresentationType() const;
185 // Returns the ImageRep of the appropriate type or NULL if there is no
186 // representation of that type (and must_exist is false).
187 internal::ImageRep
* GetRepresentation(
188 RepresentationType rep_type
, bool must_exist
) const;
190 // Stores a representation into the map.
191 void AddRepresentation(scoped_ptr
<internal::ImageRep
> rep
) const;
193 // Internal class that holds all the representations. This allows the Image to
194 // be cheaply copied.
195 scoped_refptr
<internal::ImageStorage
> storage_
;
197 friend class ::ImageTest
;
198 friend class ::ImageMacTest
;
203 #endif // UI_GFX_IMAGE_IMAGE_H_