OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
(...skipping 24 matching lines...) Expand all Loading... |
35 #ifndef int_p_NULL | 35 #ifndef int_p_NULL |
36 #define int_p_NULL NULL | 36 #define int_p_NULL NULL |
37 #endif | 37 #endif |
38 | 38 |
39 #ifndef png_flush_ptr_NULL | 39 #ifndef png_flush_ptr_NULL |
40 #define png_flush_ptr_NULL NULL | 40 #define png_flush_ptr_NULL NULL |
41 #endif | 41 #endif |
42 | 42 |
43 class SkPNGImageIndex { | 43 class SkPNGImageIndex { |
44 public: | 44 public: |
45 SkPNGImageIndex(SkStream* stream, png_structp png_ptr, png_infop info_ptr) | 45 SkPNGImageIndex(SkStreamRewindable* stream, png_structp png_ptr, png_infop i
nfo_ptr) |
46 : fStream(stream) | 46 : fStream(stream) |
47 , fPng_ptr(png_ptr) | 47 , fPng_ptr(png_ptr) |
48 , fInfo_ptr(info_ptr) | 48 , fInfo_ptr(info_ptr) |
49 , fConfig(SkBitmap::kNo_Config) { | 49 , fConfig(SkBitmap::kNo_Config) { |
50 SkASSERT(stream != NULL); | 50 SkASSERT(stream != NULL); |
51 stream->ref(); | 51 stream->ref(); |
52 } | 52 } |
53 ~SkPNGImageIndex() { | 53 ~SkPNGImageIndex() { |
54 if (NULL != fPng_ptr) { | 54 if (NULL != fPng_ptr) { |
55 png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, png_infopp_NULL); | 55 png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, png_infopp_NULL); |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 SkAutoTUnref<SkStream> fStream; | 59 SkAutoTUnref<SkStreamRewindable> fStream; |
60 png_structp fPng_ptr; | 60 png_structp fPng_ptr; |
61 png_infop fInfo_ptr; | 61 png_infop fInfo_ptr; |
62 SkBitmap::Config fConfig; | 62 SkBitmap::Config fConfig; |
63 }; | 63 }; |
64 | 64 |
65 class SkPNGImageDecoder : public SkImageDecoder { | 65 class SkPNGImageDecoder : public SkImageDecoder { |
66 public: | 66 public: |
67 SkPNGImageDecoder() { | 67 SkPNGImageDecoder() { |
68 fImageIndex = NULL; | 68 fImageIndex = NULL; |
69 } | 69 } |
70 virtual Format getFormat() const SK_OVERRIDE { | 70 virtual Format getFormat() const SK_OVERRIDE { |
71 return kPNG_Format; | 71 return kPNG_Format; |
72 } | 72 } |
73 | 73 |
74 virtual ~SkPNGImageDecoder() { | 74 virtual ~SkPNGImageDecoder() { |
75 SkDELETE(fImageIndex); | 75 SkDELETE(fImageIndex); |
76 } | 76 } |
77 | 77 |
78 protected: | 78 protected: |
79 #ifdef SK_BUILD_FOR_ANDROID | 79 #ifdef SK_BUILD_FOR_ANDROID |
80 virtual bool onBuildTileIndex(SkStream *stream, int *width, int *height) SK_
OVERRIDE; | 80 virtual bool onBuildTileIndex(SkStreamRewindable *stream, int *width, int *h
eight) SK_OVERRIDE; |
81 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& region) SK_OVER
RIDE; | 81 virtual bool onDecodeSubset(SkBitmap* bitmap, const SkIRect& region) SK_OVER
RIDE; |
82 #endif | 82 #endif |
83 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; | 83 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode) SK_OVERRIDE; |
84 | 84 |
85 private: | 85 private: |
86 SkPNGImageIndex* fImageIndex; | 86 SkPNGImageIndex* fImageIndex; |
87 | 87 |
88 bool onDecodeInit(SkStream* stream, png_structp *png_ptrp, png_infop *info_p
trp); | 88 bool onDecodeInit(SkStream* stream, png_structp *png_ptrp, png_infop *info_p
trp); |
89 bool decodePalette(png_structp png_ptr, png_infop info_ptr, | 89 bool decodePalette(png_structp png_ptr, png_infop info_ptr, |
90 bool * SK_RESTRICT hasAlphap, bool *reallyHasAlphap, | 90 bool * SK_RESTRICT hasAlphap, bool *reallyHasAlphap, |
(...skipping 25 matching lines...) Expand all Loading... |
116 static void sk_read_fn(png_structp png_ptr, png_bytep data, png_size_t length) { | 116 static void sk_read_fn(png_structp png_ptr, png_bytep data, png_size_t length) { |
117 SkStream* sk_stream = (SkStream*) png_get_io_ptr(png_ptr); | 117 SkStream* sk_stream = (SkStream*) png_get_io_ptr(png_ptr); |
118 size_t bytes = sk_stream->read(data, length); | 118 size_t bytes = sk_stream->read(data, length); |
119 if (bytes != length) { | 119 if (bytes != length) { |
120 png_error(png_ptr, "Read Error!"); | 120 png_error(png_ptr, "Read Error!"); |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 #ifdef SK_BUILD_FOR_ANDROID | 124 #ifdef SK_BUILD_FOR_ANDROID |
125 static void sk_seek_fn(png_structp png_ptr, png_uint_32 offset) { | 125 static void sk_seek_fn(png_structp png_ptr, png_uint_32 offset) { |
126 SkStream* sk_stream = (SkStream*) png_get_io_ptr(png_ptr); | 126 SkStreamRewindable* sk_stream = (SkStreamRewindable*) png_get_io_ptr(png_ptr
); |
127 if (!sk_stream->rewind()) { | 127 if (!sk_stream->rewind()) { |
128 png_error(png_ptr, "Failed to rewind stream!"); | 128 png_error(png_ptr, "Failed to rewind stream!"); |
129 } | 129 } |
130 (void)sk_stream->skip(offset); | 130 (void)sk_stream->skip(offset); |
131 } | 131 } |
132 #endif | 132 #endif |
133 | 133 |
134 static int sk_read_user_chunk(png_structp png_ptr, png_unknown_chunkp chunk) { | 134 static int sk_read_user_chunk(png_structp png_ptr, png_unknown_chunkp chunk) { |
135 SkImageDecoder::Peeker* peeker = | 135 SkImageDecoder::Peeker* peeker = |
136 (SkImageDecoder::Peeker*)png_get_user_chunk_ptr(png_ptr); | 136 (SkImageDecoder::Peeker*)png_get_user_chunk_ptr(png_ptr); |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 *colorPtr = colorPtr[-1]; | 660 *colorPtr = colorPtr[-1]; |
661 } | 661 } |
662 colorTable->unlockColors(true); | 662 colorTable->unlockColors(true); |
663 *colorTablep = colorTable; | 663 *colorTablep = colorTable; |
664 *reallyHasAlphap = reallyHasAlpha; | 664 *reallyHasAlphap = reallyHasAlpha; |
665 return true; | 665 return true; |
666 } | 666 } |
667 | 667 |
668 #ifdef SK_BUILD_FOR_ANDROID | 668 #ifdef SK_BUILD_FOR_ANDROID |
669 | 669 |
670 bool SkPNGImageDecoder::onBuildTileIndex(SkStream* sk_stream, int *width, int *h
eight) { | 670 bool SkPNGImageDecoder::onBuildTileIndex(SkStreamRewindable* sk_stream, int *wid
th, int *height) { |
671 png_structp png_ptr; | 671 png_structp png_ptr; |
672 png_infop info_ptr; | 672 png_infop info_ptr; |
673 | 673 |
674 if (!onDecodeInit(sk_stream, &png_ptr, &info_ptr)) { | 674 if (!onDecodeInit(sk_stream, &png_ptr, &info_ptr)) { |
675 return false; | 675 return false; |
676 } | 676 } |
677 | 677 |
678 if (setjmp(png_jmpbuf(png_ptr)) != 0) { | 678 if (setjmp(png_jmpbuf(png_ptr)) != 0) { |
679 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); | 679 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); |
680 return false; | 680 return false; |
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1167 /* clean up after the write, and free any memory allocated */ | 1167 /* clean up after the write, and free any memory allocated */ |
1168 png_destroy_write_struct(&png_ptr, &info_ptr); | 1168 png_destroy_write_struct(&png_ptr, &info_ptr); |
1169 return true; | 1169 return true; |
1170 } | 1170 } |
1171 | 1171 |
1172 /////////////////////////////////////////////////////////////////////////////// | 1172 /////////////////////////////////////////////////////////////////////////////// |
1173 DEFINE_DECODER_CREATOR(PNGImageDecoder); | 1173 DEFINE_DECODER_CREATOR(PNGImageDecoder); |
1174 DEFINE_ENCODER_CREATOR(PNGImageEncoder); | 1174 DEFINE_ENCODER_CREATOR(PNGImageEncoder); |
1175 /////////////////////////////////////////////////////////////////////////////// | 1175 /////////////////////////////////////////////////////////////////////////////// |
1176 | 1176 |
1177 static bool is_png(SkStream* stream) { | 1177 static bool is_png(SkStreamRewindable* stream) { |
1178 char buf[PNG_BYTES_TO_CHECK]; | 1178 char buf[PNG_BYTES_TO_CHECK]; |
1179 if (stream->read(buf, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK && | 1179 if (stream->read(buf, PNG_BYTES_TO_CHECK) == PNG_BYTES_TO_CHECK && |
1180 !png_sig_cmp((png_bytep) buf, (png_size_t)0, PNG_BYTES_TO_CHECK)) { | 1180 !png_sig_cmp((png_bytep) buf, (png_size_t)0, PNG_BYTES_TO_CHECK)) { |
1181 return true; | 1181 return true; |
1182 } | 1182 } |
1183 return false; | 1183 return false; |
1184 } | 1184 } |
1185 | 1185 |
1186 SkImageDecoder* sk_libpng_dfactory(SkStream* stream) { | 1186 SkImageDecoder* sk_libpng_dfactory(SkStreamRewindable* stream) { |
1187 if (is_png(stream)) { | 1187 if (is_png(stream)) { |
1188 return SkNEW(SkPNGImageDecoder); | 1188 return SkNEW(SkPNGImageDecoder); |
1189 } | 1189 } |
1190 return NULL; | 1190 return NULL; |
1191 } | 1191 } |
1192 | 1192 |
1193 static SkImageDecoder::Format get_format_png(SkStream* stream) { | 1193 static SkImageDecoder::Format get_format_png(SkStreamRewindable* stream) { |
1194 if (is_png(stream)) { | 1194 if (is_png(stream)) { |
1195 return SkImageDecoder::kPNG_Format; | 1195 return SkImageDecoder::kPNG_Format; |
1196 } | 1196 } |
1197 return SkImageDecoder::kUnknown_Format; | 1197 return SkImageDecoder::kUnknown_Format; |
1198 } | 1198 } |
1199 | 1199 |
1200 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { | 1200 SkImageEncoder* sk_libpng_efactory(SkImageEncoder::Type t) { |
1201 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL; | 1201 return (SkImageEncoder::kPNG_Type == t) ? SkNEW(SkPNGImageEncoder) : NULL; |
1202 } | 1202 } |
1203 | 1203 |
1204 static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory); | 1204 static SkImageDecoder_DecodeReg gDReg(sk_libpng_dfactory); |
1205 static SkImageDecoder_FormatReg gFormatReg(get_format_png); | 1205 static SkImageDecoder_FormatReg gFormatReg(get_format_png); |
1206 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); | 1206 static SkImageEncoder_EncodeReg gEReg(sk_libpng_efactory); |
OLD | NEW |