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