add a use_alsa gyp setting
[chromium-blink-merge.git] / ui / gfx / icon_util.h
blobb48c0a52af7c150188f808818f4422ee3bd46e7a
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 #ifndef UI_GFX_ICON_UTIL_H_
6 #define UI_GFX_ICON_UTIL_H_
8 #include <windows.h>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "ui/base/ui_export.h"
16 #include "ui/gfx/point.h"
17 #include "ui/gfx/size.h"
19 namespace base {
20 class FilePath;
23 namespace gfx {
24 class Size;
26 class SkBitmap;
28 ///////////////////////////////////////////////////////////////////////////////
30 // The IconUtil class contains helper functions for manipulating Windows icons.
31 // The class interface contains methods for converting an HICON handle into an
32 // SkBitmap object and vice versa. The class can also create a .ico file given
33 // a PNG image contained in an SkBitmap object. The following code snippet
34 // shows an example usage of IconUtil::CreateHICONFromSkBitmap():
36 // SkBitmap bitmap;
38 // // Fill |bitmap| with valid data
39 // bitmap.setConfig(...);
40 // bitmap.allocPixels();
42 // ...
44 // // Convert the bitmap into a Windows HICON
45 // HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap);
46 // if (icon == NULL) {
47 // // Handle error
48 // ...
49 // }
51 // // Use the icon with a WM_SETICON message
52 // ::SendMessage(hwnd, WM_SETICON, static_cast<WPARAM>(ICON_BIG),
53 // reinterpret_cast<LPARAM>(icon));
55 // // Destroy the icon when we are done
56 // ::DestroyIcon(icon);
58 ///////////////////////////////////////////////////////////////////////////////
59 class UI_EXPORT IconUtil {
60 public:
61 // The size of the large icon entries in .ico files on Windows Vista+.
62 static const int kLargeIconSize = 256;
64 // Given an SkBitmap object, the function converts the bitmap to a Windows
65 // icon and returns the corresponding HICON handle. If the function cannot
66 // convert the bitmap, NULL is returned.
68 // The client is responsible for destroying the icon when it is no longer
69 // needed by calling ::DestroyIcon().
70 static HICON CreateHICONFromSkBitmap(const SkBitmap& bitmap);
72 // Given a valid HICON handle representing an icon, this function converts
73 // the icon into an SkBitmap object containing an ARGB bitmap using the
74 // dimensions specified in |s|. |s| must specify valid dimensions (both
75 // width() an height() must be greater than zero). If the function cannot
76 // convert the icon to a bitmap (most probably due to an invalid parameter),
77 // the return value is NULL.
79 // The client owns the returned bitmap object and is responsible for deleting
80 // it when it is no longer needed.
81 static SkBitmap* CreateSkBitmapFromHICON(HICON icon, const gfx::Size& s);
83 // Loads an icon resource as a SkBitmap for the specified |size| from a
84 // loaded .dll or .exe |module|. Supports loading smaller icon sizes as well
85 // as the Vista+ 256x256 PNG icon size. If the icon could not be loaded or
86 // found, returns a NULL scoped_ptr.
87 static scoped_ptr<SkBitmap> CreateSkBitmapFromIconResource(HMODULE module,
88 int resource_id,
89 int size);
91 // Given a valid HICON handle representing an icon, this function converts
92 // the icon into an SkBitmap object containing an ARGB bitmap using the
93 // dimensions of HICON. If the function cannot convert the icon to a bitmap
94 // (most probably due to an invalid parameter), the return value is NULL.
96 // The client owns the returned bitmap object and is responsible for deleting
97 // it when it is no longer needed.
98 static SkBitmap* CreateSkBitmapFromHICON(HICON icon);
100 // Creates Windows .ico file at |icon_path|. The icon file is created with
101 // multiple BMP representations at varying predefined dimensions (by resizing
102 // |bitmap|) because Windows uses different image sizes when loading icons,
103 // depending on where the icon is drawn (ALT+TAB window, desktop shortcut,
104 // Quick Launch, etc.).
106 // To create an icon file containing a 256x256 PNG entry, which is used by
107 // Vista+ for high res icons, specify a non-empty 256x256 SkBitmap for the
108 // |large_bitmap| parameter.
110 // The function returns true on success and false otherwise.
111 static bool CreateIconFileFromSkBitmap(const SkBitmap& bitmap,
112 const SkBitmap& large_bitmap,
113 const base::FilePath& icon_path);
115 // Creates a cursor of the specified size from the DIB passed in.
116 // Returns the cursor on success or NULL on failure.
117 static HICON CreateCursorFromDIB(const gfx::Size& icon_size,
118 const gfx::Point& hotspot,
119 const void* dib_bits,
120 size_t dib_size);
122 private:
123 // The icon format is published in the MSDN but there is no definition of
124 // the icon file structures in any of the Windows header files so we need to
125 // define these structure within the class. We must make sure we use 2 byte
126 // packing so that the structures are layed out properly within the file.
127 // See: http://msdn.microsoft.com/en-us/library/ms997538.aspx
128 #pragma pack(push)
129 #pragma pack(2)
131 // ICONDIRENTRY contains meta data for an individual icon image within a
132 // .ico file.
133 struct ICONDIRENTRY {
134 BYTE bWidth;
135 BYTE bHeight;
136 BYTE bColorCount;
137 BYTE bReserved;
138 WORD wPlanes;
139 WORD wBitCount;
140 DWORD dwBytesInRes;
141 DWORD dwImageOffset;
144 // ICONDIR Contains information about all the icon images contained within a
145 // single .ico file.
146 struct ICONDIR {
147 WORD idReserved;
148 WORD idType;
149 WORD idCount;
150 ICONDIRENTRY idEntries[1];
153 // GRPICONDIRENTRY contains meta data for an individual icon image within a
154 // RT_GROUP_ICON resource in an .exe or .dll.
155 struct GRPICONDIRENTRY {
156 BYTE bWidth;
157 BYTE bHeight;
158 BYTE bColorCount;
159 BYTE bReserved;
160 WORD wPlanes;
161 WORD wBitCount;
162 DWORD dwBytesInRes;
163 WORD nID;
166 // GRPICONDIR Contains information about all the icon images contained within
167 // a RT_GROUP_ICON resource in an .exe or .dll.
168 struct GRPICONDIR {
169 WORD idReserved;
170 WORD idType;
171 WORD idCount;
172 GRPICONDIRENTRY idEntries[1];
175 // Contains the actual icon image.
176 struct ICONIMAGE {
177 BITMAPINFOHEADER icHeader;
178 RGBQUAD icColors[1];
179 BYTE icXOR[1];
180 BYTE icAND[1];
182 #pragma pack(pop)
184 FRIEND_TEST_ALL_PREFIXES(IconUtilTest, TestCreateIconFileWithLargeBitmap);
186 // Used for indicating that the .ico contains an icon (rather than a cursor)
187 // image. This value is set in the |idType| field of the ICONDIR structure.
188 static const int kResourceTypeIcon = 1;
190 // The dimensions of the icon images we insert into the .ico file.
191 static const int icon_dimensions_[];
193 // Returns true if any pixel in the given pixels buffer has an non-zero alpha.
194 static bool PixelsHaveAlpha(const uint32* pixels, size_t num_pixels);
196 // A helper function that initializes a BITMAPV5HEADER structure with a set
197 // of values.
198 static void InitializeBitmapHeader(BITMAPV5HEADER* header, int width,
199 int height);
201 // Given a single SkBitmap object and pointers to the corresponding icon
202 // structures within the icon data buffer, this function sets the image
203 // information (dimensions, color depth, etc.) in the icon structures and
204 // also copies the underlying icon image into the appropriate location.
206 // The function will set the data pointed to by |image_byte_count| with the
207 // number of image bytes written to the buffer. Note that the number of bytes
208 // includes only the image data written into the memory pointed to by
209 // |icon_image|.
210 static void SetSingleIconImageInformation(const SkBitmap& bitmap,
211 size_t index,
212 ICONDIR* icon_dir,
213 ICONIMAGE* icon_image,
214 size_t image_offset,
215 size_t* image_byte_count);
217 // Copies the bits of an SkBitmap object into a buffer holding the bits of
218 // the corresponding image for an icon within the .ico file.
219 static void CopySkBitmapBitsIntoIconBuffer(const SkBitmap& bitmap,
220 unsigned char* buffer,
221 size_t buffer_size);
223 // Given a single bitmap, this function creates a set of bitmaps with
224 // specific dimensions by resizing the given bitmap to the appropriate sizes.
225 static void CreateResizedBitmapSet(const SkBitmap& bitmap_to_resize,
226 std::vector<SkBitmap>* bitmaps);
228 // Given a set of bitmaps with varying dimensions, this function computes
229 // the amount of memory needed in order to store the bitmaps as image icons
230 // in a .ico file.
231 static size_t ComputeIconFileBufferSize(const std::vector<SkBitmap>& set);
233 // A helper function for computing various size components of a given bitmap.
234 // The different sizes can be used within the various .ico file structures.
236 // |xor_mask_size| - the size, in bytes, of the XOR mask in the ICONIMAGE
237 // structure.
238 // |and_mask_size| - the size, in bytes, of the AND mask in the ICONIMAGE
239 // structure.
240 // |bytes_in_resource| - the total number of bytes set in the ICONIMAGE
241 // structure. This value is equal to the sum of the
242 // bytes in the AND mask and the XOR mask plus the size
243 // of the BITMAPINFOHEADER structure. Note that since
244 // only 32bpp are handled by the IconUtil class, the
245 // icColors field in the ICONIMAGE structure is ignored
246 // and is not accounted for when computing the
247 // different size components.
248 static void ComputeBitmapSizeComponents(const SkBitmap& bitmap,
249 size_t* xor_mask_size,
250 size_t* bytes_in_resource);
252 // A helper function of CreateSkBitmapFromHICON.
253 static SkBitmap CreateSkBitmapFromHICONHelper(HICON icon,
254 const gfx::Size& s);
256 // Prevent clients from instantiating objects of that class by declaring the
257 // ctor/dtor as private.
258 DISALLOW_IMPLICIT_CONSTRUCTORS(IconUtil);
261 #endif // UI_GFX_ICON_UTIL_H_