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 | c4ce6b5 | 2016-06-16 07:37:41 -0700 | [diff] [blame] | 11 | #include "SkColorSpace_Base.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" |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 16 | #include "SkPoint3.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 17 | #include "SkSize.h" |
| 18 | #include "SkStream.h" |
| 19 | #include "SkSwizzler.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 20 | #include "SkTemplates.h" |
bungeman | 5d2cd6e | 2016-02-23 07:34:25 -0800 | [diff] [blame] | 21 | #include "SkUtils.h" |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 22 | |
mtklein | dc90b53 | 2016-07-28 14:45:28 -0700 | [diff] [blame] | 23 | // This warning triggers false postives way too often in here. |
| 24 | #if defined(__GNUC__) && !defined(__clang__) |
| 25 | #pragma GCC diagnostic ignored "-Wclobbered" |
| 26 | #endif |
| 27 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 28 | /////////////////////////////////////////////////////////////////////////////// |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 29 | // Callback functions |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
| 31 | |
| 32 | static void sk_error_fn(png_structp png_ptr, png_const_charp msg) { |
scroggo | 230d4ac | 2015-03-26 07:15:55 -0700 | [diff] [blame] | 33 | SkCodecPrintf("------ png error %s\n", msg); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 34 | longjmp(png_jmpbuf(png_ptr), 1); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 35 | } |
| 36 | |
scroggo | 0eed6df | 2015-03-26 10:07:56 -0700 | [diff] [blame] | 37 | void sk_warning_fn(png_structp, png_const_charp msg) { |
| 38 | SkCodecPrintf("----- png warning %s\n", msg); |
| 39 | } |
| 40 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 41 | static void sk_read_fn(png_structp png_ptr, png_bytep data, |
| 42 | png_size_t length) { |
| 43 | SkStream* stream = static_cast<SkStream*>(png_get_io_ptr(png_ptr)); |
| 44 | const size_t bytes = stream->read(data, length); |
| 45 | if (bytes != length) { |
| 46 | // FIXME: We want to report the fact that the stream was truncated. |
| 47 | // One way to do that might be to pass a enum to longjmp so setjmp can |
| 48 | // specify the failure. |
| 49 | png_error(png_ptr, "Read Error!"); |
| 50 | } |
| 51 | } |
| 52 | |
scroggo | cf98fa9 | 2015-11-23 08:14:40 -0800 | [diff] [blame] | 53 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 54 | static int sk_read_user_chunk(png_structp png_ptr, png_unknown_chunkp chunk) { |
| 55 | SkPngChunkReader* chunkReader = (SkPngChunkReader*)png_get_user_chunk_ptr(png_ptr); |
| 56 | // readChunk() returning true means continue decoding |
| 57 | return chunkReader->readChunk((const char*)chunk->name, chunk->data, chunk->size) ? 1 : -1; |
| 58 | } |
| 59 | #endif |
| 60 | |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 61 | /////////////////////////////////////////////////////////////////////////////// |
| 62 | // Helpers |
| 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | |
| 65 | class AutoCleanPng : public SkNoncopyable { |
| 66 | public: |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 67 | AutoCleanPng(png_structp png_ptr) |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 68 | : fPng_ptr(png_ptr) |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 69 | , fInfo_ptr(nullptr) {} |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 70 | |
| 71 | ~AutoCleanPng() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 72 | // fInfo_ptr will never be non-nullptr unless fPng_ptr is. |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 73 | if (fPng_ptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 74 | png_infopp info_pp = fInfo_ptr ? &fInfo_ptr : nullptr; |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 75 | png_destroy_read_struct(&fPng_ptr, info_pp, nullptr); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | void setInfoPtr(png_infop info_ptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | SkASSERT(nullptr == fInfo_ptr); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 81 | fInfo_ptr = info_ptr; |
| 82 | } |
| 83 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 84 | void release() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 85 | fPng_ptr = nullptr; |
| 86 | fInfo_ptr = nullptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 87 | } |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | png_structp fPng_ptr; |
| 91 | png_infop fInfo_ptr; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 92 | }; |
| 93 | #define AutoCleanPng(...) SK_REQUIRE_LOCAL_VAR(AutoCleanPng) |
| 94 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 95 | static inline SkAlphaType xform_alpha_type(SkAlphaType dstAlphaType, SkAlphaType srcAlphaType) { |
| 96 | return (kOpaque_SkAlphaType == srcAlphaType) ? kOpaque_SkAlphaType : dstAlphaType; |
| 97 | } |
| 98 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 99 | // Note: SkColorTable claims to store SkPMColors, which is not necessarily the case here. |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 100 | bool SkPngCodec::createColorTable(const SkImageInfo& dstInfo, int* ctableCount) { |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 101 | |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 102 | int numColors; |
| 103 | png_color* palette; |
| 104 | if (!png_get_PLTE(fPng_ptr, fInfo_ptr, &palette, &numColors)) { |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 105 | return false; |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 106 | } |
| 107 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 108 | // Contents depend on tableColorType and our choice of if/when to premultiply: |
| 109 | // { kPremul, kUnpremul, kOpaque } x { RGBA, BGRA } |
| 110 | SkPMColor colorTable[256]; |
| 111 | SkColorType tableColorType = fColorXform ? kRGBA_8888_SkColorType : dstInfo.colorType(); |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 112 | |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 113 | png_bytep alphas; |
| 114 | int numColorsWithAlpha = 0; |
| 115 | if (png_get_tRNS(fPng_ptr, fInfo_ptr, &alphas, &numColorsWithAlpha, nullptr)) { |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 116 | // If we are performing a color xform, it will handle the premultiply. Otherwise, |
| 117 | // we'll do it here. |
| 118 | bool premultiply = !fColorXform && needs_premul(dstInfo, this->getInfo()); |
| 119 | |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 120 | // Choose which function to use to create the color table. If the final destination's |
| 121 | // colortype is unpremultiplied, the color table will store unpremultiplied colors. |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 122 | PackColorProc proc = choose_pack_color_proc(premultiply, tableColorType); |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 123 | |
| 124 | for (int i = 0; i < numColorsWithAlpha; i++) { |
| 125 | // We don't have a function in SkOpts that combines a set of alphas with a set |
| 126 | // of RGBs. We could write one, but it's hardly worth it, given that this |
| 127 | // is such a small fraction of the total decode time. |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 128 | colorTable[i] = proc(alphas[i], palette->red, palette->green, palette->blue); |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 129 | palette++; |
| 130 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 131 | } |
| 132 | |
msarett | 13a9123 | 2016-02-01 08:03:29 -0800 | [diff] [blame] | 133 | if (numColorsWithAlpha < numColors) { |
| 134 | // The optimized code depends on a 3-byte png_color struct with the colors |
| 135 | // in RGB order. These checks make sure it is safe to use. |
| 136 | static_assert(3 == sizeof(png_color), "png_color struct has changed. Opts are broken."); |
| 137 | #ifdef SK_DEBUG |
| 138 | SkASSERT(&palette->red < &palette->green); |
| 139 | SkASSERT(&palette->green < &palette->blue); |
| 140 | #endif |
| 141 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 142 | if (is_rgba(tableColorType)) { |
| 143 | SkOpts::RGB_to_RGB1(colorTable + numColorsWithAlpha, palette, |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 144 | numColors - numColorsWithAlpha); |
| 145 | } else { |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 146 | SkOpts::RGB_to_BGR1(colorTable + numColorsWithAlpha, palette, |
msarett | 34e0ec4 | 2016-04-22 16:27:24 -0700 | [diff] [blame] | 147 | numColors - numColorsWithAlpha); |
| 148 | } |
scroggo | f24f224 | 2015-03-03 08:59:20 -0800 | [diff] [blame] | 149 | } |
| 150 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 151 | // If we are not decoding to F16, we can color xform now and store the results |
| 152 | // in the color table. |
| 153 | if (fColorXform && kRGBA_F16_SkColorType != dstInfo.colorType()) { |
| 154 | SkColorType xformColorType = is_rgba(dstInfo.colorType()) ? |
| 155 | kRGBA_8888_SkColorType : kBGRA_8888_SkColorType; |
| 156 | SkAlphaType xformAlphaType = xform_alpha_type(dstInfo.alphaType(), |
| 157 | this->getInfo().alphaType()); |
| 158 | fColorXform->apply(colorTable, colorTable, numColors, xformColorType, xformAlphaType); |
| 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) { |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 165 | SkPMColor lastColor = numColors > 0 ? colorTable[numColors - 1] : SK_ColorBLACK; |
| 166 | sk_memset32(colorTable + 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 | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 174 | fColorTable.reset(new SkColorTable(colorTable, 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 | |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 186 | static float png_fixed_point_to_float(png_fixed_point x) { |
| 187 | // We multiply by the same factor that libpng used to convert |
| 188 | // fixed point -> double. Since we want floats, we choose to |
| 189 | // do the conversion ourselves rather than convert |
| 190 | // fixed point -> double -> float. |
| 191 | return ((float) x) * 0.00001f; |
| 192 | } |
| 193 | |
msarett | 128245c | 2016-03-30 12:01:47 -0700 | [diff] [blame] | 194 | static float png_inverted_fixed_point_to_float(png_fixed_point x) { |
| 195 | // This is necessary because the gAMA chunk actually stores 1/gamma. |
| 196 | return 1.0f / png_fixed_point_to_float(x); |
| 197 | } |
| 198 | |
msarett | c4ce6b5 | 2016-06-16 07:37:41 -0700 | [diff] [blame] | 199 | static constexpr float gSRGB_toXYZD50[] { |
| 200 | 0.4358f, 0.2224f, 0.0139f, // * R |
| 201 | 0.3853f, 0.7170f, 0.0971f, // * G |
| 202 | 0.1430f, 0.0606f, 0.7139f, // * B |
| 203 | }; |
| 204 | |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 205 | static bool convert_to_D50(SkMatrix44* toXYZD50, float toXYZ[9], float whitePoint[2]) { |
| 206 | float wX = whitePoint[0]; |
| 207 | float wY = whitePoint[1]; |
| 208 | if (wX < 0.0f || wY < 0.0f || (wX + wY > 1.0f)) { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | // Calculate the XYZ illuminant. Call this the src illuminant. |
| 213 | float wZ = 1.0f - wX - wY; |
| 214 | float scale = 1.0f / wY; |
| 215 | // TODO (msarett): |
| 216 | // What are common src illuminants? I'm guessing we will almost always see D65. Should |
| 217 | // we go ahead and save a precomputed D65->D50 Bradford matrix? Should we exit early if |
| 218 | // if the src illuminant is D50? |
| 219 | SkVector3 srcXYZ = SkVector3::Make(wX * scale, 1.0f, wZ * scale); |
| 220 | |
| 221 | // The D50 illuminant. |
| 222 | SkVector3 dstXYZ = SkVector3::Make(0.96422f, 1.0f, 0.82521f); |
| 223 | |
| 224 | // Calculate the chromatic adaptation matrix. We will use the Bradford method, thus |
| 225 | // the matrices below. The Bradford method is used by Adobe and is widely considered |
| 226 | // to be the best. |
| 227 | // http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html |
| 228 | SkMatrix mA, mAInv; |
| 229 | mA.setAll(0.8951f, 0.2664f, -0.1614f, -0.7502f, 1.7135f, 0.0367f, 0.0389f, -0.0685f, 1.0296f); |
| 230 | mAInv.setAll(0.9869929f, -0.1470543f, 0.1599627f, 0.4323053f, 0.5183603f, 0.0492912f, |
| 231 | -0.0085287f, 0.0400428f, 0.9684867f); |
| 232 | |
| 233 | // Map illuminant into cone response domain. |
| 234 | SkVector3 srcCone; |
| 235 | srcCone.fX = mA[0] * srcXYZ.fX + mA[1] * srcXYZ.fY + mA[2] * srcXYZ.fZ; |
| 236 | srcCone.fY = mA[3] * srcXYZ.fX + mA[4] * srcXYZ.fY + mA[5] * srcXYZ.fZ; |
| 237 | srcCone.fZ = mA[6] * srcXYZ.fX + mA[7] * srcXYZ.fY + mA[8] * srcXYZ.fZ; |
| 238 | SkVector3 dstCone; |
| 239 | dstCone.fX = mA[0] * dstXYZ.fX + mA[1] * dstXYZ.fY + mA[2] * dstXYZ.fZ; |
| 240 | dstCone.fY = mA[3] * dstXYZ.fX + mA[4] * dstXYZ.fY + mA[5] * dstXYZ.fZ; |
| 241 | dstCone.fZ = mA[6] * dstXYZ.fX + mA[7] * dstXYZ.fY + mA[8] * dstXYZ.fZ; |
| 242 | |
| 243 | SkMatrix DXToD50; |
| 244 | DXToD50.setIdentity(); |
| 245 | DXToD50[0] = dstCone.fX / srcCone.fX; |
| 246 | DXToD50[4] = dstCone.fY / srcCone.fY; |
| 247 | DXToD50[8] = dstCone.fZ / srcCone.fZ; |
| 248 | DXToD50.postConcat(mAInv); |
| 249 | DXToD50.preConcat(mA); |
| 250 | |
| 251 | SkMatrix toXYZ3x3; |
| 252 | toXYZ3x3.setAll(toXYZ[0], toXYZ[3], toXYZ[6], toXYZ[1], toXYZ[4], toXYZ[7], toXYZ[2], toXYZ[5], |
| 253 | toXYZ[8]); |
| 254 | toXYZ3x3.postConcat(DXToD50); |
| 255 | |
| 256 | toXYZD50->set3x3(toXYZ3x3[0], toXYZ3x3[1], toXYZ3x3[2], toXYZ3x3[3], toXYZ3x3[4], toXYZ3x3[5], |
| 257 | toXYZ3x3[6], toXYZ3x3[7], toXYZ3x3[8]); |
| 258 | return true; |
| 259 | } |
| 260 | |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 261 | // Returns a colorSpace object that represents any color space information in |
| 262 | // the encoded data. If the encoded data contains no color space, this will |
| 263 | // return NULL. |
msarett | ad8bcfe | 2016-03-07 07:09:03 -0800 | [diff] [blame] | 264 | 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] | 265 | |
msarett | e244322 | 2016-03-04 14:20:49 -0800 | [diff] [blame] | 266 | #if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6) |
| 267 | |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 268 | // First check for an ICC profile |
| 269 | png_bytep profile; |
| 270 | png_uint_32 length; |
| 271 | // The below variables are unused, however, we need to pass them in anyway or |
| 272 | // png_get_iCCP() will return nothing. |
| 273 | // Could knowing the |name| of the profile ever be interesting? Maybe for debugging? |
| 274 | png_charp name; |
| 275 | // The |compression| is uninteresting since: |
| 276 | // (1) libpng has already decompressed the profile for us. |
| 277 | // (2) "deflate" is the only mode of decompression that libpng supports. |
| 278 | int compression; |
| 279 | if (PNG_INFO_iCCP == png_get_iCCP(png_ptr, info_ptr, &name, &compression, &profile, |
| 280 | &length)) { |
| 281 | return SkColorSpace::NewICC(profile, length); |
| 282 | } |
| 283 | |
| 284 | // Second, check for sRGB. |
| 285 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) { |
| 286 | |
| 287 | // sRGB chunks also store a rendering intent: Absolute, Relative, |
| 288 | // Perceptual, and Saturation. |
| 289 | // FIXME (msarett): Extract this information from the sRGB chunk once |
| 290 | // we are able to handle this information in |
| 291 | // SkColorSpace. |
| 292 | return SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
| 293 | } |
| 294 | |
| 295 | // Next, check for chromaticities. |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 296 | png_fixed_point toXYZFixed[9]; |
| 297 | float toXYZ[9]; |
| 298 | png_fixed_point whitePointFixed[2]; |
| 299 | float whitePoint[2]; |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 300 | png_fixed_point gamma; |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 301 | float gammas[3]; |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 302 | if (png_get_cHRM_XYZ_fixed(png_ptr, info_ptr, &toXYZFixed[0], &toXYZFixed[1], &toXYZFixed[2], |
| 303 | &toXYZFixed[3], &toXYZFixed[4], &toXYZFixed[5], &toXYZFixed[6], |
| 304 | &toXYZFixed[7], &toXYZFixed[8]) && |
| 305 | png_get_cHRM_fixed(png_ptr, info_ptr, &whitePointFixed[0], &whitePointFixed[1], nullptr, |
| 306 | nullptr, nullptr, nullptr, nullptr, nullptr)) |
| 307 | { |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 308 | for (int i = 0; i < 9; i++) { |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 309 | toXYZ[i] = png_fixed_point_to_float(toXYZFixed[i]); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 310 | } |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 311 | whitePoint[0] = png_fixed_point_to_float(whitePointFixed[0]); |
| 312 | whitePoint[1] = png_fixed_point_to_float(whitePointFixed[1]); |
| 313 | |
| 314 | SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor); |
| 315 | if (!convert_to_D50(&toXYZD50, toXYZ, whitePoint)) { |
| 316 | toXYZD50.set3x3RowMajorf(gSRGB_toXYZD50); |
| 317 | } |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 318 | |
msarett | 128245c | 2016-03-30 12:01:47 -0700 | [diff] [blame] | 319 | if (PNG_INFO_gAMA == png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) { |
msarett | ffc2aea | 2016-05-02 11:12:14 -0700 | [diff] [blame] | 320 | float value = png_inverted_fixed_point_to_float(gamma); |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 321 | gammas[0] = value; |
| 322 | gammas[1] = value; |
| 323 | gammas[2] = value; |
msarett | ffc2aea | 2016-05-02 11:12:14 -0700 | [diff] [blame] | 324 | |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 325 | return SkColorSpace_Base::NewRGB(gammas, toXYZD50); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 326 | } |
msarett | 128245c | 2016-03-30 12:01:47 -0700 | [diff] [blame] | 327 | |
msarett | c4ce6b5 | 2016-06-16 07:37:41 -0700 | [diff] [blame] | 328 | // Default to sRGB gamma if the image has color space information, |
| 329 | // but does not specify gamma. |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 330 | return SkColorSpace::NewRGB(SkColorSpace::kSRGB_GammaNamed, toXYZD50); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // Last, check for gamma. |
| 334 | if (PNG_INFO_gAMA == png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) { |
| 335 | |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 336 | // Set the gammas. |
msarett | ffc2aea | 2016-05-02 11:12:14 -0700 | [diff] [blame] | 337 | float value = png_inverted_fixed_point_to_float(gamma); |
msarett | bb9f774 | 2016-05-17 09:31:20 -0700 | [diff] [blame] | 338 | gammas[0] = value; |
| 339 | gammas[1] = value; |
| 340 | gammas[2] = value; |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 341 | |
msarett | c4ce6b5 | 2016-06-16 07:37:41 -0700 | [diff] [blame] | 342 | // Since there is no cHRM, we will guess sRGB gamut. |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 343 | SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor); |
| 344 | toXYZD50.set3x3RowMajorf(gSRGB_toXYZD50); |
msarett | c4ce6b5 | 2016-06-16 07:37:41 -0700 | [diff] [blame] | 345 | |
msarett | 5544795 | 2016-07-22 14:07:23 -0700 | [diff] [blame] | 346 | return SkColorSpace_Base::NewRGB(gammas, toXYZD50); |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 347 | } |
| 348 | |
msarett | e244322 | 2016-03-04 14:20:49 -0800 | [diff] [blame] | 349 | #endif // LIBPNG >= 1.6 |
| 350 | |
msarett | c4ce6b5 | 2016-06-16 07:37:41 -0700 | [diff] [blame] | 351 | // Report that there is no color space information in the PNG. SkPngCodec is currently |
| 352 | // implemented to guess sRGB in this case. |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 353 | return nullptr; |
| 354 | } |
| 355 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 356 | static int bytes_per_pixel(int bitsPerPixel) { |
| 357 | // Note that we will have to change this implementation if we start |
| 358 | // supporting outputs from libpng that are less than 8-bits per component. |
| 359 | return bitsPerPixel / 8; |
| 360 | } |
| 361 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 362 | static bool png_conversion_possible(const SkImageInfo& dst, const SkImageInfo& src) { |
| 363 | // Ensure the alpha type is valid |
| 364 | if (!valid_alpha(dst.alphaType(), src.alphaType())) { |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | // Check for supported color types |
| 369 | switch (dst.colorType()) { |
| 370 | case kRGBA_8888_SkColorType: |
| 371 | case kBGRA_8888_SkColorType: |
| 372 | case kRGBA_F16_SkColorType: |
| 373 | return true; |
| 374 | case kRGB_565_SkColorType: |
| 375 | return kOpaque_SkAlphaType == src.alphaType(); |
| 376 | default: |
| 377 | return dst.colorType() == src.colorType(); |
| 378 | } |
| 379 | } |
| 380 | |
msarett | 35bb74b | 2016-08-22 07:41:28 -0700 | [diff] [blame] | 381 | void SkPngCodec::allocateStorage() { |
| 382 | size_t colorXformBytes = fColorXform ? fSwizzler->swizzleWidth() * sizeof(uint32_t) : 0; |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 383 | |
| 384 | fStorage.reset(SkAlign4(fSrcRowBytes) + colorXformBytes); |
| 385 | fSwizzlerSrcRow = fStorage.get(); |
| 386 | fColorXformSrcRow = |
| 387 | fColorXform ? SkTAddOffset<uint32_t>(fSwizzlerSrcRow, SkAlign4(fSrcRowBytes)) : 0; |
| 388 | } |
| 389 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 390 | static inline bool apply_xform_on_decode(SkColorType dstColorType, SkEncodedInfo::Color srcColor) { |
| 391 | // We will apply the color xform when reading the color table, unless F16 is requested. |
| 392 | return SkEncodedInfo::kPalette_Color != srcColor || kRGBA_F16_SkColorType == dstColorType; |
| 393 | } |
| 394 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 395 | class SkPngNormalCodec : public SkPngCodec { |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 396 | public: |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 397 | SkPngNormalCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo, |
| 398 | SkStream* stream, SkPngChunkReader* chunkReader, png_structp png_ptr, |
| 399 | png_infop info_ptr, int bitDepth) |
| 400 | : INHERITED(encodedInfo, imageInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, 1) |
scroggo | 1c005e4 | 2015-08-04 09:24:45 -0700 | [diff] [blame] | 401 | {} |
| 402 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 403 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options, |
| 404 | SkPMColor ctable[], int* ctableCount) override { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 405 | if (!png_conversion_possible(dstInfo, this->getInfo()) || |
| 406 | !this->initializeXforms(dstInfo, options, ctable, ctableCount)) |
| 407 | { |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 408 | return kInvalidConversion; |
| 409 | } |
| 410 | |
msarett | 35bb74b | 2016-08-22 07:41:28 -0700 | [diff] [blame] | 411 | this->allocateStorage(); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 412 | return kSuccess; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 413 | } |
| 414 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 415 | int readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count, int startRow) |
| 416 | override { |
| 417 | SkASSERT(0 == startRow); |
| 418 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 419 | // Assume that an error in libpng indicates an incomplete input. |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 420 | int y = 0; |
| 421 | if (setjmp(png_jmpbuf(fPng_ptr))) { |
| 422 | SkCodecPrintf("Failed to read row.\n"); |
| 423 | return y; |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 424 | } |
| 425 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 426 | void* swizzlerDstRow = dst; |
| 427 | size_t swizzlerDstRowBytes = rowBytes; |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 428 | |
| 429 | bool colorXform = fColorXform && |
| 430 | apply_xform_on_decode(dstInfo.colorType(), this->getEncodedInfo().color()); |
| 431 | if (colorXform) { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 432 | swizzlerDstRow = fColorXformSrcRow; |
| 433 | swizzlerDstRowBytes = 0; |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 434 | } |
| 435 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 436 | SkAlphaType xformAlphaType = xform_alpha_type(dstInfo.alphaType(), |
| 437 | this->getInfo().alphaType()); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 438 | for (; y < count; y++) { |
| 439 | png_read_row(fPng_ptr, fSwizzlerSrcRow, nullptr); |
| 440 | fSwizzler->swizzle(swizzlerDstRow, fSwizzlerSrcRow); |
| 441 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 442 | if (colorXform) { |
msarett | 35bb74b | 2016-08-22 07:41:28 -0700 | [diff] [blame] | 443 | fColorXform->apply(dst, (const uint32_t*) swizzlerDstRow, fSwizzler->swizzleWidth(), |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 444 | dstInfo.colorType(), xformAlphaType); |
| 445 | dst = SkTAddOffset<void>(dst, rowBytes); |
| 446 | } |
| 447 | |
| 448 | swizzlerDstRow = SkTAddOffset<void>(swizzlerDstRow, swizzlerDstRowBytes); |
| 449 | } |
| 450 | |
| 451 | return y; |
| 452 | } |
| 453 | |
| 454 | int onGetScanlines(void* dst, int count, size_t rowBytes) override { |
| 455 | return this->readRows(this->dstInfo(), dst, rowBytes, count, 0); |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 456 | } |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 457 | |
| 458 | bool onSkipScanlines(int count) override { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 459 | if (setjmp(png_jmpbuf(fPng_ptr))) { |
| 460 | SkCodecPrintf("Failed to skip row.\n"); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
| 464 | for (int row = 0; row < count; row++) { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 465 | png_read_row(fPng_ptr, fSwizzlerSrcRow, nullptr); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 466 | } |
| 467 | return true; |
| 468 | } |
| 469 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 470 | typedef SkPngCodec INHERITED; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 471 | }; |
| 472 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 473 | class SkPngInterlacedCodec : public SkPngCodec { |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 474 | public: |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 475 | SkPngInterlacedCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo, |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 476 | SkStream* stream, SkPngChunkReader* chunkReader, png_structp png_ptr, |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 477 | png_infop info_ptr, int bitDepth, int numberPasses) |
| 478 | : INHERITED(encodedInfo, imageInfo, stream, chunkReader, png_ptr, info_ptr, bitDepth, |
| 479 | numberPasses) |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 480 | , fCanSkipRewind(false) |
| 481 | { |
| 482 | SkASSERT(numberPasses != 1); |
| 483 | } |
| 484 | |
| 485 | Result onStartScanlineDecode(const SkImageInfo& dstInfo, const Options& options, |
| 486 | SkPMColor ctable[], int* ctableCount) override { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 487 | if (!png_conversion_possible(dstInfo, this->getInfo()) || |
| 488 | !this->initializeXforms(dstInfo, options, ctable, ctableCount)) |
| 489 | { |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 490 | return kInvalidConversion; |
| 491 | } |
| 492 | |
msarett | 35bb74b | 2016-08-22 07:41:28 -0700 | [diff] [blame] | 493 | this->allocateStorage(); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 494 | fCanSkipRewind = true; |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 495 | return SkCodec::kSuccess; |
| 496 | } |
| 497 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 498 | int readRows(const SkImageInfo& dstInfo, void* dst, size_t rowBytes, int count, int startRow) |
| 499 | override { |
| 500 | if (setjmp(png_jmpbuf(fPng_ptr))) { |
| 501 | SkCodecPrintf("Failed to get scanlines.\n"); |
| 502 | // FIXME (msarett): Returning 0 is pessimistic. If we can complete a single pass, |
| 503 | // we may be able to report that all of the memory has been initialized. Even if we |
| 504 | // fail on the first pass, we can still report than some scanlines are initialized. |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | SkAutoTMalloc<uint8_t> storage(count * fSrcRowBytes); |
| 509 | uint8_t* srcRow; |
| 510 | for (int i = 0; i < fNumberPasses; i++) { |
| 511 | // Discard rows that we planned to skip. |
| 512 | for (int y = 0; y < startRow; y++){ |
| 513 | png_read_row(fPng_ptr, fSwizzlerSrcRow, nullptr); |
| 514 | } |
| 515 | // Read rows we care about into storage. |
| 516 | srcRow = storage.get(); |
| 517 | for (int y = 0; y < count; y++) { |
| 518 | png_read_row(fPng_ptr, srcRow, nullptr); |
| 519 | srcRow += fSrcRowBytes; |
| 520 | } |
| 521 | // Discard rows that we don't need. |
| 522 | for (int y = 0; y < this->getInfo().height() - startRow - count; y++) { |
| 523 | png_read_row(fPng_ptr, fSwizzlerSrcRow, nullptr); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | // Swizzle and xform the rows we care about |
| 528 | void* swizzlerDstRow = dst; |
| 529 | size_t swizzlerDstRowBytes = rowBytes; |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 530 | |
| 531 | bool colorXform = fColorXform && |
| 532 | apply_xform_on_decode(dstInfo.colorType(), this->getEncodedInfo().color()); |
| 533 | if (colorXform) { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 534 | swizzlerDstRow = fColorXformSrcRow; |
| 535 | swizzlerDstRowBytes = 0; |
| 536 | } |
| 537 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 538 | SkAlphaType xformAlphaType = xform_alpha_type(dstInfo.alphaType(), |
| 539 | this->getInfo().alphaType()); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 540 | srcRow = storage.get(); |
| 541 | for (int y = 0; y < count; y++) { |
| 542 | fSwizzler->swizzle(swizzlerDstRow, srcRow); |
| 543 | srcRow = SkTAddOffset<uint8_t>(srcRow, fSrcRowBytes); |
| 544 | |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 545 | if (colorXform) { |
| 546 | fColorXform->apply(dst, (const uint32_t*) swizzlerDstRow, fSwizzler->swizzleWidth(), |
| 547 | dstInfo.colorType(), xformAlphaType); |
| 548 | dst = SkTAddOffset<void>(dst, rowBytes); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | swizzlerDstRow = SkTAddOffset<void>(swizzlerDstRow, swizzlerDstRowBytes); |
| 552 | } |
| 553 | |
| 554 | return count; |
| 555 | } |
| 556 | |
| 557 | int onGetScanlines(void* dst, int count, size_t rowBytes) override { |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 558 | // rewind stream if have previously called onGetScanlines, |
| 559 | // since we need entire progressive image to get scanlines |
| 560 | if (fCanSkipRewind) { |
| 561 | // We already rewound in onStartScanlineDecode, so there is no reason to rewind. |
| 562 | // Next time onGetScanlines is called, we will need to rewind. |
| 563 | fCanSkipRewind = false; |
| 564 | } else { |
| 565 | // rewindIfNeeded resets fCurrScanline, since it assumes that start |
| 566 | // needs to be called again before scanline decoding. PNG scanline |
| 567 | // decoding is the exception, since it needs to rewind between |
| 568 | // calls to getScanlines. Keep track of fCurrScanline, to undo the |
| 569 | // reset. |
| 570 | const int currScanline = this->nextScanline(); |
| 571 | // This method would never be called if currScanline is -1 |
| 572 | SkASSERT(currScanline != -1); |
| 573 | |
| 574 | if (!this->rewindIfNeeded()) { |
| 575 | return kCouldNotRewind; |
| 576 | } |
| 577 | this->updateCurrScanline(currScanline); |
| 578 | } |
| 579 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 580 | return this->readRows(this->dstInfo(), dst, rowBytes, count, this->nextScanline()); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | bool onSkipScanlines(int count) override { |
| 584 | // The non-virtual version will update fCurrScanline. |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | SkScanlineOrder onGetScanlineOrder() const override { |
| 589 | return kNone_SkScanlineOrder; |
emmaleer | 8f4ba76 | 2015-08-14 07:44:46 -0700 | [diff] [blame] | 590 | } |
| 591 | |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 592 | private: |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 593 | // FIXME: This imitates behavior in SkCodec::rewindIfNeeded. That function |
| 594 | // is called whenever some action is taken that reads the stream and |
| 595 | // therefore the next call will require a rewind. So it modifies a boolean |
| 596 | // to note that the *next* time it is called a rewind is needed. |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 597 | // SkPngInterlacedCodec has an extra wrinkle - calling |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 598 | // onStartScanlineDecode followed by onGetScanlines does *not* require a |
| 599 | // rewind. Since rewindIfNeeded does not have this flexibility, we need to |
| 600 | // add another layer. |
| 601 | bool fCanSkipRewind; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 602 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 603 | typedef SkPngCodec INHERITED; |
emmaleer | 0a4c3cb | 2015-06-22 10:40:21 -0700 | [diff] [blame] | 604 | }; |
| 605 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 606 | // Reads the header and initializes the output fields, if not NULL. |
| 607 | // |
| 608 | // @param stream Input data. Will be read to get enough information to properly |
| 609 | // setup the codec. |
| 610 | // @param chunkReader SkPngChunkReader, for reading unknown chunks. May be NULL. |
| 611 | // If not NULL, png_ptr will hold an *unowned* pointer to it. The caller is |
| 612 | // expected to continue to own it for the lifetime of the png_ptr. |
| 613 | // @param outCodec Optional output variable. If non-NULL, will be set to a new |
| 614 | // SkPngCodec on success. |
| 615 | // @param png_ptrp Optional output variable. If non-NULL, will be set to a new |
| 616 | // png_structp on success. |
| 617 | // @param info_ptrp Optional output variable. If non-NULL, will be set to a new |
| 618 | // png_infop on success; |
| 619 | // @return true on success, in which case the caller is responsible for calling |
| 620 | // png_destroy_read_struct(png_ptrp, info_ptrp). |
| 621 | // If it returns false, the passed in fields (except stream) are unchanged. |
| 622 | static bool read_header(SkStream* stream, SkPngChunkReader* chunkReader, SkCodec** outCodec, |
| 623 | png_structp* png_ptrp, png_infop* info_ptrp) { |
| 624 | // The image is known to be a PNG. Decode enough to know the SkImageInfo. |
| 625 | png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, |
| 626 | sk_error_fn, sk_warning_fn); |
| 627 | if (!png_ptr) { |
| 628 | return false; |
| 629 | } |
| 630 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 631 | AutoCleanPng autoClean(png_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 632 | |
| 633 | png_infop info_ptr = png_create_info_struct(png_ptr); |
| 634 | if (info_ptr == nullptr) { |
| 635 | return false; |
| 636 | } |
| 637 | |
| 638 | autoClean.setInfoPtr(info_ptr); |
| 639 | |
| 640 | // FIXME: Could we use the return value of setjmp to specify the type of |
| 641 | // error? |
| 642 | if (setjmp(png_jmpbuf(png_ptr))) { |
| 643 | return false; |
| 644 | } |
| 645 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 646 | png_set_read_fn(png_ptr, static_cast<void*>(stream), sk_read_fn); |
| 647 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 648 | #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 649 | // Hookup our chunkReader so we can see any user-chunks the caller may be interested in. |
| 650 | // This needs to be installed before we read the png header. Android may store ninepatch |
| 651 | // chunks in the header. |
| 652 | if (chunkReader) { |
| 653 | png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, (png_byte*)"", 0); |
| 654 | png_set_read_user_chunk_fn(png_ptr, (png_voidp) chunkReader, sk_read_user_chunk); |
| 655 | } |
| 656 | #endif |
| 657 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 658 | // The call to png_read_info() gives us all of the information from the |
| 659 | // PNG file before the first IDAT (image data chunk). |
| 660 | png_read_info(png_ptr, info_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 661 | png_uint_32 origWidth, origHeight; |
| 662 | int bitDepth, encodedColorType; |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 663 | png_get_IHDR(png_ptr, info_ptr, &origWidth, &origHeight, &bitDepth, |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 664 | &encodedColorType, nullptr, nullptr, nullptr); |
| 665 | |
| 666 | // Tell libpng to strip 16 bit/color files down to 8 bits/color. |
| 667 | // TODO: Should we handle this in SkSwizzler? Could this also benefit |
| 668 | // RAW decodes? |
| 669 | if (bitDepth == 16) { |
| 670 | SkASSERT(PNG_COLOR_TYPE_PALETTE != encodedColorType); |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 671 | png_set_strip_16(png_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // Now determine the default colorType and alphaType and set the required transforms. |
| 675 | // Often, we depend on SkSwizzler to perform any transforms that we need. However, we |
| 676 | // still depend on libpng for many of the rare and PNG-specific cases. |
| 677 | SkEncodedInfo::Color color; |
| 678 | SkEncodedInfo::Alpha alpha; |
| 679 | switch (encodedColorType) { |
| 680 | case PNG_COLOR_TYPE_PALETTE: |
| 681 | // Extract multiple pixels with bit depths of 1, 2, and 4 from a single |
| 682 | // byte into separate bytes (useful for paletted and grayscale images). |
| 683 | if (bitDepth < 8) { |
| 684 | // TODO: Should we use SkSwizzler here? |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 685 | png_set_packing(png_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | color = SkEncodedInfo::kPalette_Color; |
| 689 | // Set the alpha depending on if a transparency chunk exists. |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 690 | alpha = png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ? |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 691 | SkEncodedInfo::kUnpremul_Alpha : SkEncodedInfo::kOpaque_Alpha; |
| 692 | break; |
| 693 | case PNG_COLOR_TYPE_RGB: |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 694 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 695 | // Convert to RGBA if transparency chunk exists. |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 696 | png_set_tRNS_to_alpha(png_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 697 | color = SkEncodedInfo::kRGBA_Color; |
| 698 | alpha = SkEncodedInfo::kBinary_Alpha; |
| 699 | } else { |
| 700 | color = SkEncodedInfo::kRGB_Color; |
| 701 | alpha = SkEncodedInfo::kOpaque_Alpha; |
| 702 | } |
| 703 | break; |
| 704 | case PNG_COLOR_TYPE_GRAY: |
| 705 | // Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel. |
| 706 | if (bitDepth < 8) { |
| 707 | // TODO: Should we use SkSwizzler here? |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 708 | png_set_expand_gray_1_2_4_to_8(png_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 709 | } |
| 710 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 711 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 712 | png_set_tRNS_to_alpha(png_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 713 | color = SkEncodedInfo::kGrayAlpha_Color; |
| 714 | alpha = SkEncodedInfo::kBinary_Alpha; |
| 715 | } else { |
| 716 | color = SkEncodedInfo::kGray_Color; |
| 717 | alpha = SkEncodedInfo::kOpaque_Alpha; |
| 718 | } |
| 719 | break; |
| 720 | case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 721 | color = SkEncodedInfo::kGrayAlpha_Color; |
| 722 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 723 | break; |
| 724 | case PNG_COLOR_TYPE_RGBA: |
| 725 | color = SkEncodedInfo::kRGBA_Color; |
| 726 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 727 | break; |
| 728 | default: |
| 729 | // All the color types have been covered above. |
| 730 | SkASSERT(false); |
| 731 | color = SkEncodedInfo::kRGBA_Color; |
| 732 | alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 733 | } |
| 734 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 735 | int numberPasses = png_set_interlace_handling(png_ptr); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 736 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 737 | autoClean.release(); |
| 738 | if (png_ptrp) { |
| 739 | *png_ptrp = png_ptr; |
scroggo | 9a89a09 | 2016-05-31 13:52:47 -0700 | [diff] [blame] | 740 | } |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 741 | if (info_ptrp) { |
| 742 | *info_ptrp = info_ptr; |
| 743 | } |
| 744 | |
| 745 | if (outCodec) { |
| 746 | sk_sp<SkColorSpace> colorSpace = read_color_space(png_ptr, info_ptr); |
msarett | f34cd63 | 2016-05-25 10:13:53 -0700 | [diff] [blame] | 747 | if (!colorSpace) { |
| 748 | // Treat unmarked pngs as sRGB. |
| 749 | colorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named); |
| 750 | } |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 751 | |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 752 | SkEncodedInfo encodedInfo = SkEncodedInfo::Make(color, alpha, 8); |
| 753 | SkImageInfo imageInfo = encodedInfo.makeImageInfo(origWidth, origHeight, colorSpace); |
| 754 | |
| 755 | if (SkEncodedInfo::kOpaque_Alpha == alpha) { |
| 756 | png_color_8p sigBits; |
| 757 | if (png_get_sBIT(png_ptr, info_ptr, &sigBits)) { |
| 758 | if (5 == sigBits->red && 6 == sigBits->green && 5 == sigBits->blue) { |
| 759 | // Recommend a decode to 565 if the sBIT indicates 565. |
| 760 | imageInfo = imageInfo.makeColorType(kRGB_565_SkColorType); |
| 761 | } |
| 762 | } |
| 763 | } |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 764 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 765 | if (1 == numberPasses) { |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 766 | *outCodec = new SkPngNormalCodec(encodedInfo, imageInfo, stream, |
| 767 | chunkReader, png_ptr, info_ptr, bitDepth); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 768 | } else { |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 769 | *outCodec = new SkPngInterlacedCodec(encodedInfo, imageInfo, stream, |
| 770 | chunkReader, png_ptr, info_ptr, bitDepth, numberPasses); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 774 | return true; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 775 | } |
| 776 | |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 777 | SkPngCodec::SkPngCodec(const SkEncodedInfo& encodedInfo, const SkImageInfo& imageInfo, |
| 778 | SkStream* stream, SkPngChunkReader* chunkReader, png_structp png_ptr, |
| 779 | png_infop info_ptr, int bitDepth, int numberPasses) |
| 780 | : INHERITED(encodedInfo, imageInfo, stream) |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 781 | , fPngChunkReader(SkSafeRef(chunkReader)) |
| 782 | , fPng_ptr(png_ptr) |
| 783 | , fInfo_ptr(info_ptr) |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 784 | , fSwizzlerSrcRow(nullptr) |
| 785 | , fColorXformSrcRow(nullptr) |
msarett | 549ca32 | 2016-08-17 08:54:08 -0700 | [diff] [blame] | 786 | , fSrcRowBytes(imageInfo.width() * (bytes_per_pixel(this->getEncodedInfo().bitsPerPixel()))) |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 787 | , fNumberPasses(numberPasses) |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 788 | , fBitDepth(bitDepth) |
| 789 | {} |
| 790 | |
| 791 | SkPngCodec::~SkPngCodec() { |
| 792 | this->destroyReadStruct(); |
| 793 | } |
| 794 | |
| 795 | void SkPngCodec::destroyReadStruct() { |
| 796 | if (fPng_ptr) { |
| 797 | // We will never have a nullptr fInfo_ptr with a non-nullptr fPng_ptr |
| 798 | SkASSERT(fInfo_ptr); |
| 799 | png_destroy_read_struct(&fPng_ptr, &fInfo_ptr, nullptr); |
| 800 | fPng_ptr = nullptr; |
| 801 | fInfo_ptr = nullptr; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /////////////////////////////////////////////////////////////////////////////// |
| 806 | // Getting the pixels |
| 807 | /////////////////////////////////////////////////////////////////////////////// |
| 808 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 809 | bool SkPngCodec::initializeXforms(const SkImageInfo& dstInfo, const Options& options, |
| 810 | SkPMColor ctable[], int* ctableCount) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 811 | if (setjmp(png_jmpbuf(fPng_ptr))) { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 812 | SkCodecPrintf("Failed on png_read_update_info.\n"); |
| 813 | return false; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 814 | } |
| 815 | png_read_update_info(fPng_ptr, fInfo_ptr); |
| 816 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 817 | // It's important to reset fColorXform to nullptr. We don't do this on rewinding |
| 818 | // because the interlaced scanline decoder may need to rewind. |
| 819 | fColorXform = nullptr; |
| 820 | SkImageInfo swizzlerInfo = dstInfo; |
msarett | d331742 | 2016-08-22 13:00:05 -0700 | [diff] [blame^] | 821 | Options swizzlerOptions = options; |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 822 | bool needsColorXform = needs_color_xform(dstInfo, this->getInfo()); |
| 823 | if (needsColorXform) { |
| 824 | switch (dstInfo.colorType()) { |
| 825 | case kRGBA_8888_SkColorType: |
| 826 | case kBGRA_8888_SkColorType: |
| 827 | case kRGBA_F16_SkColorType: |
| 828 | swizzlerInfo = swizzlerInfo.makeColorType(kRGBA_8888_SkColorType); |
| 829 | if (kPremul_SkAlphaType == dstInfo.alphaType()) { |
| 830 | swizzlerInfo = swizzlerInfo.makeAlphaType(kUnpremul_SkAlphaType); |
| 831 | } |
| 832 | break; |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 833 | case kIndex_8_SkColorType: |
| 834 | break; |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 835 | default: |
| 836 | return false; |
| 837 | } |
| 838 | |
| 839 | fColorXform = SkColorSpaceXform::New(sk_ref_sp(this->getInfo().colorSpace()), |
| 840 | sk_ref_sp(dstInfo.colorSpace())); |
| 841 | |
| 842 | if (!fColorXform && kRGBA_F16_SkColorType == dstInfo.colorType()) { |
| 843 | return false; |
| 844 | } |
msarett | d331742 | 2016-08-22 13:00:05 -0700 | [diff] [blame^] | 845 | |
| 846 | // When there is a color xform, we swizzle into temporary memory, which is not |
| 847 | // zero initialized. |
| 848 | // FIXME (msarett): |
| 849 | // Is this a problem? |
| 850 | swizzlerOptions.fZeroInitialized = kNo_ZeroInitialized; |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 851 | } |
| 852 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 853 | if (SkEncodedInfo::kPalette_Color == this->getEncodedInfo().color()) { |
msarett | dcd5e65 | 2016-08-22 08:48:40 -0700 | [diff] [blame] | 854 | if (!this->createColorTable(dstInfo, ctableCount)) { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 855 | return false; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
| 859 | // Copy the color table to the client if they request kIndex8 mode |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 860 | copy_color_table(swizzlerInfo, fColorTable, ctable, ctableCount); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 861 | |
| 862 | // Create the swizzler. SkPngCodec retains ownership of the color table. |
| 863 | const SkPMColor* colors = get_color_ptr(fColorTable.get()); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 864 | fSwizzler.reset(SkSwizzler::CreateSwizzler(this->getEncodedInfo(), colors, swizzlerInfo, |
msarett | d331742 | 2016-08-22 13:00:05 -0700 | [diff] [blame^] | 865 | swizzlerOptions)); |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 866 | SkASSERT(fSwizzler); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 867 | return true; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 868 | } |
| 869 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 870 | bool SkPngCodec::onRewind() { |
| 871 | // This sets fPng_ptr and fInfo_ptr to nullptr. If read_header |
| 872 | // succeeds, they will be repopulated, and if it fails, they will |
| 873 | // remain nullptr. Any future accesses to fPng_ptr and fInfo_ptr will |
| 874 | // come through this function which will rewind and again attempt |
| 875 | // to reinitialize them. |
| 876 | this->destroyReadStruct(); |
| 877 | |
scroggo | 46c5747 | 2015-09-30 08:57:13 -0700 | [diff] [blame] | 878 | png_structp png_ptr; |
| 879 | png_infop info_ptr; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 880 | if (!read_header(this->stream(), fPngChunkReader.get(), nullptr, &png_ptr, &info_ptr)) { |
| 881 | return false; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 882 | } |
| 883 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 884 | fPng_ptr = png_ptr; |
| 885 | fInfo_ptr = info_ptr; |
| 886 | return true; |
| 887 | } |
msarett | 6a73821 | 2016-03-04 13:27:35 -0800 | [diff] [blame] | 888 | |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 889 | SkCodec::Result SkPngCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, |
| 890 | size_t rowBytes, const Options& options, |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 891 | SkPMColor ctable[], int* ctableCount, |
| 892 | int* rowsDecoded) { |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 893 | if (!png_conversion_possible(dstInfo, this->getInfo()) || |
| 894 | !this->initializeXforms(dstInfo, options, ctable, ctableCount)) |
| 895 | { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 896 | return kInvalidConversion; |
| 897 | } |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 898 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 899 | if (options.fSubset) { |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 900 | return kUnimplemented; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 901 | } |
| 902 | |
msarett | 35bb74b | 2016-08-22 07:41:28 -0700 | [diff] [blame] | 903 | this->allocateStorage(); |
msarett | d1ec89b | 2016-08-03 12:59:27 -0700 | [diff] [blame] | 904 | int count = this->readRows(dstInfo, dst, rowBytes, dstInfo.height(), 0); |
| 905 | if (count > dstInfo.height()) { |
| 906 | *rowsDecoded = count; |
| 907 | return kIncompleteInput; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 908 | } |
| 909 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 910 | return kSuccess; |
scroggo | 6fb2391 | 2016-06-02 14:16:43 -0700 | [diff] [blame] | 911 | } |
| 912 | |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 913 | uint32_t SkPngCodec::onGetFillValue(SkColorType colorType) const { |
| 914 | const SkPMColor* colorPtr = get_color_ptr(fColorTable.get()); |
| 915 | if (colorPtr) { |
| 916 | return get_color_table_fill_value(colorType, colorPtr, 0); |
| 917 | } |
| 918 | return INHERITED::onGetFillValue(colorType); |
| 919 | } |
| 920 | |
| 921 | SkCodec* SkPngCodec::NewFromStream(SkStream* stream, SkPngChunkReader* chunkReader) { |
| 922 | SkAutoTDelete<SkStream> streamDeleter(stream); |
| 923 | |
scroggo | d8d6855 | 2016-06-06 11:26:17 -0700 | [diff] [blame] | 924 | SkCodec* outCodec; |
msarett | ac6c750 | 2016-04-25 09:30:24 -0700 | [diff] [blame] | 925 | if (read_header(stream, chunkReader, &outCodec, nullptr, nullptr)) { |
| 926 | // Codec has taken ownership of the stream. |
| 927 | SkASSERT(outCodec); |
| 928 | streamDeleter.release(); |
| 929 | return outCodec; |
| 930 | } |
| 931 | |
| 932 | return nullptr; |
scroggo | 0524590 | 2015-03-25 11:11:52 -0700 | [diff] [blame] | 933 | } |