1 /* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=2: */
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/. */
6 #ifndef mozilla_image_decoders_nsICODecoder_h
7 #define mozilla_image_decoders_nsICODecoder_h
9 #include "StreamingLexer.h"
11 #include "Downscaler.h"
13 #include "mozilla/gfx/2D.h"
14 #include "nsBMPDecoder.h"
15 #include "nsPNGDecoder.h"
16 #include "ICOFileHeaders.h"
27 ITERATE_UNSIZED_DIR_ENTRY
,
39 class nsICODecoder
: public Decoder
{
41 virtual ~nsICODecoder() {}
43 /// @return The offset from the beginning of the ICO to the first resource.
44 size_t FirstResourceOffset() const;
46 DecoderType
GetType() const override
{ return DecoderType::ICO
; }
47 LexerResult
DoDecode(SourceBufferIterator
& aIterator
,
48 IResumable
* aOnResume
) override
;
49 nsresult
FinishInternal() override
;
50 nsresult
FinishWithErrorInternal() override
;
53 friend class DecoderFactory
;
55 // Decoders should only be instantiated via DecoderFactory.
56 explicit nsICODecoder(RasterImage
* aImage
);
58 // Flushes the contained decoder to read all available data and sets the
59 // appropriate errors. Returns true if there are no errors.
60 bool FlushContainedDecoder();
62 // Gets decoder state from the contained decoder so it's visible externally.
63 nsresult
GetFinalStateFromContainedDecoder();
65 // Obtains the number of colors from the BPP, mBPP must be filled in
66 uint16_t GetNumColors();
68 LexerTransition
<ICOState
> ReadHeader(const char* aData
);
69 LexerTransition
<ICOState
> ReadDirEntry(const char* aData
);
70 LexerTransition
<ICOState
> IterateUnsizedDirEntry();
71 LexerTransition
<ICOState
> FinishDirEntry();
72 LexerTransition
<ICOState
> SniffResource(const char* aData
);
73 LexerTransition
<ICOState
> ReadResource();
74 LexerTransition
<ICOState
> ReadBIH(const char* aData
);
75 LexerTransition
<ICOState
> PrepareForMask();
76 LexerTransition
<ICOState
> ReadMaskRow(const char* aData
);
77 LexerTransition
<ICOState
> FinishMask();
78 LexerTransition
<ICOState
> FinishResource();
80 struct IconDirEntryEx
: public IconDirEntry
{
81 OrientedIntSize mSize
;
84 StreamingLexer
<ICOState
, 32> mLexer
; // The lexer.
85 Maybe
<Downscaler
> mDownscaler
; // The downscaler used for the mask.
86 RefPtr
<Decoder
> mContainedDecoder
; // Either a BMP or PNG decoder.
87 Maybe
<SourceBufferIterator
>
88 mReturnIterator
; // Iterator to save return point.
89 UniquePtr
<uint8_t[]> mMaskBuffer
; // A temporary buffer for the alpha mask.
90 nsTArray
<IconDirEntryEx
> mDirEntries
; // Valid dir entries with a size.
91 nsTArray
<IconDirEntryEx
> mUnsizedDirEntries
; // Dir entries without a size.
92 IconDirEntryEx
* mDirEntry
; // The dir entry for the selected resource.
93 uint16_t mNumIcons
; // Stores the number of icons in the ICO file.
94 uint16_t mCurrIcon
; // Stores the current dir entry index we are processing.
95 uint16_t mBPP
; // The BPP of the resource we're decoding.
97 mMaskRowSize
; // The size in bytes of each row in the BMP alpha mask.
98 uint32_t mCurrMaskLine
; // The line of the BMP alpha mask we're processing.
99 bool mIsCursor
; // Is this ICO a cursor?
100 bool mHasMaskAlpha
; // Did the BMP alpha mask have any transparency?
104 } // namespace mozilla
106 #endif // mozilla_image_decoders_nsICODecoder_h