blob: f946876af2734dd7f660066d68efaa0e91585363 [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"
bungeman5d2cd6e2016-02-23 07:34:25 -080019#include "SkUtils.h"
scroggof24f2242015-03-03 08:59:20 -080020
21///////////////////////////////////////////////////////////////////////////////
scroggof24f2242015-03-03 08:59:20 -080022// Callback functions
23///////////////////////////////////////////////////////////////////////////////
24
25static void sk_error_fn(png_structp png_ptr, png_const_charp msg) {
scroggo230d4ac2015-03-26 07:15:55 -070026 SkCodecPrintf("------ png error %s\n", msg);
scroggof24f2242015-03-03 08:59:20 -080027 longjmp(png_jmpbuf(png_ptr), 1);
28}
29
scroggo0eed6df2015-03-26 10:07:56 -070030void sk_warning_fn(png_structp, png_const_charp msg) {
31 SkCodecPrintf("----- png warning %s\n", msg);
32}
33
scroggof24f2242015-03-03 08:59:20 -080034static void sk_read_fn(png_structp png_ptr, png_bytep data,
35 png_size_t length) {
36 SkStream* stream = static_cast<SkStream*>(png_get_io_ptr(png_ptr));
37 const size_t bytes = stream->read(data, length);
38 if (bytes != length) {
39 // FIXME: We want to report the fact that the stream was truncated.
40 // One way to do that might be to pass a enum to longjmp so setjmp can
41 // specify the failure.
42 png_error(png_ptr, "Read Error!");
43 }
44}
45
scroggocf98fa92015-11-23 08:14:40 -080046#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
47static int sk_read_user_chunk(png_structp png_ptr, png_unknown_chunkp chunk) {
48 SkPngChunkReader* chunkReader = (SkPngChunkReader*)png_get_user_chunk_ptr(png_ptr);
49 // readChunk() returning true means continue decoding
50 return chunkReader->readChunk((const char*)chunk->name, chunk->data, chunk->size) ? 1 : -1;
51}
52#endif
53
scroggof24f2242015-03-03 08:59:20 -080054///////////////////////////////////////////////////////////////////////////////
55// Helpers
56///////////////////////////////////////////////////////////////////////////////
57
58class AutoCleanPng : public SkNoncopyable {
59public:
60 AutoCleanPng(png_structp png_ptr)
61 : fPng_ptr(png_ptr)
halcanary96fcdcc2015-08-27 07:41:13 -070062 , fInfo_ptr(nullptr) {}
scroggof24f2242015-03-03 08:59:20 -080063
64 ~AutoCleanPng() {
halcanary96fcdcc2015-08-27 07:41:13 -070065 // fInfo_ptr will never be non-nullptr unless fPng_ptr is.
scroggof24f2242015-03-03 08:59:20 -080066 if (fPng_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -070067 png_infopp info_pp = fInfo_ptr ? &fInfo_ptr : nullptr;
msarett13a91232016-02-01 08:03:29 -080068 png_destroy_read_struct(&fPng_ptr, info_pp, nullptr);
scroggof24f2242015-03-03 08:59:20 -080069 }
70 }
71
72 void setInfoPtr(png_infop info_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -070073 SkASSERT(nullptr == fInfo_ptr);
scroggof24f2242015-03-03 08:59:20 -080074 fInfo_ptr = info_ptr;
75 }
76
77 void detach() {
halcanary96fcdcc2015-08-27 07:41:13 -070078 fPng_ptr = nullptr;
79 fInfo_ptr = nullptr;
scroggof24f2242015-03-03 08:59:20 -080080 }
81
82private:
83 png_structp fPng_ptr;
84 png_infop fInfo_ptr;
85};
86#define AutoCleanPng(...) SK_REQUIRE_LOCAL_VAR(AutoCleanPng)
87
scroggof24f2242015-03-03 08:59:20 -080088// Method for coverting to either an SkPMColor or a similarly packed
89// unpremultiplied color.
90typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
91
92// Note: SkColorTable claims to store SkPMColors, which is not necessarily
93// the case here.
msarett13a91232016-02-01 08:03:29 -080094// TODO: If we add support for non-native swizzles, we'll need to handle that here.
scroggo9b2cdbf2015-07-10 12:07:02 -070095bool SkPngCodec::decodePalette(bool premultiply, int* ctableCount) {
scroggof24f2242015-03-03 08:59:20 -080096
msarett13a91232016-02-01 08:03:29 -080097 int numColors;
98 png_color* palette;
99 if (!png_get_PLTE(fPng_ptr, fInfo_ptr, &palette, &numColors)) {
scroggo05245902015-03-25 11:11:52 -0700100 return false;
scroggof24f2242015-03-03 08:59:20 -0800101 }
102
msarett13a91232016-02-01 08:03:29 -0800103 // Note: These are not necessarily SkPMColors.
104 SkPMColor colorPtr[256];
scroggof24f2242015-03-03 08:59:20 -0800105
msarett13a91232016-02-01 08:03:29 -0800106 png_bytep alphas;
107 int numColorsWithAlpha = 0;
108 if (png_get_tRNS(fPng_ptr, fInfo_ptr, &alphas, &numColorsWithAlpha, nullptr)) {
109 // Choose which function to use to create the color table. If the final destination's
110 // colortype is unpremultiplied, the color table will store unpremultiplied colors.
111 PackColorProc proc;
112 if (premultiply) {
113 proc = &SkPremultiplyARGBInline;
114 } else {
115 proc = &SkPackARGB32NoCheck;
116 }
117
118 for (int i = 0; i < numColorsWithAlpha; i++) {
119 // We don't have a function in SkOpts that combines a set of alphas with a set
120 // of RGBs. We could write one, but it's hardly worth it, given that this
121 // is such a small fraction of the total decode time.
122 colorPtr[i] = proc(alphas[i], palette->red, palette->green, palette->blue);
123 palette++;
124 }
scroggof24f2242015-03-03 08:59:20 -0800125 }
126
msarett13a91232016-02-01 08:03:29 -0800127 if (numColorsWithAlpha < numColors) {
128 // The optimized code depends on a 3-byte png_color struct with the colors
129 // in RGB order. These checks make sure it is safe to use.
130 static_assert(3 == sizeof(png_color), "png_color struct has changed. Opts are broken.");
131#ifdef SK_DEBUG
132 SkASSERT(&palette->red < &palette->green);
133 SkASSERT(&palette->green < &palette->blue);
134#endif
135
136#ifdef SK_PMCOLOR_IS_RGBA
137 SkOpts::RGB_to_RGB1(colorPtr + numColorsWithAlpha, palette, numColors - numColorsWithAlpha);
138#else
139 SkOpts::RGB_to_BGR1(colorPtr + numColorsWithAlpha, palette, numColors - numColorsWithAlpha);
140#endif
scroggof24f2242015-03-03 08:59:20 -0800141 }
142
msarett13a91232016-02-01 08:03:29 -0800143 // Pad the color table with the last color in the table (or black) in the case that
144 // invalid pixel indices exceed the number of colors in the table.
145 const int maxColors = 1 << fBitDepth;
146 if (numColors < maxColors) {
147 SkPMColor lastColor = numColors > 0 ? colorPtr[numColors - 1] : SK_ColorBLACK;
148 sk_memset32(colorPtr + numColors, lastColor, maxColors - numColors);
scroggof24f2242015-03-03 08:59:20 -0800149 }
150
msarett13a91232016-02-01 08:03:29 -0800151 // Set the new color count.
halcanary96fcdcc2015-08-27 07:41:13 -0700152 if (ctableCount != nullptr) {
msarett13a91232016-02-01 08:03:29 -0800153 *ctableCount = maxColors;
scroggof24f2242015-03-03 08:59:20 -0800154 }
155
msarett13a91232016-02-01 08:03:29 -0800156 fColorTable.reset(new SkColorTable(colorPtr, maxColors));
scroggo05245902015-03-25 11:11:52 -0700157 return true;
scroggof24f2242015-03-03 08:59:20 -0800158}
159
160///////////////////////////////////////////////////////////////////////////////
161// Creation
162///////////////////////////////////////////////////////////////////////////////
163
scroggodb30be22015-12-08 18:54:13 -0800164bool SkPngCodec::IsPng(const char* buf, size_t bytesRead) {
165 return !png_sig_cmp((png_bytep) buf, (png_size_t)0, bytesRead);
scroggof24f2242015-03-03 08:59:20 -0800166}
167
scroggocf98fa92015-11-23 08:14:40 -0800168// Reads the header and initializes the output fields, if not NULL.
169//
170// @param stream Input data. Will be read to get enough information to properly
171// setup the codec.
172// @param chunkReader SkPngChunkReader, for reading unknown chunks. May be NULL.
173// If not NULL, png_ptr will hold an *unowned* pointer to it. The caller is
174// expected to continue to own it for the lifetime of the png_ptr.
175// @param png_ptrp Optional output variable. If non-NULL, will be set to a new
176// png_structp on success.
177// @param info_ptrp Optional output variable. If non-NULL, will be set to a new
178// png_infop on success;
179// @param imageInfo Optional output variable. If non-NULL, will be set to
180// reflect the properties of the encoded image on success.
181// @param bitDepthPtr Optional output variable. If non-NULL, will be set to the
182// bit depth of the encoded image on success.
183// @param numberPassesPtr Optional output variable. If non-NULL, will be set to
184// the number_passes of the encoded image on success.
185// @return true on success, in which case the caller is responsible for calling
186// png_destroy_read_struct(png_ptrp, info_ptrp).
187// If it returns false, the passed in fields (except stream) are unchanged.
188static bool read_header(SkStream* stream, SkPngChunkReader* chunkReader,
189 png_structp* png_ptrp, png_infop* info_ptrp,
190 SkImageInfo* imageInfo, int* bitDepthPtr, int* numberPassesPtr) {
scroggof24f2242015-03-03 08:59:20 -0800191 // The image is known to be a PNG. Decode enough to know the SkImageInfo.
halcanary96fcdcc2015-08-27 07:41:13 -0700192 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
scroggo0eed6df2015-03-26 10:07:56 -0700193 sk_error_fn, sk_warning_fn);
scroggof24f2242015-03-03 08:59:20 -0800194 if (!png_ptr) {
scroggo3eada2a2015-04-01 09:33:23 -0700195 return false;
scroggof24f2242015-03-03 08:59:20 -0800196 }
197
198 AutoCleanPng autoClean(png_ptr);
199
200 png_infop info_ptr = png_create_info_struct(png_ptr);
halcanary96fcdcc2015-08-27 07:41:13 -0700201 if (info_ptr == nullptr) {
scroggo3eada2a2015-04-01 09:33:23 -0700202 return false;
scroggof24f2242015-03-03 08:59:20 -0800203 }
204
205 autoClean.setInfoPtr(info_ptr);
206
207 // FIXME: Could we use the return value of setjmp to specify the type of
208 // error?
209 if (setjmp(png_jmpbuf(png_ptr))) {
scroggo3eada2a2015-04-01 09:33:23 -0700210 return false;
scroggof24f2242015-03-03 08:59:20 -0800211 }
212
213 png_set_read_fn(png_ptr, static_cast<void*>(stream), sk_read_fn);
214
scroggocf98fa92015-11-23 08:14:40 -0800215#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
msarett133eaaa2016-01-07 11:03:25 -0800216 // Hookup our chunkReader so we can see any user-chunks the caller may be interested in.
217 // This needs to be installed before we read the png header. Android may store ninepatch
218 // chunks in the header.
scroggocf98fa92015-11-23 08:14:40 -0800219 if (chunkReader) {
220 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"", 0);
221 png_set_read_user_chunk_fn(png_ptr, (png_voidp) chunkReader, sk_read_user_chunk);
222 }
223#endif
scroggof24f2242015-03-03 08:59:20 -0800224
225 // The call to png_read_info() gives us all of the information from the
226 // PNG file before the first IDAT (image data chunk).
227 png_read_info(png_ptr, info_ptr);
228 png_uint_32 origWidth, origHeight;
msarett13a91232016-02-01 08:03:29 -0800229 int bitDepth, encodedColorType;
scroggof24f2242015-03-03 08:59:20 -0800230 png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth,
msarett13a91232016-02-01 08:03:29 -0800231 &encodedColorType, nullptr, nullptr, nullptr);
scroggof24f2242015-03-03 08:59:20 -0800232
scroggo6f29a3c2015-07-07 06:09:08 -0700233 if (bitDepthPtr) {
234 *bitDepthPtr = bitDepth;
235 }
236
msarett13a91232016-02-01 08:03:29 -0800237 // Tell libpng to strip 16 bit/color files down to 8 bits/color.
238 // TODO: Should we handle this in SkSwizzler? Could this also benefit
239 // RAW decodes?
scroggof24f2242015-03-03 08:59:20 -0800240 if (bitDepth == 16) {
msarett13a91232016-02-01 08:03:29 -0800241 SkASSERT(PNG_COLOR_TYPE_PALETTE != encodedColorType);
scroggof24f2242015-03-03 08:59:20 -0800242 png_set_strip_16(png_ptr);
243 }
scroggof24f2242015-03-03 08:59:20 -0800244
msarett13a91232016-02-01 08:03:29 -0800245 // Now determine the default colorType and alphaType and set the required transforms.
246 // Often, we depend on SkSwizzler to perform any transforms that we need. However, we
247 // still depend on libpng for many of the rare and PNG-specific cases.
248 SkColorType colorType = kUnknown_SkColorType;
249 SkAlphaType alphaType = kUnknown_SkAlphaType;
250 switch (encodedColorType) {
scroggof24f2242015-03-03 08:59:20 -0800251 case PNG_COLOR_TYPE_PALETTE:
msarett13a91232016-02-01 08:03:29 -0800252 // Extract multiple pixels with bit depths of 1, 2, and 4 from a single
253 // byte into separate bytes (useful for paletted and grayscale images).
254 if (bitDepth < 8) {
255 // TODO: Should we use SkSwizzler here?
256 png_set_packing(png_ptr);
257 }
258
259 colorType = kIndex_8_SkColorType;
260 // Set the alpha type depending on if a transparency chunk exists.
261 alphaType = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ?
scroggof24f2242015-03-03 08:59:20 -0800262 kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
263 break;
scroggo6f29a3c2015-07-07 06:09:08 -0700264 case PNG_COLOR_TYPE_RGB:
msarett13a91232016-02-01 08:03:29 -0800265 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
266 // Convert to RGBA if transparency chunk exists.
scroggo6f29a3c2015-07-07 06:09:08 -0700267 png_set_tRNS_to_alpha(png_ptr);
msarett13a91232016-02-01 08:03:29 -0800268 alphaType = kUnpremul_SkAlphaType;
jvanverth6c90e092015-07-02 10:35:25 -0700269 } else {
msarett13a91232016-02-01 08:03:29 -0800270 alphaType = kOpaque_SkAlphaType;
jvanverth6c90e092015-07-02 10:35:25 -0700271 }
msarett13a91232016-02-01 08:03:29 -0800272 colorType = kN32_SkColorType;
jvanverth6c90e092015-07-02 10:35:25 -0700273 break;
scroggo6f29a3c2015-07-07 06:09:08 -0700274 case PNG_COLOR_TYPE_GRAY:
msarett13a91232016-02-01 08:03:29 -0800275 // Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel.
276 if (bitDepth < 8) {
277 // TODO: Should we use SkSwizzler here?
278 png_set_expand_gray_1_2_4_to_8(png_ptr);
279 }
280
281 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
scroggo6f29a3c2015-07-07 06:09:08 -0700282 png_set_tRNS_to_alpha(png_ptr);
msarett93e613d2016-02-03 10:44:46 -0800283
284 // We will recommend kN32 here since we do not support kGray
285 // with alpha.
msarett13a91232016-02-01 08:03:29 -0800286 colorType = kN32_SkColorType;
287 alphaType = kUnpremul_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700288 } else {
msarett13a91232016-02-01 08:03:29 -0800289 colorType = kGray_8_SkColorType;
290 alphaType = kOpaque_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700291 }
292 break;
293 case PNG_COLOR_TYPE_GRAY_ALPHA:
msarett93e613d2016-02-03 10:44:46 -0800294 // We will recommend kN32 here since we do not support anything
295 // similar to GRAY_ALPHA.
msarett13a91232016-02-01 08:03:29 -0800296 colorType = kN32_SkColorType;
297 alphaType = kUnpremul_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700298 break;
299 case PNG_COLOR_TYPE_RGBA:
msarett13a91232016-02-01 08:03:29 -0800300 colorType = kN32_SkColorType;
301 alphaType = kUnpremul_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700302 break;
303 default:
msarett13a91232016-02-01 08:03:29 -0800304 // All the color types have been covered above.
scroggo6f29a3c2015-07-07 06:09:08 -0700305 SkASSERT(false);
scroggof24f2242015-03-03 08:59:20 -0800306 }
307
scroggo46c57472015-09-30 08:57:13 -0700308 int numberPasses = png_set_interlace_handling(png_ptr);
309 if (numberPassesPtr) {
310 *numberPassesPtr = numberPasses;
311 }
312
msaretta87d6de2016-02-04 15:37:58 -0800313 SkColorProfileType profileType = kLinear_SkColorProfileType;
314 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
315 profileType = kSRGB_SkColorProfileType;
316 }
scroggof24f2242015-03-03 08:59:20 -0800317
scroggo3eada2a2015-04-01 09:33:23 -0700318 if (imageInfo) {
msaretta87d6de2016-02-04 15:37:58 -0800319 *imageInfo = SkImageInfo::Make(origWidth, origHeight, colorType, alphaType, profileType);
scroggo3eada2a2015-04-01 09:33:23 -0700320 }
scroggof24f2242015-03-03 08:59:20 -0800321 autoClean.detach();
scroggo3eada2a2015-04-01 09:33:23 -0700322 if (png_ptrp) {
323 *png_ptrp = png_ptr;
324 }
325 if (info_ptrp) {
326 *info_ptrp = info_ptr;
327 }
scroggo6f29a3c2015-07-07 06:09:08 -0700328
scroggo3eada2a2015-04-01 09:33:23 -0700329 return true;
330}
331
scroggocf98fa92015-11-23 08:14:40 -0800332SkPngCodec::SkPngCodec(const SkImageInfo& info, SkStream* stream, SkPngChunkReader* chunkReader,
scroggo46c57472015-09-30 08:57:13 -0700333 png_structp png_ptr, png_infop info_ptr, int bitDepth, int numberPasses)
scroggof24f2242015-03-03 08:59:20 -0800334 : INHERITED(info, stream)
scroggocf98fa92015-11-23 08:14:40 -0800335 , fPngChunkReader(SkSafeRef(chunkReader))
scroggof24f2242015-03-03 08:59:20 -0800336 , fPng_ptr(png_ptr)
scroggo05245902015-03-25 11:11:52 -0700337 , fInfo_ptr(info_ptr)
338 , fSrcConfig(SkSwizzler::kUnknown)
scroggo46c57472015-09-30 08:57:13 -0700339 , fNumberPasses(numberPasses)
scroggo6f29a3c2015-07-07 06:09:08 -0700340 , fBitDepth(bitDepth)
msaretta4970dc2016-01-11 07:23:23 -0800341{}
scroggof24f2242015-03-03 08:59:20 -0800342
343SkPngCodec::~SkPngCodec() {
scroggo3eada2a2015-04-01 09:33:23 -0700344 this->destroyReadStruct();
345}
346
347void SkPngCodec::destroyReadStruct() {
348 if (fPng_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -0700349 // We will never have a nullptr fInfo_ptr with a non-nullptr fPng_ptr
scroggo3eada2a2015-04-01 09:33:23 -0700350 SkASSERT(fInfo_ptr);
msarett13a91232016-02-01 08:03:29 -0800351 png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -0700352 fPng_ptr = nullptr;
353 fInfo_ptr = nullptr;
scroggo3eada2a2015-04-01 09:33:23 -0700354 }
scroggof24f2242015-03-03 08:59:20 -0800355}
356
357///////////////////////////////////////////////////////////////////////////////
358// Getting the pixels
359///////////////////////////////////////////////////////////////////////////////
360
scroggo05245902015-03-25 11:11:52 -0700361SkCodec::Result SkPngCodec::initializeSwizzler(const SkImageInfo& requestedInfo,
msarett438b2ad2015-04-09 12:43:10 -0700362 const Options& options,
msarett9e43cab2015-04-29 07:38:43 -0700363 SkPMColor ctable[],
msarett438b2ad2015-04-09 12:43:10 -0700364 int* ctableCount) {
scroggof24f2242015-03-03 08:59:20 -0800365 // FIXME: Could we use the return value of setjmp to specify the type of
366 // error?
367 if (setjmp(png_jmpbuf(fPng_ptr))) {
scroggo230d4ac2015-03-26 07:15:55 -0700368 SkCodecPrintf("setjmp long jump!\n");
scroggof24f2242015-03-03 08:59:20 -0800369 return kInvalidInput;
370 }
mtklein372d65c2016-01-27 13:01:41 -0800371 png_read_update_info(fPng_ptr, fInfo_ptr);
scroggof24f2242015-03-03 08:59:20 -0800372
msarett93e613d2016-02-03 10:44:46 -0800373 // suggestedColorType was determined in read_header() based on the encodedColorType
374 const SkColorType suggestedColorType = this->getInfo().colorType();
scroggo6f29a3c2015-07-07 06:09:08 -0700375
msarett93e613d2016-02-03 10:44:46 -0800376 switch (suggestedColorType) {
scroggo6f29a3c2015-07-07 06:09:08 -0700377 case kIndex_8_SkColorType:
378 //decode palette to Skia format
379 fSrcConfig = SkSwizzler::kIndex;
scroggo9b2cdbf2015-07-10 12:07:02 -0700380 if (!this->decodePalette(kPremul_SkAlphaType == requestedInfo.alphaType(),
scroggo6f29a3c2015-07-07 06:09:08 -0700381 ctableCount)) {
382 return kInvalidInput;
383 }
384 break;
385 case kGray_8_SkColorType:
386 fSrcConfig = SkSwizzler::kGray;
mtklein372d65c2016-01-27 13:01:41 -0800387 break;
msarett93e613d2016-02-03 10:44:46 -0800388 case kN32_SkColorType: {
389 const uint8_t encodedColorType = png_get_color_type(fPng_ptr, fInfo_ptr);
390 if (PNG_COLOR_TYPE_GRAY_ALPHA == encodedColorType ||
391 PNG_COLOR_TYPE_GRAY == encodedColorType) {
392 // If encodedColorType is GRAY, there must be a transparent chunk.
393 // Otherwise, suggestedColorType would be kGray. We have already
394 // instructed libpng to convert the transparent chunk to alpha,
395 // so we can treat both GRAY and GRAY_ALPHA as kGrayAlpha.
396 SkASSERT(encodedColorType == PNG_COLOR_TYPE_GRAY_ALPHA ||
397 png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS));
398
399 fSrcConfig = SkSwizzler::kGrayAlpha;
400 } else {
401 if (this->getInfo().alphaType() == kOpaque_SkAlphaType) {
msarettbda86092016-01-19 10:40:12 -0800402 fSrcConfig = SkSwizzler::kRGB;
scroggo6f29a3c2015-07-07 06:09:08 -0700403 } else {
404 fSrcConfig = SkSwizzler::kRGBA;
msarett93e613d2016-02-03 10:44:46 -0800405 }
scroggo6f29a3c2015-07-07 06:09:08 -0700406 }
407 break;
msarett93e613d2016-02-03 10:44:46 -0800408 }
scroggo6f29a3c2015-07-07 06:09:08 -0700409 default:
msarett13a91232016-02-01 08:03:29 -0800410 // We will always recommend one of the above colorTypes.
scroggo6f29a3c2015-07-07 06:09:08 -0700411 SkASSERT(false);
mtklein372d65c2016-01-27 13:01:41 -0800412 }
msarett9e43cab2015-04-29 07:38:43 -0700413
414 // Copy the color table to the client if they request kIndex8 mode
415 copy_color_table(requestedInfo, fColorTable, ctable, ctableCount);
416
417 // Create the swizzler. SkPngCodec retains ownership of the color table.
msarett99f567e2015-08-05 12:58:26 -0700418 const SkPMColor* colors = get_color_ptr(fColorTable.get());
msarettfdb47572015-10-13 12:50:14 -0700419 fSwizzler.reset(SkSwizzler::CreateSwizzler(fSrcConfig, colors, requestedInfo, options));
msarett13a91232016-02-01 08:03:29 -0800420 SkASSERT(fSwizzler);
421
scroggo05245902015-03-25 11:11:52 -0700422 return kSuccess;
423}
424
scroggo6f29a3c2015-07-07 06:09:08 -0700425
scroggob427db12015-08-12 07:24:13 -0700426bool SkPngCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -0700427 // This sets fPng_ptr and fInfo_ptr to nullptr. If read_header
scroggob427db12015-08-12 07:24:13 -0700428 // succeeds, they will be repopulated, and if it fails, they will
halcanary96fcdcc2015-08-27 07:41:13 -0700429 // remain nullptr. Any future accesses to fPng_ptr and fInfo_ptr will
scroggob427db12015-08-12 07:24:13 -0700430 // come through this function which will rewind and again attempt
431 // to reinitialize them.
432 this->destroyReadStruct();
433
434 png_structp png_ptr;
435 png_infop info_ptr;
scroggocf98fa92015-11-23 08:14:40 -0800436 if (!read_header(this->stream(), fPngChunkReader.get(), &png_ptr, &info_ptr,
437 nullptr, nullptr, nullptr)) {
scroggob427db12015-08-12 07:24:13 -0700438 return false;
scroggo58421542015-04-01 11:25:20 -0700439 }
scroggob427db12015-08-12 07:24:13 -0700440
441 fPng_ptr = png_ptr;
442 fInfo_ptr = info_ptr;
443 return true;
scroggo58421542015-04-01 11:25:20 -0700444}
445
scroggo05245902015-03-25 11:11:52 -0700446SkCodec::Result SkPngCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst,
msarett614aa072015-07-27 15:13:17 -0700447 size_t dstRowBytes, const Options& options,
msarette6dd0042015-10-09 11:07:34 -0700448 SkPMColor ctable[], int* ctableCount,
449 int* rowsDecoded) {
scroggo6f29a3c2015-07-07 06:09:08 -0700450 if (!conversion_possible(requestedInfo, this->getInfo())) {
451 return kInvalidConversion;
452 }
scroggob636b452015-07-22 07:16:20 -0700453 if (options.fSubset) {
454 // Subsets are not supported.
455 return kUnimplemented;
456 }
scroggo05245902015-03-25 11:11:52 -0700457
msarett9e43cab2015-04-29 07:38:43 -0700458 // Note that ctable and ctableCount may be modified if there is a color table
msarettfdb47572015-10-13 12:50:14 -0700459 const Result result = this->initializeSwizzler(requestedInfo, options, ctable, ctableCount);
scroggo05245902015-03-25 11:11:52 -0700460 if (result != kSuccess) {
461 return result;
462 }
msarett60dcd3c2016-02-05 15:13:12 -0800463
464 const int width = requestedInfo.width();
465 const int height = requestedInfo.height();
466 const int bpp = SkSwizzler::BytesPerPixel(fSrcConfig);
467 const size_t srcRowBytes = width * bpp;
468
scroggo05245902015-03-25 11:11:52 -0700469 // FIXME: Could we use the return value of setjmp to specify the type of
470 // error?
msarette6dd0042015-10-09 11:07:34 -0700471 int row = 0;
472 // This must be declared above the call to setjmp to avoid memory leaks on incomplete images.
scroggo565901d2015-12-10 10:44:13 -0800473 SkAutoTMalloc<uint8_t> storage;
scroggo05245902015-03-25 11:11:52 -0700474 if (setjmp(png_jmpbuf(fPng_ptr))) {
msarette6dd0042015-10-09 11:07:34 -0700475 // Assume that any error that occurs while reading rows is caused by an incomplete input.
476 if (fNumberPasses > 1) {
477 // FIXME (msarett): Handle incomplete interlaced pngs.
msarett60dcd3c2016-02-05 15:13:12 -0800478 return (row == height) ? kSuccess : kInvalidInput;
msarette6dd0042015-10-09 11:07:34 -0700479 }
480 // FIXME: We do a poor job on incomplete pngs compared to other decoders (ex: Chromium,
481 // Ubuntu Image Viewer). This is because we use the default buffer size in libpng (8192
482 // bytes), and if we can't fill the buffer, we immediately fail.
483 // For example, if we try to read 8192 bytes, and the image (incorrectly) only contains
484 // half that, which may have been enough to contain a non-zero number of lines, we fail
485 // when we could have decoded a few more lines and then failed.
486 // The read function that we provide for libpng has no way of indicating that we have
487 // made a partial read.
488 // Making our buffer size smaller improves our incomplete decodes, but what impact does
489 // it have on regular decode performance? Should we investigate using a different API
msarett60dcd3c2016-02-05 15:13:12 -0800490 // instead of png_read_row? Chromium uses png_process_data.
msarette6dd0042015-10-09 11:07:34 -0700491 *rowsDecoded = row;
msarett60dcd3c2016-02-05 15:13:12 -0800492 return (row == height) ? kSuccess : kIncompleteInput;
scroggo05245902015-03-25 11:11:52 -0700493 }
494
scroggo46c57472015-09-30 08:57:13 -0700495 // FIXME: We could split these out based on subclass.
msarett614aa072015-07-27 15:13:17 -0700496 void* dstRow = dst;
scroggo05245902015-03-25 11:11:52 -0700497 if (fNumberPasses > 1) {
msarett60dcd3c2016-02-05 15:13:12 -0800498 storage.reset(height * srcRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800499 uint8_t* const base = storage.get();
scroggof24f2242015-03-03 08:59:20 -0800500
scroggo05245902015-03-25 11:11:52 -0700501 for (int i = 0; i < fNumberPasses; i++) {
msarett614aa072015-07-27 15:13:17 -0700502 uint8_t* srcRow = base;
scroggof24f2242015-03-03 08:59:20 -0800503 for (int y = 0; y < height; y++) {
msarett60dcd3c2016-02-05 15:13:12 -0800504 png_read_row(fPng_ptr, srcRow, nullptr);
msarett614aa072015-07-27 15:13:17 -0700505 srcRow += srcRowBytes;
scroggof24f2242015-03-03 08:59:20 -0800506 }
507 }
508
509 // Now swizzle it.
msarett614aa072015-07-27 15:13:17 -0700510 uint8_t* srcRow = base;
msarett60dcd3c2016-02-05 15:13:12 -0800511 for (; row < height; row++) {
msaretta4970dc2016-01-11 07:23:23 -0800512 fSwizzler->swizzle(dstRow, srcRow);
msarett614aa072015-07-27 15:13:17 -0700513 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
514 srcRow += srcRowBytes;
scroggof24f2242015-03-03 08:59:20 -0800515 }
516 } else {
msarett60dcd3c2016-02-05 15:13:12 -0800517 storage.reset(srcRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800518 uint8_t* srcRow = storage.get();
msarett60dcd3c2016-02-05 15:13:12 -0800519 for (; row < height; row++) {
520 png_read_row(fPng_ptr, srcRow, nullptr);
msaretta4970dc2016-01-11 07:23:23 -0800521 fSwizzler->swizzle(dstRow, srcRow);
msarett614aa072015-07-27 15:13:17 -0700522 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
scroggof24f2242015-03-03 08:59:20 -0800523 }
524 }
525
emmaleer973ae862015-07-20 13:38:44 -0700526 // read rest of file, and get additional comment and time chunks in info_ptr
scroggo05245902015-03-25 11:11:52 -0700527 png_read_end(fPng_ptr, fInfo_ptr);
scroggo46c57472015-09-30 08:57:13 -0700528
emmaleer973ae862015-07-20 13:38:44 -0700529 return kSuccess;
scroggo05245902015-03-25 11:11:52 -0700530}
531
scroggoc5560be2016-02-03 09:42:42 -0800532uint32_t SkPngCodec::onGetFillValue(SkColorType colorType) const {
msarette6dd0042015-10-09 11:07:34 -0700533 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
534 if (colorPtr) {
535 return get_color_table_fill_value(colorType, colorPtr, 0);
536 }
scroggoc5560be2016-02-03 09:42:42 -0800537 return INHERITED::onGetFillValue(colorType);
msarette6dd0042015-10-09 11:07:34 -0700538}
539
scroggo46c57472015-09-30 08:57:13 -0700540// Subclass of SkPngCodec which supports scanline decoding
541class SkPngScanlineDecoder : public SkPngCodec {
scroggo05245902015-03-25 11:11:52 -0700542public:
scroggo46c57472015-09-30 08:57:13 -0700543 SkPngScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
scroggocf98fa92015-11-23 08:14:40 -0800544 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_ptr, int bitDepth)
545 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, 1)
msarettf724b992015-10-15 06:41:06 -0700546 , fSrcRow(nullptr)
scroggo1c005e42015-08-04 09:24:45 -0700547 {}
548
scroggo46c57472015-09-30 08:57:13 -0700549 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
550 SkPMColor ctable[], int* ctableCount) override {
scroggo1c005e42015-08-04 09:24:45 -0700551 if (!conversion_possible(dstInfo, this->getInfo())) {
scroggo46c57472015-09-30 08:57:13 -0700552 return kInvalidConversion;
scroggo1c005e42015-08-04 09:24:45 -0700553 }
554
scroggo46c57472015-09-30 08:57:13 -0700555 const Result result = this->initializeSwizzler(dstInfo, options, ctable,
556 ctableCount);
557 if (result != kSuccess) {
scroggo1c005e42015-08-04 09:24:45 -0700558 return result;
559 }
560
scroggo46c57472015-09-30 08:57:13 -0700561 fStorage.reset(this->getInfo().width() * SkSwizzler::BytesPerPixel(this->srcConfig()));
scroggo565901d2015-12-10 10:44:13 -0800562 fSrcRow = fStorage.get();
scroggo1c005e42015-08-04 09:24:45 -0700563
scroggo46c57472015-09-30 08:57:13 -0700564 return kSuccess;
scroggo05245902015-03-25 11:11:52 -0700565 }
566
msarette6dd0042015-10-09 11:07:34 -0700567 int onGetScanlines(void* dst, int count, size_t rowBytes) override {
568 // Assume that an error in libpng indicates an incomplete input.
569 int row = 0;
scroggo46c57472015-09-30 08:57:13 -0700570 if (setjmp(png_jmpbuf(this->png_ptr()))) {
scroggo230d4ac2015-03-26 07:15:55 -0700571 SkCodecPrintf("setjmp long jump!\n");
msarette6dd0042015-10-09 11:07:34 -0700572 return row;
scroggo05245902015-03-25 11:11:52 -0700573 }
574
msarett614aa072015-07-27 15:13:17 -0700575 void* dstRow = dst;
msarette6dd0042015-10-09 11:07:34 -0700576 for (; row < count; row++) {
msarett60dcd3c2016-02-05 15:13:12 -0800577 png_read_row(this->png_ptr(), fSrcRow, nullptr);
msaretta4970dc2016-01-11 07:23:23 -0800578 this->swizzler()->swizzle(dstRow, fSrcRow);
msarett614aa072015-07-27 15:13:17 -0700579 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
scroggo05245902015-03-25 11:11:52 -0700580 }
scroggo46c57472015-09-30 08:57:13 -0700581
msarette6dd0042015-10-09 11:07:34 -0700582 return row;
scroggo05245902015-03-25 11:11:52 -0700583 }
584
msarette6dd0042015-10-09 11:07:34 -0700585 bool onSkipScanlines(int count) override {
586 // Assume that an error in libpng indicates an incomplete input.
scroggo46c57472015-09-30 08:57:13 -0700587 if (setjmp(png_jmpbuf(this->png_ptr()))) {
scroggo230d4ac2015-03-26 07:15:55 -0700588 SkCodecPrintf("setjmp long jump!\n");
msarette6dd0042015-10-09 11:07:34 -0700589 return false;
scroggo05245902015-03-25 11:11:52 -0700590 }
msarett60dcd3c2016-02-05 15:13:12 -0800591
msarette6dd0042015-10-09 11:07:34 -0700592 for (int row = 0; row < count; row++) {
msarett60dcd3c2016-02-05 15:13:12 -0800593 png_read_row(this->png_ptr(), fSrcRow, nullptr);
emmaleer7dc91902015-05-27 08:49:04 -0700594 }
msarette6dd0042015-10-09 11:07:34 -0700595 return true;
scroggo05245902015-03-25 11:11:52 -0700596 }
597
scroggo05245902015-03-25 11:11:52 -0700598private:
scroggo565901d2015-12-10 10:44:13 -0800599 SkAutoTMalloc<uint8_t> fStorage;
scroggo9b2cdbf2015-07-10 12:07:02 -0700600 uint8_t* fSrcRow;
scroggo05245902015-03-25 11:11:52 -0700601
scroggo46c57472015-09-30 08:57:13 -0700602 typedef SkPngCodec INHERITED;
scroggo05245902015-03-25 11:11:52 -0700603};
604
emmaleer0a4c3cb2015-06-22 10:40:21 -0700605
scroggo46c57472015-09-30 08:57:13 -0700606class SkPngInterlacedScanlineDecoder : public SkPngCodec {
emmaleer0a4c3cb2015-06-22 10:40:21 -0700607public:
scroggo46c57472015-09-30 08:57:13 -0700608 SkPngInterlacedScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
scroggocf98fa92015-11-23 08:14:40 -0800609 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_ptr,
610 int bitDepth, int numberPasses)
611 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, numberPasses)
scroggo46c57472015-09-30 08:57:13 -0700612 , fHeight(-1)
scroggo1c005e42015-08-04 09:24:45 -0700613 , fCanSkipRewind(false)
scroggo1c005e42015-08-04 09:24:45 -0700614 {
scroggo46c57472015-09-30 08:57:13 -0700615 SkASSERT(numberPasses != 1);
616 }
617
618 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
msarettfdb47572015-10-13 12:50:14 -0700619 SkPMColor ctable[], int* ctableCount) override {
scroggo1c005e42015-08-04 09:24:45 -0700620 if (!conversion_possible(dstInfo, this->getInfo())) {
mtklein372d65c2016-01-27 13:01:41 -0800621 return kInvalidConversion;
scroggo1c005e42015-08-04 09:24:45 -0700622 }
623
msarettfdb47572015-10-13 12:50:14 -0700624 const Result result = this->initializeSwizzler(dstInfo, options, ctable,
625 ctableCount);
626 if (result != kSuccess) {
scroggo1c005e42015-08-04 09:24:45 -0700627 return result;
628 }
629
scroggo1c005e42015-08-04 09:24:45 -0700630 fHeight = dstInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700631 // FIXME: This need not be called on a second call to onStartScanlineDecode.
632 fSrcRowBytes = this->getInfo().width() * SkSwizzler::BytesPerPixel(this->srcConfig());
scroggo1c005e42015-08-04 09:24:45 -0700633 fGarbageRow.reset(fSrcRowBytes);
634 fGarbageRowPtr = static_cast<uint8_t*>(fGarbageRow.get());
635 fCanSkipRewind = true;
636
637 return SkCodec::kSuccess;
638 }
639
msarette6dd0042015-10-09 11:07:34 -0700640 int onGetScanlines(void* dst, int count, size_t dstRowBytes) override {
scroggo1c005e42015-08-04 09:24:45 -0700641 // rewind stream if have previously called onGetScanlines,
642 // since we need entire progressive image to get scanlines
643 if (fCanSkipRewind) {
scroggo46c57472015-09-30 08:57:13 -0700644 // We already rewound in onStartScanlineDecode, so there is no reason to rewind.
scroggo1c005e42015-08-04 09:24:45 -0700645 // Next time onGetScanlines is called, we will need to rewind.
646 fCanSkipRewind = false;
scroggo46c57472015-09-30 08:57:13 -0700647 } else {
648 // rewindIfNeeded resets fCurrScanline, since it assumes that start
649 // needs to be called again before scanline decoding. PNG scanline
650 // decoding is the exception, since it needs to rewind between
651 // calls to getScanlines. Keep track of fCurrScanline, to undo the
652 // reset.
msarette6dd0042015-10-09 11:07:34 -0700653 const int currScanline = this->nextScanline();
scroggo46c57472015-09-30 08:57:13 -0700654 // This method would never be called if currScanline is -1
655 SkASSERT(currScanline != -1);
656
657 if (!this->rewindIfNeeded()) {
658 return kCouldNotRewind;
659 }
msarettcb0d5c92015-12-03 12:23:43 -0800660 this->updateCurrScanline(currScanline);
scroggo1c005e42015-08-04 09:24:45 -0700661 }
662
scroggo46c57472015-09-30 08:57:13 -0700663 if (setjmp(png_jmpbuf(this->png_ptr()))) {
emmaleer0a4c3cb2015-06-22 10:40:21 -0700664 SkCodecPrintf("setjmp long jump!\n");
msarette6dd0042015-10-09 11:07:34 -0700665 // FIXME (msarett): Returning 0 is pessimistic. If we can complete a single pass,
666 // we may be able to report that all of the memory has been initialized. Even if we
667 // fail on the first pass, we can still report than some scanlines are initialized.
668 return 0;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700669 }
scroggo565901d2015-12-10 10:44:13 -0800670 SkAutoTMalloc<uint8_t> storage(count * fSrcRowBytes);
671 uint8_t* storagePtr = storage.get();
emmaleer0a4c3cb2015-06-22 10:40:21 -0700672 uint8_t* srcRow;
msarette6dd0042015-10-09 11:07:34 -0700673 const int startRow = this->nextScanline();
scroggo46c57472015-09-30 08:57:13 -0700674 for (int i = 0; i < this->numberPasses(); i++) {
675 // read rows we planned to skip into garbage row
676 for (int y = 0; y < startRow; y++){
msarett60dcd3c2016-02-05 15:13:12 -0800677 png_read_row(this->png_ptr(), fGarbageRowPtr, nullptr);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700678 }
scroggo46c57472015-09-30 08:57:13 -0700679 // read rows we care about into buffer
emmaleer0a4c3cb2015-06-22 10:40:21 -0700680 srcRow = storagePtr;
681 for (int y = 0; y < count; y++) {
msarett60dcd3c2016-02-05 15:13:12 -0800682 png_read_row(this->png_ptr(), srcRow, nullptr);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700683 srcRow += fSrcRowBytes;
684 }
scroggo46c57472015-09-30 08:57:13 -0700685 // read rows we don't want into garbage buffer
686 for (int y = 0; y < fHeight - startRow - count; y++) {
msarett60dcd3c2016-02-05 15:13:12 -0800687 png_read_row(this->png_ptr(), fGarbageRowPtr, nullptr);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700688 }
689 }
690 //swizzle the rows we care about
691 srcRow = storagePtr;
msarett614aa072015-07-27 15:13:17 -0700692 void* dstRow = dst;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700693 for (int y = 0; y < count; y++) {
msaretta4970dc2016-01-11 07:23:23 -0800694 this->swizzler()->swizzle(dstRow, srcRow);
msarett614aa072015-07-27 15:13:17 -0700695 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700696 srcRow += fSrcRowBytes;
697 }
scroggo46c57472015-09-30 08:57:13 -0700698
msarette6dd0042015-10-09 11:07:34 -0700699 return count;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700700 }
701
msarette6dd0042015-10-09 11:07:34 -0700702 bool onSkipScanlines(int count) override {
scroggo46c57472015-09-30 08:57:13 -0700703 // The non-virtual version will update fCurrScanline.
msarette6dd0042015-10-09 11:07:34 -0700704 return true;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700705 }
706
msarett5406d6f2015-08-31 06:55:13 -0700707 SkScanlineOrder onGetScanlineOrder() const override {
708 return kNone_SkScanlineOrder;
emmaleer8f4ba762015-08-14 07:44:46 -0700709 }
710
emmaleer0a4c3cb2015-06-22 10:40:21 -0700711private:
scroggo9b2cdbf2015-07-10 12:07:02 -0700712 int fHeight;
713 size_t fSrcRowBytes;
714 SkAutoMalloc fGarbageRow;
715 uint8_t* fGarbageRowPtr;
scroggo1c005e42015-08-04 09:24:45 -0700716 // FIXME: This imitates behavior in SkCodec::rewindIfNeeded. That function
717 // is called whenever some action is taken that reads the stream and
718 // therefore the next call will require a rewind. So it modifies a boolean
719 // to note that the *next* time it is called a rewind is needed.
scroggo46c57472015-09-30 08:57:13 -0700720 // SkPngInterlacedScanlineDecoder has an extra wrinkle - calling
721 // onStartScanlineDecode followed by onGetScanlines does *not* require a
722 // rewind. Since rewindIfNeeded does not have this flexibility, we need to
723 // add another layer.
scroggo1c005e42015-08-04 09:24:45 -0700724 bool fCanSkipRewind;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700725
scroggo46c57472015-09-30 08:57:13 -0700726 typedef SkPngCodec INHERITED;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700727};
728
scroggocf98fa92015-11-23 08:14:40 -0800729SkCodec* SkPngCodec::NewFromStream(SkStream* stream, SkPngChunkReader* chunkReader) {
scroggo46c57472015-09-30 08:57:13 -0700730 SkAutoTDelete<SkStream> streamDeleter(stream);
731 png_structp png_ptr;
732 png_infop info_ptr;
733 SkImageInfo imageInfo;
734 int bitDepth;
735 int numberPasses;
736
scroggocf98fa92015-11-23 08:14:40 -0800737 if (!read_header(stream, chunkReader, &png_ptr, &info_ptr, &imageInfo, &bitDepth,
738 &numberPasses)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700739 return nullptr;
scroggo05245902015-03-25 11:11:52 -0700740 }
741
scroggo46c57472015-09-30 08:57:13 -0700742 if (1 == numberPasses) {
scroggocf98fa92015-11-23 08:14:40 -0800743 return new SkPngScanlineDecoder(imageInfo, streamDeleter.detach(), chunkReader,
744 png_ptr, info_ptr, bitDepth);
scroggo05245902015-03-25 11:11:52 -0700745 }
746
scroggocf98fa92015-11-23 08:14:40 -0800747 return new SkPngInterlacedScanlineDecoder(imageInfo, streamDeleter.detach(), chunkReader,
748 png_ptr, info_ptr, bitDepth, numberPasses);
scroggo05245902015-03-25 11:11:52 -0700749}