2001-11-03 10:10:51 +03:00
|
|
|
/* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4: */
|
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
|
|
|
|
|
|
|
|
|
|
|
#ifndef _nsBMPDecoder_h
|
|
|
|
#define _nsBMPDecoder_h
|
|
|
|
|
2010-08-14 08:09:49 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2001-11-03 10:10:51 +03:00
|
|
|
#include "imgIDecoderObserver.h"
|
2007-07-18 01:08:46 +04:00
|
|
|
#include "gfxColor.h"
|
2010-08-23 06:30:46 +04:00
|
|
|
#include "Decoder.h"
|
2011-08-26 00:09:05 +04:00
|
|
|
#include "BMPFileHeaders.h"
|
2010-08-23 06:30:46 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2010-08-12 23:58:25 +04:00
|
|
|
|
2010-08-14 08:09:49 +04:00
|
|
|
class RasterImage;
|
|
|
|
|
2004-01-21 20:30:14 +03:00
|
|
|
/**
|
|
|
|
* Decoder for BMP-Files, as used by Windows and OS/2
|
|
|
|
*/
|
2010-08-23 06:30:46 +04:00
|
|
|
class nsBMPDecoder : public Decoder
|
2001-11-03 10:10:51 +03:00
|
|
|
{
|
|
|
|
public:
|
2010-08-23 06:30:46 +04:00
|
|
|
|
2011-09-29 17:17:13 +04:00
|
|
|
nsBMPDecoder(RasterImage &aImage, imgIDecoderObserver* aObserver);
|
2004-01-21 20:30:14 +03:00
|
|
|
~nsBMPDecoder();
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2011-08-26 00:09:01 +04:00
|
|
|
// Specifies whether or not the BMP file will contain alpha data
|
|
|
|
// If set to true and the BMP is 32BPP, the alpha data will be
|
|
|
|
// retrieved from the 4th byte of image data per pixel
|
2011-09-29 10:19:26 +04:00
|
|
|
void SetUseAlphaData(bool useAlphaData);
|
2011-08-26 00:09:01 +04:00
|
|
|
// Obtains the bits per pixel from the internal BIH header
|
|
|
|
PRInt32 GetBitsPerPixel() const;
|
|
|
|
// Obtains the width from the internal BIH header
|
|
|
|
PRInt32 GetWidth() const;
|
2012-07-24 00:20:57 +04:00
|
|
|
// Obtains the abs-value of the height from the internal BIH header
|
2011-08-26 00:09:01 +04:00
|
|
|
PRInt32 GetHeight() const;
|
|
|
|
// Obtains the internal output image buffer
|
|
|
|
PRUint32* GetImageData();
|
|
|
|
// Obtains the size of the compressed image resource
|
|
|
|
PRInt32 GetCompressedImageSize() const;
|
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 HasAlphaData() const;
|
2011-08-26 00:09:01 +04:00
|
|
|
|
2010-09-12 19:22:30 +04:00
|
|
|
virtual void WriteInternal(const char* aBuffer, PRUint32 aCount);
|
|
|
|
virtual void FinishInternal();
|
2010-08-23 06:30:46 +04:00
|
|
|
|
2001-11-03 10:10:51 +03:00
|
|
|
private:
|
|
|
|
|
2002-11-10 02:06:25 +03:00
|
|
|
/** Calculates the red-, green- and blueshift in mBitFields using
|
|
|
|
* the bitmasks from mBitFields */
|
|
|
|
NS_METHOD CalcBitShift();
|
|
|
|
|
2001-11-03 10:10:51 +03:00
|
|
|
PRUint32 mPos;
|
|
|
|
|
|
|
|
BMPFILEHEADER mBFH;
|
2012-07-17 18:20:14 +04:00
|
|
|
BITMAPV5HEADER mBIH;
|
|
|
|
char mRawBuf[WIN_V3_INTERNAL_BIH_LENGTH];
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2004-01-21 20:30:14 +03:00
|
|
|
PRUint32 mLOH; ///< Length of the header
|
2001-11-03 10:10:51 +03:00
|
|
|
|
2004-01-21 20:30:14 +03:00
|
|
|
PRUint32 mNumColors; ///< The number of used colors, i.e. the number of entries in mColors
|
2001-11-03 10:10:51 +03:00
|
|
|
colorTable *mColors;
|
|
|
|
|
2001-11-20 02:15:27 +03:00
|
|
|
bitFields mBitFields;
|
|
|
|
|
2007-07-18 01:08:46 +04:00
|
|
|
PRUint32 *mImageData; ///< Pointer to the image data for the frame
|
2004-01-21 20:30:14 +03:00
|
|
|
PRUint8 *mRow; ///< Holds one raw line of the image
|
|
|
|
PRUint32 mRowBytes; ///< How many bytes of the row were already received
|
2012-07-24 00:20:57 +04:00
|
|
|
PRInt32 mCurLine; ///< Index of the line of the image that's currently being decoded: [height,1]
|
2007-07-18 01:08:46 +04:00
|
|
|
PRInt32 mOldLine; ///< Previous index of the line
|
|
|
|
PRInt32 mCurPos; ///< Index in the current line of the image
|
2004-01-21 20:30:14 +03:00
|
|
|
|
|
|
|
ERLEState mState; ///< Maintains the current state of the RLE decoding
|
|
|
|
PRUint32 mStateData;///< Decoding information that is needed depending on mState
|
2002-12-11 17:06:51 +03:00
|
|
|
|
2004-01-21 20:30:14 +03:00
|
|
|
/** Set mBFH from the raw data in mRawBuf, converting from little-endian
|
|
|
|
* data to native data as necessary */
|
2001-11-03 10:10:51 +03:00
|
|
|
void ProcessFileHeader();
|
2004-01-21 20:30:14 +03:00
|
|
|
/** Set mBIH from the raw data in mRawBuf, converting from little-endian
|
|
|
|
* data to native data as necessary */
|
2001-11-03 10:10:51 +03:00
|
|
|
void ProcessInfoHeader();
|
2011-08-26 00:09:01 +04:00
|
|
|
|
2011-09-09 19:24:23 +04:00
|
|
|
// Stores whether the image data may store alpha data, or if
|
|
|
|
// the alpha data is unspecified and filled with a padding byte of 0.
|
2011-08-26 00:09:01 +04:00
|
|
|
// When a 32BPP bitmap is stored in an ICO or CUR file, its 4th byte
|
|
|
|
// is used for alpha transparency. When it is stored in a BMP, its
|
|
|
|
// 4th byte is reserved and is always 0.
|
|
|
|
// Reference:
|
|
|
|
// http://en.wikipedia.org/wiki/ICO_(file_format)#cite_note-9
|
2011-09-09 19:24:23 +04:00
|
|
|
// Bitmaps where the alpha bytes are all 0 should be fully visible.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mUseAlphaData;
|
2011-09-09 19:24:23 +04:00
|
|
|
// Whether the 4th byte alpha data was found to be non zero and hence used.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mHaveAlphaData;
|
2001-11-03 10:10:51 +03:00
|
|
|
};
|
|
|
|
|
2002-09-17 01:49:41 +04:00
|
|
|
/** Sets the pixel data in aDecoded to the given values.
|
2007-07-18 01:08:46 +04:00
|
|
|
* @param aDecoded pointer to pixel to be set, will be incremented to point to the next pixel.
|
|
|
|
*/
|
|
|
|
static inline void SetPixel(PRUint32*& aDecoded, PRUint8 aRed, PRUint8 aGreen, PRUint8 aBlue, PRUint8 aAlpha = 0xFF)
|
2002-09-17 01:49:41 +04:00
|
|
|
{
|
2007-07-18 01:08:46 +04:00
|
|
|
*aDecoded++ = GFX_PACKED_PIXEL(aAlpha, aRed, aGreen, aBlue);
|
2002-09-17 01:49:41 +04:00
|
|
|
}
|
|
|
|
|
2007-07-18 01:08:46 +04:00
|
|
|
static inline void SetPixel(PRUint32*& aDecoded, PRUint8 idx, colorTable* aColors)
|
2002-09-17 01:49:41 +04:00
|
|
|
{
|
2007-01-17 13:39:10 +03:00
|
|
|
SetPixel(aDecoded, aColors[idx].red, aColors[idx].green, aColors[idx].blue);
|
2002-09-17 01:49:41 +04:00
|
|
|
}
|
|
|
|
|
2002-12-11 17:06:51 +03:00
|
|
|
/** Sets two (or one if aCount = 1) pixels
|
2007-07-18 01:08:46 +04:00
|
|
|
* @param aDecoded where the data is stored. Will be moved 4 resp 8 bytes
|
2002-09-17 01:49:41 +04:00
|
|
|
* depending on whether one or two pixels are written.
|
|
|
|
* @param aData The values for the two pixels
|
2007-07-18 01:08:46 +04:00
|
|
|
* @param aCount Current count. Is decremented by one or two.
|
|
|
|
*/
|
|
|
|
inline void Set4BitPixel(PRUint32*& aDecoded, PRUint8 aData,
|
2002-12-11 17:06:51 +03:00
|
|
|
PRUint32& aCount, colorTable* aColors)
|
2002-09-17 01:49:41 +04:00
|
|
|
{
|
|
|
|
PRUint8 idx = aData >> 4;
|
2002-12-11 17:06:51 +03:00
|
|
|
SetPixel(aDecoded, idx, aColors);
|
|
|
|
if (--aCount > 0) {
|
|
|
|
idx = aData & 0xF;
|
|
|
|
SetPixel(aDecoded, idx, aColors);
|
|
|
|
--aCount;
|
|
|
|
}
|
2002-09-17 01:49:41 +04:00
|
|
|
}
|
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-23 06:30:46 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
|
2001-11-03 10:10:51 +03:00
|
|
|
#endif
|
2007-01-25 16:14:24 +03:00
|
|
|
|