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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
321 } | 321 } |
322 ~PngReadStructDestroyer() { | 322 ~PngReadStructDestroyer() { |
323 png_destroy_read_struct(ps_, pi_, NULL); | 323 png_destroy_read_struct(ps_, pi_, NULL); |
324 } | 324 } |
325 private: | 325 private: |
326 png_struct** ps_; | 326 png_struct** ps_; |
327 png_info** pi_; | 327 png_info** pi_; |
328 DISALLOW_COPY_AND_ASSIGN(PngReadStructDestroyer); | 328 DISALLOW_COPY_AND_ASSIGN(PngReadStructDestroyer); |
329 }; | 329 }; |
330 | 330 |
331 // Automatically destroys the given write structs on destruction to make | 331 // Holds png struct and info ensuring the proper destruction. |
332 // cleanup and error handling code cleaner. | 332 class PngWriteStructInfo { |
333 class PngWriteStructDestroyer { | |
334 public: | 333 public: |
335 explicit PngWriteStructDestroyer(png_struct** ps) : ps_(ps), pi_(0) { | 334 explicit PngWriteStructInfo() : png_str_(0), info_ptr_(0) { |
Nico
2016/12/14 23:47:52
(minor nit: "str" means "string" for me, not struc
dcheng
2016/12/14 23:48:52
Minor nit: no explicit, and use nullptr
krasin1
2016/12/14 23:53:47
Done.
| |
336 } | 335 } |
337 ~PngWriteStructDestroyer() { | 336 |
338 png_destroy_write_struct(ps_, pi_); | 337 ~PngWriteStructInfo() { |
338 png_destroy_write_struct(&png_str_, &info_ptr_); | |
339 } | 339 } |
340 void SetInfoStruct(png_info** pi) { | 340 |
341 pi_ = pi; | 341 png_struct* png_str_; |
342 } | 342 png_info* info_ptr_; |
343 private: | 343 private: |
344 png_struct** ps_; | 344 DISALLOW_COPY_AND_ASSIGN(PngWriteStructInfo); |
345 png_info** pi_; | |
346 DISALLOW_COPY_AND_ASSIGN(PngWriteStructDestroyer); | |
347 }; | 345 }; |
348 | 346 |
349 bool BuildPNGStruct(const unsigned char* input, size_t input_size, | 347 bool BuildPNGStruct(const unsigned char* input, size_t input_size, |
350 png_struct** png_ptr, png_info** info_ptr) { | 348 png_struct** png_ptr, png_info** info_ptr) { |
351 if (input_size < 8) | 349 if (input_size < 8) |
352 return false; // Input data too small to be a png | 350 return false; // Input data too small to be a png |
353 | 351 |
354 // Have libpng check the signature, it likes the first 8 bytes. | 352 // Have libpng check the signature, it likes the first 8 bytes. |
355 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0) | 353 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0) |
356 return false; | 354 return false; |
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
701 break; | 699 break; |
702 | 700 |
703 default: | 701 default: |
704 NOTREACHED() << "Unknown pixel format"; | 702 NOTREACHED() << "Unknown pixel format"; |
705 return false; | 703 return false; |
706 } | 704 } |
707 | 705 |
708 // Row stride should be at least as long as the length of the data. | 706 // Row stride should be at least as long as the length of the data. |
709 DCHECK(input_color_components * size.width() <= row_byte_width); | 707 DCHECK(input_color_components * size.width() <= row_byte_width); |
710 | 708 |
711 png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, | 709 PngWriteStructInfo si; |
712 NULL, NULL, NULL); | 710 si.png_str_ = png_create_write_struct(PNG_LIBPNG_VER_STRING, |
713 if (!png_ptr) | 711 NULL, NULL, NULL); |
712 if (!si.png_str_) | |
714 return false; | 713 return false; |
715 PngWriteStructDestroyer destroyer(&png_ptr); | 714 |
716 png_info* info_ptr = png_create_info_struct(png_ptr); | 715 si.info_ptr_ = png_create_info_struct(si.png_str_); |
717 if (!info_ptr) | 716 if (!si.info_ptr_) |
718 return false; | 717 return false; |
719 destroyer.SetInfoStruct(&info_ptr); | |
720 | 718 |
721 output->clear(); | 719 output->clear(); |
722 | 720 |
723 PngEncoderState state(output); | 721 PngEncoderState state(output); |
724 bool success = DoLibpngWrite(png_ptr, info_ptr, &state, | 722 bool success = DoLibpngWrite(png_ptr, info_ptr, &state, |
725 size.width(), size.height(), row_byte_width, | 723 size.width(), size.height(), row_byte_width, |
726 input, compression_level, png_output_color_type, | 724 input, compression_level, png_output_color_type, |
727 output_color_components, converter, comments); | 725 output_color_components, converter, comments); |
728 | 726 |
729 return success; | 727 return success; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
804 } | 802 } |
805 | 803 |
806 PNGCodec::Comment::Comment(const std::string& k, const std::string& t) | 804 PNGCodec::Comment::Comment(const std::string& k, const std::string& t) |
807 : key(k), text(t) { | 805 : key(k), text(t) { |
808 } | 806 } |
809 | 807 |
810 PNGCodec::Comment::~Comment() { | 808 PNGCodec::Comment::~Comment() { |
811 } | 809 } |
812 | 810 |
813 } // namespace gfx | 811 } // namespace gfx |
OLD | NEW |