This source file includes following definitions.
- BuildResizedImageFamily
- ConvertImageFamilyToBitmaps
- CreateHICONFromSkBitmap
- CreateSkBitmapFromHICON
- CreateSkBitmapFromIconResource
- CreateSkBitmapFromHICON
- CreateCursorFromDIB
- CreateSkBitmapFromHICONHelper
- CreateIconFileFromImageFamily
- PixelsHaveAlpha
- InitializeBitmapHeader
- SetSingleIconImageInformation
- CopySkBitmapBitsIntoIconBuffer
- ComputeIconFileBufferSize
- ComputeBitmapSizeComponents
#include "ui/gfx/icon_util.h"
#include "base/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/win/resource_util.h"
#include "base/win/scoped_gdi_object.h"
#include "base/win/scoped_handle.h"
#include "base/win/scoped_hdc.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/gdi_util.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_family.h"
#include "ui/gfx/size.h"
namespace {
struct ScopedICONINFO : ICONINFO {
ScopedICONINFO() {
hbmColor = NULL;
hbmMask = NULL;
}
~ScopedICONINFO() {
if (hbmColor)
::DeleteObject(hbmColor);
if (hbmMask)
::DeleteObject(hbmMask);
}
};
bool BuildResizedImageFamily(const gfx::ImageFamily& image_family,
gfx::ImageFamily* resized_image_family) {
DCHECK(resized_image_family);
DCHECK(resized_image_family->empty());
for (size_t i = 0; i < IconUtil::kNumIconDimensions; ++i) {
int dimension = IconUtil::kIconDimensions[i];
gfx::Size size(dimension, dimension);
const gfx::Image* best = image_family.GetBest(size);
if (!best || best->IsEmpty()) {
return false;
}
if (dimension > IconUtil::kMediumIconSize &&
best->Width() <= IconUtil::kMediumIconSize &&
best->Height() <= IconUtil::kMediumIconSize) {
break;
}
if (best->Size() == size) {
resized_image_family->Add(*best);
} else {
SkBitmap best_bitmap = best->AsBitmap();
if (best_bitmap.colorType() != kPMColor_SkColorType)
return false;
SkBitmap resized_bitmap = skia::ImageOperations::Resize(
best_bitmap, skia::ImageOperations::RESIZE_LANCZOS3,
dimension, dimension);
resized_image_family->Add(gfx::Image::CreateFrom1xBitmap(resized_bitmap));
}
}
return true;
}
bool ConvertImageFamilyToBitmaps(
const gfx::ImageFamily& image_family,
std::vector<SkBitmap>* bitmaps,
scoped_refptr<base::RefCountedMemory>* png_bytes) {
DCHECK(bitmaps != NULL);
DCHECK(bitmaps->empty());
for (gfx::ImageFamily::const_iterator it = image_family.begin();
it != image_family.end(); ++it) {
const gfx::Image& image = *it;
DCHECK_GT(image.Width(), 0);
DCHECK_LE(image.Width(), IconUtil::kLargeIconSize);
DCHECK_GT(image.Height(), 0);
DCHECK_LE(image.Height(), IconUtil::kLargeIconSize);
SkBitmap bitmap = image.AsBitmap();
SkAutoLockPixels bitmap_lock(bitmap);
if ((bitmap.colorType() != kPMColor_SkColorType) ||
(bitmap.getPixels() == NULL)) {
return false;
}
if (image.Width() == IconUtil::kLargeIconSize &&
image.Height() == IconUtil::kLargeIconSize) {
*png_bytes = image.As1xPNGBytes();
} else {
bitmaps->push_back(bitmap);
}
}
return true;
}
}
const int IconUtil::kIconDimensions[] = {
8,
10,
14,
16,
22,
24,
32,
40,
48,
64,
96,
128,
256
};
const size_t IconUtil::kNumIconDimensions = arraysize(kIconDimensions);
const size_t IconUtil::kNumIconDimensionsUpToMediumSize = 9;
HICON IconUtil::CreateHICONFromSkBitmap(const SkBitmap& bitmap) {
SkAutoLockPixels bitmap_lock(bitmap);
if ((bitmap.colorType() != kPMColor_SkColorType) ||
(bitmap.width() <= 0) || (bitmap.height() <= 0) ||
(bitmap.getPixels() == NULL))
return NULL;
BITMAPV5HEADER bitmap_header;
InitializeBitmapHeader(&bitmap_header, bitmap.width(), bitmap.height());
void* bits;
HDC hdc = ::GetDC(NULL);
HBITMAP dib;
dib = ::CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO*>(&bitmap_header),
DIB_RGB_COLORS, &bits, NULL, 0);
DCHECK(dib);
::ReleaseDC(NULL, hdc);
memcpy(bits, bitmap.getPixels(), bitmap.width() * bitmap.height() * 4);
bool bitmap_has_alpha_channel = PixelsHaveAlpha(
static_cast<const uint32*>(bitmap.getPixels()),
bitmap.width() * bitmap.height());
scoped_ptr<uint8[]> mask_bits;
if (!bitmap_has_alpha_channel) {
size_t bytes_per_line = (bitmap.width() + 0xF) / 16 * 2;
size_t mask_bits_size = bytes_per_line * bitmap.height();
mask_bits.reset(new uint8[mask_bits_size]);
DCHECK(mask_bits.get());
memset(mask_bits.get(), 0xFF, mask_bits_size);
}
HBITMAP mono_bitmap = ::CreateBitmap(bitmap.width(), bitmap.height(), 1, 1,
reinterpret_cast<LPVOID>(mask_bits.get()));
DCHECK(mono_bitmap);
ICONINFO icon_info;
icon_info.fIcon = TRUE;
icon_info.xHotspot = 0;
icon_info.yHotspot = 0;
icon_info.hbmMask = mono_bitmap;
icon_info.hbmColor = dib;
HICON icon = ::CreateIconIndirect(&icon_info);
::DeleteObject(dib);
::DeleteObject(mono_bitmap);
return icon;
}
SkBitmap* IconUtil::CreateSkBitmapFromHICON(HICON icon, const gfx::Size& s) {
if (!icon || s.IsEmpty())
return NULL;
ScopedICONINFO icon_info;
if (!::GetIconInfo(icon, &icon_info))
return NULL;
if (!icon_info.fIcon)
return NULL;
return new SkBitmap(CreateSkBitmapFromHICONHelper(icon, s));
}
scoped_ptr<SkBitmap> IconUtil::CreateSkBitmapFromIconResource(HMODULE module,
int resource_id,
int size) {
DCHECK_LE(size, kLargeIconSize);
if (size != kLargeIconSize) {
HICON icon_handle =
static_cast<HICON>(LoadImage(module, MAKEINTRESOURCE(resource_id),
IMAGE_ICON, size, size,
LR_DEFAULTCOLOR | LR_DEFAULTSIZE));
scoped_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon_handle));
DestroyIcon(icon_handle);
return bitmap.Pass();
}
void* icon_dir_data = NULL;
size_t icon_dir_size = 0;
if (!base::win::GetResourceFromModule(module, resource_id, RT_GROUP_ICON,
&icon_dir_data, &icon_dir_size)) {
return scoped_ptr<SkBitmap>();
}
DCHECK(icon_dir_data);
DCHECK_GE(icon_dir_size, sizeof(GRPICONDIR));
const GRPICONDIR* icon_dir =
reinterpret_cast<const GRPICONDIR*>(icon_dir_data);
const GRPICONDIRENTRY* large_icon_entry = NULL;
for (size_t i = 0; i < icon_dir->idCount; ++i) {
const GRPICONDIRENTRY* entry = &icon_dir->idEntries[i];
if (entry->bWidth == 0 && entry->bHeight == 0) {
large_icon_entry = entry;
break;
}
}
if (!large_icon_entry)
return scoped_ptr<SkBitmap>();
void* png_data = NULL;
size_t png_size = 0;
if (!base::win::GetResourceFromModule(module, large_icon_entry->nID, RT_ICON,
&png_data, &png_size)) {
return scoped_ptr<SkBitmap>();
}
DCHECK(png_data);
DCHECK_EQ(png_size, large_icon_entry->dwBytesInRes);
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(
new base::RefCountedStaticMemory(png_data, png_size));
return scoped_ptr<SkBitmap>(new SkBitmap(image.AsBitmap()));
}
SkBitmap* IconUtil::CreateSkBitmapFromHICON(HICON icon) {
if (!icon)
return NULL;
ScopedICONINFO icon_info;
BITMAP bitmap_info = { 0 };
if (!::GetIconInfo(icon, &icon_info))
return NULL;
if (!::GetObject(icon_info.hbmMask, sizeof(bitmap_info), &bitmap_info))
return NULL;
gfx::Size icon_size(bitmap_info.bmWidth, bitmap_info.bmHeight);
return new SkBitmap(CreateSkBitmapFromHICONHelper(icon, icon_size));
}
HICON IconUtil::CreateCursorFromDIB(const gfx::Size& icon_size,
const gfx::Point& hotspot,
const void* dib_bits,
size_t dib_size) {
BITMAPINFO icon_bitmap_info = {0};
gfx::CreateBitmapHeader(
icon_size.width(),
icon_size.height(),
reinterpret_cast<BITMAPINFOHEADER*>(&icon_bitmap_info));
base::win::ScopedGetDC dc(NULL);
base::win::ScopedCreateDC working_dc(CreateCompatibleDC(dc));
base::win::ScopedGDIObject<HBITMAP> bitmap_handle(
CreateDIBSection(dc,
&icon_bitmap_info,
DIB_RGB_COLORS,
0,
0,
0));
if (dib_size > 0) {
SetDIBits(0,
bitmap_handle,
0,
icon_size.height(),
dib_bits,
&icon_bitmap_info,
DIB_RGB_COLORS);
}
HBITMAP old_bitmap = reinterpret_cast<HBITMAP>(
SelectObject(working_dc, bitmap_handle));
SetBkMode(working_dc, TRANSPARENT);
SelectObject(working_dc, old_bitmap);
base::win::ScopedGDIObject<HBITMAP> mask(
CreateBitmap(icon_size.width(),
icon_size.height(),
1,
1,
NULL));
ICONINFO ii = {0};
ii.fIcon = FALSE;
ii.xHotspot = hotspot.x();
ii.yHotspot = hotspot.y();
ii.hbmMask = mask;
ii.hbmColor = bitmap_handle;
return CreateIconIndirect(&ii);
}
SkBitmap IconUtil::CreateSkBitmapFromHICONHelper(HICON icon,
const gfx::Size& s) {
DCHECK(icon);
DCHECK(!s.IsEmpty());
SkBitmap bitmap;
bitmap.allocN32Pixels(s.width(), s.height());
bitmap.eraseARGB(0, 0, 0, 0);
SkAutoLockPixels bitmap_lock(bitmap);
BITMAPV5HEADER h;
InitializeBitmapHeader(&h, s.width(), s.height());
HDC hdc = ::GetDC(NULL);
uint32* bits;
HBITMAP dib = ::CreateDIBSection(hdc, reinterpret_cast<BITMAPINFO*>(&h),
DIB_RGB_COLORS, reinterpret_cast<void**>(&bits), NULL, 0);
DCHECK(dib);
HDC dib_dc = CreateCompatibleDC(hdc);
::ReleaseDC(NULL, hdc);
DCHECK(dib_dc);
HGDIOBJ old_obj = ::SelectObject(dib_dc, dib);
size_t num_pixels = s.GetArea();
memset(bits, 0, num_pixels * 4);
::DrawIconEx(dib_dc, 0, 0, icon, s.width(), s.height(), 0, NULL, DI_MASK);
scoped_ptr<bool[]> opaque(new bool[num_pixels]);
for (size_t i = 0; i < num_pixels; ++i)
opaque[i] = !bits[i];
memset(bits, 0, num_pixels * 4);
::DrawIconEx(dib_dc, 0, 0, icon, s.width(), s.height(), 0, NULL, DI_NORMAL);
memcpy(bitmap.getPixels(), static_cast<void*>(bits), num_pixels * 4);
bool bitmap_has_alpha_channel = PixelsHaveAlpha(
static_cast<const uint32*>(bitmap.getPixels()), num_pixels);
if (!bitmap_has_alpha_channel) {
uint32* p = static_cast<uint32*>(bitmap.getPixels());
for (size_t i = 0; i < num_pixels; ++p, ++i) {
DCHECK_EQ((*p & 0xff000000), 0u);
if (opaque[i])
*p |= 0xff000000;
else
*p &= 0x00ffffff;
}
}
::SelectObject(dib_dc, old_obj);
::DeleteObject(dib);
::DeleteDC(dib_dc);
return bitmap;
}
bool IconUtil::CreateIconFileFromImageFamily(
const gfx::ImageFamily& image_family,
const base::FilePath& icon_path) {
gfx::ImageFamily resized_image_family;
if (!BuildResizedImageFamily(image_family, &resized_image_family))
return false;
std::vector<SkBitmap> bitmaps;
scoped_refptr<base::RefCountedMemory> png_bytes;
if (!ConvertImageFamilyToBitmaps(resized_image_family, &bitmaps, &png_bytes))
return false;
DCHECK(!bitmaps.empty());
size_t bitmap_count = bitmaps.size();
size_t image_count = bitmap_count + (png_bytes.get() ? 1 : 0);
size_t buffer_size = ComputeIconFileBufferSize(bitmaps);
if (png_bytes.get())
buffer_size += sizeof(ICONDIRENTRY) + png_bytes->size();
std::vector<uint8> buffer(buffer_size);
ICONDIR* icon_dir = reinterpret_cast<ICONDIR*>(&buffer[0]);
icon_dir->idType = kResourceTypeIcon;
icon_dir->idCount = static_cast<WORD>(image_count);
size_t icon_dir_count = image_count - 1;
size_t offset = sizeof(ICONDIR) + (sizeof(ICONDIRENTRY) * icon_dir_count);
for (size_t i = 0; i < bitmap_count; i++) {
ICONIMAGE* image = reinterpret_cast<ICONIMAGE*>(&buffer[offset]);
DCHECK_LT(offset, buffer_size);
size_t icon_image_size = 0;
SetSingleIconImageInformation(bitmaps[i], i, icon_dir, image, offset,
&icon_image_size);
DCHECK_GT(icon_image_size, 0U);
offset += icon_image_size;
}
if (png_bytes.get()) {
ICONDIRENTRY* entry = &icon_dir->idEntries[bitmap_count];
entry->bWidth = 0;
entry->bHeight = 0;
entry->wPlanes = 1;
entry->wBitCount = 32;
entry->dwBytesInRes = static_cast<DWORD>(png_bytes->size());
entry->dwImageOffset = static_cast<DWORD>(offset);
memcpy(&buffer[offset], png_bytes->front(), png_bytes->size());
offset += png_bytes->size();
}
DCHECK_EQ(offset, buffer_size);
std::string data(buffer.begin(), buffer.end());
return base::ImportantFileWriter::WriteFileAtomically(icon_path, data);
}
bool IconUtil::PixelsHaveAlpha(const uint32* pixels, size_t num_pixels) {
for (const uint32* end = pixels + num_pixels; pixels != end; ++pixels) {
if ((*pixels & 0xff000000) != 0)
return true;
}
return false;
}
void IconUtil::InitializeBitmapHeader(BITMAPV5HEADER* header, int width,
int height) {
DCHECK(header);
memset(header, 0, sizeof(BITMAPV5HEADER));
header->bV5Size = sizeof(BITMAPV5HEADER);
header->bV5Width = width;
header->bV5Height = -height;
header->bV5Planes = 1;
header->bV5Compression = BI_RGB;
header->bV5BitCount = 32;
header->bV5RedMask = 0x00FF0000;
header->bV5GreenMask = 0x0000FF00;
header->bV5BlueMask = 0x000000FF;
header->bV5AlphaMask = 0xFF000000;
header->bV5CSType = LCS_WINDOWS_COLOR_SPACE;
header->bV5Intent = LCS_GM_IMAGES;
}
void IconUtil::SetSingleIconImageInformation(const SkBitmap& bitmap,
size_t index,
ICONDIR* icon_dir,
ICONIMAGE* icon_image,
size_t image_offset,
size_t* image_byte_count) {
DCHECK(icon_dir != NULL);
DCHECK(icon_image != NULL);
DCHECK_GT(image_offset, 0U);
DCHECK(image_byte_count != NULL);
DCHECK_LT(bitmap.width(), kLargeIconSize);
DCHECK_LT(bitmap.height(), kLargeIconSize);
size_t xor_mask_size, bytes_in_resource;
ComputeBitmapSizeComponents(bitmap,
&xor_mask_size,
&bytes_in_resource);
icon_dir->idEntries[index].bWidth = static_cast<BYTE>(bitmap.width());
icon_dir->idEntries[index].bHeight = static_cast<BYTE>(bitmap.height());
icon_dir->idEntries[index].wPlanes = 1;
icon_dir->idEntries[index].wBitCount = 32;
icon_dir->idEntries[index].dwBytesInRes = bytes_in_resource;
icon_dir->idEntries[index].dwImageOffset = image_offset;
icon_image->icHeader.biSize = sizeof(BITMAPINFOHEADER);
icon_image->icHeader.biHeight = bitmap.height() * 2;
icon_image->icHeader.biWidth = bitmap.width();
icon_image->icHeader.biPlanes = 1;
icon_image->icHeader.biBitCount = 32;
unsigned char* image_addr = reinterpret_cast<unsigned char*>(icon_image);
unsigned char* xor_mask_addr = image_addr + sizeof(BITMAPINFOHEADER);
CopySkBitmapBitsIntoIconBuffer(bitmap, xor_mask_addr, xor_mask_size);
*image_byte_count = bytes_in_resource;
}
void IconUtil::CopySkBitmapBitsIntoIconBuffer(const SkBitmap& bitmap,
unsigned char* buffer,
size_t buffer_size) {
SkAutoLockPixels bitmap_lock(bitmap);
unsigned char* bitmap_ptr = static_cast<unsigned char*>(bitmap.getPixels());
size_t bitmap_size = bitmap.height() * bitmap.width() * 4;
DCHECK_EQ(buffer_size, bitmap_size);
for (size_t i = 0; i < bitmap_size; i += bitmap.width() * 4) {
memcpy(buffer + bitmap_size - bitmap.width() * 4 - i,
bitmap_ptr + i,
bitmap.width() * 4);
}
}
size_t IconUtil::ComputeIconFileBufferSize(const std::vector<SkBitmap>& set) {
DCHECK(!set.empty());
size_t total_buffer_size = sizeof(ICONDIR);
size_t bitmap_count = set.size();
total_buffer_size += sizeof(ICONDIRENTRY) * (bitmap_count - 1);
DCHECK_GE(bitmap_count, kNumIconDimensionsUpToMediumSize);
for (size_t i = 0; i < bitmap_count; i++) {
size_t xor_mask_size, bytes_in_resource;
ComputeBitmapSizeComponents(set[i],
&xor_mask_size,
&bytes_in_resource);
total_buffer_size += bytes_in_resource;
}
return total_buffer_size;
}
void IconUtil::ComputeBitmapSizeComponents(const SkBitmap& bitmap,
size_t* xor_mask_size,
size_t* bytes_in_resource) {
*xor_mask_size = bitmap.width() * bitmap.height() * 4;
size_t and_line_length = (bitmap.width() + 7) >> 3;
and_line_length = (and_line_length + 3) & ~3;
size_t and_mask_size = and_line_length * bitmap.height();
size_t masks_size = *xor_mask_size + and_mask_size;
*bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER);
}