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
|
2007-02-01 00:09:20 +03:00
|
|
|
#include "gfxColor.h"
|
2014-09-25 01:38:00 +04:00
|
|
|
#include "gfxPlatform.h"
|
2002-04-13 14:03:59 +04:00
|
|
|
#include "nsColor.h"
|
2014-09-25 01:38:00 +04:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsPNGDecoder.h"
|
|
|
|
#include "nsRect.h"
|
2001-10-12 10:43:52 +04:00
|
|
|
#include "nspr.h"
|
|
|
|
#include "png.h"
|
2014-09-25 01:38:00 +04:00
|
|
|
#include "RasterImage.h"
|
2015-02-03 18:05:49 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2001-10-12 10:43:52 +04:00
|
|
|
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2015-08-12 20:41:02 +03:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
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
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
static PRLogModuleInfo*
|
2012-10-30 03:32:10 +04:00
|
|
|
GetPNGLog()
|
|
|
|
{
|
2014-11-14 20:59:00 +03:00
|
|
|
static PRLogModuleInfo* sPNGLog;
|
|
|
|
if (!sPNGLog) {
|
2012-10-30 03:32:10 +04:00
|
|
|
sPNGLog = PR_NewLogModule("PNGDecoder");
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2012-10-30 03:32:10 +04:00
|
|
|
return sPNGLog;
|
|
|
|
}
|
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
static PRLogModuleInfo*
|
2012-10-30 03:32:10 +04:00
|
|
|
GetPNGDecoderAccountingLog()
|
|
|
|
{
|
2014-11-14 20:59:00 +03:00
|
|
|
static PRLogModuleInfo* sPNGDecoderAccountingLog;
|
|
|
|
if (!sPNGDecoderAccountingLog) {
|
2012-10-30 03:32:10 +04:00
|
|
|
sPNGDecoderAccountingLog = PR_NewLogModule("PNGDecoderAccounting");
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2012-10-30 03:32:10 +04:00
|
|
|
return sPNGDecoderAccountingLog;
|
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
// Limit image dimensions (bug #251381, #591822, and #967656)
|
2014-04-01 17:14:14 +04:00
|
|
|
#ifndef MOZ_PNG_MAX_DIMENSION
|
|
|
|
# define MOZ_PNG_MAX_DIMENSION 32767
|
|
|
|
#endif
|
|
|
|
|
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)
|
2013-04-22 18:57:17 +04:00
|
|
|
, mTimeout(0)
|
2014-11-14 20:59:00 +03:00
|
|
|
{ }
|
2012-12-20 00:11:42 +04:00
|
|
|
|
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
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)
|
2013-04-22 18:57:17 +04:00
|
|
|
, mTimeout(0)
|
|
|
|
{
|
|
|
|
png_uint_16 delay_num, delay_den;
|
2014-09-25 01:38:00 +04:00
|
|
|
// delay, in seconds is delay_num/delay_den
|
2013-04-22 18:57:17 +04:00
|
|
|
png_byte dispose_op;
|
|
|
|
png_byte blend_op;
|
|
|
|
delay_num = png_get_next_frame_delay_num(aPNG, aInfo);
|
|
|
|
delay_den = png_get_next_frame_delay_den(aPNG, aInfo);
|
|
|
|
dispose_op = png_get_next_frame_dispose_op(aPNG, aInfo);
|
|
|
|
blend_op = png_get_next_frame_blend_op(aPNG, aInfo);
|
|
|
|
|
|
|
|
if (delay_num == 0) {
|
|
|
|
mTimeout = 0; // SetFrameTimeout() will set to a minimum
|
|
|
|
} else {
|
2014-11-14 20:59:00 +03:00
|
|
|
if (delay_den == 0) {
|
2013-04-22 18:57:17 +04:00
|
|
|
delay_den = 100; // so says the APNG spec
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2013-04-22 18:57:17 +04:00
|
|
|
|
|
|
|
// Need to cast delay_num to float to have a proper division and
|
|
|
|
// the result to int to avoid compiler warning
|
2014-09-25 01:38:00 +04:00
|
|
|
mTimeout = static_cast<int32_t>(static_cast<double>(delay_num) *
|
|
|
|
1000 / delay_den);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-12-20 00:11:42 +04:00
|
|
|
|
2011-08-26 00:09:01 +04:00
|
|
|
// First 8 bytes of a PNG file
|
2013-05-04 13:39:47 +04:00
|
|
|
const uint8_t
|
2011-08-26 00:09:01 +04:00
|
|
|
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)
|
2013-01-19 01:47:18 +04:00
|
|
|
: Decoder(aImage),
|
2012-07-30 18:20:58 +04:00
|
|
|
mPNG(nullptr), mInfo(nullptr),
|
|
|
|
mCMSLine(nullptr), interlacebuf(nullptr),
|
|
|
|
mInProfile(nullptr), mTransform(nullptr),
|
2015-05-26 12:48:00 +03:00
|
|
|
format(gfx::SurfaceFormat::UNKNOWN),
|
2013-05-04 13:39:47 +04:00
|
|
|
mHeaderBytesRead(0), mCMSMode(0),
|
2011-10-17 18:59:28 +04:00
|
|
|
mChannels(0), mFrameIsHidden(false),
|
2013-05-04 13:39:47 +04:00
|
|
|
mDisablePremultipliedAlpha(false),
|
2015-08-01 04:10:23 +03:00
|
|
|
mSuccessfulEarlyFinish(false),
|
2013-02-27 23:23:08 +04:00
|
|
|
mNumFrames(0)
|
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
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
if (mInProfile) {
|
2009-04-07 20:02:11 +04:00
|
|
|
qcms_profile_release(mInProfile);
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
// mTransform belongs to us only if mInProfile is non-null
|
2014-11-14 20:59:00 +03:00
|
|
|
if (mTransform) {
|
2009-04-07 20:02:11 +04:00
|
|
|
qcms_transform_release(mTransform);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2015-08-12 20:41:02 +03:00
|
|
|
void
|
|
|
|
nsPNGDecoder::CheckForTransparency(SurfaceFormat aFormat,
|
|
|
|
const IntRect& aFrameRect)
|
|
|
|
{
|
|
|
|
// Check if the image has a transparent color in its palette.
|
|
|
|
if (aFormat == SurfaceFormat::B8G8R8A8) {
|
|
|
|
PostHasTransparency();
|
|
|
|
}
|
|
|
|
|
|
|
|
// PNGs shouldn't have first-frame padding.
|
|
|
|
MOZ_ASSERT_IF(mNumFrames == 0,
|
|
|
|
IntRect(IntPoint(), GetSize()).IsEqualEdges(aFrameRect));
|
|
|
|
}
|
|
|
|
|
2007-03-21 02:56:50 +03:00
|
|
|
// CreateFrame() is used for both simple and animated images
|
2015-07-11 05:26:15 +03:00
|
|
|
nsresult
|
|
|
|
nsPNGDecoder::CreateFrame(png_uint_32 aXOffset, png_uint_32 aYOffset,
|
|
|
|
int32_t aWidth, int32_t aHeight,
|
|
|
|
gfx::SurfaceFormat aFormat)
|
2007-03-21 02:56:50 +03:00
|
|
|
{
|
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
|
|
|
|
2015-08-12 20:41:02 +03:00
|
|
|
IntRect frameRect(aXOffset, aYOffset, aWidth, aHeight);
|
|
|
|
CheckForTransparency(aFormat, frameRect);
|
2015-07-11 05:26:15 +03:00
|
|
|
|
|
|
|
// XXX(seth): Some tests depend on the first frame of PNGs being B8G8R8A8.
|
|
|
|
// This is something we should fix.
|
|
|
|
gfx::SurfaceFormat format = aFormat;
|
|
|
|
if (mNumFrames == 0) {
|
|
|
|
format = gfx::SurfaceFormat::B8G8R8A8;
|
|
|
|
}
|
2014-11-25 10:42:43 +03:00
|
|
|
|
2015-07-11 05:26:15 +03:00
|
|
|
nsresult rv = AllocateFrame(mNumFrames, GetSize(), frameRect, format);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
2013-05-08 06:25:03 +04:00
|
|
|
}
|
2007-03-21 02:56:50 +03:00
|
|
|
|
2015-07-11 05:26:15 +03:00
|
|
|
mFrameRect = frameRect;
|
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-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetPNGDecoderAccountingLog(), 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",
|
|
|
|
aWidth, aHeight, this));
|
2007-10-19 04:36:34 +04:00
|
|
|
|
2013-04-22 18:57:17 +04:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
|
|
|
if (png_get_valid(mPNG, mInfo, PNG_INFO_acTL)) {
|
|
|
|
mAnimInfo = AnimFrameInfo(mPNG, mInfo);
|
2014-11-25 10:42:43 +03:00
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
if (mAnimInfo.mDispose == DisposalMethod::CLEAR) {
|
2014-11-25 10:42:43 +03:00
|
|
|
// We may have to display the background under this image during
|
|
|
|
// animation playback, so we regard it as transparent.
|
|
|
|
PostHasTransparency();
|
|
|
|
}
|
2013-04-22 18:57:17 +04:00
|
|
|
}
|
|
|
|
#endif
|
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()
|
2008-02-13 13:53:17 +03:00
|
|
|
{
|
2014-11-14 20:59:00 +03:00
|
|
|
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++;
|
|
|
|
|
2015-01-08 11:04:31 +03:00
|
|
|
Opacity opacity = Opacity::SOME_TRANSPARENCY;
|
2015-02-10 08:26:14 +03:00
|
|
|
if (format == gfx::SurfaceFormat::B8G8R8X8) {
|
2015-01-08 00:07:23 +03:00
|
|
|
opacity = Opacity::OPAQUE;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2012-12-20 00:11:42 +04:00
|
|
|
|
2010-01-06 00:46:26 +03:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2012-12-20 00:11:42 +04:00
|
|
|
uint32_t numFrames = GetFrameCount();
|
2008-02-13 13:53:17 +03:00
|
|
|
|
|
|
|
// We can't use mPNG->num_frames_read as it may be one ahead.
|
|
|
|
if (numFrames > 1) {
|
2010-08-25 01:12:04 +04:00
|
|
|
PostInvalidation(mFrameRect);
|
2008-02-13 13:53:17 +03:00
|
|
|
}
|
2010-01-06 00:46:26 +03:00
|
|
|
#endif
|
2008-02-13 13:53:17 +03:00
|
|
|
|
2015-01-08 00:07:23 +03:00
|
|
|
PostFrameStop(opacity, mAnimInfo.mDispose, mAnimInfo.mTimeout,
|
2014-09-25 01:38:00 +04:00
|
|
|
mAnimInfo.mBlend);
|
2008-02-13 13:53:17 +03:00
|
|
|
}
|
|
|
|
|
2010-09-12 19:22:30 +04:00
|
|
|
void
|
2010-08-23 06:30:46 +04:00
|
|
|
nsPNGDecoder::InitInternal()
|
2010-08-22 18:13:09 +04:00
|
|
|
{
|
2013-09-07 07:08:36 +04:00
|
|
|
mCMSMode = gfxPlatform::GetCMSMode();
|
2015-02-03 08:40:35 +03:00
|
|
|
if (GetDecodeFlags() & imgIContainer::FLAG_DECODE_NO_COLORSPACE_CONVERSION) {
|
2011-01-13 04:45:13 +03:00
|
|
|
mCMSMode = eCMSMode_Off;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2015-02-03 08:40:35 +03:00
|
|
|
mDisablePremultipliedAlpha =
|
|
|
|
GetDecodeFlags() & imgIContainer::FLAG_DECODE_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[]=
|
2014-09-25 01:38:00 +04:00
|
|
|
{ 99, 72, 82, 77, '\0', // cHRM
|
|
|
|
105, 67, 67, 80, '\0'}; // iCCP
|
2004-08-04 18:02:34 +04:00
|
|
|
static png_byte unused_chunks[]=
|
2014-09-25 01:38:00 +04:00
|
|
|
{ 98, 75, 71, 68, '\0', // bKGD
|
|
|
|
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,
|
2013-08-23 23:51:00 +04:00
|
|
|
nullptr, nsPNGDecoder::error_callback,
|
2010-08-23 06:30:46 +04:00
|
|
|
nsPNGDecoder::warning_callback);
|
2010-09-12 19:22:27 +04:00
|
|
|
if (!mPNG) {
|
|
|
|
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2010-09-12 19:22:27 +04:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
|
|
|
mInfo = png_create_info_struct(mPNG);
|
|
|
|
if (!mInfo) {
|
2010-09-12 19:22:27 +04:00
|
|
|
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
|
2013-08-23 23:51:00 +04:00
|
|
|
png_destroy_read_struct(&mPNG, nullptr, nullptr);
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
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
|
2014-11-14 20:59:00 +03:00
|
|
|
if (mCMSMode != eCMSMode_Off) {
|
2010-03-12 17:15:00 +03:00
|
|
|
png_set_chunk_malloc_max(mPNG, 4000000L);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
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
|
|
|
|
|
2014-08-22 15:11:00 +04:00
|
|
|
#if defined(PNG_SET_OPTION_SUPPORTED) && 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
|
|
|
|
|
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),
|
2010-08-23 06:30:46 +04:00
|
|
|
nsPNGDecoder::info_callback,
|
|
|
|
nsPNGDecoder::row_callback,
|
|
|
|
nsPNGDecoder::end_callback);
|
2001-01-23 01:01:03 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-09-12 19:22:30 +04:00
|
|
|
void
|
2015-01-08 11:04:31 +03:00
|
|
|
nsPNGDecoder::WriteInternal(const char* aBuffer, uint32_t aCount)
|
2001-01-23 01:01:03 +03:00
|
|
|
{
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!HasError(), "Shouldn't call WriteInternal after error!");
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2015-08-11 01:34:27 +03:00
|
|
|
// libpng uses setjmp/longjmp for error handling. Set it up.
|
|
|
|
if (setjmp(png_jmpbuf(mPNG))) {
|
2013-05-04 13:39:47 +04:00
|
|
|
|
2015-08-11 01:34:27 +03:00
|
|
|
// We exited early. If mSuccessfulEarlyFinish isn't true, then we
|
|
|
|
// encountered an error. We might not really know what caused it, but it
|
|
|
|
// makes more sense to blame the data.
|
|
|
|
if (!mSuccessfulEarlyFinish && !HasError()) {
|
|
|
|
PostDataError();
|
2013-05-04 13:39:47 +04:00
|
|
|
}
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2015-08-11 01:34:27 +03:00
|
|
|
png_destroy_read_struct(&mPNG, &mInfo, nullptr);
|
|
|
|
return;
|
2008-11-07 00:31:20 +03:00
|
|
|
}
|
2015-08-11 01:34:27 +03:00
|
|
|
|
|
|
|
// Pass the data off to libpng.
|
|
|
|
png_process_data(mPNG, mInfo,
|
|
|
|
reinterpret_cast<unsigned char*>(const_cast<char*>((aBuffer))),
|
|
|
|
aCount);
|
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
|
2014-11-14 20:59:00 +03:00
|
|
|
static qcms_profile*
|
2007-07-24 02:02:17 +04:00
|
|
|
PNGGetColorProfile(png_structp png_ptr, png_infop info_ptr,
|
2014-11-14 20:59:00 +03:00
|
|
|
int color_type, qcms_data_type* inType, uint32_t* intent)
|
2007-07-24 02:02:17 +04:00
|
|
|
{
|
2014-11-14 20:59:00 +03:00
|
|
|
qcms_profile* profile = nullptr;
|
2009-04-07 20:02:11 +04:00
|
|
|
*intent = QCMS_INTENT_PERCEPTUAL; // Our default
|
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);
|
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
profile = qcms_profile_from_memory((char*)profileData, profileLen);
|
2009-04-07 20:02:11 +04:00
|
|
|
if (profile) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t profileSpace = qcms_profile_get_color_space(profile);
|
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) {
|
|
|
|
qcms_profile_release(profile);
|
2012-07-30 18:20:58 +04:00
|
|
|
profile = nullptr;
|
2009-04-07 20:02:11 +04:00
|
|
|
} else {
|
|
|
|
*intent = qcms_profile_get_rendering_intent(profile);
|
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check sRGB chunk
|
|
|
|
if (!profile && png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
|
2009-04-07 20:02:11 +04:00
|
|
|
profile = qcms_profile_sRGB();
|
2007-07-24 02:02:17 +04:00
|
|
|
|
|
|
|
if (profile) {
|
|
|
|
int fileIntent;
|
2009-12-04 05:41:00 +03:00
|
|
|
png_set_gray_to_rgb(png_ptr);
|
2007-07-24 02:02:17 +04:00
|
|
|
png_get_sRGB(png_ptr, info_ptr, &fileIntent);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t map[] = { QCMS_INTENT_PERCEPTUAL,
|
2009-12-04 05:41:00 +03:00
|
|
|
QCMS_INTENT_RELATIVE_COLORIMETRIC,
|
|
|
|
QCMS_INTENT_SATURATION,
|
|
|
|
QCMS_INTENT_ABSOLUTE_COLORIMETRIC };
|
2007-07-24 02:02:17 +04:00
|
|
|
*intent = map[fileIntent];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check gAMA/cHRM chunks
|
2009-12-04 05:41:00 +03:00
|
|
|
if (!profile &&
|
2008-08-31 02:54:58 +04:00
|
|
|
png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) &&
|
|
|
|
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);
|
2008-08-31 02:54:58 +04:00
|
|
|
whitePoint.Y =
|
2009-04-07 20:02:11 +04:00
|
|
|
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);
|
|
|
|
|
2009-12-04 05:41:00 +03:00
|
|
|
profile = qcms_profile_create_rgb_with_gamma(whitePoint, primaries,
|
|
|
|
1.0/gammaOfFile);
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
if (profile) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (profile) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t profileSpace = qcms_profile_get_color_space(profile);
|
2007-07-24 02:02:17 +04:00
|
|
|
if (profileSpace == icSigGrayData) {
|
2014-11-14 20:59:00 +03:00
|
|
|
if (color_type & PNG_COLOR_MASK_ALPHA) {
|
2009-04-07 20:02:11 +04:00
|
|
|
*inType = QCMS_DATA_GRAYA_8;
|
2014-11-14 20:59:00 +03:00
|
|
|
} else {
|
2009-04-07 20:02:11 +04:00
|
|
|
*inType = QCMS_DATA_GRAY_8;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
} else {
|
|
|
|
if (color_type & PNG_COLOR_MASK_ALPHA ||
|
2014-11-14 20:59:00 +03:00
|
|
|
png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
2009-04-07 20:02:11 +04:00
|
|
|
*inType = QCMS_DATA_RGBA_8;
|
2014-11-14 20:59:00 +03:00
|
|
|
} else {
|
2009-04-07 20:02:11 +04:00
|
|
|
*inType = QCMS_DATA_RGB_8;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
|
|
|
void
|
2010-08-23 06:30:46 +04:00
|
|
|
nsPNGDecoder::info_callback(png_structp png_ptr, png_infop info_ptr)
|
2001-01-23 01:01:03 +03:00
|
|
|
{
|
2014-09-25 01:38:00 +04:00
|
|
|
// int number_passes; NOT USED
|
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
|
|
|
|
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
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
// Are we too big?
|
2014-11-14 20:59:00 +03:00
|
|
|
if (width > MOZ_PNG_MAX_DIMENSION || height > MOZ_PNG_MAX_DIMENSION) {
|
2014-09-25 01:38:00 +04:00
|
|
|
png_longjmp(decoder->mPNG, 1);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
// Post our size to the superclass
|
|
|
|
decoder->PostSize(width, height);
|
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.
|
2014-09-25 01:38:00 +04:00
|
|
|
png_longjmp(decoder->mPNG, 1);
|
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
|
|
|
|
2013-09-06 02:55:13 +04:00
|
|
|
qcms_data_type inType = QCMS_DATA_RGBA_8;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t intent = -1;
|
|
|
|
uint32_t pIntent;
|
2011-01-13 04:45:13 +03:00
|
|
|
if (decoder->mCMSMode != eCMSMode_Off) {
|
2013-09-07 07:08:36 +04:00
|
|
|
intent = gfxPlatform::GetRenderingIntent();
|
2007-07-24 02:02:17 +04:00
|
|
|
decoder->mInProfile = PNGGetColorProfile(png_ptr, info_ptr,
|
2008-07-23 21:33:12 +04:00
|
|
|
color_type, &inType, &pIntent);
|
2014-09-25 01:38:00 +04:00
|
|
|
// If we're not mandating an intent, use the one from the image.
|
2014-11-14 20:59:00 +03:00
|
|
|
if (intent == uint32_t(-1)) {
|
2008-07-23 21:33:12 +04:00
|
|
|
intent = pIntent;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
2013-09-07 07:08:36 +04:00
|
|
|
if (decoder->mInProfile && gfxPlatform::GetCMSOutputProfile()) {
|
2009-04-07 20:02:11 +04:00
|
|
|
qcms_data_type outType;
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
if (color_type & PNG_COLOR_MASK_ALPHA || num_trans) {
|
2009-04-07 20:02:11 +04:00
|
|
|
outType = QCMS_DATA_RGBA_8;
|
2014-11-14 20:59:00 +03:00
|
|
|
} else {
|
2009-04-07 20:02:11 +04:00
|
|
|
outType = QCMS_DATA_RGB_8;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2008-08-18 09:08:53 +04:00
|
|
|
|
2009-04-07 20:02:11 +04:00
|
|
|
decoder->mTransform = qcms_transform_create(decoder->mInProfile,
|
2009-12-04 05:41:00 +03:00
|
|
|
inType,
|
2013-09-07 07:08:36 +04:00
|
|
|
gfxPlatform::GetCMSOutputProfile(),
|
2009-12-04 05:41:00 +03:00
|
|
|
outType,
|
|
|
|
(qcms_intent)intent);
|
2007-07-24 02:02:17 +04:00
|
|
|
} else {
|
2007-12-04 07:43:09 +03:00
|
|
|
png_set_gray_to_rgb(png_ptr);
|
2008-08-31 02:54:58 +04:00
|
|
|
|
2011-01-13 04:45:13 +03:00
|
|
|
// only do gamma correction if CMS isn't entirely disabled
|
2014-11-14 20:59:00 +03:00
|
|
|
if (decoder->mCMSMode != eCMSMode_Off) {
|
2011-01-13 04:45:13 +03:00
|
|
|
PNGDoGammaCorrection(png_ptr, info_ptr);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2011-01-13 04:45:13 +03:00
|
|
|
|
|
|
|
if (decoder->mCMSMode == eCMSMode_All) {
|
2014-11-14 20:59:00 +03:00
|
|
|
if (color_type & PNG_COLOR_MASK_ALPHA || num_trans) {
|
2013-09-07 07:08:36 +04:00
|
|
|
decoder->mTransform = gfxPlatform::GetCMSRGBATransform();
|
2014-11-14 20:59:00 +03:00
|
|
|
} else {
|
2013-09-07 07:08:36 +04:00
|
|
|
decoder->mTransform = gfxPlatform::GetCMSRGBTransform();
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 01:38:00 +04:00
|
|
|
// let libpng expand interlaced images
|
2001-01-26 15:05:55 +03:00
|
|
|
if (interlace_type == PNG_INTERLACE_ADAM7) {
|
2014-09-25 01:38:00 +04:00
|
|
|
// number_passes =
|
2001-04-16 13:41:01 +04:00
|
|
|
png_set_interlace_handling(png_ptr);
|
2001-01-26 15:05:55 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
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
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
if (channels == 1 || channels == 3) {
|
2014-04-20 05:28:38 +04:00
|
|
|
decoder->format = gfx::SurfaceFormat::B8G8R8X8;
|
2014-11-14 20:59:00 +03:00
|
|
|
} else if (channels == 2 || channels == 4) {
|
2014-04-20 05:28:38 +04:00
|
|
|
decoder->format = gfx::SurfaceFormat::B8G8R8A8;
|
2015-05-26 12:48:00 +03:00
|
|
|
} else {
|
|
|
|
png_longjmp(decoder->mPNG, 1); // invalid number of channels
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
|
2015-08-12 20:41:02 +03:00
|
|
|
if (decoder->IsMetadataDecode()) {
|
|
|
|
decoder->CheckForTransparency(decoder->format,
|
|
|
|
IntRect(0, 0, width, height));
|
|
|
|
|
|
|
|
// We have the size and transparency information we're looking for, so we
|
|
|
|
// don't need to decode any further.
|
|
|
|
decoder->mSuccessfulEarlyFinish = true;
|
|
|
|
png_longjmp(decoder->mPNG, 1);
|
|
|
|
}
|
|
|
|
|
2010-01-06 00:46:26 +03:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2014-11-14 20:59:00 +03:00
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_acTL)) {
|
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
|
2015-07-11 05:26:15 +03:00
|
|
|
nsresult rv = decoder->CreateFrame(0, 0, width, height, decoder->format);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
png_longjmp(decoder->mPNG, 5); // NS_ERROR_OUT_OF_MEMORY
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2007-07-24 02:02:17 +04:00
|
|
|
if (decoder->mTransform &&
|
|
|
|
(channels <= 2 || interlace_type == PNG_INTERLACE_ADAM7)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bpp[] = { 0, 3, 4, 3, 4 };
|
2007-07-24 02:02:17 +04:00
|
|
|
decoder->mCMSLine =
|
2015-02-19 07:51:06 +03:00
|
|
|
(uint8_t*)malloc(bpp[channels] * width);
|
2009-09-13 02:44:18 +04:00
|
|
|
if (!decoder->mCMSLine) {
|
2014-09-25 01:38:00 +04:00
|
|
|
png_longjmp(decoder->mPNG, 5); // NS_ERROR_OUT_OF_MEMORY
|
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) {
|
2014-11-14 20:59:00 +03:00
|
|
|
if (height < INT32_MAX / (width * channels)) {
|
2015-02-19 07:51:06 +03:00
|
|
|
decoder->interlacebuf = (uint8_t*)malloc(channels * width * height);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2001-02-21 01:43:56 +03:00
|
|
|
if (!decoder->interlacebuf) {
|
2014-09-25 01:38:00 +04:00
|
|
|
png_longjmp(decoder->mPNG, 5); // NS_ERROR_OUT_OF_MEMORY
|
2007-03-21 02:56:50 +03:00
|
|
|
}
|
2001-02-21 01:43:56 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-08-23 06:30:46 +04:00
|
|
|
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:
|
|
|
|
*
|
|
|
|
* this function is called for every row in the image. If the
|
|
|
|
* 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 =
|
2009-12-04 05:41:00 +03:00
|
|
|
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
|
|
|
|
|
2008-02-13 13:53:17 +03:00
|
|
|
// skip this frame
|
2014-11-14 20:59:00 +03:00
|
|
|
if (decoder->mFrameIsHidden) {
|
2007-03-21 02:56:50 +03:00
|
|
|
return;
|
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 (row_num >= (png_uint_32) decoder->mFrameRect.height) {
|
2010-06-25 23:20:18 +04:00
|
|
|
return;
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
2010-06-25 23:20:18 +04:00
|
|
|
|
2001-01-23 01:01:03 +03:00
|
|
|
if (new_row) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t width = decoder->mFrameRect.width;
|
|
|
|
uint32_t iwidth = decoder->mFrameRect.width;
|
2001-02-21 01:43:56 +03:00
|
|
|
|
2008-02-13 13:53:17 +03:00
|
|
|
png_bytep line = new_row;
|
|
|
|
if (decoder->interlacebuf) {
|
|
|
|
line = decoder->interlacebuf + (row_num * decoder->mChannels * width);
|
|
|
|
png_progressive_combine_row(png_ptr, line, new_row);
|
|
|
|
}
|
2007-02-28 00:13:25 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bpr = width * sizeof(uint32_t);
|
2014-11-14 20:59:00 +03:00
|
|
|
uint32_t* cptr32 = (uint32_t*)(decoder->mImageData + (row_num*bpr));
|
2007-02-01 00:09:20 +03:00
|
|
|
|
2007-07-24 02:02:17 +04:00
|
|
|
if (decoder->mTransform) {
|
|
|
|
if (decoder->mCMSLine) {
|
2009-12-04 05:41:00 +03:00
|
|
|
qcms_transform_data(decoder->mTransform, line, decoder->mCMSLine,
|
|
|
|
iwidth);
|
2014-09-25 01:38:00 +04:00
|
|
|
// copy alpha over
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t channels = decoder->mChannels;
|
2007-07-24 02:02:17 +04:00
|
|
|
if (channels == 2 || channels == 4) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < iwidth; i++)
|
2007-07-24 02:02:17 +04:00
|
|
|
decoder->mCMSLine[4 * i + 3] = line[channels * i + channels - 1];
|
|
|
|
}
|
|
|
|
line = decoder->mCMSLine;
|
|
|
|
} else {
|
2009-04-07 20:02:11 +04:00
|
|
|
qcms_transform_data(decoder->mTransform, line, line, iwidth);
|
2014-11-14 20:59:00 +03:00
|
|
|
}
|
|
|
|
}
|
2007-07-24 02:02:17 +04:00
|
|
|
|
2008-02-13 13:53:17 +03:00
|
|
|
switch (decoder->format) {
|
2014-11-14 20:59:00 +03:00
|
|
|
case gfx::SurfaceFormat::B8G8R8X8: {
|
2007-12-21 13:26:31 +03:00
|
|
|
// counter for while() loops below
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t idx = iwidth;
|
2007-12-21 13:26:31 +03:00
|
|
|
|
2008-01-30 09:22:23 +03:00
|
|
|
// copy as bytes until source pointer is 32-bit-aligned
|
2008-01-31 04:04:36 +03:00
|
|
|
for (; (NS_PTR_TO_UINT32(line) & 0x3) && idx; --idx) {
|
2012-09-06 09:31:29 +04:00
|
|
|
*cptr32++ = gfxPackedPixel(0xFF, line[0], line[1], line[2]);
|
2009-12-04 05:41:00 +03:00
|
|
|
line += 3;
|
2008-01-30 09:22:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy pixels in blocks of 4
|
|
|
|
while (idx >= 4) {
|
|
|
|
GFX_BLOCK_RGB_TO_FRGB(line, cptr32);
|
2007-12-21 13:26:31 +03:00
|
|
|
idx -= 4;
|
|
|
|
line += 12;
|
|
|
|
cptr32 += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy remaining pixel(s)
|
|
|
|
while (idx--) {
|
|
|
|
// 32-bit read of final pixel will exceed buffer, so read bytes
|
2012-09-06 09:31:29 +04:00
|
|
|
*cptr32++ = gfxPackedPixel(0xFF, line[0], line[1], line[2]);
|
2007-02-01 00:09:20 +03:00
|
|
|
line += 3;
|
2006-03-25 03:34:48 +03:00
|
|
|
}
|
|
|
|
}
|
2001-02-21 01:43:56 +03:00
|
|
|
break;
|
2014-11-14 20:59:00 +03:00
|
|
|
case gfx::SurfaceFormat::B8G8R8A8: {
|
2011-01-13 04:45:13 +03:00
|
|
|
if (!decoder->mDisablePremultipliedAlpha) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t x=width; x>0; --x) {
|
2012-09-06 09:31:29 +04:00
|
|
|
*cptr32++ = gfxPackedPixel(line[3], line[0], line[1], line[2]);
|
2011-01-13 04:45:13 +03:00
|
|
|
line += 4;
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t x=width; x>0; --x) {
|
2014-09-25 01:38:00 +04:00
|
|
|
*cptr32++ = gfxPackedPixelNoPreMultiply(line[3], line[0], line[1],
|
|
|
|
line[2]);
|
2011-01-13 04:45:13 +03:00
|
|
|
line += 4;
|
|
|
|
}
|
2001-02-21 01:43:56 +03:00
|
|
|
}
|
2006-03-25 03:34:48 +03:00
|
|
|
}
|
2003-04-18 18:30:10 +04:00
|
|
|
break;
|
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
|
|
|
default:
|
2014-09-25 01:38:00 +04:00
|
|
|
png_longjmp(decoder->mPNG, 1);
|
2001-02-21 01:43:56 +03:00
|
|
|
}
|
|
|
|
|
2013-02-27 23:23:08 +04:00
|
|
|
if (decoder->mNumFrames <= 1) {
|
2008-02-13 13:53:17 +03:00
|
|
|
// Only do incremental image display for the first frame
|
2010-08-25 01:12:04 +04:00
|
|
|
// XXXbholley - this check should be handled in the superclass
|
2008-02-13 13:53:17 +03:00
|
|
|
nsIntRect r(0, row_num, width, 1);
|
2010-08-25 01:12:04 +04:00
|
|
|
decoder->PostInvalidation(r);
|
2008-02-13 13:53:17 +03:00
|
|
|
}
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
void
|
2010-08-23 06:30:46 +04:00
|
|
|
nsPNGDecoder::frame_info_callback(png_structp png_ptr, png_uint_32 frame_num)
|
2007-03-21 02:56:50 +03:00
|
|
|
{
|
|
|
|
png_uint_32 x_offset, y_offset;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t width, height;
|
2009-12-04 05:41:00 +03:00
|
|
|
|
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
|
|
|
|
2015-08-01 04:10:23 +03:00
|
|
|
if (!decoder->mFrameIsHidden && decoder->IsFirstFrameDecode()) {
|
|
|
|
// We're about to get a second non-hidden frame, but we only want the first.
|
|
|
|
// Stop decoding now.
|
|
|
|
decoder->PostDecodeDone();
|
|
|
|
decoder->mSuccessfulEarlyFinish = true;
|
|
|
|
png_longjmp(decoder->mPNG, 1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2007-03-21 02:56:50 +03:00
|
|
|
x_offset = png_get_next_frame_x_offset(png_ptr, decoder->mInfo);
|
|
|
|
y_offset = png_get_next_frame_y_offset(png_ptr, decoder->mInfo);
|
|
|
|
width = png_get_next_frame_width(png_ptr, decoder->mInfo);
|
|
|
|
height = png_get_next_frame_height(png_ptr, decoder->mInfo);
|
2009-12-04 05:41:00 +03:00
|
|
|
|
2015-07-11 05:26:15 +03:00
|
|
|
nsresult rv =
|
|
|
|
decoder->CreateFrame(x_offset, y_offset, width, height, decoder->format);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
png_longjmp(decoder->mPNG, 5); // NS_ERROR_OUT_OF_MEMORY
|
2013-05-22 05:52:10 +04:00
|
|
|
}
|
2015-07-11 05:26:15 +03:00
|
|
|
MOZ_ASSERT(decoder->mImageData, "Should have a buffer now");
|
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
|
|
|
|
|
|
|
void
|
2010-08-23 06:30:46 +04:00
|
|
|
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
|
|
|
|
2012-12-20 00:11:42 +04:00
|
|
|
int32_t loop_count = 0;
|
2010-01-06 00:46:26 +03:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-03-21 22:12:00 +03:00
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_acTL)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t num_plays = png_get_num_plays(png_ptr, info_ptr);
|
2012-12-20 00:11:42 +04:00
|
|
|
loop_count = num_plays - 1;
|
2007-03-21 02:56:50 +03:00
|
|
|
}
|
2010-01-06 00:46:26 +03:00
|
|
|
#endif
|
2007-10-19 04:36:34 +04:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// Send final notifications
|
2010-09-12 19:22:30 +04:00
|
|
|
decoder->EndImageFrame();
|
2012-12-20 00:11:42 +04:00
|
|
|
decoder->PostDecodeDone(loop_count);
|
2001-01-23 01:01:03 +03:00
|
|
|
}
|
|
|
|
|
2002-09-23 21:34:21 +04:00
|
|
|
|
|
|
|
void
|
2010-08-23 06:30:46 +04:00
|
|
|
nsPNGDecoder::error_callback(png_structp png_ptr, png_const_charp error_msg)
|
2002-09-23 21:34:21 +04:00
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetPNGLog(), 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-08-23 06:30:46 +04:00
|
|
|
nsPNGDecoder::warning_callback(png_structp png_ptr, png_const_charp warning_msg)
|
2002-09-23 21:34:21 +04:00
|
|
|
{
|
2015-06-04 01:25:57 +03:00
|
|
|
MOZ_LOG(GetPNGLog(), LogLevel::Warning, ("libpng warning: %s\n", warning_msg));
|
2002-09-23 21:34:21 +04:00
|
|
|
}
|
2010-03-09 21:22:44 +03:00
|
|
|
|
2011-09-23 00:25:56 +04:00
|
|
|
Telemetry::ID
|
|
|
|
nsPNGDecoder::SpeedHistogram()
|
|
|
|
{
|
|
|
|
return Telemetry::IMAGE_DECODE_SPEED_PNG;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-23 06:30:46 +04:00
|
|
|
} // namespace mozilla
|