2001-01-23 01:01:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
#include "ImageLogging.h" // Must appear first
|
2016-06-30 20:27:03 +03:00
|
|
|
#include "nsPNGDecoder.h"
|
2016-06-27 23:38:34 +03:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2007-02-01 00:09:20 +03:00
|
|
|
#include "gfxColor.h"
|
2014-09-25 01:38:00 +04:00
|
|
|
#include "gfxPlatform.h"
|
2016-03-10 02:39:02 +03:00
|
|
|
#include "imgFrame.h"
|
2002-04-13 14:03:59 +04:00
|
|
|
#include "nsColor.h"
|
2014-09-25 01:38:00 +04:00
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsRect.h"
|
2001-10-12 10:43:52 +04:00
|
|
|
#include "nspr.h"
|
|
|
|
#include "png.h"
|
2017-06-03 21:37:00 +03:00
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
#include "RasterImage.h"
|
2017-06-03 21:37:00 +03:00
|
|
|
#include "SurfaceCache.h"
|
2016-06-25 01:20:32 +03:00
|
|
|
#include "SurfacePipeFactory.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2015-02-03 18:05:49 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2001-10-12 10:43:52 +04:00
|
|
|
|
2015-08-12 20:41:02 +03:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2016-07-19 09:51:16 +03:00
|
|
|
using std::min;
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
namespace mozilla {
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2010-08-14 08:09:49 +04:00
|
|
|
|
2015-10-28 23:08:06 +03:00
|
|
|
static LazyLogModule sPNGLog("PNGDecoder");
|
|
|
|
static LazyLogModule sPNGDecoderAccountingLog("PNGDecoderAccounting");
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2016-07-28 14:37:00 +03:00
|
|
|
// limit image dimensions (bug #251381, #591822, #967656, and #1283961)
|
|
|
|
#ifndef MOZ_PNG_MAX_WIDTH
|
|
|
|
# define MOZ_PNG_MAX_WIDTH 0x7fffffff // Unlimited
|
|
|
|
#endif
|
|
|
|
#ifndef MOZ_PNG_MAX_HEIGHT
|
|
|
|
# define MOZ_PNG_MAX_HEIGHT 0x7fffffff // Unlimited
|
2014-04-01 17:14:14 +04:00
|
|
|
#endif
|
|
|
|
|
2019-02-28 16:44:43 +03:00
|
|
|
/* Controls the maximum chunk size configuration for libpng. We set this to a
|
|
|
|
* very large number, 256MB specifically. */
|
|
|
|
static constexpr png_alloc_size_t kPngMaxChunkSize = 0x10000000;
|
|
|
|
|
2013-04-22 18:57:17 +04:00
|
|
|
nsPNGDecoder::AnimFrameInfo::AnimFrameInfo()
|
2015-01-08 00:07:23 +03:00
|
|
|
: mDispose(DisposalMethod::KEEP), mBlend(BlendMethod::OVER), mTimeout(0) {}
|
2012-12-20 00:11:42 +04:00
|
|
|
|
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2015-08-14 10:37:13 +03:00
|
|
|
|
|
|
|
int32_t GetNextFrameDelay(png_structp aPNG, png_infop aInfo) {
|
|
|
|
// Delay, in seconds, is delayNum / delayDen.
|
|
|
|
png_uint_16 delayNum = png_get_next_frame_delay_num(aPNG, aInfo);
|
|
|
|
png_uint_16 delayDen = png_get_next_frame_delay_den(aPNG, aInfo);
|
|
|
|
|
|
|
|
if (delayNum == 0) {
|
|
|
|
return 0; // SetFrameTimeout() will set to a minimum.
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delayDen == 0) {
|
|
|
|
delayDen = 100; // So says the APNG spec.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Need to cast delay_num to float to have a proper division and
|
|
|
|
// the result to int to avoid a compiler warning.
|
|
|
|
return static_cast<int32_t>(static_cast<double>(delayNum) * 1000 / delayDen);
|
|
|
|
}
|
|
|
|
|
2013-04-22 18:57:17 +04:00
|
|
|
nsPNGDecoder::AnimFrameInfo::AnimFrameInfo(png_structp aPNG, png_infop aInfo)
|
2015-01-08 00:07:23 +03:00
|
|
|
: mDispose(DisposalMethod::KEEP), mBlend(BlendMethod::OVER), mTimeout(0) {
|
2015-08-14 10:37:13 +03:00
|
|
|
png_byte dispose_op = png_get_next_frame_dispose_op(aPNG, aInfo);
|
|
|
|
png_byte blend_op = png_get_next_frame_blend_op(aPNG, aInfo);
|
2012-12-20 00:11:42 +04:00
|
|
|
|
2013-04-22 18:57:17 +04:00
|
|
|
if (dispose_op == PNG_DISPOSE_OP_PREVIOUS) {
|
2015-01-08 00:07:23 +03:00
|
|
|
mDispose = DisposalMethod::RESTORE_PREVIOUS;
|
2013-04-22 18:57:17 +04:00
|
|
|
} else if (dispose_op == PNG_DISPOSE_OP_BACKGROUND) {
|
2015-01-08 00:07:23 +03:00
|
|
|
mDispose = DisposalMethod::CLEAR;
|
2013-04-22 18:57:17 +04:00
|
|
|
} else {
|
2015-01-08 00:07:23 +03:00
|
|
|
mDispose = DisposalMethod::KEEP;
|
2013-04-22 18:57:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (blend_op == PNG_BLEND_OP_SOURCE) {
|
2015-01-08 00:07:23 +03:00
|
|
|
mBlend = BlendMethod::SOURCE;
|
2013-04-22 18:57:17 +04:00
|
|
|
} else {
|
2015-01-08 00:07:23 +03:00
|
|
|
mBlend = BlendMethod::OVER;
|
2013-04-22 18:57:17 +04:00
|
|
|
}
|
2015-08-14 10:37:13 +03:00
|
|
|
|
|
|
|
mTimeout = GetNextFrameDelay(aPNG, aInfo);
|
2013-04-22 18:57:17 +04:00
|
|
|
}
|
|
|
|
#endif
|
2012-12-20 00:11:42 +04:00
|
|
|
|
2011-08-26 00:09:01 +04:00
|
|
|
// First 8 bytes of a PNG file
|
|
|
|
const uint8_t nsPNGDecoder::pngSignatureBytes[] = {137, 80, 78, 71,
|
|
|
|
13, 10, 26, 10};
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2015-01-16 02:11:35 +03:00
|
|
|
nsPNGDecoder::nsPNGDecoder(RasterImage* aImage)
|
2016-06-25 01:20:32 +03:00
|
|
|
: Decoder(aImage),
|
2016-06-27 23:38:34 +03:00
|
|
|
mLexer(Transition::ToUnbuffered(State::FINISHED_PNG_DATA, State::PNG_DATA,
|
2016-07-18 08:51:19 +03:00
|
|
|
SIZE_MAX),
|
|
|
|
Transition::TerminateSuccess()),
|
2016-07-19 09:51:16 +03:00
|
|
|
mNextTransition(Transition::ContinueUnbuffered(State::PNG_DATA)),
|
|
|
|
mLastChunkLength(0),
|
2016-06-25 01:20:32 +03:00
|
|
|
mPNG(nullptr),
|
|
|
|
mInfo(nullptr),
|
|
|
|
mCMSLine(nullptr),
|
|
|
|
interlacebuf(nullptr),
|
2016-08-18 16:55:45 +03:00
|
|
|
mFormat(SurfaceFormat::UNKNOWN),
|
2016-06-25 01:20:32 +03:00
|
|
|
mChannels(0),
|
|
|
|
mPass(0),
|
|
|
|
mFrameIsHidden(false),
|
|
|
|
mDisablePremultipliedAlpha(false),
|
2019-03-14 22:32:37 +03:00
|
|
|
mGotInfoCallback(false),
|
2019-10-02 16:37:29 +03:00
|
|
|
mUsePipeTransform(false),
|
2016-06-25 01:20:32 +03:00
|
|
|
mNumFrames(0) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2001-01-23 01:01:03 +03:00
|
|
|
nsPNGDecoder::~nsPNGDecoder() {
|
2014-11-14 20:59:00 +03:00
|
|
|
if (mPNG) {
|
2013-08-23 23:51:00 +04:00
|
|
|
png_destroy_read_struct(&mPNG, mInfo ? &mInfo : nullptr, nullptr);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
|
|
|
if (mCMSLine) {
|
2015-03-27 03:01:12 +03:00
|
|
|
free(mCMSLine);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
|
|
|
if (interlacebuf) {
|
2015-03-27 03:01:12 +03:00
|
|
|
free(interlacebuf);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2016-08-18 16:55:45 +03:00
|
|
|
nsPNGDecoder::TransparencyType nsPNGDecoder::GetTransparencyType(
|
|
|
|
const IntRect& aFrameRect) {
|
2015-08-12 20:41:02 +03:00
|
|
|
// Check if the image has a transparent color in its palette.
|
2016-08-18 16:55:45 +03:00
|
|
|
if (HasAlphaChannel()) {
|
2016-06-25 01:20:32 +03:00
|
|
|
return TransparencyType::eAlpha;
|
2015-08-12 20:41:02 +03:00
|
|
|
}
|
2016-07-30 00:26:23 +03:00
|
|
|
if (!aFrameRect.IsEqualEdges(FullFrame())) {
|
2016-02-23 22:22:00 +03:00
|
|
|
MOZ_ASSERT(HasAnimation());
|
2016-06-25 01:20:32 +03:00
|
|
|
return TransparencyType::eFrameRect;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TransparencyType::eNone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsPNGDecoder::PostHasTransparencyIfNeeded(
|
|
|
|
TransparencyType aTransparencyType) {
|
|
|
|
switch (aTransparencyType) {
|
|
|
|
case TransparencyType::eNone:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case TransparencyType::eAlpha:
|
|
|
|
PostHasTransparency();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case TransparencyType::eFrameRect:
|
|
|
|
// If the first frame of animated image doesn't draw into the whole image,
|
|
|
|
// then record that it is transparent. For subsequent frames, this doesn't
|
|
|
|
// affect transparency, because they're composited on top of all previous
|
|
|
|
// frames.
|
|
|
|
if (mNumFrames == 0) {
|
|
|
|
PostHasTransparency();
|
|
|
|
}
|
|
|
|
return;
|
2016-02-23 22:22:00 +03:00
|
|
|
}
|
2015-08-12 20:41:02 +03:00
|
|
|
}
|
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
// CreateFrame() is used for both simple and animated images.
|
2016-07-19 09:51:16 +03:00
|
|
|
nsresult nsPNGDecoder::CreateFrame(const FrameInfo& aFrameInfo) {
|
2014-11-17 22:16:45 +03:00
|
|
|
MOZ_ASSERT(HasSize());
|
2015-08-11 01:34:27 +03:00
|
|
|
MOZ_ASSERT(!IsMetadataDecode());
|
2014-11-17 22:16:45 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
// Check if we have transparency, and send notifications if needed.
|
2016-08-18 16:55:45 +03:00
|
|
|
auto transparency = GetTransparencyType(aFrameInfo.mFrameRect);
|
2016-06-25 01:20:32 +03:00
|
|
|
PostHasTransparencyIfNeeded(transparency);
|
2019-11-12 21:22:33 +03:00
|
|
|
mFormat = transparency == TransparencyType::eNone ? SurfaceFormat::OS_RGBX
|
|
|
|
: SurfaceFormat::OS_RGBA;
|
2015-07-11 05:26:15 +03:00
|
|
|
|
2015-09-02 00:13:17 +03:00
|
|
|
// Make sure there's no animation or padding if we're downscaling.
|
2016-08-05 14:21:40 +03:00
|
|
|
MOZ_ASSERT_IF(Size() != OutputSize(), mNumFrames == 0);
|
|
|
|
MOZ_ASSERT_IF(Size() != OutputSize(), !GetImageMetadata().HasAnimation());
|
|
|
|
MOZ_ASSERT_IF(Size() != OutputSize(),
|
|
|
|
transparency != TransparencyType::eFrameRect);
|
2015-09-02 00:13:17 +03:00
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
Maybe<AnimationParams> animParams;
|
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2019-03-15 20:29:02 +03:00
|
|
|
if (!IsFirstFrameDecode() && png_get_valid(mPNG, mInfo, PNG_INFO_acTL)) {
|
2018-05-29 15:36:12 +03:00
|
|
|
mAnimInfo = AnimFrameInfo(mPNG, mInfo);
|
|
|
|
|
|
|
|
if (mAnimInfo.mDispose == DisposalMethod::CLEAR) {
|
|
|
|
// We may have to display the background under this image during
|
|
|
|
// animation playback, so we regard it as transparent.
|
|
|
|
PostHasTransparency();
|
|
|
|
}
|
|
|
|
|
|
|
|
animParams.emplace(
|
|
|
|
AnimationParams{aFrameInfo.mFrameRect,
|
|
|
|
FrameTimeout::FromRawMilliseconds(mAnimInfo.mTimeout),
|
|
|
|
mNumFrames, mAnimInfo.mBlend, mAnimInfo.mDispose});
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
// If this image is interlaced, we can display better quality intermediate
|
|
|
|
// results to the user by post processing them with ADAM7InterpolatingFilter.
|
2016-07-19 09:51:16 +03:00
|
|
|
SurfacePipeFlags pipeFlags = aFrameInfo.mIsInterlaced
|
2016-06-25 01:20:32 +03:00
|
|
|
? SurfacePipeFlags::ADAM7_INTERPOLATE
|
|
|
|
: SurfacePipeFlags();
|
|
|
|
|
|
|
|
if (mNumFrames == 0) {
|
|
|
|
// The first frame may be displayed progressively.
|
|
|
|
pipeFlags |= SurfacePipeFlags::PROGRESSIVE_DISPLAY;
|
|
|
|
}
|
|
|
|
|
2019-10-02 16:37:29 +03:00
|
|
|
SurfaceFormat inFormat;
|
|
|
|
if (mTransform && !mUsePipeTransform) {
|
|
|
|
// QCMS will output in the correct format.
|
|
|
|
inFormat = mFormat;
|
|
|
|
} else if (transparency == TransparencyType::eAlpha) {
|
|
|
|
// We are outputting directly as RGBA, so we need to swap at this step.
|
|
|
|
inFormat = SurfaceFormat::R8G8B8A8;
|
|
|
|
} else {
|
|
|
|
// We have no alpha channel, so we need to unpack from RGB to BGRA.
|
|
|
|
inFormat = SurfaceFormat::R8G8B8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only apply premultiplication if the frame has true alpha. If we ever
|
|
|
|
// support downscaling animated images, we will need to premultiply for frame
|
|
|
|
// rect transparency when downscaling as well.
|
|
|
|
if (transparency == TransparencyType::eAlpha && !mDisablePremultipliedAlpha) {
|
|
|
|
pipeFlags |= SurfacePipeFlags::PREMULTIPLY_ALPHA;
|
|
|
|
}
|
|
|
|
|
|
|
|
qcms_transform* pipeTransform = mUsePipeTransform ? mTransform : nullptr;
|
2018-05-29 15:36:12 +03:00
|
|
|
Maybe<SurfacePipe> pipe = SurfacePipeFactory::CreateSurfacePipe(
|
2019-10-02 16:37:29 +03:00
|
|
|
this, Size(), OutputSize(), aFrameInfo.mFrameRect, inFormat, mFormat,
|
|
|
|
animParams, pipeTransform, pipeFlags);
|
2016-06-25 01:20:32 +03:00
|
|
|
|
|
|
|
if (!pipe) {
|
|
|
|
mPipe = SurfacePipe();
|
|
|
|
return NS_ERROR_FAILURE;
|
2013-05-08 06:25:03 +04:00
|
|
|
}
|
2007-03-21 02:56:50 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mPipe = std::move(*pipe);
|
2016-06-25 01:20:32 +03:00
|
|
|
|
2016-07-19 09:51:16 +03:00
|
|
|
mFrameRect = aFrameInfo.mFrameRect;
|
2016-06-25 01:20:32 +03:00
|
|
|
mPass = 0;
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
|
2015-10-28 23:08:06 +03:00
|
|
|
MOZ_LOG(sPNGDecoderAccountingLog, LogLevel::Debug,
|
2010-01-06 00:46:26 +03:00
|
|
|
("PNGDecoderAccounting: nsPNGDecoder::CreateFrame -- created "
|
2015-07-31 17:29:03 +03:00
|
|
|
"image frame with %dx%d pixels for decoder %p",
|
2017-08-14 15:29:56 +03:00
|
|
|
mFrameRect.Width(), mFrameRect.Height(), this));
|
2007-10-19 04:36:34 +04:00
|
|
|
|
2015-07-11 05:26:15 +03:00
|
|
|
return NS_OK;
|
2007-03-21 02:56:50 +03:00
|
|
|
}
|
|
|
|
|
2008-02-13 13:53:17 +03:00
|
|
|
// set timeout and frame disposal method for the current frame
|
2014-11-14 20:59:00 +03:00
|
|
|
void nsPNGDecoder::EndImageFrame() {
|
|
|
|
if (mFrameIsHidden) {
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2010-09-12 19:22:30 +04:00
|
|
|
|
2013-02-27 23:23:08 +04:00
|
|
|
mNumFrames++;
|
|
|
|
|
2019-11-12 21:22:33 +03:00
|
|
|
Opacity opacity = mFormat == SurfaceFormat::OS_RGBX
|
2016-08-18 16:55:45 +03:00
|
|
|
? Opacity::FULLY_OPAQUE
|
|
|
|
: Opacity::SOME_TRANSPARENCY;
|
2012-12-20 00:11:42 +04:00
|
|
|
|
2018-05-29 15:36:12 +03:00
|
|
|
PostFrameStop(opacity);
|
2008-02-13 13:53:17 +03:00
|
|
|
}
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
nsresult nsPNGDecoder::InitInternal() {
|
2015-02-03 08:40:35 +03:00
|
|
|
mDisablePremultipliedAlpha =
|
2015-08-15 03:56:44 +03:00
|
|
|
bool(GetSurfaceFlags() & SurfaceFlags::NO_PREMULTIPLY_ALPHA);
|
2010-08-23 06:30:46 +04:00
|
|
|
|
2010-03-09 21:22:44 +03:00
|
|
|
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
|
2007-11-07 07:29:37 +03:00
|
|
|
static png_byte color_chunks[] = {99, 72, 82, 77, '\0', // cHRM
|
2014-09-25 01:38:00 +04:00
|
|
|
105, 67, 67, 80, '\0'}; // iCCP
|
2004-08-04 18:02:34 +04:00
|
|
|
static png_byte unused_chunks[] = {98, 75, 71, 68, '\0', // bKGD
|
2017-09-21 22:35:00 +03:00
|
|
|
101, 88, 73, 102, '\0', // eXIf
|
2014-09-25 01:38:00 +04:00
|
|
|
104, 73, 83, 84, '\0', // hIST
|
|
|
|
105, 84, 88, 116, '\0', // iTXt
|
|
|
|
111, 70, 70, 115, '\0', // oFFs
|
|
|
|
112, 67, 65, 76, '\0', // pCAL
|
|
|
|
115, 67, 65, 76, '\0', // sCAL
|
|
|
|
112, 72, 89, 115, '\0', // pHYs
|
|
|
|
115, 66, 73, 84, '\0', // sBIT
|
|
|
|
115, 80, 76, 84, '\0', // sPLT
|
|
|
|
116, 69, 88, 116, '\0', // tEXt
|
|
|
|
116, 73, 77, 69, '\0', // tIME
|
|
|
|
122, 84, 88, 116, '\0'}; // zTXt
|
2004-08-04 18:02:34 +04:00
|
|
|
#endif
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
// Initialize the container's source image header
|
|
|
|
// Always decode to 24 bit pixdepth
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2010-03-12 17:15:00 +03:00
|
|
|
mPNG = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr,
|
2013-08-23 23:51:00 +04:00
|
|
|
nsPNGDecoder::error_callback,
|
2010-08-23 06:30:46 +04:00
|
|
|
nsPNGDecoder::warning_callback);
|
2010-09-12 19:22:27 +04:00
|
|
|
if (!mPNG) {
|
2016-07-11 10:34:50 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2010-09-12 19:22:27 +04:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
|
|
|
mInfo = png_create_info_struct(mPNG);
|
|
|
|
if (!mInfo) {
|
2013-08-23 23:51:00 +04:00
|
|
|
png_destroy_read_struct(&mPNG, nullptr, nullptr);
|
2016-07-11 10:34:50 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2010-03-09 21:22:44 +03:00
|
|
|
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
|
2014-09-25 01:38:00 +04:00
|
|
|
// Ignore unused chunks
|
2015-08-12 20:41:02 +03:00
|
|
|
if (mCMSMode == eCMSMode_Off || IsMetadataDecode()) {
|
2007-11-07 07:29:37 +03:00
|
|
|
png_set_keep_unknown_chunks(mPNG, 1, color_chunks, 2);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2010-01-06 00:46:26 +03:00
|
|
|
|
2006-09-27 01:15:04 +04:00
|
|
|
png_set_keep_unknown_chunks(mPNG, 1, unused_chunks,
|
2013-05-04 13:39:47 +04:00
|
|
|
(int)sizeof(unused_chunks) / 5);
|
2010-03-09 21:22:44 +03:00
|
|
|
#endif
|
|
|
|
|
2015-03-28 13:07:00 +03:00
|
|
|
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
|
2016-07-28 14:37:00 +03:00
|
|
|
png_set_user_limits(mPNG, MOZ_PNG_MAX_WIDTH, MOZ_PNG_MAX_HEIGHT);
|
2019-02-28 16:44:43 +03:00
|
|
|
png_set_chunk_malloc_max(mPNG, kPngMaxChunkSize);
|
2004-08-04 18:02:34 +04:00
|
|
|
#endif
|
|
|
|
|
2012-07-24 04:02:20 +04:00
|
|
|
#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
|
2014-09-25 01:38:00 +04:00
|
|
|
// Disallow palette-index checking, for speed; we would ignore the warning
|
2015-05-11 23:42:30 +03:00
|
|
|
// anyhow. This feature was added at libpng version 1.5.10 and is disabled
|
|
|
|
// in the embedded libpng but enabled by default in the system libpng. This
|
|
|
|
// call also disables it in the system libpng, for decoding speed.
|
|
|
|
// Bug #745202.
|
2014-08-22 15:11:00 +04:00
|
|
|
png_set_check_for_invalid_index(mPNG, 0);
|
2012-07-24 04:02:20 +04:00
|
|
|
#endif
|
|
|
|
|
2016-11-16 23:21:33 +03:00
|
|
|
#ifdef PNG_SET_OPTION_SUPPORTED
|
|
|
|
# if defined(PNG_sRGB_PROFILE_CHECKS) && PNG_sRGB_PROFILE_CHECKS >= 0
|
2014-09-25 01:38:00 +04:00
|
|
|
// Skip checking of sRGB ICC profiles
|
2014-08-22 15:11:00 +04:00
|
|
|
png_set_option(mPNG, PNG_SKIP_sRGB_CHECK_PROFILE, PNG_OPTION_ON);
|
|
|
|
# endif
|
|
|
|
|
2016-11-16 23:21:33 +03:00
|
|
|
# ifdef PNG_MAXIMUM_INFLATE_WINDOW
|
|
|
|
// Force a larger zlib inflate window as some images in the wild have
|
|
|
|
// incorrectly set metadata (specifically CMF bits) which prevent us from
|
|
|
|
// decoding them otherwise.
|
|
|
|
png_set_option(mPNG, PNG_MAXIMUM_INFLATE_WINDOW, PNG_OPTION_ON);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
// use this as libpng "progressive pointer" (retrieve in callbacks)
|
2007-07-08 11:08:04 +04:00
|
|
|
png_set_progressive_read_fn(
|
|
|
|
mPNG, static_cast<png_voidp>(this), nsPNGDecoder::info_callback,
|
2010-08-23 06:30:46 +04:00
|
|
|
nsPNGDecoder::row_callback, nsPNGDecoder::end_callback);
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2016-07-11 10:34:50 +03:00
|
|
|
return NS_OK;
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2016-07-15 08:39:39 +03:00
|
|
|
LexerResult nsPNGDecoder::DoDecode(SourceBufferIterator& aIterator,
|
|
|
|
IResumable* aOnResume) {
|
2016-07-11 10:05:51 +03:00
|
|
|
MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
|
2016-06-27 23:38:34 +03:00
|
|
|
|
2016-07-15 08:39:39 +03:00
|
|
|
return mLexer.Lex(aIterator, aOnResume,
|
2016-07-11 10:07:07 +03:00
|
|
|
[=](State aState, const char* aData, size_t aLength) {
|
|
|
|
switch (aState) {
|
|
|
|
case State::PNG_DATA:
|
|
|
|
return ReadPNGData(aData, aLength);
|
|
|
|
case State::FINISHED_PNG_DATA:
|
|
|
|
return FinishedPNGData();
|
|
|
|
}
|
|
|
|
MOZ_CRASH("Unknown State");
|
|
|
|
});
|
2016-06-27 23:38:34 +03:00
|
|
|
}
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2016-06-27 23:38:34 +03:00
|
|
|
LexerTransition<nsPNGDecoder::State> nsPNGDecoder::ReadPNGData(
|
|
|
|
const char* aData, size_t aLength) {
|
2016-07-19 09:51:16 +03:00
|
|
|
// If we were waiting until after returning from a yield to call
|
|
|
|
// CreateFrame(), call it now.
|
|
|
|
if (mNextFrameInfo) {
|
|
|
|
if (NS_FAILED(CreateFrame(*mNextFrameInfo))) {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mImageData, "Should have a buffer now");
|
|
|
|
mNextFrameInfo = Nothing();
|
|
|
|
}
|
|
|
|
|
2016-06-27 23:38:34 +03:00
|
|
|
// libpng uses setjmp/longjmp for error handling.
|
|
|
|
if (setjmp(png_jmpbuf(mPNG))) {
|
|
|
|
return Transition::TerminateFailure();
|
2008-11-07 00:31:20 +03:00
|
|
|
}
|
2015-08-11 01:34:27 +03:00
|
|
|
|
|
|
|
// Pass the data off to libpng.
|
2016-07-19 09:51:16 +03:00
|
|
|
mLastChunkLength = aLength;
|
|
|
|
mNextTransition = Transition::ContinueUnbuffered(State::PNG_DATA);
|
2015-08-11 01:34:27 +03:00
|
|
|
png_process_data(mPNG, mInfo,
|
2016-06-27 23:38:34 +03:00
|
|
|
reinterpret_cast<unsigned char*>(const_cast<char*>((aData))),
|
|
|
|
aLength);
|
|
|
|
|
2016-07-19 09:51:16 +03:00
|
|
|
// Make sure that we've reached a terminal state if decoding is done.
|
|
|
|
MOZ_ASSERT_IF(GetDecodeDone(), mNextTransition.NextStateIsTerminal());
|
|
|
|
MOZ_ASSERT_IF(HasError(), mNextTransition.NextStateIsTerminal());
|
2016-06-27 23:38:34 +03:00
|
|
|
|
2016-07-19 09:51:16 +03:00
|
|
|
// Continue with whatever transition the callback code requested. We
|
|
|
|
// initialized this to Transition::ContinueUnbuffered(State::PNG_DATA) above,
|
|
|
|
// so by default we just continue the unbuffered read.
|
|
|
|
return mNextTransition;
|
2016-06-27 23:38:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsPNGDecoder::State> nsPNGDecoder::FinishedPNGData() {
|
|
|
|
// Since we set up an unbuffered read for SIZE_MAX bytes, if we actually read
|
|
|
|
// all that data something is really wrong.
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Read the entire address space?");
|
|
|
|
return Transition::TerminateFailure();
|
2009-09-13 02:44:18 +04:00
|
|
|
}
|
2002-03-12 10:15:21 +03:00
|
|
|
|
2009-12-04 05:41:00 +03:00
|
|
|
// Sets up gamma pre-correction in libpng before our callback gets called.
|
2008-08-31 02:54:58 +04:00
|
|
|
// We need to do this if we don't end up with a CMS profile.
|
|
|
|
static void PNGDoGammaCorrection(png_structp png_ptr, png_infop info_ptr) {
|
|
|
|
double aGamma;
|
|
|
|
|
|
|
|
if (png_get_gAMA(png_ptr, info_ptr, &aGamma)) {
|
|
|
|
if ((aGamma <= 0.0) || (aGamma > 21474.83)) {
|
|
|
|
aGamma = 0.45455;
|
|
|
|
png_set_gAMA(png_ptr, info_ptr, aGamma);
|
|
|
|
}
|
|
|
|
png_set_gamma(png_ptr, 2.2, aGamma);
|
2014-11-14 20:59:00 +03:00
|
|
|
} else {
|
2008-08-31 02:54:58 +04:00
|
|
|
png_set_gamma(png_ptr, 2.2, 0.45455);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2008-08-31 02:54:58 +04:00
|
|
|
}
|
|
|
|
|
2007-07-24 02:02:17 +04:00
|
|
|
// Adapted from http://www.littlecms.com/pngchrm.c example code
|
2020-03-05 14:51:15 +03:00
|
|
|
uint32_t nsPNGDecoder::ReadColorProfile(png_structp png_ptr, png_infop info_ptr,
|
|
|
|
int color_type, bool* sRGBTag) {
|
2007-07-24 02:02:17 +04:00
|
|
|
// First try to see if iCCP chunk is present
|
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_iCCP)) {
|
|
|
|
png_uint_32 profileLen;
|
2011-03-29 04:14:00 +04:00
|
|
|
png_bytep profileData;
|
|
|
|
png_charp profileName;
|
2007-07-24 02:02:17 +04:00
|
|
|
int compression;
|
|
|
|
|
|
|
|
png_get_iCCP(png_ptr, info_ptr, &profileName, &compression, &profileData,
|
|
|
|
&profileLen);
|
|
|
|
|
2020-03-05 14:51:15 +03:00
|
|
|
mInProfile = qcms_profile_from_memory((char*)profileData, profileLen);
|
|
|
|
if (mInProfile) {
|
|
|
|
uint32_t profileSpace = qcms_profile_get_color_space(mInProfile);
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mismatch = false;
|
2009-04-07 20:02:11 +04:00
|
|
|
if (color_type & PNG_COLOR_MASK_COLOR) {
|
2014-11-14 20:59:00 +03:00
|
|
|
if (profileSpace != icSigRgbData) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mismatch = true;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2009-04-07 20:02:11 +04:00
|
|
|
} else {
|
2014-11-14 20:59:00 +03:00
|
|
|
if (profileSpace == icSigRgbData) {
|
2009-04-07 20:02:11 +04:00
|
|
|
png_set_gray_to_rgb(png_ptr);
|
2014-11-14 20:59:00 +03:00
|
|
|
} else if (profileSpace != icSigGrayData) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mismatch = true;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2009-04-07 20:02:11 +04:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2009-04-07 20:02:11 +04:00
|
|
|
if (mismatch) {
|
2020-03-05 14:51:15 +03:00
|
|
|
qcms_profile_release(mInProfile);
|
|
|
|
mInProfile = nullptr;
|
2009-04-07 20:02:11 +04:00
|
|
|
} else {
|
2020-03-05 14:51:15 +03:00
|
|
|
return qcms_profile_get_rendering_intent(mInProfile);
|
2009-04-07 20:02:11 +04:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check sRGB chunk
|
2020-03-05 14:51:15 +03:00
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
|
|
|
|
*sRGBTag = true;
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2020-03-05 14:51:15 +03:00
|
|
|
int fileIntent;
|
|
|
|
png_set_gray_to_rgb(png_ptr);
|
|
|
|
png_get_sRGB(png_ptr, info_ptr, &fileIntent);
|
|
|
|
uint32_t map[] = {QCMS_INTENT_PERCEPTUAL, QCMS_INTENT_RELATIVE_COLORIMETRIC,
|
|
|
|
QCMS_INTENT_SATURATION,
|
|
|
|
QCMS_INTENT_ABSOLUTE_COLORIMETRIC};
|
|
|
|
return map[fileIntent];
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check gAMA/cHRM chunks
|
2020-03-05 14:51:15 +03:00
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) &&
|
2008-08-31 02:54:58 +04:00
|
|
|
png_get_valid(png_ptr, info_ptr, PNG_INFO_cHRM)) {
|
2009-04-07 20:02:11 +04:00
|
|
|
qcms_CIE_xyYTRIPLE primaries;
|
|
|
|
qcms_CIE_xyY whitePoint;
|
2008-08-31 02:54:58 +04:00
|
|
|
|
|
|
|
png_get_cHRM(png_ptr, info_ptr, &whitePoint.x, &whitePoint.y,
|
2009-04-07 20:02:11 +04:00
|
|
|
&primaries.red.x, &primaries.red.y, &primaries.green.x,
|
|
|
|
&primaries.green.y, &primaries.blue.x, &primaries.blue.y);
|
|
|
|
whitePoint.Y = primaries.red.Y = primaries.green.Y = primaries.blue.Y = 1.0;
|
2007-07-24 02:02:17 +04:00
|
|
|
|
|
|
|
double gammaOfFile;
|
|
|
|
|
|
|
|
png_get_gAMA(png_ptr, info_ptr, &gammaOfFile);
|
|
|
|
|
2020-03-05 14:51:15 +03:00
|
|
|
mInProfile = qcms_profile_create_rgb_with_gamma(whitePoint, primaries,
|
|
|
|
1.0 / gammaOfFile);
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2020-03-05 14:51:15 +03:00
|
|
|
if (mInProfile) {
|
2007-07-24 02:02:17 +04:00
|
|
|
png_set_gray_to_rgb(png_ptr);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
|
|
|
|
2020-03-05 14:51:15 +03:00
|
|
|
return QCMS_INTENT_PERCEPTUAL; // Our default
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
void nsPNGDecoder::info_callback(png_structp png_ptr, png_infop info_ptr) {
|
2001-01-26 15:05:55 +03:00
|
|
|
png_uint_32 width, height;
|
|
|
|
int bit_depth, color_type, interlace_type, compression_type, filter_type;
|
2008-07-12 00:52:30 +04:00
|
|
|
unsigned int channels;
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2013-08-23 23:51:00 +04:00
|
|
|
png_bytep trans = nullptr;
|
2007-03-21 02:56:50 +03:00
|
|
|
int num_trans = 0;
|
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
nsPNGDecoder* decoder =
|
2009-12-04 05:41:00 +03:00
|
|
|
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2019-03-14 22:32:37 +03:00
|
|
|
if (decoder->mGotInfoCallback) {
|
2019-03-15 20:29:02 +03:00
|
|
|
MOZ_LOG(sPNGLog, LogLevel::Warning,
|
|
|
|
("libpng called info_callback more than once\n"));
|
2019-03-14 22:32:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder->mGotInfoCallback = true;
|
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
// Always decode to 24-bit RGB or 32-bit RGBA
|
2001-01-26 15:05:55 +03:00
|
|
|
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
|
|
|
|
&interlace_type, &compression_type, &filter_type);
|
2009-12-04 05:41:00 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
const IntRect frameRect(0, 0, width, height);
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
// Post our size to the superclass
|
2017-08-14 15:29:56 +03:00
|
|
|
decoder->PostSize(frameRect.Width(), frameRect.Height());
|
2017-06-03 21:37:00 +03:00
|
|
|
|
|
|
|
if (width > SurfaceCache::MaximumCapacity() / (bit_depth > 8 ? 16 : 8)) {
|
|
|
|
// libpng needs space to allocate two row buffers
|
|
|
|
png_error(decoder->mPNG, "Image is too wide");
|
|
|
|
}
|
|
|
|
|
2011-05-11 13:46:59 +04:00
|
|
|
if (decoder->HasError()) {
|
2012-05-19 23:32:37 +04:00
|
|
|
// Setting the size led to an error.
|
2016-07-29 06:11:00 +03:00
|
|
|
png_error(decoder->mPNG, "Sizing error");
|
2011-05-11 13:46:59 +04:00
|
|
|
}
|
2004-08-04 18:02:34 +04:00
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
if (color_type == PNG_COLOR_TYPE_PALETTE) {
|
2001-01-26 15:05:55 +03:00
|
|
|
png_set_expand(png_ptr);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
|
2001-01-26 15:05:55 +03:00
|
|
|
png_set_expand(png_ptr);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2001-01-26 15:05:55 +03:00
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
2008-08-19 12:12:31 +04:00
|
|
|
png_color_16p trans_values;
|
|
|
|
png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values);
|
2014-09-25 01:38:00 +04:00
|
|
|
// libpng doesn't reject a tRNS chunk with out-of-range samples
|
|
|
|
// so we check it here to avoid setting up a useless opacity
|
2015-01-24 20:43:49 +03:00
|
|
|
// channel or producing unexpected transparent pixels (bug #428045)
|
|
|
|
if (bit_depth < 16) {
|
|
|
|
png_uint_16 sample_max = (1 << bit_depth) - 1;
|
|
|
|
if ((color_type == PNG_COLOR_TYPE_GRAY &&
|
|
|
|
trans_values->gray > sample_max) ||
|
|
|
|
(color_type == PNG_COLOR_TYPE_RGB &&
|
|
|
|
(trans_values->red > sample_max ||
|
|
|
|
trans_values->green > sample_max ||
|
|
|
|
trans_values->blue > sample_max))) {
|
|
|
|
// clear the tRNS valid flag and release tRNS memory
|
|
|
|
png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
|
|
|
|
num_trans = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (num_trans != 0) {
|
2010-01-06 00:46:26 +03:00
|
|
|
png_set_expand(png_ptr);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-01-26 15:05:55 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
if (bit_depth == 16) {
|
2012-03-04 07:18:31 +04:00
|
|
|
png_set_scale_16(png_ptr);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2019-06-21 02:50:20 +03:00
|
|
|
// We only need to extract the color profile for non-metadata decodes. It is
|
|
|
|
// fairly expensive to read the profile and create the transform so we should
|
|
|
|
// avoid it if not necessary.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t intent = -1;
|
2020-03-05 14:51:15 +03:00
|
|
|
bool sRGBTag = false;
|
2019-06-21 02:50:20 +03:00
|
|
|
if (!decoder->IsMetadataDecode()) {
|
2014-11-14 20:59:00 +03:00
|
|
|
if (decoder->mCMSMode != eCMSMode_Off) {
|
2019-06-21 02:50:20 +03:00
|
|
|
intent = gfxPlatform::GetRenderingIntent();
|
2020-03-05 14:51:15 +03:00
|
|
|
uint32_t pIntent =
|
|
|
|
decoder->ReadColorProfile(png_ptr, info_ptr, color_type, &sRGBTag);
|
2019-06-21 02:50:20 +03:00
|
|
|
// If we're not mandating an intent, use the one from the image.
|
|
|
|
if (intent == uint32_t(-1)) {
|
|
|
|
intent = pIntent;
|
|
|
|
}
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2020-04-10 19:26:33 +03:00
|
|
|
if (!decoder->mInProfile || !decoder->GetCMSOutputProfile()) {
|
2019-06-21 02:50:20 +03:00
|
|
|
png_set_gray_to_rgb(png_ptr);
|
2011-01-13 04:45:13 +03:00
|
|
|
|
2019-06-21 02:50:20 +03:00
|
|
|
// only do gamma correction if CMS isn't entirely disabled
|
|
|
|
if (decoder->mCMSMode != eCMSMode_Off) {
|
|
|
|
PNGDoGammaCorrection(png_ptr, info_ptr);
|
2019-06-11 17:54:11 +03:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-11 17:54:11 +03:00
|
|
|
// Let libpng expand interlaced images.
|
|
|
|
const bool isInterlaced = interlace_type == PNG_INTERLACE_ADAM7;
|
|
|
|
if (isInterlaced) {
|
|
|
|
png_set_interlace_handling(png_ptr);
|
|
|
|
}
|
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
// now all of those things we set above are used to update various struct
|
|
|
|
// members and whatnot, after which we can get channels, rowbytes, etc.
|
2001-01-26 15:05:55 +03:00
|
|
|
png_read_update_info(png_ptr, info_ptr);
|
2007-07-24 02:02:17 +04:00
|
|
|
decoder->mChannels = channels = png_get_channels(png_ptr, info_ptr);
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
//---------------------------------------------------------------//
|
|
|
|
// copy PNG info into imagelib structs (formerly png_set_dims()) //
|
|
|
|
//---------------------------------------------------------------//
|
2001-01-26 15:05:55 +03:00
|
|
|
|
2016-08-18 16:55:45 +03:00
|
|
|
if (channels < 1 || channels > 4) {
|
2016-07-29 06:11:00 +03:00
|
|
|
png_error(decoder->mPNG, "Invalid number of channels");
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2015-08-14 10:37:13 +03:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
|
|
|
bool isAnimated = png_get_valid(png_ptr, info_ptr, PNG_INFO_acTL);
|
|
|
|
if (isAnimated) {
|
2016-07-20 02:22:34 +03:00
|
|
|
int32_t rawTimeout = GetNextFrameDelay(png_ptr, info_ptr);
|
|
|
|
decoder->PostIsAnimated(FrameTimeout::FromRawMilliseconds(rawTimeout));
|
2015-09-02 00:13:17 +03:00
|
|
|
|
2016-08-05 14:21:40 +03:00
|
|
|
if (decoder->Size() != decoder->OutputSize() &&
|
|
|
|
!decoder->IsFirstFrameDecode()) {
|
2015-09-02 00:13:17 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE(
|
|
|
|
"Doing downscale-during-decode "
|
|
|
|
"for an animated image?");
|
2016-07-29 06:11:00 +03:00
|
|
|
png_error(decoder->mPNG, "Invalid downscale attempt"); // Abort decode.
|
2015-09-02 00:13:17 +03:00
|
|
|
}
|
2015-08-14 10:37:13 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-10-02 16:37:29 +03:00
|
|
|
auto transparency = decoder->GetTransparencyType(frameRect);
|
2015-08-12 20:41:02 +03:00
|
|
|
if (decoder->IsMetadataDecode()) {
|
2016-09-07 16:03:00 +03:00
|
|
|
// If we are animated then the first frame rect is either:
|
|
|
|
// 1) the whole image if the IDAT chunk is part of the animation
|
|
|
|
// 2) the frame rect of the first fDAT chunk otherwise.
|
|
|
|
// If we are not animated then we want to make sure to call
|
|
|
|
// PostHasTransparency in the metadata decode if we need to. So it's
|
|
|
|
// okay to pass IntRect(0, 0, width, height) here for animated images;
|
|
|
|
// they will call with the proper first frame rect in the full decode.
|
2016-06-25 01:20:32 +03:00
|
|
|
decoder->PostHasTransparencyIfNeeded(transparency);
|
2015-08-12 20:41:02 +03:00
|
|
|
|
2016-06-27 22:00:16 +03:00
|
|
|
// We have the metadata we're looking for, so stop here, before we allocate
|
|
|
|
// buffers below.
|
2016-07-19 09:51:16 +03:00
|
|
|
return decoder->DoTerminate(png_ptr, TerminalState::SUCCESS);
|
2015-08-12 20:41:02 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 19:26:33 +03:00
|
|
|
if (decoder->mInProfile && decoder->GetCMSOutputProfile()) {
|
2019-10-02 16:37:29 +03:00
|
|
|
qcms_data_type inType;
|
2019-06-21 02:50:20 +03:00
|
|
|
qcms_data_type outType;
|
|
|
|
|
2019-10-02 16:37:29 +03:00
|
|
|
uint32_t profileSpace = qcms_profile_get_color_space(decoder->mInProfile);
|
|
|
|
decoder->mUsePipeTransform = profileSpace != icSigGrayData;
|
|
|
|
if (decoder->mUsePipeTransform) {
|
|
|
|
// If the transform happens with SurfacePipe, it will be in RGBA if we
|
|
|
|
// have an alpha channel, because the swizzle and premultiplication
|
|
|
|
// happens after color management. Otherwise it will be in BGRA because
|
|
|
|
// the swizzle happens at the start.
|
|
|
|
if (transparency == TransparencyType::eAlpha) {
|
|
|
|
inType = QCMS_DATA_RGBA_8;
|
|
|
|
outType = QCMS_DATA_RGBA_8;
|
|
|
|
} else {
|
2019-11-12 21:22:33 +03:00
|
|
|
inType = gfxPlatform::GetCMSOSRGBAType();
|
|
|
|
outType = inType;
|
2019-10-02 16:37:29 +03:00
|
|
|
}
|
2019-06-21 02:50:20 +03:00
|
|
|
} else {
|
2019-10-02 16:37:29 +03:00
|
|
|
if (color_type & PNG_COLOR_MASK_ALPHA) {
|
|
|
|
inType = QCMS_DATA_GRAYA_8;
|
2019-11-12 21:22:33 +03:00
|
|
|
outType = gfxPlatform::GetCMSOSRGBAType();
|
2019-10-02 16:37:29 +03:00
|
|
|
} else {
|
|
|
|
inType = QCMS_DATA_GRAY_8;
|
2019-11-12 21:22:33 +03:00
|
|
|
outType = gfxPlatform::GetCMSOSRGBAType();
|
2019-10-02 16:37:29 +03:00
|
|
|
}
|
2019-06-21 02:50:20 +03:00
|
|
|
}
|
|
|
|
|
2020-04-10 19:26:33 +03:00
|
|
|
decoder->mTransform = qcms_transform_create(decoder->mInProfile, inType,
|
|
|
|
decoder->GetCMSOutputProfile(),
|
|
|
|
outType, (qcms_intent)intent);
|
2020-06-12 00:46:45 +03:00
|
|
|
} else if ((sRGBTag && decoder->mCMSMode == eCMSMode_TaggedOnly) ||
|
|
|
|
decoder->mCMSMode == eCMSMode_All) {
|
2019-10-02 16:37:29 +03:00
|
|
|
// If the transform happens with SurfacePipe, it will be in RGBA if we
|
|
|
|
// have an alpha channel, because the swizzle and premultiplication
|
2020-04-10 19:26:33 +03:00
|
|
|
// happens after color management. Otherwise it will be in OS_RGBA because
|
2019-10-02 16:37:29 +03:00
|
|
|
// the swizzle happens at the start.
|
|
|
|
if (transparency == TransparencyType::eAlpha) {
|
2020-04-10 19:26:33 +03:00
|
|
|
decoder->mTransform =
|
|
|
|
decoder->GetCMSsRGBTransform(SurfaceFormat::R8G8B8A8);
|
2019-06-21 02:50:20 +03:00
|
|
|
} else {
|
2020-04-10 19:26:33 +03:00
|
|
|
decoder->mTransform =
|
|
|
|
decoder->GetCMSsRGBTransform(SurfaceFormat::OS_RGBA);
|
2019-06-21 02:50:20 +03:00
|
|
|
}
|
2019-10-02 16:37:29 +03:00
|
|
|
decoder->mUsePipeTransform = true;
|
2019-06-21 02:50:20 +03:00
|
|
|
}
|
|
|
|
|
2010-01-06 00:46:26 +03:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2015-08-14 10:37:13 +03:00
|
|
|
if (isAnimated) {
|
2013-08-23 23:51:00 +04:00
|
|
|
png_set_progressive_frame_fn(png_ptr, nsPNGDecoder::frame_info_callback,
|
|
|
|
nullptr);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2009-12-04 05:41:00 +03:00
|
|
|
|
2007-04-20 00:40:53 +04:00
|
|
|
if (png_get_first_frame_is_hidden(png_ptr, info_ptr)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
decoder->mFrameIsHidden = true;
|
2007-03-21 02:56:50 +03:00
|
|
|
} else {
|
2010-01-06 00:46:26 +03:00
|
|
|
#endif
|
2016-08-18 16:55:45 +03:00
|
|
|
nsresult rv = decoder->CreateFrame(FrameInfo{frameRect, isInterlaced});
|
2015-07-11 05:26:15 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2016-07-29 06:11:00 +03:00
|
|
|
png_error(decoder->mPNG, "CreateFrame failed");
|
2015-07-11 05:26:15 +03:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(decoder->mImageData, "Should have a buffer now");
|
2010-01-06 00:46:26 +03:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-03-21 02:56:50 +03:00
|
|
|
}
|
2010-01-06 00:46:26 +03:00
|
|
|
#endif
|
2009-12-04 05:41:00 +03:00
|
|
|
|
2019-10-02 16:37:29 +03:00
|
|
|
if (decoder->mTransform && !decoder->mUsePipeTransform) {
|
2007-07-24 02:02:17 +04:00
|
|
|
decoder->mCMSLine =
|
2019-10-02 16:37:29 +03:00
|
|
|
static_cast<uint8_t*>(malloc(sizeof(uint32_t) * frameRect.Width()));
|
2009-09-13 02:44:18 +04:00
|
|
|
if (!decoder->mCMSLine) {
|
2016-07-29 06:11:00 +03:00
|
|
|
png_error(decoder->mPNG, "malloc of mCMSLine failed");
|
2009-09-13 02:44:18 +04:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
|
|
|
|
2001-02-21 01:43:56 +03:00
|
|
|
if (interlace_type == PNG_INTERLACE_ADAM7) {
|
2017-08-14 15:29:56 +03:00
|
|
|
if (frameRect.Height() <
|
|
|
|
INT32_MAX / (frameRect.Width() * int32_t(channels))) {
|
|
|
|
const size_t bufferSize =
|
|
|
|
channels * frameRect.Width() * frameRect.Height();
|
2017-06-03 21:37:00 +03:00
|
|
|
|
|
|
|
if (bufferSize > SurfaceCache::MaximumCapacity()) {
|
|
|
|
png_error(decoder->mPNG, "Insufficient memory to deinterlace image");
|
|
|
|
}
|
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
decoder->interlacebuf = static_cast<uint8_t*>(malloc(bufferSize));
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-02-21 01:43:56 +03:00
|
|
|
if (!decoder->interlacebuf) {
|
2016-07-29 06:11:00 +03:00
|
|
|
png_error(decoder->mPNG, "malloc of interlacebuf failed");
|
2007-03-21 02:56:50 +03:00
|
|
|
}
|
2001-02-21 01:43:56 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
void nsPNGDecoder::PostInvalidationIfNeeded() {
|
|
|
|
Maybe<SurfaceInvalidRect> invalidRect = mPipe.TakeInvalidRect();
|
|
|
|
if (!invalidRect) {
|
2015-09-02 00:13:17 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
PostInvalidation(invalidRect->mInputSpaceRect,
|
|
|
|
Some(invalidRect->mOutputSpaceRect));
|
2015-09-02 00:13:17 +03:00
|
|
|
}
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
void nsPNGDecoder::row_callback(png_structp png_ptr, png_bytep new_row,
|
|
|
|
png_uint_32 row_num, int pass) {
|
2001-01-23 01:01:03 +03:00
|
|
|
/* libpng comments:
|
|
|
|
*
|
2015-11-14 13:33:00 +03:00
|
|
|
* This function is called for every row in the image. If the
|
2001-01-23 01:01:03 +03:00
|
|
|
* image is interlacing, and you turned on the interlace handler,
|
|
|
|
* this function will be called for every row in every pass.
|
|
|
|
* Some of these rows will not be changed from the previous pass.
|
2013-08-23 23:51:00 +04:00
|
|
|
* When the row is not changed, the new_row variable will be
|
|
|
|
* nullptr. The rows and passes are called in order, so you don't
|
|
|
|
* really need the row_num and pass, but I'm supplying them
|
|
|
|
* because it may make your life easier.
|
2001-01-23 01:01:03 +03:00
|
|
|
*
|
2013-08-23 23:51:00 +04:00
|
|
|
* For the non-nullptr rows of interlaced images, you must call
|
2001-01-23 01:01:03 +03:00
|
|
|
* png_progressive_combine_row() passing in the row and the
|
2013-08-23 23:51:00 +04:00
|
|
|
* old row. You can call this function for nullptr rows (it will
|
2001-01-23 01:01:03 +03:00
|
|
|
* just return) and for non-interlaced images (it just does the
|
|
|
|
* memcpy for you) if it will make the code easier. Thus, you
|
|
|
|
* can just do this for all cases:
|
|
|
|
*
|
|
|
|
* png_progressive_combine_row(png_ptr, old_row, new_row);
|
|
|
|
*
|
|
|
|
* where old_row is what was displayed for previous rows. Note
|
|
|
|
* that the first pass (pass == 0 really) will completely cover
|
|
|
|
* the old row, so the rows do not have to be initialized. After
|
|
|
|
* the first pass (and only for interlaced images), you will have
|
|
|
|
* to pass the current row, and the function will combine the
|
|
|
|
* old row and the new row.
|
|
|
|
*/
|
2014-11-14 20:59:00 +03:00
|
|
|
nsPNGDecoder* decoder =
|
2016-06-25 01:20:32 +03:00
|
|
|
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
|
2009-12-04 05:41:00 +03:00
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
if (decoder->mFrameIsHidden) {
|
2016-06-25 01:20:32 +03:00
|
|
|
return; // Skip this frame.
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2016-06-27 22:00:16 +03:00
|
|
|
MOZ_ASSERT_IF(decoder->IsFirstFrameDecode(), decoder->mNumFrames == 0);
|
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
while (pass > decoder->mPass) {
|
|
|
|
// Advance to the next pass. We may have to do this multiple times because
|
|
|
|
// libpng will skip passes if the image is so small that no pixels have
|
|
|
|
// changed on a given pass, but ADAM7InterpolatingFilter needs to be reset
|
|
|
|
// once for every pass to perform interpolation properly.
|
|
|
|
decoder->mPipe.ResetToFirstRow();
|
|
|
|
decoder->mPass++;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2010-06-25 23:20:18 +04:00
|
|
|
|
2016-09-07 16:03:00 +03:00
|
|
|
const png_uint_32 height =
|
2017-08-14 15:29:56 +03:00
|
|
|
static_cast<png_uint_32>(decoder->mFrameRect.Height());
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
if (row_num >= height) {
|
|
|
|
// Bail if we receive extra rows. This is especially important because if we
|
|
|
|
// didn't, we might overflow the deinterlacing buffer.
|
|
|
|
MOZ_ASSERT_UNREACHABLE("libpng producing extra rows?");
|
2015-11-14 13:33:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2007-02-28 00:13:25 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
// Note that |new_row| may be null here, indicating that this is an interlaced
|
|
|
|
// image and |row_callback| is being called for a row that hasn't changed.
|
|
|
|
MOZ_ASSERT_IF(!new_row, decoder->interlacebuf);
|
2015-11-14 13:33:00 +03:00
|
|
|
|
|
|
|
if (decoder->interlacebuf) {
|
2017-08-14 15:29:56 +03:00
|
|
|
uint32_t width = uint32_t(decoder->mFrameRect.Width());
|
2016-06-25 01:20:32 +03:00
|
|
|
|
|
|
|
// We'll output the deinterlaced version of the row.
|
2019-08-30 14:27:58 +03:00
|
|
|
uint8_t* rowToWrite =
|
|
|
|
decoder->interlacebuf + (row_num * decoder->mChannels * width);
|
2016-06-25 01:20:32 +03:00
|
|
|
|
|
|
|
// Update the deinterlaced version of this row with the new data.
|
|
|
|
png_progressive_combine_row(png_ptr, rowToWrite, new_row);
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2019-08-30 14:27:58 +03:00
|
|
|
decoder->WriteRow(rowToWrite);
|
|
|
|
} else {
|
|
|
|
decoder->WriteRow(new_row);
|
|
|
|
}
|
2016-06-25 01:20:32 +03:00
|
|
|
}
|
2007-12-21 13:26:31 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
void nsPNGDecoder::WriteRow(uint8_t* aRow) {
|
|
|
|
MOZ_ASSERT(aRow);
|
2008-01-30 09:22:23 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
uint8_t* rowToWrite = aRow;
|
2017-08-14 15:29:56 +03:00
|
|
|
uint32_t width = uint32_t(mFrameRect.Width());
|
2007-12-21 13:26:31 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
// Apply color management to the row, if necessary, before writing it out.
|
2019-10-02 16:37:29 +03:00
|
|
|
// This is only needed for grayscale images.
|
|
|
|
if (mTransform && !mUsePipeTransform) {
|
|
|
|
MOZ_ASSERT(mCMSLine);
|
|
|
|
qcms_transform_data(mTransform, rowToWrite, mCMSLine, width);
|
|
|
|
rowToWrite = mCMSLine;
|
2015-11-14 13:33:00 +03:00
|
|
|
}
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
// Write this row to the SurfacePipe.
|
2019-10-02 16:37:29 +03:00
|
|
|
DebugOnly<WriteState> result =
|
|
|
|
mPipe.WriteBuffer(reinterpret_cast<uint32_t*>(rowToWrite));
|
2016-06-26 00:36:47 +03:00
|
|
|
MOZ_ASSERT(WriteState(result) != WriteState::FAILURE);
|
2015-09-02 00:13:17 +03:00
|
|
|
|
2016-06-25 01:20:32 +03:00
|
|
|
PostInvalidationIfNeeded();
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2016-07-19 09:51:16 +03:00
|
|
|
void nsPNGDecoder::DoTerminate(png_structp aPNGStruct, TerminalState aState) {
|
|
|
|
// Stop processing data. Note that we intentionally ignore the return value of
|
|
|
|
// png_process_data_pause(), which tells us how many bytes of the data that
|
|
|
|
// was passed to png_process_data() have not been consumed yet, because now
|
|
|
|
// that we've reached a terminal state, we won't do any more decoding or call
|
|
|
|
// back into libpng anymore.
|
|
|
|
png_process_data_pause(aPNGStruct, /* save = */ false);
|
|
|
|
|
|
|
|
mNextTransition = aState == TerminalState::SUCCESS
|
|
|
|
? Transition::TerminateSuccess()
|
|
|
|
: Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsPNGDecoder::DoYield(png_structp aPNGStruct) {
|
|
|
|
// Pause data processing. png_process_data_pause() returns how many bytes of
|
|
|
|
// the data that was passed to png_process_data() have not been consumed yet.
|
|
|
|
// We use this information to tell StreamingLexer where to place us in the
|
|
|
|
// input stream when we come back from the yield.
|
2016-09-07 16:03:00 +03:00
|
|
|
png_size_t pendingBytes = png_process_data_pause(aPNGStruct,
|
|
|
|
/* save = */ false);
|
2016-07-19 09:51:16 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(pendingBytes < mLastChunkLength);
|
|
|
|
size_t consumedBytes = mLastChunkLength - min(pendingBytes, mLastChunkLength);
|
|
|
|
|
|
|
|
mNextTransition =
|
|
|
|
Transition::ContinueUnbufferedAfterYield(State::PNG_DATA, consumedBytes);
|
|
|
|
}
|
|
|
|
|
2017-04-05 19:28:40 +03:00
|
|
|
nsresult nsPNGDecoder::FinishInternal() {
|
|
|
|
// We shouldn't be called in error cases.
|
|
|
|
MOZ_ASSERT(!HasError(), "Can't call FinishInternal on error!");
|
|
|
|
|
|
|
|
if (IsMetadataDecode()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t loop_count = 0;
|
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
|
|
|
if (png_get_valid(mPNG, mInfo, PNG_INFO_acTL)) {
|
|
|
|
int32_t num_plays = png_get_num_plays(mPNG, mInfo);
|
|
|
|
loop_count = num_plays - 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (InFrame()) {
|
|
|
|
EndImageFrame();
|
|
|
|
}
|
|
|
|
PostDecodeDone(loop_count);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-04 17:01:34 +04:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-03-21 02:56:50 +03:00
|
|
|
// got the header of a new frame that's coming
|
2010-08-23 06:30:46 +04:00
|
|
|
void nsPNGDecoder::frame_info_callback(png_structp png_ptr,
|
|
|
|
png_uint_32 frame_num) {
|
2014-11-14 20:59:00 +03:00
|
|
|
nsPNGDecoder* decoder =
|
2009-12-04 05:41:00 +03:00
|
|
|
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
|
|
|
|
|
2007-03-21 02:56:50 +03:00
|
|
|
// old frame is done
|
2010-09-12 19:22:30 +04:00
|
|
|
decoder->EndImageFrame();
|
2009-12-04 05:41:00 +03:00
|
|
|
|
2016-08-16 11:08:39 +03:00
|
|
|
const bool previousFrameWasHidden = decoder->mFrameIsHidden;
|
|
|
|
|
|
|
|
if (!previousFrameWasHidden && decoder->IsFirstFrameDecode()) {
|
2015-08-01 04:10:23 +03:00
|
|
|
// We're about to get a second non-hidden frame, but we only want the first.
|
2016-06-27 22:00:16 +03:00
|
|
|
// Stop decoding now. (And avoid allocating the unnecessary buffers below.)
|
2016-07-19 09:51:16 +03:00
|
|
|
return decoder->DoTerminate(png_ptr, TerminalState::SUCCESS);
|
2015-08-01 04:10:23 +03:00
|
|
|
}
|
|
|
|
|
2010-09-12 19:22:30 +04:00
|
|
|
// Only the first frame can be hidden, so unhide unconditionally here.
|
2011-10-17 18:59:28 +04:00
|
|
|
decoder->mFrameIsHidden = false;
|
2009-12-04 05:41:00 +03:00
|
|
|
|
2016-07-19 09:51:16 +03:00
|
|
|
// Save the information necessary to create the frame; we'll actually create
|
|
|
|
// it when we return from the yield.
|
2016-06-25 01:20:32 +03:00
|
|
|
const IntRect frameRect(png_get_next_frame_x_offset(png_ptr, decoder->mInfo),
|
|
|
|
png_get_next_frame_y_offset(png_ptr, decoder->mInfo),
|
|
|
|
png_get_next_frame_width(png_ptr, decoder->mInfo),
|
|
|
|
png_get_next_frame_height(png_ptr, decoder->mInfo));
|
2016-08-16 11:08:39 +03:00
|
|
|
const bool isInterlaced = bool(decoder->interlacebuf);
|
2016-06-25 01:20:32 +03:00
|
|
|
|
2016-08-27 08:23:00 +03:00
|
|
|
# ifndef MOZ_EMBEDDED_LIBPNG
|
2016-08-13 04:31:31 +03:00
|
|
|
// if using system library, check frame_width and height against 0
|
2016-09-07 16:03:00 +03:00
|
|
|
if (frameRect.width == 0) {
|
2016-08-13 04:31:31 +03:00
|
|
|
png_error(png_ptr, "Frame width must not be 0");
|
2016-09-07 16:03:00 +03:00
|
|
|
}
|
|
|
|
if (frameRect.height == 0) {
|
2016-08-13 04:31:31 +03:00
|
|
|
png_error(png_ptr, "Frame height must not be 0");
|
2016-09-07 16:03:00 +03:00
|
|
|
}
|
2016-08-13 04:31:31 +03:00
|
|
|
# endif
|
|
|
|
|
2016-08-18 16:55:45 +03:00
|
|
|
const FrameInfo info{frameRect, isInterlaced};
|
2009-12-04 05:41:00 +03:00
|
|
|
|
2016-08-16 11:08:39 +03:00
|
|
|
// If the previous frame was hidden, skip the yield (which will mislead the
|
|
|
|
// caller, who will think the previous frame was real) and just allocate the
|
|
|
|
// new frame here.
|
|
|
|
if (previousFrameWasHidden) {
|
|
|
|
if (NS_FAILED(decoder->CreateFrame(info))) {
|
|
|
|
return decoder->DoTerminate(png_ptr, TerminalState::FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(decoder->mImageData, "Should have a buffer now");
|
|
|
|
return; // No yield, so we'll just keep decoding.
|
|
|
|
}
|
2016-07-19 09:51:16 +03:00
|
|
|
|
|
|
|
// Yield to the caller to notify them that the previous frame is now complete.
|
2016-08-16 11:08:39 +03:00
|
|
|
decoder->mNextFrameInfo = Some(info);
|
2016-07-19 09:51:16 +03:00
|
|
|
return decoder->DoYield(png_ptr);
|
2007-03-21 02:56:50 +03:00
|
|
|
}
|
2013-12-04 17:01:34 +04:00
|
|
|
#endif
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
void nsPNGDecoder::end_callback(png_structp png_ptr, png_infop info_ptr) {
|
2001-01-23 01:01:03 +03:00
|
|
|
/* libpng comments:
|
|
|
|
*
|
|
|
|
* this function is called when the whole image has been read,
|
|
|
|
* including any chunks after the image (up to and including
|
|
|
|
* the IEND). You will usually have the same info chunk as you
|
|
|
|
* had in the header, although some data may have been added
|
|
|
|
* to the comments and time fields.
|
|
|
|
*
|
|
|
|
* Most people won't do much here, perhaps setting a flag that
|
|
|
|
* marks the image as finished.
|
|
|
|
*/
|
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
nsPNGDecoder* decoder =
|
2009-12-04 05:41:00 +03:00
|
|
|
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
|
2009-09-13 02:44:18 +04:00
|
|
|
|
|
|
|
// We shouldn't get here if we've hit an error
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!decoder->HasError(), "Finishing up PNG but hit error!");
|
2009-12-04 05:41:00 +03:00
|
|
|
|
2016-07-19 09:51:16 +03:00
|
|
|
return decoder->DoTerminate(png_ptr, TerminalState::SUCCESS);
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
void nsPNGDecoder::error_callback(png_structp png_ptr,
|
|
|
|
png_const_charp error_msg) {
|
2015-10-28 23:08:06 +03:00
|
|
|
MOZ_LOG(sPNGLog, LogLevel::Error, ("libpng error: %s\n", error_msg));
|
2014-09-25 01:38:00 +04:00
|
|
|
png_longjmp(png_ptr, 1);
|
2002-09-23 21:34:21 +04:00
|
|
|
}
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
void nsPNGDecoder::warning_callback(png_structp png_ptr,
|
|
|
|
png_const_charp warning_msg) {
|
2015-10-28 23:08:06 +03:00
|
|
|
MOZ_LOG(sPNGLog, LogLevel::Warning, ("libpng warning: %s\n", warning_msg));
|
2002-09-23 21:34:21 +04:00
|
|
|
}
|
2010-03-09 21:22:44 +03:00
|
|
|
|
2016-08-03 02:45:22 +03:00
|
|
|
Maybe<Telemetry::HistogramID> nsPNGDecoder::SpeedHistogram() const {
|
|
|
|
return Some(Telemetry::IMAGE_DECODE_SPEED_PNG);
|
2011-09-23 00:25:56 +04:00
|
|
|
}
|
|
|
|
|
2017-07-22 14:50:31 +03:00
|
|
|
bool nsPNGDecoder::IsValidICOResource() const {
|
2016-07-03 06:22:06 +03:00
|
|
|
// Only 32-bit RGBA PNGs are valid ICO resources; see here:
|
|
|
|
// http://blogs.msdn.com/b/oldnewthing/archive/2010/10/22/10079192.aspx
|
|
|
|
|
|
|
|
// If there are errors in the call to png_get_IHDR, the error_callback in
|
|
|
|
// nsPNGDecoder.cpp is called. In this error callback we do a longjmp, so
|
2016-09-07 16:03:00 +03:00
|
|
|
// we need to save the jump buffer here. Otherwise we'll end up without a
|
2016-07-03 06:22:06 +03:00
|
|
|
// proper callstack.
|
|
|
|
if (setjmp(png_jmpbuf(mPNG))) {
|
|
|
|
// We got here from a longjmp call indirectly from png_get_IHDR
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
png_uint_32 png_width, // Unused
|
|
|
|
png_height; // Unused
|
|
|
|
|
|
|
|
int png_bit_depth, png_color_type;
|
|
|
|
|
|
|
|
if (png_get_IHDR(mPNG, mInfo, &png_width, &png_height, &png_bit_depth,
|
|
|
|
&png_color_type, nullptr, nullptr, nullptr)) {
|
|
|
|
return ((png_color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
|
|
|
|
png_color_type == PNG_COLOR_TYPE_RGB) &&
|
|
|
|
png_bit_depth == 8);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-09-23 00:25:56 +04:00
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-23 06:30:46 +04:00
|
|
|
} // namespace mozilla
|