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