1 /* GIF reading routines based on those in pbmplus:ppm/giftoppm.c, bearing
2 * following copyright notice:
5 /* +-------------------------------------------------------------------+ */
6 /* | Copyright 1990, David Koblas. | */
7 /* | Permission to use, copy, modify, and distribute this software | */
8 /* | and its documentation for any purpose and without fee is hereby | */
9 /* | granted, provided that the above copyright notice appear in all | */
10 /* | copies and that both that copyright notice and this permission | */
11 /* | notice appear in supporting documentation. This software is | */
12 /* | provided "as is" without express or implied warranty. | */
13 /* +-------------------------------------------------------------------+ */
20 extern long *FDECL(alloc, (unsigned int));
23 #define PPM_ASSIGN(p,red,grn,blu) do { (p).r = (red); (p).g = (grn); (p).b = (blu); } while ( 0 )
25 #define MAX_LWZ_BITS 12
27 #define INTERLACE 0x40
28 #define LOCALCOLORMAP 0x80
29 #define BitSet(byte, bit) (((byte) & (bit)) == (bit))
31 #define ReadOK(file,buffer,len) (fread((genericptr_t)buffer, (int)len, 1, file) != 0)
33 #define LM_to_uint(a,b) (((b)<<8)|(a))
50 } Gif89 = { -1, -1, -1, 0 };
52 int ZeroDataBlock = FALSE;
54 static FILE *gif_file;
55 static int tiles_across, tiles_down, curr_tiles_across, curr_tiles_down;
57 static unsigned char input_code_size;
59 static int FDECL(GetDataBlock, (FILE *fd, unsigned char *buf));
60 static void FDECL(DoExtension, (FILE *fd, int label));
61 static boolean FDECL(ReadColorMap, (FILE *fd, int number));
62 static void FDECL(read_header, (FILE *fd));
63 static int FDECL(GetCode, (FILE *fd, int code_size, int flag));
64 static int FDECL(LWZReadByte, (FILE *fd, int flag, int input_code_size));
65 static void FDECL(ReadInterleavedImage, (FILE *fd, int len, int height));
66 static void FDECL(ReadTileStrip, (FILE *fd, int len));
68 /* These should be in gif.h, but there isn't one. */
69 boolean FDECL(fopen_gif_file, (const char *, const char *));
70 boolean FDECL(read_gif_tile, (pixel(*)[]));
71 int NDECL(fclose_gif_file);
80 if (!ReadOK(fd,&count,1)) {
81 Fprintf(stderr, "error in getting DataBlock size\n");
85 ZeroDataBlock = (count == 0);
87 if ((count != 0) && (!ReadOK(fd, buf, count))) {
88 Fprintf(stderr, "error in reading DataBlock\n");
96 DoExtension(fd, label)
100 static char buf[256];
104 case 0x01: /* Plain Text Extension */
105 str = "Plain Text Extension";
107 if (GetDataBlock(fd, (unsigned char*) buf) == 0)
110 lpos = LM_to_uint(buf[0], buf[1]);
111 tpos = LM_to_uint(buf[2], buf[3]);
112 width = LM_to_uint(buf[4], buf[5]);
113 height = LM_to_uint(buf[6], buf[7]);
116 foreground = buf[10];
117 background = buf[11];
119 while (GetDataBlock(fd, (unsigned char*) buf) != 0) {
120 PPM_ASSIGN(image[ypos][xpos],
131 case 0xff: /* Application Extension */
132 str = "Application Extension";
134 case 0xfe: /* Comment Extension */
135 str = "Comment Extension";
136 while (GetDataBlock(fd, (unsigned char*) buf) != 0) {
137 Fprintf(stderr, "gif comment: %s\n", buf );
140 case 0xf9: /* Graphic Control Extension */
141 str = "Graphic Control Extension";
142 (void) GetDataBlock(fd, (unsigned char*) buf);
143 Gif89.disposal = (buf[0] >> 2) & 0x7;
144 Gif89.inputFlag = (buf[0] >> 1) & 0x1;
145 Gif89.delayTime = LM_to_uint(buf[1],buf[2]);
146 if ((buf[0] & 0x1) != 0)
147 Gif89.transparent = buf[3];
149 while (GetDataBlock(fd, (unsigned char*) buf) != 0)
154 Sprintf(buf, "UNKNOWN (0x%02x)", label);
158 Fprintf(stderr, "got a '%s' extension\n", str);
160 while (GetDataBlock(fd, (unsigned char*) buf) != 0)
166 ReadColorMap(fd,number)
171 unsigned char rgb[3];
173 for (i = 0; i < number; ++i) {
174 if (!ReadOK(fd, rgb, sizeof(rgb))) {
178 ColorMap[CM_RED][i] = rgb[0] ;
179 ColorMap[CM_GREEN][i] = rgb[1] ;
180 ColorMap[CM_BLUE][i] = rgb[2] ;
182 colorsinmap = number;
187 * Read gif header, including colormaps. We expect only one image per
188 * file, so if that image has a local colormap, overwrite the global one.
194 unsigned char buf[16];
198 if (!ReadOK(fd,buf,6)) {
199 Fprintf(stderr, "error reading magic number\n");
203 if (strncmp((genericptr_t)buf,"GIF",3) != 0) {
204 Fprintf(stderr, "not a GIF file\n");
208 (void) strncpy(version, (char *)buf + 3, 3);
211 if ((strcmp(version, "87a") != 0) && (strcmp(version, "89a") != 0)) {
212 Fprintf(stderr, "bad version number, not '87a' or '89a'\n");
216 if (!ReadOK(fd,buf,7)) {
217 Fprintf(stderr, "failed to read screen descriptor\n");
221 GifScreen.Width = LM_to_uint(buf[0],buf[1]);
222 GifScreen.Height = LM_to_uint(buf[2],buf[3]);
223 GifScreen.Colors = 2<<(buf[4]&0x07);
224 GifScreen.ColorResolution = (((buf[4]&0x70)>>3)+1);
225 GifScreen.Background = buf[5];
226 GifScreen.AspectRatio = buf[6];
228 if (BitSet(buf[4], LOCALCOLORMAP)) { /* Global Colormap */
229 if (!ReadColorMap(fd, GifScreen.Colors)) {
230 Fprintf(stderr, "error reading global colormap\n");
235 if (GifScreen.AspectRatio != 0 && GifScreen.AspectRatio != 49) {
236 Fprintf(stderr, "warning - non-square pixels\n");
240 if (!ReadOK(fd,&c,1)) {
241 Fprintf(stderr, "EOF / read error on image data\n");
245 if (c == ';') { /* GIF terminator */
249 if (c == '!') { /* Extension */
250 if (!ReadOK(fd,&c,1)) {
252 "EOF / read error on extension function code\n");
255 DoExtension(fd, (int)c);
259 if (c != ',') { /* Not a valid start character */
261 "bogus character 0x%02x, ignoring\n", (int) c);
265 if (!ReadOK(fd,buf,9)) {
266 Fprintf(stderr, "couldn't read left/top/width/height\n");
270 if (BitSet(buf[8], LOCALCOLORMAP)) {
271 /* replace global color map with local */
272 GifScreen.Colors = 1<<((buf[8]&0x07)+1);
273 if (!ReadColorMap(fd, GifScreen.Colors)) {
274 Fprintf(stderr, "error reading local colormap\n");
279 if (GifScreen.Width != LM_to_uint(buf[4],buf[5])) {
280 Fprintf(stderr, "warning: widths don't match\n");
281 GifScreen.Width = LM_to_uint(buf[4],buf[5]);
283 if (GifScreen.Height != LM_to_uint(buf[6],buf[7])) {
284 Fprintf(stderr, "warning: heights don't match\n");
285 GifScreen.Height = LM_to_uint(buf[6],buf[7]);
287 GifScreen.Interlace = BitSet(buf[8], INTERLACE);
293 GetCode(fd, code_size, flag)
298 static unsigned char buf[280];
299 static int curbit, lastbit, done, last_byte;
310 if ((curbit+code_size) >= lastbit) {
312 if (curbit >= lastbit)
313 Fprintf(stderr, "ran off the end of my bits\n");
316 buf[0] = buf[last_byte-2];
317 buf[1] = buf[last_byte-1];
319 if ((count = GetDataBlock(fd, &buf[2])) == 0)
322 last_byte = 2 + count;
323 curbit = (curbit - lastbit) + 16;
324 lastbit = (2+count)*8 ;
328 for (i = curbit, j = 0; j < code_size; ++i, ++j)
329 ret |= ((buf[ i / 8 ] & (1 << (i % 8))) != 0) << j;
337 LWZReadByte(fd, flag, input_code_size)
342 static int fresh = FALSE;
344 static int code_size, set_code_size;
345 static int max_code, max_code_size;
346 static int firstcode, oldcode;
347 static int clear_code, end_code;
348 static int table[2][(1<< MAX_LWZ_BITS)];
349 static int stack[(1<<(MAX_LWZ_BITS))*2], *sp;
353 set_code_size = input_code_size;
354 code_size = set_code_size+1;
355 clear_code = 1 << set_code_size ;
356 end_code = clear_code + 1;
357 max_code_size = 2*clear_code;
358 max_code = clear_code+2;
360 (void) GetCode(fd, 0, TRUE);
364 for (i = 0; i < clear_code; ++i) {
368 for (; i < (1<<MAX_LWZ_BITS); ++i)
369 table[0][i] = table[1][0] = 0;
377 firstcode = oldcode = GetCode(fd, code_size, FALSE);
378 } while (firstcode == clear_code);
385 while ((code = GetCode(fd, code_size, FALSE)) >= 0) {
386 if (code == clear_code) {
387 for (i = 0; i < clear_code; ++i) {
391 for (; i < (1<<MAX_LWZ_BITS); ++i)
392 table[0][i] = table[1][i] = 0;
393 code_size = set_code_size+1;
394 max_code_size = 2*clear_code;
395 max_code = clear_code+2;
397 firstcode = oldcode = GetCode(fd, code_size, FALSE);
399 } else if (code == end_code) {
401 unsigned char buf[260];
406 while ((count = GetDataBlock(fd, buf)) > 0)
411 "missing EOD in data stream (common occurrence)\n");
417 if (code >= max_code) {
422 while (code >= clear_code) {
423 *sp++ = table[1][code];
424 if (code == table[0][code]) {
425 Fprintf(stderr, "circular table entry BIG ERROR\n");
428 code = table[0][code];
431 *sp++ = firstcode = table[1][code];
433 if ((code = max_code) <(1<<MAX_LWZ_BITS)) {
434 table[0][code] = oldcode;
435 table[1][code] = firstcode;
437 if ((max_code >= max_code_size) &&
438 (max_code_size < (1<<MAX_LWZ_BITS))) {
454 ReadInterleavedImage(fd, len, height)
459 int xpos = 0, ypos = 0, pass = 0;
461 while ((v = LWZReadByte(fd,FALSE,(int)input_code_size)) >= 0 ) {
462 PPM_ASSIGN(image[ypos][xpos], ColorMap[CM_RED][v],
463 ColorMap[CM_GREEN][v], ColorMap[CM_BLUE][v]);
478 if (ypos >= height) {
497 if (LWZReadByte(fd,FALSE,(int)input_code_size)>=0)
498 Fprintf(stderr, "too much input data, ignoring extra...\n");
503 ReadTileStrip(fd,len)
508 int xpos = 0, ypos = 0;
510 while ((v = LWZReadByte(fd,FALSE,(int)input_code_size)) >= 0 ) {
511 PPM_ASSIGN(image[ypos][xpos], ColorMap[CM_RED][v],
512 ColorMap[CM_GREEN][v], ColorMap[CM_BLUE][v]);
528 fopen_gif_file(filename, type)
529 const char *filename;
534 if (strcmp(type, RDBMODE)) {
535 Fprintf(stderr, "using reading routine for non-reading?\n");
538 gif_file = fopen(filename, type);
539 if (gif_file == (FILE *)0) {
540 Fprintf(stderr, "cannot open gif file %s\n", filename);
544 read_header(gif_file);
545 if (GifScreen.Width % TILE_X) {
546 Fprintf(stderr, "error: width %d not divisible by %d\n",
547 GifScreen.Width, TILE_X);
550 tiles_across = GifScreen.Width / TILE_X;
551 curr_tiles_across = 0;
552 if (GifScreen.Height % TILE_Y) {
553 Fprintf(stderr, "error: height %d not divisible by %d\n",
554 GifScreen.Height, TILE_Y);
555 /* exit(EXIT_FAILURE) */;
557 tiles_down = GifScreen.Height / TILE_Y;
560 if (GifScreen.Interlace) {
561 /* sigh -- hope this doesn't happen on micros */
562 image = (pixel **)alloc(GifScreen.Height * sizeof(pixel *));
563 for (i = 0; i < GifScreen.Height; i++) {
564 image[i] = (pixel *) alloc(GifScreen.Width * sizeof(pixel));
567 image = (pixel **)alloc(TILE_Y * sizeof(pixel *));
568 for (i = 0; i < TILE_Y; i++) {
569 image[i] = (pixel *) alloc(GifScreen.Width * sizeof(pixel));
574 ** Initialize the Compression routines
576 if (!ReadOK(gif_file,&input_code_size,1)) {
577 Fprintf(stderr, "EOF / read error on image data\n");
581 if (LWZReadByte(gif_file, TRUE, (int)input_code_size) < 0) {
582 Fprintf(stderr, "error reading image\n");
586 /* read first section */
587 if (GifScreen.Interlace) {
588 ReadInterleavedImage(gif_file,
589 GifScreen.Width,GifScreen.Height);
591 ReadTileStrip(gif_file,GifScreen.Width);
596 /* Read a tile. Returns FALSE when there are no more tiles */
598 read_gif_tile(pixels)
599 pixel (*pixels)[TILE_X];
603 if (curr_tiles_down >= tiles_down) return FALSE;
604 if (curr_tiles_across == tiles_across) {
605 curr_tiles_across = 0;
607 if (curr_tiles_down >= tiles_down) return FALSE;
608 if (!GifScreen.Interlace)
609 ReadTileStrip(gif_file,GifScreen.Width);
611 if (GifScreen.Interlace) {
612 for (j = 0; j < TILE_Y; j++) {
613 for (i = 0; i < TILE_X; i++) {
614 pixels[j][i] = image[curr_tiles_down*TILE_Y + j]
615 [curr_tiles_across*TILE_X + i];
619 for (j = 0; j < TILE_Y; j++) {
620 for (i = 0; i < TILE_X; i++) {
621 pixels[j][i] = image[j][curr_tiles_across*TILE_X + i];
627 /* check for "filler" tile */
628 for (j = 0; j < TILE_Y; j++) {
629 for (i = 0; i < TILE_X && i < 4; i += 2) {
630 if (pixels[j][i].r != ColorMap[CM_RED][0] ||
631 pixels[j][i].g != ColorMap[CM_GREEN][0] ||
632 pixels[j][i].b != ColorMap[CM_BLUE][0] ||
633 pixels[j][i+1].r != ColorMap[CM_RED][1] ||
634 pixels[j][i+1].g != ColorMap[CM_GREEN][1] ||
635 pixels[j][i+1].b != ColorMap[CM_BLUE][1])
647 if (GifScreen.Interlace) {
648 for (i = 0; i < GifScreen.Height; i++) {
649 free((genericptr_t)image[i]);
651 free((genericptr_t)image);
653 for (i = 0; i < TILE_Y; i++) {
654 free((genericptr_t)image[i]);
656 free((genericptr_t)image);
658 return(fclose(gif_file));
662 static char *std_args[] = { "tilemap", /* dummy argv[0] */
663 "monsters.gif", "monsters.txt",
664 "objects.gif", "objects.txt",
665 "other.gif", "other.txt" };
672 pixel pixels[TILE_Y][TILE_X];
675 argc = SIZE(std_args);
677 } else if (argc != 3) {
678 Fprintf(stderr, "usage: gif2txt giffile txtfile\n");
683 if (!fopen_gif_file(argv[1], RDBMODE))
688 if (!fopen_text_file(argv[2], WRTMODE)) {
689 (void) fclose_gif_file();
693 while (read_gif_tile(pixels))
694 (void) write_text_tile(pixels);
696 (void) fclose_gif_file();
697 (void) fclose_text_file();