8 #define ARGS(alist) alist
10 #define ARGS(alist) ()
18 #define put_byte(A) (*put_b)((byte)(A)); Nbyte++
20 typedef unsigned char byte;
22 static long HashTab [HSIZE];
23 static int CodeTab [HSIZE];
34 static void (*put_b) ARGS((byte));
36 static
void output ARGS((
int));
37 static
void char_init();
38 static
void char_out ARGS((
int));
39 static
void char_flush();
40 static
void put_short ARGS((
int));
63 long GIFencode(Width, Height, Ncol, R, G, B, ScLine, get_scline, pb)
64 int Width, Height, Ncol;
65 byte R[], G[], B[], ScLine[];
66 void (*get_scline) ARGS((
int,
int, byte *)), (*pb) ARGS((byte));
69 int ncol, i, x, y, disp, Code, K;
74 if (Width <= 0 || Width > 4096 || Height <= 0 || Height > 4096) {
76 "\nGIFencode: incorrect image size: %d x %d\n", Width, Height);
80 if (Ncol <= 0 || Ncol > 256) {
81 fprintf(stderr,
"\nGIFencode: wrong number of colors: %d\n", Ncol);
94 if (Ncol > 2) BitsPixel = 2;
95 if (Ncol > 4) BitsPixel = 3;
96 if (Ncol > 8) BitsPixel = 4;
97 if (Ncol > 16) BitsPixel = 5;
98 if (Ncol > 32) BitsPixel = 6;
99 if (Ncol > 64) BitsPixel = 7;
100 if (Ncol > 128) BitsPixel = 8;
102 ncol = 1 << BitsPixel;
103 IniCodeSize = BitsPixel;
104 if (BitsPixel <= 1) IniCodeSize = 2;
120 K |= (BitsPixel - 1);
126 for (i=0; i<Ncol; i++) {
131 for (; i<ncol; i++) {
143 put_byte(IniCodeSize);
147 CurCodeSize = ++IniCodeSize;
148 CurMaxCode = (1 << (IniCodeSize)) - 1;
149 ClearCode = (1 << (IniCodeSize - 1));
150 EOFCode = ClearCode + 1;
151 FreeCode = ClearCode + 2;
153 for (y=0; y<Height; y++) {
154 (*get_scline)(y, Width, ScLine);
160 CodeK = ((long) K << BITS) + Code;
161 i = (K << SHIFT) ^ Code;
163 if (HashTab[i] == CodeK) {
167 else if (HashTab[i] < 0 )
171 if (i == 0) disp = 1;
177 if (HashTab[i] == CodeK) {
189 if (FreeCode < THELIMIT) {
190 CodeTab[i] = FreeCode++;
207 static unsigned long cur_accum;
210 static char accum[256];
211 static unsigned long masks[] = { 0x0000,
212 0x0001, 0x0003, 0x0007, 0x000F,
213 0x001F, 0x003F, 0x007F, 0x00FF,
214 0x01FF, 0x03FF, 0x07FF, 0x0FFF,
215 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };
226 static void output(code)
231 cur_accum &= masks[cur_bits];
233 cur_accum |= ((long)code << cur_bits);
236 cur_bits += CurCodeSize;
237 while( cur_bits >= 8 ) {
238 char_out( (
unsigned int) (cur_accum & 0xFF) );
245 if (code == ClearCode ) {
246 memset((
char *) HashTab, -1,
sizeof(HashTab));
247 FreeCode = ClearCode + 2;
248 CurCodeSize = IniCodeSize;
249 CurMaxCode = (1 << (IniCodeSize)) - 1;
254 if (FreeCode > CurMaxCode ) {
256 if ( CurCodeSize == BITS )
257 CurMaxCode = THELIMIT;
259 CurMaxCode = (1 << (CurCodeSize)) - 1;
264 if( code == EOFCode ) {
265 while( cur_bits > 0 ) {
266 char_out( (
unsigned int)(cur_accum & 0xff) );
274 static void char_init()
281 static void char_out(c)
284 accum[a_count++] = c;
289 static void char_flush()
293 if (a_count == 0)
return;
295 for (i=0; i<a_count; i++) {
301 static void put_short(word)
304 put_byte(word & 0xFF);
305 put_byte((word>>8) & 0xFF);