image/decoders/nsBMPDecoder.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/image/decoders/nsBMPDecoder.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,135 @@
     1.4 +/* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4: */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +#ifndef _nsBMPDecoder_h
    1.11 +#define _nsBMPDecoder_h
    1.12 +
    1.13 +#include "nsAutoPtr.h"
    1.14 +#include "gfxColor.h"
    1.15 +#include "Decoder.h"
    1.16 +#include "BMPFileHeaders.h"
    1.17 +
    1.18 +namespace mozilla {
    1.19 +namespace image {
    1.20 +
    1.21 +class RasterImage;
    1.22 +
    1.23 +/**
    1.24 + * Decoder for BMP-Files, as used by Windows and OS/2
    1.25 + */
    1.26 +class nsBMPDecoder : public Decoder
    1.27 +{
    1.28 +public:
    1.29 +
    1.30 +    nsBMPDecoder(RasterImage &aImage);
    1.31 +    ~nsBMPDecoder();
    1.32 +
    1.33 +    // Specifies whether or not the BMP file will contain alpha data
    1.34 +    // If set to true and the BMP is 32BPP, the alpha data will be
    1.35 +    // retrieved from the 4th byte of image data per pixel 
    1.36 +    void SetUseAlphaData(bool useAlphaData);
    1.37 +    // Obtains the bits per pixel from the internal BIH header
    1.38 +    int32_t GetBitsPerPixel() const;
    1.39 +    // Obtains the width from the internal BIH header
    1.40 +    int32_t GetWidth() const;
    1.41 +    // Obtains the abs-value of the height from the internal BIH header
    1.42 +    int32_t GetHeight() const;
    1.43 +    // Obtains the internal output image buffer
    1.44 +    uint32_t* GetImageData();
    1.45 +    // Obtains the size of the compressed image resource
    1.46 +    int32_t GetCompressedImageSize() const;
    1.47 +    // Obtains whether or not a BMP file had alpha data in its 4th byte
    1.48 +    // for 32BPP bitmaps.  Only use after the bitmap has been processed.
    1.49 +    bool HasAlphaData() const;
    1.50 +
    1.51 +    virtual void WriteInternal(const char* aBuffer, uint32_t aCount, DecodeStrategy aStrategy);
    1.52 +    virtual void FinishInternal();
    1.53 +
    1.54 +private:
    1.55 +
    1.56 +    /** Calculates the red-, green- and blueshift in mBitFields using
    1.57 +     * the bitmasks from mBitFields */
    1.58 +    NS_METHOD CalcBitShift();
    1.59 +
    1.60 +    uint32_t mPos;
    1.61 +
    1.62 +    BMPFILEHEADER mBFH;
    1.63 +    BITMAPV5HEADER mBIH;
    1.64 +    char mRawBuf[WIN_V3_INTERNAL_BIH_LENGTH];
    1.65 +
    1.66 +    uint32_t mLOH; ///< Length of the header
    1.67 +
    1.68 +    uint32_t mNumColors; ///< The number of used colors, i.e. the number of entries in mColors
    1.69 +    colorTable *mColors;
    1.70 +
    1.71 +    bitFields mBitFields;
    1.72 +
    1.73 +    uint8_t *mRow;      ///< Holds one raw line of the image
    1.74 +    uint32_t mRowBytes; ///< How many bytes of the row were already received
    1.75 +    int32_t mCurLine;   ///< Index of the line of the image that's currently being decoded: [height,1]
    1.76 +    int32_t mOldLine;   ///< Previous index of the line 
    1.77 +    int32_t mCurPos;    ///< Index in the current line of the image
    1.78 +
    1.79 +    ERLEState mState;   ///< Maintains the current state of the RLE decoding
    1.80 +    uint32_t mStateData;///< Decoding information that is needed depending on mState
    1.81 +
    1.82 +    /** Set mBFH from the raw data in mRawBuf, converting from little-endian
    1.83 +     * data to native data as necessary */
    1.84 +    void ProcessFileHeader();
    1.85 +    /** Set mBIH from the raw data in mRawBuf, converting from little-endian
    1.86 +     * data to native data as necessary */
    1.87 +    void ProcessInfoHeader();
    1.88 +
    1.89 +    // Stores whether the image data may store alpha data, or if
    1.90 +    // the alpha data is unspecified and filled with a padding byte of 0. 
    1.91 +    // When a 32BPP bitmap is stored in an ICO or CUR file, its 4th byte
    1.92 +    // is used for alpha transparency.  When it is stored in a BMP, its
    1.93 +    // 4th byte is reserved and is always 0.
    1.94 +    // Reference: 
    1.95 +    // http://en.wikipedia.org/wiki/ICO_(file_format)#cite_note-9
    1.96 +    // Bitmaps where the alpha bytes are all 0 should be fully visible.
    1.97 +    bool mUseAlphaData;
    1.98 +    // Whether the 4th byte alpha data was found to be non zero and hence used.
    1.99 +    bool mHaveAlphaData;
   1.100 +};
   1.101 +
   1.102 +/** Sets the pixel data in aDecoded to the given values.
   1.103 + * @param aDecoded pointer to pixel to be set, will be incremented to point to the next pixel.
   1.104 + */
   1.105 +static inline void SetPixel(uint32_t*& aDecoded, uint8_t aRed, uint8_t aGreen, uint8_t aBlue, uint8_t aAlpha = 0xFF)
   1.106 +{
   1.107 +    *aDecoded++ = gfxPackedPixel(aAlpha, aRed, aGreen, aBlue);
   1.108 +}
   1.109 +
   1.110 +static inline void SetPixel(uint32_t*& aDecoded, uint8_t idx, colorTable* aColors)
   1.111 +{
   1.112 +    SetPixel(aDecoded, aColors[idx].red, aColors[idx].green, aColors[idx].blue);
   1.113 +}
   1.114 +
   1.115 +/** Sets two (or one if aCount = 1) pixels
   1.116 + * @param aDecoded where the data is stored. Will be moved 4 resp 8 bytes
   1.117 + * depending on whether one or two pixels are written.
   1.118 + * @param aData The values for the two pixels
   1.119 + * @param aCount Current count. Is decremented by one or two.
   1.120 + */
   1.121 +inline void Set4BitPixel(uint32_t*& aDecoded, uint8_t aData,
   1.122 +                         uint32_t& aCount, colorTable* aColors)
   1.123 +{
   1.124 +    uint8_t idx = aData >> 4;
   1.125 +    SetPixel(aDecoded, idx, aColors);
   1.126 +    if (--aCount > 0) {
   1.127 +        idx = aData & 0xF;
   1.128 +        SetPixel(aDecoded, idx, aColors);
   1.129 +        --aCount;
   1.130 +    }
   1.131 +}
   1.132 +
   1.133 +} // namespace image
   1.134 +} // namespace mozilla
   1.135 +
   1.136 +
   1.137 +#endif
   1.138 +

mercurial