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_
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"
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():
38 // // Fill |bitmap| with valid data
39 // bitmap.setConfig(...);
40 // bitmap.allocPixels();
44 // // Convert the bitmap into a Windows HICON
45 // HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap);
46 // if (icon == NULL) {
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
{
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
,
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
,
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
131 // ICONDIRENTRY contains meta data for an individual icon image within a
133 struct ICONDIRENTRY
{
144 // ICONDIR Contains information about all the icon images contained within a
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
{
166 // GRPICONDIR Contains information about all the icon images contained within
167 // a RT_GROUP_ICON resource in an .exe or .dll.
172 GRPICONDIRENTRY idEntries
[1];
175 // Contains the actual icon image.
177 BITMAPINFOHEADER icHeader
;
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
198 static void InitializeBitmapHeader(BITMAPV5HEADER
* header
, int width
,
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
210 static void SetSingleIconImageInformation(const SkBitmap
& bitmap
,
213 ICONIMAGE
* icon_image
,
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
,
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
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
238 // |and_mask_size| - the size, in bytes, of the AND mask in the ICONIMAGE
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
,
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_