Mailbox support for texture layers.
[chromium-blink-merge.git] / ui / gfx / icon_util.h
blobedd49614f396c87da5e5e89e4f7f7258ef7256a5
1 // Copyright (c) 2011 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 "ui/base/ui_export.h"
14 #include "ui/gfx/point.h"
15 #include "ui/gfx/size.h"
17 namespace gfx {
18 class Size;
20 class FilePath;
21 class SkBitmap;
23 ///////////////////////////////////////////////////////////////////////////////
25 // The IconUtil class contains helper functions for manipulating Windows icons.
26 // The class interface contains methods for converting an HICON handle into an
27 // SkBitmap object and vice versa. The class can also create a .ico file given
28 // a PNG image contained in an SkBitmap object. The following code snippet
29 // shows an example usage of IconUtil::CreateHICONFromSkBitmap():
31 // SkBitmap bitmap;
33 // // Fill |bitmap| with valid data
34 // bitmap.setConfig(...);
35 // bitmap.allocPixels();
37 // ...
39 // // Convert the bitmap into a Windows HICON
40 // HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap);
41 // if (icon == NULL) {
42 // // Handle error
43 // ...
44 // }
46 // // Use the icon with a WM_SETICON message
47 // ::SendMessage(hwnd, WM_SETICON, static_cast<WPARAM>(ICON_BIG),
48 // reinterpret_cast<LPARAM>(icon));
50 // // Destroy the icon when we are done
51 // ::DestroyIcon(icon);
53 ///////////////////////////////////////////////////////////////////////////////
54 class UI_EXPORT IconUtil {
55 public:
56 // Given an SkBitmap object, the function converts the bitmap to a Windows
57 // icon and returns the corresponding HICON handle. If the function cannot
58 // convert the bitmap, NULL is returned.
60 // The client is responsible for destroying the icon when it is no longer
61 // needed by calling ::DestroyIcon().
62 static HICON CreateHICONFromSkBitmap(const SkBitmap& bitmap);
64 // Given a valid HICON handle representing an icon, this function converts
65 // the icon into an SkBitmap object containing an ARGB bitmap using the
66 // dimensions specified in |s|. |s| must specify valid dimensions (both
67 // width() an height() must be greater than zero). If the function cannot
68 // convert the icon to a bitmap (most probably due to an invalid parameter),
69 // the return value is NULL.
71 // The client owns the returned bitmap object and is responsible for deleting
72 // it when it is no longer needed.
73 static SkBitmap* CreateSkBitmapFromHICON(HICON icon, const gfx::Size& s);
75 // Given a valid HICON handle representing an icon, this function converts
76 // the icon into an SkBitmap object containing an ARGB bitmap using the
77 // dimensions of HICON. If the function cannot convert the icon to a bitmap
78 // (most probably due to an invalid parameter), the return value is NULL.
80 // The client owns the returned bitmap object and is responsible for deleting
81 // it when it is no longer needed.
82 static SkBitmap* CreateSkBitmapFromHICON(HICON icon);
84 // Given an initialized SkBitmap object and a file name, this function
85 // creates a .ico file with the given name using the provided bitmap. The
86 // icon file is created with multiple icon images of varying predefined
87 // dimensions because Windows uses different image sizes when loading icons,
88 // depending on where the icon is drawn (ALT+TAB window, desktop shortcut,
89 // Quick Launch, etc.). |icon_file_name| needs to specify the full path for
90 // the desired .ico file.
92 // The function returns true on success and false otherwise.
93 static bool CreateIconFileFromSkBitmap(const SkBitmap& bitmap,
94 const FilePath& icon_path);
96 // Creates a cursor of the specified size from the DIB passed in.
97 // Returns the cursor on success or NULL on failure.
98 static HICON CreateCursorFromDIB(const gfx::Size& icon_size,
99 const gfx::Point& hotspot,
100 const void* dib_bits,
101 size_t dib_size);
103 private:
104 // The icon format is published in the MSDN but there is no definition of
105 // the icon file structures in any of the Windows header files so we need to
106 // define these structure within the class. We must make sure we use 2 byte
107 // packing so that the structures are layed out properly within the file.
108 #pragma pack(push)
109 #pragma pack(2)
111 // ICONDIRENTRY contains meta data for an individual icon image within a
112 // .ico file.
113 struct ICONDIRENTRY {
114 BYTE bWidth;
115 BYTE bHeight;
116 BYTE bColorCount;
117 BYTE bReserved;
118 WORD wPlanes;
119 WORD wBitCount;
120 DWORD dwBytesInRes;
121 DWORD dwImageOffset;
124 // ICONDIR Contains information about all the icon images contained within a
125 // single .ico file.
126 struct ICONDIR {
127 WORD idReserved;
128 WORD idType;
129 WORD idCount;
130 ICONDIRENTRY idEntries[1];
133 // Contains the actual icon image.
134 struct ICONIMAGE {
135 BITMAPINFOHEADER icHeader;
136 RGBQUAD icColors[1];
137 BYTE icXOR[1];
138 BYTE icAND[1];
140 #pragma pack(pop)
142 // Used for indicating that the .ico contains an icon (rather than a cursor)
143 // image. This value is set in the |idType| field of the ICONDIR structure.
144 static const int kResourceTypeIcon = 1;
146 // The dimensions of the icon images we insert into the .ico file.
147 static const int icon_dimensions_[];
149 // Returns true if any pixel in the given pixels buffer has an non-zero alpha.
150 static bool PixelsHaveAlpha(const uint32* pixels, size_t num_pixels);
152 // A helper function that initializes a BITMAPV5HEADER structure with a set
153 // of values.
154 static void InitializeBitmapHeader(BITMAPV5HEADER* header, int width,
155 int height);
157 // Given a single SkBitmap object and pointers to the corresponding icon
158 // structures within the icon data buffer, this function sets the image
159 // information (dimensions, color depth, etc.) in the icon structures and
160 // also copies the underlying icon image into the appropriate location.
162 // The function will set the data pointed to by |image_byte_count| with the
163 // number of image bytes written to the buffer. Note that the number of bytes
164 // includes only the image data written into the memory pointed to by
165 // |icon_image|.
166 static void SetSingleIconImageInformation(const SkBitmap& bitmap,
167 size_t index,
168 ICONDIR* icon_dir,
169 ICONIMAGE* icon_image,
170 size_t image_offset,
171 size_t* image_byte_count);
173 // Copies the bits of an SkBitmap object into a buffer holding the bits of
174 // the corresponding image for an icon within the .ico file.
175 static void CopySkBitmapBitsIntoIconBuffer(const SkBitmap& bitmap,
176 unsigned char* buffer,
177 size_t buffer_size);
179 // Given a single bitmap, this function creates a set of bitmaps with
180 // specific dimensions by resizing the given bitmap to the appropriate sizes.
181 static void CreateResizedBitmapSet(const SkBitmap& bitmap_to_resize,
182 std::vector<SkBitmap>* bitmaps);
184 // Given a set of bitmaps with varying dimensions, this function computes
185 // the amount of memory needed in order to store the bitmaps as image icons
186 // in a .ico file.
187 static size_t ComputeIconFileBufferSize(const std::vector<SkBitmap>& set);
189 // A helper function for computing various size components of a given bitmap.
190 // The different sizes can be used within the various .ico file structures.
192 // |xor_mask_size| - the size, in bytes, of the XOR mask in the ICONIMAGE
193 // structure.
194 // |and_mask_size| - the size, in bytes, of the AND mask in the ICONIMAGE
195 // structure.
196 // |bytes_in_resource| - the total number of bytes set in the ICONIMAGE
197 // structure. This value is equal to the sum of the
198 // bytes in the AND mask and the XOR mask plus the size
199 // of the BITMAPINFOHEADER structure. Note that since
200 // only 32bpp are handled by the IconUtil class, the
201 // icColors field in the ICONIMAGE structure is ignored
202 // and is not accounted for when computing the
203 // different size components.
204 static void ComputeBitmapSizeComponents(const SkBitmap& bitmap,
205 size_t* xor_mask_size,
206 size_t* bytes_in_resource);
208 // A helper function of CreateSkBitmapFromHICON.
209 static SkBitmap CreateSkBitmapFromHICONHelper(HICON icon,
210 const gfx::Size& s);
212 // Prevent clients from instantiating objects of that class by declaring the
213 // ctor/dtor as private.
214 DISALLOW_IMPLICIT_CONSTRUCTORS(IconUtil);
217 #endif // UI_GFX_ICON_UTIL_H_