Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: ui/gfx/icon_util.cc

Issue 15080002: Use important_file_writer to write .ico files (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/files/important_file_writer.h"
8 #include "base/logging.h" 9 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
10 #include "base/win/resource_util.h" 11 #include "base/win/resource_util.h"
11 #include "base/win/scoped_gdi_object.h" 12 #include "base/win/scoped_gdi_object.h"
12 #include "base/win/scoped_handle.h" 13 #include "base/win/scoped_handle.h"
13 #include "base/win/scoped_hdc.h" 14 #include "base/win/scoped_hdc.h"
14 #include "skia/ext/image_operations.h" 15 #include "skia/ext/image_operations.h"
15 #include "third_party/skia/include/core/SkBitmap.h" 16 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/gfx/gdi_util.h" 17 #include "ui/gfx/gdi_util.h"
17 #include "ui/gfx/image/image.h" 18 #include "ui/gfx/image/image.h"
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 if (!ConvertImageFamilyToBitmaps(resized_image_family, &bitmaps, &png_bytes)) 464 if (!ConvertImageFamilyToBitmaps(resized_image_family, &bitmaps, &png_bytes))
464 return false; 465 return false;
465 466
466 // Guaranteed true because BuildResizedImageFamily will provide at least one 467 // Guaranteed true because BuildResizedImageFamily will provide at least one
467 // image < 256x256. 468 // image < 256x256.
468 DCHECK(!bitmaps.empty()); 469 DCHECK(!bitmaps.empty());
469 size_t bitmap_count = bitmaps.size(); // Not including PNG image. 470 size_t bitmap_count = bitmaps.size(); // Not including PNG image.
470 // Including PNG image, if any. 471 // Including PNG image, if any.
471 size_t image_count = bitmap_count + (png_bytes.get() ? 1 : 0); 472 size_t image_count = bitmap_count + (png_bytes.get() ? 1 : 0);
472 473
473 // Now that basic checks are done, we can create the file.
474 base::win::ScopedHandle icon_file(::CreateFile(icon_path.value().c_str(),
475 GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL));
476
477 if (!icon_file.IsValid())
478 return false;
479
480 // Computing the total size of the buffer we need in order to store the 474 // Computing the total size of the buffer we need in order to store the
481 // images in the desired icon format. 475 // images in the desired icon format.
482 size_t buffer_size = ComputeIconFileBufferSize(bitmaps); 476 size_t buffer_size = ComputeIconFileBufferSize(bitmaps);
483 // Account for the bytes needed for the PNG entry. 477 // Account for the bytes needed for the PNG entry.
484 if (png_bytes.get()) 478 if (png_bytes.get())
485 buffer_size += sizeof(ICONDIRENTRY) + png_bytes->size(); 479 buffer_size += sizeof(ICONDIRENTRY) + png_bytes->size();
486 480
487 // Setting the information in the structures residing within the buffer. 481 // Setting the information in the structures residing within the buffer.
488 // First, we set the information which doesn't require iterating through the 482 // First, we set the information which doesn't require iterating through the
489 // bitmap set and then we set the bitmap specific structures. In the latter 483 // bitmap set and then we set the bitmap specific structures. In the latter
(...skipping 24 matching lines...) Expand all
514 entry->wPlanes = 1; 508 entry->wPlanes = 1;
515 entry->wBitCount = 32; 509 entry->wBitCount = 32;
516 entry->dwBytesInRes = static_cast<DWORD>(png_bytes->size()); 510 entry->dwBytesInRes = static_cast<DWORD>(png_bytes->size());
517 entry->dwImageOffset = static_cast<DWORD>(offset); 511 entry->dwImageOffset = static_cast<DWORD>(offset);
518 memcpy(&buffer[offset], png_bytes->front(), png_bytes->size()); 512 memcpy(&buffer[offset], png_bytes->front(), png_bytes->size());
519 offset += png_bytes->size(); 513 offset += png_bytes->size();
520 } 514 }
521 515
522 DCHECK_EQ(offset, buffer_size); 516 DCHECK_EQ(offset, buffer_size);
523 517
524 // Finally, write the data to the file. 518 std::string data(buffer.begin(), buffer.end());
525 DWORD bytes_written; 519 return base::ImportantFileWriter::WriteFileAtomically(icon_path, data);
526 bool delete_file = false;
527 if (!WriteFile(icon_file.Get(), &buffer[0], buffer_size, &bytes_written,
528 NULL) ||
529 bytes_written != buffer_size) {
530 delete_file = true;
531 }
532
533 ::CloseHandle(icon_file.Take());
534 if (delete_file) {
535 bool success = file_util::Delete(icon_path, false);
536 DCHECK(success);
537 }
538
539 return !delete_file;
540 } 520 }
541 521
542 bool IconUtil::PixelsHaveAlpha(const uint32* pixels, size_t num_pixels) { 522 bool IconUtil::PixelsHaveAlpha(const uint32* pixels, size_t num_pixels) {
543 for (const uint32* end = pixels + num_pixels; pixels != end; ++pixels) { 523 for (const uint32* end = pixels + num_pixels; pixels != end; ++pixels) {
544 if ((*pixels & 0xff000000) != 0) 524 if ((*pixels & 0xff000000) != 0)
545 return true; 525 return true;
546 } 526 }
547 527
548 return false; 528 return false;
549 } 529 }
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
700 // Once we compute the size for a singe AND mask scan line, we multiply that 680 // Once we compute the size for a singe AND mask scan line, we multiply that
701 // number by the image height in order to get the total number of bytes for 681 // number by the image height in order to get the total number of bytes for
702 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes 682 // the AND mask. Thus, for a 15X15 image, we need 15 * 4 which is 60 bytes
703 // for the monochrome bitmap representing the AND mask. 683 // for the monochrome bitmap representing the AND mask.
704 size_t and_line_length = (bitmap.width() + 7) >> 3; 684 size_t and_line_length = (bitmap.width() + 7) >> 3;
705 and_line_length = (and_line_length + 3) & ~3; 685 and_line_length = (and_line_length + 3) & ~3;
706 size_t and_mask_size = and_line_length * bitmap.height(); 686 size_t and_mask_size = and_line_length * bitmap.height();
707 size_t masks_size = *xor_mask_size + and_mask_size; 687 size_t masks_size = *xor_mask_size + and_mask_size;
708 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER); 688 *bytes_in_resource = masks_size + sizeof(BITMAPINFOHEADER);
709 } 689 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698