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/gfx/gfx_export.h"
16 #include "ui/gfx/point.h"
17 #include "ui/gfx/size.h"
29 ///////////////////////////////////////////////////////////////////////////////
31 // The IconUtil class contains helper functions for manipulating Windows icons.
32 // The class interface contains methods for converting an HICON handle into an
33 // SkBitmap object and vice versa. The class can also create a .ico file given
34 // a PNG image contained in an SkBitmap object. The following code snippet
35 // shows an example usage of IconUtil::CreateHICONFromSkBitmap():
39 // // Fill |bitmap| with valid data
40 // bitmap.setConfig(...);
41 // bitmap.allocPixels();
45 // // Convert the bitmap into a Windows HICON
46 // HICON icon = IconUtil::CreateHICONFromSkBitmap(bitmap);
47 // if (icon == NULL) {
52 // // Use the icon with a WM_SETICON message
53 // ::SendMessage(hwnd, WM_SETICON, static_cast<WPARAM>(ICON_BIG),
54 // reinterpret_cast<LPARAM>(icon));
56 // // Destroy the icon when we are done
57 // ::DestroyIcon(icon);
59 ///////////////////////////////////////////////////////////////////////////////
60 class GFX_EXPORT IconUtil
{
62 // The size of the large icon entries in .ico files on Windows Vista+.
63 static const int kLargeIconSize
= 256;
64 // The size of icons in the medium icons view on Windows Vista+. This is the
65 // maximum size Windows will display an icon that does not have a 256x256
66 // image, even at the large or extra large icons views.
67 static const int kMediumIconSize
= 48;
69 // The dimensions for icon images in Windows icon files. All sizes are square;
70 // that is, the value 48 means a 48x48 pixel image. Sizes are listed in
72 static const int kIconDimensions
[];
74 // The number of elements in kIconDimensions.
75 static const size_t kNumIconDimensions
;
76 // The number of elements in kIconDimensions <= kMediumIconSize.
77 static const size_t kNumIconDimensionsUpToMediumSize
;
79 // Given an SkBitmap object, the function converts the bitmap to a Windows
80 // icon and returns the corresponding HICON handle. If the function cannot
81 // convert the bitmap, NULL is returned.
83 // The client is responsible for destroying the icon when it is no longer
84 // needed by calling ::DestroyIcon().
85 static HICON
CreateHICONFromSkBitmap(const SkBitmap
& bitmap
);
87 // Given a valid HICON handle representing an icon, this function converts
88 // the icon into an SkBitmap object containing an ARGB bitmap using the
89 // dimensions specified in |s|. |s| must specify valid dimensions (both
90 // width() an height() must be greater than zero). If the function cannot
91 // convert the icon to a bitmap (most probably due to an invalid parameter),
92 // the return value is NULL.
94 // The client owns the returned bitmap object and is responsible for deleting
95 // it when it is no longer needed.
96 static SkBitmap
* CreateSkBitmapFromHICON(HICON icon
, const gfx::Size
& s
);
98 // Loads an icon resource as a SkBitmap for the specified |size| from a
99 // loaded .dll or .exe |module|. Supports loading smaller icon sizes as well
100 // as the Vista+ 256x256 PNG icon size. If the icon could not be loaded or
101 // found, returns a NULL scoped_ptr.
102 static scoped_ptr
<SkBitmap
> CreateSkBitmapFromIconResource(HMODULE module
,
106 // Given a valid HICON handle representing an icon, this function converts
107 // the icon into an SkBitmap object containing an ARGB bitmap using the
108 // dimensions of HICON. If the function cannot convert the icon to a bitmap
109 // (most probably due to an invalid parameter), the return value is NULL.
111 // The client owns the returned bitmap object and is responsible for deleting
112 // it when it is no longer needed.
113 static SkBitmap
* CreateSkBitmapFromHICON(HICON icon
);
115 // Creates Windows .ico file at |icon_path|. The icon file is created with
116 // multiple BMP representations at varying predefined dimensions (by resizing
117 // an appropriately sized image from |image_family|) because Windows uses
118 // different image sizes when loading icons, depending on where the icon is
119 // drawn (ALT+TAB window, desktop shortcut, Quick Launch, etc.).
121 // If |image_family| contains an image larger than 48x48, the resulting icon
122 // will contain all sizes up to 256x256. The 256x256 image will be stored in
123 // PNG format inside the .ico file. If not, the resulting icon will contain
124 // all sizes up to 48x48.
126 // The function returns true on success and false otherwise. Returns false if
127 // |image_family| is empty.
128 static bool CreateIconFileFromImageFamily(
129 const gfx::ImageFamily
& image_family
,
130 const base::FilePath
& icon_path
);
132 // Creates a cursor of the specified size from the DIB passed in.
133 // Returns the cursor on success or NULL on failure.
134 static HICON
CreateCursorFromDIB(const gfx::Size
& icon_size
,
135 const gfx::Point
& hotspot
,
136 const void* dib_bits
,
140 // The icon format is published in the MSDN but there is no definition of
141 // the icon file structures in any of the Windows header files so we need to
142 // define these structure within the class. We must make sure we use 2 byte
143 // packing so that the structures are layed out properly within the file.
144 // See: http://msdn.microsoft.com/en-us/library/ms997538.aspx
148 // ICONDIRENTRY contains meta data for an individual icon image within a
150 struct ICONDIRENTRY
{
161 // ICONDIR Contains information about all the icon images contained within a
167 ICONDIRENTRY idEntries
[1];
170 // GRPICONDIRENTRY contains meta data for an individual icon image within a
171 // RT_GROUP_ICON resource in an .exe or .dll.
172 struct GRPICONDIRENTRY
{
183 // GRPICONDIR Contains information about all the icon images contained within
184 // a RT_GROUP_ICON resource in an .exe or .dll.
189 GRPICONDIRENTRY idEntries
[1];
192 // Contains the actual icon image.
194 BITMAPINFOHEADER icHeader
;
201 friend class IconUtilTest
;
203 // Used for indicating that the .ico contains an icon (rather than a cursor)
204 // image. This value is set in the |idType| field of the ICONDIR structure.
205 static const int kResourceTypeIcon
= 1;
207 // Returns true if any pixel in the given pixels buffer has an non-zero alpha.
208 static bool PixelsHaveAlpha(const uint32
* pixels
, size_t num_pixels
);
210 // A helper function that initializes a BITMAPV5HEADER structure with a set
212 static void InitializeBitmapHeader(BITMAPV5HEADER
* header
, int width
,
215 // Given a single SkBitmap object and pointers to the corresponding icon
216 // structures within the icon data buffer, this function sets the image
217 // information (dimensions, color depth, etc.) in the icon structures and
218 // also copies the underlying icon image into the appropriate location.
219 // The width and height of |bitmap| must be < 256.
220 // (Note that the 256x256 icon is treated specially, as a PNG, and should not
223 // The function will set the data pointed to by |image_byte_count| with the
224 // number of image bytes written to the buffer. Note that the number of bytes
225 // includes only the image data written into the memory pointed to by
227 static void SetSingleIconImageInformation(const SkBitmap
& bitmap
,
230 ICONIMAGE
* icon_image
,
232 size_t* image_byte_count
);
234 // Copies the bits of an SkBitmap object into a buffer holding the bits of
235 // the corresponding image for an icon within the .ico file.
236 static void CopySkBitmapBitsIntoIconBuffer(const SkBitmap
& bitmap
,
237 unsigned char* buffer
,
240 // Given a set of bitmaps with varying dimensions, this function computes
241 // the amount of memory needed in order to store the bitmaps as image icons
243 static size_t ComputeIconFileBufferSize(const std::vector
<SkBitmap
>& set
);
245 // A helper function for computing various size components of a given bitmap.
246 // The different sizes can be used within the various .ico file structures.
248 // |xor_mask_size| - the size, in bytes, of the XOR mask in the ICONIMAGE
250 // |and_mask_size| - the size, in bytes, of the AND mask in the ICONIMAGE
252 // |bytes_in_resource| - the total number of bytes set in the ICONIMAGE
253 // structure. This value is equal to the sum of the
254 // bytes in the AND mask and the XOR mask plus the size
255 // of the BITMAPINFOHEADER structure. Note that since
256 // only 32bpp are handled by the IconUtil class, the
257 // icColors field in the ICONIMAGE structure is ignored
258 // and is not accounted for when computing the
259 // different size components.
260 static void ComputeBitmapSizeComponents(const SkBitmap
& bitmap
,
261 size_t* xor_mask_size
,
262 size_t* bytes_in_resource
);
264 // A helper function of CreateSkBitmapFromHICON.
265 static SkBitmap
CreateSkBitmapFromHICONHelper(HICON icon
,
268 // Prevent clients from instantiating objects of that class by declaring the
269 // ctor/dtor as private.
270 DISALLOW_IMPLICIT_CONSTRUCTORS(IconUtil
);
273 #endif // UI_GFX_ICON_UTIL_H_