Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_libpng.h" |
| 9 #include "SkImageEncoder.h" | 9 |
| 10 #include "SkColor.h" | 10 #include "SkColor.h" |
| 11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
| 12 #include "SkDither.h" | 12 #include "SkDither.h" |
| 13 #include "SkMath.h" | 13 #include "SkMath.h" |
| 14 #include "SkRTConf.h" | 14 #include "SkRTConf.h" |
| 15 #include "SkScaledBitmapSampler.h" | 15 #include "SkScaledBitmapSampler.h" |
| 16 #include "SkStream.h" | 16 #include "SkStream.h" |
| 17 #include "SkTemplates.h" | 17 #include "SkTemplates.h" |
| 18 #include "SkUtils.h" | 18 #include "SkUtils.h" |
| 19 #include "transform_scanline.h" | 19 #include "transform_scanline.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 66 png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, png_infopp_NULL); | 66 png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, png_infopp_NULL); |
| 67 } | 67 } |
| 68 } | 68 } |
| 69 | 69 |
| 70 SkAutoTUnref<SkStreamRewindable> fStream; | 70 SkAutoTUnref<SkStreamRewindable> fStream; |
| 71 png_structp fPng_ptr; | 71 png_structp fPng_ptr; |
| 72 png_infop fInfo_ptr; | 72 png_infop fInfo_ptr; |
| 73 SkColorType fColorType; | 73 SkColorType fColorType; |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 | |
|
scroggo
2014/11/12 18:00:13
Why the extra line?
Kimmo Kinnunen
2014/11/18 08:29:45
Done.
| |
| 76 class SkPNGImageDecoder : public SkImageDecoder { | 77 class SkPNGImageDecoder : public SkImageDecoder { |
| 77 public: | 78 public: |
| 78 SkPNGImageDecoder() { | 79 SkPNGImageDecoder() { |
| 79 fImageIndex = NULL; | 80 fImageIndex = NULL; |
| 80 } | 81 } |
| 81 virtual Format getFormat() const SK_OVERRIDE { | 82 virtual Format getFormat() const SK_OVERRIDE { |
| 82 return kPNG_Format; | 83 return kPNG_Format; |
| 83 } | 84 } |
| 84 | 85 |
| 85 virtual ~SkPNGImageDecoder() { | 86 virtual ~SkPNGImageDecoder() { |
| (...skipping 1002 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1088 // these (remaining) entries are opaque | 1089 // these (remaining) entries are opaque |
| 1089 for (i = num_trans; i < ctCount; i++) { | 1090 for (i = num_trans; i < ctCount; i++) { |
| 1090 SkPMColor c = *colors++; | 1091 SkPMColor c = *colors++; |
| 1091 palette[i].red = SkGetPackedR32(c); | 1092 palette[i].red = SkGetPackedR32(c); |
| 1092 palette[i].green = SkGetPackedG32(c); | 1093 palette[i].green = SkGetPackedG32(c); |
| 1093 palette[i].blue = SkGetPackedB32(c); | 1094 palette[i].blue = SkGetPackedB32(c); |
| 1094 } | 1095 } |
| 1095 return num_trans; | 1096 return num_trans; |
| 1096 } | 1097 } |
| 1097 | 1098 |
| 1099 SkImageDecoder::Format SkDetectFormatPNGImageDecoder(SkStreamRewindable* stream) { | |
| 1100 char buf[PNG_BYTES_TO_CHECK]; | |
| 1101 if (stream->read(buf, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK && | |
| 1102 !png_sig_cmp((png_bytep) buf, (png_size_t)0, PNG_BYTES_TO_CHECK)) { | |
|
scroggo
2014/11/12 18:00:13
nit: move brace to next line.
(Again, easier to f
Kimmo Kinnunen
2014/11/18 08:29:45
Done. (Reverted the reordering.)
| |
| 1103 return SkImageDecoder::kPNG_Format; | |
| 1104 } | |
| 1105 return SkImageDecoder::kUnknown_Format; | |
| 1106 } | |
| 1107 | |
| 1108 SkImageDecoder* SkCreatePNGImageDecoder(SkStreamRewindable* stream) { | |
| 1109 if (SkDetectFormatPNGImageDecoder(stream) == SkImageDecoder::kPNG_Format) { | |
| 1110 return SkNEW(SkPNGImageDecoder); | |
| 1111 } | |
| 1112 return NULL; | |
| 1113 } | |
| 1114 | |
| 1098 class SkPNGImageEncoder : public SkImageEncoder { | 1115 class SkPNGImageEncoder : public SkImageEncoder { |
| 1099 protected: | 1116 protected: |
| 1100 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK _OVERRIDE; | 1117 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality) SK _OVERRIDE; |
| 1101 private: | 1118 private: |
| 1102 bool doEncode(SkWStream* stream, const SkBitmap& bm, | 1119 bool doEncode(SkWStream* stream, const SkBitmap& bm, |
| 1103 const bool& hasAlpha, int colorType, | 1120 const bool& hasAlpha, int colorType, |
| 1104 int bitDepth, SkColorType ct, | 1121 int bitDepth, SkColorType ct, |
| 1105 png_color_8& sig_bit); | 1122 png_color_8& sig_bit); |
| 1106 | 1123 |
| 1107 typedef SkImageEncoder INHERITED; | 1124 typedef SkImageEncoder INHERITED; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1241 srcImage += bitmap.rowBytes(); | 1258 srcImage += bitmap.rowBytes(); |
| 1242 } | 1259 } |
| 1243 | 1260 |
| 1244 png_write_end(png_ptr, info_ptr); | 1261 png_write_end(png_ptr, info_ptr); |
| 1245 | 1262 |
| 1246 /* clean up after the write, and free any memory allocated */ | 1263 /* clean up after the write, and free any memory allocated */ |
| 1247 png_destroy_write_struct(&png_ptr, &info_ptr); | 1264 png_destroy_write_struct(&png_ptr, &info_ptr); |
| 1248 return true; | 1265 return true; |
| 1249 } | 1266 } |
| 1250 | 1267 |
| 1251 /////////////////////////////////////////////////////////////////////////////// | 1268 SkImageEncoder* SkCreatePNGImageEncoder(SkImageEncoder::Type t) { |
| 1252 DEFINE_DECODER_CREATOR(PNGImageDecoder); | |
| 1253 DEFINE_ENCODER_CREATOR(PNGImageEncoder); | |
| 1254 /////////////////////////////////////////////////////////////////////////////// | |
| 1255 | |
| 1256 static bool is_png(SkStreamRewindable* stream) { | |
| 1257 char buf[PNG_BYTES_TO_CHECK]; | |
| 1258 if (stream->read(buf, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK && | |
| 1259 !png_sig_cmp((png_bytep) buf, (png_size_t)0, PNG_BYTES_TO_CHECK)) { | |
| 1260 return true; | |
| 1261 } | |
| 1262 return false; | |
| 1263 } | |
| 1264 | |
| 1265 SkImageDecoder* sk_libpng_dfactory(SkStreamRewindable* stream) { | |
| 1266 if (is_png(stream)) { | |
| 1267 return SkNEW(SkPNGImageDecoder); | |
| 1268 } | |
| 1269 return NULL; | |
| 1270 } | |
| 1271 | |
| 1272 static SkImageDecoder::Format get_format_png(SkStreamRewindable* stream) { | |
| 1273 if (is_png(stream)) { | |
| 1274 return SkImageDecoder::kPNG_Format; | |
| 1275 } | |
| 1276 return SkImageDecoder::kUnknown_Format; | |
| 1277 } | |
| 1278 | |
| 1279 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { | |
| 1280 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL; | 1269 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL; |
| 1281 } | 1270 } |
| 1282 | 1271 |
| 1283 static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory); | |
| 1284 static SkImageDecoder_FormatReg gFormatReg(get_format_png); | |
| 1285 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); | |
| OLD | NEW |