michael@0: /* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: michael@0: #ifndef _nsBMPDecoder_h michael@0: #define _nsBMPDecoder_h michael@0: michael@0: #include "nsAutoPtr.h" michael@0: #include "gfxColor.h" michael@0: #include "Decoder.h" michael@0: #include "BMPFileHeaders.h" michael@0: michael@0: namespace mozilla { michael@0: namespace image { michael@0: michael@0: class RasterImage; michael@0: michael@0: /** michael@0: * Decoder for BMP-Files, as used by Windows and OS/2 michael@0: */ michael@0: class nsBMPDecoder : public Decoder michael@0: { michael@0: public: michael@0: michael@0: nsBMPDecoder(RasterImage &aImage); michael@0: ~nsBMPDecoder(); michael@0: michael@0: // Specifies whether or not the BMP file will contain alpha data michael@0: // If set to true and the BMP is 32BPP, the alpha data will be michael@0: // retrieved from the 4th byte of image data per pixel michael@0: void SetUseAlphaData(bool useAlphaData); michael@0: // Obtains the bits per pixel from the internal BIH header michael@0: int32_t GetBitsPerPixel() const; michael@0: // Obtains the width from the internal BIH header michael@0: int32_t GetWidth() const; michael@0: // Obtains the abs-value of the height from the internal BIH header michael@0: int32_t GetHeight() const; michael@0: // Obtains the internal output image buffer michael@0: uint32_t* GetImageData(); michael@0: // Obtains the size of the compressed image resource michael@0: int32_t GetCompressedImageSize() const; michael@0: // Obtains whether or not a BMP file had alpha data in its 4th byte michael@0: // for 32BPP bitmaps. Only use after the bitmap has been processed. michael@0: bool HasAlphaData() const; michael@0: michael@0: virtual void WriteInternal(const char* aBuffer, uint32_t aCount, DecodeStrategy aStrategy); michael@0: virtual void FinishInternal(); michael@0: michael@0: private: michael@0: michael@0: /** Calculates the red-, green- and blueshift in mBitFields using michael@0: * the bitmasks from mBitFields */ michael@0: NS_METHOD CalcBitShift(); michael@0: michael@0: uint32_t mPos; michael@0: michael@0: BMPFILEHEADER mBFH; michael@0: BITMAPV5HEADER mBIH; michael@0: char mRawBuf[WIN_V3_INTERNAL_BIH_LENGTH]; michael@0: michael@0: uint32_t mLOH; ///< Length of the header michael@0: michael@0: uint32_t mNumColors; ///< The number of used colors, i.e. the number of entries in mColors michael@0: colorTable *mColors; michael@0: michael@0: bitFields mBitFields; michael@0: michael@0: uint8_t *mRow; ///< Holds one raw line of the image michael@0: uint32_t mRowBytes; ///< How many bytes of the row were already received michael@0: int32_t mCurLine; ///< Index of the line of the image that's currently being decoded: [height,1] michael@0: int32_t mOldLine; ///< Previous index of the line michael@0: int32_t mCurPos; ///< Index in the current line of the image michael@0: michael@0: ERLEState mState; ///< Maintains the current state of the RLE decoding michael@0: uint32_t mStateData;///< Decoding information that is needed depending on mState michael@0: michael@0: /** Set mBFH from the raw data in mRawBuf, converting from little-endian michael@0: * data to native data as necessary */ michael@0: void ProcessFileHeader(); michael@0: /** Set mBIH from the raw data in mRawBuf, converting from little-endian michael@0: * data to native data as necessary */ michael@0: void ProcessInfoHeader(); michael@0: michael@0: // Stores whether the image data may store alpha data, or if michael@0: // the alpha data is unspecified and filled with a padding byte of 0. michael@0: // When a 32BPP bitmap is stored in an ICO or CUR file, its 4th byte michael@0: // is used for alpha transparency. When it is stored in a BMP, its michael@0: // 4th byte is reserved and is always 0. michael@0: // Reference: michael@0: // http://en.wikipedia.org/wiki/ICO_(file_format)#cite_note-9 michael@0: // Bitmaps where the alpha bytes are all 0 should be fully visible. michael@0: bool mUseAlphaData; michael@0: // Whether the 4th byte alpha data was found to be non zero and hence used. michael@0: bool mHaveAlphaData; michael@0: }; michael@0: michael@0: /** Sets the pixel data in aDecoded to the given values. michael@0: * @param aDecoded pointer to pixel to be set, will be incremented to point to the next pixel. michael@0: */ michael@0: static inline void SetPixel(uint32_t*& aDecoded, uint8_t aRed, uint8_t aGreen, uint8_t aBlue, uint8_t aAlpha = 0xFF) michael@0: { michael@0: *aDecoded++ = gfxPackedPixel(aAlpha, aRed, aGreen, aBlue); michael@0: } michael@0: michael@0: static inline void SetPixel(uint32_t*& aDecoded, uint8_t idx, colorTable* aColors) michael@0: { michael@0: SetPixel(aDecoded, aColors[idx].red, aColors[idx].green, aColors[idx].blue); michael@0: } michael@0: michael@0: /** Sets two (or one if aCount = 1) pixels michael@0: * @param aDecoded where the data is stored. Will be moved 4 resp 8 bytes michael@0: * depending on whether one or two pixels are written. michael@0: * @param aData The values for the two pixels michael@0: * @param aCount Current count. Is decremented by one or two. michael@0: */ michael@0: inline void Set4BitPixel(uint32_t*& aDecoded, uint8_t aData, michael@0: uint32_t& aCount, colorTable* aColors) michael@0: { michael@0: uint8_t idx = aData >> 4; michael@0: SetPixel(aDecoded, idx, aColors); michael@0: if (--aCount > 0) { michael@0: idx = aData & 0xF; michael@0: SetPixel(aDecoded, idx, aColors); michael@0: --aCount; michael@0: } michael@0: } michael@0: michael@0: } // namespace image michael@0: } // namespace mozilla michael@0: michael@0: michael@0: #endif michael@0: