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