scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 8 | #include "SkBitmap.h" |
msarett | 7411438 | 2015-03-16 11:55:18 -0700 | [diff] [blame] | 9 | #include "SkCodecPriv.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 10 | #include "SkColorData.h" |
Matt Sarett | c434f51 | 2016-10-13 18:24:20 -0400 | [diff] [blame] | 11 | #include "SkColorSpace.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 12 | #include "SkColorTable.h" |
Hal Canary | 22be4c4 | 2018-06-12 12:37:31 -0400 | [diff] [blame] | 13 | #include "SkMacros.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 14 | #include "SkMath.h" |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 15 | #include "SkOpts.h" |
msarett | be1d555 | 2016-01-21 09:05:23 -0800 | [diff] [blame] | 16 | #include "SkPngCodec.h" |
Mike Reed | d6cb11e | 2017-11-30 15:33:04 -0500 | [diff] [blame] | 17 | #include "SkPngPriv.h" |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 18 | #include "SkPoint3.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 19 | #include "SkSize.h" |
| 20 | #include "SkStream.h" |
| 21 | #include "SkSwizzler.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 22 | #include "SkTemplates.h" |
bungeman | 5d2cd6e | 2016-02-23 07:34:25 -0800 | [diff] [blame] | 23 | #include "SkUtils.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 24 | |
mtklein | 6321381 | 2016-08-24 09:55:56 -0700 | [diff] [blame] | 25 | #include "png.h" |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 26 | #include <algorithm> |
mtklein | 6321381 | 2016-08-24 09:55:56 -0700 | [diff] [blame] | 27 | |
mtklein | dc90b53 | 2016-07-28 14:45:28 -0700 | [diff] [blame] | 28 | // 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 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 33 | // 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 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 37 | // Callback functions |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 40 | // When setjmp is first called, it returns 0, meaning longjmp was not called. |
| 41 | constexpr int kSetJmpOkay = 0; |
| 42 | // An error internal to libpng. |
| 43 | constexpr int kPngError = 1; |
| 44 | // Passed to longjmp when we have decoded as many lines as we need. |
| 45 | constexpr int kStopDecoding = 2; |
| 46 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 47 | static void sk_error_fn(png_structp png_ptr, png_const_charp msg) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 48 | SkCodecPrintf("------ png error %s\n", msg); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 49 | longjmp(PNG_JMPBUF(png_ptr), kPngError); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 50 | } |
| 51 | |
scroggo | 0eed6df | 2015-03-26 10:07:56 -0700 | [diff] [blame] | 52 | void sk_warning_fn(png_structp, png_const_charp msg) { |
| 53 | SkCodecPrintf("----- png warning %s\n", msg); |
| 54 | } |
| 55 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 56 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 57 | static 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 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | // Helpers |
| 66 | /////////////////////////////////////////////////////////////////////////////// |
| 67 | |
| 68 | class AutoCleanPng : public SkNoncopyable { |
| 69 | public: |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 70 | /* |
| 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) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 78 | : fPng_ptr(png_ptr) |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 79 | , fInfo_ptr(nullptr) |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 80 | , fStream(stream) |
| 81 | , fChunkReader(reader) |
| 82 | , fOutCodec(codecPtr) |
| 83 | {} |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 84 | |
| 85 | ~AutoCleanPng() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 86 | // fInfo_ptr will never be non-nullptr unless fPng_ptr is. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 87 | if (fPng_ptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 88 | png_infopp info_pp = fInfo_ptr ? &fInfo_ptr : nullptr; |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 89 | png_destroy_read_struct(&fPng_ptr, info_pp, nullptr); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | void setInfoPtr(png_infop info_ptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 94 | SkASSERT(nullptr == fInfo_ptr); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 95 | fInfo_ptr = info_ptr; |
| 96 | } |
| 97 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 98 | /** |
| 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 | |
| 112 | private: |
| 113 | png_structp fPng_ptr; |
| 114 | png_infop fInfo_ptr; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 115 | SkStream* fStream; |
| 116 | SkPngChunkReader* fChunkReader; |
| 117 | SkCodec** fOutCodec; |
| 118 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 119 | void infoCallback(size_t idatLength); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 120 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 121 | void releasePngPtrs() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 122 | fPng_ptr = nullptr; |
| 123 | fInfo_ptr = nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 124 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 125 | }; |
| 126 | #define AutoCleanPng(...) SK_REQUIRE_LOCAL_VAR(AutoCleanPng) |
| 127 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 128 | static inline bool is_chunk(const png_byte* chunk, const char* tag) { |
| 129 | return memcmp(chunk + 4, tag, 4) == 0; |
| 130 | } |
| 131 | |
| 132 | static 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 III | b644650 | 2017-04-24 09:32:50 -0400 | [diff] [blame] | 136 | 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 III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 139 | return false; |
| 140 | } |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 141 | length -= bytesToProcess; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 146 | bool AutoCleanPng::decodeBounds() { |
| 147 | if (setjmp(PNG_JMPBUF(fPng_ptr))) { |
| 148 | return false; |
| 149 | } |
| 150 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 151 | png_set_progressive_read_fn(fPng_ptr, nullptr, nullptr, nullptr, nullptr); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 152 | |
| 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 III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 159 | { |
| 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 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 168 | while (true) { |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 169 | // Parse chunk length and type. |
| 170 | if (fStream->read(buffer, 8) < 8) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 171 | // We have read to the end of the input without decoding bounds. |
| 172 | break; |
| 173 | } |
| 174 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 175 | 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; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 190 | return false; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 191 | } |
| 192 | |
nagarajan.n | dd7ffa5 | 2017-09-29 08:23:32 +0530 | [diff] [blame] | 193 | bool SkPngCodec::processData() { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 194 | 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.n | dd7ffa5 | 2017-09-29 08:23:32 +0530 | [diff] [blame] | 198 | return false;; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 199 | case kStopDecoding: |
| 200 | // We decoded all the lines we want. |
nagarajan.n | dd7ffa5 | 2017-09-29 08:23:32 +0530 | [diff] [blame] | 201 | return true; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 202 | 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 III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 214 | bool iend = false; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 215 | while (true) { |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 216 | size_t length; |
| 217 | if (fDecodedIdat) { |
| 218 | // Parse chunk length and type. |
| 219 | if (this->stream()->read(buffer, 8) < 8) { |
| 220 | break; |
| 221 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 222 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 223 | 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) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 241 | break; |
| 242 | } |
| 243 | } |
nagarajan.n | dd7ffa5 | 2017-09-29 08:23:32 +0530 | [diff] [blame] | 244 | |
| 245 | return true; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Leon Scroggins III | 862c196 | 2017-10-02 16:28:49 -0400 | [diff] [blame] | 248 | static constexpr SkColorType kXformSrcColorType = kRGBA_8888_SkColorType; |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 249 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 250 | static inline bool needs_premul(SkAlphaType dstAT, SkEncodedInfo::Alpha encodedAlpha) { |
| 251 | return kPremul_SkAlphaType == dstAT && SkEncodedInfo::kUnpremul_Alpha == encodedAlpha; |
| 252 | } |
| 253 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 254 | // Note: SkColorTable claims to store SkPMColors, which is not necessarily the case here. |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 255 | bool SkPngCodec::createColorTable(const SkImageInfo& dstInfo) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 256 | |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 257 | int numColors; |
| 258 | png_color* palette; |
| 259 | if (!png_get_PLTE(fPng_ptr, fInfo_ptr, &palette, &numColors)) { |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 260 | return false; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 261 | } |
| 262 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 263 | // Contents depend on tableColorType and our choice of if/when to premultiply: |
| 264 | // { kPremul, kUnpremul, kOpaque } x { RGBA, BGRA } |
| 265 | SkPMColor colorTable[256]; |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 266 | SkColorType tableColorType = this->colorXform() ? kXformSrcColorType : dstInfo.colorType(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 267 | |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 268 | png_bytep alphas; |
| 269 | int numColorsWithAlpha = 0; |
| 270 | if (png_get_tRNS(fPng_ptr, fInfo_ptr, &alphas, &numColorsWithAlpha, nullptr)) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 271 | bool premultiply = needs_premul(dstInfo.alphaType(), this->getEncodedInfo().alpha()); |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 272 | |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 273 | // Choose which function to use to create the color table. If the final destination's |
| 274 | // colortype is unpremultiplied, the color table will store unpremultiplied colors. |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 275 | PackColorProc proc = choose_pack_color_proc(premultiply, tableColorType); |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 276 | |
| 277 | for (int i = 0; i < numColorsWithAlpha; i++) { |
| 278 | // We don't have a function in SkOpts that combines a set of alphas with a set |
| 279 | // of RGBs. We could write one, but it's hardly worth it, given that this |
| 280 | // is such a small fraction of the total decode time. |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 281 | colorTable[i] = proc(alphas[i], palette->red, palette->green, palette->blue); |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 282 | palette++; |
| 283 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 284 | } |
| 285 | |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 286 | if (numColorsWithAlpha < numColors) { |
| 287 | // The optimized code depends on a 3-byte png_color struct with the colors |
| 288 | // in RGB order. These checks make sure it is safe to use. |
| 289 | static_assert(3 == sizeof(png_color), "png_color struct has changed. Opts are broken."); |
| 290 | #ifdef SK_DEBUG |
| 291 | SkASSERT(&palette->red < &palette->green); |
| 292 | SkASSERT(&palette->green < &palette->blue); |
| 293 | #endif |
| 294 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 295 | if (is_rgba(tableColorType)) { |
| 296 | SkOpts::RGB_to_RGB1(colorTable + numColorsWithAlpha, palette, |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 297 | numColors - numColorsWithAlpha); |
| 298 | } else { |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 299 | SkOpts::RGB_to_BGR1(colorTable + numColorsWithAlpha, palette, |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 300 | numColors - numColorsWithAlpha); |
| 301 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 304 | if (this->colorXform() && !this->xformOnDecode()) { |
| 305 | this->applyColorXform(colorTable, colorTable, numColors); |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 306 | } |
| 307 | |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 308 | // Pad the color table with the last color in the table (or black) in the case that |
| 309 | // invalid pixel indices exceed the number of colors in the table. |
| 310 | const int maxColors = 1 << fBitDepth; |
| 311 | if (numColors < maxColors) { |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 312 | SkPMColor lastColor = numColors > 0 ? colorTable[numColors - 1] : SK_ColorBLACK; |
| 313 | sk_memset32(colorTable + numColors, lastColor, maxColors - numColors); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 314 | } |
| 315 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 316 | fColorTable.reset(new SkColorTable(colorTable, maxColors)); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 317 | return true; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /////////////////////////////////////////////////////////////////////////////// |
| 321 | // Creation |
| 322 | /////////////////////////////////////////////////////////////////////////////// |
| 323 | |
scroggo | db30be2 | 2015-12-08 18:54:13 -0800 | [diff] [blame] | 324 | bool SkPngCodec::IsPng(const char* buf, size_t bytesRead) { |
| 325 | return !png_sig_cmp((png_bytep) buf, (png_size_t)0, bytesRead); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 326 | } |
| 327 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 328 | #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6) |
| 329 | |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 330 | static float png_fixed_point_to_float(png_fixed_point x) { |
| 331 | // We multiply by the same factor that libpng used to convert |
| 332 | // fixed point -> double. Since we want floats, we choose to |
| 333 | // do the conversion ourselves rather than convert |
| 334 | // fixed point -> double -> float. |
| 335 | return ((float) x) * 0.00001f; |
| 336 | } |
| 337 | |
msarett | 128245c | 2016-03-30 12:01:47 -0700 | [diff] [blame] | 338 | static float png_inverted_fixed_point_to_float(png_fixed_point x) { |
| 339 | // This is necessary because the gAMA chunk actually stores 1/gamma. |
| 340 | return 1.0f / png_fixed_point_to_float(x); |
| 341 | } |
| 342 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 343 | #endif // LIBPNG >= 1.6 |
| 344 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 345 | // If there is no color profile information, it will use sRGB. |
| 346 | std::unique_ptr<SkEncodedInfo::ICCProfile> read_color_profile(png_structp png_ptr, |
| 347 | png_infop info_ptr) { |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 348 | |
msarett | e244322 | 2016-03-04 14:20:49 -0800 | [diff] [blame] | 349 | #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6) |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 350 | // First check for an ICC profile |
| 351 | png_bytep profile; |
| 352 | png_uint_32 length; |
| 353 | // The below variables are unused, however, we need to pass them in anyway or |
| 354 | // png_get_iCCP() will return nothing. |
| 355 | // Could knowing the |name| of the profile ever be interesting? Maybe for debugging? |
| 356 | png_charp name; |
| 357 | // The |compression| is uninteresting since: |
| 358 | // (1) libpng has already decompressed the profile for us. |
| 359 | // (2) "deflate" is the only mode of decompression that libpng supports. |
| 360 | int compression; |
| 361 | if (PNG_INFO_iCCP == png_get_iCCP(png_ptr, info_ptr, &name, &compression, &profile, |
| 362 | &length)) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 363 | auto data = SkData::MakeWithCopy(profile, length); |
| 364 | return SkEncodedInfo::ICCProfile::Make(std::move(data)); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // Second, check for sRGB. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 368 | // Note that Blink does this first. This code checks ICC first, with the thinking that |
| 369 | // an image has both truly wants the potentially more specific ICC chunk, with sRGB as a |
| 370 | // backup in case the decoder does not support full color management. |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 371 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) { |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 372 | // sRGB chunks also store a rendering intent: Absolute, Relative, |
| 373 | // Perceptual, and Saturation. |
| 374 | // FIXME (msarett): Extract this information from the sRGB chunk once |
| 375 | // we are able to handle this information in |
| 376 | // SkColorSpace. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 377 | return SkEncodedInfo::ICCProfile::MakeSRGB(); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 378 | } |
| 379 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 380 | // Default to SRGB gamut. |
| 381 | skcms_Matrix3x3 toXYZD50 = skcms_sRGB_profile()->toXYZD50; |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 382 | // Next, check for chromaticities. |
msarett | a5a31dd | 2016-10-11 09:41:16 -0700 | [diff] [blame] | 383 | png_fixed_point chrm[8]; |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 384 | png_fixed_point gamma; |
msarett | a5a31dd | 2016-10-11 09:41:16 -0700 | [diff] [blame] | 385 | if (png_get_cHRM_fixed(png_ptr, info_ptr, &chrm[0], &chrm[1], &chrm[2], &chrm[3], &chrm[4], |
| 386 | &chrm[5], &chrm[6], &chrm[7])) |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 387 | { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 388 | float rx = png_fixed_point_to_float(chrm[2]); |
| 389 | float ry = png_fixed_point_to_float(chrm[3]); |
| 390 | float gx = png_fixed_point_to_float(chrm[4]); |
| 391 | float gy = png_fixed_point_to_float(chrm[5]); |
| 392 | float bx = png_fixed_point_to_float(chrm[6]); |
| 393 | float by = png_fixed_point_to_float(chrm[7]); |
| 394 | float wx = png_fixed_point_to_float(chrm[0]); |
| 395 | float wy = png_fixed_point_to_float(chrm[1]); |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 396 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 397 | skcms_Matrix3x3 tmp; |
| 398 | if (skcms_PrimariesToXYZD50(rx, ry, gx, gy, bx, by, wx, wy, &tmp)) { |
| 399 | toXYZD50 = tmp; |
| 400 | } else { |
| 401 | // Note that Blink simply returns nullptr in this case. We'll fall |
| 402 | // back to srgb. |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 403 | } |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 404 | } |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 405 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 406 | skcms_TransferFunction fn; |
| 407 | if (PNG_INFO_gAMA == png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) { |
| 408 | fn.a = 1.0f; |
| 409 | fn.b = fn.c = fn.d = fn.e = fn.f = 0.0f; |
| 410 | fn.g = png_inverted_fixed_point_to_float(gamma); |
| 411 | } else { |
msarett | c4ce6b5 | 2016-06-16 07:37:41 -0700 | [diff] [blame] | 412 | // Default to sRGB gamma if the image has color space information, |
| 413 | // but does not specify gamma. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 414 | // Note that Blink would again return nullptr in this case. |
| 415 | fn = *skcms_sRGB_TransferFunction(); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 418 | skcms_ICCProfile skcmsProfile; |
| 419 | skcms_Init(&skcmsProfile); |
| 420 | skcms_SetTransferFunction(&skcmsProfile, &fn); |
| 421 | skcms_SetXYZD50(&skcmsProfile, &toXYZD50); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 422 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 423 | return SkEncodedInfo::ICCProfile::Make(skcmsProfile); |
| 424 | #else // LIBPNG >= 1.6 |
| 425 | return SkEncodedInfo::ICCProfile::MakeSRGB(); |
msarett | e244322 | 2016-03-04 14:20:49 -0800 | [diff] [blame] | 426 | #endif // LIBPNG >= 1.6 |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 427 | } |
| 428 | |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 429 | void SkPngCodec::allocateStorage(const SkImageInfo& dstInfo) { |
| 430 | switch (fXformMode) { |
| 431 | case kSwizzleOnly_XformMode: |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 432 | break; |
| 433 | case kColorOnly_XformMode: |
| 434 | // Intentional fall through. A swizzler hasn't been created yet, but one will |
| 435 | // be created later if we are sampling. We'll go ahead and allocate |
| 436 | // enough memory to swizzle if necessary. |
| 437 | case kSwizzleColor_XformMode: { |
Matt Sarett | 34c69d6 | 2017-01-19 17:42:23 -0500 | [diff] [blame] | 438 | const int bitsPerPixel = this->getEncodedInfo().bitsPerPixel(); |
| 439 | |
| 440 | // If we have more than 8-bits (per component) of precision, we will keep that |
| 441 | // extra precision. Otherwise, we will swizzle to RGBA_8888 before transforming. |
| 442 | const size_t bytesPerPixel = (bitsPerPixel > 32) ? bitsPerPixel / 8 : 4; |
| 443 | const size_t colorXformBytes = dstInfo.width() * bytesPerPixel; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 444 | fStorage.reset(colorXformBytes); |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 445 | fColorXformSrcRow = fStorage.get(); |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | } |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 451 | static skcms_PixelFormat png_select_xform_format(const SkEncodedInfo& info) { |
Matt Sarett | 34c69d6 | 2017-01-19 17:42:23 -0500 | [diff] [blame] | 452 | // We use kRGB and kRGBA formats because color PNGs are always RGB or RGBA. |
| 453 | if (16 == info.bitsPerComponent()) { |
| 454 | if (SkEncodedInfo::kRGBA_Color == info.color()) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 455 | return skcms_PixelFormat_RGBA_16161616; |
Matt Sarett | 34c69d6 | 2017-01-19 17:42:23 -0500 | [diff] [blame] | 456 | } else if (SkEncodedInfo::kRGB_Color == info.color()) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 457 | return skcms_PixelFormat_RGB_161616; |
Matt Sarett | 34c69d6 | 2017-01-19 17:42:23 -0500 | [diff] [blame] | 458 | } |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 459 | } |
| 460 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 461 | return skcms_PixelFormat_RGBA_8888; |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 462 | } |
| 463 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 464 | void SkPngCodec::applyXformRow(void* dst, const void* src) { |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 465 | switch (fXformMode) { |
| 466 | case kSwizzleOnly_XformMode: |
| 467 | fSwizzler->swizzle(dst, (const uint8_t*) src); |
| 468 | break; |
| 469 | case kColorOnly_XformMode: |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 470 | this->applyColorXform(dst, src, fXformWidth); |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 471 | break; |
| 472 | case kSwizzleColor_XformMode: |
| 473 | fSwizzler->swizzle(fColorXformSrcRow, (const uint8_t*) src); |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 474 | this->applyColorXform(dst, fColorXformSrcRow, fXformWidth); |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 475 | break; |
| 476 | } |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 477 | } |
| 478 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 479 | class SkPngNormalDecoder : public SkPngCodec { |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 480 | public: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 481 | SkPngNormalDecoder(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream, |
| 482 | SkPngChunkReader* reader, png_structp png_ptr, png_infop info_ptr, int bitDepth) |
| 483 | : INHERITED(std::move(info), std::move(stream), reader, png_ptr, info_ptr, bitDepth) |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 484 | , fRowsWrittenToOutput(0) |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 485 | , fDst(nullptr) |
| 486 | , fRowBytes(0) |
| 487 | , fFirstRow(0) |
| 488 | , fLastRow(0) |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 489 | {} |
| 490 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 491 | static void AllRowsCallback(png_structp png_ptr, png_bytep row, png_uint_32 rowNum, int /*pass*/) { |
| 492 | GetDecoder(png_ptr)->allRowsCallback(row, rowNum); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 493 | } |
| 494 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 495 | static void RowCallback(png_structp png_ptr, png_bytep row, png_uint_32 rowNum, int /*pass*/) { |
| 496 | GetDecoder(png_ptr)->rowCallback(row, rowNum); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 497 | } |
| 498 | |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 499 | private: |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 500 | int fRowsWrittenToOutput; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 501 | void* fDst; |
| 502 | size_t fRowBytes; |
| 503 | |
| 504 | // Variables for partial decode |
| 505 | int fFirstRow; // FIXME: Move to baseclass? |
| 506 | int fLastRow; |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 507 | int fRowsNeeded; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 508 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 509 | typedef SkPngCodec INHERITED; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 510 | |
| 511 | static SkPngNormalDecoder* GetDecoder(png_structp png_ptr) { |
| 512 | return static_cast<SkPngNormalDecoder*>(png_get_progressive_ptr(png_ptr)); |
| 513 | } |
| 514 | |
| 515 | Result decodeAllRows(void* dst, size_t rowBytes, int* rowsDecoded) override { |
| 516 | const int height = this->getInfo().height(); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 517 | png_set_progressive_read_fn(this->png_ptr(), this, nullptr, AllRowsCallback, nullptr); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 518 | fDst = dst; |
| 519 | fRowBytes = rowBytes; |
| 520 | |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 521 | fRowsWrittenToOutput = 0; |
scroggo | c46cdd4 | 2016-10-10 06:45:32 -0700 | [diff] [blame] | 522 | fFirstRow = 0; |
| 523 | fLastRow = height - 1; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 524 | |
nagarajan.n | dd7ffa5 | 2017-09-29 08:23:32 +0530 | [diff] [blame] | 525 | if (!this->processData()) { |
| 526 | return kErrorInInput; |
| 527 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 528 | |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 529 | if (fRowsWrittenToOutput == height) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 530 | return SkCodec::kSuccess; |
| 531 | } |
| 532 | |
| 533 | if (rowsDecoded) { |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 534 | *rowsDecoded = fRowsWrittenToOutput; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | return SkCodec::kIncompleteInput; |
| 538 | } |
| 539 | |
| 540 | void allRowsCallback(png_bytep row, int rowNum) { |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 541 | SkASSERT(rowNum == fRowsWrittenToOutput); |
| 542 | fRowsWrittenToOutput++; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 543 | this->applyXformRow(fDst, row); |
| 544 | fDst = SkTAddOffset<void>(fDst, fRowBytes); |
| 545 | } |
| 546 | |
| 547 | void setRange(int firstRow, int lastRow, void* dst, size_t rowBytes) override { |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 548 | png_set_progressive_read_fn(this->png_ptr(), this, nullptr, RowCallback, nullptr); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 549 | fFirstRow = firstRow; |
| 550 | fLastRow = lastRow; |
| 551 | fDst = dst; |
| 552 | fRowBytes = rowBytes; |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 553 | fRowsWrittenToOutput = 0; |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 554 | fRowsNeeded = fLastRow - fFirstRow + 1; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | SkCodec::Result decode(int* rowsDecoded) override { |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 558 | if (this->swizzler()) { |
| 559 | const int sampleY = this->swizzler()->sampleY(); |
| 560 | fRowsNeeded = get_scaled_dimension(fLastRow - fFirstRow + 1, sampleY); |
| 561 | } |
nagarajan.n | dd7ffa5 | 2017-09-29 08:23:32 +0530 | [diff] [blame] | 562 | |
| 563 | if (!this->processData()) { |
| 564 | return kErrorInInput; |
| 565 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 566 | |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 567 | if (fRowsWrittenToOutput == fRowsNeeded) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 568 | return SkCodec::kSuccess; |
| 569 | } |
| 570 | |
| 571 | if (rowsDecoded) { |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 572 | *rowsDecoded = fRowsWrittenToOutput; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | return SkCodec::kIncompleteInput; |
| 576 | } |
| 577 | |
| 578 | void rowCallback(png_bytep row, int rowNum) { |
| 579 | if (rowNum < fFirstRow) { |
| 580 | // Ignore this row. |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | SkASSERT(rowNum <= fLastRow); |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 585 | SkASSERT(fRowsWrittenToOutput < fRowsNeeded); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 586 | |
| 587 | // If there is no swizzler, all rows are needed. |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 588 | if (!this->swizzler() || this->swizzler()->rowNeeded(rowNum - fFirstRow)) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 589 | this->applyXformRow(fDst, row); |
| 590 | fDst = SkTAddOffset<void>(fDst, fRowBytes); |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 591 | fRowsWrittenToOutput++; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 592 | } |
| 593 | |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 594 | if (fRowsWrittenToOutput == fRowsNeeded) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 595 | // Fake error to stop decoding scanlines. |
| 596 | longjmp(PNG_JMPBUF(this->png_ptr()), kStopDecoding); |
| 597 | } |
| 598 | } |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 599 | }; |
| 600 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 601 | class SkPngInterlacedDecoder : public SkPngCodec { |
| 602 | public: |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 603 | SkPngInterlacedDecoder(SkEncodedInfo&& info, std::unique_ptr<SkStream> stream, |
| 604 | SkPngChunkReader* reader, png_structp png_ptr, |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 605 | png_infop info_ptr, int bitDepth, int numberPasses) |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 606 | : INHERITED(std::move(info), std::move(stream), reader, png_ptr, info_ptr, bitDepth) |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 607 | , fNumberPasses(numberPasses) |
| 608 | , fFirstRow(0) |
| 609 | , fLastRow(0) |
| 610 | , fLinesDecoded(0) |
| 611 | , fInterlacedComplete(false) |
| 612 | , fPng_rowbytes(0) |
| 613 | {} |
| 614 | |
| 615 | static void InterlacedRowCallback(png_structp png_ptr, png_bytep row, png_uint_32 rowNum, int pass) { |
| 616 | auto decoder = static_cast<SkPngInterlacedDecoder*>(png_get_progressive_ptr(png_ptr)); |
| 617 | decoder->interlacedRowCallback(row, rowNum, pass); |
| 618 | } |
| 619 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 620 | private: |
| 621 | const int fNumberPasses; |
| 622 | int fFirstRow; |
| 623 | int fLastRow; |
| 624 | void* fDst; |
| 625 | size_t fRowBytes; |
| 626 | int fLinesDecoded; |
| 627 | bool fInterlacedComplete; |
| 628 | size_t fPng_rowbytes; |
| 629 | SkAutoTMalloc<png_byte> fInterlaceBuffer; |
| 630 | |
| 631 | typedef SkPngCodec INHERITED; |
| 632 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 633 | // FIXME: Currently sharing interlaced callback for all rows and subset. It's not |
| 634 | // as expensive as the subset version of non-interlaced, but it still does extra |
| 635 | // work. |
| 636 | void interlacedRowCallback(png_bytep row, int rowNum, int pass) { |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 637 | if (rowNum < fFirstRow || rowNum > fLastRow || fInterlacedComplete) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 638 | // Ignore this row |
| 639 | return; |
| 640 | } |
| 641 | |
| 642 | png_bytep oldRow = fInterlaceBuffer.get() + (rowNum - fFirstRow) * fPng_rowbytes; |
| 643 | png_progressive_combine_row(this->png_ptr(), oldRow, row); |
| 644 | |
| 645 | if (0 == pass) { |
| 646 | // The first pass initializes all rows. |
| 647 | SkASSERT(row); |
| 648 | SkASSERT(fLinesDecoded == rowNum - fFirstRow); |
| 649 | fLinesDecoded++; |
| 650 | } else { |
| 651 | SkASSERT(fLinesDecoded == fLastRow - fFirstRow + 1); |
| 652 | if (fNumberPasses - 1 == pass && rowNum == fLastRow) { |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 653 | // Last pass, and we have read all of the rows we care about. |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 654 | fInterlacedComplete = true; |
Leon Scroggins III | 600effb | 2017-04-24 15:44:45 -0400 | [diff] [blame] | 655 | if (fLastRow != this->getInfo().height() - 1 || |
| 656 | (this->swizzler() && this->swizzler()->sampleY() != 1)) { |
| 657 | // Fake error to stop decoding scanlines. Only stop if we're not decoding the |
| 658 | // whole image, in which case processing the rest of the image might be |
| 659 | // expensive. When decoding the whole image, read through the IEND chunk to |
| 660 | // preserve Android behavior of leaving the input stream in the right place. |
| 661 | longjmp(PNG_JMPBUF(this->png_ptr()), kStopDecoding); |
| 662 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 663 | } |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | SkCodec::Result decodeAllRows(void* dst, size_t rowBytes, int* rowsDecoded) override { |
| 668 | const int height = this->getInfo().height(); |
| 669 | this->setUpInterlaceBuffer(height); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 670 | png_set_progressive_read_fn(this->png_ptr(), this, nullptr, InterlacedRowCallback, |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 671 | nullptr); |
| 672 | |
| 673 | fFirstRow = 0; |
| 674 | fLastRow = height - 1; |
| 675 | fLinesDecoded = 0; |
| 676 | |
nagarajan.n | dd7ffa5 | 2017-09-29 08:23:32 +0530 | [diff] [blame] | 677 | if (!this->processData()) { |
| 678 | return kErrorInInput; |
| 679 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 680 | |
| 681 | png_bytep srcRow = fInterlaceBuffer.get(); |
| 682 | // FIXME: When resuming, this may rewrite rows that did not change. |
| 683 | for (int rowNum = 0; rowNum < fLinesDecoded; rowNum++) { |
| 684 | this->applyXformRow(dst, srcRow); |
| 685 | dst = SkTAddOffset<void>(dst, rowBytes); |
| 686 | srcRow = SkTAddOffset<png_byte>(srcRow, fPng_rowbytes); |
| 687 | } |
| 688 | if (fInterlacedComplete) { |
| 689 | return SkCodec::kSuccess; |
| 690 | } |
| 691 | |
| 692 | if (rowsDecoded) { |
| 693 | *rowsDecoded = fLinesDecoded; |
| 694 | } |
| 695 | |
| 696 | return SkCodec::kIncompleteInput; |
| 697 | } |
| 698 | |
| 699 | void setRange(int firstRow, int lastRow, void* dst, size_t rowBytes) override { |
| 700 | // FIXME: We could skip rows in the interlace buffer that we won't put in the output. |
| 701 | this->setUpInterlaceBuffer(lastRow - firstRow + 1); |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 702 | png_set_progressive_read_fn(this->png_ptr(), this, nullptr, InterlacedRowCallback, nullptr); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 703 | fFirstRow = firstRow; |
| 704 | fLastRow = lastRow; |
| 705 | fDst = dst; |
| 706 | fRowBytes = rowBytes; |
| 707 | fLinesDecoded = 0; |
| 708 | } |
| 709 | |
| 710 | SkCodec::Result decode(int* rowsDecoded) override { |
nagarajan.n | dd7ffa5 | 2017-09-29 08:23:32 +0530 | [diff] [blame] | 711 | if (this->processData() == false) { |
| 712 | return kErrorInInput; |
| 713 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 714 | |
| 715 | // Now apply Xforms on all the rows that were decoded. |
| 716 | if (!fLinesDecoded) { |
scroggo | e61b3b4 | 2016-10-10 07:17:32 -0700 | [diff] [blame] | 717 | if (rowsDecoded) { |
| 718 | *rowsDecoded = 0; |
| 719 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 720 | return SkCodec::kIncompleteInput; |
| 721 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 722 | |
scroggo | 4f2a88c | 2016-10-17 14:32:41 -0700 | [diff] [blame] | 723 | const int sampleY = this->swizzler() ? this->swizzler()->sampleY() : 1; |
| 724 | const int rowsNeeded = get_scaled_dimension(fLastRow - fFirstRow + 1, sampleY); |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 725 | int rowsWrittenToOutput = 0; |
| 726 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 727 | // FIXME: For resuming interlace, we may swizzle a row that hasn't changed. But it |
| 728 | // may be too tricky/expensive to handle that correctly. |
scroggo | e9ea349 | 2016-10-18 09:14:11 -0700 | [diff] [blame] | 729 | |
| 730 | // Offset srcRow by get_start_coord rows. We do not need to account for fFirstRow, |
| 731 | // since the first row in fInterlaceBuffer corresponds to fFirstRow. |
| 732 | png_bytep srcRow = SkTAddOffset<png_byte>(fInterlaceBuffer.get(), |
| 733 | fPng_rowbytes * get_start_coord(sampleY)); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 734 | void* dst = fDst; |
scroggo | e9ea349 | 2016-10-18 09:14:11 -0700 | [diff] [blame] | 735 | for (; rowsWrittenToOutput < rowsNeeded; rowsWrittenToOutput++) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 736 | this->applyXformRow(dst, srcRow); |
| 737 | dst = SkTAddOffset<void>(dst, fRowBytes); |
| 738 | srcRow = SkTAddOffset<png_byte>(srcRow, fPng_rowbytes * sampleY); |
| 739 | } |
| 740 | |
| 741 | if (fInterlacedComplete) { |
| 742 | return SkCodec::kSuccess; |
| 743 | } |
| 744 | |
| 745 | if (rowsDecoded) { |
scroggo | ff9f7bb | 2016-10-10 11:35:01 -0700 | [diff] [blame] | 746 | *rowsDecoded = rowsWrittenToOutput; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 747 | } |
| 748 | return SkCodec::kIncompleteInput; |
| 749 | } |
| 750 | |
| 751 | void setUpInterlaceBuffer(int height) { |
| 752 | fPng_rowbytes = png_get_rowbytes(this->png_ptr(), this->info_ptr()); |
| 753 | fInterlaceBuffer.reset(fPng_rowbytes * height); |
| 754 | fInterlacedComplete = false; |
| 755 | } |
| 756 | }; |
| 757 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 758 | // Reads the header and initializes the output fields, if not NULL. |
| 759 | // |
| 760 | // @param stream Input data. Will be read to get enough information to properly |
| 761 | // setup the codec. |
| 762 | // @param chunkReader SkPngChunkReader, for reading unknown chunks. May be NULL. |
| 763 | // If not NULL, png_ptr will hold an *unowned* pointer to it. The caller is |
| 764 | // expected to continue to own it for the lifetime of the png_ptr. |
| 765 | // @param outCodec Optional output variable. If non-NULL, will be set to a new |
| 766 | // SkPngCodec on success. |
| 767 | // @param png_ptrp Optional output variable. If non-NULL, will be set to a new |
| 768 | // png_structp on success. |
| 769 | // @param info_ptrp Optional output variable. If non-NULL, will be set to a new |
| 770 | // png_infop on success; |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 771 | // @return if kSuccess, the caller is responsible for calling |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 772 | // png_destroy_read_struct(png_ptrp, info_ptrp). |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 773 | // Otherwise, the passed in fields (except stream) are unchanged. |
| 774 | static SkCodec::Result read_header(SkStream* stream, SkPngChunkReader* chunkReader, |
| 775 | SkCodec** outCodec, |
| 776 | png_structp* png_ptrp, png_infop* info_ptrp) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 777 | // The image is known to be a PNG. Decode enough to know the SkImageInfo. |
| 778 | png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, |
| 779 | sk_error_fn, sk_warning_fn); |
| 780 | if (!png_ptr) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 781 | return SkCodec::kInternalError; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Leon Scroggins III | 9b1a886 | 2018-03-01 15:57:32 -0500 | [diff] [blame] | 784 | #ifdef PNG_SET_OPTION_SUPPORTED |
Leon Scroggins III | 9e8a594 | 2018-02-28 16:24:18 -0500 | [diff] [blame] | 785 | // This setting ensures that we display images with incorrect CMF bytes. |
| 786 | // See crbug.com/807324. |
| 787 | png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW, PNG_OPTION_ON); |
Leon Scroggins III | 9b1a886 | 2018-03-01 15:57:32 -0500 | [diff] [blame] | 788 | #endif |
Leon Scroggins III | 9e8a594 | 2018-02-28 16:24:18 -0500 | [diff] [blame] | 789 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 790 | AutoCleanPng autoClean(png_ptr, stream, chunkReader, outCodec); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 791 | |
| 792 | png_infop info_ptr = png_create_info_struct(png_ptr); |
| 793 | if (info_ptr == nullptr) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 794 | return SkCodec::kInternalError; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | autoClean.setInfoPtr(info_ptr); |
| 798 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 799 | if (setjmp(PNG_JMPBUF(png_ptr))) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 800 | return SkCodec::kInvalidInput; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 801 | } |
| 802 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 803 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 804 | // Hookup our chunkReader so we can see any user-chunks the caller may be interested in. |
| 805 | // This needs to be installed before we read the png header. Android may store ninepatch |
| 806 | // chunks in the header. |
| 807 | if (chunkReader) { |
| 808 | png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"", 0); |
| 809 | png_set_read_user_chunk_fn(png_ptr, (png_voidp) chunkReader, sk_read_user_chunk); |
| 810 | } |
| 811 | #endif |
| 812 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 813 | const bool decodedBounds = autoClean.decodeBounds(); |
| 814 | |
| 815 | if (!decodedBounds) { |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 816 | return SkCodec::kIncompleteInput; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | // On success, decodeBounds releases ownership of png_ptr and info_ptr. |
| 820 | if (png_ptrp) { |
| 821 | *png_ptrp = png_ptr; |
| 822 | } |
| 823 | if (info_ptrp) { |
| 824 | *info_ptrp = info_ptr; |
| 825 | } |
| 826 | |
| 827 | // decodeBounds takes care of setting outCodec |
| 828 | if (outCodec) { |
| 829 | SkASSERT(*outCodec); |
| 830 | } |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 831 | return SkCodec::kSuccess; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 834 | void AutoCleanPng::infoCallback(size_t idatLength) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 835 | png_uint_32 origWidth, origHeight; |
| 836 | int bitDepth, encodedColorType; |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 837 | png_get_IHDR(fPng_ptr, fInfo_ptr, &origWidth, &origHeight, &bitDepth, |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 838 | &encodedColorType, nullptr, nullptr, nullptr); |
| 839 | |
Matt Sarett | 7a1cc67 | 2016-12-14 11:48:31 -0500 | [diff] [blame] | 840 | // TODO: Should we support 16-bits of precision for gray images? |
| 841 | if (bitDepth == 16 && (PNG_COLOR_TYPE_GRAY == encodedColorType || |
| 842 | PNG_COLOR_TYPE_GRAY_ALPHA == encodedColorType)) { |
| 843 | bitDepth = 8; |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 844 | png_set_strip_16(fPng_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | // Now determine the default colorType and alphaType and set the required transforms. |
| 848 | // Often, we depend on SkSwizzler to perform any transforms that we need. However, we |
| 849 | // still depend on libpng for many of the rare and PNG-specific cases. |
| 850 | SkEncodedInfo::Color color; |
| 851 | SkEncodedInfo::Alpha alpha; |
| 852 | switch (encodedColorType) { |
| 853 | case PNG_COLOR_TYPE_PALETTE: |
| 854 | // Extract multiple pixels with bit depths of 1, 2, and 4 from a single |
| 855 | // byte into separate bytes (useful for paletted and grayscale images). |
| 856 | if (bitDepth < 8) { |
| 857 | // TODO: Should we use SkSwizzler here? |
Matt Sarett | 7a1cc67 | 2016-12-14 11:48:31 -0500 | [diff] [blame] | 858 | bitDepth = 8; |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 859 | png_set_packing(fPng_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | color = SkEncodedInfo::kPalette_Color; |
| 863 | // Set the alpha depending on if a transparency chunk exists. |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 864 | alpha = png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS) ? |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 865 | SkEncodedInfo::kUnpremul_Alpha : SkEncodedInfo::kOpaque_Alpha; |
| 866 | break; |
| 867 | case PNG_COLOR_TYPE_RGB: |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 868 | if (png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS)) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 869 | // Convert to RGBA if transparency chunk exists. |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 870 | png_set_tRNS_to_alpha(fPng_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 871 | color = SkEncodedInfo::kRGBA_Color; |
| 872 | alpha = SkEncodedInfo::kBinary_Alpha; |
| 873 | } else { |
| 874 | color = SkEncodedInfo::kRGB_Color; |
| 875 | alpha = SkEncodedInfo::kOpaque_Alpha; |
| 876 | } |
| 877 | break; |
| 878 | case PNG_COLOR_TYPE_GRAY: |
| 879 | // Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel. |
| 880 | if (bitDepth < 8) { |
| 881 | // TODO: Should we use SkSwizzler here? |
Matt Sarett | 7a1cc67 | 2016-12-14 11:48:31 -0500 | [diff] [blame] | 882 | bitDepth = 8; |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 883 | png_set_expand_gray_1_2_4_to_8(fPng_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 886 | if (png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS)) { |
| 887 | png_set_tRNS_to_alpha(fPng_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 888 | color = SkEncodedInfo::kGrayAlpha_Color; |
| 889 | alpha = SkEncodedInfo::kBinary_Alpha; |
| 890 | } else { |
| 891 | color = SkEncodedInfo::kGray_Color; |
| 892 | alpha = SkEncodedInfo::kOpaque_Alpha; |
| 893 | } |
| 894 | break; |
| 895 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 896 | color = SkEncodedInfo::kGrayAlpha_Color; |
| 897 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 898 | break; |
| 899 | case PNG_COLOR_TYPE_RGBA: |
| 900 | color = SkEncodedInfo::kRGBA_Color; |
| 901 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 902 | break; |
| 903 | default: |
| 904 | // All the color types have been covered above. |
| 905 | SkASSERT(false); |
| 906 | color = SkEncodedInfo::kRGBA_Color; |
| 907 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 908 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 909 | |
| 910 | const int numberPasses = png_set_interlace_handling(fPng_ptr); |
| 911 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 912 | if (fOutCodec) { |
| 913 | SkASSERT(nullptr == *fOutCodec); |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 914 | auto profile = read_color_profile(fPng_ptr, fInfo_ptr); |
| 915 | if (profile) { |
| 916 | switch (profile->profile()->data_color_space) { |
| 917 | case skcms_Signature_CMYK: |
| 918 | profile = nullptr; |
Leon Scroggins III | f78b55c | 2017-10-31 13:49:14 -0400 | [diff] [blame] | 919 | break; |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 920 | case skcms_Signature_Gray: |
Leon Scroggins III | f78b55c | 2017-10-31 13:49:14 -0400 | [diff] [blame] | 921 | if (SkEncodedInfo::kGray_Color != color && |
| 922 | SkEncodedInfo::kGrayAlpha_Color != color) |
| 923 | { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 924 | profile = nullptr; |
Leon Scroggins III | f78b55c | 2017-10-31 13:49:14 -0400 | [diff] [blame] | 925 | } |
| 926 | break; |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 927 | default: |
Leon Scroggins III | f78b55c | 2017-10-31 13:49:14 -0400 | [diff] [blame] | 928 | break; |
| 929 | } |
Matt Sarett | 523116d | 2017-01-12 18:36:38 -0500 | [diff] [blame] | 930 | } |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 931 | if (!profile) { |
raftias | d737bee | 2016-12-08 10:53:24 -0500 | [diff] [blame] | 932 | // Treat unsupported/invalid color spaces as sRGB. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 933 | profile = SkEncodedInfo::ICCProfile::MakeSRGB(); |
msarett | f34cd63 | 2016-05-25 10:13:53 -0700 | [diff] [blame] | 934 | } |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 935 | |
Mike Reed | d6cb11e | 2017-11-30 15:33:04 -0500 | [diff] [blame] | 936 | if (encodedColorType == PNG_COLOR_TYPE_GRAY_ALPHA) { |
| 937 | png_color_8p sigBits; |
| 938 | if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) { |
| 939 | if (8 == sigBits->alpha && kGraySigBit_GrayAlphaIsJustAlpha == sigBits->gray) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 940 | color = SkEncodedInfo::kXAlpha_Color; |
Mike Reed | d6cb11e | 2017-11-30 15:33:04 -0500 | [diff] [blame] | 941 | } |
| 942 | } |
| 943 | } else if (SkEncodedInfo::kOpaque_Alpha == alpha) { |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 944 | png_color_8p sigBits; |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 945 | if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) { |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 946 | if (5 == sigBits->red && 6 == sigBits->green && 5 == sigBits->blue) { |
| 947 | // Recommend a decode to 565 if the sBIT indicates 565. |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 948 | color = SkEncodedInfo::k565_Color; |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | } |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 952 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 953 | SkEncodedInfo encodedInfo = SkEncodedInfo::Make(origWidth, origHeight, color, alpha, |
| 954 | bitDepth, std::move(profile)); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 955 | if (1 == numberPasses) { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 956 | *fOutCodec = new SkPngNormalDecoder(std::move(encodedInfo), |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 957 | std::unique_ptr<SkStream>(fStream), fChunkReader, fPng_ptr, fInfo_ptr, bitDepth); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 958 | } else { |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 959 | *fOutCodec = new SkPngInterlacedDecoder(std::move(encodedInfo), |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 960 | std::unique_ptr<SkStream>(fStream), fChunkReader, fPng_ptr, fInfo_ptr, bitDepth, |
| 961 | numberPasses); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 962 | } |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 963 | static_cast<SkPngCodec*>(*fOutCodec)->setIdatLength(idatLength); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 964 | } |
| 965 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 966 | // Release the pointers, which are now owned by the codec or the caller is expected to |
| 967 | // take ownership. |
| 968 | this->releasePngPtrs(); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 969 | } |
| 970 | |
Leon Scroggins III | 36f7e32 | 2018-08-27 11:55:46 -0400 | [diff] [blame] | 971 | SkPngCodec::SkPngCodec(SkEncodedInfo&& encodedInfo, std::unique_ptr<SkStream> stream, |
| 972 | SkPngChunkReader* chunkReader, void* png_ptr, void* info_ptr, int bitDepth) |
| 973 | : INHERITED(std::move(encodedInfo), png_select_xform_format(encodedInfo), std::move(stream)) |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 974 | , fPngChunkReader(SkSafeRef(chunkReader)) |
| 975 | , fPng_ptr(png_ptr) |
| 976 | , fInfo_ptr(info_ptr) |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 977 | , fColorXformSrcRow(nullptr) |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 978 | , fBitDepth(bitDepth) |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 979 | , fIdatLength(0) |
| 980 | , fDecodedIdat(false) |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 981 | {} |
| 982 | |
| 983 | SkPngCodec::~SkPngCodec() { |
| 984 | this->destroyReadStruct(); |
| 985 | } |
| 986 | |
| 987 | void SkPngCodec::destroyReadStruct() { |
| 988 | if (fPng_ptr) { |
| 989 | // We will never have a nullptr fInfo_ptr with a non-nullptr fPng_ptr |
| 990 | SkASSERT(fInfo_ptr); |
mtklein | 6dc5b9a | 2016-08-24 12:22:32 -0700 | [diff] [blame] | 991 | png_destroy_read_struct((png_struct**)&fPng_ptr, (png_info**)&fInfo_ptr, nullptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 992 | fPng_ptr = nullptr; |
| 993 | fInfo_ptr = nullptr; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | /////////////////////////////////////////////////////////////////////////////// |
| 998 | // Getting the pixels |
| 999 | /////////////////////////////////////////////////////////////////////////////// |
| 1000 | |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 1001 | SkCodec::Result SkPngCodec::initializeXforms(const SkImageInfo& dstInfo, const Options& options) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1002 | if (setjmp(PNG_JMPBUF((png_struct*)fPng_ptr))) { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 1003 | SkCodecPrintf("Failed on png_read_update_info.\n"); |
Matt Sarett | d59948a | 2017-04-26 10:59:48 -0400 | [diff] [blame] | 1004 | return kInvalidInput; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1005 | } |
| 1006 | png_read_update_info(fPng_ptr, fInfo_ptr); |
| 1007 | |
Matt Sarett | 313c463 | 2016-10-20 12:35:23 -0400 | [diff] [blame] | 1008 | // Reset fSwizzler and this->colorXform(). We can't do this in onRewind() because the |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 1009 | // interlaced scanline decoder may need to rewind. |
| 1010 | fSwizzler.reset(nullptr); |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 1011 | |
Matt Sarett | 34c69d6 | 2017-01-19 17:42:23 -0500 | [diff] [blame] | 1012 | // If SkColorSpaceXform directly supports the encoded PNG format, we should skip format |
| 1013 | // conversion in the swizzler (or skip swizzling altogether). |
| 1014 | bool skipFormatConversion = false; |
| 1015 | switch (this->getEncodedInfo().color()) { |
| 1016 | case SkEncodedInfo::kRGB_Color: |
| 1017 | if (this->getEncodedInfo().bitsPerComponent() != 16) { |
| 1018 | break; |
| 1019 | } |
| 1020 | |
| 1021 | // Fall through |
| 1022 | case SkEncodedInfo::kRGBA_Color: |
| 1023 | skipFormatConversion = this->colorXform(); |
| 1024 | break; |
| 1025 | default: |
| 1026 | break; |
| 1027 | } |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 1028 | if (skipFormatConversion && !options.fSubset) { |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 1029 | fXformMode = kColorOnly_XformMode; |
Matt Sarett | d59948a | 2017-04-26 10:59:48 -0400 | [diff] [blame] | 1030 | return kSuccess; |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1033 | if (SkEncodedInfo::kPalette_Color == this->getEncodedInfo().color()) { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 1034 | if (!this->createColorTable(dstInfo)) { |
Matt Sarett | d59948a | 2017-04-26 10:59:48 -0400 | [diff] [blame] | 1035 | return kInvalidInput; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 1039 | this->initializeSwizzler(dstInfo, options, skipFormatConversion); |
Matt Sarett | d59948a | 2017-04-26 10:59:48 -0400 | [diff] [blame] | 1040 | return kSuccess; |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 1043 | void SkPngCodec::initializeXformParams() { |
| 1044 | switch (fXformMode) { |
| 1045 | case kColorOnly_XformMode: |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 1046 | fXformWidth = this->dstInfo().width(); |
| 1047 | break; |
| 1048 | case kSwizzleColor_XformMode: |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 1049 | fXformWidth = this->swizzler()->swizzleWidth(); |
| 1050 | break; |
| 1051 | default: |
| 1052 | break; |
| 1053 | } |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 1056 | void SkPngCodec::initializeSwizzler(const SkImageInfo& dstInfo, const Options& options, |
| 1057 | bool skipFormatConversion) { |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 1058 | SkImageInfo swizzlerInfo = dstInfo; |
| 1059 | Options swizzlerOptions = options; |
| 1060 | fXformMode = kSwizzleOnly_XformMode; |
Leon Scroggins III | c6e6a5f | 2017-06-05 15:53:38 -0400 | [diff] [blame] | 1061 | if (this->colorXform() && this->xformOnDecode()) { |
Matt Sarett | 562e681 | 2016-11-08 16:13:43 -0500 | [diff] [blame] | 1062 | swizzlerInfo = swizzlerInfo.makeColorType(kXformSrcColorType); |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 1063 | if (kPremul_SkAlphaType == dstInfo.alphaType()) { |
| 1064 | swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType); |
| 1065 | } |
| 1066 | |
| 1067 | fXformMode = kSwizzleColor_XformMode; |
| 1068 | |
| 1069 | // Here, we swizzle into temporary memory, which is not zero initialized. |
| 1070 | // FIXME (msarett): |
| 1071 | // Is this a problem? |
| 1072 | swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized; |
| 1073 | } |
| 1074 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1075 | const SkPMColor* colors = get_color_ptr(fColorTable.get()); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 1076 | fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), colors, swizzlerInfo, |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 1077 | swizzlerOptions, nullptr, skipFormatConversion)); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1078 | SkASSERT(fSwizzler); |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | SkSampler* SkPngCodec::getSampler(bool createIfNecessary) { |
| 1082 | if (fSwizzler || !createIfNecessary) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1083 | return fSwizzler.get(); |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Matt Sarett | 379938e | 2017-01-12 18:34:29 -0500 | [diff] [blame] | 1086 | this->initializeSwizzler(this->dstInfo(), this->options(), true); |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 1087 | return fSwizzler.get(); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1090 | bool SkPngCodec::onRewind() { |
| 1091 | // This sets fPng_ptr and fInfo_ptr to nullptr. If read_header |
| 1092 | // succeeds, they will be repopulated, and if it fails, they will |
| 1093 | // remain nullptr. Any future accesses to fPng_ptr and fInfo_ptr will |
| 1094 | // come through this function which will rewind and again attempt |
| 1095 | // to reinitialize them. |
| 1096 | this->destroyReadStruct(); |
| 1097 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 1098 | png_structp png_ptr; |
| 1099 | png_infop info_ptr; |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 1100 | if (kSuccess != read_header(this->stream(), fPngChunkReader.get(), nullptr, |
| 1101 | &png_ptr, &info_ptr)) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1102 | return false; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1105 | fPng_ptr = png_ptr; |
| 1106 | fInfo_ptr = info_ptr; |
Leon Scroggins III | 8323965 | 2017-04-21 13:47:12 -0400 | [diff] [blame] | 1107 | fDecodedIdat = false; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1108 | return true; |
| 1109 | } |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 1110 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 1111 | SkCodec::Result SkPngCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, |
| 1112 | size_t rowBytes, const Options& options, |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1113 | int* rowsDecoded) { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 1114 | Result result = this->initializeXforms(dstInfo, options); |
Matt Sarett | d59948a | 2017-04-26 10:59:48 -0400 | [diff] [blame] | 1115 | if (kSuccess != result) { |
| 1116 | return result; |
| 1117 | } |
| 1118 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1119 | if (options.fSubset) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1120 | return kUnimplemented; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
msarett | 400a93b | 2016-09-01 18:32:52 -0700 | [diff] [blame] | 1123 | this->allocateStorage(dstInfo); |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 1124 | this->initializeXformParams(); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1125 | return this->decodeAllRows(dst, rowBytes, rowsDecoded); |
| 1126 | } |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1127 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1128 | SkCodec::Result SkPngCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo, |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 1129 | void* dst, size_t rowBytes, const SkCodec::Options& options) { |
Leon Scroggins | 571b30f | 2017-07-11 17:35:31 +0000 | [diff] [blame] | 1130 | Result result = this->initializeXforms(dstInfo, options); |
Matt Sarett | d59948a | 2017-04-26 10:59:48 -0400 | [diff] [blame] | 1131 | if (kSuccess != result) { |
| 1132 | return result; |
| 1133 | } |
| 1134 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1135 | this->allocateStorage(dstInfo); |
| 1136 | |
| 1137 | int firstRow, lastRow; |
| 1138 | if (options.fSubset) { |
| 1139 | firstRow = options.fSubset->top(); |
| 1140 | lastRow = options.fSubset->bottom() - 1; |
| 1141 | } else { |
| 1142 | firstRow = 0; |
| 1143 | lastRow = dstInfo.height() - 1; |
| 1144 | } |
| 1145 | this->setRange(firstRow, lastRow, dst, rowBytes); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 1146 | return kSuccess; |
scroggo | 6fb2391 | 2016-06-02 14:16:43 -0700 | [diff] [blame] | 1147 | } |
| 1148 | |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1149 | SkCodec::Result SkPngCodec::onIncrementalDecode(int* rowsDecoded) { |
| 1150 | // FIXME: Only necessary on the first call. |
msarett | c044461 | 2016-09-16 11:45:58 -0700 | [diff] [blame] | 1151 | this->initializeXformParams(); |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1152 | |
| 1153 | return this->decode(rowsDecoded); |
| 1154 | } |
| 1155 | |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 1156 | std::unique_ptr<SkCodec> SkPngCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 1157 | Result* result, SkPngChunkReader* chunkReader) { |
scroggo | 8e6c7ad | 2016-09-16 08:20:38 -0700 | [diff] [blame] | 1158 | SkCodec* outCodec = nullptr; |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 1159 | *result = read_header(stream.get(), chunkReader, &outCodec, nullptr, nullptr); |
Leon Scroggins III | 588fb04 | 2017-07-14 16:32:31 -0400 | [diff] [blame] | 1160 | if (kSuccess == *result) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1161 | // Codec has taken ownership of the stream. |
| 1162 | SkASSERT(outCodec); |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 1163 | stream.release(); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 1164 | } |
Mike Reed | ede7bac | 2017-07-23 15:30:02 -0400 | [diff] [blame] | 1165 | return std::unique_ptr<SkCodec>(outCodec); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 1166 | } |