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/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/files/important_file_writer.h" | 8 #include "base/files/important_file_writer.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/trace_event/trace_event.h" | |
11 #include "base/win/resource_util.h" | 12 #include "base/win/resource_util.h" |
12 #include "base/win/scoped_gdi_object.h" | 13 #include "base/win/scoped_gdi_object.h" |
13 #include "base/win/scoped_handle.h" | 14 #include "base/win/scoped_handle.h" |
14 #include "base/win/scoped_hdc.h" | 15 #include "base/win/scoped_hdc.h" |
15 #include "skia/ext/image_operations.h" | 16 #include "skia/ext/image_operations.h" |
16 #include "third_party/skia/include/core/SkBitmap.h" | 17 #include "third_party/skia/include/core/SkBitmap.h" |
17 #include "ui/gfx/gdi_util.h" | 18 #include "ui/gfx/gdi_util.h" |
18 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
19 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
20 #include "ui/gfx/image/image_family.h" | 21 #include "ui/gfx/image/image_family.h" |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
445 ::SelectObject(dib_dc, old_obj); | 446 ::SelectObject(dib_dc, old_obj); |
446 ::DeleteObject(dib); | 447 ::DeleteObject(dib); |
447 ::DeleteDC(dib_dc); | 448 ::DeleteDC(dib_dc); |
448 | 449 |
449 return bitmap; | 450 return bitmap; |
450 } | 451 } |
451 | 452 |
452 // static | 453 // static |
453 bool IconUtil::CreateIconFileFromImageFamily( | 454 bool IconUtil::CreateIconFileFromImageFamily( |
454 const gfx::ImageFamily& image_family, | 455 const gfx::ImageFamily& image_family, |
455 const base::FilePath& icon_path) { | 456 const base::FilePath& icon_path, |
457 WriteType write_type) { | |
456 // Creating a set of bitmaps corresponding to the icon images we'll end up | 458 // Creating a set of bitmaps corresponding to the icon images we'll end up |
457 // storing in the icon file. Each bitmap is created by resizing the most | 459 // storing in the icon file. Each bitmap is created by resizing the most |
458 // appropriate image from |image_family| to the desired size. | 460 // appropriate image from |image_family| to the desired size. |
459 gfx::ImageFamily resized_image_family; | 461 gfx::ImageFamily resized_image_family; |
460 if (!BuildResizedImageFamily(image_family, &resized_image_family)) | 462 if (!BuildResizedImageFamily(image_family, &resized_image_family)) |
461 return false; | 463 return false; |
462 | 464 |
463 std::vector<SkBitmap> bitmaps; | 465 std::vector<SkBitmap> bitmaps; |
464 scoped_refptr<base::RefCountedMemory> png_bytes; | 466 scoped_refptr<base::RefCountedMemory> png_bytes; |
465 if (!ConvertImageFamilyToBitmaps(resized_image_family, &bitmaps, &png_bytes)) | 467 if (!ConvertImageFamilyToBitmaps(resized_image_family, &bitmaps, &png_bytes)) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
509 entry->wPlanes = 1; | 511 entry->wPlanes = 1; |
510 entry->wBitCount = 32; | 512 entry->wBitCount = 32; |
511 entry->dwBytesInRes = static_cast<DWORD>(png_bytes->size()); | 513 entry->dwBytesInRes = static_cast<DWORD>(png_bytes->size()); |
512 entry->dwImageOffset = static_cast<DWORD>(offset); | 514 entry->dwImageOffset = static_cast<DWORD>(offset); |
513 memcpy(&buffer[offset], png_bytes->front(), png_bytes->size()); | 515 memcpy(&buffer[offset], png_bytes->front(), png_bytes->size()); |
514 offset += png_bytes->size(); | 516 offset += png_bytes->size(); |
515 } | 517 } |
516 | 518 |
517 DCHECK_EQ(offset, buffer_size); | 519 DCHECK_EQ(offset, buffer_size); |
518 | 520 |
519 std::string data(buffer.begin(), buffer.end()); | 521 if (write_type == NORMAL_WRITE) { |
520 return base::ImportantFileWriter::WriteFileAtomically(icon_path, data); | 522 auto savedSize = |
Alexei Svitkine (slow)
2015/09/30 21:26:23
saved_size
brucedawson
2015/09/30 22:13:59
Uggh. One of these days I will internalize the nam
| |
523 base::WriteFile(icon_path, reinterpret_cast<const char*>(&buffer[0]), | |
524 static_cast<int>(buffer.size())); | |
525 if (savedSize == static_cast<int>(buffer.size())) | |
526 return true; | |
527 bool delete_success = base::DeleteFile(icon_path, false); | |
528 DCHECK(delete_success); | |
529 return false; | |
530 } else { | |
531 std::string data(buffer.begin(), buffer.end()); | |
532 return base::ImportantFileWriter::WriteFileAtomically(icon_path, data); | |
533 } | |
521 } | 534 } |
522 | 535 |
523 bool IconUtil::PixelsHaveAlpha(const uint32* pixels, size_t num_pixels) { | 536 bool IconUtil::PixelsHaveAlpha(const uint32* pixels, size_t num_pixels) { |
524 for (const uint32* end = pixels + num_pixels; pixels != end; ++pixels) { | 537 for (const uint32* end = pixels + num_pixels; pixels != end; ++pixels) { |
525 if ((*pixels & 0xff000000) != 0) | 538 if ((*pixels & 0xff000000) != 0) |
526 return true; | 539 return true; |
527 } | 540 } |
528 | 541 |
529 return false; | 542 return false; |
530 } | 543 } |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
681 // Once we compute the size for a singe AND mask scan line, we multiply that | 694 // Once we compute the size for a singe AND mask scan line, we multiply that |
682 // number by the image height in order to get the total number of bytes for | 695 // number by the image height in order to get the total number of bytes for |
683 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes | 696 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes |
684 // for the monochrome bitmap representing the AND mask. | 697 // for the monochrome bitmap representing the AND mask. |
685 size_t and_line_length = (bitmap.width() + 7) >> 3; | 698 size_t and_line_length = (bitmap.width() + 7) >> 3; |
686 and_line_length = (and_line_length + 3) & ~3; | 699 and_line_length = (and_line_length + 3) & ~3; |
687 size_t and_mask_size = and_line_length * bitmap.height(); | 700 size_t and_mask_size = and_line_length * bitmap.height(); |
688 size_t masks_size = *xor_mask_size + and_mask_size; | 701 size_t masks_size = *xor_mask_size + and_mask_size; |
689 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); | 702 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); |
690 } | 703 } |
OLD | NEW |