| 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 |
|
| 8 |
#include "src/codec/SkPngCodec.h" |
| 9 |
|
| 10 |
#include "include/codec/SkEncodedOrigin.h" |
| 11 |
#include "include/codec/SkPngChunkReader.h" |
| 12 |
#include "include/codec/SkPngDecoder.h" |
| 13 |
#include "include/core/SkColorSpace.h" |
| 14 |
#include "include/core/SkData.h" |
| 15 |
#include "include/core/SkImageInfo.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkSize.h" |
| 18 |
#include "include/core/SkSpan.h" |
| 19 |
#include "include/core/SkStream.h" |
| 20 |
#include "include/core/SkTypes.h" |
| 21 |
#include "include/private/SkEncodedInfo.h" |
| 22 |
#include "include/private/base/SkMalloc.h" |
| 23 |
#include "include/private/base/SkNoncopyable.h" |
| 24 |
#include "include/private/base/SkTemplates.h" |
| 25 |
#include "modules/skcms/skcms.h" |
| 26 |
#include "src/codec/SkCodecPriv.h" |
| 27 |
#include "src/codec/SkPngCompositeChunkReader.h" |
| 28 |
#include "src/codec/SkPngPriv.h" |
| 29 |
#include "src/codec/SkSwizzler.h" |
| 30 |
|
| 31 |
#include <csetjmp> |
| 32 |
#include <algorithm> |
| 33 |
#include <cstring> |
| 34 |
#include <utility> |
| 35 |
|
| 36 |
#include <png.h> |
| 37 |
#include <pngconf.h> |
| 38 |
|
| 39 |
using namespace skia_private; |
| 40 |
|
| 41 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 42 |
#include "include/android/SkAndroidFrameworkUtils.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
// This warning triggers false positives way too often in here. |
| 46 |
#if defined(__GNUC__) && !defined(__clang__) |
| 47 |
#pragma GCC diagnostic ignored "-Wclobbered" |
| 48 |
#endif |
| 49 |
|
| 50 |
// FIXME (scroggo): We can use png_jumpbuf directly once Google3 is on 1.6 |
| 51 |
#define PNG_JMPBUF(x) png_jmpbuf((png_structp) x) |
| 52 |
|
| 53 |
/////////////////////////////////////////////////////////////////////////////// |
| 54 |
// Callback functions |
| 55 |
/////////////////////////////////////////////////////////////////////////////// |
| 56 |
|
| 57 |
// When setjmp is first called, it returns 0, meaning longjmp was not called. |
| 58 |
constexpr int kSetJmpOkay = 0; |
| 59 |
// An error internal to libpng. |
| 60 |
constexpr int kPngError = 1; |
| 61 |
// Passed to longjmp when we have decoded as many lines as we need. |
| 62 |
constexpr int kStopDecoding = 2; |
| 63 |
|
| 64 |
static void sk_error_fn(png_structp png_ptr, png_const_charp msg) { |
| 65 |
SkCodecPrintf("------ png error %s\n", msg); |
| 66 |
longjmp(PNG_JMPBUF(png_ptr), kPngError); |
| 67 |
} |
| 68 |
|
| 69 |
void sk_warning_fn(png_structp, png_const_charp msg) { |
| 70 |
SkCodecPrintf("----- png warning %s\n", msg); |
| 71 |
} |
| 72 |
|
| 73 |
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 74 |
static int sk_read_user_chunk(png_structp png_ptr, png_unknown_chunkp chunk) { |
| 75 |
SkPngChunkReader* chunkReader = (SkPngChunkReader*)png_get_user_chunk_ptr(png_ptr); |
| 76 |
// readChunk() returning true means continue decoding |
| 77 |
return chunkReader->readChunk((const char*)chunk->name, chunk->data, chunk->size) ? 1 : -1; |
| 78 |
} |
| 79 |
#endif |
| 80 |
|
| 81 |
/////////////////////////////////////////////////////////////////////////////// |
| 82 |
// Helpers |
| 83 |
/////////////////////////////////////////////////////////////////////////////// |
| 84 |
|
| 85 |
class AutoCleanPng : public SkNoncopyable { |
| 86 |
public: |
| 87 |
/* |
| 88 |
* This class does not take ownership of stream or reader, but if codecPtr |
| 89 |
* is non-NULL, and decodeBounds succeeds, it will have created a new |
| 90 |
* SkCodec (pointed to by *codecPtr) which will own/ref them, as well as |
| 91 |
* the png_ptr and info_ptr. |
| 92 |
*/ |
| 93 |
AutoCleanPng(png_structp png_ptr, |
| 94 |
SkStream* stream, |
| 95 |
sk_sp<SkPngCompositeChunkReader> reader, |
| 96 |
SkCodec** codecPtr) |
| 97 |
: fPng_ptr(png_ptr) |
| 98 |
, fInfo_ptr(nullptr) |
| 99 |
, fStream(stream) |
| 100 |
, fChunkReader(std::move(reader)) |
| 101 |
, fOutCodec(codecPtr) {} |
| 102 |
|
| 103 |
~AutoCleanPng() { |
| 104 |
// fInfo_ptr will never be non-nullptr unless fPng_ptr is. |
| 105 |
if (fPng_ptr) { |
| 106 |
png_infopp info_pp = fInfo_ptr ? &fInfo_ptr : nullptr; |
| 107 |
png_destroy_read_struct(&fPng_ptr, info_pp, nullptr); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
void setInfoPtr(png_infop info_ptr) { |
| 112 |
SkASSERT(nullptr == fInfo_ptr); |
| 113 |
fInfo_ptr = info_ptr; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Reads enough of the input stream to decode the bounds. |
| 118 |
* @return false if the stream is not a valid PNG (or too short). |
| 119 |
* true if it read enough of the stream to determine the bounds. |
| 120 |
* In the latter case, the stream may have been read beyond the |
| 121 |
* point to determine the bounds, and the png_ptr will have saved |
| 122 |
* any extra data. Further, if the codecPtr supplied to the |
| 123 |
* constructor was not NULL, it will now point to a new SkCodec, |
| 124 |
* which owns (or refs, in the case of the SkPngChunkReader) the |
| 125 |
* inputs. If codecPtr was NULL, the png_ptr and info_ptr are |
| 126 |
* unowned, and it is up to the caller to destroy them. |
| 127 |
*/ |
| 128 |
bool decodeBounds(); |
| 129 |
|
| 130 |
private: |
| 131 |
png_structp fPng_ptr; |
| 132 |
png_infop fInfo_ptr; |
| 133 |
SkStream* fStream; |
| 134 |
sk_sp<SkPngCompositeChunkReader> fChunkReader; |
| 135 |
SkCodec** fOutCodec; |
| 136 |
|
| 137 |
void infoCallback(size_t idatLength); |
| 138 |
|
| 139 |
void releasePngPtrs() { |
| 140 |
fPng_ptr = nullptr; |
| 141 |
fInfo_ptr = nullptr; |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
static inline bool is_chunk(const png_byte* chunk, const char* tag) { |
| 146 |
return memcmp(chunk + 4, tag, 4) == 0; |
| 147 |
} |
| 148 |
|
| 149 |
static inline bool process_data(png_structp png_ptr, png_infop info_ptr, |
| 150 |
SkStream* stream, void* buffer, size_t bufferSize, size_t length) { |
| 151 |
while (length > 0) { |
| 152 |
const size_t bytesToProcess = std::min(bufferSize, length); |
| 153 |
const size_t bytesRead = stream->read(buffer, bytesToProcess); |
| 154 |
png_process_data(png_ptr, info_ptr, (png_bytep) buffer, bytesRead); |
| 155 |
if (bytesRead < bytesToProcess) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
length -= bytesToProcess; |
| 159 |
} |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
bool AutoCleanPng::decodeBounds() { |
| 164 |
SkASSERT(fStream); |
| 165 |
if (setjmp(PNG_JMPBUF(fPng_ptr))) { |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
png_set_progressive_read_fn(fPng_ptr, nullptr, nullptr, nullptr, nullptr); |
| 170 |
|
| 171 |
// Arbitrary buffer size, though note that it matches (below) |
| 172 |
// SkPngCodec::processData(). FIXME: Can we better suit this to the size of |
| 173 |
// the PNG header? |
| 174 |
constexpr size_t kBufferSize = 4096; |
| 175 |
char buffer[kBufferSize]; |
| 176 |
|
| 177 |
{ |
| 178 |
// Parse the signature. |
| 179 |
if (fStream->read(buffer, 8) < 8) { |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
png_process_data(fPng_ptr, fInfo_ptr, (png_bytep) buffer, 8); |
| 184 |
} |
| 185 |
|
| 186 |
while (true) { |
| 187 |
// Parse chunk length and type. |
| 188 |
if (fStream->read(buffer, 8) < 8) { |
| 189 |
// We have read to the end of the input without decoding bounds. |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
png_byte* chunk = reinterpret_cast<png_byte*>(buffer); |
| 194 |
const size_t length = png_get_uint_32(chunk); |
| 195 |
|
| 196 |
if (is_chunk(chunk, "IDAT")) { |
| 197 |
this->infoCallback(length); |
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
png_process_data(fPng_ptr, fInfo_ptr, chunk, 8); |
| 202 |
// Process the full chunk + CRC. |
| 203 |
if (!process_data(fPng_ptr, fInfo_ptr, fStream, buffer, kBufferSize, length + 4)) { |
| 204 |
return false; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return false; |
| 209 |
} |
| 210 |
|
| 211 |
bool SkPngCodec::processData() { |
| 212 |
switch (setjmp(PNG_JMPBUF(fPng_ptr))) { |
| 213 |
case kPngError: |
| 214 |
// There was an error. Stop processing data. |
| 215 |
// FIXME: Do we need to discard png_ptr? |
| 216 |
return false; |
| 217 |
case kStopDecoding: |
| 218 |
// We decoded all the lines we want. |
| 219 |
return true; |
| 220 |
case kSetJmpOkay: |
| 221 |
// Everything is okay. |
| 222 |
break; |
| 223 |
default: |
| 224 |
// No other values should be passed to longjmp. |
| 225 |
SkASSERT(false); |
| 226 |
} |
| 227 |
|
| 228 |
// Arbitrary buffer size |
| 229 |
constexpr size_t kBufferSize = 4096; |
| 230 |
char buffer[kBufferSize]; |
| 231 |
|
| 232 |
bool iend = false; |
| 233 |
while (true) { |
| 234 |
size_t length; |
| 235 |
if (fDecodedIdat) { |
| 236 |
// Parse chunk length and type. |
| 237 |
if (this->stream()->read(buffer, 8) < 8) { |
| 238 |
break; |
| 239 |
} |
| 240 |
|
| 241 |
png_byte* chunk = reinterpret_cast<png_byte*>(buffer); |
| 242 |
png_process_data(fPng_ptr, fInfo_ptr, chunk, 8); |
| 243 |
if (is_chunk(chunk, "IEND")) { |
| 244 |
iend = true; |
| 245 |
} |
| 246 |
|
| 247 |
length = png_get_uint_32(chunk); |
| 248 |
} else { |
| 249 |
length = fIdatLength; |
| 250 |
png_byte idat[] = {0, 0, 0, 0, 'I', 'D', 'A', 'T'}; |
| 251 |
png_save_uint_32(idat, length); |
| 252 |
png_process_data(fPng_ptr, fInfo_ptr, idat, 8); |
| 253 |
fDecodedIdat = true; |
| 254 |
} |
| 255 |
|
| 256 |
// Process the full chunk + CRC. |
| 257 |
if (!process_data(fPng_ptr, fInfo_ptr, this->stream(), buffer, kBufferSize, length + 4) |
| 258 |
|| iend) { |
| 259 |
break; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
std::optional<SkSpan<const SkPngCodecBase::PaletteColorEntry>> SkPngCodec::onTryGetPlteChunk() { |
| 267 |
int numColors; |
| 268 |
png_color* palette; |
| 269 |
if (!png_get_PLTE(fPng_ptr, fInfo_ptr, &palette, &numColors)) { |
| 270 |
return std::nullopt; |
| 271 |
} |
| 272 |
|
| 273 |
static_assert(sizeof(png_color) == sizeof(PaletteColorEntry)); |
| 274 |
return SkSpan(reinterpret_cast<const PaletteColorEntry*>(palette), numColors); |
| 275 |
} |
| 276 |
|
| 277 |
std::optional<SkSpan<const uint8_t>> SkPngCodec::onTryGetTrnsChunk() { |
| 278 |
png_bytep alphas; |
| 279 |
int numColorsWithAlpha = 0; |
| 280 |
if (!png_get_tRNS(fPng_ptr, fInfo_ptr, &alphas, &numColorsWithAlpha, nullptr)) { |
| 281 |
return std::nullopt; |
| 282 |
} |
| 283 |
return SkSpan(alphas, numColorsWithAlpha); |
| 284 |
} |
| 285 |
|
| 286 |
/////////////////////////////////////////////////////////////////////////////// |
| 287 |
// Creation |
| 288 |
/////////////////////////////////////////////////////////////////////////////// |
| 289 |
|
| 290 |
bool SkPngCodec::IsPng(const void* buf, size_t bytesRead) { |
| 291 |
return !png_sig_cmp((png_const_bytep) buf, (png_size_t)0, bytesRead); |
| 292 |
} |
| 293 |
|
| 294 |
#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6) |
| 295 |
|
| 296 |
static float png_fixed_point_to_float(png_fixed_point x) { |
| 297 |
// We multiply by the same factor that libpng used to convert |
| 298 |
// fixed point -> double. Since we want floats, we choose to |
| 299 |
// do the conversion ourselves rather than convert |
| 300 |
// fixed point -> double -> float. |
| 301 |
return ((float) x) * 0.00001f; |
| 302 |
} |
| 303 |
|
| 304 |
static float png_inverted_fixed_point_to_float(png_fixed_point x) { |
| 305 |
// This is necessary because the gAMA chunk actually stores 1/gamma. |
| 306 |
return 1.0f / png_fixed_point_to_float(x); |
| 307 |
} |
| 308 |
|
| 309 |
#endif // LIBPNG >= 1.6 |
| 310 |
|
| 311 |
// If there is no color profile information, it will use sRGB. |
| 312 |
std::unique_ptr<SkCodecs::ColorProfile> read_color_profile( |
| 313 |
png_structp png_ptr, png_infop info_ptr) { |
| 314 |
|
| 315 |
#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 6) |
| 316 |
// First check for an ICC profile |
| 317 |
png_bytep profile; |
| 318 |
png_uint_32 length; |
| 319 |
// The below variables are unused, however, we need to pass them in anyway or |
| 320 |
// png_get_iCCP() will return nothing. |
| 321 |
// Could knowing the |name| of the profile ever be interesting? Maybe for debugging? |
| 322 |
png_charp name; |
| 323 |
// The |compression| is uninteresting since: |
| 324 |
// (1) libpng has already decompressed the profile for us. |
| 325 |
// (2) "deflate" is the only mode of decompression that libpng supports. |
| 326 |
int compression; |
| 327 |
if (PNG_INFO_iCCP == png_get_iCCP(png_ptr, info_ptr, &name, &compression, &profile, |
| 328 |
&length)) { |
| 329 |
auto data = SkData::MakeWithCopy(profile, length); |
| 330 |
return SkCodecs::ColorProfile::MakeICCProfile(std::move(data)); |
| 331 |
} |
| 332 |
|
| 333 |
// Second, check for sRGB. |
| 334 |
// Note that Blink does this first. This code checks ICC first, with the thinking that |
| 335 |
// an image has both truly wants the potentially more specific ICC chunk, with sRGB as a |
| 336 |
// backup in case the decoder does not support full color management. |
| 337 |
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) { |
| 338 |
// TODO(https://crbug.com/362304558): Consider the intent field from the |
| 339 |
// `sRGB` chunk. |
| 340 |
return nullptr; |
| 341 |
} |
| 342 |
|
| 343 |
// Default to SRGB gamut. |
| 344 |
skcms_Matrix3x3 toXYZD50 = SkNamedGamut::kSRGB; |
| 345 |
// Next, check for chromaticities. |
| 346 |
png_fixed_point chrm[8]; |
| 347 |
png_fixed_point gamma; |
| 348 |
if (png_get_cHRM_fixed(png_ptr, info_ptr, &chrm[0], &chrm[1], &chrm[2], &chrm[3], &chrm[4], |
| 349 |
&chrm[5], &chrm[6], &chrm[7])) |
| 350 |
{ |
| 351 |
float rx = png_fixed_point_to_float(chrm[2]); |
| 352 |
float ry = png_fixed_point_to_float(chrm[3]); |
| 353 |
float gx = png_fixed_point_to_float(chrm[4]); |
| 354 |
float gy = png_fixed_point_to_float(chrm[5]); |
| 355 |
float bx = png_fixed_point_to_float(chrm[6]); |
| 356 |
float by = png_fixed_point_to_float(chrm[7]); |
| 357 |
float wx = png_fixed_point_to_float(chrm[0]); |
| 358 |
float wy = png_fixed_point_to_float(chrm[1]); |
| 359 |
|
| 360 |
skcms_Matrix3x3 tmp; |
| 361 |
if (skcms_PrimariesToXYZD50(rx, ry, gx, gy, bx, by, wx, wy, &tmp)) { |
| 362 |
toXYZD50 = tmp; |
| 363 |
} else { |
| 364 |
// Note that Blink simply returns nullptr in this case. We'll fall |
| 365 |
// back to srgb. |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
skcms_TransferFunction fn; |
| 370 |
if (PNG_INFO_gAMA == png_get_gAMA_fixed(png_ptr, info_ptr, &gamma)) { |
| 371 |
fn.a = 1.0f; |
| 372 |
fn.b = fn.c = fn.d = fn.e = fn.f = 0.0f; |
| 373 |
fn.g = png_inverted_fixed_point_to_float(gamma); |
| 374 |
} else { |
| 375 |
// Default to sRGB gamma if the image has color space information, |
| 376 |
// but does not specify gamma. |
| 377 |
// Note that Blink would again return nullptr in this case. |
| 378 |
fn = *skcms_sRGB_TransferFunction(); |
| 379 |
} |
| 380 |
|
| 381 |
return SkCodecs::ColorProfile::Make(fn, toXYZD50); |
| 382 |
#else // LIBPNG >= 1.6 |
| 383 |
return nullptr; |
| 384 |
#endif // LIBPNG >= 1.6 |
| 385 |
} |
| 386 |
|
| 387 |
static SkCodec::Result log_and_return_error(bool success) { |
| 388 |
if (success) return SkCodec::kIncompleteInput; |
| 389 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 390 |
SkAndroidFrameworkUtils::SafetyNetLog("117838472"); |
| 391 |
#endif |
| 392 |
return SkCodec::kErrorInInput; |
| 393 |
} |
| 394 |
|
| 395 |
class SkPngNormalDecoder : public SkPngCodec { |
| 396 |
public: |
| 397 |
SkPngNormalDecoder(SkEncodedInfo&& info, |
| 398 |
std::unique_ptr<SkStream> stream, |
| 399 |
sk_sp<SkPngCompositeChunkReader> reader, |
| 400 |
png_structp png_ptr, |
| 401 |
png_infop info_ptr, |
| 402 |
std::unique_ptr<SkStream> gainmapStream, |
| 403 |
std::optional<SkGainmapInfo> gainmapInfo) |
| 404 |
: SkPngCodec(std::move(info), |
| 405 |
std::move(stream), |
| 406 |
std::move(reader), |
| 407 |
png_ptr, |
| 408 |
info_ptr, |
| 409 |
std::move(gainmapStream), |
| 410 |
gainmapInfo) |
| 411 |
, fRowsWrittenToOutput(0) |
| 412 |
, fDst(nullptr) |
| 413 |
, fRowBytes(0) |
| 414 |
, fFirstRow(0) |
| 415 |
, fLastRow(0) {} |
| 416 |
|
| 417 |
static void AllRowsCallback(png_structp png_ptr, png_bytep row, png_uint_32 rowNum, int /*pass*/) { |
| 418 |
GetDecoder(png_ptr)->allRowsCallback(row, rowNum); |
| 419 |
} |
| 420 |
|
| 421 |
static void RowCallback(png_structp png_ptr, png_bytep row, png_uint_32 rowNum, int /*pass*/) { |
| 422 |
GetDecoder(png_ptr)->rowCallback(row, rowNum); |
| 423 |
} |
| 424 |
|
| 425 |
private: |
| 426 |
int fRowsWrittenToOutput; |
| 427 |
void* fDst; |
| 428 |
size_t fRowBytes; |
| 429 |
|
| 430 |
// Variables for partial decode |
| 431 |
int fFirstRow; // FIXME: Move to baseclass? |
| 432 |
int fLastRow; |
| 433 |
int fRowsNeeded; |
| 434 |
|
| 435 |
static SkPngNormalDecoder* GetDecoder(png_structp png_ptr) { |
| 436 |
return static_cast<SkPngNormalDecoder*>(png_get_progressive_ptr(png_ptr)); |
| 437 |
} |
| 438 |
|
| 439 |
Result decodeAllRows(void* dst, size_t rowBytes, int* rowsDecoded) override { |
| 440 |
const int height = this->dimensions().height(); |
| 441 |
png_set_progressive_read_fn(this->png_ptr(), this, nullptr, AllRowsCallback, nullptr); |
| 442 |
fDst = dst; |
| 443 |
fRowBytes = rowBytes; |
| 444 |
|
| 445 |
fRowsWrittenToOutput = 0; |
| 446 |
fFirstRow = 0; |
| 447 |
fLastRow = height - 1; |
| 448 |
|
| 449 |
const bool success = this->processData(); |
| 450 |
if (success && fRowsWrittenToOutput == height) { |
| 451 |
return kSuccess; |
| 452 |
} |
| 453 |
|
| 454 |
if (rowsDecoded) { |
| 455 |
*rowsDecoded = fRowsWrittenToOutput; |
| 456 |
} |
| 457 |
|
| 458 |
return log_and_return_error(success); |
| 459 |
} |
| 460 |
|
| 461 |
void allRowsCallback(png_bytep row, int rowNum) { |
| 462 |
SkASSERT(rowNum == fRowsWrittenToOutput); |
| 463 |
fRowsWrittenToOutput++; |
| 464 |
this->applyXformRow(fDst, row); |
| 465 |
fDst = SkTAddOffset<void>(fDst, fRowBytes); |
| 466 |
} |
| 467 |
|
| 468 |
Result setRange(int firstRow, int lastRow, void* dst, size_t rowBytes) override { |
| 469 |
png_set_progressive_read_fn(this->png_ptr(), this, nullptr, RowCallback, nullptr); |
| 470 |
fFirstRow = firstRow; |
| 471 |
fLastRow = lastRow; |
| 472 |
fDst = dst; |
| 473 |
fRowBytes = rowBytes; |
| 474 |
fRowsWrittenToOutput = 0; |
| 475 |
fRowsNeeded = fLastRow - fFirstRow + 1; |
| 476 |
return kSuccess; |
| 477 |
} |
| 478 |
|
| 479 |
Result decode(int* rowsDecoded) override { |
| 480 |
if (this->swizzler()) { |
| 481 |
const int sampleY = this->swizzler()->sampleY(); |
| 482 |
fRowsNeeded = SkCodecPriv::GetSampledDimension(fLastRow - fFirstRow + 1, sampleY); |
| 483 |
} |
| 484 |
|
| 485 |
const bool success = this->processData(); |
| 486 |
if (success && fRowsWrittenToOutput == fRowsNeeded) { |
| 487 |
return kSuccess; |
| 488 |
} |
| 489 |
|
| 490 |
if (rowsDecoded) { |
| 491 |
*rowsDecoded = fRowsWrittenToOutput; |
| 492 |
} |
| 493 |
|
| 494 |
return log_and_return_error(success); |
| 495 |
} |
| 496 |
|
| 497 |
void rowCallback(png_bytep row, int rowNum) { |
| 498 |
if (rowNum < fFirstRow) { |
| 499 |
// Ignore this row. |
| 500 |
return; |
| 501 |
} |
| 502 |
|
| 503 |
SkASSERT(rowNum <= fLastRow); |
| 504 |
SkASSERT(fRowsWrittenToOutput < fRowsNeeded); |
| 505 |
|
| 506 |
// If there is no swizzler, all rows are needed. |
| 507 |
if (!this->swizzler() || this->swizzler()->rowNeeded(rowNum - fFirstRow)) { |
| 508 |
this->applyXformRow(fDst, row); |
| 509 |
fDst = SkTAddOffset<void>(fDst, fRowBytes); |
| 510 |
fRowsWrittenToOutput++; |
| 511 |
} |
| 512 |
|
| 513 |
if (fRowsWrittenToOutput == fRowsNeeded) { |
| 514 |
// Fake error to stop decoding scanlines. |
| 515 |
longjmp(PNG_JMPBUF(this->png_ptr()), kStopDecoding); |
| 516 |
} |
| 517 |
} |
| 518 |
}; |
| 519 |
|
| 520 |
class SkPngInterlacedDecoder : public SkPngCodec { |
| 521 |
public: |
| 522 |
SkPngInterlacedDecoder(SkEncodedInfo&& info, |
| 523 |
std::unique_ptr<SkStream> stream, |
| 524 |
sk_sp<SkPngCompositeChunkReader> reader, |
| 525 |
png_structp png_ptr, |
| 526 |
png_infop info_ptr, |
| 527 |
int numberPasses, |
| 528 |
std::unique_ptr<SkStream> gainmapStream, |
| 529 |
std::optional<SkGainmapInfo> gainmapInfo) |
| 530 |
: SkPngCodec(std::move(info), |
| 531 |
std::move(stream), |
| 532 |
std::move(reader), |
| 533 |
png_ptr, |
| 534 |
info_ptr, |
| 535 |
std::move(gainmapStream), |
| 536 |
gainmapInfo) |
| 537 |
, fNumberPasses(numberPasses) |
| 538 |
, fFirstRow(0) |
| 539 |
, fLastRow(0) |
| 540 |
, fLinesDecoded(0) |
| 541 |
, fInterlacedComplete(false) |
| 542 |
, fPng_rowbytes(0) {} |
| 543 |
|
| 544 |
static void InterlacedRowCallback(png_structp png_ptr, png_bytep row, png_uint_32 rowNum, int pass) { |
| 545 |
auto decoder = static_cast<SkPngInterlacedDecoder*>(png_get_progressive_ptr(png_ptr)); |
| 546 |
decoder->interlacedRowCallback(row, rowNum, pass); |
| 547 |
} |
| 548 |
|
| 549 |
private: |
| 550 |
const int fNumberPasses; |
| 551 |
int fFirstRow; |
| 552 |
int fLastRow; |
| 553 |
void* fDst; |
| 554 |
size_t fRowBytes; |
| 555 |
int fLinesDecoded; |
| 556 |
bool fInterlacedComplete; |
| 557 |
size_t fPng_rowbytes; |
| 558 |
std::unique_ptr<png_byte, SkOverloadedFunctionObject<void(void*), sk_free>> fInterlaceBuffer; |
| 559 |
|
| 560 |
// FIXME: Currently sharing interlaced callback for all rows and subset. It's not |
| 561 |
// as expensive as the subset version of non-interlaced, but it still does extra |
| 562 |
// work. |
| 563 |
void interlacedRowCallback(png_bytep row, int rowNum, int pass) { |
| 564 |
if (rowNum < fFirstRow || rowNum > fLastRow || fInterlacedComplete) { |
| 565 |
// Ignore this row |
| 566 |
return; |
| 567 |
} |
| 568 |
|
| 569 |
png_bytep oldRow = fInterlaceBuffer.get() + (rowNum - fFirstRow) * fPng_rowbytes; |
| 570 |
png_progressive_combine_row(this->png_ptr(), oldRow, row); |
| 571 |
|
| 572 |
if (0 == pass) { |
| 573 |
// The first pass initializes all rows. |
| 574 |
SkASSERT(row); |
| 575 |
SkASSERT(fLinesDecoded == rowNum - fFirstRow); |
| 576 |
fLinesDecoded++; |
| 577 |
} else { |
| 578 |
SkASSERT(fLinesDecoded == fLastRow - fFirstRow + 1); |
| 579 |
if (fNumberPasses - 1 == pass && rowNum == fLastRow) { |
| 580 |
// Last pass, and we have read all of the rows we care about. |
| 581 |
fInterlacedComplete = true; |
| 582 |
if (fLastRow != this->dimensions().height() - 1 || |
| 583 |
(this->swizzler() && this->swizzler()->sampleY() != 1)) { |
| 584 |
// Fake error to stop decoding scanlines. Only stop if we're not decoding the |
| 585 |
// whole image, in which case processing the rest of the image might be |
| 586 |
// expensive. When decoding the whole image, read through the IEND chunk to |
| 587 |
// preserve Android behavior of leaving the input stream in the right place. |
| 588 |
longjmp(PNG_JMPBUF(this->png_ptr()), kStopDecoding); |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
Result decodeAllRows(void* dst, size_t rowBytes, int* rowsDecoded) override { |
| 595 |
const int height = this->dimensions().height(); |
| 596 |
Result res = this->setUpInterlaceBuffer(height); |
| 597 |
if (res != kSuccess) { |
| 598 |
return res; |
| 599 |
} |
| 600 |
png_set_progressive_read_fn(this->png_ptr(), this, nullptr, InterlacedRowCallback, |
| 601 |
nullptr); |
| 602 |
|
| 603 |
fFirstRow = 0; |
| 604 |
fLastRow = height - 1; |
| 605 |
fLinesDecoded = 0; |
| 606 |
|
| 607 |
const bool success = this->processData(); |
| 608 |
png_bytep srcRow = fInterlaceBuffer.get(); |
| 609 |
// FIXME: When resuming, this may rewrite rows that did not change. |
| 610 |
for (int rowNum = 0; rowNum < fLinesDecoded; rowNum++) { |
| 611 |
this->applyXformRow(dst, srcRow); |
| 612 |
dst = SkTAddOffset<void>(dst, rowBytes); |
| 613 |
srcRow = SkTAddOffset<png_byte>(srcRow, fPng_rowbytes); |
| 614 |
} |
| 615 |
if (success && fInterlacedComplete) { |
| 616 |
return kSuccess; |
| 617 |
} |
| 618 |
|
| 619 |
if (rowsDecoded) { |
| 620 |
*rowsDecoded = fLinesDecoded; |
| 621 |
} |
| 622 |
|
| 623 |
return log_and_return_error(success); |
| 624 |
} |
| 625 |
|
| 626 |
Result setRange(int firstRow, int lastRow, void* dst, size_t rowBytes) override { |
| 627 |
// FIXME: We could skip rows in the interlace buffer that we won't put in the output. |
| 628 |
Result res = this->setUpInterlaceBuffer(lastRow - firstRow + 1); |
| 629 |
if (res != kSuccess) { |
| 630 |
return res; |
| 631 |
} |
| 632 |
png_set_progressive_read_fn(this->png_ptr(), this, nullptr, InterlacedRowCallback, nullptr); |
| 633 |
fFirstRow = firstRow; |
| 634 |
fLastRow = lastRow; |
| 635 |
fDst = dst; |
| 636 |
fRowBytes = rowBytes; |
| 637 |
fLinesDecoded = 0; |
| 638 |
return kSuccess; |
| 639 |
} |
| 640 |
|
| 641 |
Result decode(int* rowsDecoded) override { |
| 642 |
const bool success = this->processData(); |
| 643 |
|
| 644 |
// Now apply Xforms on all the rows that were decoded. |
| 645 |
if (!fLinesDecoded) { |
| 646 |
if (rowsDecoded) { |
| 647 |
*rowsDecoded = 0; |
| 648 |
} |
| 649 |
return log_and_return_error(success); |
| 650 |
} |
| 651 |
|
| 652 |
const int sampleY = this->swizzler() ? this->swizzler()->sampleY() : 1; |
| 653 |
const int rowsNeeded = SkCodecPriv::GetSampledDimension(fLastRow - fFirstRow + 1, sampleY); |
| 654 |
|
| 655 |
// FIXME: For resuming interlace, we may swizzle a row that hasn't changed. But it |
| 656 |
// may be too tricky/expensive to handle that correctly. |
| 657 |
|
| 658 |
// Offset srcRow by SkCodecPriv::GetStartCoord rows. We do not need to account for |
| 659 |
// fFirstRow, since the first row in fInterlaceBuffer corresponds to fFirstRow. |
| 660 |
int srcRow = SkCodecPriv::GetStartCoord(sampleY); |
| 661 |
void* dst = fDst; |
| 662 |
int rowsWrittenToOutput = 0; |
| 663 |
while (rowsWrittenToOutput < rowsNeeded && srcRow < fLinesDecoded) { |
| 664 |
png_bytep src = SkTAddOffset<png_byte>(fInterlaceBuffer.get(), fPng_rowbytes * srcRow); |
| 665 |
this->applyXformRow(dst, src); |
| 666 |
dst = SkTAddOffset<void>(dst, fRowBytes); |
| 667 |
|
| 668 |
rowsWrittenToOutput++; |
| 669 |
srcRow += sampleY; |
| 670 |
} |
| 671 |
|
| 672 |
if (success && fInterlacedComplete) { |
| 673 |
return kSuccess; |
| 674 |
} |
| 675 |
|
| 676 |
if (rowsDecoded) { |
| 677 |
*rowsDecoded = rowsWrittenToOutput; |
| 678 |
} |
| 679 |
return log_and_return_error(success); |
| 680 |
} |
| 681 |
|
| 682 |
Result setUpInterlaceBuffer(int height) { |
| 683 |
fPng_rowbytes = png_get_rowbytes(this->png_ptr(), this->info_ptr()); |
| 684 |
size_t interlaceBufferSize = fPng_rowbytes * height; |
| 685 |
void* interlaceBufferRaw = nullptr; |
| 686 |
if (interlaceBufferSize) { |
| 687 |
interlaceBufferRaw = sk_malloc_canfail(interlaceBufferSize, sizeof(png_byte)); |
| 688 |
if (!interlaceBufferRaw) { |
| 689 |
return kInternalError; |
| 690 |
} |
| 691 |
} |
| 692 |
fInterlaceBuffer.reset(reinterpret_cast<png_byte*>(interlaceBufferRaw)); |
| 693 |
fInterlacedComplete = false; |
| 694 |
return kSuccess; |
| 695 |
} |
| 696 |
}; |
| 697 |
|
| 698 |
// Reads the header and initializes the output fields, if not NULL. |
| 699 |
// |
| 700 |
// @param stream Input data. Will be read to get enough information to properly |
| 701 |
// setup the codec. |
| 702 |
// @param chunkReader SkPngChunkReader, for reading unknown chunks. May be NULL. |
| 703 |
// If not NULL, png_ptr will hold an *unowned* pointer to it. The caller is |
| 704 |
// expected to continue to own it for the lifetime of the png_ptr. |
| 705 |
// @param outCodec Optional output variable. If non-NULL, will be set to a new |
| 706 |
// SkPngCodec on success. |
| 707 |
// @param png_ptrp Optional output variable. If non-NULL, will be set to a new |
| 708 |
// png_structp on success. |
| 709 |
// @param info_ptrp Optional output variable. If non-NULL, will be set to a new |
| 710 |
// png_infop on success; |
| 711 |
// @return if kSuccess, the caller is responsible for calling |
| 712 |
// png_destroy_read_struct(png_ptrp, info_ptrp). |
| 713 |
// Otherwise, the passed in fields (except stream) are unchanged. |
| 714 |
static SkCodec::Result read_header(SkStream* stream, const sk_sp<SkPngCompositeChunkReader>& chunkReader, |
| 715 |
SkCodec** outCodec, |
| 716 |
png_structp* png_ptrp, png_infop* info_ptrp) { |
| 717 |
// The image is known to be a PNG. Decode enough to know the SkImageInfo. |
| 718 |
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, |
| 719 |
sk_error_fn, sk_warning_fn); |
| 720 |
if (!png_ptr) { |
| 721 |
return SkCodec::kInternalError; |
| 722 |
} |
| 723 |
|
| 724 |
#ifdef PNG_SET_OPTION_SUPPORTED |
| 725 |
// This setting ensures that we display images with incorrect CMF bytes. |
| 726 |
// See crbug.com/807324. |
| 727 |
png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW, PNG_OPTION_ON); |
| 728 |
#endif |
| 729 |
|
| 730 |
AutoCleanPng autoClean(png_ptr, stream, chunkReader, outCodec); |
| 731 |
|
| 732 |
png_infop info_ptr = png_create_info_struct(png_ptr); |
| 733 |
if (info_ptr == nullptr) { |
| 734 |
return SkCodec::kInternalError; |
| 735 |
} |
| 736 |
|
| 737 |
autoClean.setInfoPtr(info_ptr); |
| 738 |
|
| 739 |
if (setjmp(PNG_JMPBUF(png_ptr))) { |
| 740 |
return SkCodec::kInvalidInput; |
| 741 |
} |
| 742 |
|
| 743 |
#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED |
| 744 |
// Hookup our chunkReader so we can see any user-chunks the caller may be interested in. |
| 745 |
// This needs to be installed before we read the png header. Android may store ninepatch |
| 746 |
// chunks in the header. |
| 747 |
if (chunkReader) { |
| 748 |
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, (png_const_bytep)"", 0); |
| 749 |
png_set_read_user_chunk_fn(png_ptr, (png_voidp)chunkReader.get(), sk_read_user_chunk); |
| 750 |
} |
| 751 |
#endif |
| 752 |
|
| 753 |
const bool decodedBounds = autoClean.decodeBounds(); |
| 754 |
|
| 755 |
if (!decodedBounds) { |
| 756 |
return SkCodec::kIncompleteInput; |
| 757 |
} |
| 758 |
|
| 759 |
// On success, decodeBounds releases ownership of png_ptr and info_ptr. |
| 760 |
if (png_ptrp) { |
| 761 |
*png_ptrp = png_ptr; |
| 762 |
} |
| 763 |
if (info_ptrp) { |
| 764 |
*info_ptrp = info_ptr; |
| 765 |
} |
| 766 |
|
| 767 |
// decodeBounds takes care of setting outCodec |
| 768 |
if (outCodec) { |
| 769 |
SkASSERT(*outCodec); |
| 770 |
} |
| 771 |
return SkCodec::kSuccess; |
| 772 |
} |
| 773 |
|
| 774 |
void AutoCleanPng::infoCallback(size_t idatLength) { |
| 775 |
png_uint_32 origWidth, origHeight; |
| 776 |
int bitDepth, encodedColorType; |
| 777 |
png_get_IHDR(fPng_ptr, fInfo_ptr, &origWidth, &origHeight, &bitDepth, |
| 778 |
&encodedColorType, nullptr, nullptr, nullptr); |
| 779 |
|
| 780 |
// TODO(https://crbug.com/359245096): Should we support 16-bits of precision |
| 781 |
// for gray images? |
| 782 |
if (bitDepth == 16 && (PNG_COLOR_TYPE_GRAY == encodedColorType || |
| 783 |
PNG_COLOR_TYPE_GRAY_ALPHA == encodedColorType)) { |
| 784 |
bitDepth = 8; |
| 785 |
png_set_strip_16(fPng_ptr); |
| 786 |
} |
| 787 |
|
| 788 |
// Now determine the default colorType and alphaType and set the required transforms. |
| 789 |
// Often, we depend on SkSwizzler to perform any transforms that we need. However, we |
| 790 |
// still depend on libpng for many of the rare and PNG-specific cases. |
| 791 |
SkEncodedInfo::Color color; |
| 792 |
SkEncodedInfo::Alpha alpha; |
| 793 |
switch (encodedColorType) { |
| 794 |
case PNG_COLOR_TYPE_PALETTE: |
| 795 |
// Extract multiple pixels with bit depths of 1, 2, and 4 from a single |
| 796 |
// byte into separate bytes (useful for paletted and grayscale images). |
| 797 |
if (bitDepth < 8) { |
| 798 |
// TODO: Should we use SkSwizzler here? |
| 799 |
bitDepth = 8; |
| 800 |
png_set_packing(fPng_ptr); |
| 801 |
} |
| 802 |
|
| 803 |
color = SkEncodedInfo::kPalette_Color; |
| 804 |
// Set the alpha depending on if a transparency chunk exists. |
| 805 |
alpha = png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS) ? |
| 806 |
SkEncodedInfo::kUnpremul_Alpha : SkEncodedInfo::kOpaque_Alpha; |
| 807 |
break; |
| 808 |
case PNG_COLOR_TYPE_RGB: |
| 809 |
if (png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS)) { |
| 810 |
// Convert to RGBA if transparency chunk exists. |
| 811 |
png_set_tRNS_to_alpha(fPng_ptr); |
| 812 |
color = SkEncodedInfo::kRGBA_Color; |
| 813 |
alpha = SkEncodedInfo::kBinary_Alpha; |
| 814 |
} else { |
| 815 |
color = SkEncodedInfo::kRGB_Color; |
| 816 |
alpha = SkEncodedInfo::kOpaque_Alpha; |
| 817 |
} |
| 818 |
break; |
| 819 |
case PNG_COLOR_TYPE_GRAY: |
| 820 |
// Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel. |
| 821 |
if (bitDepth < 8) { |
| 822 |
// TODO: Should we use SkSwizzler here? |
| 823 |
bitDepth = 8; |
| 824 |
png_set_expand_gray_1_2_4_to_8(fPng_ptr); |
| 825 |
} |
| 826 |
|
| 827 |
if (png_get_valid(fPng_ptr, fInfo_ptr, PNG_INFO_tRNS)) { |
| 828 |
png_set_tRNS_to_alpha(fPng_ptr); |
| 829 |
color = SkEncodedInfo::kGrayAlpha_Color; |
| 830 |
alpha = SkEncodedInfo::kBinary_Alpha; |
| 831 |
} else { |
| 832 |
color = SkEncodedInfo::kGray_Color; |
| 833 |
alpha = SkEncodedInfo::kOpaque_Alpha; |
| 834 |
} |
| 835 |
break; |
| 836 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 837 |
color = SkEncodedInfo::kGrayAlpha_Color; |
| 838 |
alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 839 |
break; |
| 840 |
case PNG_COLOR_TYPE_RGBA: |
| 841 |
color = SkEncodedInfo::kRGBA_Color; |
| 842 |
alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 843 |
break; |
| 844 |
default: |
| 845 |
// All the color types have been covered above. |
| 846 |
SkASSERT(false); |
| 847 |
color = SkEncodedInfo::kRGBA_Color; |
| 848 |
alpha = SkEncodedInfo::kUnpremul_Alpha; |
| 849 |
} |
| 850 |
|
| 851 |
const int numberPasses = png_set_interlace_handling(fPng_ptr); |
| 852 |
|
| 853 |
if (fOutCodec) { |
| 854 |
SkASSERT(nullptr == *fOutCodec); |
| 855 |
auto profile = read_color_profile(fPng_ptr, fInfo_ptr); |
| 856 |
if (!SkPngCodecBase::isCompatibleColorProfileAndType(profile.get(), color)) { |
| 857 |
profile = nullptr; |
| 858 |
} |
| 859 |
|
| 860 |
switch (encodedColorType) { |
| 861 |
case PNG_COLOR_TYPE_GRAY_ALPHA:{ |
| 862 |
png_color_8p sigBits; |
| 863 |
if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) { |
| 864 |
if (8 == sigBits->alpha && kGraySigBit_GrayAlphaIsJustAlpha == sigBits->gray) { |
| 865 |
color = SkEncodedInfo::kXAlpha_Color; |
| 866 |
} |
| 867 |
} |
| 868 |
break; |
| 869 |
} |
| 870 |
case PNG_COLOR_TYPE_RGB:{ |
| 871 |
png_color_8p sigBits; |
| 872 |
if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) { |
| 873 |
if (5 == sigBits->red && 6 == sigBits->green && 5 == sigBits->blue) { |
| 874 |
// Recommend a decode to 565 if the sBIT indicates 565. |
| 875 |
color = SkEncodedInfo::k565_Color; |
| 876 |
} |
| 877 |
} |
| 878 |
break; |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
#ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 883 |
if (encodedColorType != PNG_COLOR_TYPE_GRAY_ALPHA |
| 884 |
&& SkEncodedInfo::kOpaque_Alpha == alpha) { |
| 885 |
png_color_8p sigBits; |
| 886 |
if (png_get_sBIT(fPng_ptr, fInfo_ptr, &sigBits)) { |
| 887 |
if (5 == sigBits->red && 6 == sigBits->green && 5 == sigBits->blue) { |
| 888 |
SkAndroidFrameworkUtils::SafetyNetLog("190188264"); |
| 889 |
} |
| 890 |
} |
| 891 |
} |
| 892 |
#endif // SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 893 |
|
| 894 |
SkEncodedInfo encodedInfo = SkEncodedInfo::Make( |
| 895 |
origWidth, origHeight, color, alpha, bitDepth, bitDepth, std::move(profile), |
| 896 |
fChunkReader ? fChunkReader->getHdrMetadata() : skhdr::Metadata::MakeEmpty()); |
| 897 |
if (1 == numberPasses) { |
| 898 |
*fOutCodec = new SkPngNormalDecoder(std::move(encodedInfo), |
| 899 |
std::unique_ptr<SkStream>(fStream), |
| 900 |
fChunkReader, |
| 901 |
fPng_ptr, |
| 902 |
fInfo_ptr, |
| 903 |
fChunkReader->takeGaimapStream(), |
| 904 |
fChunkReader->getGainmapInfo()); |
| 905 |
} else { |
| 906 |
*fOutCodec = new SkPngInterlacedDecoder(std::move(encodedInfo), |
| 907 |
std::unique_ptr<SkStream>(fStream), |
| 908 |
fChunkReader, |
| 909 |
fPng_ptr, |
| 910 |
fInfo_ptr, |
| 911 |
numberPasses, |
| 912 |
fChunkReader->takeGaimapStream(), |
| 913 |
fChunkReader->getGainmapInfo()); |
| 914 |
} |
| 915 |
static_cast<SkPngCodec*>(*fOutCodec)->setIdatLength(idatLength); |
| 916 |
} |
| 917 |
|
| 918 |
// Release the pointers, which are now owned by the codec or the caller is expected to |
| 919 |
// take ownership. |
| 920 |
this->releasePngPtrs(); |
| 921 |
} |
| 922 |
|
| 923 |
// TODO(https://crbug.com/390707316): Consider adding handling of eXIF chunks |
| 924 |
// for parity with Blink. |
| 925 |
constexpr SkEncodedOrigin kDefaultEncodedOrigin = kTopLeft_SkEncodedOrigin; |
| 926 |
|
| 927 |
SkPngCodec::SkPngCodec(SkEncodedInfo&& encodedInfo, |
| 928 |
std::unique_ptr<SkStream> stream, |
| 929 |
sk_sp<SkPngCompositeChunkReader> chunkReader, |
| 930 |
void* png_ptr, |
| 931 |
void* info_ptr, |
| 932 |
std::unique_ptr<SkStream> gainmapStream, |
| 933 |
std::optional<SkGainmapInfo> gainmapInfo) |
| 934 |
: SkPngCodecBase(std::move(encodedInfo), std::move(stream), kDefaultEncodedOrigin) |
| 935 |
, fPngChunkReader(std::move(chunkReader)) |
| 936 |
, fPng_ptr(png_ptr) |
| 937 |
, fInfo_ptr(info_ptr) |
| 938 |
, fIdatLength(0) |
| 939 |
, fDecodedIdat(false) |
| 940 |
, fGainmapStream(std::move(gainmapStream)) |
| 941 |
, fGainmapInfo(gainmapInfo) {} |
| 942 |
|
| 943 |
SkPngCodec::~SkPngCodec() { |
| 944 |
this->destroyReadStruct(); |
| 945 |
} |
| 946 |
|
| 947 |
void SkPngCodec::destroyReadStruct() { |
| 948 |
if (fPng_ptr) { |
| 949 |
// We will never have a nullptr fInfo_ptr with a non-nullptr fPng_ptr |
| 950 |
SkASSERT(fInfo_ptr); |
| 951 |
png_destroy_read_struct((png_struct**)&fPng_ptr, (png_info**)&fInfo_ptr, nullptr); |
| 952 |
fPng_ptr = nullptr; |
| 953 |
fInfo_ptr = nullptr; |
| 954 |
} |
| 955 |
} |
| 956 |
|
| 957 |
/////////////////////////////////////////////////////////////////////////////// |
| 958 |
// Getting the pixels |
| 959 |
/////////////////////////////////////////////////////////////////////////////// |
| 960 |
|
| 961 |
SkCodec::Result SkPngCodec::initializeXforms(const SkImageInfo& dstInfo, const Options& options) { |
| 962 |
if (setjmp(PNG_JMPBUF((png_struct*)fPng_ptr))) { |
| 963 |
SkCodecPrintf("Failed on png_read_update_info.\n"); |
| 964 |
return kInvalidInput; |
| 965 |
} |
| 966 |
png_read_update_info(fPng_ptr, fInfo_ptr); |
| 967 |
|
| 968 |
// `SkPngCodec` doesn't support APNG - the `frameWidth` is always the same |
| 969 |
// as the full image width. |
| 970 |
int frameWidth = dstInfo.width(); |
| 971 |
|
| 972 |
return SkPngCodecBase::initializeXforms(dstInfo, options, frameWidth); |
| 973 |
} |
| 974 |
|
| 975 |
bool SkPngCodec::onRewind() { |
| 976 |
if (!this->rewindStream()) { |
| 977 |
return false; |
| 978 |
} |
| 979 |
// This sets fPng_ptr and fInfo_ptr to nullptr. If read_header |
| 980 |
// succeeds, they will be repopulated, and if it fails, they will |
| 981 |
// remain nullptr. Any future accesses to fPng_ptr and fInfo_ptr will |
| 982 |
// come through this function which will rewind and again attempt |
| 983 |
// to reinitialize them. |
| 984 |
this->destroyReadStruct(); |
| 985 |
|
| 986 |
png_structp png_ptr; |
| 987 |
png_infop info_ptr; |
| 988 |
if (kSuccess != read_header(this->stream(), fPngChunkReader, nullptr, |
| 989 |
&png_ptr, &info_ptr)) { |
| 990 |
return false; |
| 991 |
} |
| 992 |
|
| 993 |
fPng_ptr = png_ptr; |
| 994 |
fInfo_ptr = info_ptr; |
| 995 |
fDecodedIdat = false; |
| 996 |
return true; |
| 997 |
} |
| 998 |
|
| 999 |
SkCodec::Result SkPngCodec::onGetPixels(const SkImageInfo& dstInfo, void* dst, |
| 1000 |
size_t rowBytes, const Options& options, |
| 1001 |
int* rowsDecoded) { |
| 1002 |
Result result = this->initializeXforms(dstInfo, options); |
| 1003 |
if (kSuccess != result) { |
| 1004 |
return result; |
| 1005 |
} |
| 1006 |
|
| 1007 |
if (options.fSubset) { |
| 1008 |
return kUnimplemented; |
| 1009 |
} |
| 1010 |
|
| 1011 |
this->initializeXformParams(); |
| 1012 |
return this->decodeAllRows(dst, rowBytes, rowsDecoded); |
| 1013 |
} |
| 1014 |
|
| 1015 |
SkCodec::Result SkPngCodec::onStartIncrementalDecode(const SkImageInfo& dstInfo, |
| 1016 |
void* dst, size_t rowBytes, const SkCodec::Options& options) { |
| 1017 |
Result result = this->initializeXforms(dstInfo, options); |
| 1018 |
if (kSuccess != result) { |
| 1019 |
return result; |
| 1020 |
} |
| 1021 |
|
| 1022 |
int firstRow, lastRow; |
| 1023 |
if (options.fSubset) { |
| 1024 |
firstRow = options.fSubset->top(); |
| 1025 |
lastRow = options.fSubset->bottom() - 1; |
| 1026 |
} else { |
| 1027 |
firstRow = 0; |
| 1028 |
lastRow = dstInfo.height() - 1; |
| 1029 |
} |
| 1030 |
return this->setRange(firstRow, lastRow, dst, rowBytes); |
| 1031 |
} |
| 1032 |
|
| 1033 |
SkCodec::Result SkPngCodec::onIncrementalDecode(int* rowsDecoded) { |
| 1034 |
// FIXME: Only necessary on the first call. |
| 1035 |
this->initializeXformParams(); |
| 1036 |
|
| 1037 |
return this->decode(rowsDecoded); |
| 1038 |
} |
| 1039 |
|
| 1040 |
std::unique_ptr<SkCodec> SkPngCodec::MakeFromStream(std::unique_ptr<SkStream> stream, |
| 1041 |
Result* result, SkPngChunkReader* chunkReader) { |
| 1042 |
SkASSERT(result); |
| 1043 |
if (!stream) { |
| 1044 |
*result = SkCodec::kInvalidInput; |
| 1045 |
return nullptr; |
| 1046 |
} |
| 1047 |
SkCodec* outCodec = nullptr; |
| 1048 |
auto compositeReader = sk_make_sp<SkPngCompositeChunkReader>(chunkReader); |
| 1049 |
*result = read_header(stream.get(), compositeReader, &outCodec, nullptr, nullptr); |
| 1050 |
if (kSuccess == *result) { |
| 1051 |
// Codec has taken ownership of the stream. |
| 1052 |
SkASSERT(outCodec); |
| 1053 |
stream.release(); |
| 1054 |
} |
| 1055 |
return std::unique_ptr<SkCodec>(outCodec); |
| 1056 |
} |
| 1057 |
|
| 1058 |
bool SkPngCodec::onGetGainmapCodec(SkGainmapInfo* info, std::unique_ptr<SkCodec>* gainmapCodec) { |
| 1059 |
if (!fGainmapStream) { |
| 1060 |
return false; |
| 1061 |
} |
| 1062 |
|
| 1063 |
sk_sp<const SkData> data = fGainmapStream->getData(); |
| 1064 |
if (!data) { |
| 1065 |
return false; |
| 1066 |
} |
| 1067 |
|
| 1068 |
if (!SkPngDecoder::IsPng(data->bytes(), data->size())) { |
| 1069 |
return false; |
| 1070 |
} |
| 1071 |
|
| 1072 |
// The gainmap information lives on the gainmap image itself, so we need to |
| 1073 |
// create the gainmap codec first, then check if it has a metadata chunk. |
| 1074 |
SkCodec::Result result; |
| 1075 |
std::unique_ptr<SkCodec> codec = |
| 1076 |
SkPngCodec::MakeFromStream(fGainmapStream->duplicate(), &result, fPngChunkReader.get()); |
| 1077 |
|
| 1078 |
if (result != SkCodec::Result::kSuccess) { |
| 1079 |
return false; |
| 1080 |
} |
| 1081 |
|
| 1082 |
bool hasInfo = codec->onGetGainmapInfo(info); |
| 1083 |
|
| 1084 |
if (hasInfo && gainmapCodec) { |
| 1085 |
// The ISO gainmap payload does not contain the actual alterative image |
| 1086 |
// primaries, so we need to query the ICC profile stored on the gainmap. |
| 1087 |
if (info->fGainmapMathColorSpace) { |
| 1088 |
const auto* colorProfile = codec->getEncodedInfo().colorProfile(); |
| 1089 |
if (colorProfile) { |
| 1090 |
auto colorSpace = colorProfile->getExactColorSpace(); |
| 1091 |
if (colorSpace) { |
| 1092 |
info->fGainmapMathColorSpace = std::move(colorSpace); |
| 1093 |
} |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
*gainmapCodec = std::move(codec); |
| 1098 |
} |
| 1099 |
|
| 1100 |
return hasInfo; |
| 1101 |
} |
| 1102 |
|
| 1103 |
bool SkPngCodec::onGetGainmapInfo(SkGainmapInfo* info) { |
| 1104 |
if (fGainmapInfo) { |
| 1105 |
if (info) { |
| 1106 |
*info = *fGainmapInfo; |
| 1107 |
} |
| 1108 |
return true; |
| 1109 |
} |
| 1110 |
|
| 1111 |
return false; |
| 1112 |
} |
| 1113 |
|
| 1114 |
namespace SkPngDecoder { |
| 1115 |
bool IsPng(const void* data, size_t len) { |
| 1116 |
return SkPngCodec::IsPng(data, len); |
| 1117 |
} |
| 1118 |
|
| 1119 |
std::unique_ptr<SkCodec> Decode(std::unique_ptr<SkStream> stream, |
| 1120 |
SkCodec::Result* outResult, |
| 1121 |
SkCodecs::DecodeContext ctx) { |
| 1122 |
SkCodec::Result resultStorage; |
| 1123 |
if (!outResult) { |
| 1124 |
outResult = &resultStorage; |
| 1125 |
} |
| 1126 |
SkPngChunkReader* chunkReader = nullptr; |
| 1127 |
if (ctx) { |
| 1128 |
chunkReader = static_cast<SkPngChunkReader*>(ctx); |
| 1129 |
} |
| 1130 |
return SkPngCodec::MakeFromStream(std::move(stream), outResult, chunkReader); |
| 1131 |
} |
| 1132 |
|
| 1133 |
std::unique_ptr<SkCodec> Decode(sk_sp<const SkData> data, |
| 1134 |
SkCodec::Result* outResult, |
| 1135 |
SkCodecs::DecodeContext ctx) { |
| 1136 |
if (!data) { |
| 1137 |
if (outResult) { |
| 1138 |
*outResult = SkCodec::kInvalidInput; |
| 1139 |
} |
| 1140 |
return nullptr; |
| 1141 |
} |
| 1142 |
return Decode(SkMemoryStream::Make(std::move(data)), outResult, ctx); |
| 1143 |
} |
| 1144 |
} // namespace SkPngDecoder |