1 //////////////////////////////////////////////////////////////////////////////// 2 // 3 // File: GIFLoad.h 4 // 5 // Date: December 1999 6 // 7 // Author: Daniel Switkin 8 // 9 // Copyright 2003 (c) by Daniel Switkin. This file is made publically available 10 // under the BSD license, with the stipulations that this complete header must 11 // remain at the top of the file indefinitely, and credit must be given to the 12 // original author in any about box using this software. 13 // 14 //////////////////////////////////////////////////////////////////////////////// 15 16 #ifndef GIFLOAD_H 17 #define GIFLOAD_H 18 19 #include <DataIO.h> 20 #include "LoadPalette.h" 21 22 #define GIF_INTERLACED 0x40 23 #define GIF_LOCALCOLORMAP 0x80 24 25 26 class Memblock { 27 public: 28 uchar data[4096]; 29 int offset; 30 Memblock *next; 31 }; 32 33 34 const int gl_pass_starts_at[] = {0, 4, 2, 1, 0}; 35 const int gl_increment_pass_by[] = {8, 8, 4, 2, 0}; 36 37 38 class GIFLoad { 39 public: 40 GIFLoad(BPositionIO *input, BPositionIO *output); 41 ~GIFLoad(); 42 bool fatalerror; 43 44 private: 45 bool ReadGIFHeader(); 46 bool ReadGIFLoopBlock(); 47 bool ReadGIFControlBlock(); 48 bool ReadGIFImageHeader(); 49 bool ReadGIFImageData(); 50 bool ReadGIFCommentBlock(); 51 bool ReadGIFUnknownBlock(unsigned char c); 52 53 bool InitFrame(int size); 54 short NextCode(); 55 void ResetTable(); 56 57 uchar *MemblockAllocate(int size); 58 void MemblockDeleteAll(); 59 60 inline bool OutputColor(unsigned char *string, int size) { 61 int bpr = fWidth << 2; 62 63 for (int x = 0; x < size; x++) { 64 fScanLine[fScanlinePosition] = fPalette->ColorForIndex(string[x]); 65 fScanlinePosition++; 66 67 if (fScanlinePosition >= fWidth) { 68 if (fOutput->WriteAt(32 + (fRow * bpr), fScanLine, bpr) < bpr) return false; 69 fScanlinePosition = 0; 70 if (fInterlaced) { 71 fRow += gl_increment_pass_by[fPass]; 72 while (fRow >= fHeight) { 73 fPass++; 74 if (fPass > 3) return true; 75 fRow = gl_pass_starts_at[fPass]; 76 } 77 } else fRow++; 78 } 79 } 80 return true; 81 } 82 83 BPositionIO *fInput, *fOutput; 84 LoadPalette *fPalette; 85 bool fInterlaced; 86 int fPass, fRow, fWidth, fHeight; 87 88 unsigned char fOldCode[4096]; 89 int fOldCodeLength; 90 short fNewCode; 91 int fBits, fMaxCode, fCodeSize; 92 short fClearCode, fEndCode, fNextCode; 93 94 unsigned char *fTable[4096]; 95 short fEntrySize[4096]; 96 Memblock *fHeadMemblock; 97 98 int fBitCount; 99 unsigned int fBitBuffer; 100 unsigned char fByteCount; 101 unsigned char fByteBuffer[255]; 102 103 uint32 *fScanLine; 104 int fScanlinePosition; 105 }; 106 107 #endif 108 109