Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / image / image.h
blob87c80834f711191d515ba4927a8455f015268ee2
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.
9 //
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_
22 #include <map>
23 #include <vector>
25 #include "base/basictypes.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;
34 #endif
36 class SkBitmap;
38 namespace {
39 class ImageTest;
40 class ImageMacTest;
43 namespace gfx {
44 struct ImagePNGRep;
45 class ImageSkia;
46 class Size;
48 namespace internal {
49 class ImageRep;
50 class ImageStorage;
53 class GFX_EXPORT Image {
54 public:
55 enum RepresentationType {
56 kImageRepCocoa,
57 kImageRepCocoaTouch,
58 kImageRepSkia,
59 kImageRepPNG,
62 typedef std::map<RepresentationType, internal::ImageRep*> RepresentationMap;
64 // Creates an empty image with no representations.
65 Image();
67 // Creates a new image by copying the raw PNG-encoded input for use as the
68 // default representation.
69 explicit Image(const std::vector<ImagePNGRep>& image_reps);
71 // Creates a new image by copying the ImageSkia for use as the default
72 // representation.
73 explicit Image(const ImageSkia& image);
75 #if defined(OS_IOS)
76 // Does not retain |image|; expects to take ownership.
77 explicit Image(UIImage* image);
78 #elif defined(OS_MACOSX)
79 // Does not retain |image|; expects to take ownership.
80 // A single NSImage object can contain multiple bitmaps so there's no reason
81 // to pass a vector of these.
82 explicit Image(NSImage* image);
83 #endif
85 // Initializes a new Image by AddRef()ing |other|'s internal storage.
86 Image(const Image& other);
88 // Copies a reference to |other|'s storage.
89 Image& operator=(const Image& other);
91 // Deletes the image and, if the only owner of the storage, all of its cached
92 // representations.
93 ~Image();
95 // Creates an image from the passed in 1x bitmap.
96 // WARNING: The resulting image will be pixelated when painted on a high
97 // density display.
98 static Image CreateFrom1xBitmap(const SkBitmap& bitmap);
100 // Creates an image from the PNG encoded input.
101 // For example (from an std::vector):
102 // std::vector<unsigned char> png = ...;
103 // gfx::Image image =
104 // Image::CreateFrom1xPNGBytes(&png.front(), png.size());
105 static Image CreateFrom1xPNGBytes(const unsigned char* input,
106 size_t input_size);
108 // Creates an image from the PNG encoded input.
109 static Image CreateFrom1xPNGBytes(
110 const scoped_refptr<base::RefCountedMemory>& input);
112 // Converts the Image to the desired representation and stores it internally.
113 // The returned result is a weak pointer owned by and scoped to the life of
114 // the Image. Must only be called if IsEmpty() is false.
115 const SkBitmap* ToSkBitmap() const;
116 const ImageSkia* ToImageSkia() const;
117 #if defined(OS_IOS)
118 UIImage* ToUIImage() const;
119 #elif defined(OS_MACOSX)
120 NSImage* ToNSImage() const;
121 #endif
123 // Returns the raw PNG-encoded data for the bitmap at 1x. If the data is
124 // unavailable, either because the image has no data for 1x or because it is
125 // empty, an empty RefCountedBytes object is returned. NULL is never
126 // returned.
127 scoped_refptr<base::RefCountedMemory> As1xPNGBytes() const;
129 // Same as ToSkBitmap(), but returns a null SkBitmap if this image is empty.
130 SkBitmap AsBitmap() const;
132 // Same as ToImageSkia(), but returns an empty ImageSkia if this
133 // image is empty.
134 ImageSkia AsImageSkia() const;
136 #if defined(OS_MACOSX) && !defined(OS_IOS)
137 // Same as ToSkBitmap(), but returns nil if this image is empty.
138 NSImage* AsNSImage() const;
139 #endif
141 // Performs a conversion, like above, but returns a copy of the result rather
142 // than a weak pointer. The caller is responsible for deleting the result.
143 // Note that the result is only a copy in terms of memory management; the
144 // backing pixels are shared amongst all copies (a fact of each of the
145 // converted representations, rather than a limitation imposed by Image) and
146 // so the result should be considered immutable.
147 scoped_refptr<base::RefCountedMemory> Copy1xPNGBytes() const;
148 ImageSkia* CopyImageSkia() const;
149 SkBitmap* CopySkBitmap() const;
150 #if defined(OS_IOS)
151 UIImage* CopyUIImage() const;
152 #elif defined(OS_MACOSX)
153 NSImage* CopyNSImage() const;
154 #endif
156 // Inspects the representations map to see if the given type exists.
157 bool HasRepresentation(RepresentationType type) const;
159 // Returns the number of representations.
160 size_t RepresentationCount() const;
162 // Returns true if this Image has no representations.
163 bool IsEmpty() const;
165 // Width and height of image in DIP coordinate system.
166 int Width() const;
167 int Height() const;
168 gfx::Size Size() const;
170 // Swaps this image's internal representations with |other|.
171 void SwapRepresentations(gfx::Image* other);
173 #if defined(OS_MACOSX) && !defined(OS_IOS)
174 // Set the default representation's color space. This is used for converting
175 // to NSImage. This is used to compensate for PNGCodec not writing or reading
176 // colorspace ancillary chunks. (sRGB, iCCP).
177 void SetSourceColorSpace(CGColorSpaceRef color_space);
178 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
180 private:
181 // Returns the type of the default representation.
182 RepresentationType DefaultRepresentationType() const;
184 // Returns the ImageRep of the appropriate type or NULL if there is no
185 // representation of that type (and must_exist is false).
186 internal::ImageRep* GetRepresentation(
187 RepresentationType rep_type, bool must_exist) const;
189 // Stores a representation into the map.
190 void AddRepresentation(scoped_ptr<internal::ImageRep> rep) const;
192 // Internal class that holds all the representations. This allows the Image to
193 // be cheaply copied.
194 scoped_refptr<internal::ImageStorage> storage_;
196 friend class ::ImageTest;
197 friend class ::ImageMacTest;
200 } // namespace gfx
202 #endif // UI_GFX_IMAGE_IMAGE_H_