OLD | NEW |
---|---|
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/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "third_party/libpng/png.h" | 9 #include "third_party/libpng/png.h" |
10 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
(...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
671 png_output_color_type = PNG_COLOR_TYPE_RGB; | 671 png_output_color_type = PNG_COLOR_TYPE_RGB; |
672 converter = ConvertBGRAtoRGB; | 672 converter = ConvertBGRAtoRGB; |
673 } else { | 673 } else { |
674 output_color_components = 4; | 674 output_color_components = 4; |
675 png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA; | 675 png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
676 converter = ConvertBetweenBGRAandRGBA; | 676 converter = ConvertBetweenBGRAandRGBA; |
677 } | 677 } |
678 break; | 678 break; |
679 | 679 |
680 case PNGCodec::FORMAT_SkBitmap: | 680 case PNGCodec::FORMAT_SkBitmap: |
681 input_color_components = 4; | 681 // Compare row_byte_width and size.width() to detect the format of |
682 if (discard_transparency) { | 682 // SkBitmap. kA8_Config (1bpp) and kARGB_8888_Config (4bpp) are the two |
683 output_color_components = 3; | 683 // supported formats |
Peter Kasting
2014/01/25 03:29:58
Nit: Trailing period
mfomitchev
2014/01/27 17:15:28
Done.
| |
684 png_output_color_type = PNG_COLOR_TYPE_RGB; | 684 if (row_byte_width < 4 * size.width()) { |
685 converter = ConvertSkiatoRGB; | 685 // Not 4bpp, so must be 1bpp. |
686 // Ignore discard_transparency - it doesn't make sense in this context, | |
687 // since alpha is the only thing we have and it needs to be used for | |
688 // color intensity. | |
689 input_color_components = 1; | |
690 output_color_components = 1; | |
691 png_output_color_type = PNG_COLOR_TYPE_GRAY; | |
692 //converter = NULL; | |
Peter Kasting
2014/01/25 03:29:58
Nit: Remove this line. If you need to explain tha
mfomitchev
2014/01/27 17:15:28
Done.
| |
686 } else { | 693 } else { |
687 output_color_components = 4; | 694 input_color_components = 4; |
688 png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA; | 695 if (discard_transparency) { |
689 converter = ConvertSkiatoRGBA; | 696 output_color_components = 3; |
697 png_output_color_type = PNG_COLOR_TYPE_RGB; | |
698 converter = ConvertSkiatoRGB; | |
699 } else { | |
700 output_color_components = 4; | |
701 png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA; | |
702 converter = ConvertSkiatoRGBA; | |
703 } | |
690 } | 704 } |
691 break; | 705 break; |
692 | 706 |
693 default: | 707 default: |
694 NOTREACHED() << "Unknown pixel format"; | 708 NOTREACHED() << "Unknown pixel format"; |
695 return false; | 709 return false; |
696 } | 710 } |
697 | 711 |
698 // Row stride should be at least as long as the length of the data. | 712 // Row stride should be at least as long as the length of the data. |
699 DCHECK(input_color_components * size.width() <= row_byte_width); | 713 DCHECK(input_color_components * size.width() <= row_byte_width); |
(...skipping 12 matching lines...) Expand all Loading... | |
712 | 726 |
713 PngEncoderState state(output); | 727 PngEncoderState state(output); |
714 bool success = DoLibpngWrite(png_ptr, info_ptr, &state, | 728 bool success = DoLibpngWrite(png_ptr, info_ptr, &state, |
715 size.width(), size.height(), row_byte_width, | 729 size.width(), size.height(), row_byte_width, |
716 input, compression_level, png_output_color_type, | 730 input, compression_level, png_output_color_type, |
717 output_color_components, converter, comments); | 731 output_color_components, converter, comments); |
718 | 732 |
719 return success; | 733 return success; |
720 } | 734 } |
721 | 735 |
736 bool InternalEncodeSkBitmap(const SkBitmap& input, | |
737 int bpp, | |
738 bool discard_transparency, | |
739 int compression_level, | |
740 std::vector<unsigned char>* output) { | |
741 if (input.empty()) | |
742 return false; | |
743 DCHECK_EQ(input.bytesPerPixel(), bpp); | |
Peter Kasting
2014/01/25 03:29:58
If these are always equal, why pass |bpp| at all?
mfomitchev
2014/01/27 17:15:28
Now EncodeA8SkBitmap() and EncodeBGRASkBitmap() ar
mfomitchev
2014/01/27 17:15:28
Now EncodeA8SkBitmap() and EncodeBGRASkBitmap() ar
| |
744 DCHECK_GE(static_cast<int>(input.rowBytes()), input.width() * bpp); | |
745 | |
746 SkAutoLockPixels lock_input(input); | |
747 return EncodeWithCompressionLevel( | |
748 reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), | |
749 PNGCodec::FORMAT_SkBitmap, | |
750 Size(input.width(), input.height()), | |
751 static_cast<int>(input.rowBytes()), | |
752 discard_transparency, | |
753 std::vector<PNGCodec::Comment>(), | |
754 compression_level, | |
755 output); | |
756 } | |
757 | |
722 | 758 |
723 } // namespace | 759 } // namespace |
724 | 760 |
725 // static | 761 // static |
726 bool PNGCodec::Encode(const unsigned char* input, ColorFormat format, | 762 bool PNGCodec::Encode(const unsigned char* input, |
727 const Size& size, int row_byte_width, | 763 ColorFormat format, |
764 const Size& size, | |
765 int row_byte_width, | |
728 bool discard_transparency, | 766 bool discard_transparency, |
729 const std::vector<Comment>& comments, | 767 const std::vector<Comment>& comments, |
730 std::vector<unsigned char>* output) { | 768 std::vector<unsigned char>* output) { |
731 return EncodeWithCompressionLevel(input, | 769 return EncodeWithCompressionLevel(input, |
732 format, | 770 format, |
733 size, | 771 size, |
734 row_byte_width, | 772 row_byte_width, |
735 discard_transparency, | 773 discard_transparency, |
736 comments, | 774 comments, |
737 Z_DEFAULT_COMPRESSION, | 775 Z_DEFAULT_COMPRESSION, |
738 output); | 776 output); |
739 } | 777 } |
740 | 778 |
741 // static | 779 // static |
742 bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input, | 780 bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input, |
743 bool discard_transparency, | 781 bool discard_transparency, |
744 std::vector<unsigned char>* output) { | 782 std::vector<unsigned char>* output) { |
745 static const int bbp = 4; | 783 static const int bpp = 4; |
746 | 784 |
747 if (input.empty()) | 785 return InternalEncodeSkBitmap(input, |
748 return false; | 786 bpp, |
749 DCHECK_EQ(input.bytesPerPixel(), bbp); | 787 discard_transparency, |
750 DCHECK_GE(static_cast<int>(input.rowBytes()), input.width() * bbp); | 788 Z_DEFAULT_COMPRESSION, |
789 output); | |
790 } | |
751 | 791 |
752 SkAutoLockPixels lock_input(input); | 792 // static |
753 return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), | 793 bool PNGCodec::EncodeA8SkBitmap(const SkBitmap& input, |
754 FORMAT_SkBitmap, Size(input.width(), input.height()), | 794 std::vector<unsigned char>* output) { |
Peter Kasting
2014/01/25 03:29:58
Nit: Fix indenting
mfomitchev
2014/01/27 17:15:28
Done.
| |
755 static_cast<int>(input.rowBytes()), discard_transparency, | 795 static const int bpp = 1; |
756 std::vector<Comment>(), output); | 796 |
797 return InternalEncodeSkBitmap(input, | |
798 bpp, | |
799 false, | |
800 Z_DEFAULT_COMPRESSION, | |
801 output); | |
757 } | 802 } |
758 | 803 |
759 // static | 804 // static |
760 bool PNGCodec::FastEncodeBGRASkBitmap(const SkBitmap& input, | 805 bool PNGCodec::FastEncodeBGRASkBitmap(const SkBitmap& input, |
761 bool discard_transparency, | 806 bool discard_transparency, |
762 std::vector<unsigned char>* output) { | 807 std::vector<unsigned char>* output) { |
763 static const int bbp = 4; | 808 static const int bpp = 4; |
764 | 809 |
765 if (input.empty()) | 810 return InternalEncodeSkBitmap(input, |
766 return false; | 811 bpp, |
767 DCHECK_EQ(input.bytesPerPixel(), bbp); | 812 discard_transparency, |
768 DCHECK_GE(static_cast<int>(input.rowBytes()), input.width() * bbp); | 813 Z_BEST_SPEED, |
769 | 814 output); |
770 SkAutoLockPixels lock_input(input); | |
771 return EncodeWithCompressionLevel( | |
772 reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), | |
773 FORMAT_SkBitmap, Size(input.width(), input.height()), | |
774 static_cast<int>(input.rowBytes()), discard_transparency, | |
775 std::vector<Comment>(), Z_BEST_SPEED, output); | |
776 } | 815 } |
777 | 816 |
778 PNGCodec::Comment::Comment(const std::string& k, const std::string& t) | 817 PNGCodec::Comment::Comment(const std::string& k, const std::string& t) |
779 : key(k), text(t) { | 818 : key(k), text(t) { |
780 } | 819 } |
781 | 820 |
782 PNGCodec::Comment::~Comment() { | 821 PNGCodec::Comment::~Comment() { |
783 } | 822 } |
784 | 823 |
785 } // namespace gfx | 824 } // namespace gfx |
OLD | NEW |