nsICODecoder.h (3770B)
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/. */ 5 6 #ifndef mozilla_image_decoders_nsICODecoder_h 7 #define mozilla_image_decoders_nsICODecoder_h 8 9 #include "StreamingLexer.h" 10 #include "Decoder.h" 11 #include "Downscaler.h" 12 #include "imgFrame.h" 13 #include "mozilla/gfx/2D.h" 14 #include "nsBMPDecoder.h" 15 #include "nsPNGDecoder.h" 16 #include "ICOFileHeaders.h" 17 18 namespace mozilla { 19 namespace image { 20 21 class RasterImage; 22 23 enum class ICOState { 24 HEADER, 25 DIR_ENTRY, 26 FINISHED_DIR_ENTRY, 27 ITERATE_UNSIZED_DIR_ENTRY, 28 SKIP_TO_RESOURCE, 29 FOUND_RESOURCE, 30 SNIFF_RESOURCE, 31 READ_RESOURCE, 32 PREPARE_FOR_MASK, 33 READ_MASK_ROW, 34 FINISH_MASK, 35 SKIP_MASK, 36 FINISHED_RESOURCE 37 }; 38 39 class nsICODecoder : public Decoder { 40 public: 41 virtual ~nsICODecoder() {} 42 43 /// @return The offset from the beginning of the ICO to the first resource. 44 size_t FirstResourceOffset() const; 45 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; 51 52 private: 53 friend class DecoderFactory; 54 55 // Decoders should only be instantiated via DecoderFactory. 56 explicit nsICODecoder(RasterImage* aImage); 57 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(); 61 62 // Gets decoder state from the contained decoder so it's visible externally. 63 nsresult GetFinalStateFromContainedDecoder(); 64 65 // Obtains the number of colors from the BPP, mBPP must be filled in 66 uint16_t GetNumColors(); 67 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(); 79 80 struct IconDirEntryEx : public IconDirEntry { 81 OrientedIntSize mSize; 82 }; 83 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. 96 uint32_t 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? 101 }; 102 103 } // namespace image 104 } // namespace mozilla 105 106 #endif // mozilla_image_decoders_nsICODecoder_h