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