2015-07-11 05:26:15 +03:00
|
|
|
/* vim:set tw=80 expandtab softtabstop=2 ts=2 sw=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-11-03 10:10:51 +03:00
|
|
|
|
|
|
|
/* This is a Cross-Platform ICO Decoder, which should work everywhere, including
|
|
|
|
* Big-Endian machines like the PowerPC. */
|
|
|
|
|
2016-06-30 20:27:03 +03:00
|
|
|
#include "nsICODecoder.h"
|
|
|
|
|
2005-05-27 02:43:36 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2016-05-22 23:31:11 +03:00
|
|
|
#include "mozilla/EndianUtils.h"
|
2014-11-27 00:22:10 +03:00
|
|
|
#include "mozilla/Move.h"
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2010-08-14 08:09:49 +04:00
|
|
|
#include "RasterImage.h"
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2015-09-19 23:34:14 +03:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
namespace mozilla {
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
// Constants.
|
|
|
|
static const uint32_t ICOHEADERSIZE = 6;
|
2015-10-16 01:43:31 +03:00
|
|
|
static const uint32_t BITMAPINFOSIZE = bmp::InfoHeaderLength::WIN_ICO;
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2001-11-03 12:50:31 +03:00
|
|
|
// ----------------------------------------
|
|
|
|
// Actual Data Processing
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2011-08-26 00:09:01 +04:00
|
|
|
// Obtains the number of colors from the bits per pixel
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t
|
2014-11-14 20:59:00 +03:00
|
|
|
nsICODecoder::GetNumColors()
|
2011-08-26 00:09:01 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t numColors = 0;
|
2011-08-26 00:09:01 +04:00
|
|
|
if (mBPP <= 8) {
|
|
|
|
switch (mBPP) {
|
|
|
|
case 1:
|
|
|
|
numColors = 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
numColors = 16;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
numColors = 256;
|
|
|
|
break;
|
|
|
|
default:
|
2012-08-22 19:56:38 +04:00
|
|
|
numColors = (uint16_t)-1;
|
2011-08-26 00:09:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return numColors;
|
|
|
|
}
|
|
|
|
|
2015-01-16 02:11:35 +03:00
|
|
|
nsICODecoder::nsICODecoder(RasterImage* aImage)
|
2015-09-19 09:12:30 +03:00
|
|
|
: Decoder(aImage)
|
2016-07-18 08:51:19 +03:00
|
|
|
, mLexer(Transition::To(ICOState::HEADER, ICOHEADERSIZE),
|
|
|
|
Transition::TerminateSuccess())
|
2017-07-22 12:03:46 +03:00
|
|
|
, mBiggestResourceColorDepth(0)
|
|
|
|
, mBestResourceDelta(INT_MIN)
|
|
|
|
, mBestResourceColorDepth(0)
|
2015-09-19 09:12:30 +03:00
|
|
|
, mNumIcons(0)
|
|
|
|
, mCurrIcon(0)
|
|
|
|
, mBPP(0)
|
|
|
|
, mMaskRowSize(0)
|
|
|
|
, mCurrMaskLine(0)
|
2015-09-22 05:52:31 +03:00
|
|
|
, mIsCursor(false)
|
|
|
|
, mHasMaskAlpha(false)
|
2015-09-19 09:12:30 +03:00
|
|
|
{ }
|
2015-09-18 20:54:40 +03:00
|
|
|
|
2016-07-11 10:38:54 +03:00
|
|
|
nsresult
|
2010-08-23 06:30:46 +04:00
|
|
|
nsICODecoder::FinishInternal()
|
2001-11-03 10:10:51 +03:00
|
|
|
{
|
2010-09-12 19:22:31 +04:00
|
|
|
// We shouldn't be called in error cases
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!HasError(), "Shouldn't call FinishInternal after error!");
|
2010-09-12 19:22:31 +04:00
|
|
|
|
2016-07-11 10:38:54 +03:00
|
|
|
return GetFinalStateFromContainedDecoder();
|
2015-07-11 05:26:15 +03:00
|
|
|
}
|
|
|
|
|
2016-07-11 10:38:54 +03:00
|
|
|
nsresult
|
2015-07-11 05:26:15 +03:00
|
|
|
nsICODecoder::FinishWithErrorInternal()
|
|
|
|
{
|
2017-02-10 16:33:11 +03:00
|
|
|
// No need to assert !mInFrame here because this condition is enforced by
|
|
|
|
// mContainedDecoder.
|
2016-07-11 10:38:54 +03:00
|
|
|
return GetFinalStateFromContainedDecoder();
|
2015-07-11 05:26:15 +03:00
|
|
|
}
|
|
|
|
|
2016-07-11 10:38:54 +03:00
|
|
|
nsresult
|
2015-07-11 05:26:15 +03:00
|
|
|
nsICODecoder::GetFinalStateFromContainedDecoder()
|
|
|
|
{
|
|
|
|
if (!mContainedDecoder) {
|
2016-07-11 10:38:54 +03:00
|
|
|
return NS_OK;
|
2015-07-11 05:26:15 +03:00
|
|
|
}
|
|
|
|
|
2016-07-03 08:21:00 +03:00
|
|
|
// Let the contained decoder finish up if necessary.
|
2017-07-22 14:50:31 +03:00
|
|
|
FlushContainedDecoder();
|
2016-07-03 08:21:00 +03:00
|
|
|
|
|
|
|
// Make our state the same as the state of the contained decoder.
|
2015-07-11 05:26:15 +03:00
|
|
|
mDecodeDone = mContainedDecoder->GetDecodeDone();
|
|
|
|
mProgress |= mContainedDecoder->TakeProgress();
|
|
|
|
mInvalidRect.UnionRect(mInvalidRect, mContainedDecoder->TakeInvalidRect());
|
|
|
|
mCurrentFrame = mContainedDecoder->GetCurrentFrameRef();
|
2015-09-19 09:12:30 +03:00
|
|
|
|
2016-07-11 10:38:54 +03:00
|
|
|
// Propagate errors.
|
|
|
|
nsresult rv = HasError() || mContainedDecoder->HasError()
|
|
|
|
? NS_ERROR_FAILURE
|
|
|
|
: NS_OK;
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_FAILED(rv) || !mCurrentFrame || mCurrentFrame->IsFinished());
|
|
|
|
return rv;
|
2011-08-26 00:09:01 +04:00
|
|
|
}
|
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
LexerTransition<ICOState>
|
|
|
|
nsICODecoder::ReadHeader(const char* aData)
|
2015-09-18 20:54:27 +03:00
|
|
|
{
|
2015-09-19 09:12:30 +03:00
|
|
|
// If the third byte is 1, this is an icon. If 2, a cursor.
|
|
|
|
if ((aData[2] != 1) && (aData[2] != 2)) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2011-08-26 00:09:01 +04:00
|
|
|
}
|
2015-09-19 09:12:30 +03:00
|
|
|
mIsCursor = (aData[2] == 2);
|
2011-08-26 00:09:01 +04:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
// The fifth and sixth bytes specify the number of resources in the file.
|
2015-10-16 03:54:41 +03:00
|
|
|
mNumIcons = LittleEndian::readUint16(aData + 4);
|
2015-09-18 23:01:25 +03:00
|
|
|
if (mNumIcons == 0) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateSuccess(); // Nothing to do.
|
2011-08-26 00:09:01 +04:00
|
|
|
}
|
|
|
|
|
2015-09-19 23:34:14 +03:00
|
|
|
// Downscale-during-decode can end up decoding different resources in the ICO
|
|
|
|
// file depending on the target size. Since the resources are not necessarily
|
|
|
|
// scaled versions of the same image, some may be transparent and some may not
|
|
|
|
// be. We could be precise about transparency if we decoded the metadata of
|
|
|
|
// every resource, but for now we don't and it's safest to assume that
|
|
|
|
// transparency could be present.
|
|
|
|
PostHasTransparency();
|
2011-08-26 00:09:01 +04:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
return Transition::To(ICOState::DIR_ENTRY, ICODIRENTRYSIZE);
|
|
|
|
}
|
2005-01-12 23:16:07 +03:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
size_t
|
|
|
|
nsICODecoder::FirstResourceOffset() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mNumIcons > 0,
|
|
|
|
"Calling FirstResourceOffset before processing header");
|
2005-01-12 23:16:07 +03:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
// The first resource starts right after the directory, which starts right
|
|
|
|
// after the ICO header.
|
|
|
|
return ICOHEADERSIZE + mNumIcons * ICODIRENTRYSIZE;
|
|
|
|
}
|
2011-08-26 00:09:01 +04:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
LexerTransition<ICOState>
|
|
|
|
nsICODecoder::ReadDirEntry(const char* aData)
|
|
|
|
{
|
|
|
|
mCurrIcon++;
|
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
// Read the directory entry.
|
|
|
|
IconDirEntry e;
|
|
|
|
e.mWidth = aData[0];
|
|
|
|
e.mHeight = aData[1];
|
|
|
|
e.mColorCount = aData[2];
|
|
|
|
e.mReserved = aData[3];
|
|
|
|
e.mPlanes = LittleEndian::readUint16(aData + 4);
|
|
|
|
e.mBitCount = LittleEndian::readUint16(aData + 6);
|
|
|
|
e.mBytesInRes = LittleEndian::readUint32(aData + 8);
|
|
|
|
e.mImageOffset = LittleEndian::readUint32(aData + 12);
|
2017-07-22 07:14:59 +03:00
|
|
|
|
|
|
|
// If an explicit output size was specified, we'll try to select the resource
|
|
|
|
// that matches it best below.
|
|
|
|
const Maybe<IntSize> desiredSize = ExplicitOutputSize();
|
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
// Determine if this is the biggest resource we've seen so far. We always use
|
|
|
|
// the biggest resource for the intrinsic size, and if we don't have a
|
|
|
|
// specific desired size, we select it as the best resource as well.
|
|
|
|
IntSize entrySize(GetRealWidth(e), GetRealHeight(e));
|
|
|
|
if (e.mBitCount >= mBiggestResourceColorDepth &&
|
|
|
|
entrySize.width * entrySize.height >=
|
|
|
|
mBiggestResourceSize.width * mBiggestResourceSize.height) {
|
|
|
|
mBiggestResourceSize = entrySize;
|
|
|
|
mBiggestResourceColorDepth = e.mBitCount;
|
|
|
|
mBiggestResourceHotSpot = IntSize(e.mXHotspot, e.mYHotspot);
|
|
|
|
|
|
|
|
if (!desiredSize) {
|
|
|
|
mDirEntry = e;
|
2015-09-19 23:34:06 +03:00
|
|
|
}
|
2017-07-22 12:03:46 +03:00
|
|
|
}
|
2015-09-19 23:34:06 +03:00
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
mImageMetadata.AddNativeSize(entrySize);
|
|
|
|
|
|
|
|
if (desiredSize) {
|
|
|
|
// Calculate the delta between this resource's size and the desired size, so
|
|
|
|
// we can see if it is better than our current-best option. In the case of
|
|
|
|
// several equally-good resources, we use the last one. "Better" in this
|
|
|
|
// case is determined by |delta|, a measure of the difference in size
|
|
|
|
// between the entry we've found and the desired size. We will choose the
|
|
|
|
// smallest resource that is greater than or equal to the desired size (i.e.
|
|
|
|
// we assume it's better to downscale a larger icon than to upscale a
|
|
|
|
// smaller one).
|
|
|
|
int32_t delta = std::min(entrySize.width - desiredSize->width,
|
|
|
|
entrySize.height - desiredSize->height);
|
|
|
|
if (e.mBitCount >= mBestResourceColorDepth &&
|
|
|
|
((mBestResourceDelta < 0 && delta >= mBestResourceDelta) ||
|
|
|
|
(delta >= 0 && delta <= mBestResourceDelta))) {
|
|
|
|
mBestResourceDelta = delta;
|
|
|
|
mBestResourceColorDepth = e.mBitCount;
|
|
|
|
mDirEntry = e;
|
2015-09-19 23:34:14 +03:00
|
|
|
}
|
2017-07-22 07:14:59 +03:00
|
|
|
}
|
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
if (mCurrIcon == mNumIcons) {
|
|
|
|
// Ensure the resource we selected has an offset past the ICO headers.
|
|
|
|
if (mDirEntry.mImageOffset < FirstResourceOffset()) {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
2015-09-19 23:34:14 +03:00
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
// If this is a cursor, set the hotspot. We use the hotspot from the biggest
|
|
|
|
// resource since we also use that resource for the intrinsic size.
|
|
|
|
if (mIsCursor) {
|
|
|
|
mImageMetadata.SetHotspot(mBiggestResourceHotSpot.width,
|
|
|
|
mBiggestResourceHotSpot.height);
|
|
|
|
}
|
2015-09-18 20:54:27 +03:00
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
// We always report the biggest resource's size as the intrinsic size; this
|
|
|
|
// is necessary for downscale-during-decode to work since we won't even
|
|
|
|
// attempt to *upscale* while decoding.
|
|
|
|
PostSize(mBiggestResourceSize.width, mBiggestResourceSize.height);
|
2017-07-22 14:50:31 +03:00
|
|
|
if (HasError()) {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
if (IsMetadataDecode()) {
|
|
|
|
return Transition::TerminateSuccess();
|
|
|
|
}
|
2017-07-22 07:14:59 +03:00
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
// If the resource we selected matches the output size perfectly, we don't
|
|
|
|
// need to do any downscaling.
|
|
|
|
if (GetRealSize() == OutputSize()) {
|
|
|
|
MOZ_ASSERT_IF(desiredSize, GetRealSize() == *desiredSize);
|
|
|
|
MOZ_ASSERT_IF(!desiredSize, GetRealSize() == Size());
|
|
|
|
mDownscaler.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t offsetToResource = mDirEntry.mImageOffset - FirstResourceOffset();
|
|
|
|
return Transition::ToUnbuffered(ICOState::FOUND_RESOURCE,
|
|
|
|
ICOState::SKIP_TO_RESOURCE,
|
|
|
|
offsetToResource);
|
2017-07-22 07:14:59 +03:00
|
|
|
}
|
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
return Transition::To(ICOState::DIR_ENTRY, ICODIRENTRYSIZE);
|
2015-09-19 09:12:30 +03:00
|
|
|
}
|
2015-09-18 20:54:27 +03:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
LexerTransition<ICOState>
|
|
|
|
nsICODecoder::SniffResource(const char* aData)
|
|
|
|
{
|
2017-07-22 14:50:32 +03:00
|
|
|
// We have BITMAPINFOSIZE bytes buffered at this point. We know an embedded
|
|
|
|
// BMP will have at least that many bytes by definition. We can also infer
|
|
|
|
// that any valid embedded PNG will contain that many bytes as well because:
|
|
|
|
// BITMAPINFOSIZE
|
|
|
|
// <
|
|
|
|
// signature (8 bytes) +
|
|
|
|
// IHDR (12 bytes header + 13 bytes data)
|
|
|
|
// IDAT (12 bytes header)
|
2017-07-22 14:50:31 +03:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
// We use the first PNGSIGNATURESIZE bytes to determine whether this resource
|
|
|
|
// is a PNG or a BMP.
|
|
|
|
bool isPNG = !memcmp(aData, nsPNGDecoder::pngSignatureBytes,
|
|
|
|
PNGSIGNATURESIZE);
|
|
|
|
if (isPNG) {
|
2017-07-22 14:50:32 +03:00
|
|
|
if (mDirEntry.mBytesInRes <= BITMAPINFOSIZE) {
|
2017-07-22 14:50:31 +03:00
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
2017-07-22 14:50:32 +03:00
|
|
|
// Prepare a new iterator for the contained decoder to advance as it wills.
|
|
|
|
// Cloning at the point ensures it will begin at the resource offset.
|
|
|
|
SourceBufferIterator containedIterator
|
|
|
|
= mLexer.Clone(*mIterator, mDirEntry.mBytesInRes);
|
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
// Create a PNG decoder which will do the rest of the work for us.
|
2016-07-03 06:20:55 +03:00
|
|
|
mContainedDecoder =
|
|
|
|
DecoderFactory::CreateDecoderForICOResource(DecoderType::PNG,
|
2017-07-22 14:50:32 +03:00
|
|
|
Move(containedIterator),
|
2017-07-22 14:50:31 +03:00
|
|
|
WrapNotNull(this),
|
2017-07-22 14:50:32 +03:00
|
|
|
false,
|
2017-07-22 14:50:31 +03:00
|
|
|
Some(GetRealSize()));
|
2015-09-18 20:54:27 +03:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
// Read in the rest of the PNG unbuffered.
|
2017-07-22 14:50:32 +03:00
|
|
|
size_t toRead = mDirEntry.mBytesInRes - BITMAPINFOSIZE;
|
2015-09-19 09:12:30 +03:00
|
|
|
return Transition::ToUnbuffered(ICOState::FINISHED_RESOURCE,
|
2017-07-22 14:50:31 +03:00
|
|
|
ICOState::READ_RESOURCE,
|
2015-09-19 09:12:30 +03:00
|
|
|
toRead);
|
|
|
|
} else {
|
|
|
|
// Make sure we have a sane size for the bitmap information header.
|
2015-10-16 03:54:41 +03:00
|
|
|
int32_t bihSize = LittleEndian::readUint32(aData);
|
2015-09-19 09:12:30 +03:00
|
|
|
if (bihSize != static_cast<int32_t>(BITMAPINFOSIZE)) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-18 23:01:25 +03:00
|
|
|
}
|
2015-09-18 20:54:27 +03:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
// Read in the rest of the bitmap information header.
|
2017-07-22 14:50:32 +03:00
|
|
|
return ReadBIH(aData);
|
2015-09-19 09:12:30 +03:00
|
|
|
}
|
|
|
|
}
|
2014-11-14 20:59:00 +03:00
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
LexerTransition<ICOState>
|
2017-07-22 14:50:31 +03:00
|
|
|
nsICODecoder::ReadResource()
|
2015-09-19 09:12:30 +03:00
|
|
|
{
|
2017-07-22 14:50:31 +03:00
|
|
|
if (!FlushContainedDecoder()) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-19 09:12:30 +03:00
|
|
|
}
|
2015-09-18 20:54:27 +03:00
|
|
|
|
2017-07-22 14:50:31 +03:00
|
|
|
return Transition::ContinueUnbuffered(ICOState::READ_RESOURCE);
|
2015-09-19 09:12:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<ICOState>
|
|
|
|
nsICODecoder::ReadBIH(const char* aData)
|
|
|
|
{
|
2015-10-16 03:35:21 +03:00
|
|
|
// Extract the BPP from the BIH header; it should be trusted over the one
|
2015-10-16 03:54:41 +03:00
|
|
|
// we have from the ICO header which is usually set to 0.
|
2017-07-22 14:50:32 +03:00
|
|
|
mBPP = LittleEndian::readUint16(aData + 14);
|
|
|
|
|
|
|
|
// Check to make sure we have valid color settings.
|
|
|
|
uint16_t numColors = GetNumColors();
|
|
|
|
if (numColors == uint16_t(-1)) {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
// The color table is present only if BPP is <= 8.
|
|
|
|
MOZ_ASSERT_IF(mBPP > 8, numColors == 0);
|
2015-09-19 09:12:30 +03:00
|
|
|
|
|
|
|
// The ICO format when containing a BMP does not include the 14 byte
|
2015-10-16 01:43:31 +03:00
|
|
|
// bitmap file header. So we create the BMP decoder via the constructor that
|
|
|
|
// tells it to skip this, and pass in the required data (dataOffset) that
|
|
|
|
// would have been present in the header.
|
2017-07-22 14:50:32 +03:00
|
|
|
uint32_t dataOffset = bmp::FILE_HEADER_LENGTH + BITMAPINFOSIZE + 4 * numColors;
|
|
|
|
|
|
|
|
// Prepare a new iterator for the contained decoder to advance as it wills.
|
|
|
|
// Cloning at the point ensures it will begin at the resource offset.
|
|
|
|
SourceBufferIterator containedIterator
|
|
|
|
= mLexer.Clone(*mIterator, mDirEntry.mBytesInRes);
|
2015-09-18 20:54:32 +03:00
|
|
|
|
2015-10-16 01:43:31 +03:00
|
|
|
// Create a BMP decoder which will do most of the work for us; the exception
|
|
|
|
// is the AND mask, which isn't present in standalone BMPs.
|
2016-07-03 06:20:55 +03:00
|
|
|
mContainedDecoder =
|
|
|
|
DecoderFactory::CreateDecoderForICOResource(DecoderType::BMP,
|
2017-07-22 14:50:32 +03:00
|
|
|
Move(containedIterator),
|
2016-07-03 06:20:55 +03:00
|
|
|
WrapNotNull(this),
|
2017-07-22 14:50:32 +03:00
|
|
|
false,
|
2017-07-22 14:50:31 +03:00
|
|
|
Some(GetRealSize()),
|
2016-07-03 06:20:55 +03:00
|
|
|
Some(dataOffset));
|
2017-07-22 14:50:31 +03:00
|
|
|
|
2016-07-03 06:20:55 +03:00
|
|
|
RefPtr<nsBMPDecoder> bmpDecoder =
|
|
|
|
static_cast<nsBMPDecoder*>(mContainedDecoder.get());
|
2015-09-18 20:54:27 +03:00
|
|
|
|
2017-07-22 14:50:31 +03:00
|
|
|
// Ensure the decoder has parsed at least the BMP's bitmap info header.
|
|
|
|
if (!FlushContainedDecoder()) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-19 09:12:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do we have an AND mask on this BMP? If so, we need to read it after we read
|
|
|
|
// the BMP data itself.
|
|
|
|
uint32_t bmpDataLength = bmpDecoder->GetCompressedImageSize() + 4 * numColors;
|
2017-07-22 12:03:46 +03:00
|
|
|
bool hasANDMask = (BITMAPINFOSIZE + bmpDataLength) < mDirEntry.mBytesInRes;
|
2015-09-19 09:12:30 +03:00
|
|
|
ICOState afterBMPState = hasANDMask ? ICOState::PREPARE_FOR_MASK
|
|
|
|
: ICOState::FINISHED_RESOURCE;
|
|
|
|
|
|
|
|
// Read in the rest of the BMP unbuffered.
|
|
|
|
return Transition::ToUnbuffered(afterBMPState,
|
2017-07-22 14:50:31 +03:00
|
|
|
ICOState::READ_RESOURCE,
|
2015-09-19 09:12:30 +03:00
|
|
|
bmpDataLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<ICOState>
|
|
|
|
nsICODecoder::PrepareForMask()
|
|
|
|
{
|
2017-07-22 14:50:31 +03:00
|
|
|
// We have received all of the data required by the BMP decoder so flushing
|
|
|
|
// here guarantees the decode has finished.
|
|
|
|
if (!FlushContainedDecoder()) {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mContainedDecoder->GetDecodeDone());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsBMPDecoder> bmpDecoder =
|
2015-09-19 09:12:30 +03:00
|
|
|
static_cast<nsBMPDecoder*>(mContainedDecoder.get());
|
|
|
|
|
|
|
|
uint16_t numColors = GetNumColors();
|
|
|
|
MOZ_ASSERT(numColors != uint16_t(-1));
|
|
|
|
|
|
|
|
// Determine the length of the AND mask.
|
|
|
|
uint32_t bmpLengthWithHeader =
|
|
|
|
BITMAPINFOSIZE + bmpDecoder->GetCompressedImageSize() + 4 * numColors;
|
2017-07-22 12:03:46 +03:00
|
|
|
MOZ_ASSERT(bmpLengthWithHeader < mDirEntry.mBytesInRes);
|
|
|
|
uint32_t maskLength = mDirEntry.mBytesInRes - bmpLengthWithHeader;
|
2015-09-19 09:12:30 +03:00
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
// If the BMP provides its own transparency, we ignore the AND mask. We can
|
|
|
|
// also obviously ignore it if the image has zero width or zero height.
|
|
|
|
if (bmpDecoder->HasTransparency() ||
|
|
|
|
GetRealWidth() == 0 || GetRealHeight() == 0) {
|
2015-09-19 09:12:30 +03:00
|
|
|
return Transition::ToUnbuffered(ICOState::FINISHED_RESOURCE,
|
|
|
|
ICOState::SKIP_MASK,
|
|
|
|
maskLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the row size for the mask.
|
2017-07-22 12:03:46 +03:00
|
|
|
mMaskRowSize = ((GetRealWidth() + 31) / 32) * 4; // + 31 to round up
|
2015-09-19 09:12:30 +03:00
|
|
|
|
|
|
|
// If the expected size of the AND mask is larger than its actual size, then
|
|
|
|
// we must have a truncated (and therefore corrupt) AND mask.
|
2017-07-22 12:03:46 +03:00
|
|
|
uint32_t expectedLength = mMaskRowSize * GetRealHeight();
|
2015-09-19 09:12:30 +03:00
|
|
|
if (maskLength < expectedLength) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-19 09:12:30 +03:00
|
|
|
}
|
|
|
|
|
2015-09-22 05:52:31 +03:00
|
|
|
// If we're downscaling, the mask is the wrong size for the surface we've
|
|
|
|
// produced, so we need to downscale the mask into a temporary buffer and then
|
|
|
|
// combine the mask's alpha values with the color values from the image.
|
|
|
|
if (mDownscaler) {
|
|
|
|
MOZ_ASSERT(bmpDecoder->GetImageDataLength() ==
|
|
|
|
mDownscaler->TargetSize().width *
|
|
|
|
mDownscaler->TargetSize().height *
|
|
|
|
sizeof(uint32_t));
|
|
|
|
mMaskBuffer = MakeUnique<uint8_t[]>(bmpDecoder->GetImageDataLength());
|
2017-07-22 12:03:46 +03:00
|
|
|
nsresult rv = mDownscaler->BeginFrame(GetRealSize(), Nothing(),
|
2015-09-22 05:52:31 +03:00
|
|
|
mMaskBuffer.get(),
|
|
|
|
/* aHasAlpha = */ true,
|
|
|
|
/* aFlipVertically = */ true);
|
|
|
|
if (NS_FAILED(rv)) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-22 05:52:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
mCurrMaskLine = GetRealHeight();
|
2015-09-19 09:12:30 +03:00
|
|
|
return Transition::To(ICOState::READ_MASK_ROW, mMaskRowSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
LexerTransition<ICOState>
|
|
|
|
nsICODecoder::ReadMaskRow(const char* aData)
|
|
|
|
{
|
|
|
|
mCurrMaskLine--;
|
|
|
|
|
2015-09-22 05:52:31 +03:00
|
|
|
uint8_t sawTransparency = 0;
|
2015-09-19 09:12:30 +03:00
|
|
|
|
2015-09-22 05:52:31 +03:00
|
|
|
// Get the mask row we're reading.
|
|
|
|
const uint8_t* mask = reinterpret_cast<const uint8_t*>(aData);
|
|
|
|
const uint8_t* maskRowEnd = mask + mMaskRowSize;
|
|
|
|
|
|
|
|
// Get the corresponding row of the mask buffer (if we're downscaling) or the
|
|
|
|
// decoded image data (if we're not).
|
|
|
|
uint32_t* decoded = nullptr;
|
|
|
|
if (mDownscaler) {
|
|
|
|
// Initialize the row to all white and fully opaque.
|
2017-07-22 12:03:46 +03:00
|
|
|
memset(mDownscaler->RowBuffer(), 0xFF, GetRealWidth() * sizeof(uint32_t));
|
2015-09-22 05:52:31 +03:00
|
|
|
|
|
|
|
decoded = reinterpret_cast<uint32_t*>(mDownscaler->RowBuffer());
|
|
|
|
} else {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsBMPDecoder> bmpDecoder =
|
2015-09-22 05:52:31 +03:00
|
|
|
static_cast<nsBMPDecoder*>(mContainedDecoder.get());
|
|
|
|
uint32_t* imageData = bmpDecoder->GetImageData();
|
|
|
|
if (!imageData) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-22 05:52:31 +03:00
|
|
|
}
|
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
decoded = imageData + mCurrMaskLine * GetRealWidth();
|
2015-09-19 09:12:30 +03:00
|
|
|
}
|
2015-09-18 20:54:27 +03:00
|
|
|
|
2015-09-22 05:52:31 +03:00
|
|
|
MOZ_ASSERT(decoded);
|
2017-07-22 12:03:46 +03:00
|
|
|
uint32_t* decodedRowEnd = decoded + GetRealWidth();
|
2015-09-19 09:12:30 +03:00
|
|
|
|
|
|
|
// Iterate simultaneously through the AND mask and the image data.
|
|
|
|
while (mask < maskRowEnd) {
|
|
|
|
uint8_t idx = *mask++;
|
|
|
|
sawTransparency |= idx;
|
|
|
|
for (uint8_t bit = 0x80; bit && decoded < decodedRowEnd; bit >>= 1) {
|
|
|
|
// Clear pixel completely for transparency.
|
|
|
|
if (idx & bit) {
|
|
|
|
*decoded = 0;
|
2015-09-18 23:01:25 +03:00
|
|
|
}
|
2015-09-19 09:12:30 +03:00
|
|
|
decoded++;
|
2015-09-18 23:01:25 +03:00
|
|
|
}
|
2015-09-18 20:54:27 +03:00
|
|
|
}
|
2015-09-19 09:12:30 +03:00
|
|
|
|
2015-09-22 05:52:31 +03:00
|
|
|
if (mDownscaler) {
|
|
|
|
mDownscaler->CommitRow();
|
|
|
|
}
|
|
|
|
|
2015-09-19 09:12:30 +03:00
|
|
|
// If any bits are set in sawTransparency, then we know at least one pixel was
|
|
|
|
// transparent.
|
|
|
|
if (sawTransparency) {
|
2015-09-22 05:52:31 +03:00
|
|
|
mHasMaskAlpha = true;
|
2015-09-19 09:12:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mCurrMaskLine == 0) {
|
2015-09-22 05:52:31 +03:00
|
|
|
return Transition::To(ICOState::FINISH_MASK, 0);
|
2015-09-19 09:12:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Transition::To(ICOState::READ_MASK_ROW, mMaskRowSize);
|
|
|
|
}
|
|
|
|
|
2015-09-22 05:52:31 +03:00
|
|
|
LexerTransition<ICOState>
|
|
|
|
nsICODecoder::FinishMask()
|
|
|
|
{
|
|
|
|
// If we're downscaling, we now have the appropriate alpha values in
|
|
|
|
// mMaskBuffer. We just need to transfer them to the image.
|
|
|
|
if (mDownscaler) {
|
|
|
|
// Retrieve the image data.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsBMPDecoder> bmpDecoder =
|
2015-09-22 05:52:31 +03:00
|
|
|
static_cast<nsBMPDecoder*>(mContainedDecoder.get());
|
|
|
|
uint8_t* imageData = reinterpret_cast<uint8_t*>(bmpDecoder->GetImageData());
|
|
|
|
if (!imageData) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-09-22 05:52:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate through the alpha values, copying from mask to image.
|
|
|
|
MOZ_ASSERT(mMaskBuffer);
|
|
|
|
MOZ_ASSERT(bmpDecoder->GetImageDataLength() > 0);
|
|
|
|
for (size_t i = 3 ; i < bmpDecoder->GetImageDataLength() ; i += 4) {
|
|
|
|
imageData[i] = mMaskBuffer[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Transition::To(ICOState::FINISHED_RESOURCE, 0);
|
|
|
|
}
|
|
|
|
|
2015-09-19 23:34:06 +03:00
|
|
|
LexerTransition<ICOState>
|
|
|
|
nsICODecoder::FinishResource()
|
|
|
|
{
|
2017-07-22 14:50:31 +03:00
|
|
|
// We have received all of the data required by the PNG/BMP decoder so
|
|
|
|
// flushing here guarantees the decode has finished.
|
|
|
|
if (!FlushContainedDecoder()) {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mContainedDecoder->GetDecodeDone());
|
|
|
|
|
2017-07-22 12:03:46 +03:00
|
|
|
// Make sure the actual size of the resource matches the size in the directory
|
|
|
|
// entry. If not, we consider the image corrupt.
|
|
|
|
if (mContainedDecoder->HasSize() &&
|
|
|
|
mContainedDecoder->Size() != GetRealSize()) {
|
|
|
|
return Transition::TerminateFailure();
|
2015-09-19 23:34:06 +03:00
|
|
|
}
|
|
|
|
|
2017-07-22 14:50:31 +03:00
|
|
|
// Raymond Chen says that 32bpp only are valid PNG ICOs
|
|
|
|
// http://blogs.msdn.com/b/oldnewthing/archive/2010/10/22/10079192.aspx
|
|
|
|
if (!mContainedDecoder->IsValidICOResource()) {
|
|
|
|
return Transition::TerminateFailure();
|
|
|
|
}
|
|
|
|
|
2017-02-08 23:48:59 +03:00
|
|
|
// Finalize the frame which we deferred to ensure we could modify the final
|
|
|
|
// result (e.g. to apply the BMP mask).
|
|
|
|
MOZ_ASSERT(!mContainedDecoder->GetFinalizeFrames());
|
|
|
|
if (mCurrentFrame) {
|
|
|
|
mCurrentFrame->FinalizeSurface();
|
|
|
|
}
|
|
|
|
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateSuccess();
|
2015-09-19 23:34:06 +03:00
|
|
|
}
|
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult
|
2016-07-15 08:39:39 +03:00
|
|
|
nsICODecoder::DoDecode(SourceBufferIterator& aIterator, IResumable* aOnResume)
|
2015-09-19 09:12:30 +03:00
|
|
|
{
|
2016-07-11 10:05:51 +03:00
|
|
|
MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
|
2015-09-19 09:12:30 +03:00
|
|
|
|
2016-07-15 08:39:39 +03:00
|
|
|
return mLexer.Lex(aIterator, aOnResume,
|
2016-07-11 10:07:07 +03:00
|
|
|
[=](ICOState aState, const char* aData, size_t aLength) {
|
|
|
|
switch (aState) {
|
|
|
|
case ICOState::HEADER:
|
|
|
|
return ReadHeader(aData);
|
|
|
|
case ICOState::DIR_ENTRY:
|
|
|
|
return ReadDirEntry(aData);
|
|
|
|
case ICOState::SKIP_TO_RESOURCE:
|
|
|
|
return Transition::ContinueUnbuffered(ICOState::SKIP_TO_RESOURCE);
|
|
|
|
case ICOState::FOUND_RESOURCE:
|
2017-07-22 14:50:32 +03:00
|
|
|
return Transition::To(ICOState::SNIFF_RESOURCE, BITMAPINFOSIZE);
|
2016-07-11 10:07:07 +03:00
|
|
|
case ICOState::SNIFF_RESOURCE:
|
|
|
|
return SniffResource(aData);
|
2017-07-22 14:50:31 +03:00
|
|
|
case ICOState::READ_RESOURCE:
|
2017-07-22 14:50:31 +03:00
|
|
|
return ReadResource();
|
2016-07-11 10:07:07 +03:00
|
|
|
case ICOState::PREPARE_FOR_MASK:
|
|
|
|
return PrepareForMask();
|
|
|
|
case ICOState::READ_MASK_ROW:
|
|
|
|
return ReadMaskRow(aData);
|
|
|
|
case ICOState::FINISH_MASK:
|
|
|
|
return FinishMask();
|
|
|
|
case ICOState::SKIP_MASK:
|
|
|
|
return Transition::ContinueUnbuffered(ICOState::SKIP_MASK);
|
|
|
|
case ICOState::FINISHED_RESOURCE:
|
|
|
|
return FinishResource();
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown ICOState");
|
|
|
|
}
|
|
|
|
});
|
2012-05-08 16:38:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2017-07-22 14:50:31 +03:00
|
|
|
nsICODecoder::FlushContainedDecoder()
|
2012-05-08 16:38:43 +04:00
|
|
|
{
|
2016-07-03 08:21:00 +03:00
|
|
|
MOZ_ASSERT(mContainedDecoder);
|
|
|
|
|
2016-07-11 10:38:54 +03:00
|
|
|
bool succeeded = true;
|
|
|
|
|
2017-07-22 14:50:31 +03:00
|
|
|
// If we run out of data, the ICO decoder will get resumed when there's more
|
|
|
|
// data available, as usual, so we don't need the contained decoder to get
|
|
|
|
// resumed too. To avoid that, we provide an IResumable which just does
|
|
|
|
// nothing. All the caller needs to do is flush when there is new data.
|
2016-07-19 09:46:35 +03:00
|
|
|
LexerResult result = mContainedDecoder->Decode();
|
|
|
|
if (result == LexerResult(TerminalState::FAILURE)) {
|
2016-07-11 10:38:54 +03:00
|
|
|
succeeded = false;
|
2016-07-03 08:21:00 +03:00
|
|
|
}
|
|
|
|
|
2016-07-19 09:46:35 +03:00
|
|
|
MOZ_ASSERT(result != LexerResult(Yield::OUTPUT_AVAILABLE),
|
|
|
|
"Unexpected yield");
|
|
|
|
|
2016-07-11 10:38:54 +03:00
|
|
|
// Make our state the same as the state of the contained decoder, and
|
|
|
|
// propagate errors.
|
2014-11-18 12:48:49 +03:00
|
|
|
mProgress |= mContainedDecoder->TakeProgress();
|
2015-03-25 11:00:00 +03:00
|
|
|
mInvalidRect.UnionRect(mInvalidRect, mContainedDecoder->TakeInvalidRect());
|
2016-07-11 11:09:10 +03:00
|
|
|
if (mContainedDecoder->HasError()) {
|
2016-07-11 10:38:54 +03:00
|
|
|
succeeded = false;
|
2012-05-08 16:38:43 +04:00
|
|
|
}
|
2016-07-03 08:21:00 +03:00
|
|
|
|
2016-07-11 10:38:54 +03:00
|
|
|
return succeeded;
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-23 06:30:46 +04:00
|
|
|
} // namespace mozilla
|