icon_util.cc revision 116680a4aac90f2aa7413d9095a592090648e557
176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman// Copyright (c) 2012 The Chromium Authors. All rights reserved. 276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman// Use of this source code is governed by a BSD-style license that can be 376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman// found in the LICENSE file. 476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "ui/gfx/icon_util.h" 676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "base/file_util.h" 876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "base/files/important_file_writer.h" 976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "base/logging.h" 1076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "base/memory/scoped_ptr.h" 1176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "base/win/resource_util.h" 1276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "base/win/scoped_gdi_object.h" 1376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "base/win/scoped_handle.h" 1476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "base/win/scoped_hdc.h" 1576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "skia/ext/image_operations.h" 1676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "third_party/skia/include/core/SkBitmap.h" 1776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "ui/gfx/gdi_util.h" 1876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "ui/gfx/image/image.h" 1976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "ui/gfx/image/image_family.h" 2076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman#include "ui/gfx/size.h" 2176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 2276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermannamespace { 2376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 2476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanstruct ScopedICONINFO : ICONINFO { 2576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ScopedICONINFO() { 2676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman hbmColor = NULL; 2776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman hbmMask = NULL; 2876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 2976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 3076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ~ScopedICONINFO() { 3176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (hbmColor) 32d856b99aff36012d1c8bc72012d0ede414e17971Wichert Akkerman ::DeleteObject(hbmColor); 33d83c50b8e44db2a2e19d048ab7d1e1caf1fa1996Roland McGrath if (hbmMask) 34d83c50b8e44db2a2e19d048ab7d1e1caf1fa1996Roland McGrath ::DeleteObject(hbmMask); 356afc5659acc3df3d2e446ba4aa3a76bdd7264e1bRoland McGrath } 366afc5659acc3df3d2e446ba4aa3a76bdd7264e1bRoland McGrath}; 376afc5659acc3df3d2e446ba4aa3a76bdd7264e1bRoland McGrath 386afc5659acc3df3d2e446ba4aa3a76bdd7264e1bRoland McGrath// Creates a new ImageFamily, |resized_image_family|, based on the images in 396afc5659acc3df3d2e446ba4aa3a76bdd7264e1bRoland McGrath// |image_family|, but containing images of specific dimensions desirable for 406afc5659acc3df3d2e446ba4aa3a76bdd7264e1bRoland McGrath// Windows icons. For each desired image dimension, it chooses the most 416afc5659acc3df3d2e446ba4aa3a76bdd7264e1bRoland McGrath// appropriate image for that size, and resizes it to the desired size. 4276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman// Returns true on success, false on failure. Failure can occur if 43a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levin// |image_family| is empty, all images in the family have size 0x0, or an image 4476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman// has no allocated pixel data. 45a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levin// |resized_image_family| must be empty. 46a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levinbool BuildResizedImageFamily(const gfx::ImageFamily& image_family, 4776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman gfx::ImageFamily* resized_image_family) { 481cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin DCHECK(resized_image_family); 491cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin DCHECK(resized_image_family->empty()); 501cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin 511cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin for (size_t i = 0; i < IconUtil::kNumIconDimensions; ++i) { 521cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin int dimension = IconUtil::kIconDimensions[i]; 531cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin gfx::Size size(dimension, dimension); 541cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin const gfx::Image* best = image_family.GetBest(size); 551cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin if (!best || best->IsEmpty()) { 561cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin // Either |image_family| is empty, or all images have size 0x0. 571cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin return false; 581cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin } 591cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin 601cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin // Optimize for the "Large icons" view in Windows Vista+. This view displays 61a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levin // icons at full size if only if there is a 256x256 (kLargeIconSize) image 626afc5659acc3df3d2e446ba4aa3a76bdd7264e1bRoland McGrath // in the .ico file. Otherwise, it shrinks icons to 48x48 (kMediumIconSize). 63a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levin if (dimension > IconUtil::kMediumIconSize && 64a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko best->Width() <= IconUtil::kMediumIconSize && 65a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko best->Height() <= IconUtil::kMediumIconSize) { 66a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko // There is no source icon larger than 48x48, so do not create any 67a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levin // images larger than 48x48. kIconDimensions is sorted in ascending 68221f54f721a2f74e629bb70e34888205f68e95ccWichert Akkerman // order, so it is safe to break here. 692fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko break; 70a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko } 71f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman 722fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko if (best->Size() == size) { 732fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko resized_image_family->Add(*best); 74a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levin } else { 752fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko // There is no |dimension|x|dimension| source image. 76a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levin // Resize this one to the desired size, and insert it. 77b9c7ae621172bba141ef96e95e43f658c3643c71Denys Vlasenko SkBitmap best_bitmap = best->AsBitmap(); 78b9c7ae621172bba141ef96e95e43f658c3643c71Denys Vlasenko // Only kARGB_8888 images are supported. 792fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko // This will also filter out images with no pixels. 802fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko if (best_bitmap.colorType() != kN32_SkColorType) 818470374cba7df0e70653d95c4f336a4082c68d82Denys Vlasenko return false; 829fd4f96d2a2527ac7ca90c156bfc11ce10118684Denys Vlasenko SkBitmap resized_bitmap = skia::ImageOperations::Resize( 83f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman best_bitmap, skia::ImageOperations::RESIZE_LANCZOS3, 842fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko dimension, dimension); 852fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko resized_image_family->Add(gfx::Image::CreateFrom1xBitmap(resized_bitmap)); 862fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko } 872fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko } 882fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko return true; 89a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko} 90a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko 91a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko// Creates a set of bitmaps from an image family. 92a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko// All images smaller than 256x256 are converted to SkBitmaps, and inserted into 93a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko// |bitmaps| in order of aspect ratio (thinnest to widest), and then ascending 94a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko// size order. If an image of exactly 256x256 is specified, it is converted into 95a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko// PNG format and stored in |png_bytes|. Images with width or height larger than 96b9c7ae621172bba141ef96e95e43f658c3643c71Denys Vlasenko// 256 are ignored. 972fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko// |bitmaps| must be an empty vector, and not NULL. 98a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko// Returns true on success, false on failure. This fails if any image in 992fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko// |image_family| is not a 32-bit ARGB image, or is otherwise invalid. 1002fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenkobool ConvertImageFamilyToBitmaps( 1012fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko const gfx::ImageFamily& image_family, 1022fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko std::vector<SkBitmap>* bitmaps, 103a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko scoped_refptr<base::RefCountedMemory>* png_bytes) { 104a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko DCHECK(bitmaps != NULL); 105a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko DCHECK(bitmaps->empty()); 106a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko 107a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko for (gfx::ImageFamily::const_iterator it = image_family.begin(); 108a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko it != image_family.end(); ++it) { 109a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko const gfx::Image& image = *it; 110b9c7ae621172bba141ef96e95e43f658c3643c71Denys Vlasenko 1112fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko // All images should have one of the kIconDimensions sizes. 1122fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko DCHECK_GT(image.Width(), 0); 113a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko DCHECK_LE(image.Width(), IconUtil::kLargeIconSize); 114a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levin DCHECK_GT(image.Height(), 0); 1152fb4db3e7aa1d6ac6b4b43f47597197492a846ddDenys Vlasenko DCHECK_LE(image.Height(), IconUtil::kLargeIconSize); 116b9c7ae621172bba141ef96e95e43f658c3643c71Denys Vlasenko 117a7945a3d4e144674a8dd1d885e7086bc274e391bDmitry V. Levin SkBitmap bitmap = image.AsBitmap(); 118f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman 119a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko // Only 32 bit ARGB bitmaps are supported. We also make sure the bitmap has 120a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko // been properly initialized. 1216bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath SkAutoLockPixels bitmap_lock(bitmap); 122a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko if ((bitmap.colorType() != kN32_SkColorType) || 123a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko (bitmap.getPixels() == NULL)) { 124a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko return false; 1256bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath } 1266bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 127a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko // Special case: Icons exactly 256x256 are stored in PNG format. 128a1d541ec56e2fb4716f083fcc814b1dedde63d87Denys Vlasenko if (image.Width() == IconUtil::kLargeIconSize && 1296bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath image.Height() == IconUtil::kLargeIconSize) { 1306bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath *png_bytes = image.As1xPNGBytes(); 1316bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath } else { 1326bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath bitmaps->push_back(bitmap); 1336bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath } 1346bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath } 1351d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko 1366bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath return true; 1378470374cba7df0e70653d95c4f336a4082c68d82Denys Vlasenko} 1389fd4f96d2a2527ac7ca90c156bfc11ce10118684Denys Vlasenko 1396bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath} // namespace 1406bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 1415d64581e106f47c474707001f924ee15ef22830bDenys Vlasenko// The icon images appear in the icon file in same order in which their 1425d64581e106f47c474707001f924ee15ef22830bDenys Vlasenko// corresponding dimensions appear in this array, so it is important to keep 1436bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath// this array sorted. Also note that the maximum icon image size we can handle 1446bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath// is 256 by 256. See: 1456bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath// http://msdn.microsoft.com/en-us/library/windows/desktop/aa511280.aspx#size 1466bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrathconst int IconUtil::kIconDimensions[] = { 1471d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko 8, // Recommended by the MSDN as a nice to have icon size. 1486bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 10, // Used by the Shell (e.g. for shortcuts). 1496bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 14, // Recommended by the MSDN as a nice to have icon size. 1505d64581e106f47c474707001f924ee15ef22830bDenys Vlasenko 16, // Toolbar, Application and Shell icon sizes. 1515d64581e106f47c474707001f924ee15ef22830bDenys Vlasenko 22, // Recommended by the MSDN as a nice to have icon size. 1526bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 24, // Used by the Shell (e.g. for shortcuts). 1536bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 32, // Toolbar, Dialog and Wizard icon size. 1546bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 40, // Quick Launch. 1556bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 48, // Alt+Tab icon size. 1566bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 64, // Recommended by the MSDN as a nice to have icon size. 1576bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 96, // Recommended by the MSDN as a nice to have icon size. 1586bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 128, // Used by the Shell (e.g. for shortcuts). 1596bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath 256 // Used by Vista onwards for large icons. 1606bc09daaf6b249b35dec4283b165b1f39e6d6a0dRoland McGrath}; 16176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 1621201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenkoconst size_t IconUtil::kNumIconDimensions = arraysize(kIconDimensions); 16376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanconst size_t IconUtil::kNumIconDimensionsUpToMediumSize = 9; 16476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 16576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert AkkermanHICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) { 16676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Only 32 bit ARGB bitmaps are supported. We also try to perform as many 16776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // validations as we can on the bitmap. 16876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman SkAutoLockPixels bitmap_lock(bitmap); 16976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if ((bitmap.colorType() != kN32_SkColorType) || 17076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman (bitmap.width() <= 0) || (bitmap.height() <= 0) || 1711201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko (bitmap.getPixels() == NULL)) 17276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return NULL; 17376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 17476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // We start by creating a DIB which we'll use later on in order to create 17576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // the HICON. We use BITMAPV5HEADER since the bitmap we are about to convert 17676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // may contain an alpha channel and the V5 header allows us to specify the 17776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // alpha mask for the DIB. 17876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman BITMAPV5HEADER bitmap_header; 17976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman InitializeBitmapHeader(&bitmap_header, bitmap.width(), bitmap.height()); 1801201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko 18176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman void* bits = NULL; 18276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman HBITMAP dib; 18376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 18476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman { 18576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman base::win::ScopedGetDC hdc(NULL); 18676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman dib = ::CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO*>(&bitmap_header), 18776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman DIB_RGB_COLORS, &bits, NULL, 0); 18876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 18960fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko if (!dib || !bits) 19076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return NULL; 19176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 19276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman memcpy(bits, bitmap.getPixels(), bitmap.width() * bitmap.height() * 4); 19376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 19476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Icons are generally created using an AND and XOR masks where the AND 195f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman // specifies boolean transparency (the pixel is either opaque or 196f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman // transparent) and the XOR mask contains the actual image pixels. If the XOR 1971201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko // mask bitmap has an alpha channel, the AND monochrome bitmap won't 198f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman // actually be used for computing the pixel transparency. Even though all our 1991d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko // bitmap has an alpha channel, Windows might not agree when all alpha values 2001d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko // are zero. So the monochrome bitmap is created with all pixels transparent 2011d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko // for this case. Otherwise, it is created with all pixels opaque. 2021d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko bool bitmap_has_alpha_channel = PixelsHaveAlpha( 2031d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko static_cast<const uint32*>(bitmap.getPixels()), 2041d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko bitmap.width() * bitmap.height()); 20560fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko 2061d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko scoped_ptr<uint8[]> mask_bits; 2071d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko if (!bitmap_has_alpha_channel) { 2081d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko // Bytes per line with paddings to make it word alignment. 209f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman size_t bytes_per_line = (bitmap.width() + 0xF) / 16 * 2; 210f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman size_t mask_bits_size = bytes_per_line * bitmap.height(); 211f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman 21276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman mask_bits.reset(new uint8[mask_bits_size]); 2131201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko DCHECK(mask_bits.get()); 21476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 21576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Make all pixels transparent. 21676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman memset(mask_bits.get(), 0xFF, mask_bits_size); 21760fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko } 21876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 21976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman HBITMAP mono_bitmap = ::CreateBitmap(bitmap.width(), bitmap.height(), 1, 1, 22076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman reinterpret_cast<LPVOID>(mask_bits.get())); 22176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman DCHECK(mono_bitmap); 22276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 223f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman ICONINFO icon_info; 224f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman icon_info.fIcon = TRUE; 2251201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko icon_info.xHotspot = 0; 226f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman icon_info.yHotspot = 0; 2271d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko icon_info.hbmMask = mono_bitmap; 2281d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko icon_info.hbmColor = dib; 22960fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko HICON icon = ::CreateIconIndirect(&icon_info); 2301d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko ::DeleteObject(dib); 2311d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko ::DeleteObject(mono_bitmap); 2321d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko return icon; 233f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman} 234f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman 235f5eeabb156641482abd504fb98b039e1aae4ae87Wichert AkkermanSkBitmap* IconUtil::CreateSkBitmapFromHICON(HICON icon, const gfx::Size& s) { 23676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // We start with validating parameters. 2371201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko if (!icon || s.IsEmpty()) 23876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return NULL; 23976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ScopedICONINFO icon_info; 24076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!::GetIconInfo(icon, &icon_info)) 24160fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko return NULL; 24276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!icon_info.fIcon) 24376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return NULL; 24476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return new SkBitmap(CreateSkBitmapFromHICONHelper(icon, s)); 24576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman} 24676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 24776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkermanscoped_ptr<SkBitmap> IconUtil::CreateSkBitmapFromIconResource(HMODULE module, 24876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int resource_id, 24976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman int size) { 25076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman DCHECK_LE(size, kLargeIconSize); 2512e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin 2522e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin // For everything except the Vista+ 256x256 icons, use |LoadImage()|. 2532e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin if (size != kLargeIconSize) { 2542e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin HICON icon_handle = 2552e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin static_cast<HICON>(LoadImage(module, MAKEINTRESOURCE(resource_id), 25660fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko IMAGE_ICON, size, size, 2572e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin LR_DEFAULTCOLOR | LR_DEFAULTSIZE)); 25864acaa1193173c965ef32919aa5c092ce912d57cDenys Vlasenko scoped_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon_handle)); 2594793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko DestroyIcon(icon_handle); 2604793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko return bitmap.Pass(); 2614793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko } 26264acaa1193173c965ef32919aa5c092ce912d57cDenys Vlasenko 2634793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko // For Vista+ 256x256 PNG icons, read the resource directly and find 2644793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko // the corresponding icon entry to get its PNG bytes. 2654793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko void* icon_dir_data = NULL; 2664793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko size_t icon_dir_size = 0; 2674793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko if (!base::win::GetResourceFromModule(module, resource_id, RT_GROUP_ICON, 2684793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko &icon_dir_data, &icon_dir_size)) { 2694793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko return scoped_ptr<SkBitmap>(); 2704793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko } 2714793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko DCHECK(icon_dir_data); 2724793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko DCHECK_GE(icon_dir_size, sizeof(GRPICONDIR)); 2734793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko 2744793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko const GRPICONDIR* icon_dir = 2754793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko reinterpret_cast<const GRPICONDIR*>(icon_dir_data); 2762e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin const GRPICONDIRENTRY* large_icon_entry = NULL; 2774793221a53fb69aa519bc91ab19a79524c0df097Denys Vlasenko for (size_t i = 0; i < icon_dir->idCount; ++i) { 2782e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin const GRPICONDIRENTRY* entry = &icon_dir->idEntries[i]; 2792e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin // 256x256 icons are stored with width and height set to 0. 2802e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin // See: http://en.wikipedia.org/wiki/ICO_(file_format) 2812e55ff4562e87f8361f0c1db5a42ee6e9ac0cc56Dmitry V. Levin if (entry->bWidth == 0 && entry->bHeight == 0) { 2820ed617bd66624cec6138102545d73b2e2346f1f6Dmitry V. Levin large_icon_entry = entry; 28376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman break; 28476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman } 2851cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin } 28676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (!large_icon_entry) 28776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return scoped_ptr<SkBitmap>(); 28860fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko 28976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman void* png_data = NULL; 29076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman size_t png_size = 0; 2911d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko if (!base::win::GetResourceFromModule(module, large_icon_entry->nID, RT_ICON, 2921d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko &png_data, &png_size)) { 29376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return scoped_ptr<SkBitmap>(); 2941cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin } 2958470374cba7df0e70653d95c4f336a4082c68d82Denys Vlasenko DCHECK(png_data); 2969fd4f96d2a2527ac7ca90c156bfc11ce10118684Denys Vlasenko DCHECK_EQ(png_size, large_icon_entry->dwBytesInRes); 2971cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin 2981cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin gfx::Image image = gfx::Image::CreateFrom1xPNGBytes( 2991cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin new base::RefCountedStaticMemory(png_data, png_size)); 3001d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko return scoped_ptr<SkBitmap>(new SkBitmap(image.AsBitmap())); 3011cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin} 3021cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin 3031cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. LevinSkBitmap* IconUtil::CreateSkBitmapFromHICON(HICON icon) { 3045d64581e106f47c474707001f924ee15ef22830bDenys Vlasenko // We start with validating parameters. 3055d64581e106f47c474707001f924ee15ef22830bDenys Vlasenko if (!icon) 30660fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko return NULL; 3071cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin 30860fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko ScopedICONINFO icon_info; 3091cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin BITMAP bitmap_info = { 0 }; 31060fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko 311e46623403567c7dab387c8a9c6e40ae891c6ab21Roland McGrath if (!::GetIconInfo(icon, &icon_info)) 3121d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko return NULL; 3131cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin 3141cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin if (!::GetObject(icon_info.hbmMask, sizeof(bitmap_info), &bitmap_info)) 3155d64581e106f47c474707001f924ee15ef22830bDenys Vlasenko return NULL; 3165d64581e106f47c474707001f924ee15ef22830bDenys Vlasenko 31760fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko gfx::Size icon_size(bitmap_info.bmWidth, bitmap_info.bmHeight); 3181cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin return new SkBitmap(CreateSkBitmapFromHICONHelper(icon, icon_size)); 31960fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko} 3201cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin 32160fe8c139c6f2febefe595781812ddf0864a6ab8Denys VlasenkoHICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size, 322e46623403567c7dab387c8a9c6e40ae891c6ab21Roland McGrath const gfx::Point& hotspot, 3231cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin const void* dib_bits, 3241cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin size_t dib_size) { 32560fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko BITMAPINFO icon_bitmap_info = {0}; 3261cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin gfx::CreateBitmapHeader( 327f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman icon_size.width(), 3281cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin icon_size.height(), 3291cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin reinterpret_cast<BITMAPINFOHEADER*>(&icon_bitmap_info)); 3301cad25dd44afc58a5345e704dcbe4ec0d57e58ebDmitry V. Levin 331f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman base::win::ScopedGetDC dc(NULL); 33276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman base::win::ScopedCreateDC working_dc(CreateCompatibleDC(dc)); 3331201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko base::win::ScopedGDIObject<HBITMAP> bitmap_handle( 33476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman CreateDIBSection(dc, 33576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman &icon_bitmap_info, 336297b59401c998a2154b2fd1af7b234e2fa3a9305Dmitry V. Levin DIB_RGB_COLORS, 33760fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko 0, 33876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 0, 33976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 0)); 34076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman if (dib_size > 0) { 34176baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman SetDIBits(0, 34276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman bitmap_handle, 34376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 0, 34476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman icon_size.height(), 34576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman dib_bits, 34676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman &icon_bitmap_info, 347f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman DIB_RGB_COLORS); 348f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman } 3491201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko 350f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman HBITMAP old_bitmap = reinterpret_cast<HBITMAP>( 3511d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko SelectObject(working_dc, bitmap_handle)); 352297b59401c998a2154b2fd1af7b234e2fa3a9305Dmitry V. Levin SetBkMode(working_dc, TRANSPARENT); 35360fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko SelectObject(working_dc, old_bitmap); 3541d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko 3551d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko base::win::ScopedGDIObject<HBITMAP> mask( 3561d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko CreateBitmap(icon_size.width(), 3571d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko icon_size.height(), 3581d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko 1, 3591d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko 1, 3601d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko NULL)); 361f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman ICONINFO ii = {0}; 362f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman ii.fIcon = FALSE; 363f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman ii.xHotspot = hotspot.x(); 36476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ii.yHotspot = hotspot.y(); 3651201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko ii.hbmMask = mask; 36676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman ii.hbmColor = bitmap_handle; 36776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 368297b59401c998a2154b2fd1af7b234e2fa3a9305Dmitry V. Levin return CreateIconIndirect(&ii); 36960fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko} 37076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 37160fe8c139c6f2febefe595781812ddf0864a6ab8Denys VlasenkoSkBitmap IconUtil::CreateSkBitmapFromHICONHelper(HICON icon, 37276baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman const gfx::Size& s) { 37376baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman DCHECK(icon); 37476baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman DCHECK(!s.IsEmpty()); 37576baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman 37676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Allocating memory for the SkBitmap object. We are going to create an ARGB 37776baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // bitmap so we should set the configuration appropriately. 37876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman SkBitmap bitmap; 37976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman bitmap.allocN32Pixels(s.width(), s.height()); 38076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman bitmap.eraseARGB(0, 0, 0, 0); 381f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman SkAutoLockPixels bitmap_lock(bitmap); 382f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman 3831201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko // Now we should create a DIB so that we can use ::DrawIconEx in order to 384f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman // obtain the icon's image. 3851d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko BITMAPV5HEADER h; 386297b59401c998a2154b2fd1af7b234e2fa3a9305Dmitry V. Levin InitializeBitmapHeader(&h, s.width(), s.height()); 38760fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko HDC hdc = ::GetDC(NULL); 3881d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko uint32* bits; 38960fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko HBITMAP dib = ::CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO*>(&h), 3901d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko DIB_RGB_COLORS, reinterpret_cast<void**>(&bits), NULL, 0); 3911d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko DCHECK(dib); 3921d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko HDC dib_dc = CreateCompatibleDC(hdc); 3931d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko ::ReleaseDC(NULL, hdc); 3941d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko DCHECK(dib_dc); 3951d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko HGDIOBJ old_obj = ::SelectObject(dib_dc, dib); 3961d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko 397f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman // Windows icons are defined using two different masks. The XOR mask, which 398f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman // represents the icon image and an AND mask which is a monochrome bitmap 399f5eeabb156641482abd504fb98b039e1aae4ae87Wichert Akkerman // which indicates the transparency of each pixel. 4000ed617bd66624cec6138102545d73b2e2346f1f6Dmitry V. Levin // 4010ed617bd66624cec6138102545d73b2e2346f1f6Dmitry V. Levin // To make things more complex, the icon image itself can be an ARGB bitmap 4020ed617bd66624cec6138102545d73b2e2346f1f6Dmitry V. Levin // and therefore contain an alpha channel which specifies the transparency 4031a684d6ebee655035b06facee28c83f94c7dfbb9Dmitry V. Levin // for each pixel. Unfortunately, there is no easy way to determine whether 404165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // or not a bitmap has an alpha channel and therefore constructing the bitmap 405165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // for the icon is nothing but straightforward. 406165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // 407165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // The idea is to read the AND mask but use it only if we know for sure that 4081d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko // the icon image does not have an alpha channel. The only way to tell if the 409165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // bitmap has an alpha channel is by looking through the pixels and checking 410165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // whether there are non-zero alpha bytes. 411165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // 412165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // We start by drawing the AND mask into our DIB. 413165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin size_t num_pixels = s.GetArea(); 414165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin memset(bits, 0, num_pixels * 4); 415165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin ::DrawIconEx(dib_dc, 0, 0, icon, s.width(), s.height(), 0, NULL, DI_MASK); 416165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 417165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // Capture boolean opacity. We may not use it if we find out the bitmap has 418165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // an alpha channel. 419165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin scoped_ptr<bool[]> opaque(new bool[num_pixels]); 420165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin for (size_t i = 0; i < num_pixels; ++i) 421165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin opaque[i] = !bits[i]; 422165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 423165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // Then draw the image itself which is really the XOR mask. 424165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin memset(bits, 0, num_pixels * 4); 425165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin ::DrawIconEx(dib_dc, 0, 0, icon, s.width(), s.height(), 0, NULL, DI_NORMAL); 426165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin memcpy(bitmap.getPixels(), static_cast<void*>(bits), num_pixels * 4); 427165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 428165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // Finding out whether the bitmap has an alpha channel. 429165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin bool bitmap_has_alpha_channel = PixelsHaveAlpha( 430165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin static_cast<const uint32*>(bitmap.getPixels()), num_pixels); 431165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 432165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // If the bitmap does not have an alpha channel, we need to build it using 43360fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko // the previously captured AND mask. Otherwise, we are done. 43471d7089055b0ce830bf13d9322f06b87d6ce47c0Dmitry V. Levin if (!bitmap_has_alpha_channel) { 435165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin uint32* p = static_cast<uint32*>(bitmap.getPixels()); 436165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin for (size_t i = 0; i < num_pixels; ++p, ++i) { 437165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin DCHECK_EQ((*p & 0xff000000), 0u); 438165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin if (opaque[i]) 439165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin *p |= 0xff000000; 440165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin else 441165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin *p &= 0x00ffffff; 442165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin } 443165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin } 444165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 445165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin ::SelectObject(dib_dc, old_obj); 446165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin ::DeleteObject(dib); 447165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin ::DeleteDC(dib_dc); 448165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 44960fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko return bitmap; 450165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin} 451165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 452165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin// static 453165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levinbool IconUtil::CreateIconFileFromImageFamily( 454165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin const gfx::ImageFamily& image_family, 455165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin const base::FilePath& icon_path) { 45676baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // Creating a set of bitmaps corresponding to the icon images we'll end up 4571a684d6ebee655035b06facee28c83f94c7dfbb9Dmitry V. Levin // storing in the icon file. Each bitmap is created by resizing the most 45876baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman // appropriate image from |image_family| to the desired size. 459165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin gfx::ImageFamily resized_image_family; 4609fd4f96d2a2527ac7ca90c156bfc11ce10118684Denys Vlasenko if (!BuildResizedImageFamily(image_family, &resized_image_family)) 461165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin return false; 462165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 463165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin std::vector<SkBitmap> bitmaps; 464165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin scoped_refptr<base::RefCountedMemory> png_bytes; 465165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin if (!ConvertImageFamilyToBitmaps(resized_image_family, &bitmaps, &png_bytes)) 466165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin return false; 467165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 468165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // Guaranteed true because BuildResizedImageFamily will provide at least one 469165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // image < 256x256. 470165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin DCHECK(!bitmaps.empty()); 471165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin size_t bitmap_count = bitmaps.size(); // Not including PNG image. 472165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // Including PNG image, if any. 473165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin size_t image_count = bitmap_count + (png_bytes.get() ? 1 : 0); 474165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 475165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // Computing the total size of the buffer we need in order to store the 47660fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko // images in the desired icon format. 47771d7089055b0ce830bf13d9322f06b87d6ce47c0Dmitry V. Levin size_t buffer_size = ComputeIconFileBufferSize(bitmaps); 478165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // Account for the bytes needed for the PNG entry. 4790b315b65432cda20c7e5608c5124289036522fa7H.J. Lu if (png_bytes.get()) 4800b315b65432cda20c7e5608c5124289036522fa7H.J. Lu buffer_size += sizeof(ICONDIRENTRY) + png_bytes->size(); 481165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 482165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // Setting the information in the structures residing within the buffer. 4830b315b65432cda20c7e5608c5124289036522fa7H.J. Lu // First, we set the information which doesn't require iterating through the 4840b315b65432cda20c7e5608c5124289036522fa7H.J. Lu // bitmap set and then we set the bitmap specific structures. In the latter 485165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // step we also copy the actual bits. 486165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin std::vector<uint8> buffer(buffer_size); 4870b315b65432cda20c7e5608c5124289036522fa7H.J. Lu ICONDIR* icon_dir = reinterpret_cast<ICONDIR*>(&buffer[0]); 488165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin icon_dir->idType = kResourceTypeIcon; 4890b315b65432cda20c7e5608c5124289036522fa7H.J. Lu icon_dir->idCount = static_cast<WORD>(image_count); 490165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin // - 1 because there is already one ICONDIRENTRY in ICONDIR. 4910b315b65432cda20c7e5608c5124289036522fa7H.J. Lu size_t icon_dir_count = image_count - 1; 492165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 49360fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko size_t offset = sizeof(ICONDIR) + (sizeof(ICONDIRENTRY) * icon_dir_count); 494165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin for (size_t i = 0; i < bitmap_count; i++) { 495165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin ICONIMAGE* image = reinterpret_cast<ICONIMAGE*>(&buffer[offset]); 496165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin DCHECK_LT(offset, buffer_size); 49773215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin size_t icon_image_size = 0; 49873215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin SetSingleIconImageInformation(bitmaps[i], i, icon_dir, image, offset, 49973215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin &icon_image_size); 50073215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin DCHECK_GT(icon_image_size, 0U); 50173215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin offset += icon_image_size; 50273215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin } 50373215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin 50473215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin // Add the PNG entry, if necessary. 50573215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin if (png_bytes.get()) { 50673215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin ICONDIRENTRY* entry = &icon_dir->idEntries[bitmap_count]; 50773215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin entry->bWidth = 0; 50873215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin entry->bHeight = 0; 50973215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin entry->wPlanes = 1; 51073215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin entry->wBitCount = 32; 51173215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin entry->dwBytesInRes = static_cast<DWORD>(png_bytes->size()); 51273215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin entry->dwImageOffset = static_cast<DWORD>(offset); 51373215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin memcpy(&buffer[offset], png_bytes->front(), png_bytes->size()); 514165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin offset += png_bytes->size(); 515165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin } 516165b15dbe8ca39334dd7059883c955829c95ac70Dmitry V. Levin 51773215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin DCHECK_EQ(offset, buffer_size); 51873215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin 51976baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman std::string data(buffer.begin(), buffer.end()); 52076baf7c9f6dd61a15524ad43c1b690c252cf5b7Wichert Akkerman return base::ImportantFileWriter::WriteFileAtomically(icon_path, data); 5211e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath} 5220ed617bd66624cec6138102545d73b2e2346f1f6Dmitry V. Levin 5230ed617bd66624cec6138102545d73b2e2346f1f6Dmitry V. Levinbool IconUtil::PixelsHaveAlpha(const uint32* pixels, size_t num_pixels) { 52454a4edd69a320542ddd0dffec05dacab7443d453Roland McGrath for (const uint32* end = pixels + num_pixels; pixels != end; ++pixels) { 525a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen if ((*pixels & 0xff000000) != 0) 526a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen return true; 527a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen } 528a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen 529d35bdcad13caac3e167735e1f0fc50355b2f9523Dmitry V. Levin return false; 530d35bdcad13caac3e167735e1f0fc50355b2f9523Dmitry V. Levin} 531a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen 532a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensenvoid IconUtil::InitializeBitmapHeader(BITMAPV5HEADER* header, int width, 533a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen int height) { 534a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen DCHECK(header); 535a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen memset(header, 0, sizeof(BITMAPV5HEADER)); 536a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen header->bV5Size = sizeof(BITMAPV5HEADER); 537a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen 538a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen // Note that icons are created using top-down DIBs so we must negate the 539a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen // value used for the icon's height. 540a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen header->bV5Width = width; 541a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen header->bV5Height = -height; 542a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen header->bV5Planes = 1; 543a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen header->bV5Compression = BI_RGB; 544a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen 545a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen // Initializing the bitmap format to 32 bit ARGB. 546a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen header->bV5BitCount = 32; 547a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen header->bV5RedMask = 0x00FF0000; 5481e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath header->bV5GreenMask = 0x0000FF00; 5491201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko header->bV5BlueMask = 0x000000FF; 5501e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath header->bV5AlphaMask = 0xFF000000; 5511e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath 552a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen // Use the system color space. The default value is LCS_CALIBRATED_RGB, which 55360fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko // causes us to crash if we don't specify the approprite gammas, etc. See 5541e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // <http://msdn.microsoft.com/en-us/library/ms536531(VS.85).aspx> and 5551e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // <http://b/1283121>. 5561e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath header->bV5CSType = LCS_WINDOWS_COLOR_SPACE; 5571e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath 5581e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // Use a valid value for bV5Intent as 0 is not a valid one. 5591e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // <http://msdn.microsoft.com/en-us/library/dd183381(VS.85).aspx> 5601201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko header->bV5Intent = LCS_GM_IMAGES; 5611e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath} 5621e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath 563a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensenvoid IconUtil::SetSingleIconImageInformation(const SkBitmap& bitmap, 56460fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko size_t index, 5651e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath ICONDIR* icon_dir, 5661e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath ICONIMAGE* icon_image, 5671e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath size_t image_offset, 5681e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath size_t* image_byte_count) { 5691e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath DCHECK(icon_dir != NULL); 5701e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath DCHECK(icon_image != NULL); 5711e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath DCHECK_GT(image_offset, 0U); 5721e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath DCHECK(image_byte_count != NULL); 5731e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath DCHECK_LT(bitmap.width(), kLargeIconSize); 5741e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath DCHECK_LT(bitmap.height(), kLargeIconSize); 5751201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko 5761e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // We start by computing certain image values we'll use later on. 5771e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath size_t xor_mask_size, bytes_in_resource; 578a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen ComputeBitmapSizeComponents(bitmap, 57960fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko &xor_mask_size, 580b2dee13345a62c80a677f3342cd525d611fbc632Roland McGrath &bytes_in_resource); 58160fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko 5821e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath icon_dir->idEntries[index].bWidth = static_cast<BYTE>(bitmap.width()); 58360fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko icon_dir->idEntries[index].bHeight = static_cast<BYTE>(bitmap.height()); 5841e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath icon_dir->idEntries[index].wPlanes = 1; 5851e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath icon_dir->idEntries[index].wBitCount = 32; 5861e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath icon_dir->idEntries[index].dwBytesInRes = bytes_in_resource; 5871e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath icon_dir->idEntries[index].dwImageOffset = image_offset; 5881e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath icon_image->icHeader.biSize = sizeof(BITMAPINFOHEADER); 5891e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath 5901e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // The width field in the BITMAPINFOHEADER structure accounts for the height 5911e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // of both the AND mask and the XOR mask so we need to multiply the bitmap's 5921e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // height by 2. The same does NOT apply to the width field. 59373215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin icon_image->icHeader.biHeight = bitmap.height() * 2; 59473215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin icon_image->icHeader.biWidth = bitmap.width(); 59573215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin icon_image->icHeader.biPlanes = 1; 59673215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin icon_image->icHeader.biBitCount = 32; 59773215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin 598a5fea9060f382fb57f3c4c6492184cd2a7f92b40Stefan Sørensen // We use a helper function for copying to actual bits from the SkBitmap 59973215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin // object into the appropriate space in the buffer. We use a helper function 60073215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin // (rather than just copying the bits) because there is no way to specify the 60173215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin // orientation (bottom-up vs. top-down) of a bitmap residing in a .ico file. 60273215473cea2bc53a520b5285775be84501b1f2dDmitry V. Levin // Thus, if we just copy the bits, we'll end up with a bottom up bitmap in 6031e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // the .ico file which will result in the icon being displayed upside down. 6041e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // The helper function copies the image into the buffer one scanline at a 6051e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // time. 6060ed617bd66624cec6138102545d73b2e2346f1f6Dmitry V. Levin // 6071e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // Note that we don't need to initialize the AND mask since the memory 608d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin // allocated for the icon data buffer was initialized to zero. The icon we 609d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin // create will therefore use an AND mask containing only zeros, which is OK 610d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin // because the underlying image has an alpha channel. An AND mask containing 611d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin // only zeros essentially means we'll initially treat all the pixels as 6121d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko // opaque. 613d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin unsigned char* image_addr = reinterpret_cast<unsigned char*>(icon_image); 614d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin unsigned char* xor_mask_addr = image_addr + sizeof(BITMAPINFOHEADER); 615d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin CopySkBitmapBitsIntoIconBuffer(bitmap, xor_mask_addr, xor_mask_size); 616d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin *image_byte_count = bytes_in_resource; 6171d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko} 618d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin 6191d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenkovoid IconUtil::CopySkBitmapBitsIntoIconBuffer(const SkBitmap& bitmap, 620d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin unsigned char* buffer, 621d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin size_t buffer_size) { 622d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin SkAutoLockPixels bitmap_lock(bitmap); 623d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin unsigned char* bitmap_ptr = static_cast<unsigned char*>(bitmap.getPixels()); 624d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin size_t bitmap_size = bitmap.height() * bitmap.width() * 4; 625d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin DCHECK_EQ(buffer_size, bitmap_size); 62660fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko for (size_t i = 0; i < bitmap_size; i += bitmap.width() * 4) { 6271d632468c09756d3382d1a92f99a8413636e75d3Denys Vlasenko memcpy(buffer + bitmap_size - bitmap.width() * 4 - i, 628d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin bitmap_ptr + i, 629d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin bitmap.width() * 4); 630d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin } 631d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin} 632d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin 633bae549e91b1bf0d2261d1137c7f2bf08180ad9e6Dmitry V. Levinsize_t IconUtil::ComputeIconFileBufferSize(const std::vector<SkBitmap>& set) { 63460fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko DCHECK(!set.empty()); 635d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin 636d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin // We start by counting the bytes for the structures that don't depend on the 637d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin // number of icon images. Note that sizeof(ICONDIR) already accounts for a 638d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin // single ICONDIRENTRY structure, which is why we subtract one from the 639d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin // number of bitmaps. 640d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin size_t total_buffer_size = sizeof(ICONDIR); 641d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin size_t bitmap_count = set.size(); 64260fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko total_buffer_size += sizeof(ICONDIRENTRY) * (bitmap_count - 1); 64360fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko // May not have all icon sizes, but must have at least up to medium icon size. 644d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin DCHECK_GE(bitmap_count, kNumIconDimensionsUpToMediumSize); 645d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin 646d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin // Add the bitmap specific structure sizes. 647d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin for (size_t i = 0; i < bitmap_count; i++) { 6481e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath size_t xor_mask_size, bytes_in_resource; 649d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin ComputeBitmapSizeComponents(set[i], 6501e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath &xor_mask_size, 6511e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath &bytes_in_resource); 652d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin total_buffer_size += bytes_in_resource; 653d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin } 6549fd4f96d2a2527ac7ca90c156bfc11ce10118684Denys Vlasenko return total_buffer_size; 655d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin} 656d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin 657d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levinvoid IconUtil::ComputeBitmapSizeComponents(const SkBitmap& bitmap, 658d3cb392f8520cb1a6a3d6c93febe2a2424560574Dmitry V. Levin size_t* xor_mask_size, 659b63256e69bf3f1a74aadb0e14556490bc8f4ef95Denys Vlasenko size_t* bytes_in_resource) { 66060fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko // The XOR mask size is easy to calculate since we only deal with 32bpp 6611e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // images. 662675d4a6dba13915527309c962c38f5f961ec2996Roland McGrath *xor_mask_size = bitmap.width() * bitmap.height() * 4; 663675d4a6dba13915527309c962c38f5f961ec2996Roland McGrath 664675d4a6dba13915527309c962c38f5f961ec2996Roland McGrath // Computing the AND mask is a little trickier since it is a monochrome 665675d4a6dba13915527309c962c38f5f961ec2996Roland McGrath // bitmap (regardless of the number of bits per pixels used in the XOR mask). 666675d4a6dba13915527309c962c38f5f961ec2996Roland McGrath // There are two things we must make sure we do when computing the AND mask 667bae549e91b1bf0d2261d1137c7f2bf08180ad9e6Dmitry V. Levin // size: 66860fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko // 6691e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // 1. Make sure the right number of bytes is allocated for each AND mask 670ae5aa47370455123bf84e52dd8354d26c29efea8Dmitry V. Levin // scan line in case the number of pixels in the image is not divisible by 6711e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // 8. For example, in a 15X15 image, 15 / 8 is one byte short of 6721e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // containing the number of bits we need in order to describe a single 6731e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // image scan line so we need to add a byte. Thus, we need 2 bytes instead 6741e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // of 1 for each scan line. 675ae5aa47370455123bf84e52dd8354d26c29efea8Dmitry V. Levin // 676ae5aa47370455123bf84e52dd8354d26c29efea8Dmitry V. Levin // 2. Make sure each scan line in the AND mask is 4 byte aligned (so that the 677ae5aa47370455123bf84e52dd8354d26c29efea8Dmitry V. Levin // total icon image has a 4 byte alignment). In the 15X15 image example 678ae5aa47370455123bf84e52dd8354d26c29efea8Dmitry V. Levin // above, we can not use 2 bytes so we increase it to the next multiple of 679ae5aa47370455123bf84e52dd8354d26c29efea8Dmitry V. Levin // 4 which is 4. 680ae5aa47370455123bf84e52dd8354d26c29efea8Dmitry V. Levin // 681d4c85ebbc64b1eb141b310a4634c6ca37fd352c1Roland McGrath // Once we compute the size for a singe AND mask scan line, we multiply that 682d4c85ebbc64b1eb141b310a4634c6ca37fd352c1Roland McGrath // number by the image height in order to get the total number of bytes for 683d4c85ebbc64b1eb141b310a4634c6ca37fd352c1Roland McGrath // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes 6841e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath // for the monochrome bitmap representing the AND mask. 68560fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko size_t and_line_length = (bitmap.width() + 7) >> 3; 68660fe8c139c6f2febefe595781812ddf0864a6ab8Denys Vlasenko and_line_length = (and_line_length + 3) & ~3; 6871e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath size_t and_mask_size = and_line_length * bitmap.height(); 6881e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath size_t masks_size = *xor_mask_size + and_mask_size; 6891e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); 6901e35679bbb8c840bd0b107c68cb187feeb3e3282Roland McGrath} 6911201426dd43f5b4e12dfe520e2a9c5027d33dc11Denys Vlasenko