1 /*
2  * whirlgif.h
3  *
4  * Copyright (c) 1997,1998 by Hans Dinsen-Hansen
5  * Copyright (c) 1995,1996 by Kevin Kadow
6  * Copyright (c) 1990,1991,1992 by Mark Podlipec.
7  * All rights reserved.
8  *
9  * This software may be freely copied, modified and redistributed
10  * without fee provided that this copyright notice is preserved
11  * intact on all copies and modified copies.
12  *
13  * There is no warranty or other guarantee of fitness of this software.
14  * It is provided solely "as is". The author(s) disclaim(s) all
15  * responsibility and liability with respect to this software's usage
16  * or its effect upon hardware or computer systems.
17  *
18  * The Graphics Interchange format (c) is the Copyright property of
19  * Compuserve Incorporated.  Gif(sm) is a Service Mark property of
20  * Compuserve Incorporated.
21  *
22  */
23 
24 #define gif_encod_header
25 
26 #define DA_REV  3.02
27 
28 /* common includes */
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 #ifdef _USE_STRINGS_H
33 #include <strings.h>
34 #else
35 #include <string.h>
36 #endif
37 
38 #ifdef _FOPEN_TXT_OR_BIN
39 #define WRIBIN	"wb"
40 #define REATXT	"rt"
41 #define REABIN	"rb"
42 #else
43 /* Usually there is no need to distinguish between binary and txt */
44 #define WRIBIN	"w"
45 #define REATXT	"r"
46 #define REABIN	"r"
47 #endif
48 
49 #ifndef TRUE
50 #define TRUE 1
51 #endif
52 #ifndef FALSE
53 #define FALSE 0
54 #endif
55 
56 /* define constants and defaults */
57     /* Default amount of inter-frame time */
58 #define DEFAULT_TIME 10
59     /* If set to 1, Netscape 'loop' code will be added by default */
60 #define DEFAULT_LOOP 0
61     /* If set to 1, use the colormaps from all images, not just the first */
62 #define DEFAULT_USE_COLORMAP 0
63 
64     /* Used in calculating the transparent color */
65 #define TRANS_NONE 1
66 #define TRANS_RGB 2
67 #define TRANS_MAP 3
68 
69 #define DISP_NONE 0
70 #define DISP_NOT  1
71 #define DISP_BACK 2
72 #define DISP_PREV 3
73 #define DEFAULT_DISPOSAL DISP_NONE
74     /* set default disposal method here to any of the DISP_XXXX values */
75 
76 #define BIGSTRING 256
77 #define MAXVAL  4100        /* maxval of lzw coding size */
78 #define MAXVALP 4200
79 #define TERMIN 'T'
80 #define LOOKUP 'L'
81 #define SEARCH 'S'
82 #define noOfArrays 20
83 /* defines the amount of memory set aside in the encoding for the
84  * LOOKUP type nodes; for a 256 color GIF, the number of LOOKUP
85  * nodes will be <= noOfArrays, for a 128 color GIF the number of
86  * LOOKUP nodes will be <= 2 * noOfArrays, etc.  */
87 
88 /* define shorthand for various types */
89 #define LONG int
90 #define ULONG unsigned int
91 #define BYTE char
92 #define UBYTE unsigned char
93 #define SHORT short
94 #define USHORT unsigned short
95 #define WORD short int
96 #define UWORD unsigned short int
97 
98 int chainlen = 0, maxchainlen = 0, nodecount = 0, lookuptypes = 0, nbits;
99 
100 short need = 8;
101 
102 
103 
104 
105 unsigned int debugFlag, verbose;
106 int count;
107 
108 /* definition of various structures */
109 typedef struct Transparency {
110   int type;
111   UBYTE valid;
112   UBYTE map;
113   UBYTE red;
114   UBYTE green;
115   UBYTE blue;
116   } Transparency;
117 
118 typedef struct Global {
119   Transparency trans;
120   int left;
121   int top;
122   unsigned int time;
123   unsigned short disposal;
124   } Global;
125 
126 typedef struct GifScreenHdr {
127   int width;
128   int height;
129   UBYTE m;
130   UBYTE cres;
131   UBYTE pixbits;
132   UBYTE bc;
133   UBYTE aspect;
134  } GifScreenHdr;
135 
136 typedef union GifColor {
137   struct cmap {
138     UBYTE red;
139     UBYTE green;
140     UBYTE blue;
141     UBYTE pad;
142    } cmap;
143   ULONG pixel;
144  } GifColor;
145 
146 typedef struct GifImageHdr {
147   int left;
148   int top;
149   int width;
150   int height;
151   UBYTE m;
152   UBYTE i;
153   UBYTE pixbits;
154   UBYTE reserved;
155  } GifImageHdr;
156 
157 typedef struct GifTable {
158   UBYTE valid;
159   UBYTE data;
160   UBYTE first;
161   UBYTE res;
162   int last;
163  } GifTable;
164 
165 typedef struct GifTree {
166   char typ;             /* terminating, lookup, or search */
167   int code;             /* the code to be output */
168   UBYTE ix;             /* the color map index */
169   struct GifTree **node, *nxt, *alt;
170 } GifTree;
171 
172 GifTree *empty[256], GifRoot = {LOOKUP, 0, 0, empty, NULL, NULL},
173 *topNode, *baseNode, **nodeArray, **lastArray;
174 
175 /* define inline functions */
176 #define GifPutShort(i, fout)    {fputc(i&0xff, fout); fputc(i>>8, fout);}
177 #define GifGetShort(fin)        (Xgetc(fin) | Xgetc(fin)<<8)
178 
179 /* forward declaration of the functions  */
180 void  CalcTrans();
181 void  GifAddToTable();
182 void  GifClearTable();
183 void  GifComment();
184 void  GifDecode();
185 void  GifEncode();
186 ULONG GifGetCode();
187 void  GifGetNextEntry();
188 void  GifLoop();
189 void  GifReadFile();
190 void  GifScreenHeader();
191 UBYTE *GifSendData();
192 void  ReadImageHeader();
193 void  SetOffset();
194 void  TheEnd();
195 void  TheEnd1();
196 void  Usage();
197 void  WriteImageHeader();
198 UBYTE Xgetc();
199 void ClearTree(int cc, GifTree *root);
200 char *AddCodeToBuffer(int code, short n, char *buf);
201