1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_image_decoders_nsBMPDecoder_h
8 #define mozilla_image_decoders_nsBMPDecoder_h
10 #include "BMPHeaders.h"
13 #include "StreamingLexer.h"
14 #include "SurfacePipe.h"
15 #include "mozilla/UniquePtr.h"
22 struct CalRgbEndpoint
{
29 /// This struct contains the fields from the file header and info header that
30 /// we use during decoding. (Excluding bitfields fields, which are kept in
33 uint32_t mDataOffset
; // Offset to raster data.
34 uint32_t mBIHSize
; // Header size.
35 int32_t mWidth
; // Image width.
36 int32_t mHeight
; // Image height.
37 uint16_t mBpp
; // Bits per pixel.
38 uint32_t mCompression
; // See struct Compression for valid values.
39 uint32_t mImageSize
; // (compressed) image size. Can be 0 if
41 uint32_t mNumColors
; // Used colors.
42 InfoColorSpace mCsType
; // Color space type.
43 InfoColorIntent mCsIntent
; // Color space intent.
48 CalRgbEndpoint mGreen
;
67 mCsType(InfoColorSpace::SRGB
),
68 mCsIntent(InfoColorIntent::IMAGES
) {}
71 /// An entry in the color table.
72 struct ColorTableEntry
{
78 /// All the color-related bitfields for 16bpp and 32bpp images. We use this
79 /// even for older format BMPs that don't have explicit bitfields.
82 friend class BitFields
;
84 uint32_t mMask
; // The mask for the value.
85 uint8_t mRightShift
; // The amount to right-shift after masking.
86 uint8_t mBitWidth
; // The width (in bits) of the value.
88 /// Sets the mask (and thus the right-shift and bit-width as well).
89 void Set(uint32_t aMask
);
98 /// Returns true if this channel is used. Only used for alpha.
99 bool IsPresent() const { return mMask
!= 0x0; }
101 /// Extracts the single color value from the multi-color value.
102 uint8_t Get(uint32_t aVal
) const;
104 /// Like Get(), but specially for alpha.
105 uint8_t GetAlpha(uint32_t aVal
, bool& aHasAlphaOut
) const;
107 /// Specialized versions of Get() for when the bit-width is 5 or 8.
108 /// (They will assert if called and the bit-width is not 5 or 8.)
109 uint8_t Get5(uint32_t aVal
) const;
110 uint8_t Get8(uint32_t aVal
) const;
114 /// The individual color channels.
120 /// Set bitfields to the standard 5-5-5 16bpp values.
123 /// Set bitfields to the standard 8-8-8 32bpp values.
126 /// Test if bitfields have the standard 5-5-5 16bpp values.
127 bool IsR5G5B5() const;
129 /// Test if bitfields have the standard 8-8-8 32bpp values.
130 bool IsR8G8B8() const;
132 /// Read the bitfields from a header. The reading of the alpha mask is
134 void ReadFromHeader(const char* aData
, bool aReadAlpha
);
136 /// Length of the bitfields structure in the BMP file.
137 static const size_t LENGTH
= 12;
144 /// Decoder for BMP-Files, as used by Windows and OS/2.
146 class nsBMPDecoder
: public Decoder
{
150 DecoderType
GetType() const override
{ return DecoderType::BMP
; }
152 /// @return true if this BMP is a valid ICO resource.
153 bool IsValidICOResource() const override
{ return true; }
155 /// Obtains the internal output image buffer.
156 uint32_t* GetImageData() { return reinterpret_cast<uint32_t*>(mImageData
); }
158 /// Obtains the length of the internal output image buffer.
159 size_t GetImageDataLength() const { return mImageDataLength
; }
161 /// Obtains the size of the compressed image resource.
162 int32_t GetCompressedImageSize() const;
164 /// Mark this BMP as being within an ICO file. Only used for testing purposes
165 /// because the ICO-specific constructor does this marking automatically.
166 void SetIsWithinICO() { mIsWithinICO
= true; }
168 /// Did the BMP file have alpha data of any kind? (Only use this after the
169 /// bitmap has been fully decoded.)
170 bool HasTransparency() const { return mDoesHaveTransparency
; }
172 LexerResult
DoDecode(SourceBufferIterator
& aIterator
,
173 IResumable
* aOnResume
) override
;
174 nsresult
BeforeFinishInternal() override
;
175 nsresult
FinishInternal() override
;
178 friend class DecoderFactory
;
185 SKIP_TO_COLOR_PROFILE
,
198 // This is the constructor used for normal and clipboard BMP images.
199 explicit nsBMPDecoder(RasterImage
* aImage
, bool aForClipboard
= false);
201 // This is the constructor used for BMP resources in ICO images.
202 nsBMPDecoder(RasterImage
* aImage
, uint32_t aDataOffset
);
204 // Helper constructor called by the other two.
205 nsBMPDecoder(RasterImage
* aImage
, State aState
, size_t aLength
,
208 int32_t AbsoluteHeight() const { return abs(mH
.mHeight
); }
210 uint32_t* RowBuffer();
211 void ClearRowBufferRemainder();
215 void PrepareCalibratedColorProfile();
216 void PrepareColorProfileTransform();
218 LexerTransition
<State
> ReadFileHeader(const char* aData
, size_t aLength
);
219 LexerTransition
<State
> ReadInfoHeaderSize(const char* aData
, size_t aLength
);
220 LexerTransition
<State
> ReadInfoHeaderRest(const char* aData
, size_t aLength
);
221 LexerTransition
<State
> ReadBitfields(const char* aData
, size_t aLength
);
222 LexerTransition
<State
> SeekColorProfile(size_t aLength
);
223 LexerTransition
<State
> ReadColorProfile(const char* aData
, size_t aLength
);
224 LexerTransition
<State
> AllocateSurface();
225 LexerTransition
<State
> ReadColorTable(const char* aData
, size_t aLength
);
226 LexerTransition
<State
> SkipGap();
227 LexerTransition
<State
> AfterGap();
228 LexerTransition
<State
> ReadPixelRow(const char* aData
);
229 LexerTransition
<State
> ReadRLESegment(const char* aData
);
230 LexerTransition
<State
> ReadRLEDelta(const char* aData
);
231 LexerTransition
<State
> ReadRLEAbsolute(const char* aData
, size_t aLength
);
235 StreamingLexer
<State
> mLexer
;
237 // Iterator to save return point.
238 Maybe
<SourceBufferIterator
> mReturnIterator
;
240 UniquePtr
<uint32_t[]> mRowBuffer
;
244 // If the BMP is within an ICO file our treatment of it differs slightly.
247 // If the BMP decoded from the clipboard, we don't start with a data offset.
248 bool mIsForClipboard
;
250 bmp::BitFields mBitFields
;
252 // Might the image have transparency? Determined from the headers during
253 // metadata decode. (Does not guarantee the image actually has transparency.)
254 bool mMayHaveTransparency
;
256 // Does the image have transparency? Determined during full decoding, so only
257 // use this after that has been completed.
258 bool mDoesHaveTransparency
;
260 uint32_t mNumColors
; // The number of used colors, i.e. the number of
261 // entries in mColors, if it's present.
262 UniquePtr
<bmp::ColorTableEntry
[]>
263 mColors
; // The color table, if it's present.
264 uint32_t mBytesPerColor
; // 3 or 4, depending on the format
266 // The number of bytes prior to the optional gap that have been read. This
267 // is used to find the start of the pixel data.
268 uint32_t mPreGapLength
;
270 uint32_t mPixelRowSize
; // The number of bytes per pixel row.
272 int32_t mCurrentRow
; // Index of the row of the image that's currently
273 // being decoded: [height,1].
274 int32_t mCurrentPos
; // Index into the current line. Used when
275 // doing RLE decoding and when filling in pixels
276 // for truncated files.
278 // Only used in RLE_ABSOLUTE state: the number of pixels to read.
279 uint32_t mAbsoluteModeNumPixels
;
283 } // namespace mozilla
285 #endif // mozilla_image_decoders_nsBMPDecoder_h