Chromium Code Reviews| Index: ui/gfx/codec/png_codec.cc |
| =================================================================== |
| --- ui/gfx/codec/png_codec.cc (revision 130053) |
| +++ ui/gfx/codec/png_codec.cc (working copy) |
| @@ -361,6 +361,23 @@ |
| png_info** pi_; |
| }; |
| +// 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_; |
| +}; |
|
sky
2012/04/02 15:35:01
DISALLOW_COPY_AND_ASSIGN
|
| + |
| bool BuildPNGStruct(const unsigned char* input, size_t input_size, |
| png_struct** png_ptr, png_info** info_ptr) { |
| if (input_size < 8) |
| @@ -746,18 +763,17 @@ |
| 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; |
| } |