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