gifutil.h

Go to the documentation of this file.
00001 // $Header: /Camelot/winoil/gifutil.h 12    20/05/97 16:18 Neville $
00002 
00003 // Header for GIFUtil which suprisingly enough contains lots of
00004 // useful routines for:-
00005 //      reading in a GIF file as a bitmap
00006 //
00007 
00008 // Code based on the GD image library code which has the following
00009 // copyright notice:-
00010 /* +-------------------------------------------------------------------+ */
00011 /* | Copyright 1990, 1991, 1993, David Koblas.  (koblas@netcom.com)    | */
00012 /* |   Permission to use, copy, modify, and distribute this software   | */
00013 /* |   and its documentation for any purpose and without fee is hereby | */
00014 /* |   granted, provided that the above copyright notice appear in all | */
00015 /* |   copies and that both that copyright notice and this permission  | */
00016 /* |   notice appear in supporting documentation.  This software is    | */
00017 /* |   provided "as is" without express or implied warranty.           | */
00018 /* +-------------------------------------------------------------------+ */
00019 // Code in this file at present is mainly from GIDDecod.c 
00020 
00021 #ifndef INC_GIFUTIL
00022 #define INC_GIFUTIL
00023 
00024 #include "ccfile.h"                     // needs FilePos
00025 #include "dibconv.h"                    // needs DIBConvert
00026 #include "dibutil.h"                    // needs FNPTR_SCANLINE
00027 //#include "outptdib.h" 
00028 
00029 typedef struct tagGIFINFOHEADER
00030 {
00031         char        giName[6];      // GIF87a or 9a
00032         WORD        giWidth;
00033         WORD        giHeight;
00034         BYTE        giFlags;        // Colourmap flag, bpp, resolution
00035         BYTE        giBackground;   // background colour
00036         BYTE        giAspect;       // aspect
00037 } GIFINFOHEADER, FAR *LPGIFINFOHEADER;
00038 
00039 typedef struct tagGIFIMAGEBLOCK
00040 {
00041         WORD        gibLeft;
00042         WORD        gibTop;
00043         WORD        gibWidth;
00044         WORD        gibDepth;
00045         BYTE        gibFlags;       // Colourmap flag, bpp, resolution
00046 } GIFIMAGEBLOCK, FAR *LPGIFIMAGEBLOCK;
00047 
00048 // Flags definitions
00049 #define INTERLACE       0x40
00050 #define LOCALCOLOURMAP  0x80
00051 #define GLOBALCOLOURMAP 0x80
00052 
00053 // Extension block type definitions
00054 #define EXTENSIONBLOCK      0x21
00055 #define TRANSPARENTBLOCK    0xf9
00056 
00057 typedef struct tagGIFTRANSBLOCK
00058 {
00059         BYTE        gtbBlockStart;  // 0x21
00060         BYTE        gtbIdentifier;  // 0xf9
00061         BYTE        gtbBlockSize;   // 4 (always)
00062         BYTE        gtbFlags;       // 1 (transparent flag 0x01, wait for key 0x02)
00063         WORD        gtbDelay;       // 0 (delay to display for if 0x02 set)
00064         BYTE        gtbTransparency;// transparency
00065         BYTE        gtbTerminator;  // 0
00066 } GIFTRANSBLOCK, FAR *LPGIFTRANSBLOCK;
00067 
00068 #define COMMENTBLOCK 0xfe
00069 
00070 typedef struct tagGIFCOMMENTBLOCK
00071 {
00072         BYTE        gcbBlockStart;  // 0x21
00073         BYTE        gcbIdentifier;  // 0xfe
00074         BYTE        gcbTextSize;    //  size of text, followed by text
00075 } GIFCOMMENTBLOCK, FAR *LPGIFCOMMENTBLOCK;
00076 
00077 #define APPLICATIONBLOCK 0xff
00078 
00079 typedef struct tagGIFAPPLICATIONBLOCK
00080 {
00081         BYTE        gabBlockStart;  // 0x21
00082         BYTE        gabIdentifier;  // 0xff
00083         BYTE        gabBlockSize;   // size of block
00084         CHAR        gabString[8];   // application block
00085         BYTE        gabAuthentication;  // check sum
00086 } GIFAPPLICATIONBLOCK, FAR *LPGIFAPPLICATIONBLOCK;
00087 
00088 #define PLAINTEXTBLOCK  0x01
00089 
00090 typedef struct tagGIFRGBTRIPLE
00091 {
00092         BYTE        grgbtRed;
00093         BYTE        grgbtGreen;
00094         BYTE        grgbtBlue;
00095 } GIFRGBTRIPLE, FAR *LPGIFRGBTRIPLE;
00096 
00097 
00098 const unsigned long masks[] = {
00099                                  0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
00100                                  0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF,
00101                                  0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF,
00102                                  0x7FFF, 0xFFFF
00103                               };
00104 
00105 //#define MAXCOLORMAPSIZE   256
00106 #define GIFBITS         12
00107 #define MAX_LWZ_BITS    12
00108 #define HSIZE           5003            // 80% occupancy
00109 
00110 typedef int         code_int;
00111 typedef long int    count_int;
00112 
00113 const int maxbits = GIFBITS;                            // user settable max # bits/code
00114 const code_int maxmaxcode = (code_int)1 << GIFBITS;     // should NEVER generate this code
00115 
00116 const code_int hsize = HSIZE;                   // for dynamic table sizing
00117 
00118 enum GIFDisposalMethod
00119 {
00120     GDM_NONE        = 0,    // No disposal specified. The decoder is not required to take any action.
00121     GDM_LEAVE       = 1,    // Do not dispose. The graphic is to be left in place.
00122     GDM_BACKTOBACK  = 2,    // Restore to background color. The area used by the graphic must be restored to the background color.
00123     GDM_PREVIOUS    = 3     // Restore to previous. The decoder is required to restore the area overwritten by the graphic with what was there prior to rendering the graphic.
00124 };
00125 
00126 
00127     
00128 class BaseCamelotFilter;
00129 
00130 /********************************************************************************************
00131 
00132 >   class Palette : public CC_CLASS_MEMDUMP
00133 
00134     Author:     Colin
00135     Created:    24/4/95
00136     Purpose:    Something that should have been done long ago...
00137     SeeAlso:    Everywhere
00138 
00139 ********************************************************************************************/
00140 class PaletteIterator
00141 {
00142 public:
00143     PaletteIterator(): m_pPalette(NULL) {}
00144 
00145     void SetRed(const UINT32 nRed)
00146     {   m_pPalette->rgbRed = nRed;  }
00147 
00148     void SetGreen(const UINT32 nGreen)
00149     {   m_pPalette->rgbGreen = nGreen;  }
00150 
00151     void SetBlue(const UINT32 nBlue)
00152     {   m_pPalette->rgbBlue = nBlue;    }
00153 
00154     void SetTransparent(const UINT32 nTransparent)
00155     {   m_pPalette->rgbReserved = nTransparent; }
00156 
00157     BOOL operator< (const PaletteIterator& rhsIterator)
00158     {   return m_pPalette < rhsIterator.m_pPalette; }
00159 
00160     PaletteIterator& operator++()
00161     {
00162         ++m_pPalette;
00163         return *this;
00164     }
00165 
00166 protected:
00167     LPRGBQUAD   m_pPalette;
00168 };
00169 
00170 
00171 /********************************************************************************************
00172 
00173 >   class Palette : public CC_CLASS_MEMDUMP
00174 
00175     Author:     Colin
00176     Created:    24/4/95
00177     Purpose:    Something that should have been done long ago...
00178     SeeAlso:    Everywhere
00179 
00180 ********************************************************************************************/
00181 class Palette : public CC_CLASS_MEMDUMP
00182 {
00183     // Declare the class for memory tracking
00184     CC_DECLARE_MEMDUMP(Palette);
00185 public:
00186     Palette() : m_pColours(NULL), m_nSize(0) {}
00187     ~Palette();
00188 
00189 //  Palette& operator= (const Palette& otherPalette) {return Palette(otherPalette);}
00190 
00191     BOOL    SetSize(const UINT32 nSize);
00192 
00193     
00194     BOOL    IsConstructedOK() const
00195     {   return m_bInitOK;   }
00196 
00197 
00198     const PaletteIterator&  Start() const
00199     {   return m_StartIterator; }
00200 
00201     const PaletteIterator&  End() const
00202     {   return m_EndIterator;   }
00203 
00204 protected:
00205     Palette(const Palette& otherPalette);
00206 
00207 protected:
00208     LPRGBQUAD   m_pColours;
00209     UINT32      m_nSize;
00210 
00211     PaletteIterator     m_StartIterator;
00212     PaletteIterator     m_EndIterator;
00213 
00214 private:
00215     BOOL        m_bInitOK;
00216 };
00217 
00218 
00219 /********************************************************************************************
00220 
00221 >   class OutputGIF : public OutputDIB
00222 
00223     Author:     Neville Humphrys
00224     Created:    24/4/95
00225     Purpose:    Contains functions to read in a GIF file as a DIB.
00226     SeeAlso:    OutputGIF;
00227 
00228 ********************************************************************************************/
00229 
00230 class GIFUtil //: public CC_CLASS_MEMDUMP
00231 {
00232 public:
00233 
00234     //GIFUtil();
00235     //~GIFUtil();
00236 
00237     static BOOL ReadFromFile( CCLexFile *File, LPBITMAPINFO *Info, LPBYTE *Bits,
00238                             int *TransColour, String_64 *ProgressString = NULL,
00239                             BaseCamelotFilter *pFilter = NULL );
00240     static BOOL ReadFromFile( CCLexFile *File, LPBITMAPINFO *Info, LPBYTE *Bits,
00241                               int *TransColour, int& nBitmapToRead, String_64 *ProgressString = NULL,
00242                               BaseCamelotFilter *pFilter = NULL, UINT32* Delay=NULL,
00243                               GIFDisposalMethod *Restore=NULL,
00244                               UINT32 * pLeftOffset = NULL, UINT32* pTopOffset = NULL,
00245                               BOOL * pLocalPalette = NULL);
00246 //  static BOOL WriteToFile ( CCLexFile *, LPBITMAPINFO Info, LPBYTE Bits,
00247 //                            String_64 *ProgressString = NULL);
00248 
00249     // Access to the values read in as part of the GIF header
00250     static UINT32 GetGlobalWidth() { return m_GlobalWidth; }
00251     static UINT32 GetGlobalHeight() { return m_GlobalHeight; }
00252 
00253 protected:
00254     BOOL ProcessHeader();
00255     static BOOL ProcessExtension(CCLexFile *fd);
00256     static BOOL ProcessImageBlock(CCLexFile* File, LPBITMAPINFO *Info, LPBYTE *Bits,
00257                                   UINT32 * pLeftOffset = NULL, UINT32 * pTopOffset = NULL,
00258                                   BOOL * pLocalPalette = NULL);
00259 
00260     static BOOL ReadColourMap(CCLexFile *fd, int number, LPRGBQUAD lpPalette);
00261     static int GetDataBlock( CCLexFile *fd, unsigned char  *buf );
00262     static int GetCode( CCLexFile *fd, int code_size, int flag );
00263     static int LWZReadByte( CCLexFile *fd, int flag, int input_code_size );
00264     static BOOL ReadImage( CCLexFile *fd, LPBYTE pBitsData, int width, int height, int bpp,
00265                            BOOL interlace, BaseCamelotFilter *pFilter = NULL );
00266 
00267 private:
00268     // Some useful variables
00269     //int Width;                            // Width of the image
00270     //int Height;                           // Height of the image
00271     static WORD m_GlobalWidth;              // The overall width from the GIF header
00272     static WORD m_GlobalHeight;             // The overall height from the GIF header
00273 
00274     static BOOL Interlace;                  // Use interlace or not
00275     static int Transparent;                 // colour or -1 = no transparency
00276     static UINT32 m_Delay;                  // The Animation delay value for each bitmap.
00277     static GIFDisposalMethod m_Restore;     // The Animation Restore value for exch bitmap.     
00278 
00279     static int  m_nCurrentBitmap;           // the number of the bitmap currently (or was just) in the buffer 
00280 
00281     //WORD BitsPerPixel;                // Colour depth required
00282     
00283     //UINT32 WidthOfLine;               // word/byte rounded line width rather than the pixel width
00284 
00285     // Importint specific variables
00286     static BOOL ZeroDataBlock;      // flag to say whether we have a zero length block or not 
00287 
00288     Palette m_GlobalPalette;    // the global palette of the GIF
00289 
00290     // replace this with the above...eventually
00291     static LPRGBQUAD lpGlobalPalette;       // pointer to temporary palette store
00292     static int GlobalPaletteSize;           // size of the global palette found  
00293 
00294     static BOOL         m_bImageRead;   // Has an image been found this pass?
00295     // the start position of the next image in the file
00296     static FilePos      m_NextImageStartPosition;   
00297 };
00298 
00299 #endif // INC_GIFUTIL

Generated on Sat Nov 10 03:48:31 2007 for Camelot by  doxygen 1.4.4