diff options
-rw-r--r-- | ui/gfx/codec/png_codec.cc | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/ui/gfx/codec/png_codec.cc b/ui/gfx/codec/png_codec.cc index 3b3a8c99..1ca139d 100644 --- a/ui/gfx/codec/png_codec.cc +++ b/ui/gfx/codec/png_codec.cc @@ -359,6 +359,25 @@ class PngReadStructDestroyer { private: png_struct** ps_; png_info** pi_; + DISALLOW_COPY_AND_ASSIGN(PngReadStructDestroyer); +}; + +// Automatically destroys the given write structs on destruction to make +// cleanup and error handling code cleaner. +class PngWriteStructDestroyer { + public: + explicit PngWriteStructDestroyer(png_struct** ps) : ps_(ps), pi_(0) { + } + ~PngWriteStructDestroyer() { + png_destroy_write_struct(ps_, pi_); + } + void SetInfoStruct(png_info** pi) { + pi_ = pi; + } + private: + png_struct** ps_; + png_info** pi_; + DISALLOW_COPY_AND_ASSIGN(PngWriteStructDestroyer); }; bool BuildPNGStruct(const unsigned char* input, size_t input_size, @@ -746,18 +765,17 @@ bool PNGCodec::EncodeWithCompressionLevel(const unsigned char* input, NULL, NULL, NULL); if (!png_ptr) return false; + PngWriteStructDestroyer destroyer(&png_ptr); png_info* info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) { - png_destroy_write_struct(&png_ptr, NULL); + if (!info_ptr) return false; - } + destroyer.SetInfoStruct(&info_ptr); PngEncoderState state(output); bool success = DoLibpngWrite(png_ptr, info_ptr, &state, size.width(), size.height(), row_byte_width, input, compression_level, png_output_color_type, output_color_components, converter, comments); - png_destroy_write_struct(&png_ptr, &info_ptr); return success; } |