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