blob: e7035c6e8aa426e112ed5a002d606f90575390c6 [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
msarett74114382015-03-16 11:55:18 -07008#include "SkCodecPriv.h"
scroggof24f2242015-03-03 08:59:20 -08009#include "SkColorPriv.h"
10#include "SkColorTable.h"
11#include "SkBitmap.h"
12#include "SkMath.h"
msarett13a91232016-02-01 08:03:29 -080013#include "SkOpts.h"
msarettbe1d5552016-01-21 09:05:23 -080014#include "SkPngCodec.h"
scroggof24f2242015-03-03 08:59:20 -080015#include "SkSize.h"
16#include "SkStream.h"
17#include "SkSwizzler.h"
scroggo565901d2015-12-10 10:44:13 -080018#include "SkTemplates.h"
scroggof24f2242015-03-03 08:59:20 -080019
20///////////////////////////////////////////////////////////////////////////////
scroggof24f2242015-03-03 08:59:20 -080021// Callback functions
22///////////////////////////////////////////////////////////////////////////////
23
24static void sk_error_fn(png_structp png_ptr, png_const_charp msg) {
scroggo230d4ac2015-03-26 07:15:55 -070025 SkCodecPrintf("------ png error %s\n", msg);
scroggof24f2242015-03-03 08:59:20 -080026 longjmp(png_jmpbuf(png_ptr), 1);
27}
28
scroggo0eed6df2015-03-26 10:07:56 -070029void sk_warning_fn(png_structp, png_const_charp msg) {
30 SkCodecPrintf("----- png warning %s\n", msg);
31}
32
scroggof24f2242015-03-03 08:59:20 -080033static void sk_read_fn(png_structp png_ptr, png_bytep data,
34 png_size_t length) {
35 SkStream* stream = static_cast<SkStream*>(png_get_io_ptr(png_ptr));
36 const size_t bytes = stream->read(data, length);
37 if (bytes != length) {
38 // FIXME: We want to report the fact that the stream was truncated.
39 // One way to do that might be to pass a enum to longjmp so setjmp can
40 // specify the failure.
41 png_error(png_ptr, "Read Error!");
42 }
43}
44
scroggocf98fa92015-11-23 08:14:40 -080045#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
46static int sk_read_user_chunk(png_structp png_ptr, png_unknown_chunkp chunk) {
47 SkPngChunkReader* chunkReader = (SkPngChunkReader*)png_get_user_chunk_ptr(png_ptr);
48 // readChunk() returning true means continue decoding
49 return chunkReader->readChunk((const char*)chunk->name, chunk->data, chunk->size) ? 1 : -1;
50}
51#endif
52
scroggof24f2242015-03-03 08:59:20 -080053///////////////////////////////////////////////////////////////////////////////
54// Helpers
55///////////////////////////////////////////////////////////////////////////////
56
57class AutoCleanPng : public SkNoncopyable {
58public:
59 AutoCleanPng(png_structp png_ptr)
60 : fPng_ptr(png_ptr)
halcanary96fcdcc2015-08-27 07:41:13 -070061 , fInfo_ptr(nullptr) {}
scroggof24f2242015-03-03 08:59:20 -080062
63 ~AutoCleanPng() {
halcanary96fcdcc2015-08-27 07:41:13 -070064 // fInfo_ptr will never be non-nullptr unless fPng_ptr is.
scroggof24f2242015-03-03 08:59:20 -080065 if (fPng_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -070066 png_infopp info_pp = fInfo_ptr ? &fInfo_ptr : nullptr;
msarett13a91232016-02-01 08:03:29 -080067 png_destroy_read_struct(&fPng_ptr, info_pp, nullptr);
scroggof24f2242015-03-03 08:59:20 -080068 }
69 }
70
71 void setInfoPtr(png_infop info_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -070072 SkASSERT(nullptr == fInfo_ptr);
scroggof24f2242015-03-03 08:59:20 -080073 fInfo_ptr = info_ptr;
74 }
75
76 void detach() {
halcanary96fcdcc2015-08-27 07:41:13 -070077 fPng_ptr = nullptr;
78 fInfo_ptr = nullptr;
scroggof24f2242015-03-03 08:59:20 -080079 }
80
81private:
82 png_structp fPng_ptr;
83 png_infop fInfo_ptr;
84};
85#define AutoCleanPng(...) SK_REQUIRE_LOCAL_VAR(AutoCleanPng)
86
scroggof24f2242015-03-03 08:59:20 -080087// Method for coverting to either an SkPMColor or a similarly packed
88// unpremultiplied color.
89typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
90
91// Note: SkColorTable claims to store SkPMColors, which is not necessarily
92// the case here.
msarett13a91232016-02-01 08:03:29 -080093// TODO: If we add support for non-native swizzles, we'll need to handle that here.
scroggo9b2cdbf2015-07-10 12:07:02 -070094bool SkPngCodec::decodePalette(bool premultiply, int* ctableCount) {
scroggof24f2242015-03-03 08:59:20 -080095
msarett13a91232016-02-01 08:03:29 -080096 int numColors;
97 png_color* palette;
98 if (!png_get_PLTE(fPng_ptr, fInfo_ptr, &palette, &numColors)) {
scroggo05245902015-03-25 11:11:52 -070099 return false;
scroggof24f2242015-03-03 08:59:20 -0800100 }
101
msarett13a91232016-02-01 08:03:29 -0800102 // Note: These are not necessarily SkPMColors.
103 SkPMColor colorPtr[256];
scroggof24f2242015-03-03 08:59:20 -0800104
msarett13a91232016-02-01 08:03:29 -0800105 png_bytep alphas;
106 int numColorsWithAlpha = 0;
107 if (png_get_tRNS(fPng_ptr, fInfo_ptr, &alphas, &numColorsWithAlpha, nullptr)) {
108 // Choose which function to use to create the color table. If the final destination's
109 // colortype is unpremultiplied, the color table will store unpremultiplied colors.
110 PackColorProc proc;
111 if (premultiply) {
112 proc = &SkPremultiplyARGBInline;
113 } else {
114 proc = &SkPackARGB32NoCheck;
115 }
116
117 for (int i = 0; i < numColorsWithAlpha; i++) {
118 // We don't have a function in SkOpts that combines a set of alphas with a set
119 // of RGBs. We could write one, but it's hardly worth it, given that this
120 // is such a small fraction of the total decode time.
121 colorPtr[i] = proc(alphas[i], palette->red, palette->green, palette->blue);
122 palette++;
123 }
scroggof24f2242015-03-03 08:59:20 -0800124 }
125
msarett13a91232016-02-01 08:03:29 -0800126 if (numColorsWithAlpha < numColors) {
127 // The optimized code depends on a 3-byte png_color struct with the colors
128 // in RGB order. These checks make sure it is safe to use.
129 static_assert(3 == sizeof(png_color), "png_color struct has changed. Opts are broken.");
130#ifdef SK_DEBUG
131 SkASSERT(&palette->red < &palette->green);
132 SkASSERT(&palette->green < &palette->blue);
133#endif
134
135#ifdef SK_PMCOLOR_IS_RGBA
136 SkOpts::RGB_to_RGB1(colorPtr + numColorsWithAlpha, palette, numColors - numColorsWithAlpha);
137#else
138 SkOpts::RGB_to_BGR1(colorPtr + numColorsWithAlpha, palette, numColors - numColorsWithAlpha);
139#endif
scroggof24f2242015-03-03 08:59:20 -0800140 }
141
msarett13a91232016-02-01 08:03:29 -0800142 // Pad the color table with the last color in the table (or black) in the case that
143 // invalid pixel indices exceed the number of colors in the table.
144 const int maxColors = 1 << fBitDepth;
145 if (numColors < maxColors) {
146 SkPMColor lastColor = numColors > 0 ? colorPtr[numColors - 1] : SK_ColorBLACK;
147 sk_memset32(colorPtr + numColors, lastColor, maxColors - numColors);
scroggof24f2242015-03-03 08:59:20 -0800148 }
149
msarett13a91232016-02-01 08:03:29 -0800150 // Set the new color count.
halcanary96fcdcc2015-08-27 07:41:13 -0700151 if (ctableCount != nullptr) {
msarett13a91232016-02-01 08:03:29 -0800152 *ctableCount = maxColors;
scroggof24f2242015-03-03 08:59:20 -0800153 }
154
msarett13a91232016-02-01 08:03:29 -0800155 fColorTable.reset(new SkColorTable(colorPtr, maxColors));
scroggo05245902015-03-25 11:11:52 -0700156 return true;
scroggof24f2242015-03-03 08:59:20 -0800157}
158
159///////////////////////////////////////////////////////////////////////////////
160// Creation
161///////////////////////////////////////////////////////////////////////////////
162
scroggodb30be22015-12-08 18:54:13 -0800163bool SkPngCodec::IsPng(const char* buf, size_t bytesRead) {
164 return !png_sig_cmp((png_bytep) buf, (png_size_t)0, bytesRead);
scroggof24f2242015-03-03 08:59:20 -0800165}
166
scroggocf98fa92015-11-23 08:14:40 -0800167// Reads the header and initializes the output fields, if not NULL.
168//
169// @param stream Input data. Will be read to get enough information to properly
170// setup the codec.
171// @param chunkReader SkPngChunkReader, for reading unknown chunks. May be NULL.
172// If not NULL, png_ptr will hold an *unowned* pointer to it. The caller is
173// expected to continue to own it for the lifetime of the png_ptr.
174// @param png_ptrp Optional output variable. If non-NULL, will be set to a new
175// png_structp on success.
176// @param info_ptrp Optional output variable. If non-NULL, will be set to a new
177// png_infop on success;
178// @param imageInfo Optional output variable. If non-NULL, will be set to
179// reflect the properties of the encoded image on success.
180// @param bitDepthPtr Optional output variable. If non-NULL, will be set to the
181// bit depth of the encoded image on success.
182// @param numberPassesPtr Optional output variable. If non-NULL, will be set to
183// the number_passes of the encoded image on success.
184// @return true on success, in which case the caller is responsible for calling
185// png_destroy_read_struct(png_ptrp, info_ptrp).
186// If it returns false, the passed in fields (except stream) are unchanged.
187static bool read_header(SkStream* stream, SkPngChunkReader* chunkReader,
188 png_structp* png_ptrp, png_infop* info_ptrp,
189 SkImageInfo* imageInfo, int* bitDepthPtr, int* numberPassesPtr) {
scroggof24f2242015-03-03 08:59:20 -0800190 // The image is known to be a PNG. Decode enough to know the SkImageInfo.
halcanary96fcdcc2015-08-27 07:41:13 -0700191 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
scroggo0eed6df2015-03-26 10:07:56 -0700192 sk_error_fn, sk_warning_fn);
scroggof24f2242015-03-03 08:59:20 -0800193 if (!png_ptr) {
scroggo3eada2a2015-04-01 09:33:23 -0700194 return false;
scroggof24f2242015-03-03 08:59:20 -0800195 }
196
197 AutoCleanPng autoClean(png_ptr);
198
199 png_infop info_ptr = png_create_info_struct(png_ptr);
halcanary96fcdcc2015-08-27 07:41:13 -0700200 if (info_ptr == nullptr) {
scroggo3eada2a2015-04-01 09:33:23 -0700201 return false;
scroggof24f2242015-03-03 08:59:20 -0800202 }
203
204 autoClean.setInfoPtr(info_ptr);
205
206 // FIXME: Could we use the return value of setjmp to specify the type of
207 // error?
208 if (setjmp(png_jmpbuf(png_ptr))) {
scroggo3eada2a2015-04-01 09:33:23 -0700209 return false;
scroggof24f2242015-03-03 08:59:20 -0800210 }
211
212 png_set_read_fn(png_ptr, static_cast<void*>(stream), sk_read_fn);
213
scroggocf98fa92015-11-23 08:14:40 -0800214#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
msarett133eaaa2016-01-07 11:03:25 -0800215 // Hookup our chunkReader so we can see any user-chunks the caller may be interested in.
216 // This needs to be installed before we read the png header. Android may store ninepatch
217 // chunks in the header.
scroggocf98fa92015-11-23 08:14:40 -0800218 if (chunkReader) {
219 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"", 0);
220 png_set_read_user_chunk_fn(png_ptr, (png_voidp) chunkReader, sk_read_user_chunk);
221 }
222#endif
scroggof24f2242015-03-03 08:59:20 -0800223
224 // The call to png_read_info() gives us all of the information from the
225 // PNG file before the first IDAT (image data chunk).
226 png_read_info(png_ptr, info_ptr);
227 png_uint_32 origWidth, origHeight;
msarett13a91232016-02-01 08:03:29 -0800228 int bitDepth, encodedColorType;
scroggof24f2242015-03-03 08:59:20 -0800229 png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth,
msarett13a91232016-02-01 08:03:29 -0800230 &encodedColorType, nullptr, nullptr, nullptr);
scroggof24f2242015-03-03 08:59:20 -0800231
scroggo6f29a3c2015-07-07 06:09:08 -0700232 if (bitDepthPtr) {
233 *bitDepthPtr = bitDepth;
234 }
235
msarett13a91232016-02-01 08:03:29 -0800236 // Tell libpng to strip 16 bit/color files down to 8 bits/color.
237 // TODO: Should we handle this in SkSwizzler? Could this also benefit
238 // RAW decodes?
scroggof24f2242015-03-03 08:59:20 -0800239 if (bitDepth == 16) {
msarett13a91232016-02-01 08:03:29 -0800240 SkASSERT(PNG_COLOR_TYPE_PALETTE != encodedColorType);
scroggof24f2242015-03-03 08:59:20 -0800241 png_set_strip_16(png_ptr);
242 }
scroggof24f2242015-03-03 08:59:20 -0800243
msarett13a91232016-02-01 08:03:29 -0800244 // Now determine the default colorType and alphaType and set the required transforms.
245 // Often, we depend on SkSwizzler to perform any transforms that we need. However, we
246 // still depend on libpng for many of the rare and PNG-specific cases.
247 SkColorType colorType = kUnknown_SkColorType;
248 SkAlphaType alphaType = kUnknown_SkAlphaType;
249 switch (encodedColorType) {
scroggof24f2242015-03-03 08:59:20 -0800250 case PNG_COLOR_TYPE_PALETTE:
msarett13a91232016-02-01 08:03:29 -0800251 // Extract multiple pixels with bit depths of 1, 2, and 4 from a single
252 // byte into separate bytes (useful for paletted and grayscale images).
253 if (bitDepth < 8) {
254 // TODO: Should we use SkSwizzler here?
255 png_set_packing(png_ptr);
256 }
257
258 colorType = kIndex_8_SkColorType;
259 // Set the alpha type depending on if a transparency chunk exists.
260 alphaType = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ?
scroggof24f2242015-03-03 08:59:20 -0800261 kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
262 break;
scroggo6f29a3c2015-07-07 06:09:08 -0700263 case PNG_COLOR_TYPE_RGB:
msarett13a91232016-02-01 08:03:29 -0800264 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
265 // Convert to RGBA if transparency chunk exists.
scroggo6f29a3c2015-07-07 06:09:08 -0700266 png_set_tRNS_to_alpha(png_ptr);
msarett13a91232016-02-01 08:03:29 -0800267 alphaType = kUnpremul_SkAlphaType;
jvanverth6c90e092015-07-02 10:35:25 -0700268 } else {
msarett13a91232016-02-01 08:03:29 -0800269 alphaType = kOpaque_SkAlphaType;
jvanverth6c90e092015-07-02 10:35:25 -0700270 }
msarett13a91232016-02-01 08:03:29 -0800271 colorType = kN32_SkColorType;
jvanverth6c90e092015-07-02 10:35:25 -0700272 break;
scroggo6f29a3c2015-07-07 06:09:08 -0700273 case PNG_COLOR_TYPE_GRAY:
msarett13a91232016-02-01 08:03:29 -0800274 // Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel.
275 if (bitDepth < 8) {
276 // TODO: Should we use SkSwizzler here?
277 png_set_expand_gray_1_2_4_to_8(png_ptr);
278 }
279
280 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
scroggo6f29a3c2015-07-07 06:09:08 -0700281 png_set_tRNS_to_alpha(png_ptr);
msarett93e613d2016-02-03 10:44:46 -0800282
283 // We will recommend kN32 here since we do not support kGray
284 // with alpha.
msarett13a91232016-02-01 08:03:29 -0800285 colorType = kN32_SkColorType;
286 alphaType = kUnpremul_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700287 } else {
msarett13a91232016-02-01 08:03:29 -0800288 colorType = kGray_8_SkColorType;
289 alphaType = kOpaque_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700290 }
291 break;
292 case PNG_COLOR_TYPE_GRAY_ALPHA:
msarett93e613d2016-02-03 10:44:46 -0800293 // We will recommend kN32 here since we do not support anything
294 // similar to GRAY_ALPHA.
msarett13a91232016-02-01 08:03:29 -0800295 colorType = kN32_SkColorType;
296 alphaType = kUnpremul_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700297 break;
298 case PNG_COLOR_TYPE_RGBA:
msarett13a91232016-02-01 08:03:29 -0800299 colorType = kN32_SkColorType;
300 alphaType = kUnpremul_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700301 break;
302 default:
msarett13a91232016-02-01 08:03:29 -0800303 // All the color types have been covered above.
scroggo6f29a3c2015-07-07 06:09:08 -0700304 SkASSERT(false);
scroggof24f2242015-03-03 08:59:20 -0800305 }
306
scroggo46c57472015-09-30 08:57:13 -0700307 int numberPasses = png_set_interlace_handling(png_ptr);
308 if (numberPassesPtr) {
309 *numberPassesPtr = numberPasses;
310 }
311
msaretta87d6de2016-02-04 15:37:58 -0800312 SkColorProfileType profileType = kLinear_SkColorProfileType;
313 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
314 profileType = kSRGB_SkColorProfileType;
315 }
scroggof24f2242015-03-03 08:59:20 -0800316
scroggo3eada2a2015-04-01 09:33:23 -0700317 if (imageInfo) {
msaretta87d6de2016-02-04 15:37:58 -0800318 *imageInfo = SkImageInfo::Make(origWidth, origHeight, colorType, alphaType, profileType);
scroggo3eada2a2015-04-01 09:33:23 -0700319 }
scroggof24f2242015-03-03 08:59:20 -0800320 autoClean.detach();
scroggo3eada2a2015-04-01 09:33:23 -0700321 if (png_ptrp) {
322 *png_ptrp = png_ptr;
323 }
324 if (info_ptrp) {
325 *info_ptrp = info_ptr;
326 }
scroggo6f29a3c2015-07-07 06:09:08 -0700327
scroggo3eada2a2015-04-01 09:33:23 -0700328 return true;
329}
330
scroggocf98fa92015-11-23 08:14:40 -0800331SkPngCodec::SkPngCodec(const SkImageInfo& info, SkStream* stream, SkPngChunkReader* chunkReader,
scroggo46c57472015-09-30 08:57:13 -0700332 png_structp png_ptr, png_infop info_ptr, int bitDepth, int numberPasses)
scroggof24f2242015-03-03 08:59:20 -0800333 : INHERITED(info, stream)
scroggocf98fa92015-11-23 08:14:40 -0800334 , fPngChunkReader(SkSafeRef(chunkReader))
scroggof24f2242015-03-03 08:59:20 -0800335 , fPng_ptr(png_ptr)
scroggo05245902015-03-25 11:11:52 -0700336 , fInfo_ptr(info_ptr)
337 , fSrcConfig(SkSwizzler::kUnknown)
scroggo46c57472015-09-30 08:57:13 -0700338 , fNumberPasses(numberPasses)
scroggo6f29a3c2015-07-07 06:09:08 -0700339 , fBitDepth(bitDepth)
msaretta4970dc2016-01-11 07:23:23 -0800340{}
scroggof24f2242015-03-03 08:59:20 -0800341
342SkPngCodec::~SkPngCodec() {
scroggo3eada2a2015-04-01 09:33:23 -0700343 this->destroyReadStruct();
344}
345
346void SkPngCodec::destroyReadStruct() {
347 if (fPng_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -0700348 // We will never have a nullptr fInfo_ptr with a non-nullptr fPng_ptr
scroggo3eada2a2015-04-01 09:33:23 -0700349 SkASSERT(fInfo_ptr);
msarett13a91232016-02-01 08:03:29 -0800350 png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -0700351 fPng_ptr = nullptr;
352 fInfo_ptr = nullptr;
scroggo3eada2a2015-04-01 09:33:23 -0700353 }
scroggof24f2242015-03-03 08:59:20 -0800354}
355
356///////////////////////////////////////////////////////////////////////////////
357// Getting the pixels
358///////////////////////////////////////////////////////////////////////////////
359
scroggo05245902015-03-25 11:11:52 -0700360SkCodec::Result SkPngCodec::initializeSwizzler(const SkImageInfo& requestedInfo,
msarett438b2ad2015-04-09 12:43:10 -0700361 const Options& options,
msarett9e43cab2015-04-29 07:38:43 -0700362 SkPMColor ctable[],
msarett438b2ad2015-04-09 12:43:10 -0700363 int* ctableCount) {
scroggof24f2242015-03-03 08:59:20 -0800364 // FIXME: Could we use the return value of setjmp to specify the type of
365 // error?
366 if (setjmp(png_jmpbuf(fPng_ptr))) {
scroggo230d4ac2015-03-26 07:15:55 -0700367 SkCodecPrintf("setjmp long jump!\n");
scroggof24f2242015-03-03 08:59:20 -0800368 return kInvalidInput;
369 }
mtklein372d65c2016-01-27 13:01:41 -0800370 png_read_update_info(fPng_ptr, fInfo_ptr);
scroggof24f2242015-03-03 08:59:20 -0800371
msarett93e613d2016-02-03 10:44:46 -0800372 // suggestedColorType was determined in read_header() based on the encodedColorType
373 const SkColorType suggestedColorType = this->getInfo().colorType();
scroggo6f29a3c2015-07-07 06:09:08 -0700374
msarett93e613d2016-02-03 10:44:46 -0800375 switch (suggestedColorType) {
scroggo6f29a3c2015-07-07 06:09:08 -0700376 case kIndex_8_SkColorType:
377 //decode palette to Skia format
378 fSrcConfig = SkSwizzler::kIndex;
scroggo9b2cdbf2015-07-10 12:07:02 -0700379 if (!this->decodePalette(kPremul_SkAlphaType == requestedInfo.alphaType(),
scroggo6f29a3c2015-07-07 06:09:08 -0700380 ctableCount)) {
381 return kInvalidInput;
382 }
383 break;
384 case kGray_8_SkColorType:
385 fSrcConfig = SkSwizzler::kGray;
mtklein372d65c2016-01-27 13:01:41 -0800386 break;
msarett93e613d2016-02-03 10:44:46 -0800387 case kN32_SkColorType: {
388 const uint8_t encodedColorType = png_get_color_type(fPng_ptr, fInfo_ptr);
389 if (PNG_COLOR_TYPE_GRAY_ALPHA == encodedColorType ||
390 PNG_COLOR_TYPE_GRAY == encodedColorType) {
391 // If encodedColorType is GRAY, there must be a transparent chunk.
392 // Otherwise, suggestedColorType would be kGray. We have already
393 // instructed libpng to convert the transparent chunk to alpha,
394 // so we can treat both GRAY and GRAY_ALPHA as kGrayAlpha.
395 SkASSERT(encodedColorType == PNG_COLOR_TYPE_GRAY_ALPHA ||
396 png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS));
397
398 fSrcConfig = SkSwizzler::kGrayAlpha;
399 } else {
400 if (this->getInfo().alphaType() == kOpaque_SkAlphaType) {
msarettbda86092016-01-19 10:40:12 -0800401 fSrcConfig = SkSwizzler::kRGB;
scroggo6f29a3c2015-07-07 06:09:08 -0700402 } else {
403 fSrcConfig = SkSwizzler::kRGBA;
msarett93e613d2016-02-03 10:44:46 -0800404 }
scroggo6f29a3c2015-07-07 06:09:08 -0700405 }
406 break;
msarett93e613d2016-02-03 10:44:46 -0800407 }
scroggo6f29a3c2015-07-07 06:09:08 -0700408 default:
msarett13a91232016-02-01 08:03:29 -0800409 // We will always recommend one of the above colorTypes.
scroggo6f29a3c2015-07-07 06:09:08 -0700410 SkASSERT(false);
mtklein372d65c2016-01-27 13:01:41 -0800411 }
msarett9e43cab2015-04-29 07:38:43 -0700412
413 // Copy the color table to the client if they request kIndex8 mode
414 copy_color_table(requestedInfo, fColorTable, ctable, ctableCount);
415
416 // Create the swizzler. SkPngCodec retains ownership of the color table.
msarett99f567e2015-08-05 12:58:26 -0700417 const SkPMColor* colors = get_color_ptr(fColorTable.get());
msarettfdb47572015-10-13 12:50:14 -0700418 fSwizzler.reset(SkSwizzler::CreateSwizzler(fSrcConfig, colors, requestedInfo, options));
msarett13a91232016-02-01 08:03:29 -0800419 SkASSERT(fSwizzler);
420
scroggo05245902015-03-25 11:11:52 -0700421 return kSuccess;
422}
423
scroggo6f29a3c2015-07-07 06:09:08 -0700424
scroggob427db12015-08-12 07:24:13 -0700425bool SkPngCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -0700426 // This sets fPng_ptr and fInfo_ptr to nullptr. If read_header
scroggob427db12015-08-12 07:24:13 -0700427 // succeeds, they will be repopulated, and if it fails, they will
halcanary96fcdcc2015-08-27 07:41:13 -0700428 // remain nullptr. Any future accesses to fPng_ptr and fInfo_ptr will
scroggob427db12015-08-12 07:24:13 -0700429 // come through this function which will rewind and again attempt
430 // to reinitialize them.
431 this->destroyReadStruct();
432
433 png_structp png_ptr;
434 png_infop info_ptr;
scroggocf98fa92015-11-23 08:14:40 -0800435 if (!read_header(this->stream(), fPngChunkReader.get(), &png_ptr, &info_ptr,
436 nullptr, nullptr, nullptr)) {
scroggob427db12015-08-12 07:24:13 -0700437 return false;
scroggo58421542015-04-01 11:25:20 -0700438 }
scroggob427db12015-08-12 07:24:13 -0700439
440 fPng_ptr = png_ptr;
441 fInfo_ptr = info_ptr;
442 return true;
scroggo58421542015-04-01 11:25:20 -0700443}
444
scroggo05245902015-03-25 11:11:52 -0700445SkCodec::Result SkPngCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst,
msarett614aa072015-07-27 15:13:17 -0700446 size_t dstRowBytes, const Options& options,
msarette6dd0042015-10-09 11:07:34 -0700447 SkPMColor ctable[], int* ctableCount,
448 int* rowsDecoded) {
scroggo6f29a3c2015-07-07 06:09:08 -0700449 if (!conversion_possible(requestedInfo, this->getInfo())) {
450 return kInvalidConversion;
451 }
scroggob636b452015-07-22 07:16:20 -0700452 if (options.fSubset) {
453 // Subsets are not supported.
454 return kUnimplemented;
455 }
scroggo05245902015-03-25 11:11:52 -0700456
msarett9e43cab2015-04-29 07:38:43 -0700457 // Note that ctable and ctableCount may be modified if there is a color table
msarettfdb47572015-10-13 12:50:14 -0700458 const Result result = this->initializeSwizzler(requestedInfo, options, ctable, ctableCount);
scroggo05245902015-03-25 11:11:52 -0700459 if (result != kSuccess) {
460 return result;
461 }
msarett60dcd3c2016-02-05 15:13:12 -0800462
463 const int width = requestedInfo.width();
464 const int height = requestedInfo.height();
465 const int bpp = SkSwizzler::BytesPerPixel(fSrcConfig);
466 const size_t srcRowBytes = width * bpp;
467
scroggo05245902015-03-25 11:11:52 -0700468 // FIXME: Could we use the return value of setjmp to specify the type of
469 // error?
msarette6dd0042015-10-09 11:07:34 -0700470 int row = 0;
471 // This must be declared above the call to setjmp to avoid memory leaks on incomplete images.
scroggo565901d2015-12-10 10:44:13 -0800472 SkAutoTMalloc<uint8_t> storage;
scroggo05245902015-03-25 11:11:52 -0700473 if (setjmp(png_jmpbuf(fPng_ptr))) {
msarette6dd0042015-10-09 11:07:34 -0700474 // Assume that any error that occurs while reading rows is caused by an incomplete input.
475 if (fNumberPasses > 1) {
476 // FIXME (msarett): Handle incomplete interlaced pngs.
msarett60dcd3c2016-02-05 15:13:12 -0800477 return (row == height) ? kSuccess : kInvalidInput;
msarette6dd0042015-10-09 11:07:34 -0700478 }
479 // FIXME: We do a poor job on incomplete pngs compared to other decoders (ex: Chromium,
480 // Ubuntu Image Viewer). This is because we use the default buffer size in libpng (8192
481 // bytes), and if we can't fill the buffer, we immediately fail.
482 // For example, if we try to read 8192 bytes, and the image (incorrectly) only contains
483 // half that, which may have been enough to contain a non-zero number of lines, we fail
484 // when we could have decoded a few more lines and then failed.
485 // The read function that we provide for libpng has no way of indicating that we have
486 // made a partial read.
487 // Making our buffer size smaller improves our incomplete decodes, but what impact does
488 // it have on regular decode performance? Should we investigate using a different API
msarett60dcd3c2016-02-05 15:13:12 -0800489 // instead of png_read_row? Chromium uses png_process_data.
msarette6dd0042015-10-09 11:07:34 -0700490 *rowsDecoded = row;
msarett60dcd3c2016-02-05 15:13:12 -0800491 return (row == height) ? kSuccess : kIncompleteInput;
scroggo05245902015-03-25 11:11:52 -0700492 }
493
scroggo46c57472015-09-30 08:57:13 -0700494 // FIXME: We could split these out based on subclass.
msarett614aa072015-07-27 15:13:17 -0700495 void* dstRow = dst;
scroggo05245902015-03-25 11:11:52 -0700496 if (fNumberPasses > 1) {
msarett60dcd3c2016-02-05 15:13:12 -0800497 storage.reset(height * srcRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800498 uint8_t* const base = storage.get();
scroggof24f2242015-03-03 08:59:20 -0800499
scroggo05245902015-03-25 11:11:52 -0700500 for (int i = 0; i < fNumberPasses; i++) {
msarett614aa072015-07-27 15:13:17 -0700501 uint8_t* srcRow = base;
scroggof24f2242015-03-03 08:59:20 -0800502 for (int y = 0; y < height; y++) {
msarett60dcd3c2016-02-05 15:13:12 -0800503 png_read_row(fPng_ptr, srcRow, nullptr);
msarett614aa072015-07-27 15:13:17 -0700504 srcRow += srcRowBytes;
scroggof24f2242015-03-03 08:59:20 -0800505 }
506 }
507
508 // Now swizzle it.
msarett614aa072015-07-27 15:13:17 -0700509 uint8_t* srcRow = base;
msarett60dcd3c2016-02-05 15:13:12 -0800510 for (; row < height; row++) {
msaretta4970dc2016-01-11 07:23:23 -0800511 fSwizzler->swizzle(dstRow, srcRow);
msarett614aa072015-07-27 15:13:17 -0700512 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
513 srcRow += srcRowBytes;
scroggof24f2242015-03-03 08:59:20 -0800514 }
515 } else {
msarett60dcd3c2016-02-05 15:13:12 -0800516 storage.reset(srcRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800517 uint8_t* srcRow = storage.get();
msarett60dcd3c2016-02-05 15:13:12 -0800518 for (; row < height; row++) {
519 png_read_row(fPng_ptr, srcRow, nullptr);
msaretta4970dc2016-01-11 07:23:23 -0800520 fSwizzler->swizzle(dstRow, srcRow);
msarett614aa072015-07-27 15:13:17 -0700521 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
scroggof24f2242015-03-03 08:59:20 -0800522 }
523 }
524
emmaleer973ae862015-07-20 13:38:44 -0700525 // read rest of file, and get additional comment and time chunks in info_ptr
scroggo05245902015-03-25 11:11:52 -0700526 png_read_end(fPng_ptr, fInfo_ptr);
scroggo46c57472015-09-30 08:57:13 -0700527
emmaleer973ae862015-07-20 13:38:44 -0700528 return kSuccess;
scroggo05245902015-03-25 11:11:52 -0700529}
530
scroggoc5560be2016-02-03 09:42:42 -0800531uint32_t SkPngCodec::onGetFillValue(SkColorType colorType) const {
msarette6dd0042015-10-09 11:07:34 -0700532 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
533 if (colorPtr) {
534 return get_color_table_fill_value(colorType, colorPtr, 0);
535 }
scroggoc5560be2016-02-03 09:42:42 -0800536 return INHERITED::onGetFillValue(colorType);
msarette6dd0042015-10-09 11:07:34 -0700537}
538
scroggo46c57472015-09-30 08:57:13 -0700539// Subclass of SkPngCodec which supports scanline decoding
540class SkPngScanlineDecoder : public SkPngCodec {
scroggo05245902015-03-25 11:11:52 -0700541public:
scroggo46c57472015-09-30 08:57:13 -0700542 SkPngScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
scroggocf98fa92015-11-23 08:14:40 -0800543 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_ptr, int bitDepth)
544 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, 1)
msarettf724b992015-10-15 06:41:06 -0700545 , fSrcRow(nullptr)
scroggo1c005e42015-08-04 09:24:45 -0700546 {}
547
scroggo46c57472015-09-30 08:57:13 -0700548 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
549 SkPMColor ctable[], int* ctableCount) override {
scroggo1c005e42015-08-04 09:24:45 -0700550 if (!conversion_possible(dstInfo, this->getInfo())) {
scroggo46c57472015-09-30 08:57:13 -0700551 return kInvalidConversion;
scroggo1c005e42015-08-04 09:24:45 -0700552 }
553
scroggo46c57472015-09-30 08:57:13 -0700554 const Result result = this->initializeSwizzler(dstInfo, options, ctable,
555 ctableCount);
556 if (result != kSuccess) {
scroggo1c005e42015-08-04 09:24:45 -0700557 return result;
558 }
559
scroggo46c57472015-09-30 08:57:13 -0700560 fStorage.reset(this->getInfo().width() * SkSwizzler::BytesPerPixel(this->srcConfig()));
scroggo565901d2015-12-10 10:44:13 -0800561 fSrcRow = fStorage.get();
scroggo1c005e42015-08-04 09:24:45 -0700562
scroggo46c57472015-09-30 08:57:13 -0700563 return kSuccess;
scroggo05245902015-03-25 11:11:52 -0700564 }
565
msarette6dd0042015-10-09 11:07:34 -0700566 int onGetScanlines(void* dst, int count, size_t rowBytes) override {
567 // Assume that an error in libpng indicates an incomplete input.
568 int row = 0;
scroggo46c57472015-09-30 08:57:13 -0700569 if (setjmp(png_jmpbuf(this->png_ptr()))) {
scroggo230d4ac2015-03-26 07:15:55 -0700570 SkCodecPrintf("setjmp long jump!\n");
msarette6dd0042015-10-09 11:07:34 -0700571 return row;
scroggo05245902015-03-25 11:11:52 -0700572 }
573
msarett614aa072015-07-27 15:13:17 -0700574 void* dstRow = dst;
msarette6dd0042015-10-09 11:07:34 -0700575 for (; row < count; row++) {
msarett60dcd3c2016-02-05 15:13:12 -0800576 png_read_row(this->png_ptr(), fSrcRow, nullptr);
msaretta4970dc2016-01-11 07:23:23 -0800577 this->swizzler()->swizzle(dstRow, fSrcRow);
msarett614aa072015-07-27 15:13:17 -0700578 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
scroggo05245902015-03-25 11:11:52 -0700579 }
scroggo46c57472015-09-30 08:57:13 -0700580
msarette6dd0042015-10-09 11:07:34 -0700581 return row;
scroggo05245902015-03-25 11:11:52 -0700582 }
583
msarette6dd0042015-10-09 11:07:34 -0700584 bool onSkipScanlines(int count) override {
585 // Assume that an error in libpng indicates an incomplete input.
scroggo46c57472015-09-30 08:57:13 -0700586 if (setjmp(png_jmpbuf(this->png_ptr()))) {
scroggo230d4ac2015-03-26 07:15:55 -0700587 SkCodecPrintf("setjmp long jump!\n");
msarette6dd0042015-10-09 11:07:34 -0700588 return false;
scroggo05245902015-03-25 11:11:52 -0700589 }
msarett60dcd3c2016-02-05 15:13:12 -0800590
msarette6dd0042015-10-09 11:07:34 -0700591 for (int row = 0; row < count; row++) {
msarett60dcd3c2016-02-05 15:13:12 -0800592 png_read_row(this->png_ptr(), fSrcRow, nullptr);
emmaleer7dc91902015-05-27 08:49:04 -0700593 }
msarette6dd0042015-10-09 11:07:34 -0700594 return true;
scroggo05245902015-03-25 11:11:52 -0700595 }
596
scroggo05245902015-03-25 11:11:52 -0700597private:
scroggo565901d2015-12-10 10:44:13 -0800598 SkAutoTMalloc<uint8_t> fStorage;
scroggo9b2cdbf2015-07-10 12:07:02 -0700599 uint8_t* fSrcRow;
scroggo05245902015-03-25 11:11:52 -0700600
scroggo46c57472015-09-30 08:57:13 -0700601 typedef SkPngCodec INHERITED;
scroggo05245902015-03-25 11:11:52 -0700602};
603
emmaleer0a4c3cb2015-06-22 10:40:21 -0700604
scroggo46c57472015-09-30 08:57:13 -0700605class SkPngInterlacedScanlineDecoder : public SkPngCodec {
emmaleer0a4c3cb2015-06-22 10:40:21 -0700606public:
scroggo46c57472015-09-30 08:57:13 -0700607 SkPngInterlacedScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
scroggocf98fa92015-11-23 08:14:40 -0800608 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_ptr,
609 int bitDepth, int numberPasses)
610 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, numberPasses)
scroggo46c57472015-09-30 08:57:13 -0700611 , fHeight(-1)
scroggo1c005e42015-08-04 09:24:45 -0700612 , fCanSkipRewind(false)
scroggo1c005e42015-08-04 09:24:45 -0700613 {
scroggo46c57472015-09-30 08:57:13 -0700614 SkASSERT(numberPasses != 1);
615 }
616
617 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
msarettfdb47572015-10-13 12:50:14 -0700618 SkPMColor ctable[], int* ctableCount) override {
scroggo1c005e42015-08-04 09:24:45 -0700619 if (!conversion_possible(dstInfo, this->getInfo())) {
mtklein372d65c2016-01-27 13:01:41 -0800620 return kInvalidConversion;
scroggo1c005e42015-08-04 09:24:45 -0700621 }
622
msarettfdb47572015-10-13 12:50:14 -0700623 const Result result = this->initializeSwizzler(dstInfo, options, ctable,
624 ctableCount);
625 if (result != kSuccess) {
scroggo1c005e42015-08-04 09:24:45 -0700626 return result;
627 }
628
scroggo1c005e42015-08-04 09:24:45 -0700629 fHeight = dstInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700630 // FIXME: This need not be called on a second call to onStartScanlineDecode.
631 fSrcRowBytes = this->getInfo().width() * SkSwizzler::BytesPerPixel(this->srcConfig());
scroggo1c005e42015-08-04 09:24:45 -0700632 fGarbageRow.reset(fSrcRowBytes);
633 fGarbageRowPtr = static_cast<uint8_t*>(fGarbageRow.get());
634 fCanSkipRewind = true;
635
636 return SkCodec::kSuccess;
637 }
638
msarette6dd0042015-10-09 11:07:34 -0700639 int onGetScanlines(void* dst, int count, size_t dstRowBytes) override {
scroggo1c005e42015-08-04 09:24:45 -0700640 // rewind stream if have previously called onGetScanlines,
641 // since we need entire progressive image to get scanlines
642 if (fCanSkipRewind) {
scroggo46c57472015-09-30 08:57:13 -0700643 // We already rewound in onStartScanlineDecode, so there is no reason to rewind.
scroggo1c005e42015-08-04 09:24:45 -0700644 // Next time onGetScanlines is called, we will need to rewind.
645 fCanSkipRewind = false;
scroggo46c57472015-09-30 08:57:13 -0700646 } else {
647 // rewindIfNeeded resets fCurrScanline, since it assumes that start
648 // needs to be called again before scanline decoding. PNG scanline
649 // decoding is the exception, since it needs to rewind between
650 // calls to getScanlines. Keep track of fCurrScanline, to undo the
651 // reset.
msarette6dd0042015-10-09 11:07:34 -0700652 const int currScanline = this->nextScanline();
scroggo46c57472015-09-30 08:57:13 -0700653 // This method would never be called if currScanline is -1
654 SkASSERT(currScanline != -1);
655
656 if (!this->rewindIfNeeded()) {
657 return kCouldNotRewind;
658 }
msarettcb0d5c92015-12-03 12:23:43 -0800659 this->updateCurrScanline(currScanline);
scroggo1c005e42015-08-04 09:24:45 -0700660 }
661
scroggo46c57472015-09-30 08:57:13 -0700662 if (setjmp(png_jmpbuf(this->png_ptr()))) {
emmaleer0a4c3cb2015-06-22 10:40:21 -0700663 SkCodecPrintf("setjmp long jump!\n");
msarette6dd0042015-10-09 11:07:34 -0700664 // FIXME (msarett): Returning 0 is pessimistic. If we can complete a single pass,
665 // we may be able to report that all of the memory has been initialized. Even if we
666 // fail on the first pass, we can still report than some scanlines are initialized.
667 return 0;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700668 }
scroggo565901d2015-12-10 10:44:13 -0800669 SkAutoTMalloc<uint8_t> storage(count * fSrcRowBytes);
670 uint8_t* storagePtr = storage.get();
emmaleer0a4c3cb2015-06-22 10:40:21 -0700671 uint8_t* srcRow;
msarette6dd0042015-10-09 11:07:34 -0700672 const int startRow = this->nextScanline();
scroggo46c57472015-09-30 08:57:13 -0700673 for (int i = 0; i < this->numberPasses(); i++) {
674 // read rows we planned to skip into garbage row
675 for (int y = 0; y < startRow; y++){
msarett60dcd3c2016-02-05 15:13:12 -0800676 png_read_row(this->png_ptr(), fGarbageRowPtr, nullptr);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700677 }
scroggo46c57472015-09-30 08:57:13 -0700678 // read rows we care about into buffer
emmaleer0a4c3cb2015-06-22 10:40:21 -0700679 srcRow = storagePtr;
680 for (int y = 0; y < count; y++) {
msarett60dcd3c2016-02-05 15:13:12 -0800681 png_read_row(this->png_ptr(), srcRow, nullptr);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700682 srcRow += fSrcRowBytes;
683 }
scroggo46c57472015-09-30 08:57:13 -0700684 // read rows we don't want into garbage buffer
685 for (int y = 0; y < fHeight - startRow - count; y++) {
msarett60dcd3c2016-02-05 15:13:12 -0800686 png_read_row(this->png_ptr(), fGarbageRowPtr, nullptr);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700687 }
688 }
689 //swizzle the rows we care about
690 srcRow = storagePtr;
msarett614aa072015-07-27 15:13:17 -0700691 void* dstRow = dst;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700692 for (int y = 0; y < count; y++) {
msaretta4970dc2016-01-11 07:23:23 -0800693 this->swizzler()->swizzle(dstRow, srcRow);
msarett614aa072015-07-27 15:13:17 -0700694 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700695 srcRow += fSrcRowBytes;
696 }
scroggo46c57472015-09-30 08:57:13 -0700697
msarette6dd0042015-10-09 11:07:34 -0700698 return count;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700699 }
700
msarette6dd0042015-10-09 11:07:34 -0700701 bool onSkipScanlines(int count) override {
scroggo46c57472015-09-30 08:57:13 -0700702 // The non-virtual version will update fCurrScanline.
msarette6dd0042015-10-09 11:07:34 -0700703 return true;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700704 }
705
msarett5406d6f2015-08-31 06:55:13 -0700706 SkScanlineOrder onGetScanlineOrder() const override {
707 return kNone_SkScanlineOrder;
emmaleer8f4ba762015-08-14 07:44:46 -0700708 }
709
emmaleer0a4c3cb2015-06-22 10:40:21 -0700710private:
scroggo9b2cdbf2015-07-10 12:07:02 -0700711 int fHeight;
712 size_t fSrcRowBytes;
713 SkAutoMalloc fGarbageRow;
714 uint8_t* fGarbageRowPtr;
scroggo1c005e42015-08-04 09:24:45 -0700715 // FIXME: This imitates behavior in SkCodec::rewindIfNeeded. That function
716 // is called whenever some action is taken that reads the stream and
717 // therefore the next call will require a rewind. So it modifies a boolean
718 // to note that the *next* time it is called a rewind is needed.
scroggo46c57472015-09-30 08:57:13 -0700719 // SkPngInterlacedScanlineDecoder has an extra wrinkle - calling
720 // onStartScanlineDecode followed by onGetScanlines does *not* require a
721 // rewind. Since rewindIfNeeded does not have this flexibility, we need to
722 // add another layer.
scroggo1c005e42015-08-04 09:24:45 -0700723 bool fCanSkipRewind;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700724
scroggo46c57472015-09-30 08:57:13 -0700725 typedef SkPngCodec INHERITED;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700726};
727
scroggocf98fa92015-11-23 08:14:40 -0800728SkCodec* SkPngCodec::NewFromStream(SkStream* stream, SkPngChunkReader* chunkReader) {
scroggo46c57472015-09-30 08:57:13 -0700729 SkAutoTDelete<SkStream> streamDeleter(stream);
730 png_structp png_ptr;
731 png_infop info_ptr;
732 SkImageInfo imageInfo;
733 int bitDepth;
734 int numberPasses;
735
scroggocf98fa92015-11-23 08:14:40 -0800736 if (!read_header(stream, chunkReader, &png_ptr, &info_ptr, &imageInfo, &bitDepth,
737 &numberPasses)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700738 return nullptr;
scroggo05245902015-03-25 11:11:52 -0700739 }
740
scroggo46c57472015-09-30 08:57:13 -0700741 if (1 == numberPasses) {
scroggocf98fa92015-11-23 08:14:40 -0800742 return new SkPngScanlineDecoder(imageInfo, streamDeleter.detach(), chunkReader,
743 png_ptr, info_ptr, bitDepth);
scroggo05245902015-03-25 11:11:52 -0700744 }
745
scroggocf98fa92015-11-23 08:14:40 -0800746 return new SkPngInterlacedScanlineDecoder(imageInfo, streamDeleter.detach(), chunkReader,
747 png_ptr, info_ptr, bitDepth, numberPasses);
scroggo05245902015-03-25 11:11:52 -0700748}