OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "ui/gfx/icon_util.h" | 5 #include "ui/gfx/icon_util.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/win/scoped_handle.h" | 10 #include "base/win/scoped_handle.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 48, // Alt+Tab icon size. | 48 48, // Alt+Tab icon size. |
49 64, // Recommended by the MSDN as a nice to have icon size. | 49 64, // Recommended by the MSDN as a nice to have icon size. |
50 96, // Recommended by the MSDN as a nice to have icon size. | 50 96, // Recommended by the MSDN as a nice to have icon size. |
51 128 // Used by the Shell (e.g. for shortcuts). | 51 128 // Used by the Shell (e.g. for shortcuts). |
52 }; | 52 }; |
53 | 53 |
54 HICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) { | 54 HICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) { |
55 // Only 32 bit ARGB bitmaps are supported. We also try to perform as many | 55 // Only 32 bit ARGB bitmaps are supported. We also try to perform as many |
56 // validations as we can on the bitmap. | 56 // validations as we can on the bitmap. |
57 SkAutoLockPixels bitmap_lock(bitmap); | 57 SkAutoLockPixels bitmap_lock(bitmap); |
58 if ((bitmap.getConfig() != SkBitmap::kARGB_8888_Config) || | 58 if ((bitmap.config() != SkBitmap::kARGB_8888_Config) || |
59 (bitmap.width() <= 0) || (bitmap.height() <= 0) || | 59 (bitmap.width() <= 0) || (bitmap.height() <= 0) || |
60 (bitmap.getPixels() == NULL)) | 60 (bitmap.getPixels() == NULL)) |
61 return NULL; | 61 return NULL; |
62 | 62 |
63 // We start by creating a DIB which we'll use later on in order to create | 63 // We start by creating a DIB which we'll use later on in order to create |
64 // the HICON. We use BITMAPV5HEADER since the bitmap we are about to convert | 64 // the HICON. We use BITMAPV5HEADER since the bitmap we are about to convert |
65 // may contain an alpha channel and the V5 header allows us to specify the | 65 // may contain an alpha channel and the V5 header allows us to specify the |
66 // alpha mask for the DIB. | 66 // alpha mask for the DIB. |
67 BITMAPV5HEADER bitmap_header; | 67 BITMAPV5HEADER bitmap_header; |
68 InitializeBitmapHeader(&bitmap_header, bitmap.width(), bitmap.height()); | 68 InitializeBitmapHeader(&bitmap_header, bitmap.width(), bitmap.height()); |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 ::DeleteDC(dib_dc); | 228 ::DeleteDC(dib_dc); |
229 | 229 |
230 return bitmap; | 230 return bitmap; |
231 } | 231 } |
232 | 232 |
233 bool IconUtil::CreateIconFileFromSkBitmap(const SkBitmap& bitmap, | 233 bool IconUtil::CreateIconFileFromSkBitmap(const SkBitmap& bitmap, |
234 const FilePath& icon_path) { | 234 const FilePath& icon_path) { |
235 // Only 32 bit ARGB bitmaps are supported. We also make sure the bitmap has | 235 // Only 32 bit ARGB bitmaps are supported. We also make sure the bitmap has |
236 // been properly initialized. | 236 // been properly initialized. |
237 SkAutoLockPixels bitmap_lock(bitmap); | 237 SkAutoLockPixels bitmap_lock(bitmap); |
238 if ((bitmap.getConfig() != SkBitmap::kARGB_8888_Config) || | 238 if ((bitmap.config() != SkBitmap::kARGB_8888_Config) || |
239 (bitmap.height() <= 0) || (bitmap.width() <= 0) || | 239 (bitmap.height() <= 0) || (bitmap.width() <= 0) || |
240 (bitmap.getPixels() == NULL)) | 240 (bitmap.getPixels() == NULL)) |
241 return false; | 241 return false; |
242 | 242 |
243 // We start by creating the file. | 243 // We start by creating the file. |
244 base::win::ScopedHandle icon_file(::CreateFile(icon_path.value().c_str(), | 244 base::win::ScopedHandle icon_file(::CreateFile(icon_path.value().c_str(), |
245 GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)); | 245 GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)); |
246 | 246 |
247 if (!icon_file.IsValid()) | 247 if (!icon_file.IsValid()) |
248 return false; | 248 return false; |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 // Once we compute the size for a singe AND mask scan line, we multiply that | 492 // Once we compute the size for a singe AND mask scan line, we multiply that |
493 // number by the image height in order to get the total number of bytes for | 493 // number by the image height in order to get the total number of bytes for |
494 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes | 494 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes |
495 // for the monochrome bitmap representing the AND mask. | 495 // for the monochrome bitmap representing the AND mask. |
496 size_t and_line_length = (bitmap.width() + 7) >> 3; | 496 size_t and_line_length = (bitmap.width() + 7) >> 3; |
497 and_line_length = (and_line_length + 3) & ~3; | 497 and_line_length = (and_line_length + 3) & ~3; |
498 size_t and_mask_size = and_line_length * bitmap.height(); | 498 size_t and_mask_size = and_line_length * bitmap.height(); |
499 size_t masks_size = *xor_mask_size + and_mask_size; | 499 size_t masks_size = *xor_mask_size + and_mask_size; |
500 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); | 500 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); |
501 } | 501 } |
OLD | NEW |