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 | |
| 8 | #include "SkCodec_libpng.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" |
| 11 | #include "SkColorTable.h" |
| 12 | #include "SkBitmap.h" |
| 13 | #include "SkMath.h" |
| 14 | #include "SkSize.h" |
| 15 | #include "SkStream.h" |
| 16 | #include "SkSwizzler.h" |
| 17 | |
| 18 | /////////////////////////////////////////////////////////////////////////////// |
| 19 | // Helper macros |
| 20 | /////////////////////////////////////////////////////////////////////////////// |
| 21 | |
| 22 | #ifndef png_jmpbuf |
| 23 | # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) |
| 24 | #endif |
| 25 | |
| 26 | /* These were dropped in libpng >= 1.4 */ |
| 27 | #ifndef png_infopp_NULL |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | #define png_infopp_NULL nullptr |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 29 | #endif |
| 30 | |
| 31 | #ifndef png_bytepp_NULL |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 32 | #define png_bytepp_NULL nullptr |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | #ifndef int_p_NULL |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | #define int_p_NULL nullptr |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | #ifndef png_flush_ptr_NULL |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 40 | #define png_flush_ptr_NULL nullptr |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | // Callback functions |
| 45 | /////////////////////////////////////////////////////////////////////////////// |
| 46 | |
| 47 | static void sk_error_fn(png_structp png_ptr, png_const_charp msg) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 48 | SkCodecPrintf("------ png error %s\n", msg); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 49 | longjmp(png_jmpbuf(png_ptr), 1); |
| 50 | } |
| 51 | |
scroggo | 0eed6df | 2015-03-26 10:07:56 -0700 | [diff] [blame] | 52 | void sk_warning_fn(png_structp, png_const_charp msg) { |
| 53 | SkCodecPrintf("----- png warning %s\n", msg); |
| 54 | } |
| 55 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 56 | static void sk_read_fn(png_structp png_ptr, png_bytep data, |
| 57 | png_size_t length) { |
| 58 | SkStream* stream = static_cast<SkStream*>(png_get_io_ptr(png_ptr)); |
| 59 | const size_t bytes = stream->read(data, length); |
| 60 | if (bytes != length) { |
| 61 | // FIXME: We want to report the fact that the stream was truncated. |
| 62 | // One way to do that might be to pass a enum to longjmp so setjmp can |
| 63 | // specify the failure. |
| 64 | png_error(png_ptr, "Read Error!"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /////////////////////////////////////////////////////////////////////////////// |
| 69 | // Helpers |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | class AutoCleanPng : public SkNoncopyable { |
| 73 | public: |
| 74 | AutoCleanPng(png_structp png_ptr) |
| 75 | : fPng_ptr(png_ptr) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 76 | , fInfo_ptr(nullptr) {} |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 77 | |
| 78 | ~AutoCleanPng() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 79 | // fInfo_ptr will never be non-nullptr unless fPng_ptr is. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 80 | if (fPng_ptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | png_infopp info_pp = fInfo_ptr ? &fInfo_ptr : nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 82 | png_destroy_read_struct(&fPng_ptr, info_pp, png_infopp_NULL); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void setInfoPtr(png_infop info_ptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 87 | SkASSERT(nullptr == fInfo_ptr); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 88 | fInfo_ptr = info_ptr; |
| 89 | } |
| 90 | |
| 91 | void detach() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 92 | fPng_ptr = nullptr; |
| 93 | fInfo_ptr = nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private: |
| 97 | png_structp fPng_ptr; |
| 98 | png_infop fInfo_ptr; |
| 99 | }; |
| 100 | #define AutoCleanPng(...) SK_REQUIRE_LOCAL_VAR(AutoCleanPng) |
| 101 | |
emmaleer | 21cea72 | 2015-07-10 07:48:03 -0700 | [diff] [blame] | 102 | //checks if there is transparency info in the tRNS chunk |
| 103 | //image types which could have data in the tRNS chunk include: Index8, Gray8, RGB |
| 104 | static bool has_transparency_in_tRNS(png_structp png_ptr, |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 105 | png_infop info_ptr) { |
| 106 | if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | png_bytep trans; |
| 111 | int num_trans; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, nullptr); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 113 | return num_trans > 0; |
| 114 | } |
| 115 | |
| 116 | // Method for coverting to either an SkPMColor or a similarly packed |
| 117 | // unpremultiplied color. |
| 118 | typedef uint32_t (*PackColorProc)(U8CPU a, U8CPU r, U8CPU g, U8CPU b); |
| 119 | |
| 120 | // Note: SkColorTable claims to store SkPMColors, which is not necessarily |
| 121 | // the case here. |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 122 | bool SkPngCodec::decodePalette(bool premultiply, int* ctableCount) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 123 | int numPalette; |
| 124 | png_colorp palette; |
| 125 | png_bytep trans; |
| 126 | |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 127 | if (!png_get_PLTE(fPng_ptr, fInfo_ptr, &palette, &numPalette)) { |
| 128 | return false; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 129 | } |
| 130 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 131 | // Note: These are not necessarily SkPMColors |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 132 | SkPMColor colorStorage[256]; // worst-case storage |
| 133 | SkPMColor* colorPtr = colorStorage; |
| 134 | |
| 135 | int numTrans; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 136 | if (png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | png_get_tRNS(fPng_ptr, fInfo_ptr, &trans, &numTrans, nullptr); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 138 | } else { |
| 139 | numTrans = 0; |
| 140 | } |
| 141 | |
| 142 | // check for bad images that might make us crash |
| 143 | if (numTrans > numPalette) { |
| 144 | numTrans = numPalette; |
| 145 | } |
| 146 | |
| 147 | int index = 0; |
| 148 | int transLessThanFF = 0; |
| 149 | |
| 150 | // Choose which function to use to create the color table. If the final destination's |
| 151 | // colortype is unpremultiplied, the color table will store unpremultiplied colors. |
| 152 | PackColorProc proc; |
| 153 | if (premultiply) { |
| 154 | proc = &SkPreMultiplyARGB; |
| 155 | } else { |
| 156 | proc = &SkPackARGB32NoCheck; |
| 157 | } |
| 158 | for (; index < numTrans; index++) { |
| 159 | transLessThanFF |= (int)*trans - 0xFF; |
| 160 | *colorPtr++ = proc(*trans++, palette->red, palette->green, palette->blue); |
| 161 | palette++; |
| 162 | } |
| 163 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 164 | if (transLessThanFF >= 0) { |
| 165 | // No transparent colors were found. |
| 166 | fAlphaState = kOpaque_AlphaState; |
| 167 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 168 | |
| 169 | for (; index < numPalette; index++) { |
| 170 | *colorPtr++ = SkPackARGB32(0xFF, palette->red, palette->green, palette->blue); |
| 171 | palette++; |
| 172 | } |
| 173 | |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 174 | /* BUGGY IMAGE WORKAROUND |
| 175 | Invalid images could contain pixel values that are greater than the number of palette |
| 176 | entries. Since we use pixel values as indices into the palette this could result in reading |
| 177 | beyond the end of the palette which could leak the contents of uninitialized memory. To |
| 178 | ensure this doesn't happen, we grow the colortable to the maximum size that can be |
| 179 | addressed by the bitdepth of the image and fill it with the last palette color or black if |
| 180 | the palette is empty (really broken image). |
| 181 | */ |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 182 | int colorCount = SkTMax(numPalette, 1 << SkTMin(fBitDepth, 8)); |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 183 | SkPMColor lastColor = index > 0 ? colorPtr[-1] : SkPackARGB32(0xFF, 0, 0, 0); |
| 184 | for (; index < colorCount; index++) { |
| 185 | *colorPtr++ = lastColor; |
| 186 | } |
| 187 | |
| 188 | // Set the new color count |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 189 | if (ctableCount != nullptr) { |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 190 | *ctableCount = colorCount; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 191 | } |
| 192 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 193 | fColorTable.reset(new SkColorTable(colorStorage, colorCount)); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 194 | return true; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /////////////////////////////////////////////////////////////////////////////// |
| 198 | // Creation |
| 199 | /////////////////////////////////////////////////////////////////////////////// |
| 200 | |
| 201 | #define PNG_BYTES_TO_CHECK 4 |
| 202 | |
| 203 | bool SkPngCodec::IsPng(SkStream* stream) { |
| 204 | char buf[PNG_BYTES_TO_CHECK]; |
| 205 | if (stream->read(buf, PNG_BYTES_TO_CHECK) != PNG_BYTES_TO_CHECK) { |
| 206 | return false; |
| 207 | } |
| 208 | if (png_sig_cmp((png_bytep) buf, (png_size_t)0, PNG_BYTES_TO_CHECK)) { |
| 209 | return false; |
| 210 | } |
| 211 | return true; |
| 212 | } |
| 213 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 214 | // Reads the header, and initializes the passed in fields, if not nullptr (except |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 215 | // stream, which is passed to the read function). |
| 216 | // Returns true on success, in which case the caller is responsible for calling |
| 217 | // png_destroy_read_struct. If it returns false, the passed in fields (except |
| 218 | // stream) are unchanged. |
| 219 | static bool read_header(SkStream* stream, png_structp* png_ptrp, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 220 | png_infop* info_ptrp, SkImageInfo* imageInfo, |
| 221 | int* bitDepthPtr, int* numberPassesPtr) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 222 | // The image is known to be a PNG. Decode enough to know the SkImageInfo. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 223 | png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, |
scroggo | 0eed6df | 2015-03-26 10:07:56 -0700 | [diff] [blame] | 224 | sk_error_fn, sk_warning_fn); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 225 | if (!png_ptr) { |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 226 | return false; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | AutoCleanPng autoClean(png_ptr); |
| 230 | |
| 231 | png_infop info_ptr = png_create_info_struct(png_ptr); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 232 | if (info_ptr == nullptr) { |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 233 | return false; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | autoClean.setInfoPtr(info_ptr); |
| 237 | |
| 238 | // FIXME: Could we use the return value of setjmp to specify the type of |
| 239 | // error? |
| 240 | if (setjmp(png_jmpbuf(png_ptr))) { |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 241 | return false; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | png_set_read_fn(png_ptr, static_cast<void*>(stream), sk_read_fn); |
| 245 | |
| 246 | // FIXME: This is where the old code hooks up the Peeker. Does it need to |
| 247 | // be set this early? (i.e. where are the user chunks? early in the stream, |
| 248 | // potentially?) |
| 249 | // If it does, we need to figure out a way to set it here. |
| 250 | |
| 251 | // The call to png_read_info() gives us all of the information from the |
| 252 | // PNG file before the first IDAT (image data chunk). |
| 253 | png_read_info(png_ptr, info_ptr); |
| 254 | png_uint_32 origWidth, origHeight; |
| 255 | int bitDepth, colorType; |
| 256 | png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth, |
| 257 | &colorType, int_p_NULL, int_p_NULL, int_p_NULL); |
| 258 | |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 259 | if (bitDepthPtr) { |
| 260 | *bitDepthPtr = bitDepth; |
| 261 | } |
| 262 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 263 | // sanity check for size |
| 264 | { |
| 265 | int64_t size = sk_64_mul(origWidth, origHeight); |
| 266 | // now check that if we are 4-bytes per pixel, we also don't overflow |
| 267 | if (size < 0 || size > (0x7FFFFFFF >> 2)) { |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 268 | return false; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | // Tell libpng to strip 16 bit/color files down to 8 bits/color |
| 273 | if (bitDepth == 16) { |
| 274 | png_set_strip_16(png_ptr); |
| 275 | } |
| 276 | #ifdef PNG_READ_PACK_SUPPORTED |
| 277 | // Extract multiple pixels with bit depths of 1, 2, and 4 from a single |
| 278 | // byte into separate bytes (useful for paletted and grayscale images). |
| 279 | if (bitDepth < 8) { |
| 280 | png_set_packing(png_ptr); |
| 281 | } |
| 282 | #endif |
| 283 | // Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel. |
| 284 | if (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8) { |
| 285 | png_set_expand_gray_1_2_4_to_8(png_ptr); |
| 286 | } |
| 287 | |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 288 | // Now determine the default SkColorType and SkAlphaType and set required transforms |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 289 | SkColorType skColorType = kUnknown_SkColorType; |
| 290 | SkAlphaType skAlphaType = kUnknown_SkAlphaType; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 291 | switch (colorType) { |
| 292 | case PNG_COLOR_TYPE_PALETTE: |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 293 | skColorType = kIndex_8_SkColorType; |
emmaleer | 21cea72 | 2015-07-10 07:48:03 -0700 | [diff] [blame] | 294 | skAlphaType = has_transparency_in_tRNS(png_ptr, info_ptr) ? |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 295 | kUnpremul_SkAlphaType : kOpaque_SkAlphaType; |
| 296 | break; |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 297 | case PNG_COLOR_TYPE_RGB: |
emmaleer | 21cea72 | 2015-07-10 07:48:03 -0700 | [diff] [blame] | 298 | if (has_transparency_in_tRNS(png_ptr, info_ptr)) { |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 299 | //convert to RGBA with tranparency information in tRNS chunk if it exists |
| 300 | png_set_tRNS_to_alpha(png_ptr); |
jvanverth | 6c90e09 | 2015-07-02 10:35:25 -0700 | [diff] [blame] | 301 | skAlphaType = kUnpremul_SkAlphaType; |
| 302 | } else { |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 303 | //convert to RGBA with Opaque Alpha |
| 304 | png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
jvanverth | 6c90e09 | 2015-07-02 10:35:25 -0700 | [diff] [blame] | 305 | skAlphaType = kOpaque_SkAlphaType; |
| 306 | } |
| 307 | skColorType = kN32_SkColorType; |
| 308 | break; |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 309 | case PNG_COLOR_TYPE_GRAY: |
emmaleer | 21cea72 | 2015-07-10 07:48:03 -0700 | [diff] [blame] | 310 | if (has_transparency_in_tRNS(png_ptr, info_ptr)) { |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 311 | //FIXME: support gray with alpha as a color type |
| 312 | //convert to RGBA if there is transparentcy info in the tRNS chunk |
| 313 | png_set_tRNS_to_alpha(png_ptr); |
| 314 | png_set_gray_to_rgb(png_ptr); |
| 315 | skColorType = kN32_SkColorType; |
| 316 | skAlphaType = kUnpremul_SkAlphaType; |
| 317 | } else { |
| 318 | skColorType = kGray_8_SkColorType; |
| 319 | skAlphaType = kOpaque_SkAlphaType; |
| 320 | } |
| 321 | break; |
| 322 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 323 | //FIXME: support gray with alpha as a color type |
| 324 | //convert to RGBA |
jvanverth | 6c90e09 | 2015-07-02 10:35:25 -0700 | [diff] [blame] | 325 | png_set_gray_to_rgb(png_ptr); |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 326 | skColorType = kN32_SkColorType; |
| 327 | skAlphaType = kUnpremul_SkAlphaType; |
| 328 | break; |
| 329 | case PNG_COLOR_TYPE_RGBA: |
| 330 | skColorType = kN32_SkColorType; |
| 331 | skAlphaType = kUnpremul_SkAlphaType; |
| 332 | break; |
| 333 | default: |
| 334 | //all the color types have been covered above |
| 335 | SkASSERT(false); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 336 | } |
| 337 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 338 | int numberPasses = png_set_interlace_handling(png_ptr); |
| 339 | if (numberPassesPtr) { |
| 340 | *numberPassesPtr = numberPasses; |
| 341 | } |
| 342 | |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame^] | 343 | // FIXME: Also need to check for sRGB ( https://bug.skia.org/3471 ). |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 344 | |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 345 | if (imageInfo) { |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 346 | *imageInfo = SkImageInfo::Make(origWidth, origHeight, skColorType, skAlphaType); |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 347 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 348 | autoClean.detach(); |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 349 | if (png_ptrp) { |
| 350 | *png_ptrp = png_ptr; |
| 351 | } |
| 352 | if (info_ptrp) { |
| 353 | *info_ptrp = info_ptr; |
| 354 | } |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 355 | |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 356 | return true; |
| 357 | } |
| 358 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 359 | SkPngCodec::SkPngCodec(const SkImageInfo& info, SkStream* stream, |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 360 | png_structp png_ptr, png_infop info_ptr, int bitDepth, int numberPasses) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 361 | : INHERITED(info, stream) |
| 362 | , fPng_ptr(png_ptr) |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 363 | , fInfo_ptr(info_ptr) |
| 364 | , fSrcConfig(SkSwizzler::kUnknown) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 365 | , fNumberPasses(numberPasses) |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 366 | , fBitDepth(bitDepth) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 367 | { |
| 368 | if (info.alphaType() == kOpaque_SkAlphaType) { |
| 369 | fAlphaState = kOpaque_AlphaState; |
| 370 | } else { |
| 371 | fAlphaState = kUnknown_AlphaState; |
| 372 | } |
| 373 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 374 | |
| 375 | SkPngCodec::~SkPngCodec() { |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 376 | this->destroyReadStruct(); |
| 377 | } |
| 378 | |
| 379 | void SkPngCodec::destroyReadStruct() { |
| 380 | if (fPng_ptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 381 | // We will never have a nullptr fInfo_ptr with a non-nullptr fPng_ptr |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 382 | SkASSERT(fInfo_ptr); |
| 383 | png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, png_infopp_NULL); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 384 | fPng_ptr = nullptr; |
| 385 | fInfo_ptr = nullptr; |
scroggo | 3eada2a | 2015-04-01 09:33:23 -0700 | [diff] [blame] | 386 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /////////////////////////////////////////////////////////////////////////////// |
| 390 | // Getting the pixels |
| 391 | /////////////////////////////////////////////////////////////////////////////// |
| 392 | |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 393 | SkCodec::Result SkPngCodec::initializeSwizzler(const SkImageInfo& requestedInfo, |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 394 | const Options& options, |
msarett | 9e43cab | 2015-04-29 07:38:43 -0700 | [diff] [blame] | 395 | SkPMColor ctable[], |
msarett | 438b2ad | 2015-04-09 12:43:10 -0700 | [diff] [blame] | 396 | int* ctableCount) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 397 | // FIXME: Could we use the return value of setjmp to specify the type of |
| 398 | // error? |
| 399 | if (setjmp(png_jmpbuf(fPng_ptr))) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 400 | SkCodecPrintf("setjmp long jump!\n"); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 401 | return kInvalidInput; |
| 402 | } |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 403 | png_read_update_info(fPng_ptr, fInfo_ptr); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 404 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 405 | //srcColorType was determined in read_header() which determined png color type |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 406 | const SkColorType srcColorType = this->getInfo().colorType(); |
| 407 | |
| 408 | switch (srcColorType) { |
| 409 | case kIndex_8_SkColorType: |
| 410 | //decode palette to Skia format |
| 411 | fSrcConfig = SkSwizzler::kIndex; |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 412 | if (!this->decodePalette(kPremul_SkAlphaType == requestedInfo.alphaType(), |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 413 | ctableCount)) { |
| 414 | return kInvalidInput; |
| 415 | } |
| 416 | break; |
| 417 | case kGray_8_SkColorType: |
| 418 | fSrcConfig = SkSwizzler::kGray; |
| 419 | break; |
| 420 | case kN32_SkColorType: |
| 421 | if (this->getInfo().alphaType() == kOpaque_SkAlphaType) { |
| 422 | fSrcConfig = SkSwizzler::kRGBX; |
| 423 | } else { |
| 424 | fSrcConfig = SkSwizzler::kRGBA; |
| 425 | } |
| 426 | break; |
| 427 | default: |
| 428 | //would have exited before now if the colorType was supported by png |
| 429 | SkASSERT(false); |
| 430 | } |
msarett | 9e43cab | 2015-04-29 07:38:43 -0700 | [diff] [blame] | 431 | |
| 432 | // Copy the color table to the client if they request kIndex8 mode |
| 433 | copy_color_table(requestedInfo, fColorTable, ctable, ctableCount); |
| 434 | |
| 435 | // Create the swizzler. SkPngCodec retains ownership of the color table. |
msarett | 99f567e | 2015-08-05 12:58:26 -0700 | [diff] [blame] | 436 | const SkPMColor* colors = get_color_ptr(fColorTable.get()); |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 437 | fSwizzler.reset(SkSwizzler::CreateSwizzler(fSrcConfig, colors, requestedInfo, options)); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 438 | if (!fSwizzler) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 439 | // FIXME: CreateSwizzler could fail for another reason. |
| 440 | return kUnimplemented; |
| 441 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 442 | return kSuccess; |
| 443 | } |
| 444 | |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 445 | |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 446 | bool SkPngCodec::onRewind() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 447 | // This sets fPng_ptr and fInfo_ptr to nullptr. If read_header |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 448 | // succeeds, they will be repopulated, and if it fails, they will |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 449 | // remain nullptr. Any future accesses to fPng_ptr and fInfo_ptr will |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 450 | // come through this function which will rewind and again attempt |
| 451 | // to reinitialize them. |
| 452 | this->destroyReadStruct(); |
| 453 | |
| 454 | png_structp png_ptr; |
| 455 | png_infop info_ptr; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 456 | if (!read_header(this->stream(), &png_ptr, &info_ptr, nullptr, nullptr, nullptr)) { |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 457 | return false; |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 458 | } |
scroggo | b427db1 | 2015-08-12 07:24:13 -0700 | [diff] [blame] | 459 | |
| 460 | fPng_ptr = png_ptr; |
| 461 | fInfo_ptr = info_ptr; |
| 462 | return true; |
scroggo | 5842154 | 2015-04-01 11:25:20 -0700 | [diff] [blame] | 463 | } |
| 464 | |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 465 | SkCodec::Result SkPngCodec::onGetPixels(const SkImageInfo& requestedInfo, void* dst, |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 466 | size_t dstRowBytes, const Options& options, |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 467 | SkPMColor ctable[], int* ctableCount, |
| 468 | int* rowsDecoded) { |
scroggo | 6f29a3c | 2015-07-07 06:09:08 -0700 | [diff] [blame] | 469 | if (!conversion_possible(requestedInfo, this->getInfo())) { |
| 470 | return kInvalidConversion; |
| 471 | } |
scroggo | b636b45 | 2015-07-22 07:16:20 -0700 | [diff] [blame] | 472 | if (options.fSubset) { |
| 473 | // Subsets are not supported. |
| 474 | return kUnimplemented; |
| 475 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 476 | |
msarett | 9e43cab | 2015-04-29 07:38:43 -0700 | [diff] [blame] | 477 | // Note that ctable and ctableCount may be modified if there is a color table |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 478 | const Result result = this->initializeSwizzler(requestedInfo, options, ctable, ctableCount); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 479 | if (result != kSuccess) { |
| 480 | return result; |
| 481 | } |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 482 | // FIXME: Could we use the return value of setjmp to specify the type of |
| 483 | // error? |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 484 | int row = 0; |
| 485 | // This must be declared above the call to setjmp to avoid memory leaks on incomplete images. |
| 486 | SkAutoMalloc storage; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 487 | if (setjmp(png_jmpbuf(fPng_ptr))) { |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 488 | // Assume that any error that occurs while reading rows is caused by an incomplete input. |
| 489 | if (fNumberPasses > 1) { |
| 490 | // FIXME (msarett): Handle incomplete interlaced pngs. |
| 491 | return kInvalidInput; |
| 492 | } |
| 493 | // FIXME: We do a poor job on incomplete pngs compared to other decoders (ex: Chromium, |
| 494 | // Ubuntu Image Viewer). This is because we use the default buffer size in libpng (8192 |
| 495 | // bytes), and if we can't fill the buffer, we immediately fail. |
| 496 | // For example, if we try to read 8192 bytes, and the image (incorrectly) only contains |
| 497 | // half that, which may have been enough to contain a non-zero number of lines, we fail |
| 498 | // when we could have decoded a few more lines and then failed. |
| 499 | // The read function that we provide for libpng has no way of indicating that we have |
| 500 | // made a partial read. |
| 501 | // Making our buffer size smaller improves our incomplete decodes, but what impact does |
| 502 | // it have on regular decode performance? Should we investigate using a different API |
| 503 | // instead of png_read_row(s)? Chromium uses png_process_data. |
| 504 | *rowsDecoded = row; |
| 505 | return kIncompleteInput; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 506 | } |
| 507 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 508 | bool hasAlpha = false; |
| 509 | // FIXME: We could split these out based on subclass. |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 510 | void* dstRow = dst; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 511 | if (fNumberPasses > 1) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 512 | const int width = requestedInfo.width(); |
| 513 | const int height = requestedInfo.height(); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 514 | const int bpp = SkSwizzler::BytesPerPixel(fSrcConfig); |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 515 | const size_t srcRowBytes = width * bpp; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 516 | |
| 517 | storage.reset(width * height * bpp); |
| 518 | uint8_t* const base = static_cast<uint8_t*>(storage.get()); |
| 519 | |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 520 | for (int i = 0; i < fNumberPasses; i++) { |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 521 | uint8_t* srcRow = base; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 522 | for (int y = 0; y < height; y++) { |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 523 | uint8_t* bmRow = srcRow; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 524 | png_read_rows(fPng_ptr, &bmRow, png_bytepp_NULL, 1); |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 525 | srcRow += srcRowBytes; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | |
| 529 | // Now swizzle it. |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 530 | uint8_t* srcRow = base; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 531 | for (int y = 0; y < height; y++) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 532 | hasAlpha |= !SkSwizzler::IsOpaque(fSwizzler->swizzle(dstRow, srcRow)); |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 533 | dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
| 534 | srcRow += srcRowBytes; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 535 | } |
| 536 | } else { |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 537 | storage.reset(requestedInfo.width() * SkSwizzler::BytesPerPixel(fSrcConfig)); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 538 | uint8_t* srcRow = static_cast<uint8_t*>(storage.get()); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 539 | for (; row < requestedInfo.height(); row++) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 540 | png_read_rows(fPng_ptr, &srcRow, png_bytepp_NULL, 1); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 541 | // FIXME: Only call IsOpaque once, outside the loop. Same for onGetScanlines. |
| 542 | hasAlpha |= !SkSwizzler::IsOpaque(fSwizzler->swizzle(dstRow, srcRow)); |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 543 | dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 547 | if (hasAlpha) { |
| 548 | fAlphaState = kHasAlpha_AlphaState; |
| 549 | } else { |
| 550 | fAlphaState = kOpaque_AlphaState; |
| 551 | } |
| 552 | |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 553 | // FIXME: do we need substituteTranspColor? Note that we cannot do it for |
| 554 | // scanline decoding, but we could do it here. Alternatively, we could do |
| 555 | // it as we go, instead of in post-processing like SkPNGImageDecoder. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 556 | |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 557 | if (setjmp(png_jmpbuf(fPng_ptr))) { |
| 558 | // We've already read all the scanlines. This is a success. |
emmaleer | 973ae86 | 2015-07-20 13:38:44 -0700 | [diff] [blame] | 559 | return kSuccess; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 560 | } |
emmaleer | 973ae86 | 2015-07-20 13:38:44 -0700 | [diff] [blame] | 561 | |
| 562 | // read rest of file, and get additional comment and time chunks in info_ptr |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 563 | png_read_end(fPng_ptr, fInfo_ptr); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 564 | |
emmaleer | 973ae86 | 2015-07-20 13:38:44 -0700 | [diff] [blame] | 565 | return kSuccess; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 566 | } |
| 567 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 568 | uint32_t SkPngCodec::onGetFillValue(SkColorType colorType, SkAlphaType alphaType) const { |
| 569 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 570 | if (colorPtr) { |
| 571 | return get_color_table_fill_value(colorType, colorPtr, 0); |
| 572 | } |
| 573 | return INHERITED::onGetFillValue(colorType, alphaType); |
| 574 | } |
| 575 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 576 | bool SkPngCodec::onReallyHasAlpha() const { |
| 577 | switch (fAlphaState) { |
| 578 | case kOpaque_AlphaState: |
| 579 | return false; |
| 580 | case kUnknown_AlphaState: |
| 581 | // Maybe the subclass knows? |
| 582 | return this->alphaInScanlineDecode() == kHasAlpha_AlphaState; |
| 583 | case kHasAlpha_AlphaState: |
| 584 | switch (this->alphaInScanlineDecode()) { |
| 585 | case kUnknown_AlphaState: |
| 586 | // Scanline decoder must not have been used. Return our knowledge. |
| 587 | return true; |
| 588 | case kOpaque_AlphaState: |
| 589 | // Scanline decoder was used, and did not find alpha in its subset. |
| 590 | return false; |
| 591 | case kHasAlpha_AlphaState: |
| 592 | return true; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // All valid AlphaStates have been covered, so this should not be reached. |
| 597 | SkASSERT(false); |
| 598 | return true; |
| 599 | } |
| 600 | |
| 601 | // Subclass of SkPngCodec which supports scanline decoding |
| 602 | class SkPngScanlineDecoder : public SkPngCodec { |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 603 | public: |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 604 | SkPngScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream, |
| 605 | png_structp png_ptr, png_infop info_ptr, int bitDepth) |
| 606 | : INHERITED(srcInfo, stream, png_ptr, info_ptr, bitDepth, 1) |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 607 | , fAlphaState(kUnknown_AlphaState) |
msarett | f724b99 | 2015-10-15 06:41:06 -0700 | [diff] [blame] | 608 | , fSrcRow(nullptr) |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 609 | {} |
| 610 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 611 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options, |
| 612 | SkPMColor ctable[], int* ctableCount) override { |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 613 | if (!conversion_possible(dstInfo, this->getInfo())) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 614 | return kInvalidConversion; |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 615 | } |
| 616 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 617 | const Result result = this->initializeSwizzler(dstInfo, options, ctable, |
| 618 | ctableCount); |
| 619 | if (result != kSuccess) { |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 620 | return result; |
| 621 | } |
| 622 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 623 | fAlphaState = kUnknown_AlphaState; |
| 624 | fStorage.reset(this->getInfo().width() * SkSwizzler::BytesPerPixel(this->srcConfig())); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 625 | fSrcRow = static_cast<uint8_t*>(fStorage.get()); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 626 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 627 | return kSuccess; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 628 | } |
| 629 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 630 | int onGetScanlines(void* dst, int count, size_t rowBytes) override { |
| 631 | // Assume that an error in libpng indicates an incomplete input. |
| 632 | int row = 0; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 633 | if (setjmp(png_jmpbuf(this->png_ptr()))) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 634 | SkCodecPrintf("setjmp long jump!\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 635 | return row; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 636 | } |
| 637 | |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 638 | void* dstRow = dst; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 639 | bool hasAlpha = false; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 640 | for (; row < count; row++) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 641 | png_read_rows(this->png_ptr(), &fSrcRow, png_bytepp_NULL, 1); |
| 642 | hasAlpha |= !SkSwizzler::IsOpaque(this->swizzler()->swizzle(dstRow, fSrcRow)); |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 643 | dstRow = SkTAddOffset<void>(dstRow, rowBytes); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 644 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 645 | |
| 646 | if (hasAlpha) { |
| 647 | fAlphaState = kHasAlpha_AlphaState; |
| 648 | } else { |
| 649 | if (kUnknown_AlphaState == fAlphaState) { |
| 650 | fAlphaState = kOpaque_AlphaState; |
| 651 | } |
| 652 | // Otherwise, the AlphaState is unchanged. |
| 653 | } |
| 654 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 655 | return row; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 656 | } |
| 657 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 658 | bool onSkipScanlines(int count) override { |
| 659 | // Assume that an error in libpng indicates an incomplete input. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 660 | if (setjmp(png_jmpbuf(this->png_ptr()))) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 661 | SkCodecPrintf("setjmp long jump!\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 662 | return false; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 663 | } |
emmaleer | 7dc9190 | 2015-05-27 08:49:04 -0700 | [diff] [blame] | 664 | //there is a potential tradeoff of memory vs speed created by putting this in a loop. |
| 665 | //calling png_read_rows in a loop is insignificantly slower than calling it once with count |
| 666 | //as png_read_rows has it's own loop which calls png_read_row count times. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 667 | for (int row = 0; row < count; row++) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 668 | png_read_rows(this->png_ptr(), &fSrcRow, png_bytepp_NULL, 1); |
emmaleer | 7dc9190 | 2015-05-27 08:49:04 -0700 | [diff] [blame] | 669 | } |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 670 | return true; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 671 | } |
| 672 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 673 | AlphaState alphaInScanlineDecode() const override { |
| 674 | return fAlphaState; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 675 | } |
| 676 | |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 677 | private: |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 678 | AlphaState fAlphaState; |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 679 | SkAutoMalloc fStorage; |
| 680 | uint8_t* fSrcRow; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 681 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 682 | typedef SkPngCodec INHERITED; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 683 | }; |
| 684 | |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 685 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 686 | class SkPngInterlacedScanlineDecoder : public SkPngCodec { |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 687 | public: |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 688 | SkPngInterlacedScanlineDecoder(const SkImageInfo& srcInfo, SkStream* stream, |
| 689 | png_structp png_ptr, png_infop info_ptr, int bitDepth, int numberPasses) |
| 690 | : INHERITED(srcInfo, stream, png_ptr, info_ptr, bitDepth, numberPasses) |
| 691 | , fAlphaState(kUnknown_AlphaState) |
| 692 | , fHeight(-1) |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 693 | , fCanSkipRewind(false) |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 694 | { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 695 | SkASSERT(numberPasses != 1); |
| 696 | } |
| 697 | |
| 698 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options, |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 699 | SkPMColor ctable[], int* ctableCount) override { |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 700 | if (!conversion_possible(dstInfo, this->getInfo())) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 701 | return kInvalidConversion; |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 702 | } |
| 703 | |
msarett | fdb4757 | 2015-10-13 12:50:14 -0700 | [diff] [blame] | 704 | const Result result = this->initializeSwizzler(dstInfo, options, ctable, |
| 705 | ctableCount); |
| 706 | if (result != kSuccess) { |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 707 | return result; |
| 708 | } |
| 709 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 710 | fAlphaState = kUnknown_AlphaState; |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 711 | fHeight = dstInfo.height(); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 712 | // FIXME: This need not be called on a second call to onStartScanlineDecode. |
| 713 | fSrcRowBytes = this->getInfo().width() * SkSwizzler::BytesPerPixel(this->srcConfig()); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 714 | fGarbageRow.reset(fSrcRowBytes); |
| 715 | fGarbageRowPtr = static_cast<uint8_t*>(fGarbageRow.get()); |
| 716 | fCanSkipRewind = true; |
| 717 | |
| 718 | return SkCodec::kSuccess; |
| 719 | } |
| 720 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 721 | int onGetScanlines(void* dst, int count, size_t dstRowBytes) override { |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 722 | // rewind stream if have previously called onGetScanlines, |
| 723 | // since we need entire progressive image to get scanlines |
| 724 | if (fCanSkipRewind) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 725 | // We already rewound in onStartScanlineDecode, so there is no reason to rewind. |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 726 | // Next time onGetScanlines is called, we will need to rewind. |
| 727 | fCanSkipRewind = false; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 728 | } else { |
| 729 | // rewindIfNeeded resets fCurrScanline, since it assumes that start |
| 730 | // needs to be called again before scanline decoding. PNG scanline |
| 731 | // decoding is the exception, since it needs to rewind between |
| 732 | // calls to getScanlines. Keep track of fCurrScanline, to undo the |
| 733 | // reset. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 734 | const int currScanline = this->nextScanline(); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 735 | // This method would never be called if currScanline is -1 |
| 736 | SkASSERT(currScanline != -1); |
| 737 | |
| 738 | if (!this->rewindIfNeeded()) { |
| 739 | return kCouldNotRewind; |
| 740 | } |
| 741 | this->updateNextScanline(currScanline); |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 742 | } |
| 743 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 744 | if (setjmp(png_jmpbuf(this->png_ptr()))) { |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 745 | SkCodecPrintf("setjmp long jump!\n"); |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 746 | // FIXME (msarett): Returning 0 is pessimistic. If we can complete a single pass, |
| 747 | // we may be able to report that all of the memory has been initialized. Even if we |
| 748 | // fail on the first pass, we can still report than some scanlines are initialized. |
| 749 | return 0; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 750 | } |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 751 | SkAutoMalloc storage(count * fSrcRowBytes); |
| 752 | uint8_t* storagePtr = static_cast<uint8_t*>(storage.get()); |
| 753 | uint8_t* srcRow; |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 754 | const int startRow = this->nextScanline(); |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 755 | for (int i = 0; i < this->numberPasses(); i++) { |
| 756 | // read rows we planned to skip into garbage row |
| 757 | for (int y = 0; y < startRow; y++){ |
| 758 | png_read_rows(this->png_ptr(), &fGarbageRowPtr, png_bytepp_NULL, 1); |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 759 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 760 | // read rows we care about into buffer |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 761 | srcRow = storagePtr; |
| 762 | for (int y = 0; y < count; y++) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 763 | png_read_rows(this->png_ptr(), &srcRow, png_bytepp_NULL, 1); |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 764 | srcRow += fSrcRowBytes; |
| 765 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 766 | // read rows we don't want into garbage buffer |
| 767 | for (int y = 0; y < fHeight - startRow - count; y++) { |
| 768 | png_read_rows(this->png_ptr(), &fGarbageRowPtr, png_bytepp_NULL, 1); |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | //swizzle the rows we care about |
| 772 | srcRow = storagePtr; |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 773 | void* dstRow = dst; |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 774 | bool hasAlpha = false; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 775 | for (int y = 0; y < count; y++) { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 776 | hasAlpha |= !SkSwizzler::IsOpaque(this->swizzler()->swizzle(dstRow, srcRow)); |
msarett | 614aa07 | 2015-07-27 15:13:17 -0700 | [diff] [blame] | 777 | dstRow = SkTAddOffset<void>(dstRow, dstRowBytes); |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 778 | srcRow += fSrcRowBytes; |
| 779 | } |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 780 | |
| 781 | if (hasAlpha) { |
| 782 | fAlphaState = kHasAlpha_AlphaState; |
| 783 | } else { |
| 784 | if (kUnknown_AlphaState == fAlphaState) { |
| 785 | fAlphaState = kOpaque_AlphaState; |
| 786 | } |
| 787 | // Otherwise, the AlphaState is unchanged. |
| 788 | } |
| 789 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 790 | return count; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 791 | } |
| 792 | |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 793 | bool onSkipScanlines(int count) override { |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 794 | // The non-virtual version will update fCurrScanline. |
msarett | e6dd004 | 2015-10-09 11:07:34 -0700 | [diff] [blame] | 795 | return true; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 796 | } |
| 797 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 798 | AlphaState alphaInScanlineDecode() const override { |
| 799 | return fAlphaState; |
| 800 | } |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 801 | |
msarett | 5406d6f | 2015-08-31 06:55:13 -0700 | [diff] [blame] | 802 | SkScanlineOrder onGetScanlineOrder() const override { |
| 803 | return kNone_SkScanlineOrder; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 804 | } |
| 805 | |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 806 | private: |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 807 | AlphaState fAlphaState; |
scroggo | 9b2cdbf4 | 2015-07-10 12:07:02 -0700 | [diff] [blame] | 808 | int fHeight; |
| 809 | size_t fSrcRowBytes; |
| 810 | SkAutoMalloc fGarbageRow; |
| 811 | uint8_t* fGarbageRowPtr; |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 812 | // FIXME: This imitates behavior in SkCodec::rewindIfNeeded. That function |
| 813 | // is called whenever some action is taken that reads the stream and |
| 814 | // therefore the next call will require a rewind. So it modifies a boolean |
| 815 | // to note that the *next* time it is called a rewind is needed. |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 816 | // SkPngInterlacedScanlineDecoder has an extra wrinkle - calling |
| 817 | // onStartScanlineDecode followed by onGetScanlines does *not* require a |
| 818 | // rewind. Since rewindIfNeeded does not have this flexibility, we need to |
| 819 | // add another layer. |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 820 | bool fCanSkipRewind; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 821 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 822 | typedef SkPngCodec INHERITED; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 823 | }; |
| 824 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 825 | SkCodec* SkPngCodec::NewFromStream(SkStream* stream) { |
| 826 | SkAutoTDelete<SkStream> streamDeleter(stream); |
| 827 | png_structp png_ptr; |
| 828 | png_infop info_ptr; |
| 829 | SkImageInfo imageInfo; |
| 830 | int bitDepth; |
| 831 | int numberPasses; |
| 832 | |
| 833 | if (!read_header(stream, &png_ptr, &info_ptr, &imageInfo, &bitDepth, &numberPasses)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 834 | return nullptr; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 835 | } |
| 836 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 837 | if (1 == numberPasses) { |
| 838 | return new SkPngScanlineDecoder(imageInfo, streamDeleter.detach(), png_ptr, info_ptr, |
| 839 | bitDepth); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 840 | } |
| 841 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 842 | return new SkPngInterlacedScanlineDecoder(imageInfo, streamDeleter.detach(), png_ptr, |
| 843 | info_ptr, bitDepth, numberPasses); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 844 | } |