icon_util.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/icon_util.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/files/file_util.h" 82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/important_file_writer.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h" 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/resource_util.h" 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/win/scoped_gdi_object.h" 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/win/scoped_handle.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/win/scoped_hdc.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "skia/ext/image_operations.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkBitmap.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/gdi_util.h" 187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/gfx/image/image.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/image/image_family.h" 20c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include "ui/gfx/size.h" 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)struct ScopedICONINFO : ICONINFO { 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ScopedICONINFO() { 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) hbmColor = NULL; 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) hbmMask = NULL; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ~ScopedICONINFO() { 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (hbmColor) 3258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) ::DeleteObject(hbmColor); 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (hbmMask) 34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ::DeleteObject(hbmMask); 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Creates a new ImageFamily, |resized_image_family|, based on the images in 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |image_family|, but containing images of specific dimensions desirable for 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Windows icons. For each desired image dimension, it chooses the most 412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// appropriate image for that size, and resizes it to the desired size. 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Returns true on success, false on failure. Failure can occur if 43b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// |image_family| is empty, all images in the family have size 0x0, or an image 44b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// has no allocated pixel data. 45b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// |resized_image_family| must be empty. 46b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool BuildResizedImageFamily(const gfx::ImageFamily& image_family, 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) gfx::ImageFamily* resized_image_family) { 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(resized_image_family); 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(resized_image_family->empty()); 50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < IconUtil::kNumIconDimensions; ++i) { 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int dimension = IconUtil::kIconDimensions[i]; 530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) gfx::Size size(dimension, dimension); 540f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const gfx::Image* best = image_family.GetBest(size); 550f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (!best || best->IsEmpty()) { 56a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Either |image_family| is empty, or all images have size 0x0. 57a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) return false; 58a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) } 59a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Optimize for the "Large icons" view in Windows Vista+. This view displays 61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // icons at full size if only if there is a 256x256 (kLargeIconSize) image 620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // in the .ico file. Otherwise, it shrinks icons to 48x48 (kMediumIconSize). 630f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (dimension > IconUtil::kMediumIconSize && 640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) best->Width() <= IconUtil::kMediumIconSize && 650529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch best->Height() <= IconUtil::kMediumIconSize) { 660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // There is no source icon larger than 48x48, so do not create any 676d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) // images larger than 48x48. kIconDimensions is sorted in ascending 68e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // order, so it is safe to break here. 69e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch break; 70f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } 71f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 72f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) if (best->Size() == size) { 73f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) resized_image_family->Add(*best); 740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) } else { 750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // There is no |dimension|x|dimension| source image. 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Resize this one to the desired size, and insert it. 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SkBitmap best_bitmap = best->AsBitmap(); 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Only kARGB_8888 images are supported. 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // This will also filter out images with no pixels. 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (best_bitmap.colorType() != kN32_SkColorType) 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return false; 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SkBitmap resized_bitmap = skia::ImageOperations::Resize( 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) best_bitmap, skia::ImageOperations::RESIZE_LANCZOS3, 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) dimension, dimension); 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) resized_image_family->Add(gfx::Image::CreateFrom1xBitmap(resized_bitmap)); 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return true; 892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Creates a set of bitmaps from an image family. 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// All images smaller than 256x256 are converted to SkBitmaps, and inserted into 932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// |bitmaps| in order of aspect ratio (thinnest to widest), and then ascending 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// size order. If an image of exactly 256x256 is specified, it is converted into 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PNG format and stored in |png_bytes|. Images with width or height larger than 96e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// 256 are ignored. 97b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// |bitmaps| must be an empty vector, and not NULL. 98e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Returns true on success, false on failure. This fails if any image in 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// |image_family| is not a 32-bit ARGB image, or is otherwise invalid. 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool ConvertImageFamilyToBitmaps( 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const gfx::ImageFamily& image_family, 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<SkBitmap>* bitmaps, 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) scoped_refptr<base::RefCountedMemory>* png_bytes) { 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(bitmaps != NULL); 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(bitmaps->empty()); 10690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 10790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) for (gfx::ImageFamily::const_iterator it = image_family.begin(); 108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) it != image_family.end(); ++it) { 10990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const gfx::Image& image = *it; 110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // All images should have one of the kIconDimensions sizes. 1120529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch DCHECK_GT(image.Width(), 0); 1130529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch DCHECK_LE(image.Width(), IconUtil::kLargeIconSize); 1140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch DCHECK_GT(image.Height(), 0); 1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) DCHECK_LE(image.Height(), IconUtil::kLargeIconSize); 1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) SkBitmap bitmap = image.AsBitmap(); 1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Only 32 bit ARGB bitmaps are supported. We also make sure the bitmap has 1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // been properly initialized. 1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) SkAutoLockPixels bitmap_lock(bitmap); 1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if ((bitmap.colorType() != kN32_SkColorType) || 1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) (bitmap.getPixels() == NULL)) { 1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) return false; 1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } 1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // Special case: Icons exactly 256x256 are stored in PNG format. 1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) if (image.Width() == IconUtil::kLargeIconSize && 1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) image.Height() == IconUtil::kLargeIconSize) { 1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) *png_bytes = image.As1xPNGBytes(); 1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) } else { 13268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles) bitmaps->push_back(bitmap); 133b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) } 13423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) } 13523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 13623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) return true; 13723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} 13823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 13923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)} // namespace 14023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// The icon images appear in the icon file in same order in which their 142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// corresponding dimensions appear in this array, so it is important to keep 143a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// this array sorted. Also note that the maximum icon image size we can handle 144a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// is 256 by 256. See: 145a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// http://msdn.microsoft.com/en-us/library/windows/desktop/aa511280.aspx#size 146a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)const int IconUtil::kIconDimensions[] = { 147a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 8, // Recommended by the MSDN as a nice to have icon size. 148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 10, // Used by the Shell (e.g. for shortcuts). 149b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 14, // Recommended by the MSDN as a nice to have icon size. 150b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 16, // Toolbar, Application and Shell icon sizes. 151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 22, // Recommended by the MSDN as a nice to have icon size. 152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 24, // Used by the Shell (e.g. for shortcuts). 153a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 32, // Toolbar, Dialog and Wizard icon size. 154a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 40, // Quick Launch. 155a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 48, // Alt+Tab icon size. 156a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 64, // Recommended by the MSDN as a nice to have icon size. 157a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles) 96, // Recommended by the MSDN as a nice to have icon size. 158f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 128, // Used by the Shell (e.g. for shortcuts). 159f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 256 // Used by Vista onwards for large icons. 160f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}; 161f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) 162f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)const size_t IconUtil::kNumIconDimensions = arraysize(kIconDimensions); 163f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)const size_t IconUtil::kNumIconDimensionsUpToMediumSize = 9; 1646d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) 165f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)HICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) { 166f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) // Only 32 bit ARGB bitmaps are supported. We also try to perform as many 167f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) // validations as we can on the bitmap. 168f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) SkAutoLockPixels bitmap_lock(bitmap); 169f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles) if ((bitmap.colorType() != kN32_SkColorType) || 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (bitmap.width() <= 0) || (bitmap.height() <= 0) || 1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) (bitmap.getPixels() == NULL)) 1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return NULL; 1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // We start by creating a DIB which we'll use later on in order to create 1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the HICON. We use BITMAPV5HEADER since the bitmap we are about to convert 1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // may contain an alpha channel and the V5 header allows us to specify the 1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // alpha mask for the DIB. 1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) BITMAPV5HEADER bitmap_header; 179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) InitializeBitmapHeader(&bitmap_header, bitmap.width(), bitmap.height()); 1800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void* bits = NULL; 1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) HBITMAP dib; 1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 18458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) { 18558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::win::ScopedGetDC hdc(NULL); 18658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) dib = ::CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO*>(&bitmap_header), 1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DIB_RGB_COLORS, &bits, NULL, 0); 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!dib || !bits) 190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return NULL; 191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) memcpy(bits, bitmap.getPixels(), bitmap.width() * bitmap.height() * 4); 193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Icons are generally created using an AND and XOR masks where the AND 195868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // specifies boolean transparency (the pixel is either opaque or 196868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // transparent) and the XOR mask contains the actual image pixels. If the XOR 197868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // mask bitmap has an alpha channel, the AND monochrome bitmap won't 198868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // actually be used for computing the pixel transparency. Even though all our 199868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // bitmap has an alpha channel, Windows might not agree when all alpha values 200868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // are zero. So the monochrome bitmap is created with all pixels transparent 201868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // for this case. Otherwise, it is created with all pixels opaque. 202868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bool bitmap_has_alpha_channel = PixelsHaveAlpha( 203868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) static_cast<const uint32*>(bitmap.getPixels()), 204868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bitmap.width() * bitmap.height()); 205868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 206868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) scoped_ptr<uint8[]> mask_bits; 207868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (!bitmap_has_alpha_channel) { 208868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Bytes per line with paddings to make it word alignment. 2090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) size_t bytes_per_line = (bitmap.width() + 0xF) / 16 * 2; 2100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) size_t mask_bits_size = bytes_per_line * bitmap.height(); 2110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) mask_bits.reset(new uint8[mask_bits_size]); 2130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DCHECK(mask_bits.get()); 2140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 2150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Make all pixels transparent. 2160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) memset(mask_bits.get(), 0xFF, mask_bits_size); 2177dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) HBITMAP mono_bitmap = ::CreateBitmap(bitmap.width(), bitmap.height(), 1, 1, 2207dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch reinterpret_cast<LPVOID>(mask_bits.get())); 2217dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch DCHECK(mono_bitmap); 2227dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 2237dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch ICONINFO icon_info; 2247dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch icon_info.fIcon = TRUE; 2257dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch icon_info.xHotspot = 0; 2267dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch icon_info.yHotspot = 0; 227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) icon_info.hbmMask = mono_bitmap; 2287dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch icon_info.hbmColor = dib; 2297dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch HICON icon = ::CreateIconIndirect(&icon_info); 2307dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch ::DeleteObject(dib); 2317dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch ::DeleteObject(mono_bitmap); 232a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) return icon; 233a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 234a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 235a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)SkBitmap* IconUtil::CreateSkBitmapFromHICON(HICON icon, const gfx::Size& s) { 2367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // We start with validating parameters. 2377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!icon || s.IsEmpty()) 2387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch return NULL; 2397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch ScopedICONINFO icon_info; 2407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch if (!::GetIconInfo(icon, &icon_info)) 2417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch return NULL; 2420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) if (!icon_info.fIcon) 2430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return NULL; 2440f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) return new SkBitmap(CreateSkBitmapFromHICONHelper(icon, s)); 2450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} 2467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 2477dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochscoped_ptr<SkBitmap> IconUtil::CreateSkBitmapFromIconResource(HMODULE module, 2487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch int resource_id, 2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) int size) { 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DCHECK_LE(size, kLargeIconSize); 2517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // For everything except the Vista+ 256x256 icons, use |LoadImage()|. 2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (size != kLargeIconSize) { 2547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch HICON icon_handle = 2557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch static_cast<HICON>(LoadImage(module, MAKEINTRESOURCE(resource_id), 256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) IMAGE_ICON, size, size, 2577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch LR_DEFAULTCOLOR | LR_DEFAULTSIZE)); 2587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch scoped_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon_handle)); 2597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch DestroyIcon(icon_handle); 2607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch return bitmap.Pass(); 2617dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 2627dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch 2637dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // For Vista+ 256x256 PNG icons, read the resource directly and find 2647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch // the corresponding icon entry to get its PNG bytes. 2657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch void* icon_dir_data = NULL; 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t icon_dir_size = 0; 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!base::win::GetResourceFromModule(module, resource_id, RT_GROUP_ICON, 268558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch &icon_dir_data, &icon_dir_size)) { 269558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return scoped_ptr<SkBitmap>(); 270558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch } 271ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch DCHECK(icon_dir_data); 272ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch DCHECK_GE(icon_dir_size, sizeof(GRPICONDIR)); 273ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch 274558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const GRPICONDIR* icon_dir = 275558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch reinterpret_cast<const GRPICONDIR*>(icon_dir_data); 276558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const GRPICONDIRENTRY* large_icon_entry = NULL; 277558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch for (size_t i = 0; i < icon_dir->idCount; ++i) { 278a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const GRPICONDIRENTRY* entry = &icon_dir->idEntries[i]; 279e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch // 256x256 icons are stored with width and height set to 0. 2800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // See: http://en.wikipedia.org/wiki/ICO_(file_format) 281010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) if (entry->bWidth == 0 && entry->bHeight == 0) { 2826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) large_icon_entry = entry; 2836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) break; 284558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch } 285558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch } 286558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch if (!large_icon_entry) 287558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return scoped_ptr<SkBitmap>(); 288558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 289558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch void* png_data = NULL; 290f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) size_t png_size = 0; 291f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) if (!base::win::GetResourceFromModule(module, large_icon_entry->nID, RT_ICON, 292f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) &png_data, &png_size)) { 2936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) return scoped_ptr<SkBitmap>(); 2946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) } 295cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) DCHECK(png_data); 2960529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch DCHECK_EQ(png_size, large_icon_entry->dwBytesInRes); 297e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch 2985c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu gfx::Image image = gfx::Image::CreateFrom1xPNGBytes( 299558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch new base::RefCountedStaticMemory(png_data, png_size)); 300558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return scoped_ptr<SkBitmap>(new SkBitmap(image.AsBitmap())); 301558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch} 3021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 3031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)SkBitmap* IconUtil::CreateSkBitmapFromHICON(HICON icon) { 304c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // We start with validating parameters. 305558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch if (!icon) 306558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return NULL; 307558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 308558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch ScopedICONINFO icon_info; 309558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch BITMAP bitmap_info = { 0 }; 310558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 311558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch if (!::GetIconInfo(icon, &icon_info)) 312558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return NULL; 313558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 314558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch if (!::GetObject(icon_info.hbmMask, sizeof(bitmap_info), &bitmap_info)) 315558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return NULL; 316558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 317558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch gfx::Size icon_size(bitmap_info.bmWidth, bitmap_info.bmHeight); 318558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch return new SkBitmap(CreateSkBitmapFromHICONHelper(icon, icon_size)); 319558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch} 3201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 3211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)HICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size, 3221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const gfx::Point& hotspot, 323558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const void* dib_bits, 324558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch size_t dib_size) { 325558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch BITMAPINFO icon_bitmap_info = {0}; 326558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch gfx::CreateBitmapHeader( 327558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch icon_size.width(), 328558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch icon_size.height(), 329558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch reinterpret_cast<BITMAPINFOHEADER*>(&icon_bitmap_info)); 330558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 331558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch base::win::ScopedGetDC dc(NULL); 332558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch base::win::ScopedCreateDC working_dc(CreateCompatibleDC(dc)); 333558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch base::win::ScopedGDIObject<HBITMAP> bitmap_handle( 334558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch CreateDIBSection(dc, 33546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) &icon_bitmap_info, 33646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) DIB_RGB_COLORS, 33746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 0, 338868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 0, 3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 0)); 340868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (dib_size > 0) { 341868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SetDIBits(0, 342a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) bitmap_handle, 343868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 0, 344868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) icon_size.height(), 345868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) dib_bits, 346868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) &icon_bitmap_info, 347868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DIB_RGB_COLORS); 348868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 349a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 350868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) HBITMAP old_bitmap = reinterpret_cast<HBITMAP>( 351868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SelectObject(working_dc.Get(), bitmap_handle)); 352868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SetBkMode(working_dc.Get(), TRANSPARENT); 353868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SelectObject(working_dc.Get(), old_bitmap); 354a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 355868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) base::win::ScopedGDIObject<HBITMAP> mask( 356868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) CreateBitmap(icon_size.width(), 357868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) icon_size.height(), 358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 1, 359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 1, 3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) NULL)); 361d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) ICONINFO ii = {0}; 362d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) ii.fIcon = FALSE; 3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ii.xHotspot = hotspot.x(); 3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ii.yHotspot = hotspot.y(); 3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ii.hbmMask = mask; 3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ii.hbmColor = bitmap_handle; 367c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 368c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) return CreateIconIndirect(&ii); 369c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)} 370c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 371c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)SkBitmap IconUtil::CreateSkBitmapFromHICONHelper(HICON icon, 372424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const gfx::Size& s) { 373424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DCHECK(icon); 3740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch DCHECK(!s.IsEmpty()); 3750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 376c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // Allocating memory for the SkBitmap object. We are going to create an ARGB 377c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // bitmap so we should set the configuration appropriately. 378c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) SkBitmap bitmap; 379c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) bitmap.allocN32Pixels(s.width(), s.height()); 3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bitmap.eraseARGB(0, 0, 0, 0); 3810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch SkAutoLockPixels bitmap_lock(bitmap); 3820529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 3830529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Now we should create a DIB so that we can use ::DrawIconEx in order to 3840529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // obtain the icon's image. 3850529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch BITMAPV5HEADER h; 3860529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch InitializeBitmapHeader(&h, s.width(), s.height()); 3870529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch HDC hdc = ::GetDC(NULL); 3883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) uint32* bits; 3893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) HBITMAP dib = ::CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO*>(&h), 3903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) DIB_RGB_COLORS, reinterpret_cast<void**>(&bits), NULL, 0); 391c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(dib); 392c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) HDC dib_dc = CreateCompatibleDC(hdc); 3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ::ReleaseDC(NULL, hdc); 394c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(dib_dc); 3953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) HGDIOBJ old_obj = ::SelectObject(dib_dc, dib); 396c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 3973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Windows icons are defined using two different masks. The XOR mask, which 3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // represents the icon image and an AND mask which is a monochrome bitmap 3993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // which indicates the transparency of each pixel. 4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 4013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // To make things more complex, the icon image itself can be an ARGB bitmap 4023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // and therefore contain an alpha channel which specifies the transparency 4033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // for each pixel. Unfortunately, there is no easy way to determine whether 4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // or not a bitmap has an alpha channel and therefore constructing the bitmap 4053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // for the icon is nothing but straightforward. 4063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // 4073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // The idea is to read the AND mask but use it only if we know for sure that 4085c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // the icon image does not have an alpha channel. The only way to tell if the 4095c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // bitmap has an alpha channel is by looking through the pixels and checking 4103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // whether there are non-zero alpha bytes. 4113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // 4123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // We start by drawing the AND mask into our DIB. 4133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) size_t num_pixels = s.GetArea(); 4143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) memset(bits, 0, num_pixels * 4); 4153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ::DrawIconEx(dib_dc, 0, 0, icon, s.width(), s.height(), 0, NULL, DI_MASK); 4165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 4175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Capture boolean opacity. We may not use it if we find out the bitmap has 4185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // an alpha channel. 4195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) scoped_ptr<bool[]> opaque(new bool[num_pixels]); 420a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) for (size_t i = 0; i < num_pixels; ++i) 421a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) opaque[i] = !bits[i]; 4223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 4233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Then draw the image itself which is really the XOR mask. 4243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) memset(bits, 0, num_pixels * 4); 4253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ::DrawIconEx(dib_dc, 0, 0, icon, s.width(), s.height(), 0, NULL, DI_NORMAL); 4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) memcpy(bitmap.getPixels(), static_cast<void*>(bits), num_pixels * 4); 4273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 42890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Finding out whether the bitmap has an alpha channel. 4293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) bool bitmap_has_alpha_channel = PixelsHaveAlpha( 4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static_cast<const uint32*>(bitmap.getPixels()), num_pixels); 4313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If the bitmap does not have an alpha channel, we need to build it using 4333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // the previously captured AND mask. Otherwise, we are done. 4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (!bitmap_has_alpha_channel) { 4353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) uint32* p = static_cast<uint32*>(bitmap.getPixels()); 4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (size_t i = 0; i < num_pixels; ++p, ++i) { 4373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) DCHECK_EQ((*p & 0xff000000), 0u); 4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) if (opaque[i]) 4393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) *p |= 0xff000000; 4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) else 4413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) *p &= 0x00ffffff; 442d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 443d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) } 4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ::SelectObject(dib_dc, old_obj); 4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ::DeleteObject(dib); 4473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ::DeleteDC(dib_dc); 448b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 4493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) return bitmap; 450a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} 451a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 452a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// static 453b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)bool IconUtil::CreateIconFileFromImageFamily( 4543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) const gfx::ImageFamily& image_family, 45590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const base::FilePath& icon_path) { 4563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // Creating a set of bitmaps corresponding to the icon images we'll end up 45790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // storing in the icon file. Each bitmap is created by resizing the most 4583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // appropriate image from |image_family| to the desired size. 4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::ImageFamily resized_image_family; 4603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (!BuildResizedImageFamily(image_family, &resized_image_family)) 46190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return false; 4623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 46358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) std::vector<SkBitmap> bitmaps; 4643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) scoped_refptr<base::RefCountedMemory> png_bytes; 46558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) if (!ConvertImageFamilyToBitmaps(resized_image_family, &bitmaps, &png_bytes)) 46658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) return false; 467d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 468d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // Guaranteed true because BuildResizedImageFamily will provide at least one 4695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // image < 256x256. 4705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(!bitmaps.empty()); 47146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) size_t bitmap_count = bitmaps.size(); // Not including PNG image. 47246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Including PNG image, if any. 47346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) size_t image_count = bitmap_count + (png_bytes.get() ? 1 : 0); 47446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) 47546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // Computing the total size of the buffer we need in order to store the 47646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) // images in the desired icon format. 477cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) size_t buffer_size = ComputeIconFileBufferSize(bitmaps); 478cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Account for the bytes needed for the PNG entry. 4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (png_bytes.get()) 4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) buffer_size += sizeof(ICONDIRENTRY) + png_bytes->size(); 4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 48290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Setting the information in the structures residing within the buffer. 4833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // First, we set the information which doesn't require iterating through the 4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // bitmap set and then we set the bitmap specific structures. In the latter 4853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) // step we also copy the actual bits. 4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::vector<uint8> buffer(buffer_size); 4873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ICONDIR* icon_dir = reinterpret_cast<ICONDIR*>(&buffer[0]); 4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_dir->idType = kResourceTypeIcon; 4893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) icon_dir->idCount = static_cast<WORD>(image_count); 4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // - 1 because there is already one ICONDIRENTRY in ICONDIR. 4913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) size_t icon_dir_count = image_count - 1; 4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 4933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) size_t offset = sizeof(ICONDIR) + (sizeof(ICONDIRENTRY) * icon_dir_count); 4942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) for (size_t i = 0; i < bitmap_count; i++) { 4953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) ICONIMAGE* image = reinterpret_cast<ICONIMAGE*>(&buffer[offset]); 4964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) DCHECK_LT(offset, buffer_size); 4974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) size_t icon_image_size = 0; 4982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) SetSingleIconImageInformation(bitmaps[i], i, icon_dir, image, offset, 4993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) &icon_image_size); 5002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK_GT(icon_image_size, 0U); 5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) offset += icon_image_size; 5023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) } 5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Add the PNG entry, if necessary. 5053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) if (png_bytes.get()) { 506c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ICONDIRENTRY* entry = &icon_dir->idEntries[bitmap_count]; 5073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) entry->bWidth = 0; 508c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) entry->bHeight = 0; 5093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) entry->wPlanes = 1; 510c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) entry->wBitCount = 32; 511c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) entry->dwBytesInRes = static_cast<DWORD>(png_bytes->size()); 5123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) entry->dwImageOffset = static_cast<DWORD>(offset); 513c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) memcpy(&buffer[offset], png_bytes->front(), png_bytes->size()); 5143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) offset += png_bytes->size(); 515c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) } 5163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 517c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK_EQ(offset, buffer_size); 5183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles) 519b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) std::string data(buffer.begin(), buffer.end()); 520b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) return base::ImportantFileWriter::WriteFileAtomically(icon_path, data); 5213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)} 522b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles) 5233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool IconUtil::PixelsHaveAlpha(const uint32* pixels, size_t num_pixels) { 5244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) for (const uint32* end = pixels + num_pixels; pixels != end; ++pixels) { 5254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) if ((*pixels & 0xff000000) != 0) 526f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return true; 527f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) } 528f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 529f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return false; 530f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 531f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 532c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void IconUtil::InitializeBitmapHeader(BITMAPV5HEADER* header, int width, 533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) int height) { 53446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) DCHECK(header); 5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) memset(header, 0, sizeof(BITMAPV5HEADER)); 5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) header->bV5Size = sizeof(BITMAPV5HEADER); 5372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Note that icons are created using top-down DIBs so we must negate the 5392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // value used for the icon's height. 5402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) header->bV5Width = width; 5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) header->bV5Height = -height; 542a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) header->bV5Planes = 1; 543a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) header->bV5Compression = BI_RGB; 544a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Initializing the bitmap format to 32 bit ARGB. 5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) header->bV5BitCount = 32; 5472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) header->bV5RedMask = 0x00FF0000; 548868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) header->bV5GreenMask = 0x0000FF00; 549558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch header->bV5BlueMask = 0x000000FF; 5502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) header->bV5AlphaMask = 0xFF000000; 5512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Use the system color space. The default value is LCS_CALIBRATED_RGB, which 553c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // causes us to crash if we don't specify the approprite gammas, etc. See 554c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // <http://msdn.microsoft.com/en-us/library/ms536531(VS.85).aspx> and 555c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) // <http://b/1283121>. 5562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) header->bV5CSType = LCS_WINDOWS_COLOR_SPACE; 5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Use a valid value for bV5Intent as 0 is not a valid one. 5592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // <http://msdn.microsoft.com/en-us/library/dd183381(VS.85).aspx> 5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) header->bV5Intent = LCS_GM_IMAGES; 5612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void IconUtil::SetSingleIconImageInformation(const SkBitmap& bitmap, 5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t index, 565868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ICONDIR* icon_dir, 5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ICONIMAGE* icon_image, 5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t image_offset, 5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) size_t* image_byte_count) { 569c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) DCHECK(icon_dir != NULL); 5705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(icon_image != NULL); 5715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK_GT(image_offset, 0U); 5725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(image_byte_count != NULL); 5732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK_LT(bitmap.width(), kLargeIconSize); 5742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK_LT(bitmap.height(), kLargeIconSize); 5755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 5765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // We start by computing certain image values we'll use later on. 5772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t xor_mask_size, bytes_in_resource; 5782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ComputeBitmapSizeComponents(bitmap, 5792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) &xor_mask_size, 5802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) &bytes_in_resource); 581ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch 582ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch icon_dir->idEntries[index].bWidth = static_cast<BYTE>(bitmap.width()); 583ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch icon_dir->idEntries[index].bHeight = static_cast<BYTE>(bitmap.height()); 584ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch icon_dir->idEntries[index].wPlanes = 1; 585ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch icon_dir->idEntries[index].wBitCount = 32; 586ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch icon_dir->idEntries[index].dwBytesInRes = bytes_in_resource; 587ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch icon_dir->idEntries[index].dwImageOffset = image_offset; 588ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch icon_image->icHeader.biSize = sizeof(BITMAPINFOHEADER); 589ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch 5902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The width field in the BITMAPINFOHEADER structure accounts for the height 5912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // of both the AND mask and the XOR mask so we need to multiply the bitmap's 5922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // height by 2. The same does NOT apply to the width field. 5932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) icon_image->icHeader.biHeight = bitmap.height() * 2; 5942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) icon_image->icHeader.biWidth = bitmap.width(); 5952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) icon_image->icHeader.biPlanes = 1; 5962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) icon_image->icHeader.biBitCount = 32; 5972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 5982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // We use a helper function for copying to actual bits from the SkBitmap 5990529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // object into the appropriate space in the buffer. We use a helper function 6000529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // (rather than just copying the bits) because there is no way to specify the 6010529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // orientation (bottom-up vs. top-down) of a bitmap residing in a .ico file. 6020529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Thus, if we just copy the bits, we'll end up with a bottom up bitmap in 6030529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // the .ico file which will result in the icon being displayed upside down. 6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The helper function copies the image into the buffer one scanline at a 6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // time. 606f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // 6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Note that we don't need to initialize the AND mask since the memory 6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // allocated for the icon data buffer was initialized to zero. The icon we 6092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // create will therefore use an AND mask containing only zeros, which is OK 610f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // because the underlying image has an alpha channel. An AND mask containing 611f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // only zeros essentially means we'll initially treat all the pixels as 6125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // opaque. 6135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned char* image_addr = reinterpret_cast<unsigned char*>(icon_image); 6145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned char* xor_mask_addr = image_addr + sizeof(BITMAPINFOHEADER); 615f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) CopySkBitmapBitsIntoIconBuffer(bitmap, xor_mask_addr, xor_mask_size); 616f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) *image_byte_count = bytes_in_resource; 617f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void IconUtil::CopySkBitmapBitsIntoIconBuffer(const SkBitmap& bitmap, 6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned char* buffer, 6212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t buffer_size) { 622f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) SkAutoLockPixels bitmap_lock(bitmap); 62358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) unsigned char* bitmap_ptr = static_cast<unsigned char*>(bitmap.getPixels()); 62458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) size_t bitmap_size = bitmap.height() * bitmap.width() * 4; 62558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) DCHECK_EQ(buffer_size, bitmap_size); 62658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) for (size_t i = 0; i < bitmap_size; i += bitmap.width() * 4) { 6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) memcpy(buffer + bitmap_size - bitmap.width() * 4 - i, 6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bitmap_ptr + i, 6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) bitmap.width() * 4); 6302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 6312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 6322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)size_t IconUtil::ComputeIconFileBufferSize(const std::vector<SkBitmap>& set) { 6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK(!set.empty()); 6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // We start by counting the bytes for the structures that don't depend on the 6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // number of icon images. Note that sizeof(ICONDIR) already accounts for a 6385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // single ICONDIRENTRY structure, which is why we subtract one from the 6392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // number of bitmaps. 6402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t total_buffer_size = sizeof(ICONDIR); 6412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t bitmap_count = set.size(); 6422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) total_buffer_size += sizeof(ICONDIRENTRY) * (bitmap_count - 1); 6432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // May not have all icon sizes, but must have at least up to medium icon size. 6442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DCHECK_GE(bitmap_count, kNumIconDimensionsUpToMediumSize); 6452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Add the bitmap specific structure sizes. 6472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) for (size_t i = 0; i < bitmap_count; i++) { 6482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t xor_mask_size, bytes_in_resource; 6492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ComputeBitmapSizeComponents(set[i], 6502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) &xor_mask_size, 6512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) &bytes_in_resource); 6522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) total_buffer_size += bytes_in_resource; 6532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) } 6542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) return total_buffer_size; 6552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 6562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void IconUtil::ComputeBitmapSizeComponents(const SkBitmap& bitmap, 6582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t* xor_mask_size, 6592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t* bytes_in_resource) { 6602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // The XOR mask size is easy to calculate since we only deal with 32bpp 6612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // images. 6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *xor_mask_size = bitmap.width() * bitmap.height() * 4; 6632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 6640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Computing the AND mask is a little trickier since it is a monochrome 6650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // bitmap (regardless of the number of bits per pixels used in the XOR mask). 6660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // There are two things we must make sure we do when computing the AND mask 6675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // size: 6685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // 6695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // 1. Make sure the right number of bytes is allocated for each AND mask 6705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // scan line in case the number of pixels in the image is not divisible by 6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 8. For example, in a 15X15 image, 15 / 8 is one byte short of 6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // containing the number of bits we need in order to describe a single 6732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // image scan line so we need to add a byte. Thus, we need 2 bytes instead 6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // of 1 for each scan line. 6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 6762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 2. Make sure each scan line in the AND mask is 4 byte aligned (so that the 6772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // total icon image has a 4 byte alignment). In the 15X15 image example 6782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // above, we can not use 2 bytes so we increase it to the next multiple of 6792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 4 which is 4. 6802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // 6812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // Once we compute the size for a singe AND mask scan line, we multiply that 6822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // number by the image height in order to get the total number of bytes for 6832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes 6842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // for the monochrome bitmap representing the AND mask. 6852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) size_t and_line_length = (bitmap.width() + 7) >> 3; 686a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) and_line_length = (and_line_length + 3) & ~3; 687a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) size_t and_mask_size = and_line_length * bitmap.height(); 688a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) size_t masks_size = *xor_mask_size + and_mask_size; 6895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); 6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} 691a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)