2001-11-03 10:10:51 +03:00
|
|
|
/* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4: */
|
2004-04-19 02:01:16 +04:00
|
|
|
/* ***** 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.
|
|
|
|
*
|
2001-11-03 10:10:51 +03:00
|
|
|
* The Original Code is the Mozilla BMP Decoder.
|
2004-04-19 02:01:16 +04:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Christian Biesinger <cbiesinger@web.de>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2001
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
2001-11-03 10:10:51 +03:00
|
|
|
* Contributor(s):
|
2002-12-11 17:06:51 +03:00
|
|
|
* Neil Rashbrook <neil@parkwaycc.co.uk>
|
2009-09-13 02:44:18 +04:00
|
|
|
* Bobby Holley <bobbyholley@gmail.com>
|
2011-08-26 00:09:01 +04:00
|
|
|
* Brian R. Bondy <netzen@gmail.com>
|
2004-04-19 02:01:16 +04:00
|
|
|
*
|
2001-11-03 10:10:51 +03:00
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-19 02:01:16 +04:00
|
|
|
* 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"),
|
2001-11-03 10:10:51 +03:00
|
|
|
* 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
|
2004-04-19 02:01:16 +04:00
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
2001-11-03 10:10:51 +03:00
|
|
|
* 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.
|
2004-04-19 02:01:16 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-11-03 10:10:51 +03:00
|
|
|
/* I got the format description from http://www.daubnet.com/formats/BMP.html */
|
|
|
|
|
|
|
|
/* This is a Cross-Platform BMP Decoder, which should work everywhere, including
|
|
|
|
* Big-Endian machines like the PowerPC. */
|
|
|
|
|
2002-12-11 17:06:51 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2011-10-02 23:01:38 +04:00
|
|
|
#include "EndianMacros.h"
|
2001-11-03 10:10:51 +03:00
|
|
|
#include "nsBMPDecoder.h"
|
|
|
|
|
|
|
|
#include "nsIInputStream.h"
|
2010-08-14 08:09:49 +04:00
|
|
|
#include "RasterImage.h"
|
2001-11-03 10:10:51 +03:00
|
|
|
#include "imgIContainerObserver.h"
|
2011-07-08 21:55:03 +04:00
|
|
|
#include "ImageLogging.h"
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace imagelib {
|
2010-08-14 08:09:49 +04:00
|
|
|
|
2004-01-21 20:30:14 +03:00
|
|
|
#ifdef PR_LOGGING
|
2001-11-03 10:10:51 +03:00
|
|
|
PRLogModuleInfo *gBMPLog = PR_NewLogModule("BMPDecoder");
|
2004-01-21 20:30:14 +03:00
|
|
|
#endif
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2007-07-18 01:08:46 +04:00
|
|
|
// Convert from row (1..height) to absolute line (0..height-1)
|
|
|
|
#define LINE(row) ((mBIH.height < 0) ? (-mBIH.height - (row)) : ((row) - 1))
|
|
|
|
#define PIXEL_OFFSET(row, col) (LINE(row) * mBIH.width + col)
|
|
|
|
|
2011-09-29 17:17:13 +04:00
|
|
|
nsBMPDecoder::nsBMPDecoder(RasterImage &aImage, imgIDecoderObserver* aObserver)
|
2011-09-27 20:24:03 +04:00
|
|
|
: Decoder(aImage, aObserver)
|
2001-11-03 10:10:51 +03:00
|
|
|
{
|
2011-09-27 20:24:03 +04:00
|
|
|
mColors = nsnull;
|
|
|
|
mRow = nsnull;
|
|
|
|
mImageData = nsnull;
|
|
|
|
mCurPos = mPos = mNumColors = mRowBytes = 0;
|
|
|
|
mOldLine = mCurLine = 1; // Otherwise decoder will never start
|
|
|
|
mState = eRLEStateInitial;
|
|
|
|
mStateData = 0;
|
|
|
|
mLOH = WIN_HEADER_LENGTH;
|
2011-10-17 18:59:28 +04:00
|
|
|
mUseAlphaData = mHaveAlphaData = false;
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsBMPDecoder::~nsBMPDecoder()
|
|
|
|
{
|
2009-09-13 02:44:18 +04:00
|
|
|
delete[] mColors;
|
2011-08-26 00:09:01 +04:00
|
|
|
if (mRow) {
|
|
|
|
moz_free(mRow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets whether or not the BMP will use alpha data
|
|
|
|
void
|
2011-09-29 10:19:26 +04:00
|
|
|
nsBMPDecoder::SetUseAlphaData(bool useAlphaData)
|
2011-08-26 00:09:01 +04:00
|
|
|
{
|
|
|
|
mUseAlphaData = useAlphaData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtains the bits per pixel from the internal BIH header
|
|
|
|
PRInt32
|
|
|
|
nsBMPDecoder::GetBitsPerPixel() const
|
|
|
|
{
|
|
|
|
return mBIH.bpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtains the width from the internal BIH header
|
|
|
|
PRInt32
|
|
|
|
nsBMPDecoder::GetWidth() const
|
|
|
|
{
|
|
|
|
return mBIH.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtains the height from the internal BIH header
|
|
|
|
PRInt32
|
|
|
|
nsBMPDecoder::GetHeight() const
|
|
|
|
{
|
|
|
|
return mBIH.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtains the internal output image buffer
|
|
|
|
PRUint32*
|
|
|
|
nsBMPDecoder::GetImageData()
|
|
|
|
{
|
|
|
|
return mImageData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Obtains the size of the compressed image resource
|
|
|
|
PRInt32
|
|
|
|
nsBMPDecoder::GetCompressedImageSize() const
|
|
|
|
{
|
|
|
|
// For everything except BI_RGB the header field must be defined
|
|
|
|
if (mBIH.compression != BI_RGB) {
|
|
|
|
return mBIH.image_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// mBIH.image_size isn't always filled for BI_RGB so calculate it manually
|
|
|
|
// The pixel array size is calculated based on extra 4 byte boundary padding
|
|
|
|
PRUint32 rowSize = (mBIH.bpp * mBIH.width + 7) / 8; // + 7 to round up
|
|
|
|
// Pad to DWORD Boundary
|
|
|
|
if (rowSize % 4) {
|
|
|
|
rowSize += (4 - (rowSize % 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
// The height should be the absolute value of what the height is in the BIH.
|
|
|
|
// If positive the bitmap is stored bottom to top, otherwise top to bottom
|
|
|
|
PRInt32 pixelArraySize = rowSize * abs(mBIH.height);
|
|
|
|
return pixelArraySize;
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
|
|
|
|
2011-09-09 19:24:23 +04:00
|
|
|
// Obtains whether or not a BMP file had alpha data in its 4th byte
|
|
|
|
// for 32BPP bitmaps. Only use after the bitmap has been processed.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2011-09-09 19:24:23 +04:00
|
|
|
nsBMPDecoder::HasAlphaData() const
|
|
|
|
{
|
|
|
|
return mHaveAlphaData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-12 19:22:30 +04:00
|
|
|
void
|
2010-08-23 06:30:46 +04:00
|
|
|
nsBMPDecoder::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
|
2010-09-12 19:22:31 +04:00
|
|
|
NS_ABORT_IF_FALSE(!HasError(), "Can't call FinishInternal on error!");
|
2010-09-12 19:22:31 +04:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
// We should never make multiple frames
|
|
|
|
NS_ABORT_IF_FALSE(GetFrameCount() <= 1, "Multiple BMP frames?");
|
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// Send notifications if appropriate
|
2010-09-12 19:22:31 +04:00
|
|
|
if (!IsSizeDecode() && (GetFrameCount() == 1)) {
|
2011-08-26 00:09:01 +04:00
|
|
|
|
|
|
|
// Invalidate
|
|
|
|
nsIntRect r(0, 0, mBIH.width, mBIH.height);
|
|
|
|
PostInvalidation(r);
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
PostFrameStop();
|
2010-09-12 19:22:30 +04:00
|
|
|
PostDecodeDone();
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------
|
|
|
|
// Actual Data Processing
|
|
|
|
// ----------------------------------------
|
|
|
|
|
2002-11-10 02:06:25 +03:00
|
|
|
static void calcBitmask(PRUint32 aMask, PRUint8& aBegin, PRUint8& aLength)
|
|
|
|
{
|
|
|
|
// find the rightmost 1
|
|
|
|
PRUint8 pos;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool started = false;
|
2002-11-10 02:06:25 +03:00
|
|
|
aBegin = aLength = 0;
|
|
|
|
for (pos = 0; pos <= 31; pos++) {
|
|
|
|
if (!started && (aMask & (1 << pos))) {
|
|
|
|
aBegin = pos;
|
2011-10-17 18:59:28 +04:00
|
|
|
started = true;
|
2002-11-10 02:06:25 +03:00
|
|
|
}
|
|
|
|
else if (started && !(aMask & (1 << pos))) {
|
|
|
|
aLength = pos - aBegin;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_METHOD nsBMPDecoder::CalcBitShift()
|
|
|
|
{
|
|
|
|
PRUint8 begin, length;
|
|
|
|
// red
|
|
|
|
calcBitmask(mBitFields.red, begin, length);
|
|
|
|
mBitFields.redRightShift = begin;
|
|
|
|
mBitFields.redLeftShift = 8 - length;
|
|
|
|
// green
|
|
|
|
calcBitmask(mBitFields.green, begin, length);
|
|
|
|
mBitFields.greenRightShift = begin;
|
|
|
|
mBitFields.greenLeftShift = 8 - length;
|
|
|
|
// blue
|
|
|
|
calcBitmask(mBitFields.blue, begin, length);
|
|
|
|
mBitFields.blueRightShift = begin;
|
|
|
|
mBitFields.blueLeftShift = 8 - length;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-09-12 19:22:30 +04:00
|
|
|
void
|
2010-08-23 06:30:46 +04:00
|
|
|
nsBMPDecoder::WriteInternal(const char* aBuffer, PRUint32 aCount)
|
2001-11-03 10:10:51 +03:00
|
|
|
{
|
2010-09-12 19:22:31 +04:00
|
|
|
NS_ABORT_IF_FALSE(!HasError(), "Shouldn't call WriteInternal after error!");
|
2009-10-16 06:54:44 +04:00
|
|
|
|
|
|
|
// aCount=0 means EOF, mCurLine=0 means we're past end of image
|
|
|
|
if (!aCount || !mCurLine)
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2001-11-03 10:10:51 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
2011-08-26 00:09:01 +04:00
|
|
|
if (mPos < BFH_INTERNAL_LENGTH) { /* In BITMAPFILEHEADER */
|
|
|
|
PRUint32 toCopy = BFH_INTERNAL_LENGTH - mPos;
|
2001-11-03 10:10:51 +03:00
|
|
|
if (toCopy > aCount)
|
|
|
|
toCopy = aCount;
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(mRawBuf + mPos, aBuffer, toCopy);
|
2001-11-03 10:10:51 +03:00
|
|
|
mPos += toCopy;
|
|
|
|
aCount -= toCopy;
|
|
|
|
aBuffer += toCopy;
|
|
|
|
}
|
2011-08-26 00:09:01 +04:00
|
|
|
if (mPos == BFH_INTERNAL_LENGTH) {
|
2001-11-03 10:10:51 +03:00
|
|
|
ProcessFileHeader();
|
2009-10-16 06:54:44 +04:00
|
|
|
if (mBFH.signature[0] != 'B' || mBFH.signature[1] != 'M') {
|
2010-09-12 19:22:27 +04:00
|
|
|
PostDataError();
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2009-10-16 06:54:44 +04:00
|
|
|
}
|
2004-01-21 20:30:14 +03:00
|
|
|
if (mBFH.bihsize == OS2_BIH_LENGTH)
|
|
|
|
mLOH = OS2_HEADER_LENGTH;
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
2011-08-26 00:09:01 +04:00
|
|
|
if (mPos >= BFH_INTERNAL_LENGTH && mPos < mLOH) { /* In BITMAPINFOHEADER */
|
2001-11-03 10:10:51 +03:00
|
|
|
PRUint32 toCopy = mLOH - mPos;
|
|
|
|
if (toCopy > aCount)
|
|
|
|
toCopy = aCount;
|
2011-08-26 00:09:01 +04:00
|
|
|
memcpy(mRawBuf + (mPos - BFH_INTERNAL_LENGTH), aBuffer, toCopy);
|
2001-11-03 10:10:51 +03:00
|
|
|
mPos += toCopy;
|
|
|
|
aCount -= toCopy;
|
|
|
|
aBuffer += toCopy;
|
|
|
|
}
|
2011-08-26 00:09:01 +04:00
|
|
|
|
|
|
|
// GetNumFrames is called to ensure that if at this point mPos == mLOH but
|
|
|
|
// we have no data left to process, the next time WriteInternal is called
|
|
|
|
// we won't enter this condition again.
|
|
|
|
if (mPos == mLOH && GetFrameCount() == 0) {
|
2001-11-03 10:10:51 +03:00
|
|
|
ProcessInfoHeader();
|
2011-08-26 00:09:01 +04:00
|
|
|
PR_LOG(gBMPLog, PR_LOG_DEBUG, ("BMP is %lix%lix%lu. compression=%lu\n",
|
|
|
|
mBIH.width, mBIH.height, mBIH.bpp, mBIH.compression));
|
2004-01-21 20:30:14 +03:00
|
|
|
// Verify we support this bit depth
|
|
|
|
if (mBIH.bpp != 1 && mBIH.bpp != 4 && mBIH.bpp != 8 &&
|
2009-10-16 06:54:44 +04:00
|
|
|
mBIH.bpp != 16 && mBIH.bpp != 24 && mBIH.bpp != 32) {
|
2010-09-12 19:22:27 +04:00
|
|
|
PostDataError();
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2009-10-16 06:54:44 +04:00
|
|
|
}
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2009-09-13 02:44:18 +04:00
|
|
|
// BMPs with negative width are invalid
|
|
|
|
// Reject extremely wide images to keep the math sane
|
|
|
|
const PRInt32 k64KWidth = 0x0000FFFF;
|
2009-10-16 06:54:44 +04:00
|
|
|
if (mBIH.width < 0 || mBIH.width > k64KWidth) {
|
2010-09-12 19:22:27 +04:00
|
|
|
PostDataError();
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2009-10-16 06:54:44 +04:00
|
|
|
}
|
2009-09-13 02:44:18 +04:00
|
|
|
|
|
|
|
PRUint32 real_height = (mBIH.height > 0) ? mBIH.height : -mBIH.height;
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
// Post our size to the superclass
|
|
|
|
PostSize(mBIH.width, real_height);
|
2011-05-11 13:46:59 +04:00
|
|
|
if (HasError()) {
|
|
|
|
// Setting the size lead to an error; this can happen when for example
|
|
|
|
// a multipart channel sends an image of a different size.
|
|
|
|
return;
|
|
|
|
}
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
// We have the size. If we're doing a size decode, we got what
|
2009-09-13 02:44:18 +04:00
|
|
|
// we came for.
|
2010-08-23 06:30:46 +04:00
|
|
|
if (IsSizeDecode())
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2009-09-13 02:44:18 +04:00
|
|
|
|
|
|
|
// We're doing a real decode.
|
|
|
|
mOldLine = mCurLine = real_height;
|
|
|
|
|
2001-11-03 10:10:51 +03:00
|
|
|
if (mBIH.bpp <= 8) {
|
2004-05-14 01:41:03 +04:00
|
|
|
mNumColors = 1 << mBIH.bpp;
|
|
|
|
if (mBIH.colors && mBIH.colors < mNumColors)
|
|
|
|
mNumColors = mBIH.colors;
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2007-12-14 00:49:05 +03:00
|
|
|
// Always allocate 256 even though mNumColors might be smaller
|
|
|
|
mColors = new colorTable[256];
|
|
|
|
memset(mColors, 0, 256 * sizeof(colorTable));
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
2001-11-20 02:15:27 +03:00
|
|
|
else if (mBIH.compression != BI_BITFIELDS && mBIH.bpp == 16) {
|
|
|
|
// Use default 5-5-5 format
|
|
|
|
mBitFields.red = 0x7C00;
|
|
|
|
mBitFields.green = 0x03E0;
|
|
|
|
mBitFields.blue = 0x001F;
|
2002-11-10 02:06:25 +03:00
|
|
|
CalcBitShift();
|
2001-11-20 02:15:27 +03:00
|
|
|
}
|
2007-07-18 01:08:46 +04:00
|
|
|
|
2011-08-26 00:09:01 +04:00
|
|
|
// Make sure we have a valid value for our supported compression modes
|
|
|
|
// before adding the frame
|
|
|
|
if (mBIH.compression != BI_RGB && mBIH.compression != BI_RLE8 &&
|
|
|
|
mBIH.compression != BI_RLE4 && mBIH.compression != BI_BITFIELDS) {
|
|
|
|
PostDataError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have RLE4 or RLE8 or BI_ALPHABITFIELDS, then ensure we
|
|
|
|
// have valid BPP values before adding the frame
|
|
|
|
if (mBIH.compression == BI_RLE8 && mBIH.bpp != 8) {
|
|
|
|
PR_LOG(gBMPLog, PR_LOG_DEBUG,
|
|
|
|
("BMP RLE8 compression only supports 8 bits per pixel\n"));
|
|
|
|
PostDataError();
|
|
|
|
return;
|
|
|
|
}
|
2011-08-31 23:11:04 +04:00
|
|
|
if (mBIH.compression == BI_RLE4 && mBIH.bpp != 4 && mBIH.bpp != 1) {
|
2011-08-26 00:09:01 +04:00
|
|
|
PR_LOG(gBMPLog, PR_LOG_DEBUG,
|
|
|
|
("BMP RLE4 compression only supports 4 bits per pixel\n"));
|
|
|
|
PostDataError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mBIH.compression == BI_ALPHABITFIELDS &&
|
|
|
|
mBIH.bpp != 16 && mBIH.bpp != 32) {
|
|
|
|
PR_LOG(gBMPLog, PR_LOG_DEBUG,
|
|
|
|
("BMP ALPHABITFIELDS only supports 16 or 32 bits per pixel\n"));
|
|
|
|
PostDataError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Bug 753 - Remove nsIImage, gfxIImageFrame, and their implementations, and expose an equivalent api on imgIContainer. r=roc,josh,bz,longsonr,vlad,karlt,jimm,bsmedberg,mfinkle,peterw,peterv sr=vlad,roc
--HG--
rename : gfx/src/shared/gfxImageFrame.cpp => modules/libpr0n/src/imgFrame.cpp
rename : gfx/src/shared/gfxImageFrame.h => modules/libpr0n/src/imgFrame.h
2009-07-21 05:50:15 +04:00
|
|
|
PRUint32 imageLength;
|
2011-08-26 00:09:01 +04:00
|
|
|
if (mBIH.compression == BI_RLE8 || mBIH.compression == BI_RLE4 ||
|
|
|
|
mBIH.compression == BI_ALPHABITFIELDS) {
|
2011-09-29 17:17:13 +04:00
|
|
|
rv = mImage.EnsureFrame(0, 0, 0, mBIH.width, real_height,
|
|
|
|
gfxASurface::ImageFormatARGB32,
|
|
|
|
(PRUint8**)&mImageData, &imageLength);
|
2002-12-11 17:06:51 +03:00
|
|
|
} else {
|
2007-07-18 01:08:46 +04:00
|
|
|
// mRow is not used for RLE encoded images
|
2011-08-26 00:09:01 +04:00
|
|
|
mRow = (PRUint8*)moz_malloc((mBIH.width * mBIH.bpp) / 8 + 4);
|
|
|
|
// + 4 because the line is padded to a 4 bit boundary, but I don't want
|
2007-07-18 01:08:46 +04:00
|
|
|
// to make exact calculations here, that's unnecessary.
|
|
|
|
// Also, it compensates rounding error.
|
|
|
|
if (!mRow) {
|
2010-09-12 19:22:27 +04:00
|
|
|
PostDecoderError(NS_ERROR_OUT_OF_MEMORY);
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2007-07-18 01:08:46 +04:00
|
|
|
}
|
2011-08-26 00:09:01 +04:00
|
|
|
|
|
|
|
if (mUseAlphaData) {
|
2011-09-29 17:17:13 +04:00
|
|
|
rv = mImage.EnsureFrame(0, 0, 0, mBIH.width, real_height,
|
|
|
|
gfxASurface::ImageFormatARGB32,
|
|
|
|
(PRUint8**)&mImageData, &imageLength);
|
2011-08-26 00:09:01 +04:00
|
|
|
} else {
|
2011-09-29 17:17:13 +04:00
|
|
|
rv = mImage.EnsureFrame(0, 0, 0, mBIH.width, real_height,
|
|
|
|
gfxASurface::ImageFormatRGB24,
|
|
|
|
(PRUint8**)&mImageData, &imageLength);
|
2011-08-26 00:09:01 +04:00
|
|
|
}
|
2002-12-11 17:06:51 +03:00
|
|
|
}
|
2010-09-12 19:22:30 +04:00
|
|
|
if (NS_FAILED(rv) || !mImageData) {
|
2010-09-12 19:22:27 +04:00
|
|
|
PostDecoderError(NS_ERROR_FAILURE);
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2009-10-16 06:54:44 +04:00
|
|
|
}
|
2007-07-18 01:08:46 +04:00
|
|
|
|
2011-08-26 00:09:01 +04:00
|
|
|
// Prepare for transparency
|
2007-07-18 01:08:46 +04:00
|
|
|
if ((mBIH.compression == BI_RLE8) || (mBIH.compression == BI_RLE4)) {
|
|
|
|
// Clear the image, as the RLE may jump over areas
|
|
|
|
memset(mImageData, 0, imageLength);
|
|
|
|
}
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
// Tell the superclass we're starting a frame
|
|
|
|
PostFrameStart();
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
2011-11-16 19:14:40 +04:00
|
|
|
|
|
|
|
if (mColors && mPos >= mLOH) {
|
|
|
|
// OS/2 Bitmaps have no padding byte
|
|
|
|
PRUint8 bytesPerColor = (mBFH.bihsize == OS2_BIH_LENGTH) ? 3 : 4;
|
|
|
|
if (mPos < (mLOH + mNumColors * bytesPerColor)) {
|
|
|
|
// Number of bytes already received
|
|
|
|
PRUint32 colorBytes = mPos - mLOH;
|
|
|
|
// Color which is currently received
|
|
|
|
PRUint8 colorNum = colorBytes / bytesPerColor;
|
|
|
|
PRUint8 at = colorBytes % bytesPerColor;
|
|
|
|
while (aCount && (mPos < (mLOH + mNumColors * bytesPerColor))) {
|
2001-11-03 10:10:51 +03:00
|
|
|
switch (at) {
|
|
|
|
case 0:
|
|
|
|
mColors[colorNum].blue = *aBuffer;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
mColors[colorNum].green = *aBuffer;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mColors[colorNum].red = *aBuffer;
|
2011-11-28 00:18:17 +04:00
|
|
|
// If there is no padding byte, increment the color index
|
|
|
|
// since we're done with the current color.
|
|
|
|
if (bytesPerColor == 3)
|
|
|
|
colorNum++;
|
2001-11-03 10:10:51 +03:00
|
|
|
break;
|
|
|
|
case 3:
|
2011-11-28 00:18:17 +04:00
|
|
|
// This is a padding byte only in Windows BMPs. Increment
|
|
|
|
// the color index since we're done with the current color.
|
|
|
|
colorNum++;
|
2001-11-03 10:10:51 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mPos++; aBuffer++; aCount--;
|
2011-11-16 19:14:40 +04:00
|
|
|
at = (at + 1) % bytesPerColor;
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
2011-11-16 19:14:40 +04:00
|
|
|
}
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
2004-01-21 20:30:14 +03:00
|
|
|
else if (aCount && mBIH.compression == BI_BITFIELDS && mPos < (WIN_HEADER_LENGTH + BITFIELD_LENGTH)) {
|
|
|
|
// If compression is used, this is a windows bitmap, hence we can
|
|
|
|
// use WIN_HEADER_LENGTH instead of mLOH
|
|
|
|
PRUint32 toCopy = (WIN_HEADER_LENGTH + BITFIELD_LENGTH) - mPos;
|
2001-11-20 02:15:27 +03:00
|
|
|
if (toCopy > aCount)
|
|
|
|
toCopy = aCount;
|
2004-01-21 20:30:14 +03:00
|
|
|
memcpy(mRawBuf + (mPos - WIN_HEADER_LENGTH), aBuffer, toCopy);
|
2001-11-20 02:15:27 +03:00
|
|
|
mPos += toCopy;
|
|
|
|
aBuffer += toCopy;
|
|
|
|
aCount -= toCopy;
|
|
|
|
}
|
2004-01-21 20:30:14 +03:00
|
|
|
if (mBIH.compression == BI_BITFIELDS && mPos == WIN_HEADER_LENGTH + BITFIELD_LENGTH) {
|
2001-11-20 02:15:27 +03:00
|
|
|
mBitFields.red = LITTLE_TO_NATIVE32(*(PRUint32*)mRawBuf);
|
|
|
|
mBitFields.green = LITTLE_TO_NATIVE32(*(PRUint32*)(mRawBuf + 4));
|
|
|
|
mBitFields.blue = LITTLE_TO_NATIVE32(*(PRUint32*)(mRawBuf + 8));
|
2002-11-10 02:06:25 +03:00
|
|
|
CalcBitShift();
|
2001-11-20 02:15:27 +03:00
|
|
|
}
|
2001-11-03 10:10:51 +03:00
|
|
|
while (aCount && (mPos < mBFH.dataoffset)) { // Skip whatever is between header and data
|
|
|
|
mPos++; aBuffer++; aCount--;
|
|
|
|
}
|
2003-05-17 20:51:19 +04:00
|
|
|
if (aCount && ++mPos >= mBFH.dataoffset) {
|
2001-11-20 02:15:27 +03:00
|
|
|
// Need to increment mPos, else we might get to mPos==mLOH again
|
|
|
|
// From now on, mPos is irrelevant
|
|
|
|
if (!mBIH.compression || mBIH.compression == BI_BITFIELDS) {
|
2011-08-26 00:09:01 +04:00
|
|
|
PRUint32 rowSize = (mBIH.bpp * mBIH.width + 7) / 8; // + 7 to round up
|
|
|
|
if (rowSize % 4) {
|
2001-11-03 10:10:51 +03:00
|
|
|
rowSize += (4 - (rowSize % 4)); // Pad to DWORD Boundary
|
2011-08-26 00:09:01 +04:00
|
|
|
}
|
2001-11-03 10:10:51 +03:00
|
|
|
PRUint32 toCopy;
|
|
|
|
do {
|
|
|
|
toCopy = rowSize - mRowBytes;
|
|
|
|
if (toCopy) {
|
|
|
|
if (toCopy > aCount)
|
|
|
|
toCopy = aCount;
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(mRow + mRowBytes, aBuffer, toCopy);
|
2001-11-03 10:10:51 +03:00
|
|
|
aCount -= toCopy;
|
|
|
|
aBuffer += toCopy;
|
|
|
|
mRowBytes += toCopy;
|
|
|
|
}
|
2007-07-18 01:08:46 +04:00
|
|
|
if (rowSize == mRowBytes) {
|
|
|
|
// Collected a whole row into mRow, process it
|
2001-11-03 10:10:51 +03:00
|
|
|
PRUint8* p = mRow;
|
2007-07-18 01:08:46 +04:00
|
|
|
PRUint32* d = mImageData + PIXEL_OFFSET(mCurLine, 0);
|
2002-12-11 17:06:51 +03:00
|
|
|
PRUint32 lpos = mBIH.width;
|
2001-11-03 10:10:51 +03:00
|
|
|
switch (mBIH.bpp) {
|
|
|
|
case 1:
|
2002-12-11 17:06:51 +03:00
|
|
|
while (lpos > 0) {
|
2001-11-03 10:10:51 +03:00
|
|
|
PRInt8 bit;
|
|
|
|
PRUint8 idx;
|
2002-12-11 17:06:51 +03:00
|
|
|
for (bit = 7; bit >= 0 && lpos > 0; bit--) {
|
2001-11-03 10:10:51 +03:00
|
|
|
idx = (*p >> bit) & 1;
|
2002-09-17 01:49:41 +04:00
|
|
|
SetPixel(d, idx, mColors);
|
2002-12-11 17:06:51 +03:00
|
|
|
--lpos;
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 4:
|
2002-12-11 17:06:51 +03:00
|
|
|
while (lpos > 0) {
|
|
|
|
Set4BitPixel(d, *p, lpos, mColors);
|
2001-11-03 10:10:51 +03:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
2002-12-11 17:06:51 +03:00
|
|
|
while (lpos > 0) {
|
2002-09-17 01:49:41 +04:00
|
|
|
SetPixel(d, *p, mColors);
|
2002-12-11 17:06:51 +03:00
|
|
|
--lpos;
|
2001-11-03 10:10:51 +03:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 16:
|
2002-12-11 17:06:51 +03:00
|
|
|
while (lpos > 0) {
|
2001-11-20 02:15:27 +03:00
|
|
|
PRUint16 val = LITTLE_TO_NATIVE16(*(PRUint16*)p);
|
2001-11-03 10:10:51 +03:00
|
|
|
SetPixel(d,
|
2002-11-10 02:06:25 +03:00
|
|
|
(val & mBitFields.red) >> mBitFields.redRightShift << mBitFields.redLeftShift,
|
|
|
|
(val & mBitFields.green) >> mBitFields.greenRightShift << mBitFields.greenLeftShift,
|
|
|
|
(val & mBitFields.blue) >> mBitFields.blueRightShift << mBitFields.blueLeftShift);
|
2002-12-11 17:06:51 +03:00
|
|
|
--lpos;
|
2001-11-03 10:10:51 +03:00
|
|
|
p+=2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 24:
|
2002-12-11 17:06:51 +03:00
|
|
|
while (lpos > 0) {
|
2001-11-03 10:10:51 +03:00
|
|
|
SetPixel(d, p[2], p[1], p[0]);
|
|
|
|
p += 2;
|
2002-12-11 17:06:51 +03:00
|
|
|
--lpos;
|
2001-11-03 10:10:51 +03:00
|
|
|
++p;
|
|
|
|
}
|
|
|
|
break;
|
2011-08-26 00:09:01 +04:00
|
|
|
case 32:
|
|
|
|
while (lpos > 0) {
|
|
|
|
if (mUseAlphaData) {
|
2011-09-09 19:24:23 +04:00
|
|
|
if (!mHaveAlphaData && p[3]) {
|
|
|
|
// Non-zero alpha byte detected! Clear previous
|
|
|
|
// pixels that we have already processed.
|
|
|
|
// This works because we know that if we
|
|
|
|
// are reaching here then the alpha data in byte
|
|
|
|
// 4 has been right all along. And we know it
|
|
|
|
// has been set to 0 the whole time, so that
|
|
|
|
// means that everything is transparent so far.
|
|
|
|
memset(mImageData + (mCurLine - 1) * GetWidth(), 0,
|
|
|
|
(GetHeight() - mCurLine + 1) *
|
|
|
|
GetWidth() * sizeof(PRUint32));
|
2011-10-17 18:59:28 +04:00
|
|
|
mHaveAlphaData = true;
|
2011-09-09 19:24:23 +04:00
|
|
|
}
|
|
|
|
SetPixel(d, p[2], p[1], p[0], mHaveAlphaData ? p[3] : 0xFF);
|
|
|
|
} else {
|
2011-08-26 00:09:01 +04:00
|
|
|
SetPixel(d, p[2], p[1], p[0]);
|
|
|
|
}
|
|
|
|
p += 4;
|
|
|
|
--lpos;
|
|
|
|
}
|
|
|
|
break;
|
2001-11-03 10:10:51 +03:00
|
|
|
default:
|
2004-01-21 20:30:14 +03:00
|
|
|
NS_NOTREACHED("Unsupported color depth, but earlier check didn't catch it");
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
2007-07-18 01:08:46 +04:00
|
|
|
mCurLine --;
|
2002-10-02 01:52:30 +04:00
|
|
|
if (mCurLine == 0) { // Finished last line
|
2011-08-26 00:09:01 +04:00
|
|
|
break;
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
2002-12-11 17:06:51 +03:00
|
|
|
mRowBytes = 0;
|
2001-11-03 10:10:51 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
} while (aCount > 0);
|
2011-08-26 00:09:01 +04:00
|
|
|
}
|
2002-12-11 17:06:51 +03:00
|
|
|
else if ((mBIH.compression == BI_RLE8) || (mBIH.compression == BI_RLE4)) {
|
2011-08-26 00:09:01 +04:00
|
|
|
if (((mBIH.compression == BI_RLE8) && (mBIH.bpp != 8)) ||
|
|
|
|
((mBIH.compression == BI_RLE4) && (mBIH.bpp != 4) && (mBIH.bpp != 1))) {
|
|
|
|
PR_LOG(gBMPLog, PR_LOG_DEBUG, ("BMP RLE8/RLE4 compression only supports 8/4 bits per pixel\n"));
|
|
|
|
PostDataError();
|
|
|
|
return;
|
2002-12-11 17:06:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (aCount > 0) {
|
|
|
|
PRUint8 byte;
|
|
|
|
|
|
|
|
switch(mState) {
|
|
|
|
case eRLEStateInitial:
|
|
|
|
mStateData = (PRUint8)*aBuffer++;
|
|
|
|
aCount--;
|
|
|
|
|
|
|
|
mState = eRLEStateNeedSecondEscapeByte;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case eRLEStateNeedSecondEscapeByte:
|
|
|
|
byte = *aBuffer++;
|
|
|
|
aCount--;
|
|
|
|
if (mStateData != RLE_ESCAPE) { // encoded mode
|
|
|
|
// Encoded mode consists of two bytes:
|
|
|
|
// the first byte (mStateData) specifies the
|
|
|
|
// number of consecutive pixels to be drawn
|
|
|
|
// using the color index contained in
|
|
|
|
// the second byte
|
|
|
|
// Work around bitmaps that specify too many pixels
|
2007-07-18 01:08:46 +04:00
|
|
|
mState = eRLEStateInitial;
|
2011-06-02 16:56:50 +04:00
|
|
|
PRUint32 pixelsNeeded = NS_MIN<PRUint32>(mBIH.width - mCurPos, mStateData);
|
2007-07-18 01:08:46 +04:00
|
|
|
if (pixelsNeeded) {
|
|
|
|
PRUint32* d = mImageData + PIXEL_OFFSET(mCurLine, mCurPos);
|
|
|
|
mCurPos += pixelsNeeded;
|
|
|
|
if (mBIH.compression == BI_RLE8) {
|
|
|
|
do {
|
|
|
|
SetPixel(d, byte, mColors);
|
|
|
|
pixelsNeeded --;
|
|
|
|
} while (pixelsNeeded);
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
Set4BitPixel(d, byte, pixelsNeeded, mColors);
|
|
|
|
} while (pixelsNeeded);
|
2002-12-11 17:06:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(byte) {
|
|
|
|
case RLE_ESCAPE_EOL:
|
2007-07-18 01:08:46 +04:00
|
|
|
// End of Line: Go to next row
|
|
|
|
mCurLine --;
|
|
|
|
mCurPos = 0;
|
2002-12-11 17:06:51 +03:00
|
|
|
mState = eRLEStateInitial;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RLE_ESCAPE_EOF: // EndOfFile
|
2007-07-18 01:08:46 +04:00
|
|
|
mCurPos = mCurLine = 0;
|
2002-12-11 17:06:51 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RLE_ESCAPE_DELTA:
|
|
|
|
mState = eRLEStateNeedXDelta;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default : // absolute mode
|
|
|
|
// Save the number of pixels to read
|
|
|
|
mStateData = byte;
|
2007-07-18 01:08:46 +04:00
|
|
|
if (mCurPos + mStateData > (PRUint32)mBIH.width) {
|
2002-12-11 17:06:51 +03:00
|
|
|
// We can work around bitmaps that specify one
|
|
|
|
// pixel too many, but only if their width is odd.
|
|
|
|
mStateData -= mBIH.width & 1;
|
2009-10-16 06:54:44 +04:00
|
|
|
if (mCurPos + mStateData > (PRUint32)mBIH.width) {
|
2010-09-12 19:22:27 +04:00
|
|
|
PostDataError();
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2009-10-16 06:54:44 +04:00
|
|
|
}
|
2002-12-11 17:06:51 +03:00
|
|
|
}
|
|
|
|
|
2003-05-23 12:38:07 +04:00
|
|
|
// See if we will need to skip a byte
|
|
|
|
// to word align the pixel data
|
|
|
|
// mStateData is a number of pixels
|
|
|
|
// so allow for the RLE compression type
|
|
|
|
// Pixels RLE8=1 RLE4=2
|
|
|
|
// 1 Pad Pad
|
|
|
|
// 2 No Pad
|
|
|
|
// 3 Pad No
|
|
|
|
// 4 No No
|
|
|
|
if (((mStateData - 1) & mBIH.compression) != 0)
|
2003-05-17 20:51:19 +04:00
|
|
|
mState = eRLEStateAbsoluteMode;
|
|
|
|
else
|
|
|
|
mState = eRLEStateAbsoluteModePadded;
|
2002-12-11 17:06:51 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case eRLEStateNeedXDelta:
|
|
|
|
// Handle the XDelta and proceed to get Y Delta
|
|
|
|
byte = *aBuffer++;
|
|
|
|
aCount--;
|
2007-07-18 01:08:46 +04:00
|
|
|
mCurPos += byte;
|
|
|
|
if (mCurPos > mBIH.width)
|
|
|
|
mCurPos = mBIH.width;
|
2002-12-11 17:06:51 +03:00
|
|
|
|
|
|
|
mState = eRLEStateNeedYDelta;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case eRLEStateNeedYDelta:
|
|
|
|
// Get the Y Delta and then "handle" the move
|
|
|
|
byte = *aBuffer++;
|
|
|
|
aCount--;
|
|
|
|
mState = eRLEStateInitial;
|
2011-06-02 16:56:50 +04:00
|
|
|
mCurLine -= NS_MIN<PRInt32>(byte, mCurLine);
|
2002-12-11 17:06:51 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case eRLEStateAbsoluteMode: // Absolute Mode
|
2003-05-17 20:51:19 +04:00
|
|
|
case eRLEStateAbsoluteModePadded:
|
2007-07-18 01:08:46 +04:00
|
|
|
if (mStateData) {
|
|
|
|
// In absolute mode, the second byte (mStateData)
|
|
|
|
// represents the number of pixels
|
|
|
|
// that follow, each of which contains
|
|
|
|
// the color index of a single pixel.
|
|
|
|
PRUint32* d = mImageData + PIXEL_OFFSET(mCurLine, mCurPos);
|
|
|
|
PRUint32* oldPos = d;
|
|
|
|
if (mBIH.compression == BI_RLE8) {
|
|
|
|
while (aCount > 0 && mStateData > 0) {
|
|
|
|
byte = *aBuffer++;
|
|
|
|
aCount--;
|
|
|
|
SetPixel(d, byte, mColors);
|
|
|
|
mStateData--;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (aCount > 0 && mStateData > 0) {
|
|
|
|
byte = *aBuffer++;
|
|
|
|
aCount--;
|
|
|
|
Set4BitPixel(d, byte, mStateData, mColors);
|
|
|
|
}
|
2002-12-11 17:06:51 +03:00
|
|
|
}
|
2007-07-18 01:08:46 +04:00
|
|
|
mCurPos += d - oldPos;
|
2002-12-11 17:06:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mStateData == 0) {
|
|
|
|
// In absolute mode, each run must
|
|
|
|
// be aligned on a word boundary
|
|
|
|
|
2003-05-17 20:51:19 +04:00
|
|
|
if (mState == eRLEStateAbsoluteMode) { // Word Aligned
|
2002-12-11 17:06:51 +03:00
|
|
|
mState = eRLEStateInitial;
|
2003-05-17 20:51:19 +04:00
|
|
|
} else if (aCount > 0) { // Not word Aligned
|
2002-12-11 17:06:51 +03:00
|
|
|
// "next" byte is just a padding byte
|
|
|
|
// so "move" past it and we can continue
|
|
|
|
aBuffer++;
|
|
|
|
aCount--;
|
|
|
|
mState = eRLEStateInitial;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// else state is still eRLEStateAbsoluteMode
|
|
|
|
continue;
|
|
|
|
|
|
|
|
default :
|
2010-09-12 19:22:27 +04:00
|
|
|
NS_ABORT_IF_FALSE(0, "BMP RLE decompression: unknown state!");
|
|
|
|
PostDecoderError(NS_ERROR_UNEXPECTED);
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2002-12-11 17:06:51 +03:00
|
|
|
}
|
|
|
|
// Because of the use of the continue statement
|
|
|
|
// we only get here for eol, eof or y delta
|
|
|
|
if (mCurLine == 0) { // Finished last line
|
2007-07-18 01:08:46 +04:00
|
|
|
break;
|
2002-12-11 17:06:51 +03:00
|
|
|
}
|
|
|
|
}
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
|
|
|
}
|
2011-08-26 00:09:01 +04:00
|
|
|
|
2007-07-18 01:08:46 +04:00
|
|
|
const PRUint32 rows = mOldLine - mCurLine;
|
|
|
|
if (rows) {
|
2010-08-25 01:12:04 +04:00
|
|
|
|
|
|
|
// Invalidate
|
2007-11-09 15:47:50 +03:00
|
|
|
nsIntRect r(0, mBIH.height < 0 ? -mBIH.height - mOldLine : mCurLine,
|
|
|
|
mBIH.width, rows);
|
2010-08-25 01:12:04 +04:00
|
|
|
PostInvalidation(r);
|
2007-07-18 01:08:46 +04:00
|
|
|
|
|
|
|
mOldLine = mCurLine;
|
|
|
|
}
|
|
|
|
|
2010-09-12 19:22:30 +04:00
|
|
|
return;
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsBMPDecoder::ProcessFileHeader()
|
|
|
|
{
|
2002-02-05 04:41:13 +03:00
|
|
|
memset(&mBFH, 0, sizeof(mBFH));
|
2002-09-15 23:51:37 +04:00
|
|
|
memcpy(&mBFH.signature, mRawBuf, sizeof(mBFH.signature));
|
|
|
|
memcpy(&mBFH.filesize, mRawBuf + 2, sizeof(mBFH.filesize));
|
|
|
|
memcpy(&mBFH.reserved, mRawBuf + 6, sizeof(mBFH.reserved));
|
|
|
|
memcpy(&mBFH.dataoffset, mRawBuf + 10, sizeof(mBFH.dataoffset));
|
|
|
|
memcpy(&mBFH.bihsize, mRawBuf + 14, sizeof(mBFH.bihsize));
|
2001-11-03 10:10:51 +03:00
|
|
|
|
|
|
|
// Now correct the endianness of the header
|
|
|
|
mBFH.filesize = LITTLE_TO_NATIVE32(mBFH.filesize);
|
|
|
|
mBFH.dataoffset = LITTLE_TO_NATIVE32(mBFH.dataoffset);
|
|
|
|
mBFH.bihsize = LITTLE_TO_NATIVE32(mBFH.bihsize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsBMPDecoder::ProcessInfoHeader()
|
|
|
|
{
|
2002-02-05 04:41:13 +03:00
|
|
|
memset(&mBIH, 0, sizeof(mBIH));
|
2001-11-03 10:10:51 +03:00
|
|
|
if (mBFH.bihsize == 12) { // OS/2 Bitmap
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&mBIH.width, mRawBuf, 2);
|
|
|
|
memcpy(&mBIH.height, mRawBuf + 2, 2);
|
2002-09-15 23:51:37 +04:00
|
|
|
memcpy(&mBIH.planes, mRawBuf + 4, sizeof(mBIH.planes));
|
|
|
|
memcpy(&mBIH.bpp, mRawBuf + 6, sizeof(mBIH.bpp));
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
|
|
|
else {
|
2002-09-15 23:51:37 +04:00
|
|
|
memcpy(&mBIH.width, mRawBuf, sizeof(mBIH.width));
|
|
|
|
memcpy(&mBIH.height, mRawBuf + 4, sizeof(mBIH.height));
|
|
|
|
memcpy(&mBIH.planes, mRawBuf + 8, sizeof(mBIH.planes));
|
|
|
|
memcpy(&mBIH.bpp, mRawBuf + 10, sizeof(mBIH.bpp));
|
|
|
|
memcpy(&mBIH.compression, mRawBuf + 12, sizeof(mBIH.compression));
|
|
|
|
memcpy(&mBIH.image_size, mRawBuf + 16, sizeof(mBIH.image_size));
|
|
|
|
memcpy(&mBIH.xppm, mRawBuf + 20, sizeof(mBIH.xppm));
|
|
|
|
memcpy(&mBIH.yppm, mRawBuf + 24, sizeof(mBIH.yppm));
|
|
|
|
memcpy(&mBIH.colors, mRawBuf + 28, sizeof(mBIH.colors));
|
|
|
|
memcpy(&mBIH.important_colors, mRawBuf + 32, sizeof(mBIH.important_colors));
|
2001-11-03 10:10:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert endianness
|
|
|
|
mBIH.width = LITTLE_TO_NATIVE32(mBIH.width);
|
|
|
|
mBIH.height = LITTLE_TO_NATIVE32(mBIH.height);
|
|
|
|
mBIH.planes = LITTLE_TO_NATIVE16(mBIH.planes);
|
|
|
|
mBIH.bpp = LITTLE_TO_NATIVE16(mBIH.bpp);
|
|
|
|
|
|
|
|
mBIH.compression = LITTLE_TO_NATIVE32(mBIH.compression);
|
|
|
|
mBIH.image_size = LITTLE_TO_NATIVE32(mBIH.image_size);
|
|
|
|
mBIH.xppm = LITTLE_TO_NATIVE32(mBIH.xppm);
|
|
|
|
mBIH.yppm = LITTLE_TO_NATIVE32(mBIH.yppm);
|
|
|
|
mBIH.colors = LITTLE_TO_NATIVE32(mBIH.colors);
|
|
|
|
mBIH.important_colors = LITTLE_TO_NATIVE32(mBIH.important_colors);
|
|
|
|
}
|
2010-08-23 06:30:46 +04:00
|
|
|
|
|
|
|
} // namespace imagelib
|
|
|
|
} // namespace mozilla
|