1 /* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4: */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 #include "BMPFileHeaders.h"
13 #include "nsAutoPtr.h"
20 /// Decoder for BMP-Files, as used by Windows and OS/2
22 class nsBMPDecoder
: public Decoder
26 explicit nsBMPDecoder(RasterImage
* aImage
);
29 // Specifies whether or not the BMP file will contain alpha data
30 // If set to true and the BMP is 32BPP, the alpha data will be
31 // retrieved from the 4th byte of image data per pixel
32 void SetUseAlphaData(bool useAlphaData
);
34 // Obtains the bits per pixel from the internal BIH header
35 int32_t GetBitsPerPixel() const;
37 // Obtains the width from the internal BIH header
38 int32_t GetWidth() const;
40 // Obtains the abs-value of the height from the internal BIH header
41 int32_t GetHeight() const;
43 // Obtains the internal output image buffer
44 uint32_t* GetImageData();
46 // Obtains the size of the compressed image resource
47 int32_t GetCompressedImageSize() const;
49 // Obtains whether or not a BMP file had alpha data in its 4th byte
50 // for 32BPP bitmaps. Only use after the bitmap has been processed.
51 bool HasAlphaData() const;
53 virtual void WriteInternal(const char* aBuffer
,
54 uint32_t aCount
) MOZ_OVERRIDE
;
55 virtual void FinishInternal() MOZ_OVERRIDE
;
59 /// Calculates the red-, green- and blueshift in mBitFields using
60 /// the bitmasks from mBitFields
61 NS_METHOD
CalcBitShift();
63 uint32_t mPos
; //< Number of bytes read from aBuffer in WriteInternal()
67 char mRawBuf
[WIN_V3_INTERNAL_BIH_LENGTH
]; //< If this is changed,
68 // WriteInternal() MUST be updated
70 uint32_t mLOH
; //< Length of the header
72 uint32_t mNumColors
; //< The number of used colors, i.e. the number of
78 uint8_t* mRow
; //< Holds one raw line of the image
79 uint32_t mRowBytes
; //< How many bytes of the row were already received
80 int32_t mCurLine
; //< Index of the line of the image that's currently
81 // being decoded: [height,1]
82 int32_t mOldLine
; //< Previous index of the line
83 int32_t mCurPos
; //< Index in the current line of the image
85 ERLEState mState
; //< Maintains the current state of the RLE decoding
86 uint32_t mStateData
;//< Decoding information that is needed depending
89 /// Set mBFH from the raw data in mRawBuf, converting from little-endian
90 /// data to native data as necessary
91 void ProcessFileHeader();
93 /// Set mBIH from the raw data in mRawBuf, converting from little-endian
94 /// data to native data as necessary
95 void ProcessInfoHeader();
97 /// True if we've already processed the BMP header.
98 bool mProcessedHeader
;
100 // Stores whether the image data may store alpha data, or if
101 // the alpha data is unspecified and filled with a padding byte of 0.
102 // When a 32BPP bitmap is stored in an ICO or CUR file, its 4th byte
103 // is used for alpha transparency. When it is stored in a BMP, its
104 // 4th byte is reserved and is always 0.
106 // http://en.wikipedia.org/wiki/ICO_(file_format)#cite_note-9
107 // Bitmaps where the alpha bytes are all 0 should be fully visible.
110 // Whether the 4th byte alpha data was found to be non zero and hence used.
114 /// Sets the pixel data in aDecoded to the given values.
115 /// @param aDecoded pointer to pixel to be set, will be incremented to point to
117 static inline void SetPixel(uint32_t*& aDecoded
, uint8_t aRed
, uint8_t aGreen
,
118 uint8_t aBlue
, uint8_t aAlpha
= 0xFF)
120 *aDecoded
++ = gfxPackedPixel(aAlpha
, aRed
, aGreen
, aBlue
);
123 static inline void SetPixel(uint32_t*& aDecoded
, uint8_t idx
, colorTable
*
126 SetPixel(aDecoded
, aColors
[idx
].red
, aColors
[idx
].green
, aColors
[idx
].blue
);
129 /// Sets two (or one if aCount = 1) pixels
130 /// @param aDecoded where the data is stored. Will be moved 4 resp 8 bytes
131 /// depending on whether one or two pixels are written.
132 /// @param aData The values for the two pixels
133 /// @param aCount Current count. Is decremented by one or two.
134 inline void Set4BitPixel(uint32_t*& aDecoded
, uint8_t aData
,
135 uint32_t& aCount
, colorTable
* aColors
)
137 uint8_t idx
= aData
>> 4;
138 SetPixel(aDecoded
, idx
, aColors
);
141 SetPixel(aDecoded
, idx
, aColors
);
147 } // namespace mozilla
149 #endif // nsBMPDecoder_h