2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#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 "nspr.h"
|
|
|
|
#include "nsRect.h"
|
|
|
|
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2007-07-03 18:09:10 -07:00
|
|
|
|
2010-08-22 19:30:46 -07:00
|
|
|
namespace mozilla {
|
2012-01-06 08:02:27 -08:00
|
|
|
namespace image {
|
2009-09-12 15:44:18 -07:00
|
|
|
|
2013-01-18 13:47:18 -08:00
|
|
|
nsIconDecoder::nsIconDecoder(RasterImage &aImage)
|
|
|
|
: Decoder(aImage),
|
2011-09-27 09:24:03 -07:00
|
|
|
mWidth(-1),
|
|
|
|
mHeight(-1),
|
|
|
|
mPixBytesRead(0),
|
|
|
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
nsIconDecoder::WriteInternal(const char *aBuffer, uint32_t 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.
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bytesToRead = 0;
|
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
|
2012-08-22 08:56:38 -07:00
|
|
|
mWidth = (uint8_t)*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
|
2012-08-22 08:56:38 -07:00
|
|
|
mHeight = (uint8_t)*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);
|
2011-05-11 02:46:59 -07:00
|
|
|
if (HasError()) {
|
2012-05-19 12:32:37 -07:00
|
|
|
// Setting the size led to an error.
|
2011-05-11 02:46:59 -07:00
|
|
|
mState = iconStateFinished;
|
|
|
|
return;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2013-02-01 17:06:30 -08:00
|
|
|
if (!mImageData) {
|
|
|
|
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
|
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
|
|
|
|
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:
|
2013-01-28 09:26:36 -08:00
|
|
|
{
|
2009-09-12 15:44:18 -07:00
|
|
|
|
|
|
|
// How many bytes are we reading?
|
2013-01-28 09:26:36 -08:00
|
|
|
bytesToRead = std::min(aCount, mImageDataLength - 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
|
|
|
|
2013-01-28 09:26:36 -08:00
|
|
|
// Performance isn't critical here, so our update rectangle is
|
|
|
|
// always the full icon
|
|
|
|
nsIntRect r(0, 0, mWidth, mHeight);
|
|
|
|
|
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
|
2013-01-28 09:26:36 -08:00
|
|
|
if (mPixBytesRead == mImageDataLength) {
|
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;
|
2013-01-28 09:26:36 -08:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-06 08:02:27 -08:00
|
|
|
} // namespace image
|
2010-08-22 19:30:46 -07:00
|
|
|
} // namespace mozilla
|