2001-04-02 08:56:18 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04: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 08:56:18 +04:00
|
|
|
|
|
|
|
#include "nsIconDecoder.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nspr.h"
|
|
|
|
#include "nsRect.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2014-11-14 20:59:00 +03:00
|
|
|
#include "RasterImage.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2007-07-04 05:09:10 +04:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
namespace mozilla {
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
nsIconDecoder::nsIconDecoder(RasterImage& aImage)
|
2013-01-19 01:47:18 +04:00
|
|
|
: Decoder(aImage),
|
2011-09-27 20:24:03 +04:00
|
|
|
mWidth(-1),
|
|
|
|
mHeight(-1),
|
|
|
|
mPixBytesRead(0),
|
|
|
|
mState(iconStateStart)
|
2001-04-02 08:56:18 +04:00
|
|
|
{
|
2009-09-13 02:44:18 +04:00
|
|
|
// Nothing to do
|
2001-04-02 08:56:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIconDecoder::~nsIconDecoder()
|
|
|
|
{ }
|
|
|
|
|
2010-09-12 19:22:30 +04:00
|
|
|
void
|
2014-11-14 20:59:00 +03:00
|
|
|
nsIconDecoder::WriteInternal(const char* aBuffer, uint32_t aCount,
|
|
|
|
DecodeStrategy)
|
2001-04-02 08:56:18 +04:00
|
|
|
{
|
2010-09-12 19:22:31 +04:00
|
|
|
NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
|
2010-09-12 19:22:27 +04:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// We put this here to avoid errors about crossing initialization with case
|
|
|
|
// jumps on linux.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bytesToRead = 0;
|
2001-04-02 08:56:18 +04:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// Loop until the input data is gone
|
|
|
|
while (aCount > 0) {
|
2009-10-16 06:54:44 +04:00
|
|
|
switch (mState) {
|
2009-09-13 02:44:18 +04:00
|
|
|
case iconStateStart:
|
|
|
|
|
|
|
|
// Grab the width
|
2012-08-22 19:56:38 +04:00
|
|
|
mWidth = (uint8_t)*aBuffer;
|
2009-09-13 02:44:18 +04:00
|
|
|
|
|
|
|
// Book Keeping
|
2009-10-16 06:54:44 +04:00
|
|
|
aBuffer++;
|
2009-09-13 02:44:18 +04:00
|
|
|
aCount--;
|
2009-10-16 06:54:44 +04:00
|
|
|
mState = iconStateHaveHeight;
|
2009-09-13 02:44:18 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case iconStateHaveHeight:
|
|
|
|
|
|
|
|
// Grab the Height
|
2012-08-22 19:56:38 +04:00
|
|
|
mHeight = (uint8_t)*aBuffer;
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
// Post our size to the superclass
|
|
|
|
PostSize(mWidth, mHeight);
|
2014-11-25 10:42:43 +03:00
|
|
|
|
|
|
|
PostHasTransparency();
|
|
|
|
|
2011-05-11 13:46:59 +04:00
|
|
|
if (HasError()) {
|
2012-05-19 23:32:37 +04:00
|
|
|
// Setting the size led to an error.
|
2011-05-11 13:46:59 +04:00
|
|
|
mState = iconStateFinished;
|
|
|
|
return;
|
|
|
|
}
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
// If We're doing a size decode, we're done
|
|
|
|
if (IsSizeDecode()) {
|
2009-10-16 06:54:44 +04:00
|
|
|
mState = iconStateFinished;
|
2009-09-13 02:44:18 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-02-02 05:06:30 +04:00
|
|
|
if (!mImageData) {
|
|
|
|
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2009-09-13 02:44:18 +04:00
|
|
|
}
|
2010-08-23 06:30:46 +04:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// Book Keeping
|
2009-10-16 06:54:44 +04:00
|
|
|
aBuffer++;
|
2009-09-13 02:44:18 +04:00
|
|
|
aCount--;
|
2009-10-16 06:54:44 +04:00
|
|
|
mState = iconStateReadPixels;
|
2009-09-13 02:44:18 +04:00
|
|
|
break;
|
|
|
|
|
2014-11-14 20:59:00 +03:00
|
|
|
case iconStateReadPixels: {
|
2009-09-13 02:44:18 +04:00
|
|
|
|
|
|
|
// How many bytes are we reading?
|
2013-01-28 21:26:36 +04:00
|
|
|
bytesToRead = std::min(aCount, mImageDataLength - mPixBytesRead);
|
2007-02-28 00:13:25 +03:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// Copy the bytes
|
2009-10-16 06:54:44 +04:00
|
|
|
memcpy(mImageData + mPixBytesRead, aBuffer, bytesToRead);
|
2001-04-02 08:56:18 +04:00
|
|
|
|
2013-01-28 21:26:36 +04:00
|
|
|
// Performance isn't critical here, so our update rectangle is
|
|
|
|
// always the full icon
|
|
|
|
nsIntRect r(0, 0, mWidth, mHeight);
|
|
|
|
|
2010-08-25 01:12:04 +04:00
|
|
|
// Invalidate
|
|
|
|
PostInvalidation(r);
|
2001-04-02 08:56:18 +04:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// Book Keeping
|
2009-10-16 06:54:44 +04:00
|
|
|
aBuffer += bytesToRead;
|
2009-09-13 02:44:18 +04:00
|
|
|
aCount -= bytesToRead;
|
2009-10-16 06:54:44 +04:00
|
|
|
mPixBytesRead += bytesToRead;
|
2008-09-11 00:23:29 +04:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// If we've got all the pixel bytes, we're finished
|
2013-01-28 21:26:36 +04:00
|
|
|
if (mPixBytesRead == mImageDataLength) {
|
2010-09-12 19:22:30 +04:00
|
|
|
PostFrameStop();
|
|
|
|
PostDecodeDone();
|
2009-10-16 06:54:44 +04:00
|
|
|
mState = iconStateFinished;
|
2009-09-13 02:44:18 +04:00
|
|
|
}
|
|
|
|
break;
|
2013-01-28 21:26:36 +04:00
|
|
|
}
|
2007-02-06 04:59:19 +03:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
case iconStateFinished:
|
|
|
|
|
|
|
|
// Consume all excess data silently
|
|
|
|
aCount = 0;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-23 06:30:46 +04:00
|
|
|
} // namespace mozilla
|