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

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

Issue 6696085: Add the ability to write comments to PNGCodec::Encode. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: strdup Created 9 years, 9 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
OLDNEW
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/scoped_ptr.h" 8 #include "base/scoped_ptr.h"
9 #include "base/string_util.h"
10 #include "ui/gfx/size.h"
9 #include "third_party/skia/include/core/SkBitmap.h" 11 #include "third_party/skia/include/core/SkBitmap.h"
10 #include "third_party/skia/include/core/SkUnPreMultiply.h" 12 #include "third_party/skia/include/core/SkUnPreMultiply.h"
11 #include "third_party/skia/include/core/SkColorPriv.h" 13 #include "third_party/skia/include/core/SkColorPriv.h"
12 14
13 extern "C" { 15 extern "C" {
14 #if defined(USE_SYSTEM_LIBPNG) 16 #if defined(USE_SYSTEM_LIBPNG)
15 #include <png.h> 17 #include <png.h>
16 #else 18 #else
17 #include "third_party/libpng/png.h" 19 #include "third_party/libpng/png.h"
18 #endif 20 #endif
(...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 unsigned char* rgb, bool* is_opaque) { 541 unsigned char* rgb, bool* is_opaque) {
540 for (int x = 0; x < pixel_width; x++) { 542 for (int x = 0; x < pixel_width; x++) {
541 const unsigned char* pixel_in = &bgra[x * 4]; 543 const unsigned char* pixel_in = &bgra[x * 4];
542 unsigned char* pixel_out = &rgb[x * 3]; 544 unsigned char* pixel_out = &rgb[x * 3];
543 pixel_out[0] = pixel_in[2]; 545 pixel_out[0] = pixel_in[2];
544 pixel_out[1] = pixel_in[1]; 546 pixel_out[1] = pixel_in[1];
545 pixel_out[2] = pixel_in[0]; 547 pixel_out[2] = pixel_in[0];
546 } 548 }
547 } 549 }
548 550
551 #ifdef PNG_TEXT_SUPPORTED
552 class CommentWriter {
553 public:
554 explicit CommentWriter(const std::vector<PNGCodec::Comment>& comments)
555 : comments_(comments),
556 png_text_(new png_text[comments.size()]) {
557 for (size_t i = 0; i < comments.size(); ++i)
558 AddComment(i, comments[i]);
559 }
560
561 ~CommentWriter() {
562 for (size_t i = 0; i < comments_.size(); ++i) {
563 free(png_text_[i].key);
564 free(png_text_[i].text);
565 }
566 delete [] png_text_;
567 }
568
569 bool HasComments() {
570 return !comments_.empty();
571 }
572
573 png_text* get_png_text() {
574 return png_text_;
575 }
576
577 int size() {
578 return static_cast<int>(comments_.size());
579 }
580
581 private:
582 void AddComment(size_t pos, const PNGCodec::Comment& comment) {
583 png_text_[pos].compression = PNG_TEXT_COMPRESSION_NONE;
584 // A PNG comment's key can only be 79 characters long.
585 DCHECK(comment.key.length() < 79);
586 png_text_[pos].key = base::strdup(comment.key.substr(0, 78).c_str());
587 png_text_[pos].text = base::strdup(comment.text.c_str());
588 png_text_[pos].text_length = comment.text.length();
589 #ifdef PNG_iTXt_SUPPORTED
590 png_text_[pos].itxt_length = 0;
591 png_text_[pos].lang = 0;
592 png_text_[pos].lang_key = 0;
593 #endif
594 }
595
596 DISALLOW_COPY_AND_ASSIGN(CommentWriter);
597
598 const std::vector<PNGCodec::Comment> comments_;
599 png_text* png_text_;
600 };
601 #endif // PNG_TEXT_SUPPORTED
602
549 // The type of functions usable for converting between pixel formats. 603 // The type of functions usable for converting between pixel formats.
550 typedef void (*FormatConverter)(const unsigned char* in, int w, 604 typedef void (*FormatConverter)(const unsigned char* in, int w,
551 unsigned char* out, bool* is_opaque); 605 unsigned char* out, bool* is_opaque);
552 606
553 // libpng uses a wacky setjmp-based API, which makes the compiler nervous. 607 // libpng uses a wacky setjmp-based API, which makes the compiler nervous.
554 // We constrain all of the calls we make to libpng where the setjmp() is in 608 // We constrain all of the calls we make to libpng where the setjmp() is in
555 // place to this function. 609 // place to this function.
556 // Returns true on success. 610 // Returns true on success.
557 bool DoLibpngWrite(png_struct* png_ptr, png_info* info_ptr, 611 bool DoLibpngWrite(png_struct* png_ptr, png_info* info_ptr,
558 PngEncoderState* state, 612 PngEncoderState* state,
559 int width, int height, int row_byte_width, 613 int width, int height, int row_byte_width,
560 const unsigned char* input, 614 const unsigned char* input,
561 int png_output_color_type, int output_color_components, 615 int png_output_color_type, int output_color_components,
562 FormatConverter converter) { 616 FormatConverter converter,
617 const std::vector<PNGCodec::Comment>& comments) {
563 // Make sure to not declare any locals here -- locals in the presence 618 // Make sure to not declare any locals here -- locals in the presence
564 // of setjmp() in C++ code makes gcc complain. 619 // of setjmp() in C++ code makes gcc complain.
565 620
566 if (setjmp(png_jmpbuf(png_ptr))) 621 if (setjmp(png_jmpbuf(png_ptr)))
567 return false; 622 return false;
568 623
569 // Set our callback for libpng to give us the data. 624 // Set our callback for libpng to give us the data.
570 png_set_write_fn(png_ptr, state, EncoderWriteCallback, FakeFlushCallback); 625 png_set_write_fn(png_ptr, state, EncoderWriteCallback, FakeFlushCallback);
571 626
572 png_set_IHDR(png_ptr, info_ptr, width, height, 8, png_output_color_type, 627 png_set_IHDR(png_ptr, info_ptr, width, height, 8, png_output_color_type,
573 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, 628 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
574 PNG_FILTER_TYPE_DEFAULT); 629 PNG_FILTER_TYPE_DEFAULT);
630
631 #ifdef PNG_TEXT_SUPPORTED
632 CommentWriter comment_writer(comments);
633 if (comment_writer.HasComments()) {
634 png_set_text(png_ptr, info_ptr, comment_writer.get_png_text(),
635 comment_writer.size());
636 }
637 #endif
638
575 png_write_info(png_ptr, info_ptr); 639 png_write_info(png_ptr, info_ptr);
576 640
577 if (!converter) { 641 if (!converter) {
578 // No conversion needed, give the data directly to libpng. 642 // No conversion needed, give the data directly to libpng.
579 for (int y = 0; y < height; y ++) { 643 for (int y = 0; y < height; y ++) {
580 png_write_row(png_ptr, 644 png_write_row(png_ptr,
581 const_cast<unsigned char*>(&input[y * row_byte_width])); 645 const_cast<unsigned char*>(&input[y * row_byte_width]));
582 } 646 }
583 } else { 647 } else {
584 // Needs conversion using a separate buffer. 648 // Needs conversion using a separate buffer.
585 unsigned char* row = new unsigned char[width * output_color_components]; 649 unsigned char* row = new unsigned char[width * output_color_components];
586 for (int y = 0; y < height; y ++) { 650 for (int y = 0; y < height; y ++) {
587 converter(&input[y * row_byte_width], width, row, NULL); 651 converter(&input[y * row_byte_width], width, row, NULL);
588 png_write_row(png_ptr, row); 652 png_write_row(png_ptr, row);
589 } 653 }
590 delete[] row; 654 delete[] row;
591 } 655 }
592 656
593 png_write_end(png_ptr, info_ptr); 657 png_write_end(png_ptr, info_ptr);
594 return true; 658 return true;
595 } 659 }
596 660
597 } // namespace 661 } // namespace
598 662
599 // static 663 // static
600 bool PNGCodec::Encode(const unsigned char* input, ColorFormat format, 664 bool PNGCodec::Encode(const unsigned char* input, ColorFormat format,
601 int w, int h, int row_byte_width, 665 const Size& size, int row_byte_width,
602 bool discard_transparency, 666 bool discard_transparency,
667 const std::vector<Comment>& comments,
603 std::vector<unsigned char>* output) { 668 std::vector<unsigned char>* output) {
604 // Run to convert an input row into the output row format, NULL means no 669 // Run to convert an input row into the output row format, NULL means no
605 // conversion is necessary. 670 // conversion is necessary.
606 FormatConverter converter = NULL; 671 FormatConverter converter = NULL;
607 672
608 int input_color_components, output_color_components; 673 int input_color_components, output_color_components;
609 int png_output_color_type; 674 int png_output_color_type;
610 switch (format) { 675 switch (format) {
611 case FORMAT_RGB: 676 case FORMAT_RGB:
612 input_color_components = 3; 677 input_color_components = 3;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
653 converter = ConvertSkiatoRGBA; 718 converter = ConvertSkiatoRGBA;
654 } 719 }
655 break; 720 break;
656 721
657 default: 722 default:
658 NOTREACHED() << "Unknown pixel format"; 723 NOTREACHED() << "Unknown pixel format";
659 return false; 724 return false;
660 } 725 }
661 726
662 // Row stride should be at least as long as the length of the data. 727 // Row stride should be at least as long as the length of the data.
663 DCHECK(input_color_components * w <= row_byte_width); 728 DCHECK(input_color_components * size.width() <= row_byte_width);
664 729
665 png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 730 png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
666 NULL, NULL, NULL); 731 NULL, NULL, NULL);
667 if (!png_ptr) 732 if (!png_ptr)
668 return false; 733 return false;
669 png_info* info_ptr = png_create_info_struct(png_ptr); 734 png_info* info_ptr = png_create_info_struct(png_ptr);
670 if (!info_ptr) { 735 if (!info_ptr) {
671 png_destroy_write_struct(&png_ptr, NULL); 736 png_destroy_write_struct(&png_ptr, NULL);
672 return false; 737 return false;
673 } 738 }
674 739
675 PngEncoderState state(output); 740 PngEncoderState state(output);
676 bool success = DoLibpngWrite(png_ptr, info_ptr, &state, 741 bool success = DoLibpngWrite(png_ptr, info_ptr, &state,
677 w, h, row_byte_width, input, 742 size.width(), size.height(), row_byte_width,
678 png_output_color_type, output_color_components, 743 input, png_output_color_type,
679 converter); 744 output_color_components, converter, comments);
680 png_destroy_write_struct(&png_ptr, &info_ptr); 745 png_destroy_write_struct(&png_ptr, &info_ptr);
681 746
682 return success; 747 return success;
683 } 748 }
684 749
685 // static 750 // static
686 bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input, 751 bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input,
687 bool discard_transparency, 752 bool discard_transparency,
688 std::vector<unsigned char>* output) { 753 std::vector<unsigned char>* output) {
689 static const int bbp = 4; 754 static const int bbp = 4;
690 755
691 SkAutoLockPixels lock_input(input); 756 SkAutoLockPixels lock_input(input);
692 DCHECK(input.empty() || input.bytesPerPixel() == bbp); 757 DCHECK(input.empty() || input.bytesPerPixel() == bbp);
693 758
694 return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), 759 return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)),
695 FORMAT_SkBitmap, input.width(), input.height(), 760 FORMAT_SkBitmap, Size(input.width(), input.height()),
696 input.width() * bbp, discard_transparency, output); 761 input.width() * bbp, discard_transparency,
762 std::vector<Comment>(), output);
763 }
764
765 PNGCodec::Comment::Comment(const std::string& k, const std::string& t)
766 : key(k), text(t) {
767 }
768
769 PNGCodec::Comment::~Comment() {
697 } 770 }
698 771
699 } // namespace gfx 772 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698