2001-04-02 04:56:18 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-04-02 04:56:18 +00:00
|
|
|
|
|
|
|
#include "nsIconDecoder.h"
|
|
|
|
#include "nsIInputStream.h"
|
2010-08-14 04:09:49 +00:00
|
|
|
#include "RasterImage.h"
|
2001-04-02 04:56:18 +00:00
|
|
|
#include "nspr.h"
|
|
|
|
#include "nsRect.h"
|
|
|
|
|
2012-07-27 14:03:27 +00:00
|
|
|
#include "nsError.h"
|
2007-07-04 01:09:10 +00:00
|
|
|
|
2010-08-23 02:30:46 +00:00
|
|
|
namespace mozilla {
|
2012-01-06 16:02:27 +00:00
|
|
|
namespace image {
|
2009-09-12 22:44:18 +00:00
|
|
|
|
2011-09-29 13:17:13 +00:00
|
|
|
nsIconDecoder::nsIconDecoder(RasterImage &aImage, imgIDecoderObserver* aObserver)
|
2011-09-27 16:24:03 +00:00
|
|
|
: Decoder(aImage, aObserver),
|
|
|
|
mWidth(-1),
|
|
|
|
mHeight(-1),
|
|
|
|
mPixBytesRead(0),
|
|
|
|
mPixBytesTotal(0),
|
2012-07-30 14:20:58 +00:00
|
|
|
mImageData(nullptr),
|
2011-09-27 16:24:03 +00:00
|
|
|
mState(iconStateStart)
|
2001-04-02 04:56:18 +00:00
|
|
|
{
|
2009-09-12 22:44:18 +00:00
|
|
|
// Nothing to do
|
2001-04-02 04:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIconDecoder::~nsIconDecoder()
|
|
|
|
{ }
|
|
|
|
|
2010-09-12 15:22:30 +00:00
|
|
|
void
|
2012-08-22 15:56:38 +00:00
|
|
|
nsIconDecoder::WriteInternal(const char *aBuffer, uint32_t aCount)
|
2001-04-02 04:56:18 +00:00
|
|
|
{
|
2010-09-12 15:22:31 +00:00
|
|
|
NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
|
2010-09-12 15:22:27 +00:00
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
// We put this here to avoid errors about crossing initialization with case
|
|
|
|
// jumps on linux.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t bytesToRead = 0;
|
2010-09-12 15:22:31 +00:00
|
|
|
nsresult rv;
|
2001-04-02 04:56:18 +00:00
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
// Performance isn't critical here, so our update rectangle is
|
|
|
|
// always the full icon
|
2009-10-16 02:54:44 +00:00
|
|
|
nsIntRect r(0, 0, mWidth, mHeight);
|
2002-07-19 23:21:48 +00:00
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
// Loop until the input data is gone
|
|
|
|
while (aCount > 0) {
|
2009-10-16 02:54:44 +00:00
|
|
|
switch (mState) {
|
2009-09-12 22:44:18 +00:00
|
|
|
case iconStateStart:
|
|
|
|
|
|
|
|
// Grab the width
|
2012-08-22 15:56:38 +00:00
|
|
|
mWidth = (uint8_t)*aBuffer;
|
2009-09-12 22:44:18 +00:00
|
|
|
|
|
|
|
// Book Keeping
|
2009-10-16 02:54:44 +00:00
|
|
|
aBuffer++;
|
2009-09-12 22:44:18 +00:00
|
|
|
aCount--;
|
2009-10-16 02:54:44 +00:00
|
|
|
mState = iconStateHaveHeight;
|
2009-09-12 22:44:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case iconStateHaveHeight:
|
|
|
|
|
|
|
|
// Grab the Height
|
2012-08-22 15:56:38 +00:00
|
|
|
mHeight = (uint8_t)*aBuffer;
|
2009-09-12 22:44:18 +00:00
|
|
|
|
2010-08-23 02:30:46 +00:00
|
|
|
// Post our size to the superclass
|
|
|
|
PostSize(mWidth, mHeight);
|
2011-05-11 09:46:59 +00:00
|
|
|
if (HasError()) {
|
2012-05-19 19:32:37 +00:00
|
|
|
// Setting the size led to an error.
|
2011-05-11 09:46:59 +00:00
|
|
|
mState = iconStateFinished;
|
|
|
|
return;
|
|
|
|
}
|
2009-09-12 22:44:18 +00:00
|
|
|
|
2010-08-23 02:30:46 +00:00
|
|
|
// If We're doing a size decode, we're done
|
|
|
|
if (IsSizeDecode()) {
|
2009-10-16 02:54:44 +00:00
|
|
|
mState = iconStateFinished;
|
2009-09-12 22:44:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the frame and signal
|
2011-09-29 13:17:13 +00:00
|
|
|
rv = mImage.EnsureFrame(0, 0, 0, mWidth, mHeight,
|
|
|
|
gfxASurface::ImageFormatARGB32,
|
|
|
|
&mImageData, &mPixBytesTotal);
|
2009-09-12 22:44:18 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2010-09-12 15:22:27 +00:00
|
|
|
PostDecoderError(rv);
|
2010-09-12 15:22:30 +00:00
|
|
|
return;
|
2009-09-12 22:44:18 +00:00
|
|
|
}
|
2010-08-23 02:30:46 +00:00
|
|
|
|
|
|
|
// Tell the superclass we're starting a frame
|
|
|
|
PostFrameStart();
|
2009-09-12 22:44:18 +00:00
|
|
|
|
|
|
|
// Book Keeping
|
2009-10-16 02:54:44 +00:00
|
|
|
aBuffer++;
|
2009-09-12 22:44:18 +00:00
|
|
|
aCount--;
|
2009-10-16 02:54:44 +00:00
|
|
|
mState = iconStateReadPixels;
|
2009-09-12 22:44:18 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case iconStateReadPixels:
|
|
|
|
|
|
|
|
// How many bytes are we reading?
|
2011-06-02 12:56:50 +00:00
|
|
|
bytesToRead = NS_MIN(aCount, mPixBytesTotal - mPixBytesRead);
|
2007-02-27 21:13:25 +00:00
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
// Copy the bytes
|
2009-10-16 02:54:44 +00:00
|
|
|
memcpy(mImageData + mPixBytesRead, aBuffer, bytesToRead);
|
2001-04-02 04:56:18 +00:00
|
|
|
|
2010-08-24 21:12:04 +00:00
|
|
|
// Invalidate
|
|
|
|
PostInvalidation(r);
|
2001-04-02 04:56:18 +00:00
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
// Book Keeping
|
2009-10-16 02:54:44 +00:00
|
|
|
aBuffer += bytesToRead;
|
2009-09-12 22:44:18 +00:00
|
|
|
aCount -= bytesToRead;
|
2009-10-16 02:54:44 +00:00
|
|
|
mPixBytesRead += bytesToRead;
|
2008-09-10 20:23:29 +00:00
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
// If we've got all the pixel bytes, we're finished
|
2009-10-16 02:54:44 +00:00
|
|
|
if (mPixBytesRead == mPixBytesTotal) {
|
2010-09-12 15:22:30 +00:00
|
|
|
PostFrameStop();
|
|
|
|
PostDecodeDone();
|
2009-10-16 02:54:44 +00:00
|
|
|
mState = iconStateFinished;
|
2009-09-12 22:44:18 +00:00
|
|
|
}
|
|
|
|
break;
|
2007-02-06 01:59:19 +00:00
|
|
|
|
2009-09-12 22:44:18 +00:00
|
|
|
case iconStateFinished:
|
|
|
|
|
|
|
|
// Consume all excess data silently
|
|
|
|
aCount = 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-06 16:02:27 +00:00
|
|
|
} // namespace image
|
2010-08-23 02:30:46 +00:00
|
|
|
} // namespace mozilla
|