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):
|
|
|
|
* Scott MacGregor <mscott@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 ***** */
|
|
|
|
|
|
|
|
#include "nsIconDecoder.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 "nspr.h"
|
|
|
|
#include "nsRect.h"
|
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
#include "ImageErrors.h"
|
2007-07-03 18:09:10 -07:00
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace imagelib {
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
nsIconDecoder::nsIconDecoder() :
|
|
|
|
mWidth(-1),
|
|
|
|
mHeight(-1),
|
|
|
|
mPixBytesRead(0),
|
|
|
|
mPixBytesTotal(0),
|
|
|
|
mImageData(nsnull),
|
2010-09-12 08:22:30 -07:00
|
|
|
mState(iconStateStart)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-09-12 15:44:18 -07:00
|
|
|
// Nothing to do
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIconDecoder::~nsIconDecoder()
|
|
|
|
{ }
|
|
|
|
|
2010-09-12 08:22:30 -07:00
|
|
|
void
|
2010-08-22 19:30:46 -07:00
|
|
|
nsIconDecoder::WriteInternal(const char *aBuffer, PRUint32 aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2010-09-12 08:22:31 -07:00
|
|
|
NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
|
2010-09-12 08:22:27 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// We put this here to avoid errors about crossing initialization with case
|
|
|
|
// jumps on linux.
|
|
|
|
PRUint32 bytesToRead = 0;
|
2010-09-12 08:22:31 -07:00
|
|
|
nsresult rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// Performance isn't critical here, so our update rectangle is
|
|
|
|
// always the full icon
|
2009-10-15 19:54:44 -07:00
|
|
|
nsIntRect r(0, 0, mWidth, mHeight);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// Loop until the input data is gone
|
|
|
|
while (aCount > 0) {
|
2009-10-15 19:54:44 -07:00
|
|
|
switch (mState) {
|
2009-09-12 15:44:18 -07:00
|
|
|
case iconStateStart:
|
|
|
|
|
|
|
|
// Grab the width
|
2009-10-15 19:54:44 -07:00
|
|
|
mWidth = (PRUint8)*aBuffer;
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
// Book Keeping
|
2009-10-15 19:54:44 -07:00
|
|
|
aBuffer++;
|
2009-09-12 15:44:18 -07:00
|
|
|
aCount--;
|
2009-10-15 19:54:44 -07:00
|
|
|
mState = iconStateHaveHeight;
|
2009-09-12 15:44:18 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case iconStateHaveHeight:
|
|
|
|
|
|
|
|
// Grab the Height
|
2009-10-15 19:54:44 -07:00
|
|
|
mHeight = (PRUint8)*aBuffer;
|
2009-09-12 15:44:18 -07:00
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
// Post our size to the superclass
|
|
|
|
PostSize(mWidth, mHeight);
|
2009-09-12 15:44:18 -07:00
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
// If We're doing a size decode, we're done
|
|
|
|
if (IsSizeDecode()) {
|
2009-10-15 19:54:44 -07:00
|
|
|
mState = iconStateFinished;
|
2009-09-12 15:44:18 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the frame and signal
|
2009-10-15 19:54:44 -07:00
|
|
|
rv = mImage->AppendFrame(0, 0, mWidth, mHeight,
|
|
|
|
gfxASurface::ImageFormatARGB32,
|
|
|
|
&mImageData, &mPixBytesTotal);
|
2009-09-12 15:44:18 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-09-12 08:22:27 -07:00
|
|
|
PostDecoderError(rv);
|
2010-09-12 08:22:30 -07:00
|
|
|
return;
|
2009-09-12 15:44:18 -07:00
|
|
|
}
|
2010-08-22 19:30:46 -07:00
|
|
|
|
|
|
|
// Tell the superclass we're starting a frame
|
|
|
|
PostFrameStart();
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
// Book Keeping
|
2009-10-15 19:54:44 -07:00
|
|
|
aBuffer++;
|
2009-09-12 15:44:18 -07:00
|
|
|
aCount--;
|
2009-10-15 19:54:44 -07:00
|
|
|
mState = iconStateReadPixels;
|
2009-09-12 15:44:18 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case iconStateReadPixels:
|
|
|
|
|
|
|
|
// How many bytes are we reading?
|
2009-10-19 23:01:12 -07:00
|
|
|
bytesToRead = PR_MIN(aCount, mPixBytesTotal - mPixBytesRead);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// Copy the bytes
|
2009-10-15 19:54:44 -07:00
|
|
|
memcpy(mImageData + mPixBytesRead, aBuffer, bytesToRead);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-08-24 14:12:04 -07:00
|
|
|
// Invalidate
|
|
|
|
PostInvalidation(r);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// Book Keeping
|
2009-10-15 19:54:44 -07:00
|
|
|
aBuffer += bytesToRead;
|
2009-09-12 15:44:18 -07:00
|
|
|
aCount -= bytesToRead;
|
2009-10-15 19:54:44 -07:00
|
|
|
mPixBytesRead += bytesToRead;
|
2008-09-10 13:23:29 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
// If we've got all the pixel bytes, we're finished
|
2009-10-15 19:54:44 -07:00
|
|
|
if (mPixBytesRead == mPixBytesTotal) {
|
2010-09-12 08:22:30 -07:00
|
|
|
PostFrameStop();
|
|
|
|
PostDecodeDone();
|
2009-10-15 19:54:44 -07:00
|
|
|
mState = iconStateFinished;
|
2009-09-12 15:44:18 -07:00
|
|
|
}
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-09-12 15:44:18 -07:00
|
|
|
case iconStateFinished:
|
|
|
|
|
|
|
|
// Consume all excess data silently
|
|
|
|
aCount = 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
} // namespace imagelib
|
|
|
|
} // namespace mozilla
|