OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/icon_util.h" | 5 #include "ui/gfx/icon_util.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/win/scoped_gdi_object.h" | 10 #include "base/win/scoped_gdi_object.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 SkAutoLockPixels bitmap_lock(bitmap); | 290 SkAutoLockPixels bitmap_lock(bitmap); |
291 if ((bitmap.config() != SkBitmap::kARGB_8888_Config) || | 291 if ((bitmap.config() != SkBitmap::kARGB_8888_Config) || |
292 (bitmap.height() <= 0) || (bitmap.width() <= 0) || | 292 (bitmap.height() <= 0) || (bitmap.width() <= 0) || |
293 (bitmap.getPixels() == NULL)) { | 293 (bitmap.getPixels() == NULL)) { |
294 return false; | 294 return false; |
295 } | 295 } |
296 | 296 |
297 // If |large_bitmap| was specified, validate its dimension and convert to PNG. | 297 // If |large_bitmap| was specified, validate its dimension and convert to PNG. |
298 scoped_refptr<base::RefCountedMemory> png_bytes; | 298 scoped_refptr<base::RefCountedMemory> png_bytes; |
299 if (!large_bitmap.empty()) { | 299 if (!large_bitmap.empty()) { |
300 DCHECK_EQ(256, large_bitmap.width()); | 300 CHECK_EQ(256, large_bitmap.width()); |
301 DCHECK_EQ(256, large_bitmap.height()); | 301 CHECK_EQ(256, large_bitmap.height()); |
302 png_bytes = gfx::Image(large_bitmap).As1xPNGBytes(); | 302 png_bytes = gfx::Image(large_bitmap).As1xPNGBytes(); |
303 } | 303 } |
304 | 304 |
305 // We start by creating the file. | 305 // We start by creating the file. |
306 base::win::ScopedHandle icon_file(::CreateFile(icon_path.value().c_str(), | 306 base::win::ScopedHandle icon_file(::CreateFile(icon_path.value().c_str(), |
307 GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)); | 307 GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)); |
308 | 308 |
309 if (!icon_file.IsValid()) | 309 if (!icon_file.IsValid()) |
310 return false; | 310 return false; |
311 | 311 |
(...skipping 12 matching lines...) Expand all Loading... |
324 if (png_bytes.get()) | 324 if (png_bytes.get()) |
325 buffer_size += sizeof(ICONDIRENTRY) + png_bytes->size(); | 325 buffer_size += sizeof(ICONDIRENTRY) + png_bytes->size(); |
326 | 326 |
327 // Setting the information in the structures residing within the buffer. | 327 // Setting the information in the structures residing within the buffer. |
328 // First, we set the information which doesn't require iterating through the | 328 // First, we set the information which doesn't require iterating through the |
329 // bitmap set and then we set the bitmap specific structures. In the latter | 329 // bitmap set and then we set the bitmap specific structures. In the latter |
330 // step we also copy the actual bits. | 330 // step we also copy the actual bits. |
331 std::vector<uint8> buffer(buffer_size); | 331 std::vector<uint8> buffer(buffer_size); |
332 ICONDIR* icon_dir = reinterpret_cast<ICONDIR*>(&buffer[0]); | 332 ICONDIR* icon_dir = reinterpret_cast<ICONDIR*>(&buffer[0]); |
333 icon_dir->idType = kResourceTypeIcon; | 333 icon_dir->idType = kResourceTypeIcon; |
334 icon_dir->idCount = bitmap_count; | 334 icon_dir->idCount = static_cast<WORD>(bitmap_count); |
335 size_t icon_dir_count = bitmap_count - 1; // Note DCHECK(!bitmaps.empty())! | 335 size_t icon_dir_count = bitmap_count - 1; // Note DCHECK(!bitmaps.empty())! |
336 | 336 |
337 // Increment counts if a PNG entry will be added. | 337 // Increment counts if a PNG entry will be added. |
338 if (png_bytes.get()) { | 338 if (png_bytes.get()) { |
339 icon_dir->idCount++; | 339 icon_dir->idCount++; |
340 icon_dir_count++; | 340 icon_dir_count++; |
341 } | 341 } |
342 | 342 |
343 size_t offset = sizeof(ICONDIR) + (sizeof(ICONDIRENTRY) * icon_dir_count); | 343 size_t offset = sizeof(ICONDIR) + (sizeof(ICONDIRENTRY) * icon_dir_count); |
344 for (size_t i = 0; i < bitmap_count; i++) { | 344 for (size_t i = 0; i < bitmap_count; i++) { |
345 ICONIMAGE* image = reinterpret_cast<ICONIMAGE*>(&buffer[offset]); | 345 ICONIMAGE* image = reinterpret_cast<ICONIMAGE*>(&buffer[offset]); |
346 DCHECK_LT(offset, buffer_size); | 346 DCHECK_LT(offset, buffer_size); |
347 size_t icon_image_size = 0; | 347 size_t icon_image_size = 0; |
348 SetSingleIconImageInformation(bitmaps[i], i, icon_dir, image, offset, | 348 SetSingleIconImageInformation(bitmaps[i], i, icon_dir, image, offset, |
349 &icon_image_size); | 349 &icon_image_size); |
350 DCHECK_GT(icon_image_size, 0U); | 350 DCHECK_GT(icon_image_size, 0U); |
351 offset += icon_image_size; | 351 offset += icon_image_size; |
352 } | 352 } |
353 | 353 |
354 // Add the PNG entry, if necessary. | 354 // Add the PNG entry, if necessary. |
355 if (png_bytes.get()) { | 355 if (png_bytes.get()) { |
356 ICONDIRENTRY* entry = &icon_dir->idEntries[bitmap_count]; | 356 ICONDIRENTRY* entry = &icon_dir->idEntries[bitmap_count]; |
357 entry->bWidth = 0; | 357 entry->bWidth = 0; |
358 entry->bHeight = 0; | 358 entry->bHeight = 0; |
359 entry->wPlanes = 1; | 359 entry->wPlanes = 1; |
360 entry->wBitCount = 32; | 360 entry->wBitCount = 32; |
361 entry->dwBytesInRes = png_bytes->size(); | 361 entry->dwBytesInRes = static_cast<DWORD>(png_bytes->size()); |
362 entry->dwImageOffset = offset; | 362 entry->dwImageOffset = static_cast<DWORD>(offset); |
363 memcpy(&buffer[offset], png_bytes->front(), png_bytes->size()); | 363 memcpy(&buffer[offset], png_bytes->front(), png_bytes->size()); |
364 offset += png_bytes->size(); | 364 offset += png_bytes->size(); |
365 } | 365 } |
366 | 366 |
367 DCHECK_EQ(offset, buffer_size); | 367 DCHECK_EQ(offset, buffer_size); |
368 | 368 |
369 // Finally, write the data to the file. | 369 // Finally, write the data to the file. |
370 DWORD bytes_written; | 370 DWORD bytes_written; |
371 bool delete_file = false; | 371 bool delete_file = false; |
372 if (!WriteFile(icon_file.Get(), &buffer[0], buffer_size, &bytes_written, | 372 if (!WriteFile(icon_file.Get(), &buffer[0], buffer_size, &bytes_written, |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 // Once we compute the size for a singe AND mask scan line, we multiply that | 577 // Once we compute the size for a singe AND mask scan line, we multiply that |
578 // number by the image height in order to get the total number of bytes for | 578 // number by the image height in order to get the total number of bytes for |
579 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes | 579 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes |
580 // for the monochrome bitmap representing the AND mask. | 580 // for the monochrome bitmap representing the AND mask. |
581 size_t and_line_length = (bitmap.width() + 7) >> 3; | 581 size_t and_line_length = (bitmap.width() + 7) >> 3; |
582 and_line_length = (and_line_length + 3) & ~3; | 582 and_line_length = (and_line_length + 3) & ~3; |
583 size_t and_mask_size = and_line_length * bitmap.height(); | 583 size_t and_mask_size = and_line_length * bitmap.height(); |
584 size_t masks_size = *xor_mask_size + and_mask_size; | 584 size_t masks_size = *xor_mask_size + and_mask_size; |
585 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); | 585 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); |
586 } | 586 } |
OLD | NEW |