Chromium Code Reviews| 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/codec/png_codec.h" | 5 #include "ui/gfx/codec/png_codec.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "ui/gfx/size.h" | 10 #include "ui/gfx/size.h" |
| (...skipping 343 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 354 PngReadStructDestroyer(png_struct** ps, png_info** pi) : ps_(ps), pi_(pi) { | 354 PngReadStructDestroyer(png_struct** ps, png_info** pi) : ps_(ps), pi_(pi) { |
| 355 } | 355 } |
| 356 ~PngReadStructDestroyer() { | 356 ~PngReadStructDestroyer() { |
| 357 png_destroy_read_struct(ps_, pi_, NULL); | 357 png_destroy_read_struct(ps_, pi_, NULL); |
| 358 } | 358 } |
| 359 private: | 359 private: |
| 360 png_struct** ps_; | 360 png_struct** ps_; |
| 361 png_info** pi_; | 361 png_info** pi_; |
| 362 }; | 362 }; |
| 363 | 363 |
| 364 // Automatically destroys the given write structs on destruction to make | |
| 365 // cleanup and error handling code cleaner. | |
| 366 class PngWriteStructDestroyer { | |
| 367 public: | |
| 368 explicit PngWriteStructDestroyer(png_struct** ps) : ps_(ps), pi_(0) { | |
| 369 } | |
| 370 ~PngWriteStructDestroyer() { | |
| 371 png_destroy_write_struct(ps_, pi_); | |
| 372 } | |
| 373 void SetInfoStruct(png_info** pi) { | |
| 374 pi_ = pi; | |
| 375 } | |
| 376 private: | |
| 377 png_struct** ps_; | |
| 378 png_info** pi_; | |
| 379 }; | |
|
sky
2012/04/02 15:35:01
DISALLOW_COPY_AND_ASSIGN
| |
| 380 | |
| 364 bool BuildPNGStruct(const unsigned char* input, size_t input_size, | 381 bool BuildPNGStruct(const unsigned char* input, size_t input_size, |
| 365 png_struct** png_ptr, png_info** info_ptr) { | 382 png_struct** png_ptr, png_info** info_ptr) { |
| 366 if (input_size < 8) | 383 if (input_size < 8) |
| 367 return false; // Input data too small to be a png | 384 return false; // Input data too small to be a png |
| 368 | 385 |
| 369 // Have libpng check the signature, it likes the first 8 bytes. | 386 // Have libpng check the signature, it likes the first 8 bytes. |
| 370 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0) | 387 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0) |
| 371 return false; | 388 return false; |
| 372 | 389 |
| 373 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); | 390 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 739 return false; | 756 return false; |
| 740 } | 757 } |
| 741 | 758 |
| 742 // Row stride should be at least as long as the length of the data. | 759 // Row stride should be at least as long as the length of the data. |
| 743 DCHECK(input_color_components * size.width() <= row_byte_width); | 760 DCHECK(input_color_components * size.width() <= row_byte_width); |
| 744 | 761 |
| 745 png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, | 762 png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, |
| 746 NULL, NULL, NULL); | 763 NULL, NULL, NULL); |
| 747 if (!png_ptr) | 764 if (!png_ptr) |
| 748 return false; | 765 return false; |
| 766 PngWriteStructDestroyer destroyer(&png_ptr); | |
| 749 png_info* info_ptr = png_create_info_struct(png_ptr); | 767 png_info* info_ptr = png_create_info_struct(png_ptr); |
| 750 if (!info_ptr) { | 768 if (!info_ptr) |
| 751 png_destroy_write_struct(&png_ptr, NULL); | |
| 752 return false; | 769 return false; |
| 753 } | 770 destroyer.SetInfoStruct(&info_ptr); |
| 754 | 771 |
| 755 PngEncoderState state(output); | 772 PngEncoderState state(output); |
| 756 bool success = DoLibpngWrite(png_ptr, info_ptr, &state, | 773 bool success = DoLibpngWrite(png_ptr, info_ptr, &state, |
| 757 size.width(), size.height(), row_byte_width, | 774 size.width(), size.height(), row_byte_width, |
| 758 input, compression_level, png_output_color_type, | 775 input, compression_level, png_output_color_type, |
| 759 output_color_components, converter, comments); | 776 output_color_components, converter, comments); |
| 760 png_destroy_write_struct(&png_ptr, &info_ptr); | |
| 761 | 777 |
| 762 return success; | 778 return success; |
| 763 } | 779 } |
| 764 | 780 |
| 765 // static | 781 // static |
| 766 bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input, | 782 bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input, |
| 767 bool discard_transparency, | 783 bool discard_transparency, |
| 768 std::vector<unsigned char>* output) { | 784 std::vector<unsigned char>* output) { |
| 769 static const int bbp = 4; | 785 static const int bbp = 4; |
| 770 | 786 |
| 771 SkAutoLockPixels lock_input(input); | 787 SkAutoLockPixels lock_input(input); |
| 772 DCHECK(input.empty() || input.bytesPerPixel() == bbp); | 788 DCHECK(input.empty() || input.bytesPerPixel() == bbp); |
| 773 | 789 |
| 774 return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), | 790 return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), |
| 775 FORMAT_SkBitmap, Size(input.width(), input.height()), | 791 FORMAT_SkBitmap, Size(input.width(), input.height()), |
| 776 input.width() * bbp, discard_transparency, | 792 input.width() * bbp, discard_transparency, |
| 777 std::vector<Comment>(), output); | 793 std::vector<Comment>(), output); |
| 778 } | 794 } |
| 779 | 795 |
| 780 PNGCodec::Comment::Comment(const std::string& k, const std::string& t) | 796 PNGCodec::Comment::Comment(const std::string& k, const std::string& t) |
| 781 : key(k), text(t) { | 797 : key(k), text(t) { |
| 782 } | 798 } |
| 783 | 799 |
| 784 PNGCodec::Comment::~Comment() { | 800 PNGCodec::Comment::~Comment() { |
| 785 } | 801 } |
| 786 | 802 |
| 787 } // namespace gfx | 803 } // namespace gfx |
| OLD | NEW |