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

Side by Side Diff: ui/gfx/codec/png_codec.cc

Issue 9959061: Use RAII for write libpng structs. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 8 years, 8 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 | Annotate | Revision Log
« 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/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 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 class PngReadStructDestroyer { 352 class PngReadStructDestroyer {
353 public: 353 public:
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 DISALLOW_COPY_AND_ASSIGN(PngReadStructDestroyer);
363 };
364
365 // Automatically destroys the given write structs on destruction to make
366 // cleanup and error handling code cleaner.
367 class PngWriteStructDestroyer {
368 public:
369 explicit PngWriteStructDestroyer(png_struct** ps) : ps_(ps), pi_(0) {
370 }
371 ~PngWriteStructDestroyer() {
372 png_destroy_write_struct(ps_, pi_);
373 }
374 void SetInfoStruct(png_info** pi) {
375 pi_ = pi;
376 }
377 private:
378 png_struct** ps_;
379 png_info** pi_;
380 DISALLOW_COPY_AND_ASSIGN(PngWriteStructDestroyer);
362 }; 381 };
363 382
364 bool BuildPNGStruct(const unsigned char* input, size_t input_size, 383 bool BuildPNGStruct(const unsigned char* input, size_t input_size,
365 png_struct** png_ptr, png_info** info_ptr) { 384 png_struct** png_ptr, png_info** info_ptr) {
366 if (input_size < 8) 385 if (input_size < 8)
367 return false; // Input data too small to be a png 386 return false; // Input data too small to be a png
368 387
369 // Have libpng check the signature, it likes the first 8 bytes. 388 // Have libpng check the signature, it likes the first 8 bytes.
370 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0) 389 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0)
371 return false; 390 return false;
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
739 return false; 758 return false;
740 } 759 }
741 760
742 // Row stride should be at least as long as the length of the data. 761 // Row stride should be at least as long as the length of the data.
743 DCHECK(input_color_components * size.width() <= row_byte_width); 762 DCHECK(input_color_components * size.width() <= row_byte_width);
744 763
745 png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 764 png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
746 NULL, NULL, NULL); 765 NULL, NULL, NULL);
747 if (!png_ptr) 766 if (!png_ptr)
748 return false; 767 return false;
768 PngWriteStructDestroyer destroyer(&png_ptr);
749 png_info* info_ptr = png_create_info_struct(png_ptr); 769 png_info* info_ptr = png_create_info_struct(png_ptr);
750 if (!info_ptr) { 770 if (!info_ptr)
751 png_destroy_write_struct(&png_ptr, NULL);
752 return false; 771 return false;
753 } 772 destroyer.SetInfoStruct(&info_ptr);
754 773
755 PngEncoderState state(output); 774 PngEncoderState state(output);
756 bool success = DoLibpngWrite(png_ptr, info_ptr, &state, 775 bool success = DoLibpngWrite(png_ptr, info_ptr, &state,
757 size.width(), size.height(), row_byte_width, 776 size.width(), size.height(), row_byte_width,
758 input, compression_level, png_output_color_type, 777 input, compression_level, png_output_color_type,
759 output_color_components, converter, comments); 778 output_color_components, converter, comments);
760 png_destroy_write_struct(&png_ptr, &info_ptr);
761 779
762 return success; 780 return success;
763 } 781 }
764 782
765 // static 783 // static
766 bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input, 784 bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input,
767 bool discard_transparency, 785 bool discard_transparency,
768 std::vector<unsigned char>* output) { 786 std::vector<unsigned char>* output) {
769 static const int bbp = 4; 787 static const int bbp = 4;
770 788
771 SkAutoLockPixels lock_input(input); 789 SkAutoLockPixels lock_input(input);
772 DCHECK(input.empty() || input.bytesPerPixel() == bbp); 790 DCHECK(input.empty() || input.bytesPerPixel() == bbp);
773 791
774 return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), 792 return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)),
775 FORMAT_SkBitmap, Size(input.width(), input.height()), 793 FORMAT_SkBitmap, Size(input.width(), input.height()),
776 input.width() * bbp, discard_transparency, 794 input.width() * bbp, discard_transparency,
777 std::vector<Comment>(), output); 795 std::vector<Comment>(), output);
778 } 796 }
779 797
780 PNGCodec::Comment::Comment(const std::string& k, const std::string& t) 798 PNGCodec::Comment::Comment(const std::string& k, const std::string& t)
781 : key(k), text(t) { 799 : key(k), text(t) {
782 } 800 }
783 801
784 PNGCodec::Comment::~Comment() { 802 PNGCodec::Comment::~Comment() {
785 } 803 }
786 804
787 } // namespace gfx 805 } // namespace gfx
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