1 : /* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4: */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is the Mozilla BMP Decoder.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Christian Biesinger <cbiesinger@web.de>.
19 : * Portions created by the Initial Developer are Copyright (C) 2001
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Bobby Holley <bobbyholley@gmail.com>
24 : * Brian R. Bondy <netzen@gmail.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 :
41 : #ifndef _nsBMPDecoder_h
42 : #define _nsBMPDecoder_h
43 :
44 : #include "nsAutoPtr.h"
45 : #include "imgIDecoderObserver.h"
46 : #include "gfxColor.h"
47 : #include "Decoder.h"
48 : #include "BMPFileHeaders.h"
49 :
50 : namespace mozilla {
51 : namespace image {
52 :
53 : class RasterImage;
54 :
55 : /**
56 : * Decoder for BMP-Files, as used by Windows and OS/2
57 : */
58 : class nsBMPDecoder : public Decoder
59 : {
60 : public:
61 :
62 : nsBMPDecoder(RasterImage &aImage, imgIDecoderObserver* aObserver);
63 : ~nsBMPDecoder();
64 :
65 : // Specifies whether or not the BMP file will contain alpha data
66 : // If set to true and the BMP is 32BPP, the alpha data will be
67 : // retrieved from the 4th byte of image data per pixel
68 : void SetUseAlphaData(bool useAlphaData);
69 : // Obtains the bits per pixel from the internal BIH header
70 : PRInt32 GetBitsPerPixel() const;
71 : // Obtains the width from the internal BIH header
72 : PRInt32 GetWidth() const;
73 : // Obtains the height from the internal BIH header
74 : PRInt32 GetHeight() const;
75 : // Obtains the internal output image buffer
76 : PRUint32* GetImageData();
77 : // Obtains the size of the compressed image resource
78 : PRInt32 GetCompressedImageSize() const;
79 : // Obtains whether or not a BMP file had alpha data in its 4th byte
80 : // for 32BPP bitmaps. Only use after the bitmap has been processed.
81 : bool HasAlphaData() const;
82 :
83 : virtual void WriteInternal(const char* aBuffer, PRUint32 aCount);
84 : virtual void FinishInternal();
85 :
86 : private:
87 :
88 : /** Calculates the red-, green- and blueshift in mBitFields using
89 : * the bitmasks from mBitFields */
90 : NS_METHOD CalcBitShift();
91 :
92 : PRUint32 mPos;
93 :
94 : BMPFILEHEADER mBFH;
95 : BMPINFOHEADER mBIH;
96 : char mRawBuf[36];
97 :
98 : PRUint32 mLOH; ///< Length of the header
99 :
100 : PRUint32 mNumColors; ///< The number of used colors, i.e. the number of entries in mColors
101 : colorTable *mColors;
102 :
103 : bitFields mBitFields;
104 :
105 : PRUint32 *mImageData; ///< Pointer to the image data for the frame
106 : PRUint8 *mRow; ///< Holds one raw line of the image
107 : PRUint32 mRowBytes; ///< How many bytes of the row were already received
108 : PRInt32 mCurLine; ///< Index of the line of the image that's currently being decoded
109 : PRInt32 mOldLine; ///< Previous index of the line
110 : PRInt32 mCurPos; ///< Index in the current line of the image
111 :
112 : ERLEState mState; ///< Maintains the current state of the RLE decoding
113 : PRUint32 mStateData;///< Decoding information that is needed depending on mState
114 :
115 : /** Set mBFH from the raw data in mRawBuf, converting from little-endian
116 : * data to native data as necessary */
117 : void ProcessFileHeader();
118 : /** Set mBIH from the raw data in mRawBuf, converting from little-endian
119 : * data to native data as necessary */
120 : void ProcessInfoHeader();
121 :
122 : // Stores whether the image data may store alpha data, or if
123 : // the alpha data is unspecified and filled with a padding byte of 0.
124 : // When a 32BPP bitmap is stored in an ICO or CUR file, its 4th byte
125 : // is used for alpha transparency. When it is stored in a BMP, its
126 : // 4th byte is reserved and is always 0.
127 : // Reference:
128 : // http://en.wikipedia.org/wiki/ICO_(file_format)#cite_note-9
129 : // Bitmaps where the alpha bytes are all 0 should be fully visible.
130 : bool mUseAlphaData;
131 : // Whether the 4th byte alpha data was found to be non zero and hence used.
132 : bool mHaveAlphaData;
133 : };
134 :
135 : /** Sets the pixel data in aDecoded to the given values.
136 : * @param aDecoded pointer to pixel to be set, will be incremented to point to the next pixel.
137 : */
138 1024 : static inline void SetPixel(PRUint32*& aDecoded, PRUint8 aRed, PRUint8 aGreen, PRUint8 aBlue, PRUint8 aAlpha = 0xFF)
139 : {
140 1024 : *aDecoded++ = GFX_PACKED_PIXEL(aAlpha, aRed, aGreen, aBlue);
141 1024 : }
142 :
143 768 : static inline void SetPixel(PRUint32*& aDecoded, PRUint8 idx, colorTable* aColors)
144 : {
145 768 : SetPixel(aDecoded, aColors[idx].red, aColors[idx].green, aColors[idx].blue);
146 768 : }
147 :
148 : /** Sets two (or one if aCount = 1) pixels
149 : * @param aDecoded where the data is stored. Will be moved 4 resp 8 bytes
150 : * depending on whether one or two pixels are written.
151 : * @param aData The values for the two pixels
152 : * @param aCount Current count. Is decremented by one or two.
153 : */
154 0 : inline void Set4BitPixel(PRUint32*& aDecoded, PRUint8 aData,
155 : PRUint32& aCount, colorTable* aColors)
156 : {
157 0 : PRUint8 idx = aData >> 4;
158 0 : SetPixel(aDecoded, idx, aColors);
159 0 : if (--aCount > 0) {
160 0 : idx = aData & 0xF;
161 0 : SetPixel(aDecoded, idx, aColors);
162 0 : --aCount;
163 : }
164 0 : }
165 :
166 : } // namespace image
167 : } // namespace mozilla
168 :
169 :
170 : #endif
171 :
|