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