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