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