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

Side by Side Diff: src/images/SkImageDecoder_libpng.cpp

Issue 1506663002: SkPNGImageEncoder encodes all SkColorTypes (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2015-12-07 (Monday) 16:33:43 EST Created 5 years 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
« 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 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkImageDecoder.h" 8 #include "SkImageDecoder.h"
9 #include "SkImageEncoder.h" 9 #include "SkImageEncoder.h"
10 #include "SkColor.h" 10 #include "SkColor.h"
(...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) override; 820 bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) override;
821 private: 821 private:
822 bool doEncode(SkWStream* stream, const SkBitmap& bm, 822 bool doEncode(SkWStream* stream, const SkBitmap& bm,
823 const bool& hasAlpha, int colorType, 823 const bool& hasAlpha, int colorType,
824 int bitDepth, SkColorType ct, 824 int bitDepth, SkColorType ct,
825 png_color_8& sig_bit); 825 png_color_8& sig_bit);
826 826
827 typedef SkImageEncoder INHERITED; 827 typedef SkImageEncoder INHERITED;
828 }; 828 };
829 829
830 bool SkPNGImageEncoder::onEncode(SkWStream* stream, const SkBitmap& bitmap, int /*quality*/) { 830 bool SkPNGImageEncoder::onEncode(SkWStream* stream,
831 SkColorType ct = bitmap.colorType(); 831 const SkBitmap& originalBitmap,
832 int /*quality*/) {
833 SkBitmap copy;
834 const SkBitmap* bitmap = &originalBitmap;
835 switch (originalBitmap.colorType()) {
836 case kIndex_8_SkColorType:
837 case kN32_SkColorType:
838 case kARGB_4444_SkColorType:
839 case kRGB_565_SkColorType:
840 break;
841 default:
842 // TODO(scroggo): support 8888-but-not-N32 natively.
843 // TODO(scroggo): support kGray_8 directly.
844 // TODO(scroggo): support Alpha_8 as Grayscale(black)+Alpha
845 if (originalBitmap.copyTo(&copy, kN32_SkColorType)) {
846 bitmap = ©
847 }
848 }
849 SkColorType ct = bitmap->colorType();
832 850
833 const bool hasAlpha = !bitmap.isOpaque(); 851 const bool hasAlpha = !bitmap->isOpaque();
834 int colorType = PNG_COLOR_MASK_COLOR; 852 int colorType = PNG_COLOR_MASK_COLOR;
835 int bitDepth = 8; // default for color 853 int bitDepth = 8; // default for color
836 png_color_8 sig_bit; 854 png_color_8 sig_bit;
837 855
838 switch (ct) { 856 switch (ct) {
839 case kIndex_8_SkColorType: 857 case kIndex_8_SkColorType:
840 colorType |= PNG_COLOR_MASK_PALETTE; 858 colorType |= PNG_COLOR_MASK_PALETTE;
841 // fall through to the ARGB_8888 case 859 // fall through to the ARGB_8888 case
842 case kN32_SkColorType: 860 case kN32_SkColorType:
843 sig_bit.red = 8; 861 sig_bit.red = 8;
(...skipping 19 matching lines...) Expand all
863 881
864 if (hasAlpha) { 882 if (hasAlpha) {
865 // don't specify alpha if we're a palette, even if our ctable has alpha 883 // don't specify alpha if we're a palette, even if our ctable has alpha
866 if (!(colorType & PNG_COLOR_MASK_PALETTE)) { 884 if (!(colorType & PNG_COLOR_MASK_PALETTE)) {
867 colorType |= PNG_COLOR_MASK_ALPHA; 885 colorType |= PNG_COLOR_MASK_ALPHA;
868 } 886 }
869 } else { 887 } else {
870 sig_bit.alpha = 0; 888 sig_bit.alpha = 0;
871 } 889 }
872 890
873 SkAutoLockPixels alp(bitmap); 891 SkAutoLockPixels alp(*bitmap);
874 // readyToDraw checks for pixels (and colortable if that is required) 892 // readyToDraw checks for pixels (and colortable if that is required)
875 if (!bitmap.readyToDraw()) { 893 if (!bitmap->readyToDraw()) {
876 return false; 894 return false;
877 } 895 }
878 896
879 // we must do this after we have locked the pixels 897 // we must do this after we have locked the pixels
880 SkColorTable* ctable = bitmap.getColorTable(); 898 SkColorTable* ctable = bitmap->getColorTable();
881 if (ctable) { 899 if (ctable) {
882 if (ctable->count() == 0) { 900 if (ctable->count() == 0) {
883 return false; 901 return false;
884 } 902 }
885 // check if we can store in fewer than 8 bits 903 // check if we can store in fewer than 8 bits
886 bitDepth = computeBitDepth(ctable->count()); 904 bitDepth = computeBitDepth(ctable->count());
887 } 905 }
888 906
889 return doEncode(stream, bitmap, hasAlpha, colorType, bitDepth, ct, sig_bit); 907 return doEncode(stream, *bitmap, hasAlpha, colorType, bitDepth, ct, sig_bit) ;
890 } 908 }
891 909
892 bool SkPNGImageEncoder::doEncode(SkWStream* stream, const SkBitmap& bitmap, 910 bool SkPNGImageEncoder::doEncode(SkWStream* stream, const SkBitmap& bitmap,
893 const bool& hasAlpha, int colorType, 911 const bool& hasAlpha, int colorType,
894 int bitDepth, SkColorType ct, 912 int bitDepth, SkColorType ct,
895 png_color_8& sig_bit) { 913 png_color_8& sig_bit) {
896 914
897 png_structp png_ptr; 915 png_structp png_ptr;
898 png_infop info_ptr; 916 png_infop info_ptr;
899 917
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
996 return SkImageDecoder::kUnknown_Format; 1014 return SkImageDecoder::kUnknown_Format;
997 } 1015 }
998 1016
999 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { 1017 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) {
1000 return (SkImageEncoder::kPNG_Type == t) ? new SkPNGImageEncoder : nullptr; 1018 return (SkImageEncoder::kPNG_Type == t) ? new SkPNGImageEncoder : nullptr;
1001 } 1019 }
1002 1020
1003 static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory); 1021 static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory);
1004 static SkImageDecoder_FormatReg gFormatReg(get_format_png); 1022 static SkImageDecoder_FormatReg gFormatReg(get_format_png);
1005 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); 1023 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory);
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