|
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/. */ |
|
5 |
|
6 |
|
7 #ifndef _nsBMPDecoder_h |
|
8 #define _nsBMPDecoder_h |
|
9 |
|
10 #include "nsAutoPtr.h" |
|
11 #include "gfxColor.h" |
|
12 #include "Decoder.h" |
|
13 #include "BMPFileHeaders.h" |
|
14 |
|
15 namespace mozilla { |
|
16 namespace image { |
|
17 |
|
18 class RasterImage; |
|
19 |
|
20 /** |
|
21 * Decoder for BMP-Files, as used by Windows and OS/2 |
|
22 */ |
|
23 class nsBMPDecoder : public Decoder |
|
24 { |
|
25 public: |
|
26 |
|
27 nsBMPDecoder(RasterImage &aImage); |
|
28 ~nsBMPDecoder(); |
|
29 |
|
30 // Specifies whether or not the BMP file will contain alpha data |
|
31 // If set to true and the BMP is 32BPP, the alpha data will be |
|
32 // retrieved from the 4th byte of image data per pixel |
|
33 void SetUseAlphaData(bool useAlphaData); |
|
34 // Obtains the bits per pixel from the internal BIH header |
|
35 int32_t GetBitsPerPixel() const; |
|
36 // Obtains the width from the internal BIH header |
|
37 int32_t GetWidth() const; |
|
38 // Obtains the abs-value of the height from the internal BIH header |
|
39 int32_t GetHeight() const; |
|
40 // Obtains the internal output image buffer |
|
41 uint32_t* GetImageData(); |
|
42 // Obtains the size of the compressed image resource |
|
43 int32_t GetCompressedImageSize() const; |
|
44 // Obtains whether or not a BMP file had alpha data in its 4th byte |
|
45 // for 32BPP bitmaps. Only use after the bitmap has been processed. |
|
46 bool HasAlphaData() const; |
|
47 |
|
48 virtual void WriteInternal(const char* aBuffer, uint32_t aCount, DecodeStrategy aStrategy); |
|
49 virtual void FinishInternal(); |
|
50 |
|
51 private: |
|
52 |
|
53 /** Calculates the red-, green- and blueshift in mBitFields using |
|
54 * the bitmasks from mBitFields */ |
|
55 NS_METHOD CalcBitShift(); |
|
56 |
|
57 uint32_t mPos; |
|
58 |
|
59 BMPFILEHEADER mBFH; |
|
60 BITMAPV5HEADER mBIH; |
|
61 char mRawBuf[WIN_V3_INTERNAL_BIH_LENGTH]; |
|
62 |
|
63 uint32_t mLOH; ///< Length of the header |
|
64 |
|
65 uint32_t mNumColors; ///< The number of used colors, i.e. the number of entries in mColors |
|
66 colorTable *mColors; |
|
67 |
|
68 bitFields mBitFields; |
|
69 |
|
70 uint8_t *mRow; ///< Holds one raw line of the image |
|
71 uint32_t mRowBytes; ///< How many bytes of the row were already received |
|
72 int32_t mCurLine; ///< Index of the line of the image that's currently being decoded: [height,1] |
|
73 int32_t mOldLine; ///< Previous index of the line |
|
74 int32_t mCurPos; ///< Index in the current line of the image |
|
75 |
|
76 ERLEState mState; ///< Maintains the current state of the RLE decoding |
|
77 uint32_t mStateData;///< Decoding information that is needed depending on mState |
|
78 |
|
79 /** Set mBFH from the raw data in mRawBuf, converting from little-endian |
|
80 * data to native data as necessary */ |
|
81 void ProcessFileHeader(); |
|
82 /** Set mBIH from the raw data in mRawBuf, converting from little-endian |
|
83 * data to native data as necessary */ |
|
84 void ProcessInfoHeader(); |
|
85 |
|
86 // Stores whether the image data may store alpha data, or if |
|
87 // the alpha data is unspecified and filled with a padding byte of 0. |
|
88 // When a 32BPP bitmap is stored in an ICO or CUR file, its 4th byte |
|
89 // is used for alpha transparency. When it is stored in a BMP, its |
|
90 // 4th byte is reserved and is always 0. |
|
91 // Reference: |
|
92 // http://en.wikipedia.org/wiki/ICO_(file_format)#cite_note-9 |
|
93 // Bitmaps where the alpha bytes are all 0 should be fully visible. |
|
94 bool mUseAlphaData; |
|
95 // Whether the 4th byte alpha data was found to be non zero and hence used. |
|
96 bool mHaveAlphaData; |
|
97 }; |
|
98 |
|
99 /** Sets the pixel data in aDecoded to the given values. |
|
100 * @param aDecoded pointer to pixel to be set, will be incremented to point to the next pixel. |
|
101 */ |
|
102 static inline void SetPixel(uint32_t*& aDecoded, uint8_t aRed, uint8_t aGreen, uint8_t aBlue, uint8_t aAlpha = 0xFF) |
|
103 { |
|
104 *aDecoded++ = gfxPackedPixel(aAlpha, aRed, aGreen, aBlue); |
|
105 } |
|
106 |
|
107 static inline void SetPixel(uint32_t*& aDecoded, uint8_t idx, colorTable* aColors) |
|
108 { |
|
109 SetPixel(aDecoded, aColors[idx].red, aColors[idx].green, aColors[idx].blue); |
|
110 } |
|
111 |
|
112 /** Sets two (or one if aCount = 1) pixels |
|
113 * @param aDecoded where the data is stored. Will be moved 4 resp 8 bytes |
|
114 * depending on whether one or two pixels are written. |
|
115 * @param aData The values for the two pixels |
|
116 * @param aCount Current count. Is decremented by one or two. |
|
117 */ |
|
118 inline void Set4BitPixel(uint32_t*& aDecoded, uint8_t aData, |
|
119 uint32_t& aCount, colorTable* aColors) |
|
120 { |
|
121 uint8_t idx = aData >> 4; |
|
122 SetPixel(aDecoded, idx, aColors); |
|
123 if (--aCount > 0) { |
|
124 idx = aData & 0xF; |
|
125 SetPixel(aDecoded, idx, aColors); |
|
126 --aCount; |
|
127 } |
|
128 } |
|
129 |
|
130 } // namespace image |
|
131 } // namespace mozilla |
|
132 |
|
133 |
|
134 #endif |
|
135 |