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