-
Notifications
You must be signed in to change notification settings - Fork 3
/
gif.cpp
363 lines (307 loc) · 11.9 KB
/
gif.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Fli2gif
// Copyright (c) 1996, Jeff Lawson
// All rights reserved.
// See LICENSE for distribution information.
// "The Graphics Interchange Format(c) is the Copyright property of
// CompuServe Incorporated. GIF(sm) is a Service Mark property of
// CompuServe Incorporated."
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "gif.hpp"
static int colorstobpp(int colors);
static int sqr(int x);
static int GIFNextPixel(struct ImageType image, int &curx, int &cury,
long &CountDown, int &Pass);
static void Putword(short int w, FILE *fp);
static void compress(FILE *outfile, struct ImageType image);
//###########################################################################
//###########################################################################
//###########################################################################
static UBYTE reversebits(UBYTE value)
{
// Borland C++ assigns bits from low to high.
// GNU C++ assigns bits from high to low.
union {
struct {unsigned test:1, filler:7;} bitfield;
UBYTE value;
} test;
test.bitfield.test = 1;
test.bitfield.filler = 0;
if (test.value & 0x80) {
UBYTE newvalue = 0;
int i;
for (i = 0; i < 8; i++) {
newvalue = (newvalue << 1) | (value & 1);
value >>= 1;
}
value = newvalue;
}
return(value);
};
//###########################################################################
//###########################################################################
//###########################################################################
void WriteGif (struct GifType gif, FILE *fp)
{
int i;
// []------------------------------------[]
// | Begin the setup of the GIF headers |
// []------------------------------------[]
/* Write the Magic header */
fwrite("GIF89a", sizeof(char), 6, fp);
/* Write out the screen width and height */
Putword(gif.width, fp);
Putword(gif.height, fp);
/* Write out a bit-field */
union {
struct {unsigned GlobalSize:3, Sorted:1, ColorResolution:3,
GlobalPalette:1;} field;
UBYTE value;
} infobyte;
infobyte.field.GlobalSize = infobyte.field.ColorResolution = \
colorstobpp(gif.numcolors) - 1;
infobyte.field.GlobalPalette = (gif.palette != NULL);
infobyte.field.Sorted = FALSE;
fputc(reversebits(infobyte.value), fp);
/* Write out the Background colour */
fputc(gif.background, fp);
/* Pixel Aspect Ratio */
fputc(0, fp); // no aspect ratio specified
/* Write out the Global Colour Map, if necessary */
if (infobyte.field.GlobalPalette) {
int storedcolors = 1 << (infobyte.field.GlobalSize + 1);
for (i = 0; i < storedcolors; i++) {
fputc(gif.palette[i].red, fp);
fputc(gif.palette[i].green, fp);
fputc(gif.palette[i].blue, fp);
}
}
// []-------------------------------------------[]
// | Write out the magical Netscape loop chunk |
// []-------------------------------------------[]
if (gif.numimages) {
fputc('!', fp);
fputc(0xFF, fp);
fputc(11, fp);
fwrite("NETSCAPE2.0", sizeof(char), 11, fp);
fputc(3, fp);
fputc(1, fp);
Putword(0, fp); // loop count
fputc(0, fp);
}
// []-----------------------------------------[]
// | Write out each of the individual images |
// []-----------------------------------------[]
for (i = 0; i < (int) gif.numimages; i++) {
printf("%i..", i + 1);
/* Write out the control extension chunk */
union {
struct {unsigned TransFlag:1, InputFlag:1,
Dispose:3, Reserved:3;} field;
UBYTE value;
} infobyte3;
infobyte3.field.TransFlag = (gif.images[i].transparentflag != 0);
infobyte3.field.InputFlag = 0;
infobyte3.field.Dispose = 2; // erase by background
infobyte3.field.Reserved = 0;
fputc('!', fp);
fputc(0xF9, fp);
fputc(4, fp);
fputc(reversebits(infobyte3.value), fp);
Putword(gif.images[i].waitdelay, fp);
fputc(gif.images[i].transparentcolor, fp);
fputc(0, fp);
/* Write the Image header */
fputc(',', fp);
Putword(gif.images[i].leftcol, fp);
Putword(gif.images[i].toprow, fp);
Putword(gif.images[i].width, fp);
Putword(gif.images[i].height, fp);
/* Write out the local image bitfield */
union {
struct {unsigned LocalSize:3, reserved:2, Sorted:1,
Interlaced:1, LocalPalette:1;} field;
UBYTE value;
} infobyte2;
infobyte2.field.LocalPalette = (gif.images[i].numcolors != 0);
infobyte2.field.Interlaced = (gif.images[i].interlaced != 0);
infobyte2.field.Sorted = FALSE;
if (infobyte2.field.LocalPalette) {
infobyte2.field.LocalSize =
colorstobpp(gif.images[i].numcolors) - 1;
} else {
infobyte2.field.LocalSize = colorstobpp(gif.numcolors) - 1;
}
infobyte2.field.reserved = 0;
fputc(reversebits(infobyte2.value), fp);
/* Store the local palette, if one exists */
if (gif.images[i].numcolors) {
int storedcolors = 1 << (infobyte2.field.LocalSize + 1);
for (int j = 0; j < storedcolors; j++) {
fputc(gif.images[i].palette[j].red, fp);
fputc(gif.images[i].palette[j].green, fp);
fputc(gif.images[i].palette[j].blue, fp);
}
}
/* Go and actually compress the data */
compress(fp, gif.images[i]);
/* Write out a Zero-length packet (to end the series) */
fputc(0, fp);
}
// []------------------------------[]
// | We're done with the GIF file |
// []------------------------------[]
/* Write the GIF file terminator */
fputc(';', fp);
}
//###########################################################################
//###########################################################################
//###########################################################################
static int colorstobpp(int colors)
{
if (colors <= 2) return 1;
else if (colors <= 4) return 2;
else if (colors <= 8) return 3;
else if (colors <= 16) return 4;
else if (colors <= 32) return 5;
else if (colors <= 64) return 6;
else if (colors <= 128) return 7;
else return 8;
}
//###########################################################################
//###########################################################################
//###########################################################################
static int sqr(int x)
{
return x*x;
}
//###########################################################################
//###########################################################################
//###########################################################################
/* Return the next pixel from the image */
static int GIFNextPixel(struct ImageType image, int &curx, int &cury,
long &CountDown, int &Pass)
{
if (CountDown == 0) return EOF;
CountDown--;
/* Read in the actual pixel */
int r = image.pixels[cury * image.width + curx];
/*
* If we are at the end of a scan line, set curx back to the beginning
* If we are interlaced, bump the cury to the appropriate spot,
* otherwise, just increment it.
*/
if (++curx == (int) image.width) {
curx = 0;
if (!image.interlaced) cury++;
else if (Pass == 0) {
cury += 8;
if (cury >= (int) image.height) {Pass++; cury = 4;}
} else if (Pass == 1) {
cury += 8;
if (cury >= (int) image.height) {Pass++; cury = 2;}
} else if (Pass == 2) {
cury += 4;
if (cury >= (int) image.height) {Pass++; cury = 1;}
} else if (Pass == 3) {
cury += 2;
}
}
return(r);
}
//###########################################################################
//###########################################################################
//###########################################################################
/*
* Write out a 16bit word to the GIF file in Intel lo-hi ordering.
*/
static void Putword(short int w, FILE *fp)
{
fputc( w & 0xff, fp) ;
fputc( (w & 0xff00) >> 8, fp) ;
}
//###########################################################################
//###########################################################################
//###########################################################################
/*
* Algorithm: use open addressing double hashing (no chaining) on the
* prefix code / next character combination. We do a variant of Knuth's
* algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
* secondary probe. Here, the modular division first probe is gives way
* to a faster exclusive-or manipulation. Also do block compression with
* an adaptive reset, whereby the code table is cleared when the compression
* ratio decreases, but after the table fills. The variable-length output
* codes are re-sized at this point, and a special CLEAR code is generated
* for the decompressor.
*/
static void compress(FILE *outfile, struct ImageType image)
{
/* Set up the image related variables */
int curx = 0, cury = 0, Pass = 0;
long CountDown = (long)image.width * (long)image.height;
int Resolution;
//Resolution = colorstobpp(image.numcolors);
Resolution = 9;
int init_bits = (Resolution <= 1 ? 2 : Resolution);
PacketStream gifstream(outfile, init_bits);
/* Write out the initial code size */
fputc(init_bits - 1, outfile);
/* Set hash code range bound */
register int hshift = 0;
long fcode;
for (fcode = HSIZE; fcode < 65536L; fcode *= 2L) hshift++;
hshift = 8 - hshift;
code_int hsize_reg = HSIZE;
/* clear hash table */
static count_int htab[HSIZE];
static unsigned short codetab[HSIZE];
memset(htab, -1, HSIZE * sizeof(count_int));
gifstream.output(gifstream.ClearCode);
/* Main compression loop */
code_int ent = GIFNextPixel(image, curx, cury, CountDown, Pass);
while (TRUE) {
/* read in the next pixel to process */
int c = GIFNextPixel(image, curx, cury, CountDown, Pass);
if (c == EOF) break;
/* do the primary XOR hash */
fcode = ((long) c << gifstream.maxbits) + ent;
code_int i = ((code_int) c << hshift) ^ ent;
if ((long) htab[i] == fcode) {
ent = codetab[i];
continue;
} else if ((long)htab[i] >= 0) { /* not empty slot */
/* secondary hash */
int breakout = FALSE;
code_int disp = hsize_reg - i;
if (i == 0) disp = 1;
do {
if ((i -= disp) < 0) i += hsize_reg;
if ((long)htab[i] == fcode) {
ent = codetab[i];
breakout = TRUE;
break;
}
} while ((long)htab[i] > 0);
if (breakout) continue;
}
gifstream.output(ent);
ent = c;
if ((unsigned) gifstream.free_ent < (unsigned) gifstream.maxmaxcode) {
/* code -> hashtable */
codetab[i] = gifstream.free_ent++;
htab[i] = fcode;
} else {
/* table clear for block compress */
memset(htab, -1, HSIZE * sizeof(count_int));
gifstream.output(gifstream.ClearCode);
}
}
/* Put out the final code. */
gifstream.output(ent);
gifstream.output(gifstream.EOFCode);
}
//###########################################################################
//###########################################################################
//###########################################################################