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