Back to index

lightning-sunbird  0.9+nobinonly
nsBMPDecoder.h
Go to the documentation of this file.
00001 /* vim:set tw=80 expandtab softtabstop=4 ts=4 sw=4: */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla BMP Decoder.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Christian Biesinger <cbiesinger@web.de>.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 
00039 #ifndef _nsBMPDecoder_h
00040 #define _nsBMPDecoder_h
00041 
00042 #include "nsCOMPtr.h"
00043 #include "imgIDecoder.h"
00044 #include "imgIContainer.h"
00045 #include "imgIDecoderObserver.h"
00046 #include "gfxIImageFrame.h"
00047 
00048 #define NS_BMPDECODER_CID \
00049 { /* {78c61626-4d1f-4843-9364-4652d98ff6e1} */ \
00050   0x78c61626, \
00051   0x4d1f, \
00052   0x4843, \
00053   { 0x93, 0x64, 0x46, 0x52, 0xd9, 0x8f, 0xf6, 0xe1 } \
00054 }
00055 
00056 struct BMPFILEHEADER {
00057     char signature[2]; // String "BM"
00058     PRUint32 filesize;
00059     PRInt32 reserved; // Zero
00060     PRUint32 dataoffset; // Offset to raster data
00061 
00062     PRUint32 bihsize;
00063 };
00064 #define BFH_LENGTH 18 // Note: For our purposes, we include bihsize in the BFH
00065 
00066 #define OS2_BIH_LENGTH 12 // This is the real BIH size (as contained in the bihsize field of BMPFILEHEADER)
00067 #define OS2_HEADER_LENGTH (BFH_LENGTH + 8)
00068 #define WIN_HEADER_LENGTH (BFH_LENGTH + 36)
00069 
00070 struct BMPINFOHEADER {
00071     PRInt32 width; // Uint16 in OS/2 BMPs
00072     PRInt32 height; // Uint16 in OS/2 BMPs
00073     PRUint16 planes; // =1
00074     PRUint16 bpp; // Bits per pixel.
00075     // The rest of the header is not available in OS/2 BMP Files
00076     PRUint32 compression; // 0=no compression 1=8bit RLE 2=4bit RLE
00077     PRUint32 image_size; // (compressed) image size. Can be 0 if compression==0
00078     PRUint32 xppm; // Pixels per meter, horizontal
00079     PRUint32 yppm; // Pixels per meter, vertical
00080     PRUint32 colors; // Used Colors
00081     PRUint32 important_colors; // Number of important colors. 0=all
00082 };
00083 
00084 struct colorTable {
00085     PRUint8 red;
00086     PRUint8 green;
00087     PRUint8 blue;
00088 };
00089 
00090 struct bitFields {
00091     PRUint32 red;
00092     PRUint32 green;
00093     PRUint32 blue;
00094     PRUint8 redLeftShift;
00095     PRUint8 redRightShift;
00096     PRUint8 greenLeftShift;
00097     PRUint8 greenRightShift;
00098     PRUint8 blueLeftShift;
00099     PRUint8 blueRightShift;
00100 };
00101 
00102 #define BITFIELD_LENGTH 12 // Length of the bitfields structure in the bmp file
00103 
00104 #if defined WORDS_BIGENDIAN || defined IS_BIG_ENDIAN
00105 // We must ensure that the entity is unsigned
00106 // otherwise, if it is signed/negative, the MSB will be
00107 // propagated when we shift
00108 #define LITTLE_TO_NATIVE16(x) (((((PRUint16) x) & 0xFF) << 8) | \
00109                                (((PRUint16) x) >> 8))
00110 #define LITTLE_TO_NATIVE32(x) (((((PRUint32) x) & 0xFF) << 24) | \
00111                                (((((PRUint32) x) >> 8) & 0xFF) << 16) | \
00112                                (((((PRUint32) x) >> 16) & 0xFF) << 8) | \
00113                                (((PRUint32) x) >> 24))
00114 #else
00115 #define LITTLE_TO_NATIVE16(x) x
00116 #define LITTLE_TO_NATIVE32(x) x
00117 #endif
00118 
00119 #if defined(XP_WIN) || defined(XP_OS2) || defined(XP_BEOS) || defined(MOZ_WIDGET_PHOTON)
00120 #define BMP_GFXFORMAT gfxIFormats::BGR
00121 #define RLE_GFXFORMAT_ALPHA gfxIFormats::BGR_A1
00122 #else
00123 #define USE_RGB
00124 #define BMP_GFXFORMAT gfxIFormats::RGB
00125 #define RLE_GFXFORMAT_ALPHA gfxIFormats::RGB_A1
00126 #endif
00127 
00128 #if defined(XP_MAC) || defined(XP_MACOSX)
00129 #define GFXBYTESPERPIXEL 4
00130 #else
00131 #define GFXBYTESPERPIXEL 3
00132 #endif
00133 
00134 // BMPINFOHEADER.compression defines
00135 #define BI_RLE8 1
00136 #define BI_RLE4 2
00137 #define BI_BITFIELDS 3
00138 
00139 // RLE Escape codes
00140 #define RLE_ESCAPE       0
00141 #define RLE_ESCAPE_EOL   0
00142 #define RLE_ESCAPE_EOF   1
00143 #define RLE_ESCAPE_DELTA 2
00144 
00146 enum ERLEState {
00147     eRLEStateInitial,
00148     eRLEStateNeedSecondEscapeByte,
00149     eRLEStateNeedXDelta,
00150     eRLEStateNeedYDelta,    
00151     eRLEStateAbsoluteMode,  
00152     eRLEStateAbsoluteModePadded 
00153 };
00154 
00158 class nsBMPDecoder : public imgIDecoder
00159 {
00160 public:
00161     NS_DECL_ISUPPORTS
00162     NS_DECL_IMGIDECODER
00163     
00164     nsBMPDecoder();
00165     ~nsBMPDecoder();
00166 
00167 private:
00169     static NS_METHOD ReadSegCb(nsIInputStream* aIn, void* aClosure,
00170                                const char* aFromRawSegment, PRUint32 aToOffset,
00171                                PRUint32 aCount, PRUint32 *aWriteCount);
00172 
00176     NS_METHOD ProcessData(const char* aBuffer, PRUint32 aCount);
00177 
00180     NS_METHOD CalcBitShift();
00181 
00184     nsresult SetData();
00185 
00190     nsresult WriteRLERows(PRUint32 rows);
00191 
00192     nsCOMPtr<imgIDecoderObserver> mObserver;
00193 
00194     nsCOMPtr<imgIContainer> mImage;
00195     nsCOMPtr<gfxIImageFrame> mFrame;
00196 
00197     PRUint32 mPos;
00198 
00199     BMPFILEHEADER mBFH;
00200     BMPINFOHEADER mBIH;
00201     char mRawBuf[36];
00202 
00203     PRUint32 mLOH; 
00204 
00205     PRUint32 mNumColors; 
00206     colorTable *mColors;
00207 
00208     bitFields mBitFields;
00209 
00210     PRUint8 *mRow;      
00211     PRUint32 mRowBytes; 
00212     PRInt32 mCurLine;   
00213     PRUint8 *mAlpha;    
00214     PRUint8 *mAlphaPtr; 
00215     PRUint8 *mDecoded;  
00216     PRUint8 *mDecoding; 
00217 
00218     ERLEState mState;   
00219     PRUint32 mStateData;
00220 
00221     PRUint32 mBpr;      
00222 
00225     void ProcessFileHeader();
00228     void ProcessInfoHeader();
00229 };
00230 
00233 inline void SetPixel(PRUint8*& aDecoded, PRUint8 aRed, PRUint8 aGreen, PRUint8 aBlue)
00234 {
00235 #if defined(XP_MAC) || defined(XP_MACOSX)
00236     *aDecoded++ = 0; // Mac needs this padding byte
00237 #endif
00238 #ifdef USE_RGB
00239     *aDecoded++ = aRed;
00240     *aDecoded++ = aGreen;
00241     *aDecoded++ = aBlue;
00242 #else
00243     *aDecoded++ = aBlue;
00244     *aDecoded++ = aGreen;
00245     *aDecoded++ = aRed;
00246 #endif
00247 }
00248 
00249 inline void SetPixel(PRUint8*& aDecoded, PRUint8 idx, colorTable* aColors)
00250 {
00251     PRUint8 red, green, blue;
00252     red = aColors[idx].red;
00253     green = aColors[idx].green;
00254     blue = aColors[idx].blue;
00255     SetPixel(aDecoded, red, green, blue);
00256 }
00257 
00263 inline void Set4BitPixel(PRUint8*& aDecoded, PRUint8 aData,
00264                          PRUint32& aCount, colorTable* aColors)
00265 {
00266     PRUint8 idx = aData >> 4;
00267     SetPixel(aDecoded, idx, aColors);
00268     if (--aCount > 0) {
00269         idx = aData & 0xF;
00270         SetPixel(aDecoded, idx, aColors);
00271         --aCount;
00272     }
00273 }
00274 
00275 #endif