gd_gif_in.c 14 KB

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