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