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