Line 0
Link Here
|
|
|
1 |
#include <stdio.h> |
2 |
#include <math.h> |
3 |
#include <string.h> |
4 |
#include <stdlib.h> |
5 |
#include "gd.h" |
6 |
|
7 |
/* Used only when debugging GIF compression code */ |
8 |
/* #define DEBUGGING_ENVARS */ |
9 |
|
10 |
#ifdef DEBUGGING_ENVARS |
11 |
|
12 |
static int verbose_set = 0; |
13 |
static int verbose; |
14 |
#define VERBOSE (verbose_set?verbose:set_verbose()) |
15 |
|
16 |
static int set_verbose(void) |
17 |
{ |
18 |
verbose = !!getenv("GIF_VERBOSE"); |
19 |
verbose_set = 1; |
20 |
return(verbose); |
21 |
} |
22 |
|
23 |
#else |
24 |
|
25 |
#define VERBOSE 0 |
26 |
|
27 |
#endif |
28 |
|
29 |
|
30 |
#define MAXCOLORMAPSIZE 256 |
31 |
|
32 |
#define TRUE 1 |
33 |
#define FALSE 0 |
34 |
|
35 |
#define CM_RED 0 |
36 |
#define CM_GREEN 1 |
37 |
#define CM_BLUE 2 |
38 |
|
39 |
#define MAX_LWZ_BITS 12 |
40 |
|
41 |
#define INTERLACE 0x40 |
42 |
#define LOCALCOLORMAP 0x80 |
43 |
#define BitSet(byte, bit) (((byte) & (bit)) == (bit)) |
44 |
|
45 |
#define ReadOK(file,buffer,len) (gdGetBuf(buffer, len, file) != 0) |
46 |
|
47 |
#define LM_to_uint(a,b) (((b)<<8)|(a)) |
48 |
|
49 |
/* We may eventually want to use this information, but def it out for now */ |
50 |
#if 0 |
51 |
static struct { |
52 |
unsigned int Width; |
53 |
unsigned int Height; |
54 |
unsigned char ColorMap[3][MAXCOLORMAPSIZE]; |
55 |
unsigned int BitPixel; |
56 |
unsigned int ColorResolution; |
57 |
unsigned int Background; |
58 |
unsigned int AspectRatio; |
59 |
} GifScreen; |
60 |
#endif |
61 |
|
62 |
static struct { |
63 |
int transparent; |
64 |
int delayTime; |
65 |
int inputFlag; |
66 |
int disposal; |
67 |
} Gif89 = { -1, -1, -1, 0 }; |
68 |
|
69 |
static int ReadColorMap (gdIOCtx *fd, int number, unsigned char (*buffer)[256]); |
70 |
static int DoExtension (gdIOCtx *fd, int label, int *Transparent); |
71 |
static int GetDataBlock (gdIOCtx *fd, unsigned char *buf); |
72 |
static int GetCode (gdIOCtx *fd, int code_size, int flag); |
73 |
static int LWZReadByte (gdIOCtx *fd, int flag, int input_code_size); |
74 |
|
75 |
static void ReadImage (gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace); /*1.4//, int ignore); */ |
76 |
|
77 |
int ZeroDataBlock; |
78 |
|
79 |
gdImagePtr |
80 |
gdImageCreateFromGif(FILE *fdFile) |
81 |
{ |
82 |
gdIOCtx *fd = gdNewFileCtx(fdFile); |
83 |
gdImagePtr im = 0; |
84 |
|
85 |
im = gdImageCreateFromGifCtx(fd); |
86 |
|
87 |
fd->free(fd); |
88 |
|
89 |
return im; |
90 |
} |
91 |
|
92 |
gdImagePtr |
93 |
gdImageCreateFromGifCtx(gdIOCtxPtr fd) |
94 |
{ |
95 |
int imageNumber; |
96 |
int BitPixel; |
97 |
int ColorResolution; |
98 |
int Background; |
99 |
int AspectRatio; |
100 |
int Transparent = (-1); |
101 |
unsigned char buf[16]; |
102 |
unsigned char c; |
103 |
unsigned char ColorMap[3][MAXCOLORMAPSIZE]; |
104 |
unsigned char localColorMap[3][MAXCOLORMAPSIZE]; |
105 |
int imw, imh; |
106 |
int useGlobalColormap; |
107 |
int bitPixel; |
108 |
int i; |
109 |
/*1.4//int imageCount = 0; */ |
110 |
char version[4]; |
111 |
|
112 |
gdImagePtr im = 0; |
113 |
ZeroDataBlock = FALSE; |
114 |
|
115 |
/*1.4//imageNumber = 1; */ |
116 |
if (! ReadOK(fd,buf,6)) { |
117 |
return 0; |
118 |
} |
119 |
if (strncmp((char *)buf,"GIF",3) != 0) { |
120 |
return 0; |
121 |
} |
122 |
strncpy(version, (char *)buf + 3, 3); |
123 |
version[3] = '\0'; |
124 |
|
125 |
if ((strcmp(version, "87a") != 0) && (strcmp(version, "89a") != 0)) { |
126 |
return 0; |
127 |
} |
128 |
if (! ReadOK(fd,buf,7)) { |
129 |
return 0; |
130 |
} |
131 |
BitPixel = 2<<(buf[4]&0x07); |
132 |
ColorResolution = (int) (((buf[4]&0x70)>>3)+1); |
133 |
Background = buf[5]; |
134 |
AspectRatio = buf[6]; |
135 |
|
136 |
if (BitSet(buf[4], LOCALCOLORMAP)) { /* Global Colormap */ |
137 |
if (ReadColorMap(fd, BitPixel, ColorMap)) { |
138 |
return 0; |
139 |
} |
140 |
} |
141 |
for (;;) { |
142 |
if (! ReadOK(fd,&c,1)) { |
143 |
return 0; |
144 |
} |
145 |
if (c == ';') { /* GIF terminator */ |
146 |
goto terminated; |
147 |
} |
148 |
|
149 |
if (c == '!') { /* Extension */ |
150 |
if (! ReadOK(fd,&c,1)) { |
151 |
return 0; |
152 |
} |
153 |
DoExtension(fd, c, &Transparent); |
154 |
continue; |
155 |
} |
156 |
|
157 |
if (c != ',') { /* Not a valid start character */ |
158 |
continue; |
159 |
} |
160 |
|
161 |
/*1.4//++imageCount; */ |
162 |
|
163 |
if (! ReadOK(fd,buf,9)) { |
164 |
return 0; |
165 |
} |
166 |
|
167 |
useGlobalColormap = ! BitSet(buf[8], LOCALCOLORMAP); |
168 |
|
169 |
bitPixel = 1<<((buf[8]&0x07)+1); |
170 |
|
171 |
imw = LM_to_uint(buf[4],buf[5]); |
172 |
imh = LM_to_uint(buf[6],buf[7]); |
173 |
if (!(im = gdImageCreate(imw, imh))) { |
174 |
return 0; |
175 |
} |
176 |
im->interlace = BitSet(buf[8], INTERLACE); |
177 |
if (! useGlobalColormap) { |
178 |
if (ReadColorMap(fd, bitPixel, localColorMap)) { |
179 |
return 0; |
180 |
} |
181 |
ReadImage(im, fd, imw, imh, localColorMap, |
182 |
BitSet(buf[8], INTERLACE)); |
183 |
/*1.4//imageCount != imageNumber); */ |
184 |
} else { |
185 |
ReadImage(im, fd, imw, imh, |
186 |
ColorMap, |
187 |
BitSet(buf[8], INTERLACE)); |
188 |
/*1.4//imageCount != imageNumber); */ |
189 |
} |
190 |
if (Transparent != (-1)) { |
191 |
gdImageColorTransparent(im, Transparent); |
192 |
} |
193 |
goto terminated; |
194 |
} |
195 |
|
196 |
terminated: |
197 |
/* Terminator before any image was declared! */ |
198 |
if (!im) { |
199 |
return 0; |
200 |
} |
201 |
/* Check for open colors at the end, so |
202 |
we can reduce colorsTotal and ultimately |
203 |
BitsPerPixel */ |
204 |
for (i=((im->colorsTotal-1)); (i>=0); i--) { |
205 |
if (im->open[i]) { |
206 |
im->colorsTotal--; |
207 |
} else { |
208 |
break; |
209 |
} |
210 |
} |
211 |
return im; |
212 |
} |
213 |
|
214 |
static int |
215 |
ReadColorMap(gdIOCtx *fd, int number, unsigned char (*buffer)[256]) |
216 |
{ |
217 |
int i; |
218 |
unsigned char rgb[3]; |
219 |
|
220 |
|
221 |
for (i = 0; i < number; ++i) { |
222 |
if (! ReadOK(fd, rgb, sizeof(rgb))) { |
223 |
return TRUE; |
224 |
} |
225 |
buffer[CM_RED][i] = rgb[0] ; |
226 |
buffer[CM_GREEN][i] = rgb[1] ; |
227 |
buffer[CM_BLUE][i] = rgb[2] ; |
228 |
} |
229 |
|
230 |
|
231 |
return FALSE; |
232 |
} |
233 |
|
234 |
static int |
235 |
DoExtension(gdIOCtx *fd, int label, int *Transparent) |
236 |
{ |
237 |
static unsigned char buf[256]; |
238 |
|
239 |
switch (label) { |
240 |
case 0xf9: /* Graphic Control Extension */ |
241 |
(void) GetDataBlock(fd, (unsigned char*) buf); |
242 |
Gif89.disposal = (buf[0] >> 2) & 0x7; |
243 |
Gif89.inputFlag = (buf[0] >> 1) & 0x1; |
244 |
Gif89.delayTime = LM_to_uint(buf[1],buf[2]); |
245 |
if ((buf[0] & 0x1) != 0) |
246 |
*Transparent = buf[3]; |
247 |
|
248 |
while (GetDataBlock(fd, (unsigned char*) buf) != 0) |
249 |
; |
250 |
return FALSE; |
251 |
default: |
252 |
break; |
253 |
} |
254 |
while (GetDataBlock(fd, (unsigned char*) buf) != 0) |
255 |
; |
256 |
|
257 |
return FALSE; |
258 |
} |
259 |
|
260 |
static int |
261 |
GetDataBlock_(gdIOCtx *fd, unsigned char *buf) |
262 |
{ |
263 |
unsigned char count; |
264 |
|
265 |
if (! ReadOK(fd,&count,1)) { |
266 |
return -1; |
267 |
} |
268 |
|
269 |
ZeroDataBlock = count == 0; |
270 |
|
271 |
if ((count != 0) && (! ReadOK(fd, buf, count))) { |
272 |
return -1; |
273 |
} |
274 |
|
275 |
return count; |
276 |
} |
277 |
|
278 |
static int |
279 |
GetDataBlock(gdIOCtx *fd, unsigned char *buf) |
280 |
{ |
281 |
int rv; |
282 |
int i; |
283 |
|
284 |
rv = GetDataBlock_(fd,buf); |
285 |
if (VERBOSE) |
286 |
{ printf("[GetDataBlock returning %d",rv); |
287 |
if (rv > 0) |
288 |
{ printf(":"); |
289 |
for (i=0;i<rv;i++) printf(" %02x",buf[i]); |
290 |
} |
291 |
printf("]\n"); |
292 |
} |
293 |
return(rv); |
294 |
} |
295 |
|
296 |
static int |
297 |
GetCode_(gdIOCtx *fd, int code_size, int flag) |
298 |
{ |
299 |
static unsigned char buf[280]; |
300 |
static int curbit, lastbit, done, last_byte; |
301 |
int i, j, ret; |
302 |
unsigned char count; |
303 |
|
304 |
if (flag) { |
305 |
curbit = 0; |
306 |
lastbit = 0; |
307 |
done = FALSE; |
308 |
return 0; |
309 |
} |
310 |
|
311 |
if ( (curbit+code_size) >= lastbit) { |
312 |
if (done) { |
313 |
if (curbit >= lastbit) { |
314 |
/* Oh well */ |
315 |
} |
316 |
return -1; |
317 |
} |
318 |
buf[0] = buf[last_byte-2]; |
319 |
buf[1] = buf[last_byte-1]; |
320 |
|
321 |
if ((count = GetDataBlock(fd, &buf[2])) == 0) |
322 |
done = TRUE; |
323 |
|
324 |
last_byte = 2 + count; |
325 |
curbit = (curbit - lastbit) + 16; |
326 |
lastbit = (2+count)*8 ; |
327 |
} |
328 |
|
329 |
ret = 0; |
330 |
for (i = curbit, j = 0; j < code_size; ++i, ++j) |
331 |
ret |= ((buf[ i / 8 ] & (1 << (i % 8))) != 0) << j; |
332 |
|
333 |
curbit += code_size; |
334 |
return ret; |
335 |
} |
336 |
|
337 |
static int |
338 |
GetCode(gdIOCtx *fd, int code_size, int flag) |
339 |
{ |
340 |
int rv; |
341 |
|
342 |
rv = GetCode_(fd,code_size,flag); |
343 |
if (VERBOSE) printf("[GetCode(,%d,%d) returning %d]\n",code_size,flag,rv); |
344 |
return(rv); |
345 |
} |
346 |
|
347 |
#define STACK_SIZE ((1<<(MAX_LWZ_BITS))*2) |
348 |
static int |
349 |
LWZReadByte_(gdIOCtx *fd, int flag, int input_code_size) |
350 |
{ |
351 |
static int fresh = FALSE; |
352 |
int code, incode; |
353 |
static int code_size, set_code_size; |
354 |
static int max_code, max_code_size; |
355 |
static int firstcode, oldcode; |
356 |
static int clear_code, end_code; |
357 |
static int table[2][(1<< MAX_LWZ_BITS)]; |
358 |
static int stack[STACK_SIZE], *sp; |
359 |
register int i; |
360 |
|
361 |
if (flag) { |
362 |
set_code_size = input_code_size; |
363 |
code_size = set_code_size+1; |
364 |
clear_code = 1 << set_code_size ; |
365 |
end_code = clear_code + 1; |
366 |
max_code_size = 2*clear_code; |
367 |
max_code = clear_code+2; |
368 |
|
369 |
GetCode(fd, 0, TRUE); |
370 |
|
371 |
fresh = TRUE; |
372 |
|
373 |
for (i = 0; i < clear_code; ++i) { |
374 |
table[0][i] = 0; |
375 |
table[1][i] = i; |
376 |
} |
377 |
for (; i < (1<<MAX_LWZ_BITS); ++i) |
378 |
table[0][i] = table[1][0] = 0; |
379 |
|
380 |
sp = stack; |
381 |
|
382 |
return 0; |
383 |
} else if (fresh) { |
384 |
fresh = FALSE; |
385 |
do { |
386 |
firstcode = oldcode = |
387 |
GetCode(fd, code_size, FALSE); |
388 |
} while (firstcode == clear_code); |
389 |
return firstcode; |
390 |
} |
391 |
|
392 |
if (sp > stack) |
393 |
return *--sp; |
394 |
|
395 |
while ((code = GetCode(fd, code_size, FALSE)) >= 0) { |
396 |
if (code == clear_code) { |
397 |
for (i = 0; i < clear_code; ++i) { |
398 |
table[0][i] = 0; |
399 |
table[1][i] = i; |
400 |
} |
401 |
for (; i < (1<<MAX_LWZ_BITS); ++i) |
402 |
table[0][i] = table[1][i] = 0; |
403 |
code_size = set_code_size+1; |
404 |
max_code_size = 2*clear_code; |
405 |
max_code = clear_code+2; |
406 |
sp = stack; |
407 |
firstcode = oldcode = |
408 |
GetCode(fd, code_size, FALSE); |
409 |
return firstcode; |
410 |
} else if (code == end_code) { |
411 |
int count; |
412 |
unsigned char buf[260]; |
413 |
|
414 |
if (ZeroDataBlock) |
415 |
return -2; |
416 |
|
417 |
while ((count = GetDataBlock(fd, buf)) > 0) |
418 |
; |
419 |
|
420 |
if (count != 0) |
421 |
return -2; |
422 |
} |
423 |
|
424 |
incode = code; |
425 |
|
426 |
if (sp == (stack + STACK_SIZE)) { |
427 |
/* Bad compressed data stream */ |
428 |
return -1; |
429 |
} |
430 |
|
431 |
if (code >= max_code) { |
432 |
*sp++ = firstcode; |
433 |
code = oldcode; |
434 |
} |
435 |
|
436 |
while (code >= clear_code) { |
437 |
if (sp == (stack + STACK_SIZE)) { |
438 |
/* Bad compressed data stream */ |
439 |
return -1; |
440 |
} |
441 |
*sp++ = table[1][code]; |
442 |
if (code == table[0][code]) { |
443 |
/* Oh well */ |
444 |
} |
445 |
code = table[0][code]; |
446 |
} |
447 |
|
448 |
*sp++ = firstcode = table[1][code]; |
449 |
|
450 |
if ((code = max_code) <(1<<MAX_LWZ_BITS)) { |
451 |
table[0][code] = oldcode; |
452 |
table[1][code] = firstcode; |
453 |
++max_code; |
454 |
if ((max_code >= max_code_size) && |
455 |
(max_code_size < (1<<MAX_LWZ_BITS))) { |
456 |
max_code_size *= 2; |
457 |
++code_size; |
458 |
} |
459 |
} |
460 |
|
461 |
oldcode = incode; |
462 |
|
463 |
if (sp > stack) |
464 |
return *--sp; |
465 |
} |
466 |
return code; |
467 |
} |
468 |
|
469 |
static int |
470 |
LWZReadByte(gdIOCtx *fd, int flag, int input_code_size) |
471 |
{ |
472 |
int rv; |
473 |
|
474 |
rv = LWZReadByte_(fd,flag,input_code_size); |
475 |
if (VERBOSE) printf("[LWZReadByte(,%d,%d) returning %d]\n",flag,input_code_size,rv); |
476 |
return(rv); |
477 |
} |
478 |
|
479 |
static void |
480 |
ReadImage(gdImagePtr im, gdIOCtx *fd, int len, int height, unsigned char (*cmap)[256], int interlace) /*1.4//, int ignore) */ |
481 |
{ |
482 |
unsigned char c; |
483 |
int v; |
484 |
int xpos = 0, ypos = 0, pass = 0; |
485 |
int i; |
486 |
/* Stash the color map into the image */ |
487 |
for (i=0; (i<gdMaxColors); i++) { |
488 |
im->red[i] = cmap[CM_RED][i]; |
489 |
im->green[i] = cmap[CM_GREEN][i]; |
490 |
im->blue[i] = cmap[CM_BLUE][i]; |
491 |
im->open[i] = 1; |
492 |
} |
493 |
/* Many (perhaps most) of these colors will remain marked open. */ |
494 |
im->colorsTotal = gdMaxColors; |
495 |
/* |
496 |
** Initialize the Compression routines |
497 |
*/ |
498 |
if (! ReadOK(fd,&c,1)) { |
499 |
return; |
500 |
} |
501 |
if (LWZReadByte(fd, TRUE, c) < 0) { |
502 |
return; |
503 |
} |
504 |
|
505 |
/* |
506 |
** If this is an "uninteresting picture" ignore it. |
507 |
** REMOVED For 1.4 |
508 |
*/ |
509 |
/*if (ignore) { */ |
510 |
/* while (LWZReadByte(fd, FALSE, c) >= 0) */ |
511 |
/* ; */ |
512 |
/* return; */ |
513 |
/*} */ |
514 |
|
515 |
while ((v = LWZReadByte(fd,FALSE,c)) >= 0 ) { |
516 |
/* This how we recognize which colors are actually used. */ |
517 |
if (im->open[v]) { |
518 |
im->open[v] = 0; |
519 |
} |
520 |
gdImageSetPixel(im, xpos, ypos, v); |
521 |
++xpos; |
522 |
if (xpos == len) { |
523 |
xpos = 0; |
524 |
if (interlace) { |
525 |
switch (pass) { |
526 |
case 0: |
527 |
case 1: |
528 |
ypos += 8; break; |
529 |
case 2: |
530 |
ypos += 4; break; |
531 |
case 3: |
532 |
ypos += 2; break; |
533 |
} |
534 |
|
535 |
if (ypos >= height) { |
536 |
++pass; |
537 |
switch (pass) { |
538 |
case 1: |
539 |
ypos = 4; break; |
540 |
case 2: |
541 |
ypos = 2; break; |
542 |
case 3: |
543 |
ypos = 1; break; |
544 |
default: |
545 |
goto fini; |
546 |
} |
547 |
} |
548 |
} else { |
549 |
++ypos; |
550 |
} |
551 |
} |
552 |
if (ypos >= height) |
553 |
break; |
554 |
} |
555 |
|
556 |
fini: |
557 |
if (LWZReadByte(fd,FALSE,c)>=0) { |
558 |
/* Ignore extra */ |
559 |
} |
560 |
} |
561 |
|