Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

plgifdec.cpp

Go to the documentation of this file.
00001 /*
00002 /--------------------------------------------------------------------
00003 |
00004 |      $Id: plgifdec.cpp,v 1.1 2004/05/21 21:02:52 maxx Exp $
00005 |
00006 |      TODO: Error handling.
00007 |
00008 |      Copyright (c) 1996-1998 Ulrich von Zadow
00009 |      Initial version by Michael Salzlechner
00010 |
00011 \-------------------------------------------------------------------
00012 */
00013 
00014 #include "plstdpch.h"
00015 
00016 #include "plgifdec.h"
00017 #include "planybmp.h"
00018 
00019 extern "C" 
00020 {
00021 #define DOEXTERNGIF
00022 
00023 // Workaround for namespace clash. This has been fixed in newer versions
00024 // of libungif.
00025 #ifdef _WINDOWS
00026 #undef DrawText
00027 #endif
00028 #include "gif_lib.h"
00029 }
00030 
00031 #include <stdio.h>
00032 
00033 static int
00034     InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */
00035     InterlacedJumps[] = { 8, 8, 4, 2 };    /* be read - offsets and jumps... */
00036 
00038 // Construction/Destruction
00040 
00041 PLGIFDecoder::PLGIFDecoder()
00042   : PLPicDecoder()
00043 {
00044 //      SetFileExtensions("*.gif");
00045 //      SetFileDescription("GIF File");
00046 }
00047 
00048 PLGIFDecoder::~PLGIFDecoder()
00049 {
00050 
00051 }
00052 
00053 int GIF_Read_Data(GifFileType* pGifFile,GifByteType* pByteType,int length)
00054 {
00055   PLBYTE *ptr;
00056   PLDataSource* pSourceInfo=(PLDataSource*)pGifFile->UserData;
00057 
00058   ptr = pSourceInfo->ReadNBytes(length);
00059   memcpy(pByteType,ptr,length);
00060 
00061   return length;
00062 }
00063 
00064 void PLGIFDecoder::DoDecode(PLBmp *pBmp, PLDataSource *pDataSrc)
00065 {
00066   GifFileType *GifFile;
00067   GifRecordType RecordType;
00068   GifByteType *Extension;
00069   ColorMapObject *ColorMap = NULL;
00070 
00071         int CurLine = 0;
00072         int i, Row, Col, Width, Height, Count, j, ExtCode;
00073 
00074         GifFile = DGifOpen( (void*)pDataSrc, GIF_Read_Data);
00075 
00076         pBmp->Create (GifFile->SWidth, GifFile->SHeight , 8, false);
00077 
00078         PLBYTE ** pLineArray = pBmp->GetLineArray();
00079 
00080         CurLine = 0;
00081         while (CurLine < GifFile->SHeight)
00082         {
00083                 pLineArray[CurLine][0] = GifFile->SBackGroundColor;
00084             CurLine++;
00085         }
00086 
00087     /* Scan the content of the GIF file and load the image(s) in: */
00088   do
00089   {
00090     if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR)
00091     {
00092             PrintGifError();
00093       PLASSERT (false);
00094           }
00095 
00096     switch (RecordType)
00097     {
00098             case IMAGE_DESC_RECORD_TYPE:
00099                     if (DGifGetImageDesc(GifFile) == GIF_ERROR)
00100         {
00101                       PrintGifError();
00102           PLASSERT (false);
00103         }
00104         Row = GifFile->Image.Top; /* Image Position relative to Screen. */
00105         Col = GifFile->Image.Left;
00106         Width = GifFile->Image.Width;
00107         Height = GifFile->Image.Height;
00108         if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth ||
00109                   GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight)
00110         {
00111           fprintf(stderr, "Image %d is not confined to screen dimension, aborted.\n");
00112           PLASSERT (false);
00113         }
00114         if (GifFile->Image.Interlace)
00115         {
00116                       /* Need to perform 4 passes on the images: */
00117                       for (Count = i = 0; i < 4; i++)
00118                   for (j = Row + InterlacedOffset[i]; j < Row + Height;
00119                                          j += InterlacedJumps[i])
00120             {
00121                                 GifQprintf("\b\b\b\b%-4d", Count++);
00122                                 if (DGifGetLine(GifFile, &pLineArray[j][Col], Width) == GIF_ERROR)
00123               {
00124                             PrintGifError();
00125                 PLASSERT (false);
00126                                 }
00127             }
00128         }
00129         else
00130         {
00131                       for (i = 0; i < Height; i++)
00132           {
00133                               GifQprintf("\b\b\b\b%-4d", i);
00134                               if (DGifGetLine(GifFile, &pLineArray[Row++][Col], Width) == GIF_ERROR)
00135             {
00136                                 PrintGifError();
00137               PLASSERT (false);
00138                         }
00139                       }
00140                     }
00141                     break;
00142             case EXTENSION_RECORD_TYPE:
00143                     /* Skip any extension blocks in file: */
00144                     if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR)
00145         {
00146                       PrintGifError();
00147           PLASSERT (false);
00148                     }
00149                     while (Extension != NULL)
00150         {
00151                       if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR)
00152           {
00153                               PrintGifError();
00154             PLASSERT (false);
00155                       }
00156                     }
00157                     break;
00158             case TERMINATE_RECORD_TYPE:
00159                     break;
00160             default:                /* Should be trapped by DGifGetRecordType. */
00161                     break;
00162           }
00163   } while (RecordType != TERMINATE_RECORD_TYPE);
00164 
00165   ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap :
00166                     GifFile->SColorMap);
00167 
00168         PLBYTE* pb = (PLBYTE*)ColorMap->Colors;
00169         for (i = 0; i < ColorMap->ColorCount; i++)
00170         {
00171                 pBmp->SetPaletteEntry(i, pb[0], pb[1], pb[2], 255);
00172                 pb += 3;
00173         }
00174 
00175         DGifCloseFile(GifFile);
00176 }
00177 
00178 /*
00179 /--------------------------------------------------------------------
00180 |
00181 |      $Log: plgifdec.cpp,v $
00182 |      Revision 1.1  2004/05/21 21:02:52  maxx
00183 |      Initial Version of vuVolume, moderatly changed to make it compile on my windows and linux machine.
00184 |
00185 |      Revision 1.1  2002/11/13 01:58:21  mspindle
00186 |      *** empty log message ***
00187 |
00188 |      Revision 1.3  2001/10/21 17:12:39  uzadow
00189 |      Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel.
00190 |
00191 |      Revision 1.2  2001/10/06 22:37:08  uzadow
00192 |      Linux compatibility.
00193 |
00194 |      Revision 1.1  2001/09/16 19:03:22  uzadow
00195 |      Added global name prefix PL, changed most filenames.
00196 |
00197 |      Revision 1.6  2001/09/15 21:02:44  uzadow
00198 |      Cleaned up stdpch.h and config.h to make them internal headers.
00199 |
00200 |      Revision 1.5  2001/02/04 14:31:52  uzadow
00201 |      Member initialization list cleanup (Erik Hoffmann).
00202 |
00203 |      Revision 1.4  2001/01/15 12:09:34  uzadow
00204 |      Fixed crash decoding some gif files.
00205 |
00206 |      Revision 1.3  2000/12/18 22:42:52  uzadow
00207 |      Replaced RGBAPIXEL with PLPixel32.
00208 |
00209 |      Revision 1.2  2000/12/17 20:45:57  uzadow
00210 |      Fixed build problem with libungif.
00211 |
00212 |      Revision 1.1  2000/12/08 13:41:23  uzadow
00213 |      Added gif support by Michael Salzlechner.
00214 |
00215 |
00216 \--------------------------------------------------------------------
00217 */

Generated on Wed Dec 15 21:20:30 2004 for vuVolume by  doxygen 1.3.9.1