blob: 54a82c4b278c97421daee021303d25776d3bf6cc [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"
mtklein372d65c2016-01-27 13:01:41 -080015#include "SkPngFilters.h"
scroggof24f2242015-03-03 08:59:20 -080016#include "SkSize.h"
17#include "SkStream.h"
18#include "SkSwizzler.h"
scroggo565901d2015-12-10 10:44:13 -080019#include "SkTemplates.h"
scroggof24f2242015-03-03 08:59:20 -080020
mtklein62358e72016-01-27 18:26:31 -080021// png_struct::read_filter[] was added in libpng 1.5.7.
22#if defined(__SSE2__) && PNG_LIBPNG_VER >= 10507
mtklein372d65c2016-01-27 13:01:41 -080023 #include "pngstruct.h"
24
25 extern "C" void sk_png_init_filter_functions_sse2(png_structp png, unsigned int bpp) {
26 if (bpp == 3) {
27 png->read_filter[PNG_FILTER_VALUE_SUB -1] = sk_sub3_sse2;
28 png->read_filter[PNG_FILTER_VALUE_AVG -1] = sk_avg3_sse2;
29 png->read_filter[PNG_FILTER_VALUE_PAETH-1] = sk_paeth3_sse2;
30 }
31 if (bpp == 4) {
32 png->read_filter[PNG_FILTER_VALUE_SUB -1] = sk_sub4_sse2;
33 png->read_filter[PNG_FILTER_VALUE_AVG -1] = sk_avg4_sse2;
34 png->read_filter[PNG_FILTER_VALUE_PAETH-1] = sk_paeth4_sse2;
35 }
36 }
37#endif
38
scroggof24f2242015-03-03 08:59:20 -080039///////////////////////////////////////////////////////////////////////////////
scroggof24f2242015-03-03 08:59:20 -080040// Callback functions
41///////////////////////////////////////////////////////////////////////////////
42
43static void sk_error_fn(png_structp png_ptr, png_const_charp msg) {
scroggo230d4ac2015-03-26 07:15:55 -070044 SkCodecPrintf("------ png error %s\n", msg);
scroggof24f2242015-03-03 08:59:20 -080045 longjmp(png_jmpbuf(png_ptr), 1);
46}
47
scroggo0eed6df2015-03-26 10:07:56 -070048void sk_warning_fn(png_structp, png_const_charp msg) {
49 SkCodecPrintf("----- png warning %s\n", msg);
50}
51
scroggof24f2242015-03-03 08:59:20 -080052static void sk_read_fn(png_structp png_ptr, png_bytep data,
53 png_size_t length) {
54 SkStream* stream = static_cast<SkStream*>(png_get_io_ptr(png_ptr));
55 const size_t bytes = stream->read(data, length);
56 if (bytes != length) {
57 // FIXME: We want to report the fact that the stream was truncated.
58 // One way to do that might be to pass a enum to longjmp so setjmp can
59 // specify the failure.
60 png_error(png_ptr, "Read Error!");
61 }
62}
63
scroggocf98fa92015-11-23 08:14:40 -080064#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
65static int sk_read_user_chunk(png_structp png_ptr, png_unknown_chunkp chunk) {
66 SkPngChunkReader* chunkReader = (SkPngChunkReader*)png_get_user_chunk_ptr(png_ptr);
67 // readChunk() returning true means continue decoding
68 return chunkReader->readChunk((const char*)chunk->name, chunk->data, chunk->size) ? 1 : -1;
69}
70#endif
71
scroggof24f2242015-03-03 08:59:20 -080072///////////////////////////////////////////////////////////////////////////////
73// Helpers
74///////////////////////////////////////////////////////////////////////////////
75
76class AutoCleanPng : public SkNoncopyable {
77public:
78 AutoCleanPng(png_structp png_ptr)
79 : fPng_ptr(png_ptr)
halcanary96fcdcc2015-08-27 07:41:13 -070080 , fInfo_ptr(nullptr) {}
scroggof24f2242015-03-03 08:59:20 -080081
82 ~AutoCleanPng() {
halcanary96fcdcc2015-08-27 07:41:13 -070083 // fInfo_ptr will never be non-nullptr unless fPng_ptr is.
scroggof24f2242015-03-03 08:59:20 -080084 if (fPng_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -070085 png_infopp info_pp = fInfo_ptr ? &fInfo_ptr : nullptr;
msarett13a91232016-02-01 08:03:29 -080086 png_destroy_read_struct(&fPng_ptr, info_pp, nullptr);
scroggof24f2242015-03-03 08:59:20 -080087 }
88 }
89
90 void setInfoPtr(png_infop info_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -070091 SkASSERT(nullptr == fInfo_ptr);
scroggof24f2242015-03-03 08:59:20 -080092 fInfo_ptr = info_ptr;
93 }
94
95 void detach() {
halcanary96fcdcc2015-08-27 07:41:13 -070096 fPng_ptr = nullptr;
97 fInfo_ptr = nullptr;
scroggof24f2242015-03-03 08:59:20 -080098 }
99
100private:
101 png_structp fPng_ptr;
102 png_infop fInfo_ptr;
103};
104#define AutoCleanPng(...) SK_REQUIRE_LOCAL_VAR(AutoCleanPng)
105
scroggof24f2242015-03-03 08:59:20 -0800106// Method for coverting to either an SkPMColor or a similarly packed
107// unpremultiplied color.
108typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b);
109
110// Note: SkColorTable claims to store SkPMColors, which is not necessarily
111// the case here.
msarett13a91232016-02-01 08:03:29 -0800112// TODO: If we add support for non-native swizzles, we'll need to handle that here.
scroggo9b2cdbf42015-07-10 12:07:02 -0700113bool SkPngCodec::decodePalette(bool premultiply, int* ctableCount) {
scroggof24f2242015-03-03 08:59:20 -0800114
msarett13a91232016-02-01 08:03:29 -0800115 int numColors;
116 png_color* palette;
117 if (!png_get_PLTE(fPng_ptr, fInfo_ptr, &palette, &numColors)) {
scroggo05245902015-03-25 11:11:52 -0700118 return false;
scroggof24f2242015-03-03 08:59:20 -0800119 }
120
msarett13a91232016-02-01 08:03:29 -0800121 // Note: These are not necessarily SkPMColors.
122 SkPMColor colorPtr[256];
scroggof24f2242015-03-03 08:59:20 -0800123
msarett13a91232016-02-01 08:03:29 -0800124 png_bytep alphas;
125 int numColorsWithAlpha = 0;
126 if (png_get_tRNS(fPng_ptr, fInfo_ptr, &alphas, &numColorsWithAlpha, nullptr)) {
127 // Choose which function to use to create the color table. If the final destination's
128 // colortype is unpremultiplied, the color table will store unpremultiplied colors.
129 PackColorProc proc;
130 if (premultiply) {
131 proc = &SkPremultiplyARGBInline;
132 } else {
133 proc = &SkPackARGB32NoCheck;
134 }
135
136 for (int i = 0; i < numColorsWithAlpha; i++) {
137 // We don't have a function in SkOpts that combines a set of alphas with a set
138 // of RGBs. We could write one, but it's hardly worth it, given that this
139 // is such a small fraction of the total decode time.
140 colorPtr[i] = proc(alphas[i], palette->red, palette->green, palette->blue);
141 palette++;
142 }
scroggof24f2242015-03-03 08:59:20 -0800143 }
144
msarett13a91232016-02-01 08:03:29 -0800145 if (numColorsWithAlpha < numColors) {
146 // The optimized code depends on a 3-byte png_color struct with the colors
147 // in RGB order. These checks make sure it is safe to use.
148 static_assert(3 == sizeof(png_color), "png_color struct has changed. Opts are broken.");
149#ifdef SK_DEBUG
150 SkASSERT(&palette->red < &palette->green);
151 SkASSERT(&palette->green < &palette->blue);
152#endif
153
154#ifdef SK_PMCOLOR_IS_RGBA
155 SkOpts::RGB_to_RGB1(colorPtr + numColorsWithAlpha, palette, numColors - numColorsWithAlpha);
156#else
157 SkOpts::RGB_to_BGR1(colorPtr + numColorsWithAlpha, palette, numColors - numColorsWithAlpha);
158#endif
scroggof24f2242015-03-03 08:59:20 -0800159 }
160
msarett13a91232016-02-01 08:03:29 -0800161 // Pad the color table with the last color in the table (or black) in the case that
162 // invalid pixel indices exceed the number of colors in the table.
163 const int maxColors = 1 << fBitDepth;
164 if (numColors < maxColors) {
165 SkPMColor lastColor = numColors > 0 ? colorPtr[numColors - 1] : SK_ColorBLACK;
166 sk_memset32(colorPtr + numColors, lastColor, maxColors - numColors);
scroggof24f2242015-03-03 08:59:20 -0800167 }
168
msarett13a91232016-02-01 08:03:29 -0800169 // Set the new color count.
halcanary96fcdcc2015-08-27 07:41:13 -0700170 if (ctableCount != nullptr) {
msarett13a91232016-02-01 08:03:29 -0800171 *ctableCount = maxColors;
scroggof24f2242015-03-03 08:59:20 -0800172 }
173
msarett13a91232016-02-01 08:03:29 -0800174 fColorTable.reset(new SkColorTable(colorPtr, maxColors));
scroggo05245902015-03-25 11:11:52 -0700175 return true;
scroggof24f2242015-03-03 08:59:20 -0800176}
177
178///////////////////////////////////////////////////////////////////////////////
179// Creation
180///////////////////////////////////////////////////////////////////////////////
181
scroggodb30be22015-12-08 18:54:13 -0800182bool SkPngCodec::IsPng(const char* buf, size_t bytesRead) {
183 return !png_sig_cmp((png_bytep) buf, (png_size_t)0, bytesRead);
scroggof24f2242015-03-03 08:59:20 -0800184}
185
scroggocf98fa92015-11-23 08:14:40 -0800186// Reads the header and initializes the output fields, if not NULL.
187//
188// @param stream Input data. Will be read to get enough information to properly
189// setup the codec.
190// @param chunkReader SkPngChunkReader, for reading unknown chunks. May be NULL.
191// If not NULL, png_ptr will hold an *unowned* pointer to it. The caller is
192// expected to continue to own it for the lifetime of the png_ptr.
193// @param png_ptrp Optional output variable. If non-NULL, will be set to a new
194// png_structp on success.
195// @param info_ptrp Optional output variable. If non-NULL, will be set to a new
196// png_infop on success;
197// @param imageInfo Optional output variable. If non-NULL, will be set to
198// reflect the properties of the encoded image on success.
199// @param bitDepthPtr Optional output variable. If non-NULL, will be set to the
200// bit depth of the encoded image on success.
201// @param numberPassesPtr Optional output variable. If non-NULL, will be set to
202// the number_passes of the encoded image on success.
203// @return true on success, in which case the caller is responsible for calling
204// png_destroy_read_struct(png_ptrp, info_ptrp).
205// If it returns false, the passed in fields (except stream) are unchanged.
206static bool read_header(SkStream* stream, SkPngChunkReader* chunkReader,
207 png_structp* png_ptrp, png_infop* info_ptrp,
208 SkImageInfo* imageInfo, int* bitDepthPtr, int* numberPassesPtr) {
scroggof24f2242015-03-03 08:59:20 -0800209 // The image is known to be a PNG. Decode enough to know the SkImageInfo.
halcanary96fcdcc2015-08-27 07:41:13 -0700210 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
scroggo0eed6df2015-03-26 10:07:56 -0700211 sk_error_fn, sk_warning_fn);
scroggof24f2242015-03-03 08:59:20 -0800212 if (!png_ptr) {
scroggo3eada2a2015-04-01 09:33:23 -0700213 return false;
scroggof24f2242015-03-03 08:59:20 -0800214 }
215
216 AutoCleanPng autoClean(png_ptr);
217
218 png_infop info_ptr = png_create_info_struct(png_ptr);
halcanary96fcdcc2015-08-27 07:41:13 -0700219 if (info_ptr == nullptr) {
scroggo3eada2a2015-04-01 09:33:23 -0700220 return false;
scroggof24f2242015-03-03 08:59:20 -0800221 }
222
223 autoClean.setInfoPtr(info_ptr);
224
225 // FIXME: Could we use the return value of setjmp to specify the type of
226 // error?
227 if (setjmp(png_jmpbuf(png_ptr))) {
scroggo3eada2a2015-04-01 09:33:23 -0700228 return false;
scroggof24f2242015-03-03 08:59:20 -0800229 }
230
231 png_set_read_fn(png_ptr, static_cast<void*>(stream), sk_read_fn);
232
scroggocf98fa92015-11-23 08:14:40 -0800233#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
msarett133eaaa2016-01-07 11:03:25 -0800234 // Hookup our chunkReader so we can see any user-chunks the caller may be interested in.
235 // This needs to be installed before we read the png header. Android may store ninepatch
236 // chunks in the header.
scroggocf98fa92015-11-23 08:14:40 -0800237 if (chunkReader) {
238 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"", 0);
239 png_set_read_user_chunk_fn(png_ptr, (png_voidp) chunkReader, sk_read_user_chunk);
240 }
241#endif
scroggof24f2242015-03-03 08:59:20 -0800242
243 // The call to png_read_info() gives us all of the information from the
244 // PNG file before the first IDAT (image data chunk).
245 png_read_info(png_ptr, info_ptr);
246 png_uint_32 origWidth, origHeight;
msarett13a91232016-02-01 08:03:29 -0800247 int bitDepth, encodedColorType;
scroggof24f2242015-03-03 08:59:20 -0800248 png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth,
msarett13a91232016-02-01 08:03:29 -0800249 &encodedColorType, nullptr, nullptr, nullptr);
scroggof24f2242015-03-03 08:59:20 -0800250
scroggo6f29a3c2015-07-07 06:09:08 -0700251 if (bitDepthPtr) {
252 *bitDepthPtr = bitDepth;
253 }
254
msarett13a91232016-02-01 08:03:29 -0800255 // Tell libpng to strip 16 bit/color files down to 8 bits/color.
256 // TODO: Should we handle this in SkSwizzler? Could this also benefit
257 // RAW decodes?
scroggof24f2242015-03-03 08:59:20 -0800258 if (bitDepth == 16) {
msarett13a91232016-02-01 08:03:29 -0800259 SkASSERT(PNG_COLOR_TYPE_PALETTE != encodedColorType);
scroggof24f2242015-03-03 08:59:20 -0800260 png_set_strip_16(png_ptr);
261 }
scroggof24f2242015-03-03 08:59:20 -0800262
msarett13a91232016-02-01 08:03:29 -0800263 // Now determine the default colorType and alphaType and set the required transforms.
264 // Often, we depend on SkSwizzler to perform any transforms that we need. However, we
265 // still depend on libpng for many of the rare and PNG-specific cases.
266 SkColorType colorType = kUnknown_SkColorType;
267 SkAlphaType alphaType = kUnknown_SkAlphaType;
268 switch (encodedColorType) {
scroggof24f2242015-03-03 08:59:20 -0800269 case PNG_COLOR_TYPE_PALETTE:
msarett13a91232016-02-01 08:03:29 -0800270 // Extract multiple pixels with bit depths of 1, 2, and 4 from a single
271 // byte into separate bytes (useful for paletted and grayscale images).
272 if (bitDepth < 8) {
273 // TODO: Should we use SkSwizzler here?
274 png_set_packing(png_ptr);
275 }
276
277 colorType = kIndex_8_SkColorType;
278 // Set the alpha type depending on if a transparency chunk exists.
279 alphaType = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ?
scroggof24f2242015-03-03 08:59:20 -0800280 kUnpremul_SkAlphaType : kOpaque_SkAlphaType;
281 break;
scroggo6f29a3c2015-07-07 06:09:08 -0700282 case PNG_COLOR_TYPE_RGB:
msarett13a91232016-02-01 08:03:29 -0800283 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
284 // Convert to RGBA if transparency chunk exists.
scroggo6f29a3c2015-07-07 06:09:08 -0700285 png_set_tRNS_to_alpha(png_ptr);
msarett13a91232016-02-01 08:03:29 -0800286 alphaType = kUnpremul_SkAlphaType;
jvanverth6c90e092015-07-02 10:35:25 -0700287 } else {
msarett13a91232016-02-01 08:03:29 -0800288 alphaType = kOpaque_SkAlphaType;
jvanverth6c90e092015-07-02 10:35:25 -0700289 }
msarett13a91232016-02-01 08:03:29 -0800290 colorType = kN32_SkColorType;
jvanverth6c90e092015-07-02 10:35:25 -0700291 break;
scroggo6f29a3c2015-07-07 06:09:08 -0700292 case PNG_COLOR_TYPE_GRAY:
msarett13a91232016-02-01 08:03:29 -0800293 // Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel.
294 if (bitDepth < 8) {
295 // TODO: Should we use SkSwizzler here?
296 png_set_expand_gray_1_2_4_to_8(png_ptr);
297 }
298
299 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
scroggo6f29a3c2015-07-07 06:09:08 -0700300 png_set_tRNS_to_alpha(png_ptr);
msarett93e613d2016-02-03 10:44:46 -0800301
302 // We will recommend kN32 here since we do not support kGray
303 // with alpha.
msarett13a91232016-02-01 08:03:29 -0800304 colorType = kN32_SkColorType;
305 alphaType = kUnpremul_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700306 } else {
msarett13a91232016-02-01 08:03:29 -0800307 colorType = kGray_8_SkColorType;
308 alphaType = kOpaque_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700309 }
310 break;
311 case PNG_COLOR_TYPE_GRAY_ALPHA:
msarett93e613d2016-02-03 10:44:46 -0800312 // We will recommend kN32 here since we do not support anything
313 // similar to GRAY_ALPHA.
msarett13a91232016-02-01 08:03:29 -0800314 colorType = kN32_SkColorType;
315 alphaType = kUnpremul_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700316 break;
317 case PNG_COLOR_TYPE_RGBA:
msarett13a91232016-02-01 08:03:29 -0800318 colorType = kN32_SkColorType;
319 alphaType = kUnpremul_SkAlphaType;
scroggo6f29a3c2015-07-07 06:09:08 -0700320 break;
321 default:
msarett13a91232016-02-01 08:03:29 -0800322 // All the color types have been covered above.
scroggo6f29a3c2015-07-07 06:09:08 -0700323 SkASSERT(false);
scroggof24f2242015-03-03 08:59:20 -0800324 }
325
scroggo46c57472015-09-30 08:57:13 -0700326 int numberPasses = png_set_interlace_handling(png_ptr);
327 if (numberPassesPtr) {
328 *numberPassesPtr = numberPasses;
329 }
330
msaretta87d6de2016-02-04 15:37:58 -0800331 SkColorProfileType profileType = kLinear_SkColorProfileType;
332 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
333 profileType = kSRGB_SkColorProfileType;
334 }
scroggof24f2242015-03-03 08:59:20 -0800335
scroggo3eada2a2015-04-01 09:33:23 -0700336 if (imageInfo) {
msaretta87d6de2016-02-04 15:37:58 -0800337 *imageInfo = SkImageInfo::Make(origWidth, origHeight, colorType, alphaType, profileType);
scroggo3eada2a2015-04-01 09:33:23 -0700338 }
scroggof24f2242015-03-03 08:59:20 -0800339 autoClean.detach();
scroggo3eada2a2015-04-01 09:33:23 -0700340 if (png_ptrp) {
341 *png_ptrp = png_ptr;
342 }
343 if (info_ptrp) {
344 *info_ptrp = info_ptr;
345 }
scroggo6f29a3c2015-07-07 06:09:08 -0700346
scroggo3eada2a2015-04-01 09:33:23 -0700347 return true;
348}
349
scroggocf98fa92015-11-23 08:14:40 -0800350SkPngCodec::SkPngCodec(const SkImageInfo& info, SkStream* stream, SkPngChunkReader* chunkReader,
scroggo46c57472015-09-30 08:57:13 -0700351 png_structp png_ptr, png_infop info_ptr, int bitDepth, int numberPasses)
scroggof24f2242015-03-03 08:59:20 -0800352 : INHERITED(info, stream)
scroggocf98fa92015-11-23 08:14:40 -0800353 , fPngChunkReader(SkSafeRef(chunkReader))
scroggof24f2242015-03-03 08:59:20 -0800354 , fPng_ptr(png_ptr)
scroggo05245902015-03-25 11:11:52 -0700355 , fInfo_ptr(info_ptr)
356 , fSrcConfig(SkSwizzler::kUnknown)
scroggo46c57472015-09-30 08:57:13 -0700357 , fNumberPasses(numberPasses)
scroggo6f29a3c2015-07-07 06:09:08 -0700358 , fBitDepth(bitDepth)
msaretta4970dc2016-01-11 07:23:23 -0800359{}
scroggof24f2242015-03-03 08:59:20 -0800360
361SkPngCodec::~SkPngCodec() {
scroggo3eada2a2015-04-01 09:33:23 -0700362 this->destroyReadStruct();
363}
364
365void SkPngCodec::destroyReadStruct() {
366 if (fPng_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -0700367 // We will never have a nullptr fInfo_ptr with a non-nullptr fPng_ptr
scroggo3eada2a2015-04-01 09:33:23 -0700368 SkASSERT(fInfo_ptr);
msarett13a91232016-02-01 08:03:29 -0800369 png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -0700370 fPng_ptr = nullptr;
371 fInfo_ptr = nullptr;
scroggo3eada2a2015-04-01 09:33:23 -0700372 }
scroggof24f2242015-03-03 08:59:20 -0800373}
374
375///////////////////////////////////////////////////////////////////////////////
376// Getting the pixels
377///////////////////////////////////////////////////////////////////////////////
378
scroggo05245902015-03-25 11:11:52 -0700379SkCodec::Result SkPngCodec::initializeSwizzler(const SkImageInfo& requestedInfo,
msarett438b2ad2015-04-09 12:43:10 -0700380 const Options& options,
msarett9e43cab2015-04-29 07:38:43 -0700381 SkPMColor ctable[],
msarett438b2ad2015-04-09 12:43:10 -0700382 int* ctableCount) {
scroggof24f2242015-03-03 08:59:20 -0800383 // FIXME: Could we use the return value of setjmp to specify the type of
384 // error?
385 if (setjmp(png_jmpbuf(fPng_ptr))) {
scroggo230d4ac2015-03-26 07:15:55 -0700386 SkCodecPrintf("setjmp long jump!\n");
scroggof24f2242015-03-03 08:59:20 -0800387 return kInvalidInput;
388 }
mtklein372d65c2016-01-27 13:01:41 -0800389 png_read_update_info(fPng_ptr, fInfo_ptr);
scroggof24f2242015-03-03 08:59:20 -0800390
msarett93e613d2016-02-03 10:44:46 -0800391 // suggestedColorType was determined in read_header() based on the encodedColorType
392 const SkColorType suggestedColorType = this->getInfo().colorType();
scroggo6f29a3c2015-07-07 06:09:08 -0700393
msarett93e613d2016-02-03 10:44:46 -0800394 switch (suggestedColorType) {
scroggo6f29a3c2015-07-07 06:09:08 -0700395 case kIndex_8_SkColorType:
396 //decode palette to Skia format
397 fSrcConfig = SkSwizzler::kIndex;
scroggo9b2cdbf42015-07-10 12:07:02 -0700398 if (!this->decodePalette(kPremul_SkAlphaType == requestedInfo.alphaType(),
scroggo6f29a3c2015-07-07 06:09:08 -0700399 ctableCount)) {
400 return kInvalidInput;
401 }
402 break;
403 case kGray_8_SkColorType:
404 fSrcConfig = SkSwizzler::kGray;
mtklein372d65c2016-01-27 13:01:41 -0800405 break;
msarett93e613d2016-02-03 10:44:46 -0800406 case kN32_SkColorType: {
407 const uint8_t encodedColorType = png_get_color_type(fPng_ptr, fInfo_ptr);
408 if (PNG_COLOR_TYPE_GRAY_ALPHA == encodedColorType ||
409 PNG_COLOR_TYPE_GRAY == encodedColorType) {
410 // If encodedColorType is GRAY, there must be a transparent chunk.
411 // Otherwise, suggestedColorType would be kGray. We have already
412 // instructed libpng to convert the transparent chunk to alpha,
413 // so we can treat both GRAY and GRAY_ALPHA as kGrayAlpha.
414 SkASSERT(encodedColorType == PNG_COLOR_TYPE_GRAY_ALPHA ||
415 png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS));
416
417 fSrcConfig = SkSwizzler::kGrayAlpha;
418 } else {
419 if (this->getInfo().alphaType() == kOpaque_SkAlphaType) {
msarettbda86092016-01-19 10:40:12 -0800420 fSrcConfig = SkSwizzler::kRGB;
scroggo6f29a3c2015-07-07 06:09:08 -0700421 } else {
422 fSrcConfig = SkSwizzler::kRGBA;
msarett93e613d2016-02-03 10:44:46 -0800423 }
scroggo6f29a3c2015-07-07 06:09:08 -0700424 }
425 break;
msarett93e613d2016-02-03 10:44:46 -0800426 }
scroggo6f29a3c2015-07-07 06:09:08 -0700427 default:
msarett13a91232016-02-01 08:03:29 -0800428 // We will always recommend one of the above colorTypes.
scroggo6f29a3c2015-07-07 06:09:08 -0700429 SkASSERT(false);
mtklein372d65c2016-01-27 13:01:41 -0800430 }
msarett9e43cab2015-04-29 07:38:43 -0700431
432 // Copy the color table to the client if they request kIndex8 mode
433 copy_color_table(requestedInfo, fColorTable, ctable, ctableCount);
434
435 // Create the swizzler. SkPngCodec retains ownership of the color table.
msarett99f567e2015-08-05 12:58:26 -0700436 const SkPMColor* colors = get_color_ptr(fColorTable.get());
msarettfdb47572015-10-13 12:50:14 -0700437 fSwizzler.reset(SkSwizzler::CreateSwizzler(fSrcConfig, colors, requestedInfo, options));
msarett13a91232016-02-01 08:03:29 -0800438 SkASSERT(fSwizzler);
439
scroggo05245902015-03-25 11:11:52 -0700440 return kSuccess;
441}
442
scroggo6f29a3c2015-07-07 06:09:08 -0700443
scroggob427db12015-08-12 07:24:13 -0700444bool SkPngCodec::onRewind() {
halcanary96fcdcc2015-08-27 07:41:13 -0700445 // This sets fPng_ptr and fInfo_ptr to nullptr. If read_header
scroggob427db12015-08-12 07:24:13 -0700446 // succeeds, they will be repopulated, and if it fails, they will
halcanary96fcdcc2015-08-27 07:41:13 -0700447 // remain nullptr. Any future accesses to fPng_ptr and fInfo_ptr will
scroggob427db12015-08-12 07:24:13 -0700448 // come through this function which will rewind and again attempt
449 // to reinitialize them.
450 this->destroyReadStruct();
451
452 png_structp png_ptr;
453 png_infop info_ptr;
scroggocf98fa92015-11-23 08:14:40 -0800454 if (!read_header(this->stream(), fPngChunkReader.get(), &png_ptr, &info_ptr,
455 nullptr, nullptr, nullptr)) {
scroggob427db12015-08-12 07:24:13 -0700456 return false;
scroggo58421542015-04-01 11:25:20 -0700457 }
scroggob427db12015-08-12 07:24:13 -0700458
459 fPng_ptr = png_ptr;
460 fInfo_ptr = info_ptr;
461 return true;
scroggo58421542015-04-01 11:25:20 -0700462}
463
scroggo05245902015-03-25 11:11:52 -0700464SkCodec::Result SkPngCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst,
msarett614aa072015-07-27 15:13:17 -0700465 size_t dstRowBytes, const Options& options,
msarette6dd0042015-10-09 11:07:34 -0700466 SkPMColor ctable[], int* ctableCount,
467 int* rowsDecoded) {
scroggo6f29a3c2015-07-07 06:09:08 -0700468 if (!conversion_possible(requestedInfo, this->getInfo())) {
469 return kInvalidConversion;
470 }
scroggob636b452015-07-22 07:16:20 -0700471 if (options.fSubset) {
472 // Subsets are not supported.
473 return kUnimplemented;
474 }
scroggo05245902015-03-25 11:11:52 -0700475
msarett9e43cab2015-04-29 07:38:43 -0700476 // Note that ctable and ctableCount may be modified if there is a color table
msarettfdb47572015-10-13 12:50:14 -0700477 const Result result = this->initializeSwizzler(requestedInfo, options, ctable, ctableCount);
scroggo05245902015-03-25 11:11:52 -0700478 if (result != kSuccess) {
479 return result;
480 }
msarett60dcd3c2016-02-05 15:13:12 -0800481
482 const int width = requestedInfo.width();
483 const int height = requestedInfo.height();
484 const int bpp = SkSwizzler::BytesPerPixel(fSrcConfig);
485 const size_t srcRowBytes = width * bpp;
486
scroggo05245902015-03-25 11:11:52 -0700487 // FIXME: Could we use the return value of setjmp to specify the type of
488 // error?
msarette6dd0042015-10-09 11:07:34 -0700489 int row = 0;
490 // This must be declared above the call to setjmp to avoid memory leaks on incomplete images.
scroggo565901d2015-12-10 10:44:13 -0800491 SkAutoTMalloc<uint8_t> storage;
scroggo05245902015-03-25 11:11:52 -0700492 if (setjmp(png_jmpbuf(fPng_ptr))) {
msarette6dd0042015-10-09 11:07:34 -0700493 // Assume that any error that occurs while reading rows is caused by an incomplete input.
494 if (fNumberPasses > 1) {
495 // FIXME (msarett): Handle incomplete interlaced pngs.
msarett60dcd3c2016-02-05 15:13:12 -0800496 return (row == height) ? kSuccess : kInvalidInput;
msarette6dd0042015-10-09 11:07:34 -0700497 }
498 // FIXME: We do a poor job on incomplete pngs compared to other decoders (ex: Chromium,
499 // Ubuntu Image Viewer). This is because we use the default buffer size in libpng (8192
500 // bytes), and if we can't fill the buffer, we immediately fail.
501 // For example, if we try to read 8192 bytes, and the image (incorrectly) only contains
502 // half that, which may have been enough to contain a non-zero number of lines, we fail
503 // when we could have decoded a few more lines and then failed.
504 // The read function that we provide for libpng has no way of indicating that we have
505 // made a partial read.
506 // Making our buffer size smaller improves our incomplete decodes, but what impact does
507 // it have on regular decode performance? Should we investigate using a different API
msarett60dcd3c2016-02-05 15:13:12 -0800508 // instead of png_read_row? Chromium uses png_process_data.
msarette6dd0042015-10-09 11:07:34 -0700509 *rowsDecoded = row;
msarett60dcd3c2016-02-05 15:13:12 -0800510 return (row == height) ? kSuccess : kIncompleteInput;
scroggo05245902015-03-25 11:11:52 -0700511 }
512
scroggo46c57472015-09-30 08:57:13 -0700513 // FIXME: We could split these out based on subclass.
msarett614aa072015-07-27 15:13:17 -0700514 void* dstRow = dst;
scroggo05245902015-03-25 11:11:52 -0700515 if (fNumberPasses > 1) {
msarett60dcd3c2016-02-05 15:13:12 -0800516 storage.reset(height * srcRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800517 uint8_t* const base = storage.get();
scroggof24f2242015-03-03 08:59:20 -0800518
scroggo05245902015-03-25 11:11:52 -0700519 for (int i = 0; i < fNumberPasses; i++) {
msarett614aa072015-07-27 15:13:17 -0700520 uint8_t* srcRow = base;
scroggof24f2242015-03-03 08:59:20 -0800521 for (int y = 0; y < height; y++) {
msarett60dcd3c2016-02-05 15:13:12 -0800522 png_read_row(fPng_ptr, srcRow, nullptr);
msarett614aa072015-07-27 15:13:17 -0700523 srcRow += srcRowBytes;
scroggof24f2242015-03-03 08:59:20 -0800524 }
525 }
526
527 // Now swizzle it.
msarett614aa072015-07-27 15:13:17 -0700528 uint8_t* srcRow = base;
msarett60dcd3c2016-02-05 15:13:12 -0800529 for (; row < height; row++) {
msaretta4970dc2016-01-11 07:23:23 -0800530 fSwizzler->swizzle(dstRow, srcRow);
msarett614aa072015-07-27 15:13:17 -0700531 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
532 srcRow += srcRowBytes;
scroggof24f2242015-03-03 08:59:20 -0800533 }
534 } else {
msarett60dcd3c2016-02-05 15:13:12 -0800535 storage.reset(srcRowBytes);
scroggo565901d2015-12-10 10:44:13 -0800536 uint8_t* srcRow = storage.get();
msarett60dcd3c2016-02-05 15:13:12 -0800537 for (; row < height; row++) {
538 png_read_row(fPng_ptr, srcRow, nullptr);
msaretta4970dc2016-01-11 07:23:23 -0800539 fSwizzler->swizzle(dstRow, srcRow);
msarett614aa072015-07-27 15:13:17 -0700540 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
scroggof24f2242015-03-03 08:59:20 -0800541 }
542 }
543
emmaleer973ae862015-07-20 13:38:44 -0700544 // read rest of file, and get additional comment and time chunks in info_ptr
scroggo05245902015-03-25 11:11:52 -0700545 png_read_end(fPng_ptr, fInfo_ptr);
scroggo46c57472015-09-30 08:57:13 -0700546
emmaleer973ae862015-07-20 13:38:44 -0700547 return kSuccess;
scroggo05245902015-03-25 11:11:52 -0700548}
549
scroggoc5560be2016-02-03 09:42:42 -0800550uint32_t SkPngCodec::onGetFillValue(SkColorType colorType) const {
msarette6dd0042015-10-09 11:07:34 -0700551 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
552 if (colorPtr) {
553 return get_color_table_fill_value(colorType, colorPtr, 0);
554 }
scroggoc5560be2016-02-03 09:42:42 -0800555 return INHERITED::onGetFillValue(colorType);
msarette6dd0042015-10-09 11:07:34 -0700556}
557
scroggo46c57472015-09-30 08:57:13 -0700558// Subclass of SkPngCodec which supports scanline decoding
559class SkPngScanlineDecoder : public SkPngCodec {
scroggo05245902015-03-25 11:11:52 -0700560public:
scroggo46c57472015-09-30 08:57:13 -0700561 SkPngScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
scroggocf98fa92015-11-23 08:14:40 -0800562 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_ptr, int bitDepth)
563 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, 1)
msarettf724b992015-10-15 06:41:06 -0700564 , fSrcRow(nullptr)
scroggo1c005e42015-08-04 09:24:45 -0700565 {}
566
scroggo46c57472015-09-30 08:57:13 -0700567 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
568 SkPMColor ctable[], int* ctableCount) override {
scroggo1c005e42015-08-04 09:24:45 -0700569 if (!conversion_possible(dstInfo, this->getInfo())) {
scroggo46c57472015-09-30 08:57:13 -0700570 return kInvalidConversion;
scroggo1c005e42015-08-04 09:24:45 -0700571 }
572
scroggo46c57472015-09-30 08:57:13 -0700573 const Result result = this->initializeSwizzler(dstInfo, options, ctable,
574 ctableCount);
575 if (result != kSuccess) {
scroggo1c005e42015-08-04 09:24:45 -0700576 return result;
577 }
578
scroggo46c57472015-09-30 08:57:13 -0700579 fStorage.reset(this->getInfo().width() * SkSwizzler::BytesPerPixel(this->srcConfig()));
scroggo565901d2015-12-10 10:44:13 -0800580 fSrcRow = fStorage.get();
scroggo1c005e42015-08-04 09:24:45 -0700581
scroggo46c57472015-09-30 08:57:13 -0700582 return kSuccess;
scroggo05245902015-03-25 11:11:52 -0700583 }
584
msarette6dd0042015-10-09 11:07:34 -0700585 int onGetScanlines(void* dst, int count, size_t rowBytes) override {
586 // Assume that an error in libpng indicates an incomplete input.
587 int row = 0;
scroggo46c57472015-09-30 08:57:13 -0700588 if (setjmp(png_jmpbuf(this->png_ptr()))) {
scroggo230d4ac2015-03-26 07:15:55 -0700589 SkCodecPrintf("setjmp long jump!\n");
msarette6dd0042015-10-09 11:07:34 -0700590 return row;
scroggo05245902015-03-25 11:11:52 -0700591 }
592
msarett614aa072015-07-27 15:13:17 -0700593 void* dstRow = dst;
msarette6dd0042015-10-09 11:07:34 -0700594 for (; row < count; row++) {
msarett60dcd3c2016-02-05 15:13:12 -0800595 png_read_row(this->png_ptr(), fSrcRow, nullptr);
msaretta4970dc2016-01-11 07:23:23 -0800596 this->swizzler()->swizzle(dstRow, fSrcRow);
msarett614aa072015-07-27 15:13:17 -0700597 dstRow = SkTAddOffset<void>(dstRow, rowBytes);
scroggo05245902015-03-25 11:11:52 -0700598 }
scroggo46c57472015-09-30 08:57:13 -0700599
msarette6dd0042015-10-09 11:07:34 -0700600 return row;
scroggo05245902015-03-25 11:11:52 -0700601 }
602
msarette6dd0042015-10-09 11:07:34 -0700603 bool onSkipScanlines(int count) override {
604 // Assume that an error in libpng indicates an incomplete input.
scroggo46c57472015-09-30 08:57:13 -0700605 if (setjmp(png_jmpbuf(this->png_ptr()))) {
scroggo230d4ac2015-03-26 07:15:55 -0700606 SkCodecPrintf("setjmp long jump!\n");
msarette6dd0042015-10-09 11:07:34 -0700607 return false;
scroggo05245902015-03-25 11:11:52 -0700608 }
msarett60dcd3c2016-02-05 15:13:12 -0800609
msarette6dd0042015-10-09 11:07:34 -0700610 for (int row = 0; row < count; row++) {
msarett60dcd3c2016-02-05 15:13:12 -0800611 png_read_row(this->png_ptr(), fSrcRow, nullptr);
emmaleer7dc91902015-05-27 08:49:04 -0700612 }
msarette6dd0042015-10-09 11:07:34 -0700613 return true;
scroggo05245902015-03-25 11:11:52 -0700614 }
615
scroggo05245902015-03-25 11:11:52 -0700616private:
scroggo565901d2015-12-10 10:44:13 -0800617 SkAutoTMalloc<uint8_t> fStorage;
scroggo9b2cdbf42015-07-10 12:07:02 -0700618 uint8_t* fSrcRow;
scroggo05245902015-03-25 11:11:52 -0700619
scroggo46c57472015-09-30 08:57:13 -0700620 typedef SkPngCodec INHERITED;
scroggo05245902015-03-25 11:11:52 -0700621};
622
emmaleer0a4c3cb2015-06-22 10:40:21 -0700623
scroggo46c57472015-09-30 08:57:13 -0700624class SkPngInterlacedScanlineDecoder : public SkPngCodec {
emmaleer0a4c3cb2015-06-22 10:40:21 -0700625public:
scroggo46c57472015-09-30 08:57:13 -0700626 SkPngInterlacedScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream,
scroggocf98fa92015-11-23 08:14:40 -0800627 SkPngChunkReader* chunkReader, png_structp png_ptr, png_infop info_ptr,
628 int bitDepth, int numberPasses)
629 : INHERITED(srcInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, numberPasses)
scroggo46c57472015-09-30 08:57:13 -0700630 , fHeight(-1)
scroggo1c005e42015-08-04 09:24:45 -0700631 , fCanSkipRewind(false)
scroggo1c005e42015-08-04 09:24:45 -0700632 {
scroggo46c57472015-09-30 08:57:13 -0700633 SkASSERT(numberPasses != 1);
634 }
635
636 Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options,
msarettfdb47572015-10-13 12:50:14 -0700637 SkPMColor ctable[], int* ctableCount) override {
scroggo1c005e42015-08-04 09:24:45 -0700638 if (!conversion_possible(dstInfo, this->getInfo())) {
mtklein372d65c2016-01-27 13:01:41 -0800639 return kInvalidConversion;
scroggo1c005e42015-08-04 09:24:45 -0700640 }
641
msarettfdb47572015-10-13 12:50:14 -0700642 const Result result = this->initializeSwizzler(dstInfo, options, ctable,
643 ctableCount);
644 if (result != kSuccess) {
scroggo1c005e42015-08-04 09:24:45 -0700645 return result;
646 }
647
scroggo1c005e42015-08-04 09:24:45 -0700648 fHeight = dstInfo.height();
scroggo46c57472015-09-30 08:57:13 -0700649 // FIXME: This need not be called on a second call to onStartScanlineDecode.
650 fSrcRowBytes = this->getInfo().width() * SkSwizzler::BytesPerPixel(this->srcConfig());
scroggo1c005e42015-08-04 09:24:45 -0700651 fGarbageRow.reset(fSrcRowBytes);
652 fGarbageRowPtr = static_cast<uint8_t*>(fGarbageRow.get());
653 fCanSkipRewind = true;
654
655 return SkCodec::kSuccess;
656 }
657
msarette6dd0042015-10-09 11:07:34 -0700658 int onGetScanlines(void* dst, int count, size_t dstRowBytes) override {
scroggo1c005e42015-08-04 09:24:45 -0700659 // rewind stream if have previously called onGetScanlines,
660 // since we need entire progressive image to get scanlines
661 if (fCanSkipRewind) {
scroggo46c57472015-09-30 08:57:13 -0700662 // We already rewound in onStartScanlineDecode, so there is no reason to rewind.
scroggo1c005e42015-08-04 09:24:45 -0700663 // Next time onGetScanlines is called, we will need to rewind.
664 fCanSkipRewind = false;
scroggo46c57472015-09-30 08:57:13 -0700665 } else {
666 // rewindIfNeeded resets fCurrScanline, since it assumes that start
667 // needs to be called again before scanline decoding. PNG scanline
668 // decoding is the exception, since it needs to rewind between
669 // calls to getScanlines. Keep track of fCurrScanline, to undo the
670 // reset.
msarette6dd0042015-10-09 11:07:34 -0700671 const int currScanline = this->nextScanline();
scroggo46c57472015-09-30 08:57:13 -0700672 // This method would never be called if currScanline is -1
673 SkASSERT(currScanline != -1);
674
675 if (!this->rewindIfNeeded()) {
676 return kCouldNotRewind;
677 }
msarettcb0d5c92015-12-03 12:23:43 -0800678 this->updateCurrScanline(currScanline);
scroggo1c005e42015-08-04 09:24:45 -0700679 }
680
scroggo46c57472015-09-30 08:57:13 -0700681 if (setjmp(png_jmpbuf(this->png_ptr()))) {
emmaleer0a4c3cb2015-06-22 10:40:21 -0700682 SkCodecPrintf("setjmp long jump!\n");
msarette6dd0042015-10-09 11:07:34 -0700683 // FIXME (msarett): Returning 0 is pessimistic. If we can complete a single pass,
684 // we may be able to report that all of the memory has been initialized. Even if we
685 // fail on the first pass, we can still report than some scanlines are initialized.
686 return 0;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700687 }
scroggo565901d2015-12-10 10:44:13 -0800688 SkAutoTMalloc<uint8_t> storage(count * fSrcRowBytes);
689 uint8_t* storagePtr = storage.get();
emmaleer0a4c3cb2015-06-22 10:40:21 -0700690 uint8_t* srcRow;
msarette6dd0042015-10-09 11:07:34 -0700691 const int startRow = this->nextScanline();
scroggo46c57472015-09-30 08:57:13 -0700692 for (int i = 0; i < this->numberPasses(); i++) {
693 // read rows we planned to skip into garbage row
694 for (int y = 0; y < startRow; y++){
msarett60dcd3c2016-02-05 15:13:12 -0800695 png_read_row(this->png_ptr(), fGarbageRowPtr, nullptr);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700696 }
scroggo46c57472015-09-30 08:57:13 -0700697 // read rows we care about into buffer
emmaleer0a4c3cb2015-06-22 10:40:21 -0700698 srcRow = storagePtr;
699 for (int y = 0; y < count; y++) {
msarett60dcd3c2016-02-05 15:13:12 -0800700 png_read_row(this->png_ptr(), srcRow, nullptr);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700701 srcRow += fSrcRowBytes;
702 }
scroggo46c57472015-09-30 08:57:13 -0700703 // read rows we don't want into garbage buffer
704 for (int y = 0; y < fHeight - startRow - count; y++) {
msarett60dcd3c2016-02-05 15:13:12 -0800705 png_read_row(this->png_ptr(), fGarbageRowPtr, nullptr);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700706 }
707 }
708 //swizzle the rows we care about
709 srcRow = storagePtr;
msarett614aa072015-07-27 15:13:17 -0700710 void* dstRow = dst;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700711 for (int y = 0; y < count; y++) {
msaretta4970dc2016-01-11 07:23:23 -0800712 this->swizzler()->swizzle(dstRow, srcRow);
msarett614aa072015-07-27 15:13:17 -0700713 dstRow = SkTAddOffset<void>(dstRow, dstRowBytes);
emmaleer0a4c3cb2015-06-22 10:40:21 -0700714 srcRow += fSrcRowBytes;
715 }
scroggo46c57472015-09-30 08:57:13 -0700716
msarette6dd0042015-10-09 11:07:34 -0700717 return count;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700718 }
719
msarette6dd0042015-10-09 11:07:34 -0700720 bool onSkipScanlines(int count) override {
scroggo46c57472015-09-30 08:57:13 -0700721 // The non-virtual version will update fCurrScanline.
msarette6dd0042015-10-09 11:07:34 -0700722 return true;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700723 }
724
msarett5406d6f2015-08-31 06:55:13 -0700725 SkScanlineOrder onGetScanlineOrder() const override {
726 return kNone_SkScanlineOrder;
emmaleer8f4ba762015-08-14 07:44:46 -0700727 }
728
emmaleer0a4c3cb2015-06-22 10:40:21 -0700729private:
scroggo9b2cdbf42015-07-10 12:07:02 -0700730 int fHeight;
731 size_t fSrcRowBytes;
732 SkAutoMalloc fGarbageRow;
733 uint8_t* fGarbageRowPtr;
scroggo1c005e42015-08-04 09:24:45 -0700734 // FIXME: This imitates behavior in SkCodec::rewindIfNeeded. That function
735 // is called whenever some action is taken that reads the stream and
736 // therefore the next call will require a rewind. So it modifies a boolean
737 // to note that the *next* time it is called a rewind is needed.
scroggo46c57472015-09-30 08:57:13 -0700738 // SkPngInterlacedScanlineDecoder has an extra wrinkle - calling
739 // onStartScanlineDecode followed by onGetScanlines does *not* require a
740 // rewind. Since rewindIfNeeded does not have this flexibility, we need to
741 // add another layer.
scroggo1c005e42015-08-04 09:24:45 -0700742 bool fCanSkipRewind;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700743
scroggo46c57472015-09-30 08:57:13 -0700744 typedef SkPngCodec INHERITED;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700745};
746
scroggocf98fa92015-11-23 08:14:40 -0800747SkCodec* SkPngCodec::NewFromStream(SkStream* stream, SkPngChunkReader* chunkReader) {
scroggo46c57472015-09-30 08:57:13 -0700748 SkAutoTDelete<SkStream> streamDeleter(stream);
749 png_structp png_ptr;
750 png_infop info_ptr;
751 SkImageInfo imageInfo;
752 int bitDepth;
753 int numberPasses;
754
scroggocf98fa92015-11-23 08:14:40 -0800755 if (!read_header(stream, chunkReader, &png_ptr, &info_ptr, &imageInfo, &bitDepth,
756 &numberPasses)) {
halcanary96fcdcc2015-08-27 07:41:13 -0700757 return nullptr;
scroggo05245902015-03-25 11:11:52 -0700758 }
759
scroggo46c57472015-09-30 08:57:13 -0700760 if (1 == numberPasses) {
scroggocf98fa92015-11-23 08:14:40 -0800761 return new SkPngScanlineDecoder(imageInfo, streamDeleter.detach(), chunkReader,
762 png_ptr, info_ptr, bitDepth);
scroggo05245902015-03-25 11:11:52 -0700763 }
764
scroggocf98fa92015-11-23 08:14:40 -0800765 return new SkPngInterlacedScanlineDecoder(imageInfo, streamDeleter.detach(), chunkReader,
766 png_ptr, info_ptr, bitDepth, numberPasses);
scroggo05245902015-03-25 11:11:52 -0700767}