2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Stuart Parmenter <stuart@mozilla.com>
|
|
|
|
* Andrew Smith
|
2007-10-18 17:36:34 -07:00
|
|
|
* Federico Mena-Quintero <federico@novell.com>
|
2009-09-12 15:44:18 -07:00
|
|
|
* Bobby Holley <bobbyholley@gmail.com>
|
2010-01-05 13:46:26 -08:00
|
|
|
* Glenn Randers-Pehrson <glennrp@gmail.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsPNGDecoder.h"
|
|
|
|
|
|
|
|
#include "nsMemory.h"
|
|
|
|
#include "nsRect.h"
|
|
|
|
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
|
2010-08-13 21:09:49 -07:00
|
|
|
#include "RasterImage.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "imgIContainerObserver.h"
|
|
|
|
|
|
|
|
#include "gfxColor.h"
|
|
|
|
#include "nsColor.h"
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "png.h"
|
|
|
|
|
2007-07-23 15:02:17 -07:00
|
|
|
#include "gfxPlatform.h"
|
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace imagelib {
|
2010-08-13 21:09:49 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef PR_LOGGING
|
2007-10-18 17:36:34 -07:00
|
|
|
static PRLogModuleInfo *gPNGLog = PR_NewLogModule("PNGDecoder");
|
2009-12-03 18:41:00 -08:00
|
|
|
static PRLogModuleInfo *gPNGDecoderAccountingLog =
|
|
|
|
PR_NewLogModule("PNGDecoderAccounting");
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
/* limit image dimensions (bug #251381) */
|
|
|
|
#define MOZ_PNG_MAX_DIMENSION 1000000L
|
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
// For size decodes
|
2009-09-12 15:44:18 -07:00
|
|
|
#define WIDTH_OFFSET 16
|
|
|
|
#define HEIGHT_OFFSET (WIDTH_OFFSET + 4)
|
|
|
|
#define BYTES_NEEDED_FOR_DIMENSIONS (HEIGHT_OFFSET + 4)
|
|
|
|
|
|
|
|
// This is defined in the PNG spec as an invariant. We use it to
|
|
|
|
// do manual validation without libpng.
|
|
|
|
static const PRUint8 pngSignatureBytes[] =
|
|
|
|
{ 137, 80, 78, 71, 13, 10, 26, 10 };
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPNGDecoder::nsPNGDecoder() :
|
|
|
|
mPNG(nsnull), mInfo(nsnull),
|
2007-07-23 15:02:17 -07:00
|
|
|
mCMSLine(nsnull), interlacebuf(nsnull),
|
|
|
|
mInProfile(nsnull), mTransform(nsnull),
|
2009-09-12 15:44:18 -07:00
|
|
|
mHeaderBuf(nsnull), mHeaderBytesRead(0),
|
2011-01-12 17:45:13 -08:00
|
|
|
mChannels(0), mFrameIsHidden(PR_FALSE),
|
|
|
|
mCMSMode(0), mDisablePremultipliedAlpha(PR_FALSE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPNGDecoder::~nsPNGDecoder()
|
|
|
|
{
|
2010-08-22 19:30:46 -07:00
|
|
|
if (mPNG)
|
|
|
|
png_destroy_read_struct(&mPNG, mInfo ? &mInfo : NULL, NULL);
|
2007-07-23 15:02:17 -07:00
|
|
|
if (mCMSLine)
|
|
|
|
nsMemory::Free(mCMSLine);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (interlacebuf)
|
|
|
|
nsMemory::Free(interlacebuf);
|
2007-07-23 15:02:17 -07:00
|
|
|
if (mInProfile) {
|
2009-04-07 09:02:11 -07:00
|
|
|
qcms_profile_release(mInProfile);
|
2007-07-23 15:02:17 -07:00
|
|
|
|
|
|
|
/* mTransform belongs to us only if mInProfile is non-null */
|
|
|
|
if (mTransform)
|
2009-04-07 09:02:11 -07:00
|
|
|
qcms_transform_release(mTransform);
|
2007-07-23 15:02:17 -07:00
|
|
|
}
|
2009-09-12 15:44:18 -07:00
|
|
|
if (mHeaderBuf)
|
|
|
|
nsMemory::Free(mHeaderBuf);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateFrame() is used for both simple and animated images
|
2009-12-03 18:41:00 -08:00
|
|
|
void nsPNGDecoder::CreateFrame(png_uint_32 x_offset, png_uint_32 y_offset,
|
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-20 18:50:15 -07:00
|
|
|
PRInt32 width, PRInt32 height,
|
|
|
|
gfxASurface::gfxImageFormat format)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
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-20 18:50:15 -07:00
|
|
|
PRUint32 imageDataLength;
|
|
|
|
nsresult rv = mImage->AppendFrame(x_offset, y_offset, width, height, format,
|
|
|
|
&mImageData, &imageDataLength);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
2010-01-05 13:46:26 -08:00
|
|
|
longjmp(png_jmpbuf(mPNG), 5); // NS_ERROR_OUT_OF_MEMORY
|
2007-03-22 10:30:00 -07:00
|
|
|
|
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-20 18:50:15 -07:00
|
|
|
mFrameRect.x = x_offset;
|
|
|
|
mFrameRect.y = y_offset;
|
|
|
|
mFrameRect.width = width;
|
|
|
|
mFrameRect.height = height;
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-03-22 10:30:00 -07:00
|
|
|
if (png_get_valid(mPNG, mInfo, PNG_INFO_acTL))
|
|
|
|
SetAnimFrameInfo();
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
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-20 18:50:15 -07:00
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
// Tell the superclass we're starting a frame
|
|
|
|
PostFrameStart();
|
2007-08-17 12:54:58 -07:00
|
|
|
|
2007-10-18 17:36:34 -07:00
|
|
|
PR_LOG(gPNGDecoderAccountingLog, PR_LOG_DEBUG,
|
2010-01-05 13:46:26 -08:00
|
|
|
("PNGDecoderAccounting: nsPNGDecoder::CreateFrame -- created "
|
|
|
|
"image frame with %dx%d pixels in container %p",
|
2007-10-18 17:36:34 -07:00
|
|
|
width, height,
|
|
|
|
mImage.get ()));
|
|
|
|
|
2007-08-17 12:54:58 -07:00
|
|
|
mFrameHasNoAlpha = PR_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-03-22 10:30:00 -07:00
|
|
|
// set timeout and frame disposal method for the current frame
|
|
|
|
void nsPNGDecoder::SetAnimFrameInfo()
|
|
|
|
{
|
2007-11-13 03:03:32 -08:00
|
|
|
png_uint_16 delay_num, delay_den;
|
|
|
|
/* delay, in seconds is delay_num/delay_den */
|
2007-03-22 16:35:51 -07:00
|
|
|
png_byte dispose_op;
|
2007-10-23 12:33:20 -07:00
|
|
|
png_byte blend_op;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 timeout; /* in milliseconds */
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
delay_num = png_get_next_frame_delay_num(mPNG, mInfo);
|
|
|
|
delay_den = png_get_next_frame_delay_den(mPNG, mInfo);
|
2007-03-22 16:35:51 -07:00
|
|
|
dispose_op = png_get_next_frame_dispose_op(mPNG, mInfo);
|
2007-10-23 12:33:20 -07:00
|
|
|
blend_op = png_get_next_frame_blend_op(mPNG, mInfo);
|
2007-03-22 16:35:51 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (delay_num == 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-20 18:50:15 -07:00
|
|
|
timeout = 0; // SetFrameTimeout() will set to a minimum
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
if (delay_den == 0)
|
2007-11-13 03:03:32 -08:00
|
|
|
delay_den = 100; // so says the APNG spec
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Need to cast delay_num to float to have a proper division and
|
|
|
|
// the result to int to avoid compiler warning
|
2007-07-08 00:08:04 -07:00
|
|
|
timeout = static_cast<PRInt32>
|
2009-12-03 18:41:00 -08:00
|
|
|
(static_cast<PRFloat64>(delay_num) * 1000 / delay_den);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
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-20 18:50:15 -07:00
|
|
|
|
2010-08-22 19:30:44 -07:00
|
|
|
PRUint32 numFrames = mImage->GetNumFrames();
|
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-20 18:50:15 -07:00
|
|
|
|
|
|
|
mImage->SetFrameTimeout(numFrames - 1, timeout);
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2007-03-22 16:35:51 -07:00
|
|
|
if (dispose_op == PNG_DISPOSE_OP_PREVIOUS)
|
2009-12-03 18:41:00 -08:00
|
|
|
mImage->SetFrameDisposalMethod(numFrames - 1,
|
2010-08-13 21:09:49 -07:00
|
|
|
RasterImage::kDisposeRestorePrevious);
|
2007-03-22 16:35:51 -07:00
|
|
|
else if (dispose_op == PNG_DISPOSE_OP_BACKGROUND)
|
2009-12-03 18:41:00 -08:00
|
|
|
mImage->SetFrameDisposalMethod(numFrames - 1,
|
2010-08-13 21:09:49 -07:00
|
|
|
RasterImage::kDisposeClear);
|
2007-03-22 10:30:00 -07:00
|
|
|
else
|
2009-12-03 18:41:00 -08:00
|
|
|
mImage->SetFrameDisposalMethod(numFrames - 1,
|
2010-08-13 21:09:49 -07:00
|
|
|
RasterImage::kDisposeKeep);
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2007-10-23 12:33:20 -07:00
|
|
|
if (blend_op == PNG_BLEND_OP_SOURCE)
|
2010-08-13 21:09:49 -07:00
|
|
|
mImage->SetFrameBlendMethod(numFrames - 1, RasterImage::kBlendSource);
|
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-20 18:50:15 -07:00
|
|
|
/*else // 'over' is the default
|
2010-08-13 21:09:49 -07:00
|
|
|
mImage->SetFrameBlendMethod(numFrames - 1, RasterImage::kBlendOver); */
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-02-13 02:53:17 -08:00
|
|
|
// set timeout and frame disposal method for the current frame
|
|
|
|
void nsPNGDecoder::EndImageFrame()
|
|
|
|
{
|
2010-09-12 08:22:30 -07:00
|
|
|
if (mFrameIsHidden)
|
|
|
|
return;
|
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
PRUint32 numFrames = 1;
|
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2010-08-22 19:30:44 -07:00
|
|
|
numFrames = mImage->GetNumFrames();
|
2008-02-13 02:53:17 -08:00
|
|
|
|
|
|
|
// We can't use mPNG->num_frames_read as it may be one ahead.
|
|
|
|
if (numFrames > 1) {
|
|
|
|
// Tell the image renderer that the frame is complete
|
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-20 18:50:15 -07:00
|
|
|
if (mFrameHasNoAlpha)
|
|
|
|
mImage->SetFrameHasNoAlpha(numFrames - 1);
|
2008-02-13 02:53:17 -08:00
|
|
|
|
2010-08-24 14:12:04 -07:00
|
|
|
PostInvalidation(mFrameRect);
|
2008-02-13 02:53:17 -08:00
|
|
|
}
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2008-02-13 02:53:17 -08:00
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
PostFrameStop();
|
2008-02-13 02:53:17 -08:00
|
|
|
}
|
|
|
|
|
2010-09-12 08:22:30 -07:00
|
|
|
void
|
2010-08-22 19:30:46 -07:00
|
|
|
nsPNGDecoder::InitInternal()
|
2010-08-22 07:13:09 -07:00
|
|
|
{
|
2011-01-12 17:45:13 -08:00
|
|
|
mCMSMode = gfxPlatform::GetCMSMode();
|
|
|
|
if ((mDecodeFlags & DECODER_NO_COLORSPACE_CONVERSION) != 0)
|
|
|
|
mCMSMode = eCMSMode_Off;
|
|
|
|
mDisablePremultipliedAlpha = (mDecodeFlags & DECODER_NO_PREMULTIPLY_ALPHA) != 0;
|
2010-08-22 19:30:46 -07:00
|
|
|
|
2010-03-09 10:22:44 -08:00
|
|
|
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
|
2007-11-06 20:29:37 -08:00
|
|
|
static png_byte color_chunks[]=
|
|
|
|
{ 99, 72, 82, 77, '\0', /* cHRM */
|
|
|
|
105, 67, 67, 80, '\0'}; /* iCCP */
|
2007-03-22 10:30:00 -07:00
|
|
|
static png_byte unused_chunks[]=
|
|
|
|
{ 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 */
|
|
|
|
#endif
|
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
// For size decodes, we only need a small buffer
|
|
|
|
if (IsSizeDecode()) {
|
2010-09-12 08:22:31 -07:00
|
|
|
mHeaderBuf = (PRUint8 *)moz_xmalloc(BYTES_NEEDED_FOR_DIMENSIONS);
|
2010-09-12 08:22:30 -07:00
|
|
|
return;
|
2009-09-12 15:44:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* For full decodes, do png init stuff */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* Initialize the container's source image header. */
|
|
|
|
/* Always decode to 24 bit pixdepth */
|
|
|
|
|
2010-03-12 06:15:00 -08:00
|
|
|
mPNG = png_create_read_struct(PNG_LIBPNG_VER_STRING,
|
2010-08-22 19:30:46 -07:00
|
|
|
NULL, nsPNGDecoder::error_callback,
|
|
|
|
nsPNGDecoder::warning_callback);
|
2010-09-12 08:22:27 -07:00
|
|
|
if (!mPNG) {
|
|
|
|
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
|
2010-09-12 08:22:30 -07:00
|
|
|
return;
|
2010-09-12 08:22:27 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
mInfo = png_create_info_struct(mPNG);
|
|
|
|
if (!mInfo) {
|
2010-09-12 08:22:27 -07:00
|
|
|
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
png_destroy_read_struct(&mPNG, NULL, NULL);
|
2010-09-12 08:22:30 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-03-09 10:22:44 -08:00
|
|
|
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
|
2007-03-22 10:30:00 -07:00
|
|
|
/* Ignore unused chunks */
|
2011-01-12 17:45:13 -08:00
|
|
|
if (mCMSMode == eCMSMode_Off)
|
2007-11-06 20:29:37 -08:00
|
|
|
png_set_keep_unknown_chunks(mPNG, 1, color_chunks, 2);
|
2010-01-05 13:46:26 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
png_set_keep_unknown_chunks(mPNG, 1, unused_chunks,
|
2010-03-12 06:15:00 -08:00
|
|
|
(int)sizeof(unused_chunks)/5);
|
2010-03-09 10:22:44 -08:00
|
|
|
#endif
|
|
|
|
|
2010-03-12 06:15:00 -08:00
|
|
|
#ifdef PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
|
2011-01-12 17:45:13 -08:00
|
|
|
if (mCMSMode != eCMSMode_Off)
|
2010-03-12 06:15:00 -08:00
|
|
|
png_set_chunk_malloc_max(mPNG, 4000000L);
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* use this as libpng "progressive pointer" (retrieve in callbacks) */
|
2007-07-08 00:08:04 -07:00
|
|
|
png_set_progressive_read_fn(mPNG, static_cast<png_voidp>(this),
|
2010-08-22 19:30:46 -07:00
|
|
|
nsPNGDecoder::info_callback,
|
|
|
|
nsPNGDecoder::row_callback,
|
|
|
|
nsPNGDecoder::end_callback);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-09-12 08:22:30 -07:00
|
|
|
void
|
2010-08-22 19:30:46 -07:00
|
|
|
nsPNGDecoder::WriteInternal(const char *aBuffer, PRUint32 aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-09-12 15:44:18 -07:00
|
|
|
// We use gotos, so we need to declare variables here
|
|
|
|
PRUint32 width = 0;
|
|
|
|
PRUint32 height = 0;
|
|
|
|
|
2010-09-12 08:22:31 -07:00
|
|
|
NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
// If we only want width/height, we don't need to go through libpng
|
2010-08-22 19:30:46 -07:00
|
|
|
if (IsSizeDecode()) {
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
// Are we done?
|
|
|
|
if (mHeaderBytesRead == BYTES_NEEDED_FOR_DIMENSIONS)
|
2010-09-12 08:22:30 -07:00
|
|
|
return;
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
// Read data into our header buffer
|
2009-12-03 18:41:00 -08:00
|
|
|
PRUint32 bytesToRead = PR_MIN(aCount, BYTES_NEEDED_FOR_DIMENSIONS -
|
|
|
|
mHeaderBytesRead);
|
2009-09-12 15:44:18 -07:00
|
|
|
memcpy(mHeaderBuf + mHeaderBytesRead, aBuffer, bytesToRead);
|
|
|
|
mHeaderBytesRead += bytesToRead;
|
|
|
|
|
|
|
|
// If we're done now, verify the data and set up the container
|
|
|
|
if (mHeaderBytesRead == BYTES_NEEDED_FOR_DIMENSIONS) {
|
|
|
|
|
|
|
|
// Check that the signature bytes are right
|
2010-09-12 08:22:27 -07:00
|
|
|
if (memcmp(mHeaderBuf, pngSignatureBytes, sizeof(pngSignatureBytes))) {
|
|
|
|
PostDataError();
|
2010-09-12 08:22:30 -07:00
|
|
|
return;
|
2010-09-12 08:22:27 -07:00
|
|
|
}
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
// Grab the width and height, accounting for endianness (thanks libpng!)
|
|
|
|
width = png_get_uint_32(mHeaderBuf + WIDTH_OFFSET);
|
|
|
|
height = png_get_uint_32(mHeaderBuf + HEIGHT_OFFSET);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// Too big?
|
2010-09-12 08:22:27 -07:00
|
|
|
if ((width > MOZ_PNG_MAX_DIMENSION) || (height > MOZ_PNG_MAX_DIMENSION)) {
|
|
|
|
PostDataError();
|
2010-09-12 08:22:30 -07:00
|
|
|
return;
|
2010-09-12 08:22:27 -07:00
|
|
|
}
|
2009-09-12 15:44:18 -07:00
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
// Post our size to the superclass
|
|
|
|
PostSize(width, height);
|
2009-09-12 15:44:18 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// Otherwise, we're doing a standard decode
|
|
|
|
else {
|
|
|
|
|
|
|
|
// libpng uses setjmp/longjmp for error handling - set the buffer
|
2010-01-05 13:46:26 -08:00
|
|
|
if (setjmp(png_jmpbuf(mPNG))) {
|
2010-09-12 08:22:27 -07:00
|
|
|
|
|
|
|
// We might not really know what caused the error, but it makes more
|
|
|
|
// sense to blame the data.
|
2010-09-12 08:22:31 -07:00
|
|
|
if (!HasError())
|
2010-09-12 08:22:27 -07:00
|
|
|
PostDataError();
|
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
png_destroy_read_struct(&mPNG, &mInfo, NULL);
|
2010-09-12 08:22:30 -07:00
|
|
|
return;
|
2009-09-12 15:44:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pass the data off to libpng
|
2009-10-15 19:54:44 -07:00
|
|
|
png_process_data(mPNG, mInfo, (unsigned char *)aBuffer, aCount);
|
2009-09-12 15:44:18 -07:00
|
|
|
|
2008-11-06 13:31:20 -08:00
|
|
|
}
|
2009-09-12 15:44:18 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-12-03 18:41:00 -08:00
|
|
|
// Sets up gamma pre-correction in libpng before our callback gets called.
|
2008-08-30 15:54:58 -07: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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
png_set_gamma(png_ptr, 2.2, 0.45455);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-07-23 15:02:17 -07:00
|
|
|
// Adapted from http://www.littlecms.com/pngchrm.c example code
|
2009-04-07 09:02:11 -07:00
|
|
|
static qcms_profile *
|
2007-07-23 15:02:17 -07:00
|
|
|
PNGGetColorProfile(png_structp png_ptr, png_infop info_ptr,
|
2009-04-07 09:02:11 -07:00
|
|
|
int color_type, qcms_data_type *inType, PRUint32 *intent)
|
2007-07-23 15:02:17 -07:00
|
|
|
{
|
2009-04-07 09:02:11 -07:00
|
|
|
qcms_profile *profile = nsnull;
|
|
|
|
*intent = QCMS_INTENT_PERCEPTUAL; // Our default
|
2007-07-23 15:02:17 -07: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;
|
|
|
|
char *profileData, *profileName;
|
|
|
|
int compression;
|
|
|
|
|
|
|
|
png_get_iCCP(png_ptr, info_ptr, &profileName, &compression,
|
|
|
|
&profileData, &profileLen);
|
|
|
|
|
2009-04-07 09:02:11 -07:00
|
|
|
profile = qcms_profile_from_memory(profileData, profileLen);
|
|
|
|
if (profile) {
|
|
|
|
PRUint32 profileSpace = qcms_profile_get_color_space(profile);
|
2007-07-23 15:02:17 -07:00
|
|
|
|
2009-04-07 09:02:11 -07:00
|
|
|
PRBool mismatch = PR_FALSE;
|
|
|
|
if (color_type & PNG_COLOR_MASK_COLOR) {
|
|
|
|
if (profileSpace != icSigRgbData)
|
|
|
|
mismatch = PR_TRUE;
|
|
|
|
} else {
|
|
|
|
if (profileSpace == icSigRgbData)
|
|
|
|
png_set_gray_to_rgb(png_ptr);
|
|
|
|
else if (profileSpace != icSigGrayData)
|
|
|
|
mismatch = PR_TRUE;
|
|
|
|
}
|
2007-07-23 15:02:17 -07:00
|
|
|
|
2009-04-07 09:02:11 -07:00
|
|
|
if (mismatch) {
|
|
|
|
qcms_profile_release(profile);
|
|
|
|
profile = nsnull;
|
|
|
|
} else {
|
|
|
|
*intent = qcms_profile_get_rendering_intent(profile);
|
|
|
|
}
|
2007-07-23 15:02:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check sRGB chunk
|
|
|
|
if (!profile && png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) {
|
2009-04-07 09:02:11 -07:00
|
|
|
profile = qcms_profile_sRGB();
|
2007-07-23 15:02:17 -07:00
|
|
|
|
|
|
|
if (profile) {
|
|
|
|
int fileIntent;
|
2009-12-03 18:41:00 -08:00
|
|
|
png_set_gray_to_rgb(png_ptr);
|
2007-07-23 15:02:17 -07:00
|
|
|
png_get_sRGB(png_ptr, info_ptr, &fileIntent);
|
2009-12-03 18:41:00 -08:00
|
|
|
PRUint32 map[] = { QCMS_INTENT_PERCEPTUAL,
|
|
|
|
QCMS_INTENT_RELATIVE_COLORIMETRIC,
|
|
|
|
QCMS_INTENT_SATURATION,
|
|
|
|
QCMS_INTENT_ABSOLUTE_COLORIMETRIC };
|
2007-07-23 15:02:17 -07:00
|
|
|
*intent = map[fileIntent];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check gAMA/cHRM chunks
|
2009-12-03 18:41:00 -08:00
|
|
|
if (!profile &&
|
2008-08-30 15:54:58 -07:00
|
|
|
png_get_valid(png_ptr, info_ptr, PNG_INFO_gAMA) &&
|
|
|
|
png_get_valid(png_ptr, info_ptr, PNG_INFO_cHRM)) {
|
2009-04-07 09:02:11 -07:00
|
|
|
qcms_CIE_xyYTRIPLE primaries;
|
|
|
|
qcms_CIE_xyY whitePoint;
|
2008-08-30 15:54:58 -07:00
|
|
|
|
|
|
|
png_get_cHRM(png_ptr, info_ptr,
|
|
|
|
&whitePoint.x, &whitePoint.y,
|
2009-04-07 09:02:11 -07:00
|
|
|
&primaries.red.x, &primaries.red.y,
|
|
|
|
&primaries.green.x, &primaries.green.y,
|
|
|
|
&primaries.blue.x, &primaries.blue.y);
|
2008-08-30 15:54:58 -07:00
|
|
|
whitePoint.Y =
|
2009-04-07 09:02:11 -07:00
|
|
|
primaries.red.Y = primaries.green.Y = primaries.blue.Y = 1.0;
|
2007-07-23 15:02:17 -07:00
|
|
|
|
|
|
|
double gammaOfFile;
|
|
|
|
|
|
|
|
png_get_gAMA(png_ptr, info_ptr, &gammaOfFile);
|
|
|
|
|
2009-12-03 18:41:00 -08:00
|
|
|
profile = qcms_profile_create_rgb_with_gamma(whitePoint, primaries,
|
|
|
|
1.0/gammaOfFile);
|
2007-07-23 15:02:17 -07:00
|
|
|
|
2007-12-03 20:43:09 -08:00
|
|
|
if (profile)
|
2007-07-23 15:02:17 -07:00
|
|
|
png_set_gray_to_rgb(png_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (profile) {
|
2009-04-07 09:02:11 -07:00
|
|
|
PRUint32 profileSpace = qcms_profile_get_color_space(profile);
|
2007-07-23 15:02:17 -07:00
|
|
|
if (profileSpace == icSigGrayData) {
|
|
|
|
if (color_type & PNG_COLOR_MASK_ALPHA)
|
2009-04-07 09:02:11 -07:00
|
|
|
*inType = QCMS_DATA_GRAYA_8;
|
2007-07-23 15:02:17 -07:00
|
|
|
else
|
2009-04-07 09:02:11 -07:00
|
|
|
*inType = QCMS_DATA_GRAY_8;
|
2007-07-23 15:02:17 -07:00
|
|
|
} else {
|
|
|
|
if (color_type & PNG_COLOR_MASK_ALPHA ||
|
|
|
|
png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
|
2009-04-07 09:02:11 -07:00
|
|
|
*inType = QCMS_DATA_RGBA_8;
|
2007-07-23 15:02:17 -07:00
|
|
|
else
|
2009-04-07 09:02:11 -07:00
|
|
|
*inType = QCMS_DATA_RGB_8;
|
2007-07-23 15:02:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return profile;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void
|
2010-08-22 19:30:46 -07:00
|
|
|
nsPNGDecoder::info_callback(png_structp png_ptr, png_infop info_ptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
/* int number_passes; NOT USED */
|
|
|
|
png_uint_32 width, height;
|
|
|
|
int bit_depth, color_type, interlace_type, compression_type, filter_type;
|
2008-07-11 13:52:30 -07:00
|
|
|
unsigned int channels;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
png_bytep trans = NULL;
|
|
|
|
int num_trans = 0;
|
|
|
|
|
2009-12-03 18:41:00 -08:00
|
|
|
nsPNGDecoder *decoder =
|
|
|
|
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* always decode to 24-bit RGB or 32-bit RGBA */
|
|
|
|
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
|
|
|
|
&interlace_type, &compression_type, &filter_type);
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
/* Are we too big? */
|
2007-03-22 10:30:00 -07:00
|
|
|
if (width > MOZ_PNG_MAX_DIMENSION || height > MOZ_PNG_MAX_DIMENSION)
|
2010-01-05 13:46:26 -08:00
|
|
|
longjmp(png_jmpbuf(decoder->mPNG), 1);
|
2009-09-12 15:44:18 -07:00
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
// Post our size to the superclass
|
|
|
|
decoder->PostSize(width, height);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (color_type == PNG_COLOR_TYPE_PALETTE)
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
|
|
|
|
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
|
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
2008-08-19 01:12:31 -07:00
|
|
|
int sample_max = (1 << bit_depth);
|
|
|
|
png_color_16p trans_values;
|
|
|
|
png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &trans_values);
|
|
|
|
/* libpng doesn't reject a tRNS chunk with out-of-range samples
|
|
|
|
so we check it here to avoid setting up a useless opacity
|
|
|
|
channel or producing unexpected transparent pixels when using
|
|
|
|
libpng-1.2.19 through 1.2.26 (bug #428045) */
|
|
|
|
if ((color_type == PNG_COLOR_TYPE_GRAY &&
|
|
|
|
(int)trans_values->gray > sample_max) ||
|
|
|
|
(color_type == PNG_COLOR_TYPE_RGB &&
|
|
|
|
((int)trans_values->red > sample_max ||
|
|
|
|
(int)trans_values->green > sample_max ||
|
|
|
|
(int)trans_values->blue > sample_max)))
|
2010-01-05 13:46:26 -08:00
|
|
|
{
|
|
|
|
/* clear the tRNS valid flag and release tRNS memory */
|
|
|
|
png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0);
|
|
|
|
}
|
2008-08-19 01:12:31 -07:00
|
|
|
else
|
2010-01-05 13:46:26 -08:00
|
|
|
png_set_expand(png_ptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bit_depth == 16)
|
|
|
|
png_set_strip_16(png_ptr);
|
|
|
|
|
2009-04-07 09:02:11 -07:00
|
|
|
qcms_data_type inType;
|
2009-12-03 18:41:00 -08:00
|
|
|
PRUint32 intent = -1;
|
|
|
|
PRUint32 pIntent;
|
2011-01-12 17:45:13 -08:00
|
|
|
if (decoder->mCMSMode != eCMSMode_Off) {
|
2008-07-23 10:33:12 -07:00
|
|
|
intent = gfxPlatform::GetRenderingIntent();
|
2007-07-23 15:02:17 -07:00
|
|
|
decoder->mInProfile = PNGGetColorProfile(png_ptr, info_ptr,
|
2008-07-23 10:33:12 -07:00
|
|
|
color_type, &inType, &pIntent);
|
|
|
|
/* If we're not mandating an intent, use the one from the image. */
|
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-20 18:50:15 -07:00
|
|
|
if (intent == PRUint32(-1))
|
2008-07-23 10:33:12 -07:00
|
|
|
intent = pIntent;
|
2007-07-23 15:02:17 -07:00
|
|
|
}
|
|
|
|
if (decoder->mInProfile && gfxPlatform::GetCMSOutputProfile()) {
|
2009-04-07 09:02:11 -07:00
|
|
|
qcms_data_type outType;
|
2007-07-23 15:02:17 -07:00
|
|
|
|
2007-09-10 13:01:18 -07:00
|
|
|
if (color_type & PNG_COLOR_MASK_ALPHA || num_trans)
|
2009-04-07 09:02:11 -07:00
|
|
|
outType = QCMS_DATA_RGBA_8;
|
2007-07-23 15:02:17 -07:00
|
|
|
else
|
2009-04-07 09:02:11 -07:00
|
|
|
outType = QCMS_DATA_RGB_8;
|
2008-08-17 22:08:53 -07:00
|
|
|
|
2009-04-07 09:02:11 -07:00
|
|
|
decoder->mTransform = qcms_transform_create(decoder->mInProfile,
|
2009-12-03 18:41:00 -08:00
|
|
|
inType,
|
|
|
|
gfxPlatform::GetCMSOutputProfile(),
|
|
|
|
outType,
|
|
|
|
(qcms_intent)intent);
|
2007-07-23 15:02:17 -07:00
|
|
|
} else {
|
2007-12-03 20:43:09 -08:00
|
|
|
png_set_gray_to_rgb(png_ptr);
|
2008-08-30 15:54:58 -07:00
|
|
|
|
2011-01-12 17:45:13 -08:00
|
|
|
// only do gamma correction if CMS isn't entirely disabled
|
|
|
|
if (decoder->mCMSMode != eCMSMode_Off)
|
|
|
|
PNGDoGammaCorrection(png_ptr, info_ptr);
|
|
|
|
|
|
|
|
if (decoder->mCMSMode == eCMSMode_All) {
|
2007-09-10 13:01:18 -07:00
|
|
|
if (color_type & PNG_COLOR_MASK_ALPHA || num_trans)
|
2007-07-23 15:02:17 -07:00
|
|
|
decoder->mTransform = gfxPlatform::GetCMSRGBATransform();
|
|
|
|
else
|
|
|
|
decoder->mTransform = gfxPlatform::GetCMSRGBTransform();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* let libpng expand interlaced images */
|
|
|
|
if (interlace_type == PNG_INTERLACE_ADAM7) {
|
|
|
|
/* number_passes = */
|
|
|
|
png_set_interlace_handling(png_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
png_read_update_info(png_ptr, info_ptr);
|
2007-07-23 15:02:17 -07:00
|
|
|
decoder->mChannels = channels = png_get_channels(png_ptr, info_ptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------*/
|
|
|
|
/* copy PNG info into imagelib structs (formerly png_set_dims()) */
|
|
|
|
/*---------------------------------------------------------------*/
|
|
|
|
|
|
|
|
PRInt32 alpha_bits = 1;
|
|
|
|
|
2007-07-23 15:02:17 -07:00
|
|
|
if (channels == 2 || channels == 4) {
|
2007-03-22 10:30:00 -07:00
|
|
|
/* check if alpha is coming from a tRNS chunk and is binary */
|
|
|
|
if (num_trans) {
|
2008-08-19 01:12:31 -07:00
|
|
|
/* if it's not an indexed color image, tRNS means binary */
|
2007-03-22 10:30:00 -07:00
|
|
|
if (color_type == PNG_COLOR_TYPE_PALETTE) {
|
|
|
|
for (int i=0; i<num_trans; i++) {
|
|
|
|
if ((trans[i] != 0) && (trans[i] != 255)) {
|
|
|
|
alpha_bits = 8;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alpha_bits = 8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-20 18:50:15 -07:00
|
|
|
if (channels == 1 || channels == 3)
|
|
|
|
decoder->format = gfxASurface::ImageFormatRGB24;
|
|
|
|
else if (channels == 2 || channels == 4)
|
|
|
|
decoder->format = gfxASurface::ImageFormatARGB32;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-03-22 10:30:00 -07:00
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_acTL))
|
2010-08-22 19:30:46 -07:00
|
|
|
png_set_progressive_frame_fn(png_ptr, nsPNGDecoder::frame_info_callback, NULL);
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2007-04-19 13:40:53 -07:00
|
|
|
if (png_get_first_frame_is_hidden(png_ptr, info_ptr)) {
|
2008-02-13 02:53:17 -08:00
|
|
|
decoder->mFrameIsHidden = PR_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
decoder->CreateFrame(0, 0, width, height, decoder->format);
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2007-07-23 15:02:17 -07:00
|
|
|
if (decoder->mTransform &&
|
|
|
|
(channels <= 2 || interlace_type == PNG_INTERLACE_ADAM7)) {
|
|
|
|
PRUint32 bpp[] = { 0, 3, 4, 3, 4 };
|
|
|
|
decoder->mCMSLine =
|
2010-09-12 08:22:31 -07:00
|
|
|
(PRUint8 *)moz_malloc(bpp[channels] * width);
|
2009-09-12 15:44:18 -07:00
|
|
|
if (!decoder->mCMSLine) {
|
2010-01-05 13:46:26 -08:00
|
|
|
longjmp(png_jmpbuf(decoder->mPNG), 5); // NS_ERROR_OUT_OF_MEMORY
|
2009-09-12 15:44:18 -07:00
|
|
|
}
|
2007-07-23 15:02:17 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (interlace_type == PNG_INTERLACE_ADAM7) {
|
2008-07-16 02:20:24 -07:00
|
|
|
if (height < PR_INT32_MAX / (width * channels))
|
2010-09-12 08:22:31 -07:00
|
|
|
decoder->interlacebuf = (PRUint8 *)moz_malloc(channels * width * height);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!decoder->interlacebuf) {
|
2010-01-05 13:46:26 -08:00
|
|
|
longjmp(png_jmpbuf(decoder->mPNG), 5); // NS_ERROR_OUT_OF_MEMORY
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2009-06-28 16:08:40 -07:00
|
|
|
/* Reject any ancillary chunk after IDAT with a bad CRC (bug #397593).
|
|
|
|
* It would be better to show the default frame (if one has already been
|
|
|
|
* successfully decoded) before bailing, but it's simpler to just bail
|
|
|
|
* out with an error message.
|
|
|
|
*/
|
2011-01-31 19:45:10 -08:00
|
|
|
png_set_crc_action(png_ptr, PNG_CRC_NO_CHANGE, PNG_CRC_ERROR_QUIT);
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-08-22 19:30:46 -07:00
|
|
|
nsPNGDecoder::row_callback(png_structp png_ptr, png_bytep new_row,
|
|
|
|
png_uint_32 row_num, int pass)
|
2007-03-22 10:30:00 -07: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.
|
|
|
|
* When the row is not changed, the new_row variable will be NULL.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* For the non-NULL rows of interlaced images, you must call
|
|
|
|
* png_progressive_combine_row() passing in the row and the
|
|
|
|
* old row. You can call this function for NULL rows (it will
|
|
|
|
* 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.
|
|
|
|
*/
|
2009-12-03 18:41:00 -08:00
|
|
|
nsPNGDecoder *decoder =
|
|
|
|
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
|
|
|
|
|
2008-02-13 02:53:17 -08:00
|
|
|
// skip this frame
|
|
|
|
if (decoder->mFrameIsHidden)
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
|
2010-08-22 19:30:44 -07:00
|
|
|
if (row_num >= (png_uint_32) decoder->mFrameRect.height)
|
2010-06-25 12:20:18 -07:00
|
|
|
return;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (new_row) {
|
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-20 18:50:15 -07:00
|
|
|
PRInt32 width = decoder->mFrameRect.width;
|
|
|
|
PRUint32 iwidth = decoder->mFrameRect.width;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-02-13 02:53:17 -08: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-03-22 10:30:00 -07:00
|
|
|
|
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-20 18:50:15 -07:00
|
|
|
PRUint32 bpr = width * sizeof(PRUint32);
|
|
|
|
PRUint32 *cptr32 = (PRUint32*)(decoder->mImageData + (row_num*bpr));
|
2007-08-17 12:54:58 -07:00
|
|
|
PRBool rowHasNoAlpha = PR_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-07-23 15:02:17 -07:00
|
|
|
if (decoder->mTransform) {
|
|
|
|
if (decoder->mCMSLine) {
|
2009-12-03 18:41:00 -08:00
|
|
|
qcms_transform_data(decoder->mTransform, line, decoder->mCMSLine,
|
|
|
|
iwidth);
|
2007-07-23 15:02:17 -07:00
|
|
|
/* copy alpha over */
|
|
|
|
PRUint32 channels = decoder->mChannels;
|
|
|
|
if (channels == 2 || channels == 4) {
|
|
|
|
for (PRUint32 i = 0; i < iwidth; i++)
|
|
|
|
decoder->mCMSLine[4 * i + 3] = line[channels * i + channels - 1];
|
|
|
|
}
|
|
|
|
line = decoder->mCMSLine;
|
|
|
|
} else {
|
2009-04-07 09:02:11 -07:00
|
|
|
qcms_transform_data(decoder->mTransform, line, line, iwidth);
|
2007-07-23 15:02:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-13 02:53:17 -08:00
|
|
|
switch (decoder->format) {
|
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-20 18:50:15 -07:00
|
|
|
case gfxASurface::ImageFormatRGB24:
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-12-21 02:26:31 -08:00
|
|
|
// counter for while() loops below
|
|
|
|
PRUint32 idx = iwidth;
|
|
|
|
|
2008-01-29 22:22:23 -08:00
|
|
|
// copy as bytes until source pointer is 32-bit-aligned
|
2008-01-30 17:04:36 -08:00
|
|
|
for (; (NS_PTR_TO_UINT32(line) & 0x3) && idx; --idx) {
|
2008-01-29 22:22:23 -08:00
|
|
|
*cptr32++ = GFX_PACKED_PIXEL(0xFF, line[0], line[1], line[2]);
|
2009-12-03 18:41:00 -08:00
|
|
|
line += 3;
|
2008-01-29 22:22:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy pixels in blocks of 4
|
|
|
|
while (idx >= 4) {
|
|
|
|
GFX_BLOCK_RGB_TO_FRGB(line, cptr32);
|
2007-12-21 02:26:31 -08: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
|
2007-03-22 10:30:00 -07:00
|
|
|
*cptr32++ = GFX_PACKED_PIXEL(0xFF, line[0], line[1], line[2]);
|
|
|
|
line += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-20 18:50:15 -07:00
|
|
|
case gfxASurface::ImageFormatARGB32:
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-01-12 17:45:13 -08:00
|
|
|
if (!decoder->mDisablePremultipliedAlpha) {
|
|
|
|
for (PRUint32 x=width; x>0; --x) {
|
|
|
|
*cptr32++ = GFX_PACKED_PIXEL(line[3], line[0], line[1], line[2]);
|
|
|
|
if (line[3] != 0xff)
|
|
|
|
rowHasNoAlpha = PR_FALSE;
|
|
|
|
line += 4;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (PRUint32 x=width; x>0; --x) {
|
|
|
|
*cptr32++ = GFX_PACKED_PIXEL_NO_PREMULTIPLY(line[3], line[0], line[1], line[2]);
|
|
|
|
if (line[3] != 0xff)
|
|
|
|
rowHasNoAlpha = PR_FALSE;
|
|
|
|
line += 4;
|
|
|
|
}
|
2007-03-22 10:30:00 -07: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-20 18:50:15 -07:00
|
|
|
default:
|
2010-09-12 08:22:27 -07:00
|
|
|
longjmp(png_jmpbuf(decoder->mPNG), 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-17 12:54:58 -07:00
|
|
|
if (!rowHasNoAlpha)
|
|
|
|
decoder->mFrameHasNoAlpha = PR_FALSE;
|
|
|
|
|
2010-08-22 19:30:44 -07:00
|
|
|
PRUint32 numFrames = decoder->mImage->GetNumFrames();
|
2008-02-13 02:53:17 -08:00
|
|
|
if (numFrames <= 1) {
|
|
|
|
// Only do incremental image display for the first frame
|
2010-08-24 14:12:04 -07:00
|
|
|
// XXXbholley - this check should be handled in the superclass
|
2008-02-13 02:53:17 -08:00
|
|
|
nsIntRect r(0, row_num, width, 1);
|
2010-08-24 14:12:04 -07:00
|
|
|
decoder->PostInvalidation(r);
|
2008-02-13 02:53:17 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// got the header of a new frame that's coming
|
|
|
|
void
|
2010-08-22 19:30:46 -07:00
|
|
|
nsPNGDecoder::frame_info_callback(png_structp png_ptr, png_uint_32 frame_num)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-03-22 10:30:00 -07:00
|
|
|
png_uint_32 x_offset, y_offset;
|
|
|
|
PRInt32 width, height;
|
2009-12-03 18:41:00 -08:00
|
|
|
|
|
|
|
nsPNGDecoder *decoder =
|
|
|
|
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// old frame is done
|
2010-09-12 08:22:30 -07:00
|
|
|
decoder->EndImageFrame();
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2010-09-12 08:22:30 -07:00
|
|
|
// Only the first frame can be hidden, so unhide unconditionally here.
|
2008-02-13 02:53:17 -08:00
|
|
|
decoder->mFrameIsHidden = PR_FALSE;
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2007-03-22 10:30:00 -07: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-03 18:41:00 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
decoder->CreateFrame(x_offset, y_offset, width, height, decoder->format);
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-08-22 19:30:46 -07:00
|
|
|
nsPNGDecoder::end_callback(png_structp png_ptr, png_infop info_ptr)
|
2007-03-22 10:30:00 -07: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.
|
|
|
|
*/
|
|
|
|
|
2009-12-03 18:41:00 -08:00
|
|
|
nsPNGDecoder *decoder =
|
|
|
|
static_cast<nsPNGDecoder*>(png_get_progressive_ptr(png_ptr));
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
// We shouldn't get here if we've hit an error
|
2010-09-12 08:22:31 -07:00
|
|
|
NS_ABORT_IF_FALSE(!decoder->HasError(), "Finishing up PNG but hit error!");
|
2009-12-03 18:41:00 -08:00
|
|
|
|
2010-01-05 13:46:26 -08:00
|
|
|
#ifdef PNG_APNG_SUPPORTED
|
2007-03-22 10:30:00 -07:00
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_acTL)) {
|
2007-04-19 13:40:53 -07:00
|
|
|
PRInt32 num_plays = png_get_num_plays(png_ptr, info_ptr);
|
|
|
|
decoder->mImage->SetLoopCount(num_plays - 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2010-01-05 13:46:26 -08:00
|
|
|
#endif
|
2007-10-18 17:36:34 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// Send final notifications
|
2010-09-12 08:22:30 -07:00
|
|
|
decoder->EndImageFrame();
|
|
|
|
decoder->PostDecodeDone();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-08-22 19:30:46 -07:00
|
|
|
nsPNGDecoder::error_callback(png_structp png_ptr, png_const_charp error_msg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PR_LOG(gPNGLog, PR_LOG_ERROR, ("libpng error: %s\n", error_msg));
|
2010-01-05 13:46:26 -08:00
|
|
|
longjmp(png_jmpbuf(png_ptr), 1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2010-08-22 19:30:46 -07:00
|
|
|
nsPNGDecoder::warning_callback(png_structp png_ptr, png_const_charp warning_msg)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
PR_LOG(gPNGLog, PR_LOG_WARNING, ("libpng warning: %s\n", warning_msg));
|
|
|
|
}
|
2010-03-09 10:22:44 -08:00
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
} // namespace imagelib
|
|
|
|
} // namespace mozilla
|