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):
|
|
|
|
* Chris Saari <saari@netscape.com>
|
2009-09-12 15:44:18 -07:00
|
|
|
* Bobby Holley <bobbyholley@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 ***** */
|
2007-06-25 09:41:21 -07:00
|
|
|
/*
|
|
|
|
The Graphics Interchange Format(c) is the copyright property of CompuServe
|
|
|
|
Incorporated. Only CompuServe Incorporated is authorized to define, redefine,
|
|
|
|
enhance, alter, modify or change in any way the definition of the format.
|
|
|
|
|
|
|
|
CompuServe Incorporated hereby grants a limited, non-exclusive, royalty-free
|
|
|
|
license for the use of the Graphics Interchange Format(sm) in computer
|
|
|
|
software; computer software utilizing GIF(sm) must acknowledge ownership of the
|
|
|
|
Graphics Interchange Format and its Service Mark by CompuServe Incorporated, in
|
|
|
|
User and Technical Documentation. Computer software utilizing GIF, which is
|
|
|
|
distributed or may be distributed without User or Technical Documentation must
|
|
|
|
display to the screen or printer a message acknowledging ownership of the
|
|
|
|
Graphics Interchange Format and the Service Mark by CompuServe Incorporated; in
|
|
|
|
this case, the acknowledgement may be displayed in an opening screen or leading
|
|
|
|
banner, or a closing screen or trailing banner. A message such as the following
|
|
|
|
may be used:
|
|
|
|
|
|
|
|
"The Graphics Interchange Format(c) is the Copyright property of
|
|
|
|
CompuServe Incorporated. GIF(sm) is a Service Mark property of
|
|
|
|
CompuServe Incorporated."
|
|
|
|
|
|
|
|
For further information, please contact :
|
|
|
|
|
|
|
|
CompuServe Incorporated
|
|
|
|
Graphics Technology Department
|
|
|
|
5000 Arlington Center Boulevard
|
|
|
|
Columbus, Ohio 43220
|
|
|
|
U. S. A.
|
|
|
|
|
|
|
|
CompuServe Incorporated maintains a mailing list with all those individuals and
|
|
|
|
organizations who wish to receive copies of this document when it is corrected
|
|
|
|
or revised. This service is offered free of charge; please provide us with your
|
|
|
|
mailing address.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "prmem.h"
|
|
|
|
|
2007-08-17 12:54:58 -07:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsGIFDecoder2.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "imgIContainerObserver.h"
|
|
|
|
|
2007-08-25 22:08:53 -07:00
|
|
|
#include "gfxColor.h"
|
2007-07-23 15:02:17 -07:00
|
|
|
#include "gfxPlatform.h"
|
2009-04-07 09:02:11 -07:00
|
|
|
#include "qcms.h"
|
2007-07-23 15:02:17 -07:00
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
/*
|
|
|
|
* GETN(n, s) requests at least 'n' bytes available from 'q', at start of state 's'
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
2007-06-25 09:41:21 -07:00
|
|
|
* Note, the hold will never need to be bigger than 256 bytes to gather up in the hold,
|
|
|
|
* as each GIF block (except colormaps) can never be bigger than 256 bytes.
|
2007-11-07 13:33:57 -08:00
|
|
|
* Colormaps are directly copied in the resp. global_colormap or the local_colormap of the PAL image frame
|
2007-06-25 09:41:21 -07:00
|
|
|
* So a fixed buffer in gif_struct is good enough.
|
|
|
|
* This buffer is only needed to copy left-over data from one GifWrite call to the next
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2007-06-25 09:41:21 -07:00
|
|
|
#define GETN(n,s) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
mGIFStruct.bytes_to_consume = (n); \
|
|
|
|
mGIFStruct.state = (s); \
|
|
|
|
PR_END_MACRO
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
/* Get a 16-bit value stored in little-endian format */
|
|
|
|
#define GETINT16(p) ((p)[1]<<8|(p)[0])
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// GIF Decoder Implementation
|
|
|
|
// This is an adaptor between GIF2 and imgIDecoder
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsGIFDecoder2, imgIDecoder)
|
|
|
|
|
|
|
|
nsGIFDecoder2::nsGIFDecoder2()
|
|
|
|
: mCurrentRow(-1)
|
|
|
|
, mLastFlushedRow(-1)
|
2007-08-25 22:08:53 -07:00
|
|
|
, mImageData(nsnull)
|
2008-02-18 23:36:50 -08:00
|
|
|
, mOldColor(0)
|
2009-07-21 21:20:01 -07:00
|
|
|
, mCurrentFrame(-1)
|
2007-03-22 10:30:00 -07:00
|
|
|
, mCurrentPass(0)
|
|
|
|
, mLastFlushedPass(0)
|
|
|
|
, mGIFOpen(PR_FALSE)
|
2007-11-27 01:35:18 -08:00
|
|
|
, mSawTransparency(PR_FALSE)
|
2009-09-12 15:44:18 -07:00
|
|
|
, mError(PR_FALSE)
|
2009-09-13 11:53:55 -07:00
|
|
|
, mEnded(PR_FALSE)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-06-25 09:41:21 -07:00
|
|
|
// Clear out the structure, excluding the arrays
|
|
|
|
memset(&mGIFStruct, 0, sizeof(mGIFStruct));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
nsGIFDecoder2::~nsGIFDecoder2()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
/** imgIDecoder methods **/
|
|
|
|
//******************************************************************************
|
|
|
|
|
|
|
|
//******************************************************************************
|
2009-09-12 15:44:18 -07:00
|
|
|
/* void init (in imgIContainer aImage,
|
|
|
|
in imgIDecoderObserver aObsever,
|
|
|
|
in unsigned long aFlags); */
|
|
|
|
NS_IMETHODIMP nsGIFDecoder2::Init(imgIContainer *aImage,
|
|
|
|
imgIDecoderObserver *aObserver,
|
|
|
|
PRUint32 aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-09-12 15:44:18 -07:00
|
|
|
// Store parameters
|
|
|
|
mImageContainer = aImage;
|
|
|
|
mObserver = aObserver;
|
|
|
|
mFlags = aFlags;
|
|
|
|
|
|
|
|
// Fire OnStartDecode at init time to support bug 512435
|
|
|
|
if (!(mFlags & imgIDecoder::DECODER_FLAG_HEADERONLY) && mObserver)
|
|
|
|
mObserver->OnStartDecode(nsnull);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
// Start with the version (GIF89a|GIF87a)
|
|
|
|
mGIFStruct.state = gif_type;
|
|
|
|
mGIFStruct.bytes_to_consume = 6;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
/** nsIOutputStream methods **/
|
|
|
|
//******************************************************************************
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
/* void close (); */
|
2009-09-12 15:44:18 -07:00
|
|
|
NS_IMETHODIMP nsGIFDecoder2::Close(PRUint32 aFlags)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-09-12 15:44:18 -07:00
|
|
|
// Send notifications if appropriate
|
|
|
|
if (!(mFlags & imgIDecoder::DECODER_FLAG_HEADERONLY) &&
|
|
|
|
!mError && !(aFlags & CLOSE_FLAG_DONTNOTIFY)) {
|
|
|
|
if (mCurrentFrame == mGIFStruct.images_decoded)
|
|
|
|
EndImageFrame();
|
|
|
|
EndGIF(/* aSuccess = */ PR_TRUE);
|
|
|
|
}
|
2007-06-25 09:41:21 -07:00
|
|
|
|
|
|
|
PR_FREEIF(mGIFStruct.local_colormap);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
mImageContainer = nsnull;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
/* void flush (); */
|
|
|
|
NS_IMETHODIMP nsGIFDecoder2::Flush()
|
|
|
|
{
|
2008-09-02 04:59:59 -07:00
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push any new rows according to mCurrentPass/mLastFlushedPass and
|
|
|
|
// mCurrentRow/mLastFlushedRow. Note: caller is responsible for
|
|
|
|
// updating mlastFlushed{Row,Pass}.
|
2008-09-10 13:23:29 -07:00
|
|
|
nsresult
|
2007-08-25 22:08:53 -07:00
|
|
|
nsGIFDecoder2::FlushImageData(PRUint32 fromRow, PRUint32 rows)
|
|
|
|
{
|
2009-05-13 03:20:36 -07:00
|
|
|
nsIntRect r(0, fromRow, mGIFStruct.width, rows);
|
2007-08-25 22:08:53 -07:00
|
|
|
|
|
|
|
// Update 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
|
|
|
nsresult rv = mImageContainer->FrameUpdated(mGIFStruct.images_decoded, r);
|
2008-09-10 13:23:29 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2007-08-25 22:08:53 -07:00
|
|
|
|
|
|
|
// Offset to the frame position
|
|
|
|
// Only notify observer(s) for first frame
|
|
|
|
if (!mGIFStruct.images_decoded && mObserver) {
|
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 imgCurFrame;
|
|
|
|
mImageContainer->GetCurrentFrameIndex(&imgCurFrame);
|
2007-08-25 22:08:53 -07:00
|
|
|
r.y += mGIFStruct.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
|
|
|
mObserver->OnDataAvailable(nsnull, imgCurFrame == PRUint32(mGIFStruct.images_decoded), &r);
|
2007-08-25 22:08:53 -07:00
|
|
|
}
|
2008-09-10 13:23:29 -07:00
|
|
|
return NS_OK;
|
2007-08-25 22:08:53 -07:00
|
|
|
}
|
|
|
|
|
2008-09-10 13:23:29 -07:00
|
|
|
nsresult
|
2007-03-22 10:30:00 -07:00
|
|
|
nsGIFDecoder2::FlushImageData()
|
|
|
|
{
|
2008-09-10 13:23:29 -07:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (mCurrentPass - mLastFlushedPass) {
|
2007-08-25 22:08:53 -07:00
|
|
|
case 0: // same pass
|
|
|
|
if (mCurrentRow - mLastFlushedRow)
|
2008-09-10 13:23:29 -07:00
|
|
|
rv = FlushImageData(mLastFlushedRow + 1, mCurrentRow - mLastFlushedRow);
|
2007-08-25 22:08:53 -07:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-25 22:08:53 -07:00
|
|
|
case 1: // one pass on - need to handle bottom & top rects
|
2008-09-10 13:23:29 -07:00
|
|
|
rv = FlushImageData(0, mCurrentRow + 1);
|
|
|
|
rv |= FlushImageData(mLastFlushedRow + 1, mGIFStruct.height - (mLastFlushedRow + 1));
|
2007-08-25 22:08:53 -07:00
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-25 22:08:53 -07:00
|
|
|
default: // more than one pass on - push the whole frame
|
2008-09-10 13:23:29 -07:00
|
|
|
rv = FlushImageData(0, mGIFStruct.height);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-09-10 13:23:29 -07:00
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
2009-10-15 19:54:44 -07:00
|
|
|
/* void write (in string aBuffer, in PRUint32 aCount); */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGIFDecoder2::Write(const char *aBuffer, PRUint32 aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-10-15 19:54:44 -07:00
|
|
|
// Don't forgive previously flagged errors
|
|
|
|
if (mError)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Push the data to the GIF decoder
|
2009-10-15 19:54:44 -07:00
|
|
|
nsresult rv = GifWrite((const unsigned char *)aBuffer, aCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-11-07 13:33:57 -08:00
|
|
|
// Flushing is only needed for first frame
|
2009-10-15 19:54:44 -07:00
|
|
|
if (NS_SUCCEEDED(rv) && !mGIFStruct.images_decoded) {
|
2008-09-10 13:23:29 -07:00
|
|
|
rv = FlushImageData();
|
2009-09-12 15:44:18 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-03-22 10:30:00 -07:00
|
|
|
mLastFlushedRow = mCurrentRow;
|
|
|
|
mLastFlushedPass = mCurrentPass;
|
|
|
|
}
|
|
|
|
|
2009-10-15 19:54:44 -07:00
|
|
|
// We do some fine-grained error control here. If we have at least one frame
|
|
|
|
// of an animated gif, we still want to display it (mostly for legacy reasons).
|
|
|
|
// libpr0n code is strict, so we have to lie and tell it we were successful. So
|
|
|
|
// if we have something to salvage, we send off final decode notifications, and
|
|
|
|
// pretend that we're decoded. Otherwise, we set mError.
|
|
|
|
if (NS_FAILED(rv)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-15 19:54:44 -07:00
|
|
|
// Determine if we want to salvage the situation
|
|
|
|
PRUint32 numFrames = 0;
|
|
|
|
if (mImageContainer)
|
|
|
|
mImageContainer->GetNumFrames(&numFrames);
|
|
|
|
|
|
|
|
// If we're salvaging, send off notifications
|
|
|
|
// Note that we need to make sure that we have 2 frames, since that tells us
|
|
|
|
// that the first frame is complete (the second could be in any state).
|
|
|
|
if (numFrames > 1) {
|
|
|
|
EndGIF(/* aSuccess = */ PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, set mError
|
|
|
|
else
|
|
|
|
mError = PR_TRUE;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-15 19:54:44 -07:00
|
|
|
return mError ? NS_ERROR_FAILURE : NS_OK;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
// GIF decoder callback methods. Part of public API for GIF2
|
|
|
|
//******************************************************************************
|
|
|
|
|
|
|
|
//******************************************************************************
|
2007-06-25 09:41:21 -07:00
|
|
|
void nsGIFDecoder2::BeginGIF()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-25 22:08:53 -07:00
|
|
|
if (mGIFOpen)
|
2007-06-25 09:41:21 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
mGIFOpen = PR_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
mImageContainer->SetSize(mGIFStruct.screen_width, mGIFStruct.screen_height);
|
2007-06-25 09:41:21 -07:00
|
|
|
if (mObserver)
|
|
|
|
mObserver->OnStartContainer(nsnull, mImageContainer);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// If we're doing a header-only decode, we have what we came for
|
|
|
|
if (mFlags & imgIDecoder::DECODER_FLAG_HEADERONLY)
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
2009-09-12 15:44:18 -07:00
|
|
|
void nsGIFDecoder2::EndGIF(PRBool aSuccess)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-09-13 11:53:55 -07:00
|
|
|
if (mEnded)
|
2007-06-25 09:41:21 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
if (aSuccess)
|
|
|
|
mImageContainer->DecodingComplete();
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
if (mObserver) {
|
|
|
|
mObserver->OnStopContainer(nsnull, mImageContainer);
|
2009-09-12 15:44:18 -07:00
|
|
|
mObserver->OnStopDecode(nsnull, aSuccess ? NS_OK : NS_ERROR_FAILURE,
|
|
|
|
nsnull);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-09-12 15:44:18 -07:00
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
mImageContainer->SetLoopCount(mGIFStruct.loop_count);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
mGIFOpen = PR_FALSE;
|
2009-09-13 11:53:55 -07:00
|
|
|
mEnded = PR_TRUE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
2009-09-04 18:47:11 -07:00
|
|
|
nsresult nsGIFDecoder2::BeginImageFrame(gfx_depth aDepth)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-06-25 09:41:21 -07:00
|
|
|
if (!mGIFStruct.images_decoded) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// Send a onetime OnDataAvailable (Display Refresh) for the first frame
|
|
|
|
// if it has a y-axis offset. Otherwise, the area may never be refreshed
|
|
|
|
// and the placeholder will remain on the screen. (Bug 37589)
|
2007-06-25 09:41:21 -07:00
|
|
|
if (mGIFStruct.y_offset > 0) {
|
|
|
|
PRInt32 imgWidth;
|
|
|
|
mImageContainer->GetWidth(&imgWidth);
|
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 imgCurFrame;
|
|
|
|
mImageContainer->GetCurrentFrameIndex(&imgCurFrame);
|
2007-06-25 09:41:21 -07:00
|
|
|
nsIntRect r(0, 0, imgWidth, mGIFStruct.y_offset);
|
2009-09-12 15:44:18 -07:00
|
|
|
if (mObserver)
|
|
|
|
mObserver->OnDataAvailable(nsnull,
|
|
|
|
imgCurFrame == PRUint32(mGIFStruct.images_decoded),
|
|
|
|
&r);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-06-25 09:41:21 -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;
|
|
|
|
gfxASurface::gfxImageFormat format;
|
|
|
|
if (mGIFStruct.is_transparent)
|
|
|
|
format = gfxASurface::ImageFormatARGB32;
|
|
|
|
else
|
|
|
|
format = gfxASurface::ImageFormatRGB24;
|
|
|
|
|
2007-11-07 13:33:57 -08:00
|
|
|
// Use correct format, RGB for first frame, PAL for following frames
|
|
|
|
// and include transparency to allow for optimization of opaque images
|
|
|
|
if (mGIFStruct.images_decoded) {
|
|
|
|
// Image data is stored with original depth and palette
|
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
|
|
|
rv = mImageContainer->AppendPalettedFrame(mGIFStruct.x_offset, mGIFStruct.y_offset,
|
|
|
|
mGIFStruct.width, mGIFStruct.height,
|
|
|
|
format, aDepth, &mImageData, &imageDataLength,
|
|
|
|
&mColormap, &mColormapSize);
|
2007-11-07 13:33:57 -08:00
|
|
|
} else {
|
|
|
|
// Regardless of depth of input, image is decoded into 24bit RGB
|
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
|
|
|
rv = mImageContainer->AppendFrame(mGIFStruct.x_offset, mGIFStruct.y_offset,
|
|
|
|
mGIFStruct.width, mGIFStruct.height,
|
|
|
|
format, &mImageData, &imageDataLength);
|
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
|
|
|
if (NS_FAILED(rv))
|
2009-09-04 18:47:11 -07:00
|
|
|
return rv;
|
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
|
|
|
mImageContainer->SetFrameDisposalMethod(mGIFStruct.images_decoded,
|
|
|
|
mGIFStruct.disposal_method);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-25 22:08:53 -07:00
|
|
|
if (mObserver)
|
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
|
|
|
mObserver->OnStartFrame(nsnull, mGIFStruct.images_decoded);
|
2009-07-21 21:20:01 -07:00
|
|
|
|
|
|
|
mCurrentFrame = mGIFStruct.images_decoded;
|
2009-09-04 18:47:11 -07:00
|
|
|
return NS_OK;
|
2007-08-25 22:08:53 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
//******************************************************************************
|
2007-08-25 22:08:53 -07:00
|
|
|
void nsGIFDecoder2::EndImageFrame()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2007-08-25 22:08:53 -07:00
|
|
|
// First flush all pending image data
|
|
|
|
if (!mGIFStruct.images_decoded) {
|
2007-11-07 13:33:57 -08:00
|
|
|
// Only need to flush first frame
|
2008-09-10 13:23:29 -07:00
|
|
|
(void) FlushImageData();
|
2007-11-07 13:33:57 -08:00
|
|
|
|
2007-08-25 22:08:53 -07:00
|
|
|
// If the first frame is smaller in height than the entire image, send a
|
|
|
|
// OnDataAvailable (Display Refresh) for the area it does not have data for.
|
|
|
|
// This will clear the remaining bits of the placeholder. (Bug 37589)
|
|
|
|
const PRUint32 realFrameHeight = mGIFStruct.height + mGIFStruct.y_offset;
|
|
|
|
if (realFrameHeight < mGIFStruct.screen_height) {
|
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 imgCurFrame;
|
|
|
|
mImageContainer->GetCurrentFrameIndex(&imgCurFrame);
|
2009-09-12 15:44:18 -07:00
|
|
|
nsIntRect r(0, realFrameHeight,
|
|
|
|
mGIFStruct.screen_width,
|
|
|
|
mGIFStruct.screen_height - realFrameHeight);
|
|
|
|
if (mObserver)
|
|
|
|
mObserver->OnDataAvailable(nsnull,
|
|
|
|
imgCurFrame == PRUint32(mGIFStruct.images_decoded),
|
|
|
|
&r);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-11-27 01:35:18 -08:00
|
|
|
// This transparency check is only valid for first frame
|
|
|
|
if (mGIFStruct.is_transparent && !mSawTransparency) {
|
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
|
|
|
mImageContainer->SetFrameHasNoAlpha(mGIFStruct.images_decoded);
|
2007-11-27 01:35:18 -08:00
|
|
|
}
|
2007-08-25 22:08:53 -07:00
|
|
|
}
|
2007-11-07 13:33:57 -08:00
|
|
|
mCurrentRow = mLastFlushedRow = -1;
|
|
|
|
mCurrentPass = mLastFlushedPass = 0;
|
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 curframe = mGIFStruct.images_decoded;
|
|
|
|
|
2008-01-08 01:51:13 -08:00
|
|
|
// Only add frame if we have any rows at all
|
|
|
|
if (mGIFStruct.rows_remaining != mGIFStruct.height) {
|
|
|
|
if (mGIFStruct.rows_remaining && mGIFStruct.images_decoded) {
|
|
|
|
// Clear the remaining rows (only needed for the animation frames)
|
2008-01-18 23:00:24 -08:00
|
|
|
PRUint8 *rowp = mImageData + ((mGIFStruct.height - mGIFStruct.rows_remaining) * mGIFStruct.width);
|
2008-01-08 01:51:13 -08:00
|
|
|
memset(rowp, 0, mGIFStruct.rows_remaining * mGIFStruct.width);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-08 01:51:13 -08:00
|
|
|
// We actually have the timeout information before we get the lzw encoded
|
|
|
|
// image data, at least according to the spec, but we delay in setting the
|
|
|
|
// timeout for the image until here to help ensure that we have the whole
|
|
|
|
// image frame decoded before we go off and try to display another frame.
|
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
|
|
|
mImageContainer->SetFrameTimeout(mGIFStruct.images_decoded, mGIFStruct.delay_time);
|
2009-04-05 07:52:18 -07:00
|
|
|
mImageContainer->EndFrameDecode(mGIFStruct.images_decoded);
|
2008-01-08 01:51:13 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-02 08:50:14 -07:00
|
|
|
// Unconditionally increment images_decoded, because we unconditionally
|
|
|
|
// append frames in BeginImageFrame(). This ensures that images_decoded
|
|
|
|
// always refers to the frame in mImageContainer we're currently decoding,
|
|
|
|
// even if some of them weren't decoded properly and thus are blank.
|
|
|
|
mGIFStruct.images_decoded++;
|
|
|
|
|
2009-05-14 18:56:54 -07:00
|
|
|
if (mObserver)
|
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
|
|
|
mObserver->OnStopFrame(nsnull, curframe);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-08-25 22:08:53 -07:00
|
|
|
// Reset the transparent pixel
|
|
|
|
if (mOldColor) {
|
|
|
|
mColormap[mGIFStruct.tpixel] = mOldColor;
|
|
|
|
mOldColor = 0;
|
2007-06-25 09:41:21 -07:00
|
|
|
}
|
2009-07-21 21:20:01 -07:00
|
|
|
|
|
|
|
mCurrentFrame = -1;
|
2007-06-25 09:41:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
// Send the data to the display front-end.
|
|
|
|
PRUint32 nsGIFDecoder2::OutputRow()
|
|
|
|
{
|
2007-08-25 22:08:53 -07:00
|
|
|
int drow_start, drow_end;
|
2007-06-25 09:41:21 -07:00
|
|
|
drow_start = drow_end = mGIFStruct.irow;
|
|
|
|
|
|
|
|
/* Protect against too much image data */
|
|
|
|
if ((PRUintn)drow_start >= mGIFStruct.height) {
|
|
|
|
NS_WARNING("GIF2.cpp::OutputRow - too much image data");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-11-07 13:33:57 -08:00
|
|
|
if (!mGIFStruct.images_decoded) {
|
|
|
|
/*
|
|
|
|
* Haeberli-inspired hack for interlaced GIFs: Replicate lines while
|
|
|
|
* displaying to diminish the "venetian-blind" effect as the image is
|
|
|
|
* loaded. Adjust pixel vertical positions to avoid the appearance of the
|
|
|
|
* image crawling up the screen as successive passes are drawn.
|
|
|
|
*/
|
|
|
|
if (mGIFStruct.progressive_display && mGIFStruct.interlaced && (mGIFStruct.ipass < 4)) {
|
|
|
|
/* ipass = 1,2,3 results in resp. row_dup = 7,3,1 and row_shift = 3,1,0 */
|
|
|
|
const PRUint32 row_dup = 15 >> mGIFStruct.ipass;
|
|
|
|
const PRUint32 row_shift = row_dup >> 1;
|
|
|
|
|
|
|
|
drow_start -= row_shift;
|
|
|
|
drow_end = drow_start + row_dup;
|
|
|
|
|
|
|
|
/* Extend if bottom edge isn't covered because of the shift upward. */
|
|
|
|
if (((mGIFStruct.height - 1) - drow_end) <= row_shift)
|
|
|
|
drow_end = mGIFStruct.height - 1;
|
|
|
|
|
|
|
|
/* Clamp first and last rows to upper and lower edge of image. */
|
|
|
|
if (drow_start < 0)
|
|
|
|
drow_start = 0;
|
|
|
|
if ((PRUintn)drow_end >= mGIFStruct.height)
|
|
|
|
drow_end = mGIFStruct.height - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Row to process
|
|
|
|
const PRUint32 bpr = sizeof(PRUint32) * mGIFStruct.width;
|
|
|
|
PRUint8 *rowp = mImageData + (mGIFStruct.irow * bpr);
|
|
|
|
|
|
|
|
// Convert color indices to Cairo pixels
|
|
|
|
PRUint8 *from = rowp + mGIFStruct.width;
|
|
|
|
PRUint32 *to = ((PRUint32*)rowp) + mGIFStruct.width;
|
|
|
|
PRUint32 *cmap = mColormap;
|
2009-09-11 22:27:13 -07:00
|
|
|
if (mColorMask == 0xFF) {
|
|
|
|
for (PRUint32 c = mGIFStruct.width; c > 0; c--) {
|
|
|
|
*--to = cmap[*--from];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure that pixels within range of colormap.
|
|
|
|
PRUint8 mask = mColorMask;
|
|
|
|
for (PRUint32 c = mGIFStruct.width; c > 0; c--) {
|
|
|
|
*--to = cmap[(*--from) & mask];
|
|
|
|
}
|
2007-11-07 13:33:57 -08:00
|
|
|
}
|
2007-08-25 22:08:53 -07:00
|
|
|
|
2007-11-27 01:35:18 -08:00
|
|
|
// check for alpha (only for first frame)
|
|
|
|
if (mGIFStruct.is_transparent && !mSawTransparency) {
|
|
|
|
const PRUint32 *rgb = (PRUint32*)rowp;
|
|
|
|
for (PRUint32 i = mGIFStruct.width; i > 0; i--) {
|
|
|
|
if (*rgb++ == 0) {
|
|
|
|
mSawTransparency = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-07 13:33:57 -08:00
|
|
|
// Duplicate rows
|
|
|
|
if (drow_end > drow_start) {
|
|
|
|
// irow is the current row filled
|
|
|
|
for (int r = drow_start; r <= drow_end; r++) {
|
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 (r != int(mGIFStruct.irow)) {
|
2007-11-07 13:33:57 -08:00
|
|
|
memcpy(mImageData + (r * bpr), rowp, bpr);
|
|
|
|
}
|
2007-08-25 22:08:53 -07:00
|
|
|
}
|
|
|
|
}
|
2007-06-25 09:41:21 -07:00
|
|
|
}
|
|
|
|
|
2007-08-25 22:08:53 -07:00
|
|
|
mCurrentRow = drow_end;
|
|
|
|
mCurrentPass = mGIFStruct.ipass;
|
|
|
|
if (mGIFStruct.ipass == 1)
|
|
|
|
mLastFlushedPass = mGIFStruct.ipass; // interlaced starts at 1
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
if (!mGIFStruct.interlaced) {
|
|
|
|
mGIFStruct.irow++;
|
|
|
|
} else {
|
|
|
|
static const PRUint8 kjump[5] = { 1, 8, 8, 4, 2 };
|
|
|
|
do {
|
|
|
|
// Row increments resp. per 8,8,4,2 rows
|
|
|
|
mGIFStruct.irow += kjump[mGIFStruct.ipass];
|
|
|
|
if (mGIFStruct.irow >= mGIFStruct.height) {
|
|
|
|
// Next pass starts resp. at row 4,2,1,0
|
|
|
|
mGIFStruct.irow = 8 >> mGIFStruct.ipass;
|
|
|
|
mGIFStruct.ipass++;
|
|
|
|
}
|
|
|
|
} while (mGIFStruct.irow >= mGIFStruct.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
return --mGIFStruct.rows_remaining;
|
|
|
|
}
|
|
|
|
|
|
|
|
//******************************************************************************
|
|
|
|
/* Perform Lempel-Ziv-Welch decoding */
|
|
|
|
PRBool
|
|
|
|
nsGIFDecoder2::DoLzw(const PRUint8 *q)
|
|
|
|
{
|
2007-09-10 12:09:59 -07:00
|
|
|
if (!mGIFStruct.rows_remaining)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
/* Copy all the decoder state variables into locals so the compiler
|
|
|
|
* won't worry about them being aliased. The locals will be homed
|
|
|
|
* back into the GIF decoder structure when we exit.
|
|
|
|
*/
|
|
|
|
int avail = mGIFStruct.avail;
|
|
|
|
int bits = mGIFStruct.bits;
|
|
|
|
int codesize = mGIFStruct.codesize;
|
|
|
|
int codemask = mGIFStruct.codemask;
|
|
|
|
int count = mGIFStruct.count;
|
|
|
|
int oldcode = mGIFStruct.oldcode;
|
|
|
|
const int clear_code = ClearCode();
|
|
|
|
PRUint8 firstchar = mGIFStruct.firstchar;
|
|
|
|
PRInt32 datum = mGIFStruct.datum;
|
|
|
|
PRUint16 *prefix = mGIFStruct.prefix;
|
|
|
|
PRUint8 *stackp = mGIFStruct.stackp;
|
|
|
|
PRUint8 *suffix = mGIFStruct.suffix;
|
|
|
|
PRUint8 *stack = mGIFStruct.stack;
|
2007-11-07 13:33:57 -08:00
|
|
|
PRUint8 *rowp = mGIFStruct.rowp;
|
|
|
|
|
|
|
|
PRUint32 bpr = mGIFStruct.width;
|
|
|
|
if (!mGIFStruct.images_decoded)
|
|
|
|
bpr *= sizeof(PRUint32);
|
|
|
|
PRUint8 *rowend = mImageData + (bpr * mGIFStruct.irow) + mGIFStruct.width;
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
#define OUTPUT_ROW() \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (!OutputRow()) \
|
|
|
|
goto END; \
|
2007-11-07 13:33:57 -08:00
|
|
|
rowp = mImageData + mGIFStruct.irow * bpr; \
|
2007-08-25 22:08:53 -07:00
|
|
|
rowend = rowp + mGIFStruct.width; \
|
2007-06-25 09:41:21 -07:00
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
for (const PRUint8* ch = q; count-- > 0; ch++)
|
|
|
|
{
|
|
|
|
/* Feed the next byte into the decoder's 32-bit input buffer. */
|
|
|
|
datum += ((int32) *ch) << bits;
|
|
|
|
bits += 8;
|
|
|
|
|
|
|
|
/* Check for underflow of decoder's 32-bit input buffer. */
|
|
|
|
while (bits >= codesize)
|
|
|
|
{
|
|
|
|
/* Get the leading variable-length symbol from the data stream */
|
|
|
|
int code = datum & codemask;
|
|
|
|
datum >>= codesize;
|
|
|
|
bits -= codesize;
|
|
|
|
|
|
|
|
/* Reset the dictionary to its original state, if requested */
|
|
|
|
if (code == clear_code) {
|
|
|
|
codesize = mGIFStruct.datasize + 1;
|
|
|
|
codemask = (1 << codesize) - 1;
|
|
|
|
avail = clear_code + 2;
|
|
|
|
oldcode = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for explicit end-of-stream code */
|
|
|
|
if (code == (clear_code + 1)) {
|
|
|
|
/* end-of-stream should only appear after all image data */
|
|
|
|
return (mGIFStruct.rows_remaining == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldcode == -1) {
|
2008-01-24 01:29:38 -08:00
|
|
|
if (code >= MAX_BITS)
|
|
|
|
return PR_FALSE;
|
2007-11-07 13:33:57 -08:00
|
|
|
*rowp++ = suffix[code];
|
2007-06-25 09:41:21 -07:00
|
|
|
if (rowp == rowend)
|
|
|
|
OUTPUT_ROW();
|
|
|
|
|
|
|
|
firstchar = oldcode = code;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int incode = code;
|
|
|
|
if (code >= avail) {
|
|
|
|
*stackp++ = firstchar;
|
|
|
|
code = oldcode;
|
|
|
|
|
2008-01-26 20:45:56 -08:00
|
|
|
if (stackp >= stack + MAX_BITS)
|
2007-06-25 09:41:21 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (code >= clear_code)
|
|
|
|
{
|
2008-01-26 20:45:56 -08:00
|
|
|
if ((code >= MAX_BITS) || (code == prefix[code]))
|
2007-06-25 09:41:21 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
*stackp++ = suffix[code];
|
|
|
|
code = prefix[code];
|
|
|
|
|
|
|
|
if (stackp == stack + MAX_BITS)
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*stackp++ = firstchar = suffix[code];
|
|
|
|
|
|
|
|
/* Define a new codeword in the dictionary. */
|
|
|
|
if (avail < 4096) {
|
|
|
|
prefix[avail] = oldcode;
|
|
|
|
suffix[avail] = firstchar;
|
|
|
|
avail++;
|
|
|
|
|
|
|
|
/* If we've used up all the codewords of a given length
|
|
|
|
* increase the length of codewords by one bit, but don't
|
|
|
|
* exceed the specified maximum codeword size of 12 bits.
|
|
|
|
*/
|
|
|
|
if (((avail & codemask) == 0) && (avail < 4096)) {
|
|
|
|
codesize++;
|
|
|
|
codemask += avail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
oldcode = incode;
|
|
|
|
|
|
|
|
/* Copy the decoded data out to the scanline buffer. */
|
|
|
|
do {
|
2007-11-07 13:33:57 -08:00
|
|
|
*rowp++ = *--stackp;
|
2007-06-25 09:41:21 -07:00
|
|
|
if (rowp == rowend)
|
|
|
|
OUTPUT_ROW();
|
|
|
|
} while (stackp > stack);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
END:
|
|
|
|
|
|
|
|
/* Home the local copies of the GIF decoder state variables */
|
|
|
|
mGIFStruct.avail = avail;
|
|
|
|
mGIFStruct.bits = bits;
|
|
|
|
mGIFStruct.codesize = codesize;
|
|
|
|
mGIFStruct.codemask = codemask;
|
|
|
|
mGIFStruct.count = count;
|
|
|
|
mGIFStruct.oldcode = oldcode;
|
|
|
|
mGIFStruct.firstchar = firstchar;
|
|
|
|
mGIFStruct.datum = datum;
|
|
|
|
mGIFStruct.stackp = stackp;
|
|
|
|
mGIFStruct.rowp = rowp;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2007-08-25 22:08:53 -07:00
|
|
|
/**
|
|
|
|
* Expand the colormap from RGB to Packed ARGB as needed by Cairo.
|
|
|
|
* And apply any LCMS transformation.
|
|
|
|
*/
|
|
|
|
static void ConvertColormap(PRUint32 *aColormap, PRUint32 aColors)
|
|
|
|
{
|
|
|
|
// Apply CMS transformation if enabled and available
|
2008-08-11 21:55:36 -07:00
|
|
|
if (gfxPlatform::GetCMSMode() == eCMSMode_All) {
|
2009-04-07 09:02:11 -07:00
|
|
|
qcms_transform *transform = gfxPlatform::GetCMSRGBTransform();
|
2007-08-25 22:08:53 -07:00
|
|
|
if (transform)
|
2009-04-07 09:02:11 -07:00
|
|
|
qcms_transform_data(transform, aColormap, aColormap, aColors);
|
2007-08-25 22:08:53 -07:00
|
|
|
}
|
|
|
|
// Convert from the GIF's RGB format to the Cairo format.
|
|
|
|
// Work from end to begin, because of the in-place expansion
|
|
|
|
PRUint8 *from = ((PRUint8 *)aColormap) + 3 * aColors;
|
|
|
|
PRUint32 *to = aColormap + aColors;
|
|
|
|
|
|
|
|
// Convert color entries to Cairo format
|
2007-12-21 02:26:31 -08:00
|
|
|
|
|
|
|
// set up for loops below
|
|
|
|
if (!aColors) return;
|
|
|
|
PRUint32 c = aColors;
|
|
|
|
|
2008-01-29 22:22:23 -08:00
|
|
|
// copy as bytes until source pointer is 32-bit-aligned
|
|
|
|
// NB: can't use 32-bit reads, they might read off the end of the buffer
|
2008-01-30 17:04:36 -08:00
|
|
|
for (; (NS_PTR_TO_UINT32(from) & 0x3) && c; --c) {
|
2008-01-29 22:22:23 -08:00
|
|
|
from -= 3;
|
|
|
|
*--to = GFX_PACKED_PIXEL(0xFF, from[0], from[1], from[2]);
|
|
|
|
}
|
2007-12-21 02:26:31 -08:00
|
|
|
|
|
|
|
// bulk copy of pixels.
|
|
|
|
while (c >= 4) {
|
|
|
|
from -= 12;
|
|
|
|
to -= 4;
|
|
|
|
c -= 4;
|
2008-01-29 22:22:23 -08:00
|
|
|
GFX_BLOCK_RGB_TO_FRGB(from,to);
|
2007-12-21 02:26:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy remaining pixel(s)
|
2008-01-29 22:22:23 -08:00
|
|
|
// NB: can't use 32-bit reads, they might read off the end of the buffer
|
2007-12-21 02:26:31 -08:00
|
|
|
while (c--) {
|
2007-08-25 22:08:53 -07:00
|
|
|
from -= 3;
|
2008-01-29 22:22:23 -08:00
|
|
|
*--to = GFX_PACKED_PIXEL(0xFF, from[0], from[1], from[2]);
|
2007-08-25 22:08:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
/*
|
|
|
|
* process data arriving from the stream for the gif decoder
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsresult nsGIFDecoder2::GifWrite(const PRUint8 *buf, PRUint32 len)
|
|
|
|
{
|
|
|
|
if (!buf || !len)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
const PRUint8 *q = buf;
|
|
|
|
|
|
|
|
// Add what we have sofar to the block
|
|
|
|
// If previous call to me left something in the hold first complete current block
|
|
|
|
// Or if we are filling the colormaps, first complete the colormap
|
2007-08-25 22:08:53 -07:00
|
|
|
PRUint8* p = (mGIFStruct.state == gif_global_colormap) ? (PRUint8*)mGIFStruct.global_colormap :
|
2007-11-07 13:33:57 -08:00
|
|
|
(mGIFStruct.state == gif_image_colormap) ? (PRUint8*)mColormap :
|
2007-06-25 09:41:21 -07:00
|
|
|
(mGIFStruct.bytes_in_hold) ? mGIFStruct.hold : nsnull;
|
|
|
|
if (p) {
|
|
|
|
// Add what we have sofar to the block
|
|
|
|
PRUint32 l = PR_MIN(len, mGIFStruct.bytes_to_consume);
|
|
|
|
memcpy(p+mGIFStruct.bytes_in_hold, buf, l);
|
|
|
|
|
|
|
|
if (l < mGIFStruct.bytes_to_consume) {
|
|
|
|
// Not enough in 'buf' to complete current block, get more
|
|
|
|
mGIFStruct.bytes_in_hold += l;
|
|
|
|
mGIFStruct.bytes_to_consume -= l;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// Reset hold buffer count
|
|
|
|
mGIFStruct.bytes_in_hold = 0;
|
|
|
|
// Point 'q' to complete block in hold (or in colormap)
|
|
|
|
q = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invariant:
|
|
|
|
// 'q' is start of current to be processed block (hold, colormap or buf)
|
|
|
|
// 'bytes_to_consume' is number of bytes to consume from 'buf'
|
|
|
|
// 'buf' points to the bytes to be consumed from the input buffer
|
|
|
|
// 'len' is number of bytes left in input buffer from position 'buf'.
|
|
|
|
// At entrance of the for loop will 'buf' will be moved 'bytes_to_consume'
|
|
|
|
// to point to next buffer, 'len' is adjusted accordingly.
|
|
|
|
// So that next round in for loop, q gets pointed to the next buffer.
|
|
|
|
|
|
|
|
for (;len >= mGIFStruct.bytes_to_consume; q=buf) {
|
|
|
|
// Eat the current block from the buffer, q keeps pointed at current block
|
|
|
|
buf += mGIFStruct.bytes_to_consume;
|
|
|
|
len -= mGIFStruct.bytes_to_consume;
|
|
|
|
|
|
|
|
switch (mGIFStruct.state)
|
|
|
|
{
|
|
|
|
case gif_lzw:
|
|
|
|
if (!DoLzw(q)) {
|
|
|
|
mGIFStruct.state = gif_error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GETN(1, gif_sub_block);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_lzw_start:
|
|
|
|
{
|
2007-08-25 22:08:53 -07:00
|
|
|
// Make sure the transparent pixel is transparent in the colormap
|
|
|
|
if (mGIFStruct.is_transparent) {
|
2007-11-07 13:33:57 -08:00
|
|
|
// Save old value so we can restore it later
|
|
|
|
if (mColormap == mGIFStruct.global_colormap)
|
|
|
|
mOldColor = mColormap[mGIFStruct.tpixel];
|
2007-08-25 22:08:53 -07:00
|
|
|
mColormap[mGIFStruct.tpixel] = 0;
|
|
|
|
}
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
/* Initialize LZW parser/decoder */
|
|
|
|
mGIFStruct.datasize = *q;
|
|
|
|
const int clear_code = ClearCode();
|
|
|
|
if (mGIFStruct.datasize > MAX_LZW_BITS ||
|
|
|
|
clear_code >= MAX_BITS) {
|
|
|
|
mGIFStruct.state = gif_error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mGIFStruct.avail = clear_code + 2;
|
|
|
|
mGIFStruct.oldcode = -1;
|
|
|
|
mGIFStruct.codesize = mGIFStruct.datasize + 1;
|
|
|
|
mGIFStruct.codemask = (1 << mGIFStruct.codesize) - 1;
|
|
|
|
mGIFStruct.datum = mGIFStruct.bits = 0;
|
|
|
|
|
|
|
|
/* init the tables */
|
|
|
|
for (int i = 0; i < clear_code; i++)
|
|
|
|
mGIFStruct.suffix[i] = i;
|
|
|
|
|
|
|
|
mGIFStruct.stackp = mGIFStruct.stack;
|
|
|
|
|
|
|
|
GETN(1, gif_sub_block);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* All GIF files begin with "GIF87a" or "GIF89a" */
|
|
|
|
case gif_type:
|
|
|
|
if (!strncmp((char*)q, "GIF89a", 6)) {
|
|
|
|
mGIFStruct.version = 89;
|
|
|
|
} else if (!strncmp((char*)q, "GIF87a", 6)) {
|
|
|
|
mGIFStruct.version = 87;
|
|
|
|
} else {
|
|
|
|
mGIFStruct.state = gif_error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
GETN(7, gif_global_header);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_global_header:
|
|
|
|
/* This is the height and width of the "screen" or
|
|
|
|
* frame into which images are rendered. The
|
|
|
|
* individual images can be smaller than the
|
|
|
|
* screen size and located with an origin anywhere
|
|
|
|
* within the screen.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mGIFStruct.screen_width = GETINT16(q);
|
|
|
|
mGIFStruct.screen_height = GETINT16(q + 2);
|
2007-11-07 13:33:57 -08:00
|
|
|
mGIFStruct.global_colormap_depth = (q[4]&0x07) + 1;
|
2007-06-25 09:41:21 -07:00
|
|
|
|
|
|
|
// screen_bgcolor is not used
|
|
|
|
//mGIFStruct.screen_bgcolor = q[5];
|
|
|
|
// q[6] = Pixel Aspect Ratio
|
|
|
|
// Not used
|
|
|
|
// float aspect = (float)((q[6] + 15) / 64.0);
|
|
|
|
|
|
|
|
if (q[4] & 0x80) { /* global map */
|
|
|
|
// Get the global colormap
|
2007-11-07 13:33:57 -08:00
|
|
|
const PRUint32 size = (3 << mGIFStruct.global_colormap_depth);
|
2007-06-25 09:41:21 -07:00
|
|
|
if (len < size) {
|
|
|
|
// Use 'hold' pattern to get the global colormap
|
|
|
|
GETN(size, gif_global_colormap);
|
|
|
|
break;
|
|
|
|
}
|
2007-07-23 15:02:17 -07:00
|
|
|
// Copy everything, go to colormap state to do CMS correction
|
2007-06-25 09:41:21 -07:00
|
|
|
memcpy(mGIFStruct.global_colormap, buf, size);
|
|
|
|
buf += size;
|
|
|
|
len -= size;
|
2007-07-23 15:02:17 -07:00
|
|
|
GETN(0, gif_global_colormap);
|
|
|
|
break;
|
2007-06-25 09:41:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
GETN(1, gif_image_start);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_global_colormap:
|
2007-08-25 22:08:53 -07:00
|
|
|
// Everything is already copied into global_colormap
|
|
|
|
// Convert into Cairo colors including CMS transformation
|
2007-11-07 13:33:57 -08:00
|
|
|
ConvertColormap(mGIFStruct.global_colormap, 1<<mGIFStruct.global_colormap_depth);
|
2007-06-25 09:41:21 -07:00
|
|
|
GETN(1, gif_image_start);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_image_start:
|
|
|
|
switch (*q) {
|
2007-09-10 12:09:59 -07:00
|
|
|
case GIF_TRAILER:
|
2007-06-25 09:41:21 -07:00
|
|
|
mGIFStruct.state = gif_done;
|
|
|
|
break;
|
|
|
|
|
2007-09-10 12:09:59 -07:00
|
|
|
case GIF_EXTENSION_INTRODUCER:
|
2007-06-25 09:41:21 -07:00
|
|
|
GETN(2, gif_extension);
|
|
|
|
break;
|
|
|
|
|
2007-09-10 12:09:59 -07:00
|
|
|
case GIF_IMAGE_SEPARATOR:
|
2007-06-25 09:41:21 -07:00
|
|
|
GETN(9, gif_image_header);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2007-09-10 12:09:59 -07:00
|
|
|
/* If we get anything other than GIF_IMAGE_SEPARATOR,
|
|
|
|
* GIF_EXTENSION_INTRODUCER, or GIF_TRAILER, there is extraneous data
|
2007-06-25 09:41:21 -07:00
|
|
|
* between blocks. The GIF87a spec tells us to keep reading
|
|
|
|
* until we find an image separator, but GIF89a says such
|
|
|
|
* a file is corrupt. We follow GIF89a and bail out. */
|
|
|
|
if (mGIFStruct.images_decoded > 0) {
|
|
|
|
/* The file is corrupt, but one or more images have
|
|
|
|
* been decoded correctly. In this case, we proceed
|
|
|
|
* as if the file were correctly terminated and set
|
|
|
|
* the state to gif_done, so the GIF will display.
|
|
|
|
*/
|
|
|
|
mGIFStruct.state = gif_done;
|
|
|
|
} else {
|
|
|
|
/* No images decoded, there is nothing to display. */
|
|
|
|
mGIFStruct.state = gif_error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_extension:
|
|
|
|
mGIFStruct.bytes_to_consume = q[1];
|
|
|
|
if (mGIFStruct.bytes_to_consume) {
|
|
|
|
switch (*q) {
|
2007-09-10 12:09:59 -07:00
|
|
|
case GIF_GRAPHIC_CONTROL_LABEL:
|
2007-06-25 09:41:21 -07:00
|
|
|
mGIFStruct.state = gif_control_extension;
|
|
|
|
break;
|
|
|
|
|
2007-09-10 12:09:59 -07:00
|
|
|
case GIF_APPLICATION_EXTENSION_LABEL:
|
2007-06-25 09:41:21 -07:00
|
|
|
mGIFStruct.state = gif_application_extension;
|
|
|
|
break;
|
|
|
|
|
2007-09-10 12:09:59 -07:00
|
|
|
case GIF_COMMENT_LABEL:
|
2007-06-25 09:41:21 -07:00
|
|
|
mGIFStruct.state = gif_consume_comment;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mGIFStruct.state = gif_skip_block;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GETN(1, gif_image_start);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_consume_block:
|
|
|
|
if (!*q)
|
|
|
|
GETN(1, gif_image_start);
|
|
|
|
else
|
|
|
|
GETN(*q, gif_skip_block);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_skip_block:
|
|
|
|
GETN(1, gif_consume_block);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_control_extension:
|
|
|
|
mGIFStruct.is_transparent = *q & 0x1;
|
|
|
|
mGIFStruct.tpixel = q[3];
|
|
|
|
mGIFStruct.disposal_method = ((*q) >> 2) & 0x7;
|
|
|
|
// Some specs say 3rd bit (value 4), other specs say value 3
|
|
|
|
// Let's choose 3 (the more popular)
|
|
|
|
if (mGIFStruct.disposal_method == 4)
|
|
|
|
mGIFStruct.disposal_method = 3;
|
|
|
|
mGIFStruct.delay_time = GETINT16(q + 1) * 10;
|
|
|
|
GETN(1, gif_consume_block);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_comment_extension:
|
|
|
|
if (*q)
|
|
|
|
GETN(*q, gif_consume_comment);
|
|
|
|
else
|
|
|
|
GETN(1, gif_image_start);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_consume_comment:
|
|
|
|
GETN(1, gif_comment_extension);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_application_extension:
|
|
|
|
/* Check for netscape application extension */
|
|
|
|
if (!strncmp((char*)q, "NETSCAPE2.0", 11) ||
|
|
|
|
!strncmp((char*)q, "ANIMEXTS1.0", 11))
|
|
|
|
GETN(1, gif_netscape_extension_block);
|
|
|
|
else
|
|
|
|
GETN(1, gif_consume_block);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Netscape-specific GIF extension: animation looping */
|
|
|
|
case gif_netscape_extension_block:
|
|
|
|
if (*q)
|
|
|
|
GETN(*q, gif_consume_netscape_extension);
|
|
|
|
else
|
|
|
|
GETN(1, gif_image_start);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Parse netscape-specific application extensions */
|
|
|
|
case gif_consume_netscape_extension:
|
|
|
|
switch (q[0] & 7) {
|
|
|
|
case 1:
|
|
|
|
/* Loop entire animation specified # of times. Only read the
|
|
|
|
loop count during the first iteration. */
|
|
|
|
mGIFStruct.loop_count = GETINT16(q + 1);
|
|
|
|
|
|
|
|
/* Zero loop count is infinite animation loop request */
|
|
|
|
if (mGIFStruct.loop_count == 0)
|
|
|
|
mGIFStruct.loop_count = -1;
|
|
|
|
|
|
|
|
GETN(1, gif_netscape_extension_block);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
/* Wait for specified # of bytes to enter buffer */
|
|
|
|
// Don't do this, this extension doesn't exist (isn't used at all)
|
|
|
|
// and doesn't do anything, as our streaming/buffering takes care of it all...
|
|
|
|
// See: http://semmix.pl/color/exgraf/eeg24.htm
|
|
|
|
GETN(1, gif_netscape_extension_block);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// 0,3-7 are yet to be defined netscape extension codes
|
|
|
|
mGIFStruct.state = gif_error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_image_header:
|
2007-11-07 13:33:57 -08:00
|
|
|
{
|
2007-06-25 09:41:21 -07:00
|
|
|
/* Get image offsets, with respect to the screen origin */
|
|
|
|
mGIFStruct.x_offset = GETINT16(q);
|
|
|
|
mGIFStruct.y_offset = GETINT16(q + 2);
|
|
|
|
|
|
|
|
/* Get image width and height. */
|
|
|
|
mGIFStruct.width = GETINT16(q + 4);
|
|
|
|
mGIFStruct.height = GETINT16(q + 6);
|
|
|
|
|
2007-08-25 22:08:53 -07:00
|
|
|
if (!mGIFStruct.images_decoded) {
|
|
|
|
/* Work around broken GIF files where the logical screen
|
|
|
|
* size has weird width or height. We assume that GIF87a
|
|
|
|
* files don't contain animations.
|
|
|
|
*/
|
|
|
|
if ((mGIFStruct.screen_height < mGIFStruct.height) ||
|
|
|
|
(mGIFStruct.screen_width < mGIFStruct.width) ||
|
|
|
|
(mGIFStruct.version == 87)) {
|
|
|
|
mGIFStruct.screen_height = mGIFStruct.height;
|
|
|
|
mGIFStruct.screen_width = mGIFStruct.width;
|
|
|
|
mGIFStruct.x_offset = 0;
|
|
|
|
mGIFStruct.y_offset = 0;
|
|
|
|
}
|
|
|
|
// Create the image container with the right size.
|
2007-06-25 09:41:21 -07:00
|
|
|
BeginGIF();
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
// If we were doing header-only, we're done
|
|
|
|
if (mFlags & imgIDecoder::DECODER_FLAG_HEADERONLY)
|
|
|
|
return NS_OK;
|
2007-06-25 09:41:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Work around more broken GIF files that have zero image
|
|
|
|
width or height */
|
|
|
|
if (!mGIFStruct.height || !mGIFStruct.width) {
|
|
|
|
mGIFStruct.height = mGIFStruct.screen_height;
|
|
|
|
mGIFStruct.width = mGIFStruct.screen_width;
|
|
|
|
if (!mGIFStruct.height || !mGIFStruct.width) {
|
|
|
|
mGIFStruct.state = gif_error;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-07 13:33:57 -08:00
|
|
|
/* Depth of colors is determined by colormap */
|
|
|
|
/* (q[8] & 0x80) indicates local colormap */
|
|
|
|
/* bits per pixel is (q[8]&0x07 + 1) when local colormap is set */
|
|
|
|
PRUint32 depth = mGIFStruct.global_colormap_depth;
|
|
|
|
if (q[8] & 0x80)
|
|
|
|
depth = (q[8]&0x07) + 1;
|
2009-10-15 18:37:39 -07:00
|
|
|
PRUint32 realDepth = depth;
|
|
|
|
while (mGIFStruct.tpixel >= (1 << realDepth) && (realDepth < 8)) {
|
|
|
|
realDepth++;
|
|
|
|
}
|
2009-09-11 22:27:13 -07:00
|
|
|
// Mask to limit the color values within the colormap
|
2009-10-15 18:37:39 -07:00
|
|
|
mColorMask = 0xFF >> (8 - realDepth);
|
|
|
|
nsresult rv = BeginImageFrame(realDepth);
|
2009-09-04 18:47:11 -07:00
|
|
|
if (NS_FAILED(rv) || !mImageData) {
|
2007-10-23 23:01:16 -07:00
|
|
|
mGIFStruct.state = gif_error;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-25 09:41:21 -07:00
|
|
|
|
|
|
|
if (q[8] & 0x40) {
|
|
|
|
mGIFStruct.interlaced = PR_TRUE;
|
|
|
|
mGIFStruct.ipass = 1;
|
|
|
|
} else {
|
|
|
|
mGIFStruct.interlaced = PR_FALSE;
|
|
|
|
mGIFStruct.ipass = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only apply the Haeberli display hack on the first frame */
|
|
|
|
mGIFStruct.progressive_display = (mGIFStruct.images_decoded == 0);
|
|
|
|
|
|
|
|
/* Clear state from last image */
|
|
|
|
mGIFStruct.irow = 0;
|
|
|
|
mGIFStruct.rows_remaining = mGIFStruct.height;
|
2007-08-25 22:08:53 -07:00
|
|
|
mGIFStruct.rowp = mImageData;
|
2007-06-25 09:41:21 -07:00
|
|
|
|
|
|
|
/* bits per pixel is q[8]&0x07 */
|
|
|
|
|
|
|
|
if (q[8] & 0x80) /* has a local colormap? */
|
|
|
|
{
|
2007-11-07 13:33:57 -08:00
|
|
|
mGIFStruct.local_colormap_size = 1 << depth;
|
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 (!mGIFStruct.images_decoded) {
|
2007-11-07 13:33:57 -08:00
|
|
|
// First frame has local colormap, allocate space for it
|
|
|
|
// as the image frame doesn't have its own palette
|
2009-10-15 18:37:39 -07:00
|
|
|
mColormapSize = sizeof(PRUint32) << realDepth;
|
2007-08-25 22:08:53 -07:00
|
|
|
if (!mGIFStruct.local_colormap) {
|
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
|
|
|
mGIFStruct.local_colormap = (PRUint32*)PR_MALLOC(mColormapSize);
|
2007-11-07 13:33:57 -08:00
|
|
|
if (!mGIFStruct.local_colormap) {
|
|
|
|
mGIFStruct.state = gif_oom;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-25 09:41:21 -07:00
|
|
|
}
|
2007-11-07 13:33:57 -08:00
|
|
|
mColormap = mGIFStruct.local_colormap;
|
2007-06-25 09:41:21 -07:00
|
|
|
}
|
2007-11-07 13:33:57 -08:00
|
|
|
const PRUint32 size = 3 << depth;
|
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 (mColormapSize > size) {
|
2007-12-20 01:38:53 -08:00
|
|
|
// Clear the notfilled part of the colormap
|
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
|
|
|
memset(((PRUint8*)mColormap) + size, 0, mColormapSize - size);
|
2007-12-12 08:56:39 -08:00
|
|
|
}
|
2007-06-25 09:41:21 -07:00
|
|
|
if (len < size) {
|
|
|
|
// Use 'hold' pattern to get the image colormap
|
|
|
|
GETN(size, gif_image_colormap);
|
|
|
|
break;
|
|
|
|
}
|
2007-07-23 15:02:17 -07:00
|
|
|
// Copy everything, go to colormap state to do CMS correction
|
2007-11-07 13:33:57 -08:00
|
|
|
memcpy(mColormap, buf, size);
|
2007-06-25 09:41:21 -07:00
|
|
|
buf += size;
|
|
|
|
len -= size;
|
2007-07-23 15:02:17 -07:00
|
|
|
GETN(0, gif_image_colormap);
|
|
|
|
break;
|
2007-06-25 09:41:21 -07:00
|
|
|
} else {
|
|
|
|
/* Switch back to the global palette */
|
2007-11-07 13:33:57 -08:00
|
|
|
if (mGIFStruct.images_decoded) {
|
|
|
|
// Copy global colormap into the palette of current frame
|
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
|
|
|
memcpy(mColormap, mGIFStruct.global_colormap, mColormapSize);
|
|
|
|
} else {
|
|
|
|
mColormap = mGIFStruct.global_colormap;
|
2007-11-07 13:33:57 -08:00
|
|
|
}
|
2007-06-25 09:41:21 -07:00
|
|
|
}
|
|
|
|
GETN(1, gif_lzw_start);
|
2007-11-07 13:33:57 -08:00
|
|
|
}
|
|
|
|
break;
|
2007-06-25 09:41:21 -07:00
|
|
|
|
|
|
|
case gif_image_colormap:
|
2007-08-25 22:08:53 -07:00
|
|
|
// Everything is already copied into local_colormap
|
|
|
|
// Convert into Cairo colors including CMS transformation
|
2007-11-07 13:33:57 -08:00
|
|
|
ConvertColormap(mColormap, mGIFStruct.local_colormap_size);
|
2007-06-25 09:41:21 -07:00
|
|
|
GETN(1, gif_lzw_start);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_sub_block:
|
|
|
|
mGIFStruct.count = *q;
|
|
|
|
if (mGIFStruct.count) {
|
|
|
|
/* Still working on the same image: Process next LZW data block */
|
|
|
|
/* Make sure there are still rows left. If the GIF data */
|
|
|
|
/* is corrupt, we may not get an explicit terminator. */
|
|
|
|
if (!mGIFStruct.rows_remaining) {
|
|
|
|
#ifdef DONT_TOLERATE_BROKEN_GIFS
|
|
|
|
mGIFStruct.state = gif_error;
|
2007-08-07 09:41:30 -07:00
|
|
|
break;
|
2007-06-25 09:41:21 -07:00
|
|
|
#else
|
|
|
|
/* This is an illegal GIF, but we remain tolerant. */
|
|
|
|
GETN(1, gif_sub_block);
|
|
|
|
#endif
|
2007-09-10 12:09:59 -07:00
|
|
|
if (mGIFStruct.count == GIF_TRAILER) {
|
|
|
|
/* Found a terminator anyway, so consider the image done */
|
|
|
|
GETN(1, gif_done);
|
|
|
|
break;
|
|
|
|
}
|
2007-06-25 09:41:21 -07:00
|
|
|
}
|
|
|
|
GETN(mGIFStruct.count, gif_lzw);
|
|
|
|
} else {
|
|
|
|
/* See if there are any more images in this sequence. */
|
|
|
|
EndImageFrame();
|
|
|
|
GETN(1, gif_image_start);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case gif_done:
|
2009-09-12 15:44:18 -07:00
|
|
|
EndGIF(/* aSuccess = */ PR_TRUE);
|
2007-06-25 09:41:21 -07:00
|
|
|
return NS_OK;
|
|
|
|
break;
|
|
|
|
|
2007-10-23 23:01:16 -07:00
|
|
|
case gif_error:
|
2009-09-12 15:44:18 -07:00
|
|
|
EndGIF(/* aSuccess = */ PR_FALSE);
|
2007-10-23 23:01:16 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
break;
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
// Handle out of memory errors
|
|
|
|
case gif_oom:
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// We shouldn't ever get here.
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-23 23:01:16 -07:00
|
|
|
// if an error state is set but no data remains, code flow reaches here
|
|
|
|
if (mGIFStruct.state == gif_error) {
|
2009-09-12 15:44:18 -07:00
|
|
|
EndGIF(/* aSuccess = */ PR_FALSE);
|
2007-10-23 23:01:16 -07:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2007-06-25 09:41:21 -07:00
|
|
|
// Copy the leftover into mGIFStruct.hold
|
|
|
|
mGIFStruct.bytes_in_hold = len;
|
|
|
|
if (len) {
|
|
|
|
// Add what we have sofar to the block
|
2007-08-25 22:08:53 -07:00
|
|
|
PRUint8* p = (mGIFStruct.state == gif_global_colormap) ? (PRUint8*)mGIFStruct.global_colormap :
|
2007-11-07 13:33:57 -08:00
|
|
|
(mGIFStruct.state == gif_image_colormap) ? (PRUint8*)mColormap :
|
2007-06-25 09:41:21 -07:00
|
|
|
mGIFStruct.hold;
|
|
|
|
memcpy(p, buf, len);
|
|
|
|
mGIFStruct.bytes_to_consume -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|