blob: 31a407c6701a71c2bf8b94258a3b6714acf4ca92 [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
msarettad8bcfe2016-03-07 07:09:03 -08008#include "SkBitmap.h"
msarett74114382015-03-16 11:55:18 -07009#include "SkCodecPriv.h"
Cary Clarka4083c92017-09-15 11:59:23 -040010#include "SkColorData.h"
Matt Sarettc434f512016-10-13 18:24:20 -040011#include "SkColorSpace.h"
Matt Sarett5dfb4e42017-01-04 09:41:09 -050012#include "SkColorSpacePriv.h"
scroggof24f2242015-03-03 08:59:20 -080013#include "SkColorTable.h"
scroggof24f2242015-03-03 08:59:20 -080014#include "SkMath.h"
msarett13a91232016-02-01 08:03:29 -080015#include "SkOpts.h"
msarettbe1d5552016-01-21 09:05:23 -080016#include "SkPngCodec.h"
Mike Reedd6cb11e2017-11-30 15:33:04 -050017#include "SkPngPriv.h"
msarett55447952016-07-22 14:07:23 -070018#include "SkPoint3.h"
scroggof24f2242015-03-03 08:59:20 -080019#include "SkSize.h"
20#include "SkStream.h"
21#include "SkSwizzler.h"
scroggo565901d2015-12-10 10:44:13 -080022#include "SkTemplates.h"
bungeman5d2cd6e2016-02-23 07:34:25 -080023#include "SkUtils.h"
scroggof24f2242015-03-03 08:59:20 -080024
mtklein63213812016-08-24 09:55:56 -070025#include "png.h"
Leon Scroggins III83239652017-04-21 13:47:12 -040026#include <algorithm>
mtklein63213812016-08-24 09:55:56 -070027
mtkleindc90b532016-07-28 14:45:28 -070028// This warning triggers false postives way too often in here.
29#if defined(__GNUC__) && !defined(__clang__)
30 #pragma GCC diagnostic ignored "-Wclobbered"
31#endif
32
scroggo8e6c7ad2016-09-16 08:20:38 -070033// FIXME (scroggo): We can use png_jumpbuf directly once Google3 is on 1.6
34#define PNG_JMPBUF(x) png_jmpbuf((png_structp) x)
35
scroggof24f2242015-03-03 08:59:20 -080036///////////////////////////////////////////////////////////////////////////////
scroggof24f2242015-03-03 08:59:20 -080037// Callback functions
38///////////////////////////////////////////////////////////////////////////////
39
scroggo8e6c7ad2016-09-16 08:20:38 -070040// When setjmp is first called, it returns 0, meaning longjmp was not called.
41constexpr int kSetJmpOkay = 0;
42// An error internal to libpng.
43constexpr int kPngError = 1;
44// Passed to longjmp when we have decoded as many lines as we need.
45constexpr int kStopDecoding = 2;
46
scroggof24f2242015-03-03 08:59:20 -080047static void sk_error_fn(png_structp png_ptr, png_const_charp msg) {
scroggo230d4ac2015-03-26 07:15:55 -070048 SkCodecPrintf("------ png error %s\n", msg);
scroggo8e6c7ad2016-09-16 08:20:38 -070049 longjmp(PNG_JMPBUF(png_ptr), kPngError);
scroggof24f2242015-03-03 08:59:20 -080050}
51
scroggo0eed6df2015-03-26 10:07:56 -070052void sk_warning_fn(png_structp, png_const_charp msg) {
53 SkCodecPrintf("----- png warning %s\n", msg);
54}
55
scroggocf98fa92015-11-23 08:14:40 -080056#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
57static int sk_read_user_chunk(png_structp png_ptr, png_unknown_chunkp chunk) {
58 SkPngChunkReader* chunkReader = (SkPngChunkReader*)png_get_user_chunk_ptr(png_ptr);
59 // readChunk() returning true means continue decoding
60 return chunkReader->readChunk((const char*)chunk->name, chunk->data, chunk->size) ? 1 : -1;
61}
62#endif
63
scroggof24f2242015-03-03 08:59:20 -080064///////////////////////////////////////////////////////////////////////////////
65// Helpers
66///////////////////////////////////////////////////////////////////////////////
67
68class AutoCleanPng : public SkNoncopyable {
69public:
scroggo8e6c7ad2016-09-16 08:20:38 -070070 /*
71 * This class does not take ownership of stream or reader, but if codecPtr
72 * is non-NULL, and decodeBounds succeeds, it will have created a new
73 * SkCodec (pointed to by *codecPtr) which will own/ref them, as well as
74 * the png_ptr and info_ptr.
75 */
76 AutoCleanPng(png_structp png_ptr, SkStream* stream, SkPngChunkReader* reader,
77 SkCodec** codecPtr)
scroggof24f2242015-03-03 08:59:20 -080078 : fPng_ptr(png_ptr)
scroggo8e6c7ad2016-09-16 08:20:38 -070079 , fInfo_ptr(nullptr)
scroggo8e6c7ad2016-09-16 08:20:38 -070080 , fStream(stream)
81 , fChunkReader(reader)
82 , fOutCodec(codecPtr)
83 {}
scroggof24f2242015-03-03 08:59:20 -080084
85 ~AutoCleanPng() {
halcanary96fcdcc2015-08-27 07:41:13 -070086 // fInfo_ptr will never be non-nullptr unless fPng_ptr is.
scroggof24f2242015-03-03 08:59:20 -080087 if (fPng_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -070088 png_infopp info_pp = fInfo_ptr ? &fInfo_ptr : nullptr;
msarett13a91232016-02-01 08:03:29 -080089 png_destroy_read_struct(&fPng_ptr, info_pp, nullptr);
scroggof24f2242015-03-03 08:59:20 -080090 }
91 }
92
93 void setInfoPtr(png_infop info_ptr) {
halcanary96fcdcc2015-08-27 07:41:13 -070094 SkASSERT(nullptr == fInfo_ptr);
scroggof24f2242015-03-03 08:59:20 -080095 fInfo_ptr = info_ptr;
96 }
97
scroggo8e6c7ad2016-09-16 08:20:38 -070098 /**
99 * Reads enough of the input stream to decode the bounds.
100 * @return false if the stream is not a valid PNG (or too short).
101 * true if it read enough of the stream to determine the bounds.
102 * In the latter case, the stream may have been read beyond the
103 * point to determine the bounds, and the png_ptr will have saved
104 * any extra data. Further, if the codecPtr supplied to the
105 * constructor was not NULL, it will now point to a new SkCodec,
106 * which owns (or refs, in the case of the SkPngChunkReader) the
107 * inputs. If codecPtr was NULL, the png_ptr and info_ptr are
108 * unowned, and it is up to the caller to destroy them.
109 */
110 bool decodeBounds();
111
112private:
113 png_structp fPng_ptr;
114 png_infop fInfo_ptr;
scroggo8e6c7ad2016-09-16 08:20:38 -0700115 SkStream* fStream;
116 SkPngChunkReader* fChunkReader;
117 SkCodec** fOutCodec;
118
Leon Scroggins III83239652017-04-21 13:47:12 -0400119 void infoCallback(size_t idatLength);
scroggo8e6c7ad2016-09-16 08:20:38 -0700120
scroggo8e6c7ad2016-09-16 08:20:38 -0700121 void releasePngPtrs() {
halcanary96fcdcc2015-08-27 07:41:13 -0700122 fPng_ptr = nullptr;
123 fInfo_ptr = nullptr;
scroggof24f2242015-03-03 08:59:20 -0800124 }
scroggof24f2242015-03-03 08:59:20 -0800125};
126#define AutoCleanPng(...) SK_REQUIRE_LOCAL_VAR(AutoCleanPng)
127
Leon Scroggins III83239652017-04-21 13:47:12 -0400128static inline bool is_chunk(const png_byte* chunk, const char* tag) {
129 return memcmp(chunk + 4, tag, 4) == 0;
130}
131
132static inline bool process_data(png_structp png_ptr, png_infop info_ptr,
133 SkStream* stream, void* buffer, size_t bufferSize, size_t length) {
134 while (length > 0) {
135 const size_t bytesToProcess = std::min(bufferSize, length);
Leon Scroggins IIIb6446502017-04-24 09:32:50 -0400136 const size_t bytesRead = stream->read(buffer, bytesToProcess);
137 png_process_data(png_ptr, info_ptr, (png_bytep) buffer, bytesRead);
138 if (bytesRead < bytesToProcess) {
Leon Scroggins III83239652017-04-21 13:47:12 -0400139 return false;
140 }
Leon Scroggins III83239652017-04-21 13:47:12 -0400141 length -= bytesToProcess;
142 }
143 return true;
144}
145
scroggo8e6c7ad2016-09-16 08:20:38 -0700146bool AutoCleanPng::decodeBounds() {
147 if (setjmp(PNG_JMPBUF(fPng_ptr))) {
148 return false;
149 }
150
Leon Scroggins III83239652017-04-21 13:47:12 -0400151 png_set_progressive_read_fn(fPng_ptr, nullptr, nullptr, nullptr, nullptr);
scroggo8e6c7ad2016-09-16 08:20:38 -0700152
153 // Arbitrary buffer size, though note that it matches (below)
154 // SkPngCodec::processData(). FIXME: Can we better suit this to the size of
155 // the PNG header?
156 constexpr size_t kBufferSize = 4096;
157 char buffer[kBufferSize];
158
Leon Scroggins III83239652017-04-21 13:47:12 -0400159 {
160 // Parse the signature.
161 if (fStream->read(buffer, 8) < 8) {
162 return false;
163 }
164
165 png_process_data(fPng_ptr, fInfo_ptr, (png_bytep) buffer, 8);
166 }
167
scroggo8e6c7ad2016-09-16 08:20:38 -0700168 while (true) {
Leon Scroggins III83239652017-04-21 13:47:12 -0400169 // Parse chunk length and type.
170 if (fStream->read(buffer, 8) < 8) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700171 // We have read to the end of the input without decoding bounds.
172 break;
173 }
174
Leon Scroggins III83239652017-04-21 13:47:12 -0400175 png_byte* chunk = reinterpret_cast<png_byte*>(buffer);
176 const size_t length = png_get_uint_32(chunk);
177
178 if (is_chunk(chunk, "IDAT")) {
179 this->infoCallback(length);
180 return true;
181 }
182
183 png_process_data(fPng_ptr, fInfo_ptr, chunk, 8);
184 // Process the full chunk + CRC.
185 if (!process_data(fPng_ptr, fInfo_ptr, fStream, buffer, kBufferSize, length + 4)) {
186 return false;
scroggo8e6c7ad2016-09-16 08:20:38 -0700187 }
188 }
189
Leon Scroggins III83239652017-04-21 13:47:12 -0400190 return false;
scroggo8e6c7ad2016-09-16 08:20:38 -0700191}
192
nagarajan.ndd7ffa52017-09-29 08:23:32 +0530193bool SkPngCodec::processData() {
scroggo8e6c7ad2016-09-16 08:20:38 -0700194 switch (setjmp(PNG_JMPBUF(fPng_ptr))) {
195 case kPngError:
196 // There was an error. Stop processing data.
197 // FIXME: Do we need to discard png_ptr?
nagarajan.ndd7ffa52017-09-29 08:23:32 +0530198 return false;;
scroggo8e6c7ad2016-09-16 08:20:38 -0700199 case kStopDecoding:
200 // We decoded all the lines we want.
nagarajan.ndd7ffa52017-09-29 08:23:32 +0530201 return true;
scroggo8e6c7ad2016-09-16 08:20:38 -0700202 case kSetJmpOkay:
203 // Everything is okay.
204 break;
205 default:
206 // No other values should be passed to longjmp.
207 SkASSERT(false);
208 }
209
210 // Arbitrary buffer size
211 constexpr size_t kBufferSize = 4096;
212 char buffer[kBufferSize];
213
Leon Scroggins III83239652017-04-21 13:47:12 -0400214 bool iend = false;
scroggo8e6c7ad2016-09-16 08:20:38 -0700215 while (true) {
Leon Scroggins III83239652017-04-21 13:47:12 -0400216 size_t length;
217 if (fDecodedIdat) {
218 // Parse chunk length and type.
219 if (this->stream()->read(buffer, 8) < 8) {
220 break;
221 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700222
Leon Scroggins III83239652017-04-21 13:47:12 -0400223 png_byte* chunk = reinterpret_cast<png_byte*>(buffer);
224 png_process_data(fPng_ptr, fInfo_ptr, chunk, 8);
225 if (is_chunk(chunk, "IEND")) {
226 iend = true;
227 }
228
229 length = png_get_uint_32(chunk);
230 } else {
231 length = fIdatLength;
232 png_byte idat[] = {0, 0, 0, 0, 'I', 'D', 'A', 'T'};
233 png_save_uint_32(idat, length);
234 png_process_data(fPng_ptr, fInfo_ptr, idat, 8);
235 fDecodedIdat = true;
236 }
237
238 // Process the full chunk + CRC.
239 if (!process_data(fPng_ptr, fInfo_ptr, this->stream(), buffer, kBufferSize, length + 4)
240 || iend) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700241 break;
242 }
243 }
nagarajan.ndd7ffa52017-09-29 08:23:32 +0530244
245 return true;
scroggo8e6c7ad2016-09-16 08:20:38 -0700246}
247
Leon Scroggins III862c1962017-10-02 16:28:49 -0400248static constexpr SkColorType kXformSrcColorType = kRGBA_8888_SkColorType;
Matt Sarett562e6812016-11-08 16:13:43 -0500249
msarettd1ec89b2016-08-03 12:59:27 -0700250// Note: SkColorTable claims to store SkPMColors, which is not necessarily the case here.
Leon Scroggins571b30f2017-07-11 17:35:31 +0000251bool SkPngCodec::createColorTable(const SkImageInfo& dstInfo) {
scroggof24f2242015-03-03 08:59:20 -0800252
msarett13a91232016-02-01 08:03:29 -0800253 int numColors;
254 png_color* palette;
255 if (!png_get_PLTE(fPng_ptr, fInfo_ptr, &palette, &numColors)) {
scroggo05245902015-03-25 11:11:52 -0700256 return false;
scroggof24f2242015-03-03 08:59:20 -0800257 }
258
msarettdcd5e652016-08-22 08:48:40 -0700259 // Contents depend on tableColorType and our choice of if/when to premultiply:
260 // { kPremul, kUnpremul, kOpaque } x { RGBA, BGRA }
261 SkPMColor colorTable[256];
Matt Sarett562e6812016-11-08 16:13:43 -0500262 SkColorType tableColorType = this->colorXform() ? kXformSrcColorType : dstInfo.colorType();
scroggof24f2242015-03-03 08:59:20 -0800263
msarett13a91232016-02-01 08:03:29 -0800264 png_bytep alphas;
265 int numColorsWithAlpha = 0;
266 if (png_get_tRNS(fPng_ptr, fInfo_ptr, &alphas, &numColorsWithAlpha, nullptr)) {
msarettdcd5e652016-08-22 08:48:40 -0700267 // If we are performing a color xform, it will handle the premultiply. Otherwise,
268 // we'll do it here.
Leon Scroggins IIIae79f322017-08-18 10:53:24 -0400269 bool premultiply = !this->colorXform() && needs_premul(dstInfo.alphaType(),
270 this->getEncodedInfo().alpha());
msarettdcd5e652016-08-22 08:48:40 -0700271
msarett13a91232016-02-01 08:03:29 -0800272 // Choose which function to use to create the color table. If the final destination's
273 // colortype is unpremultiplied, the color table will store unpremultiplied colors.
msarettdcd5e652016-08-22 08:48:40 -0700274 PackColorProc proc = choose_pack_color_proc(premultiply, tableColorType);
msarett13a91232016-02-01 08:03:29 -0800275
276 for (int i = 0; i < numColorsWithAlpha; i++) {
277 // We don't have a function in SkOpts that combines a set of alphas with a set
278 // of RGBs. We could write one, but it's hardly worth it, given that this
279 // is such a small fraction of the total decode time.
msarettdcd5e652016-08-22 08:48:40 -0700280 colorTable[i] = proc(alphas[i], palette->red, palette->green, palette->blue);
msarett13a91232016-02-01 08:03:29 -0800281 palette++;
282 }
scroggof24f2242015-03-03 08:59:20 -0800283 }
284
msarett13a91232016-02-01 08:03:29 -0800285 if (numColorsWithAlpha < numColors) {
286 // The optimized code depends on a 3-byte png_color struct with the colors
287 // in RGB order. These checks make sure it is safe to use.
288 static_assert(3 == sizeof(png_color), "png_color struct has changed. Opts are broken.");
289#ifdef SK_DEBUG
290 SkASSERT(&palette->red < &palette->green);
291 SkASSERT(&palette->green < &palette->blue);
292#endif
293
msarettdcd5e652016-08-22 08:48:40 -0700294 if (is_rgba(tableColorType)) {
295 SkOpts::RGB_to_RGB1(colorTable + numColorsWithAlpha, palette,
msarett34e0ec42016-04-22 16:27:24 -0700296 numColors - numColorsWithAlpha);
297 } else {
msarettdcd5e652016-08-22 08:48:40 -0700298 SkOpts::RGB_to_BGR1(colorTable + numColorsWithAlpha, palette,
msarett34e0ec42016-04-22 16:27:24 -0700299 numColors - numColorsWithAlpha);
300 }
scroggof24f2242015-03-03 08:59:20 -0800301 }
302
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400303 if (this->colorXform() && !this->xformOnDecode()) {
304 this->applyColorXform(colorTable, colorTable, numColors);
msarettdcd5e652016-08-22 08:48:40 -0700305 }
306
msarett13a91232016-02-01 08:03:29 -0800307 // Pad the color table with the last color in the table (or black) in the case that
308 // invalid pixel indices exceed the number of colors in the table.
309 const int maxColors = 1 << fBitDepth;
310 if (numColors < maxColors) {
msarettdcd5e652016-08-22 08:48:40 -0700311 SkPMColor lastColor = numColors > 0 ? colorTable[numColors - 1] : SK_ColorBLACK;
312 sk_memset32(colorTable + numColors, lastColor, maxColors - numColors);
scroggof24f2242015-03-03 08:59:20 -0800313 }
314
msarettdcd5e652016-08-22 08:48:40 -0700315 fColorTable.reset(new SkColorTable(colorTable, maxColors));
scroggo05245902015-03-25 11:11:52 -0700316 return true;
scroggof24f2242015-03-03 08:59:20 -0800317}
318
319///////////////////////////////////////////////////////////////////////////////
320// Creation
321///////////////////////////////////////////////////////////////////////////////
322
scroggodb30be22015-12-08 18:54:13 -0800323bool SkPngCodec::IsPng(const char* buf, size_t bytesRead) {
324 return !png_sig_cmp((png_bytep) buf, (png_size_t)0, bytesRead);
scroggof24f2242015-03-03 08:59:20 -0800325}
326
scroggo8e6c7ad2016-09-16 08:20:38 -0700327#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6)
328
msarett6a738212016-03-04 13:27:35 -0800329static float png_fixed_point_to_float(png_fixed_point x) {
330 // We multiply by the same factor that libpng used to convert
331 // fixed point -> double. Since we want floats, we choose to
332 // do the conversion ourselves rather than convert
333 // fixed point -> double -> float.
334 return ((float) x) * 0.00001f;
335}
336
msarett128245c2016-03-30 12:01:47 -0700337static float png_inverted_fixed_point_to_float(png_fixed_point x) {
338 // This is necessary because the gAMA chunk actually stores 1/gamma.
339 return 1.0f / png_fixed_point_to_float(x);
340}
341
scroggo8e6c7ad2016-09-16 08:20:38 -0700342#endif // LIBPNG >= 1.6
343
msarett6a738212016-03-04 13:27:35 -0800344// Returns a colorSpace object that represents any color space information in
raftiasd737bee2016-12-08 10:53:24 -0500345// the encoded data. If the encoded data contains an invalid/unsupported color space,
346// this will return NULL. If there is no color space information, it will guess sRGB
Leon Scroggins IIIf78b55c2017-10-31 13:49:14 -0400347sk_sp<SkColorSpace> read_color_space(png_structp png_ptr, png_infop info_ptr) {
msarett6a738212016-03-04 13:27:35 -0800348
msarette2443222016-03-04 14:20:49 -0800349#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6)
350
msarett6a738212016-03-04 13:27:35 -0800351 // First check for an ICC profile
352 png_bytep profile;
353 png_uint_32 length;
354 // The below variables are unused, however, we need to pass them in anyway or
355 // png_get_iCCP() will return nothing.
356 // Could knowing the |name| of the profile ever be interesting? Maybe for debugging?
357 png_charp name;
358 // The |compression| is uninteresting since:
359 // (1) libpng has already decompressed the profile for us.
360 // (2) "deflate" is the only mode of decompression that libpng supports.
361 int compression;
362 if (PNG_INFO_iCCP == png_get_iCCP(png_ptr, info_ptr, &name, &compression, &profile,
363 &length)) {
Leon Scroggins IIIf78b55c2017-10-31 13:49:14 -0400364 return SkColorSpace::MakeICC(profile, length);
msarett6a738212016-03-04 13:27:35 -0800365 }
366
367 // Second, check for sRGB.
368 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
369
370 // sRGB chunks also store a rendering intent: Absolute, Relative,
371 // Perceptual, and Saturation.
372 // FIXME (msarett): Extract this information from the sRGB chunk once
373 // we are able to handle this information in
374 // SkColorSpace.
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500375 return SkColorSpace::MakeSRGB();
msarett6a738212016-03-04 13:27:35 -0800376 }
377
378 // Next, check for chromaticities.
msaretta5a31dd2016-10-11 09:41:16 -0700379 png_fixed_point chrm[8];
msarett6a738212016-03-04 13:27:35 -0800380 png_fixed_point gamma;
msaretta5a31dd2016-10-11 09:41:16 -0700381 if (png_get_cHRM_fixed(png_ptr, info_ptr, &chrm[0], &chrm[1], &chrm[2], &chrm[3], &chrm[4],
382 &chrm[5], &chrm[6], &chrm[7]))
msarett55447952016-07-22 14:07:23 -0700383 {
msaretta5a31dd2016-10-11 09:41:16 -0700384 SkColorSpacePrimaries primaries;
385 primaries.fRX = png_fixed_point_to_float(chrm[2]);
386 primaries.fRY = png_fixed_point_to_float(chrm[3]);
387 primaries.fGX = png_fixed_point_to_float(chrm[4]);
388 primaries.fGY = png_fixed_point_to_float(chrm[5]);
389 primaries.fBX = png_fixed_point_to_float(chrm[6]);
390 primaries.fBY = png_fixed_point_to_float(chrm[7]);
391 primaries.fWX = png_fixed_point_to_float(chrm[0]);
392 primaries.fWY = png_fixed_point_to_float(chrm[1]);
msarett55447952016-07-22 14:07:23 -0700393
394 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
msaretta5a31dd2016-10-11 09:41:16 -0700395 if (!primaries.toXYZD50(&toXYZD50)) {
msarett55447952016-07-22 14:07:23 -0700396 toXYZD50.set3x3RowMajorf(gSRGB_toXYZD50);
397 }
msarett6a738212016-03-04 13:27:35 -0800398
msarett128245c2016-03-30 12:01:47 -0700399 if (PNG_INFO_gAMA == png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) {
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400400 SkColorSpaceTransferFn fn;
401 fn.fA = 1.0f;
402 fn.fB = fn.fC = fn.fD = fn.fE = fn.fF = 0.0f;
403 fn.fG = png_inverted_fixed_point_to_float(gamma);
msarettffc2aea2016-05-02 11:12:14 -0700404
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400405 return SkColorSpace::MakeRGB(fn, toXYZD50);
msarett6a738212016-03-04 13:27:35 -0800406 }
msarett128245c2016-03-30 12:01:47 -0700407
msarettc4ce6b52016-06-16 07:37:41 -0700408 // Default to sRGB gamma if the image has color space information,
409 // but does not specify gamma.
Brian Osman526972e2016-10-24 09:24:02 -0400410 return SkColorSpace::MakeRGB(SkColorSpace::kSRGB_RenderTargetGamma, toXYZD50);
msarett6a738212016-03-04 13:27:35 -0800411 }
412
413 // Last, check for gamma.
414 if (PNG_INFO_gAMA == png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) {
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400415 SkColorSpaceTransferFn fn;
416 fn.fA = 1.0f;
417 fn.fB = fn.fC = fn.fD = fn.fE = fn.fF = 0.0f;
418 fn.fG = png_inverted_fixed_point_to_float(gamma);
msarett6a738212016-03-04 13:27:35 -0800419
msarettc4ce6b52016-06-16 07:37:41 -0700420 // Since there is no cHRM, we will guess sRGB gamut.
msarett55447952016-07-22 14:07:23 -0700421 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
422 toXYZD50.set3x3RowMajorf(gSRGB_toXYZD50);
msarettc4ce6b52016-06-16 07:37:41 -0700423
Matt Sarett99e3f7d2016-10-28 12:51:08 -0400424 return SkColorSpace::MakeRGB(fn, toXYZD50);
msarett6a738212016-03-04 13:27:35 -0800425 }
426
msarette2443222016-03-04 14:20:49 -0800427#endif // LIBPNG >= 1.6
428
raftiasd737bee2016-12-08 10:53:24 -0500429 // Report that there is no color space information in the PNG.
430 // Guess sRGB in this case.
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500431 return SkColorSpace::MakeSRGB();
msarett6a738212016-03-04 13:27:35 -0800432}
433
msarett400a93b2016-09-01 18:32:52 -0700434void SkPngCodec::allocateStorage(const SkImageInfo& dstInfo) {
435 switch (fXformMode) {
436 case kSwizzleOnly_XformMode:
msarett400a93b2016-09-01 18:32:52 -0700437 break;
438 case kColorOnly_XformMode:
439 // Intentional fall through. A swizzler hasn't been created yet, but one will
440 // be created later if we are sampling. We'll go ahead and allocate
441 // enough memory to swizzle if necessary.
442 case kSwizzleColor_XformMode: {
Matt Sarett34c69d62017-01-19 17:42:23 -0500443 const int bitsPerPixel = this->getEncodedInfo().bitsPerPixel();
444
445 // If we have more than 8-bits (per component) of precision, we will keep that
446 // extra precision. Otherwise, we will swizzle to RGBA_8888 before transforming.
447 const size_t bytesPerPixel = (bitsPerPixel > 32) ? bitsPerPixel / 8 : 4;
448 const size_t colorXformBytes = dstInfo.width() * bytesPerPixel;
scroggo8e6c7ad2016-09-16 08:20:38 -0700449 fStorage.reset(colorXformBytes);
Matt Sarett379938e2017-01-12 18:34:29 -0500450 fColorXformSrcRow = fStorage.get();
msarett400a93b2016-09-01 18:32:52 -0700451 break;
452 }
453 }
msarettd1ec89b2016-08-03 12:59:27 -0700454}
455
Matt Sarett379938e2017-01-12 18:34:29 -0500456static SkColorSpaceXform::ColorFormat png_select_xform_format(const SkEncodedInfo& info) {
Matt Sarett34c69d62017-01-19 17:42:23 -0500457 // We use kRGB and kRGBA formats because color PNGs are always RGB or RGBA.
458 if (16 == info.bitsPerComponent()) {
459 if (SkEncodedInfo::kRGBA_Color == info.color()) {
460 return SkColorSpaceXform::kRGBA_U16_BE_ColorFormat;
461 } else if (SkEncodedInfo::kRGB_Color == info.color()) {
462 return SkColorSpaceXform::kRGB_U16_BE_ColorFormat;
463 }
Matt Sarett379938e2017-01-12 18:34:29 -0500464 }
465
466 return SkColorSpaceXform::kRGBA_8888_ColorFormat;
467}
468
scroggo8e6c7ad2016-09-16 08:20:38 -0700469void SkPngCodec::applyXformRow(void* dst, const void* src) {
msarett400a93b2016-09-01 18:32:52 -0700470 switch (fXformMode) {
471 case kSwizzleOnly_XformMode:
472 fSwizzler->swizzle(dst, (const uint8_t*) src);
473 break;
474 case kColorOnly_XformMode:
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400475 this->applyColorXform(dst, src, fXformWidth);
msarett400a93b2016-09-01 18:32:52 -0700476 break;
477 case kSwizzleColor_XformMode:
478 fSwizzler->swizzle(fColorXformSrcRow, (const uint8_t*) src);
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -0400479 this->applyColorXform(dst, fColorXformSrcRow, fXformWidth);
msarett400a93b2016-09-01 18:32:52 -0700480 break;
481 }
msarettdcd5e652016-08-22 08:48:40 -0700482}
483
scroggo8e6c7ad2016-09-16 08:20:38 -0700484class SkPngNormalDecoder : public SkPngCodec {
scroggo05245902015-03-25 11:11:52 -0700485public:
Mike Reedede7bac2017-07-23 15:30:02 -0400486 SkPngNormalDecoder(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
487 std::unique_ptr<SkStream> stream, SkPngChunkReader* reader,
488 png_structp png_ptr, png_infop info_ptr, int bitDepth)
489 : INHERITED(info, imageInfo, std::move(stream), reader, png_ptr, info_ptr, bitDepth)
scroggoff9f7bb2016-10-10 11:35:01 -0700490 , fRowsWrittenToOutput(0)
scroggo8e6c7ad2016-09-16 08:20:38 -0700491 , fDst(nullptr)
492 , fRowBytes(0)
493 , fFirstRow(0)
494 , fLastRow(0)
scroggo1c005e42015-08-04 09:24:45 -0700495 {}
496
scroggo8e6c7ad2016-09-16 08:20:38 -0700497 static void AllRowsCallback(png_structp png_ptr, png_bytep row, png_uint_32 rowNum, int /*pass*/) {
498 GetDecoder(png_ptr)->allRowsCallback(row, rowNum);
scroggo05245902015-03-25 11:11:52 -0700499 }
500
scroggo8e6c7ad2016-09-16 08:20:38 -0700501 static void RowCallback(png_structp png_ptr, png_bytep row, png_uint_32 rowNum, int /*pass*/) {
502 GetDecoder(png_ptr)->rowCallback(row, rowNum);
msarettd1ec89b2016-08-03 12:59:27 -0700503 }
504
emmaleer0a4c3cb2015-06-22 10:40:21 -0700505private:
scroggoff9f7bb2016-10-10 11:35:01 -0700506 int fRowsWrittenToOutput;
scroggo8e6c7ad2016-09-16 08:20:38 -0700507 void* fDst;
508 size_t fRowBytes;
509
510 // Variables for partial decode
511 int fFirstRow; // FIXME: Move to baseclass?
512 int fLastRow;
scroggo4f2a88c2016-10-17 14:32:41 -0700513 int fRowsNeeded;
emmaleer0a4c3cb2015-06-22 10:40:21 -0700514
scroggo46c57472015-09-30 08:57:13 -0700515 typedef SkPngCodec INHERITED;
scroggo8e6c7ad2016-09-16 08:20:38 -0700516
517 static SkPngNormalDecoder* GetDecoder(png_structp png_ptr) {
518 return static_cast<SkPngNormalDecoder*>(png_get_progressive_ptr(png_ptr));
519 }
520
521 Result decodeAllRows(void* dst, size_t rowBytes, int* rowsDecoded) override {
522 const int height = this->getInfo().height();
Leon Scroggins III83239652017-04-21 13:47:12 -0400523 png_set_progressive_read_fn(this->png_ptr(), this, nullptr, AllRowsCallback, nullptr);
scroggo8e6c7ad2016-09-16 08:20:38 -0700524 fDst = dst;
525 fRowBytes = rowBytes;
526
scroggoff9f7bb2016-10-10 11:35:01 -0700527 fRowsWrittenToOutput = 0;
scroggoc46cdd42016-10-10 06:45:32 -0700528 fFirstRow = 0;
529 fLastRow = height - 1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700530
nagarajan.ndd7ffa52017-09-29 08:23:32 +0530531 if (!this->processData()) {
532 return kErrorInInput;
533 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700534
scroggoff9f7bb2016-10-10 11:35:01 -0700535 if (fRowsWrittenToOutput == height) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700536 return SkCodec::kSuccess;
537 }
538
539 if (rowsDecoded) {
scroggoff9f7bb2016-10-10 11:35:01 -0700540 *rowsDecoded = fRowsWrittenToOutput;
scroggo8e6c7ad2016-09-16 08:20:38 -0700541 }
542
543 return SkCodec::kIncompleteInput;
544 }
545
546 void allRowsCallback(png_bytep row, int rowNum) {
scroggoff9f7bb2016-10-10 11:35:01 -0700547 SkASSERT(rowNum == fRowsWrittenToOutput);
548 fRowsWrittenToOutput++;
scroggo8e6c7ad2016-09-16 08:20:38 -0700549 this->applyXformRow(fDst, row);
550 fDst = SkTAddOffset<void>(fDst, fRowBytes);
551 }
552
553 void setRange(int firstRow, int lastRow, void* dst, size_t rowBytes) override {
Leon Scroggins III83239652017-04-21 13:47:12 -0400554 png_set_progressive_read_fn(this->png_ptr(), this, nullptr, RowCallback, nullptr);
scroggo8e6c7ad2016-09-16 08:20:38 -0700555 fFirstRow = firstRow;
556 fLastRow = lastRow;
557 fDst = dst;
558 fRowBytes = rowBytes;
scroggoff9f7bb2016-10-10 11:35:01 -0700559 fRowsWrittenToOutput = 0;
scroggo4f2a88c2016-10-17 14:32:41 -0700560 fRowsNeeded = fLastRow - fFirstRow + 1;
scroggo8e6c7ad2016-09-16 08:20:38 -0700561 }
562
563 SkCodec::Result decode(int* rowsDecoded) override {
scroggo4f2a88c2016-10-17 14:32:41 -0700564 if (this->swizzler()) {
565 const int sampleY = this->swizzler()->sampleY();
566 fRowsNeeded = get_scaled_dimension(fLastRow - fFirstRow + 1, sampleY);
567 }
nagarajan.ndd7ffa52017-09-29 08:23:32 +0530568
569 if (!this->processData()) {
570 return kErrorInInput;
571 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700572
scroggo4f2a88c2016-10-17 14:32:41 -0700573 if (fRowsWrittenToOutput == fRowsNeeded) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700574 return SkCodec::kSuccess;
575 }
576
577 if (rowsDecoded) {
scroggoff9f7bb2016-10-10 11:35:01 -0700578 *rowsDecoded = fRowsWrittenToOutput;
scroggo8e6c7ad2016-09-16 08:20:38 -0700579 }
580
581 return SkCodec::kIncompleteInput;
582 }
583
584 void rowCallback(png_bytep row, int rowNum) {
585 if (rowNum < fFirstRow) {
586 // Ignore this row.
587 return;
588 }
589
590 SkASSERT(rowNum <= fLastRow);
scroggo4f2a88c2016-10-17 14:32:41 -0700591 SkASSERT(fRowsWrittenToOutput < fRowsNeeded);
scroggo8e6c7ad2016-09-16 08:20:38 -0700592
593 // If there is no swizzler, all rows are needed.
scroggo4f2a88c2016-10-17 14:32:41 -0700594 if (!this->swizzler() || this->swizzler()->rowNeeded(rowNum - fFirstRow)) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700595 this->applyXformRow(fDst, row);
596 fDst = SkTAddOffset<void>(fDst, fRowBytes);
scroggoff9f7bb2016-10-10 11:35:01 -0700597 fRowsWrittenToOutput++;
scroggo8e6c7ad2016-09-16 08:20:38 -0700598 }
599
scroggo4f2a88c2016-10-17 14:32:41 -0700600 if (fRowsWrittenToOutput == fRowsNeeded) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700601 // Fake error to stop decoding scanlines.
602 longjmp(PNG_JMPBUF(this->png_ptr()), kStopDecoding);
603 }
604 }
emmaleer0a4c3cb2015-06-22 10:40:21 -0700605};
606
scroggo8e6c7ad2016-09-16 08:20:38 -0700607class SkPngInterlacedDecoder : public SkPngCodec {
608public:
609 SkPngInterlacedDecoder(const SkEncodedInfo& info, const SkImageInfo& imageInfo,
Mike Reedede7bac2017-07-23 15:30:02 -0400610 std::unique_ptr<SkStream> stream, SkPngChunkReader* reader, png_structp png_ptr,
611 png_infop info_ptr, int bitDepth, int numberPasses)
612 : INHERITED(info, imageInfo, std::move(stream), reader, png_ptr, info_ptr, bitDepth)
scroggo8e6c7ad2016-09-16 08:20:38 -0700613 , fNumberPasses(numberPasses)
614 , fFirstRow(0)
615 , fLastRow(0)
616 , fLinesDecoded(0)
617 , fInterlacedComplete(false)
618 , fPng_rowbytes(0)
619 {}
620
621 static void InterlacedRowCallback(png_structp png_ptr, png_bytep row, png_uint_32 rowNum, int pass) {
622 auto decoder = static_cast<SkPngInterlacedDecoder*>(png_get_progressive_ptr(png_ptr));
623 decoder->interlacedRowCallback(row, rowNum, pass);
624 }
625
scroggo8e6c7ad2016-09-16 08:20:38 -0700626private:
627 const int fNumberPasses;
628 int fFirstRow;
629 int fLastRow;
630 void* fDst;
631 size_t fRowBytes;
632 int fLinesDecoded;
633 bool fInterlacedComplete;
634 size_t fPng_rowbytes;
635 SkAutoTMalloc<png_byte> fInterlaceBuffer;
636
637 typedef SkPngCodec INHERITED;
638
scroggo8e6c7ad2016-09-16 08:20:38 -0700639 // FIXME: Currently sharing interlaced callback for all rows and subset. It's not
640 // as expensive as the subset version of non-interlaced, but it still does extra
641 // work.
642 void interlacedRowCallback(png_bytep row, int rowNum, int pass) {
Leon Scroggins III600effb2017-04-24 15:44:45 -0400643 if (rowNum < fFirstRow || rowNum > fLastRow || fInterlacedComplete) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700644 // Ignore this row
645 return;
646 }
647
648 png_bytep oldRow = fInterlaceBuffer.get() + (rowNum - fFirstRow) * fPng_rowbytes;
649 png_progressive_combine_row(this->png_ptr(), oldRow, row);
650
651 if (0 == pass) {
652 // The first pass initializes all rows.
653 SkASSERT(row);
654 SkASSERT(fLinesDecoded == rowNum - fFirstRow);
655 fLinesDecoded++;
656 } else {
657 SkASSERT(fLinesDecoded == fLastRow - fFirstRow + 1);
658 if (fNumberPasses - 1 == pass && rowNum == fLastRow) {
Leon Scroggins III600effb2017-04-24 15:44:45 -0400659 // Last pass, and we have read all of the rows we care about.
scroggo8e6c7ad2016-09-16 08:20:38 -0700660 fInterlacedComplete = true;
Leon Scroggins III600effb2017-04-24 15:44:45 -0400661 if (fLastRow != this->getInfo().height() - 1 ||
662 (this->swizzler() && this->swizzler()->sampleY() != 1)) {
663 // Fake error to stop decoding scanlines. Only stop if we're not decoding the
664 // whole image, in which case processing the rest of the image might be
665 // expensive. When decoding the whole image, read through the IEND chunk to
666 // preserve Android behavior of leaving the input stream in the right place.
667 longjmp(PNG_JMPBUF(this->png_ptr()), kStopDecoding);
668 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700669 }
670 }
671 }
672
673 SkCodec::Result decodeAllRows(void* dst, size_t rowBytes, int* rowsDecoded) override {
674 const int height = this->getInfo().height();
675 this->setUpInterlaceBuffer(height);
Leon Scroggins III83239652017-04-21 13:47:12 -0400676 png_set_progressive_read_fn(this->png_ptr(), this, nullptr, InterlacedRowCallback,
scroggo8e6c7ad2016-09-16 08:20:38 -0700677 nullptr);
678
679 fFirstRow = 0;
680 fLastRow = height - 1;
681 fLinesDecoded = 0;
682
nagarajan.ndd7ffa52017-09-29 08:23:32 +0530683 if (!this->processData()) {
684 return kErrorInInput;
685 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700686
687 png_bytep srcRow = fInterlaceBuffer.get();
688 // FIXME: When resuming, this may rewrite rows that did not change.
689 for (int rowNum = 0; rowNum < fLinesDecoded; rowNum++) {
690 this->applyXformRow(dst, srcRow);
691 dst = SkTAddOffset<void>(dst, rowBytes);
692 srcRow = SkTAddOffset<png_byte>(srcRow, fPng_rowbytes);
693 }
694 if (fInterlacedComplete) {
695 return SkCodec::kSuccess;
696 }
697
698 if (rowsDecoded) {
699 *rowsDecoded = fLinesDecoded;
700 }
701
702 return SkCodec::kIncompleteInput;
703 }
704
705 void setRange(int firstRow, int lastRow, void* dst, size_t rowBytes) override {
706 // FIXME: We could skip rows in the interlace buffer that we won't put in the output.
707 this->setUpInterlaceBuffer(lastRow - firstRow + 1);
Leon Scroggins III83239652017-04-21 13:47:12 -0400708 png_set_progressive_read_fn(this->png_ptr(), this, nullptr, InterlacedRowCallback, nullptr);
scroggo8e6c7ad2016-09-16 08:20:38 -0700709 fFirstRow = firstRow;
710 fLastRow = lastRow;
711 fDst = dst;
712 fRowBytes = rowBytes;
713 fLinesDecoded = 0;
714 }
715
716 SkCodec::Result decode(int* rowsDecoded) override {
nagarajan.ndd7ffa52017-09-29 08:23:32 +0530717 if (this->processData() == false) {
718 return kErrorInInput;
719 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700720
721 // Now apply Xforms on all the rows that were decoded.
722 if (!fLinesDecoded) {
scroggoe61b3b42016-10-10 07:17:32 -0700723 if (rowsDecoded) {
724 *rowsDecoded = 0;
725 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700726 return SkCodec::kIncompleteInput;
727 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700728
scroggo4f2a88c2016-10-17 14:32:41 -0700729 const int sampleY = this->swizzler() ? this->swizzler()->sampleY() : 1;
730 const int rowsNeeded = get_scaled_dimension(fLastRow - fFirstRow + 1, sampleY);
scroggoff9f7bb2016-10-10 11:35:01 -0700731 int rowsWrittenToOutput = 0;
732
scroggo8e6c7ad2016-09-16 08:20:38 -0700733 // FIXME: For resuming interlace, we may swizzle a row that hasn't changed. But it
734 // may be too tricky/expensive to handle that correctly.
scroggoe9ea3492016-10-18 09:14:11 -0700735
736 // Offset srcRow by get_start_coord rows. We do not need to account for fFirstRow,
737 // since the first row in fInterlaceBuffer corresponds to fFirstRow.
738 png_bytep srcRow = SkTAddOffset<png_byte>(fInterlaceBuffer.get(),
739 fPng_rowbytes * get_start_coord(sampleY));
scroggo8e6c7ad2016-09-16 08:20:38 -0700740 void* dst = fDst;
scroggoe9ea3492016-10-18 09:14:11 -0700741 for (; rowsWrittenToOutput < rowsNeeded; rowsWrittenToOutput++) {
scroggo8e6c7ad2016-09-16 08:20:38 -0700742 this->applyXformRow(dst, srcRow);
743 dst = SkTAddOffset<void>(dst, fRowBytes);
744 srcRow = SkTAddOffset<png_byte>(srcRow, fPng_rowbytes * sampleY);
745 }
746
747 if (fInterlacedComplete) {
748 return SkCodec::kSuccess;
749 }
750
751 if (rowsDecoded) {
scroggoff9f7bb2016-10-10 11:35:01 -0700752 *rowsDecoded = rowsWrittenToOutput;
scroggo8e6c7ad2016-09-16 08:20:38 -0700753 }
754 return SkCodec::kIncompleteInput;
755 }
756
757 void setUpInterlaceBuffer(int height) {
758 fPng_rowbytes = png_get_rowbytes(this->png_ptr(), this->info_ptr());
759 fInterlaceBuffer.reset(fPng_rowbytes * height);
760 fInterlacedComplete = false;
761 }
762};
763
msarettac6c7502016-04-25 09:30:24 -0700764// Reads the header and initializes the output fields, if not NULL.
765//
766// @param stream Input data. Will be read to get enough information to properly
767// setup the codec.
768// @param chunkReader SkPngChunkReader, for reading unknown chunks. May be NULL.
769// If not NULL, png_ptr will hold an *unowned* pointer to it. The caller is
770// expected to continue to own it for the lifetime of the png_ptr.
771// @param outCodec Optional output variable. If non-NULL, will be set to a new
772// SkPngCodec on success.
773// @param png_ptrp Optional output variable. If non-NULL, will be set to a new
774// png_structp on success.
775// @param info_ptrp Optional output variable. If non-NULL, will be set to a new
776// png_infop on success;
Leon Scroggins III588fb042017-07-14 16:32:31 -0400777// @return if kSuccess, the caller is responsible for calling
msarettac6c7502016-04-25 09:30:24 -0700778// png_destroy_read_struct(png_ptrp, info_ptrp).
Leon Scroggins III588fb042017-07-14 16:32:31 -0400779// Otherwise, the passed in fields (except stream) are unchanged.
780static SkCodec::Result read_header(SkStream* stream, SkPngChunkReader* chunkReader,
781 SkCodec** outCodec,
782 png_structp* png_ptrp, png_infop* info_ptrp) {
msarettac6c7502016-04-25 09:30:24 -0700783 // The image is known to be a PNG. Decode enough to know the SkImageInfo.
784 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
785 sk_error_fn, sk_warning_fn);
786 if (!png_ptr) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400787 return SkCodec::kInternalError;
msarettac6c7502016-04-25 09:30:24 -0700788 }
789
Leon Scroggins III9b1a8862018-03-01 15:57:32 -0500790#ifdef PNG_SET_OPTION_SUPPORTED
Leon Scroggins III9e8a5942018-02-28 16:24:18 -0500791 // This setting ensures that we display images with incorrect CMF bytes.
792 // See crbug.com/807324.
793 png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW, PNG_OPTION_ON);
Leon Scroggins III9b1a8862018-03-01 15:57:32 -0500794#endif
Leon Scroggins III9e8a5942018-02-28 16:24:18 -0500795
scroggo8e6c7ad2016-09-16 08:20:38 -0700796 AutoCleanPng autoClean(png_ptr, stream, chunkReader, outCodec);
msarettac6c7502016-04-25 09:30:24 -0700797
798 png_infop info_ptr = png_create_info_struct(png_ptr);
799 if (info_ptr == nullptr) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400800 return SkCodec::kInternalError;
msarettac6c7502016-04-25 09:30:24 -0700801 }
802
803 autoClean.setInfoPtr(info_ptr);
804
scroggo8e6c7ad2016-09-16 08:20:38 -0700805 if (setjmp(PNG_JMPBUF(png_ptr))) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400806 return SkCodec::kInvalidInput;
msarettac6c7502016-04-25 09:30:24 -0700807 }
808
msarettac6c7502016-04-25 09:30:24 -0700809#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
810 // Hookup our chunkReader so we can see any user-chunks the caller may be interested in.
811 // This needs to be installed before we read the png header. Android may store ninepatch
812 // chunks in the header.
813 if (chunkReader) {
814 png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"", 0);
815 png_set_read_user_chunk_fn(png_ptr, (png_voidp) chunkReader, sk_read_user_chunk);
816 }
817#endif
818
scroggo8e6c7ad2016-09-16 08:20:38 -0700819 const bool decodedBounds = autoClean.decodeBounds();
820
821 if (!decodedBounds) {
Leon Scroggins III588fb042017-07-14 16:32:31 -0400822 return SkCodec::kIncompleteInput;
scroggo8e6c7ad2016-09-16 08:20:38 -0700823 }
824
825 // On success, decodeBounds releases ownership of png_ptr and info_ptr.
826 if (png_ptrp) {
827 *png_ptrp = png_ptr;
828 }
829 if (info_ptrp) {
830 *info_ptrp = info_ptr;
831 }
832
833 // decodeBounds takes care of setting outCodec
834 if (outCodec) {
835 SkASSERT(*outCodec);
836 }
Leon Scroggins III588fb042017-07-14 16:32:31 -0400837 return SkCodec::kSuccess;
scroggo8e6c7ad2016-09-16 08:20:38 -0700838}
839
Leon Scroggins III83239652017-04-21 13:47:12 -0400840void AutoCleanPng::infoCallback(size_t idatLength) {
msarettac6c7502016-04-25 09:30:24 -0700841 png_uint_32 origWidth, origHeight;
842 int bitDepth, encodedColorType;
Leon Scroggins III83239652017-04-21 13:47:12 -0400843 png_get_IHDR(fPng_ptr, fInfo_ptr, &origWidth, &origHeight, &bitDepth,
msarettac6c7502016-04-25 09:30:24 -0700844 &encodedColorType, nullptr, nullptr, nullptr);
845
Matt Sarett7a1cc672016-12-14 11:48:31 -0500846 // TODO: Should we support 16-bits of precision for gray images?
847 if (bitDepth == 16 && (PNG_COLOR_TYPE_GRAY == encodedColorType ||
848 PNG_COLOR_TYPE_GRAY_ALPHA == encodedColorType)) {
849 bitDepth = 8;
Leon Scroggins III83239652017-04-21 13:47:12 -0400850 png_set_strip_16(fPng_ptr);
msarettac6c7502016-04-25 09:30:24 -0700851 }
852
853 // Now determine the default colorType and alphaType and set the required transforms.
854 // Often, we depend on SkSwizzler to perform any transforms that we need. However, we
855 // still depend on libpng for many of the rare and PNG-specific cases.
856 SkEncodedInfo::Color color;
857 SkEncodedInfo::Alpha alpha;
858 switch (encodedColorType) {
859 case PNG_COLOR_TYPE_PALETTE:
860 // Extract multiple pixels with bit depths of 1, 2, and 4 from a single
861 // byte into separate bytes (useful for paletted and grayscale images).
862 if (bitDepth < 8) {
863 // TODO: Should we use SkSwizzler here?
Matt Sarett7a1cc672016-12-14 11:48:31 -0500864 bitDepth = 8;
Leon Scroggins III83239652017-04-21 13:47:12 -0400865 png_set_packing(fPng_ptr);
msarettac6c7502016-04-25 09:30:24 -0700866 }
867
868 color = SkEncodedInfo::kPalette_Color;
869 // Set the alpha depending on if a transparency chunk exists.
Leon Scroggins III83239652017-04-21 13:47:12 -0400870 alpha = png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS) ?
msarettac6c7502016-04-25 09:30:24 -0700871 SkEncodedInfo::kUnpremul_Alpha : SkEncodedInfo::kOpaque_Alpha;
872 break;
873 case PNG_COLOR_TYPE_RGB:
Leon Scroggins III83239652017-04-21 13:47:12 -0400874 if (png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS)) {
msarettac6c7502016-04-25 09:30:24 -0700875 // Convert to RGBA if transparency chunk exists.
Leon Scroggins III83239652017-04-21 13:47:12 -0400876 png_set_tRNS_to_alpha(fPng_ptr);
msarettac6c7502016-04-25 09:30:24 -0700877 color = SkEncodedInfo::kRGBA_Color;
878 alpha = SkEncodedInfo::kBinary_Alpha;
879 } else {
880 color = SkEncodedInfo::kRGB_Color;
881 alpha = SkEncodedInfo::kOpaque_Alpha;
882 }
883 break;
884 case PNG_COLOR_TYPE_GRAY:
885 // Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel.
886 if (bitDepth < 8) {
887 // TODO: Should we use SkSwizzler here?
Matt Sarett7a1cc672016-12-14 11:48:31 -0500888 bitDepth = 8;
Leon Scroggins III83239652017-04-21 13:47:12 -0400889 png_set_expand_gray_1_2_4_to_8(fPng_ptr);
msarettac6c7502016-04-25 09:30:24 -0700890 }
891
Leon Scroggins III83239652017-04-21 13:47:12 -0400892 if (png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS)) {
893 png_set_tRNS_to_alpha(fPng_ptr);
msarettac6c7502016-04-25 09:30:24 -0700894 color = SkEncodedInfo::kGrayAlpha_Color;
895 alpha = SkEncodedInfo::kBinary_Alpha;
896 } else {
897 color = SkEncodedInfo::kGray_Color;
898 alpha = SkEncodedInfo::kOpaque_Alpha;
899 }
900 break;
901 case PNG_COLOR_TYPE_GRAY_ALPHA:
902 color = SkEncodedInfo::kGrayAlpha_Color;
903 alpha = SkEncodedInfo::kUnpremul_Alpha;
904 break;
905 case PNG_COLOR_TYPE_RGBA:
906 color = SkEncodedInfo::kRGBA_Color;
907 alpha = SkEncodedInfo::kUnpremul_Alpha;
908 break;
909 default:
910 // All the color types have been covered above.
911 SkASSERT(false);
912 color = SkEncodedInfo::kRGBA_Color;
913 alpha = SkEncodedInfo::kUnpremul_Alpha;
914 }
scroggo8e6c7ad2016-09-16 08:20:38 -0700915
916 const int numberPasses = png_set_interlace_handling(fPng_ptr);
917
scroggo8e6c7ad2016-09-16 08:20:38 -0700918 if (fOutCodec) {
919 SkASSERT(nullptr == *fOutCodec);
Leon Scroggins IIIf78b55c2017-10-31 13:49:14 -0400920 sk_sp<SkColorSpace> colorSpace = read_color_space(fPng_ptr, fInfo_ptr);
921 if (colorSpace) {
922 switch (colorSpace->type()) {
923 case SkColorSpace::kCMYK_Type:
924 colorSpace = nullptr;
925 break;
926 case SkColorSpace::kGray_Type:
927 if (SkEncodedInfo::kGray_Color != color &&
928 SkEncodedInfo::kGrayAlpha_Color != color)
929 {
930 colorSpace = nullptr;
931 }
932 break;
933 case SkColorSpace::kRGB_Type:
934 break;
935 }
Matt Sarett523116d2017-01-12 18:36:38 -0500936 }
msarettf34cd632016-05-25 10:13:53 -0700937 if (!colorSpace) {
raftiasd737bee2016-12-08 10:53:24 -0500938 // Treat unsupported/invalid color spaces as sRGB.
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500939 colorSpace = SkColorSpace::MakeSRGB();
msarettf34cd632016-05-25 10:13:53 -0700940 }
scroggod8d68552016-06-06 11:26:17 -0700941
Matt Sarett7a1cc672016-12-14 11:48:31 -0500942 SkEncodedInfo encodedInfo = SkEncodedInfo::Make(color, alpha, bitDepth);
msarett549ca322016-08-17 08:54:08 -0700943 SkImageInfo imageInfo = encodedInfo.makeImageInfo(origWidth, origHeight, colorSpace);
944
Mike Reedd6cb11e2017-11-30 15:33:04 -0500945 if (encodedColorType == PNG_COLOR_TYPE_GRAY_ALPHA) {
946 png_color_8p sigBits;
947 if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) {
948 if (8 == sigBits->alpha && kGraySigBit_GrayAlphaIsJustAlpha == sigBits->gray) {
949 imageInfo = imageInfo.makeColorType(kAlpha_8_SkColorType);
950 }
951 }
952 } else if (SkEncodedInfo::kOpaque_Alpha == alpha) {
msarett549ca322016-08-17 08:54:08 -0700953 png_color_8p sigBits;
scroggo8e6c7ad2016-09-16 08:20:38 -0700954 if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) {
msarett549ca322016-08-17 08:54:08 -0700955 if (5 == sigBits->red && 6 == sigBits->green && 5 == sigBits->blue) {
956 // Recommend a decode to 565 if the sBIT indicates 565.
957 imageInfo = imageInfo.makeColorType(kRGB_565_SkColorType);
958 }
959 }
960 }
scroggod8d68552016-06-06 11:26:17 -0700961
msarettac6c7502016-04-25 09:30:24 -0700962 if (1 == numberPasses) {
Mike Reedede7bac2017-07-23 15:30:02 -0400963 *fOutCodec = new SkPngNormalDecoder(encodedInfo, imageInfo,
964 std::unique_ptr<SkStream>(fStream), fChunkReader, fPng_ptr, fInfo_ptr, bitDepth);
msarettac6c7502016-04-25 09:30:24 -0700965 } else {
Mike Reedede7bac2017-07-23 15:30:02 -0400966 *fOutCodec = new SkPngInterlacedDecoder(encodedInfo, imageInfo,
967 std::unique_ptr<SkStream>(fStream), fChunkReader, fPng_ptr, fInfo_ptr, bitDepth,
968 numberPasses);
msarettac6c7502016-04-25 09:30:24 -0700969 }
Leon Scroggins III83239652017-04-21 13:47:12 -0400970 static_cast<SkPngCodec*>(*fOutCodec)->setIdatLength(idatLength);
msarettac6c7502016-04-25 09:30:24 -0700971 }
972
scroggo8e6c7ad2016-09-16 08:20:38 -0700973 // Release the pointers, which are now owned by the codec or the caller is expected to
974 // take ownership.
975 this->releasePngPtrs();
msarettac6c7502016-04-25 09:30:24 -0700976}
977
msarett549ca322016-08-17 08:54:08 -0700978SkPngCodec::SkPngCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo,
Mike Reedede7bac2017-07-23 15:30:02 -0400979 std::unique_ptr<SkStream> stream, SkPngChunkReader* chunkReader,
980 void* png_ptr, void* info_ptr, int bitDepth)
981 : INHERITED(encodedInfo, imageInfo, png_select_xform_format(encodedInfo), std::move(stream))
msarettac6c7502016-04-25 09:30:24 -0700982 , fPngChunkReader(SkSafeRef(chunkReader))
983 , fPng_ptr(png_ptr)
984 , fInfo_ptr(info_ptr)
msarettd1ec89b2016-08-03 12:59:27 -0700985 , fColorXformSrcRow(nullptr)
msarettac6c7502016-04-25 09:30:24 -0700986 , fBitDepth(bitDepth)
Leon Scroggins III83239652017-04-21 13:47:12 -0400987 , fIdatLength(0)
988 , fDecodedIdat(false)
msarettac6c7502016-04-25 09:30:24 -0700989{}
990
991SkPngCodec::~SkPngCodec() {
992 this->destroyReadStruct();
993}
994
995void SkPngCodec::destroyReadStruct() {
996 if (fPng_ptr) {
997 // We will never have a nullptr fInfo_ptr with a non-nullptr fPng_ptr
998 SkASSERT(fInfo_ptr);
mtklein6dc5b9a2016-08-24 12:22:32 -0700999 png_destroy_read_struct((png_struct**)&fPng_ptr, (png_info**)&fInfo_ptr, nullptr);
msarettac6c7502016-04-25 09:30:24 -07001000 fPng_ptr = nullptr;
1001 fInfo_ptr = nullptr;
1002 }
1003}
1004
1005///////////////////////////////////////////////////////////////////////////////
1006// Getting the pixels
1007///////////////////////////////////////////////////////////////////////////////
1008
Leon Scroggins571b30f2017-07-11 17:35:31 +00001009SkCodec::Result SkPngCodec::initializeXforms(const SkImageInfo& dstInfo, const Options& options) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001010 if (setjmp(PNG_JMPBUF((png_struct*)fPng_ptr))) {
msarettd1ec89b2016-08-03 12:59:27 -07001011 SkCodecPrintf("Failed on png_read_update_info.\n");
Matt Sarettd59948a2017-04-26 10:59:48 -04001012 return kInvalidInput;
msarettac6c7502016-04-25 09:30:24 -07001013 }
1014 png_read_update_info(fPng_ptr, fInfo_ptr);
1015
Matt Sarett313c4632016-10-20 12:35:23 -04001016 // Reset fSwizzler and this->colorXform(). We can't do this in onRewind() because the
msarett400a93b2016-09-01 18:32:52 -07001017 // interlaced scanline decoder may need to rewind.
1018 fSwizzler.reset(nullptr);
msarett400a93b2016-09-01 18:32:52 -07001019
Matt Sarett34c69d62017-01-19 17:42:23 -05001020 // If SkColorSpaceXform directly supports the encoded PNG format, we should skip format
1021 // conversion in the swizzler (or skip swizzling altogether).
1022 bool skipFormatConversion = false;
1023 switch (this->getEncodedInfo().color()) {
1024 case SkEncodedInfo::kRGB_Color:
1025 if (this->getEncodedInfo().bitsPerComponent() != 16) {
1026 break;
1027 }
1028
1029 // Fall through
1030 case SkEncodedInfo::kRGBA_Color:
1031 skipFormatConversion = this->colorXform();
1032 break;
1033 default:
1034 break;
1035 }
Matt Sarett379938e2017-01-12 18:34:29 -05001036 if (skipFormatConversion && !options.fSubset) {
msarett400a93b2016-09-01 18:32:52 -07001037 fXformMode = kColorOnly_XformMode;
Matt Sarettd59948a2017-04-26 10:59:48 -04001038 return kSuccess;
msarettd1ec89b2016-08-03 12:59:27 -07001039 }
1040
msarettac6c7502016-04-25 09:30:24 -07001041 if (SkEncodedInfo::kPalette_Color == this->getEncodedInfo().color()) {
Leon Scroggins571b30f2017-07-11 17:35:31 +00001042 if (!this->createColorTable(dstInfo)) {
Matt Sarettd59948a2017-04-26 10:59:48 -04001043 return kInvalidInput;
msarettac6c7502016-04-25 09:30:24 -07001044 }
1045 }
1046
Matt Sarett379938e2017-01-12 18:34:29 -05001047 this->initializeSwizzler(dstInfo, options, skipFormatConversion);
Matt Sarettd59948a2017-04-26 10:59:48 -04001048 return kSuccess;
msarett400a93b2016-09-01 18:32:52 -07001049}
1050
msarettc0444612016-09-16 11:45:58 -07001051void SkPngCodec::initializeXformParams() {
1052 switch (fXformMode) {
1053 case kColorOnly_XformMode:
msarettc0444612016-09-16 11:45:58 -07001054 fXformWidth = this->dstInfo().width();
1055 break;
1056 case kSwizzleColor_XformMode:
msarettc0444612016-09-16 11:45:58 -07001057 fXformWidth = this->swizzler()->swizzleWidth();
1058 break;
1059 default:
1060 break;
1061 }
scroggo8e6c7ad2016-09-16 08:20:38 -07001062}
1063
Matt Sarett379938e2017-01-12 18:34:29 -05001064void SkPngCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options,
1065 bool skipFormatConversion) {
msarett400a93b2016-09-01 18:32:52 -07001066 SkImageInfo swizzlerInfo = dstInfo;
1067 Options swizzlerOptions = options;
1068 fXformMode = kSwizzleOnly_XformMode;
Leon Scroggins IIIc6e6a5f2017-06-05 15:53:38 -04001069 if (this->colorXform() && this->xformOnDecode()) {
Matt Sarett562e6812016-11-08 16:13:43 -05001070 swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType);
msarett400a93b2016-09-01 18:32:52 -07001071 if (kPremul_SkAlphaType == dstInfo.alphaType()) {
1072 swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType);
1073 }
1074
1075 fXformMode = kSwizzleColor_XformMode;
1076
1077 // Here, we swizzle into temporary memory, which is not zero initialized.
1078 // FIXME (msarett):
1079 // Is this a problem?
1080 swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized;
1081 }
1082
msarettac6c7502016-04-25 09:30:24 -07001083 const SkPMColor* colors = get_color_ptr(fColorTable.get());
msarettd1ec89b2016-08-03 12:59:27 -07001084 fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), colors, swizzlerInfo,
Matt Sarett379938e2017-01-12 18:34:29 -05001085 swizzlerOptions, nullptr, skipFormatConversion));
msarettac6c7502016-04-25 09:30:24 -07001086 SkASSERT(fSwizzler);
msarett400a93b2016-09-01 18:32:52 -07001087}
1088
1089SkSampler* SkPngCodec::getSampler(bool createIfNecessary) {
1090 if (fSwizzler || !createIfNecessary) {
Ben Wagner145dbcd2016-11-03 14:40:50 -04001091 return fSwizzler.get();
msarett400a93b2016-09-01 18:32:52 -07001092 }
1093
Matt Sarett379938e2017-01-12 18:34:29 -05001094 this->initializeSwizzler(this->dstInfo(), this->options(), true);
Ben Wagner145dbcd2016-11-03 14:40:50 -04001095 return fSwizzler.get();
msarettac6c7502016-04-25 09:30:24 -07001096}
1097
msarettac6c7502016-04-25 09:30:24 -07001098bool SkPngCodec::onRewind() {
1099 // This sets fPng_ptr and fInfo_ptr to nullptr. If read_header
1100 // succeeds, they will be repopulated, and if it fails, they will
1101 // remain nullptr. Any future accesses to fPng_ptr and fInfo_ptr will
1102 // come through this function which will rewind and again attempt
1103 // to reinitialize them.
1104 this->destroyReadStruct();
1105
scroggo46c57472015-09-30 08:57:13 -07001106 png_structp png_ptr;
1107 png_infop info_ptr;
Leon Scroggins III588fb042017-07-14 16:32:31 -04001108 if (kSuccess != read_header(this->stream(), fPngChunkReader.get(), nullptr,
1109 &png_ptr, &info_ptr)) {
msarettac6c7502016-04-25 09:30:24 -07001110 return false;
scroggo05245902015-03-25 11:11:52 -07001111 }
1112
msarettac6c7502016-04-25 09:30:24 -07001113 fPng_ptr = png_ptr;
1114 fInfo_ptr = info_ptr;
Leon Scroggins III83239652017-04-21 13:47:12 -04001115 fDecodedIdat = false;
msarettac6c7502016-04-25 09:30:24 -07001116 return true;
1117}
msarett6a738212016-03-04 13:27:35 -08001118
msarettd1ec89b2016-08-03 12:59:27 -07001119SkCodec::Result SkPngCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst,
1120 size_t rowBytes, const Options& options,
msarettac6c7502016-04-25 09:30:24 -07001121 int* rowsDecoded) {
Leon Scroggins571b30f2017-07-11 17:35:31 +00001122 Result result = this->initializeXforms(dstInfo, options);
Matt Sarettd59948a2017-04-26 10:59:48 -04001123 if (kSuccess != result) {
1124 return result;
1125 }
1126
msarettac6c7502016-04-25 09:30:24 -07001127 if (options.fSubset) {
msarettac6c7502016-04-25 09:30:24 -07001128 return kUnimplemented;
scroggo05245902015-03-25 11:11:52 -07001129 }
1130
msarett400a93b2016-09-01 18:32:52 -07001131 this->allocateStorage(dstInfo);
msarettc0444612016-09-16 11:45:58 -07001132 this->initializeXformParams();
scroggo8e6c7ad2016-09-16 08:20:38 -07001133 return this->decodeAllRows(dst, rowBytes, rowsDecoded);
1134}
msarettac6c7502016-04-25 09:30:24 -07001135
scroggo8e6c7ad2016-09-16 08:20:38 -07001136SkCodec::Result SkPngCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo,
Leon Scroggins571b30f2017-07-11 17:35:31 +00001137 void* dst, size_t rowBytes, const SkCodec::Options& options) {
Leon Scroggins571b30f2017-07-11 17:35:31 +00001138 Result result = this->initializeXforms(dstInfo, options);
Matt Sarettd59948a2017-04-26 10:59:48 -04001139 if (kSuccess != result) {
1140 return result;
1141 }
1142
scroggo8e6c7ad2016-09-16 08:20:38 -07001143 this->allocateStorage(dstInfo);
1144
1145 int firstRow, lastRow;
1146 if (options.fSubset) {
1147 firstRow = options.fSubset->top();
1148 lastRow = options.fSubset->bottom() - 1;
1149 } else {
1150 firstRow = 0;
1151 lastRow = dstInfo.height() - 1;
1152 }
1153 this->setRange(firstRow, lastRow, dst, rowBytes);
scroggod8d68552016-06-06 11:26:17 -07001154 return kSuccess;
scroggo6fb23912016-06-02 14:16:43 -07001155}
1156
scroggo8e6c7ad2016-09-16 08:20:38 -07001157SkCodec::Result SkPngCodec::onIncrementalDecode(int* rowsDecoded) {
1158 // FIXME: Only necessary on the first call.
msarettc0444612016-09-16 11:45:58 -07001159 this->initializeXformParams();
scroggo8e6c7ad2016-09-16 08:20:38 -07001160
1161 return this->decode(rowsDecoded);
1162}
1163
msarettf7eb6fc2016-09-13 09:04:11 -07001164uint64_t SkPngCodec::onGetFillValue(const SkImageInfo& dstInfo) const {
msarettac6c7502016-04-25 09:30:24 -07001165 const SkPMColor* colorPtr = get_color_ptr(fColorTable.get());
1166 if (colorPtr) {
msarettc0444612016-09-16 11:45:58 -07001167 SkAlphaType alphaType = select_xform_alpha(dstInfo.alphaType(),
msarettf7eb6fc2016-09-13 09:04:11 -07001168 this->getInfo().alphaType());
1169 return get_color_table_fill_value(dstInfo.colorType(), alphaType, colorPtr, 0,
Matt Sarett19aff5d2017-04-03 16:01:10 -04001170 this->colorXform(), true);
msarettac6c7502016-04-25 09:30:24 -07001171 }
msarettf7eb6fc2016-09-13 09:04:11 -07001172 return INHERITED::onGetFillValue(dstInfo);
msarettac6c7502016-04-25 09:30:24 -07001173}
1174
Mike Reedede7bac2017-07-23 15:30:02 -04001175std::unique_ptr<SkCodec> SkPngCodec::MakeFromStream(std::unique_ptr<SkStream> stream,
1176 Result* result, SkPngChunkReader* chunkReader) {
scroggo8e6c7ad2016-09-16 08:20:38 -07001177 SkCodec* outCodec = nullptr;
Mike Reedede7bac2017-07-23 15:30:02 -04001178 *result = read_header(stream.get(), chunkReader, &outCodec, nullptr, nullptr);
Leon Scroggins III588fb042017-07-14 16:32:31 -04001179 if (kSuccess == *result) {
msarettac6c7502016-04-25 09:30:24 -07001180 // Codec has taken ownership of the stream.
1181 SkASSERT(outCodec);
Mike Reedede7bac2017-07-23 15:30:02 -04001182 stream.release();
msarettac6c7502016-04-25 09:30:24 -07001183 }
Mike Reedede7bac2017-07-23 15:30:02 -04001184 return std::unique_ptr<SkCodec>(outCodec);
scroggo05245902015-03-25 11:11:52 -07001185}