7 typedef struct Entry Entry;
8 typedef struct Header Header;
38 static char readerr[] = "ReadGIF: read error: %r";
39 static char extreaderr[] = "ReadGIF: can't read extension: %r";
40 static char memerr[] = "ReadGIF: malloc failed: %r";
42 static Rawimage** readarray(Header*);
43 static Rawimage* readone(Header*);
44 static void readheader(Header*);
45 static void skipextension(Header*);
46 static uchar* readcmap(Header*, int);
47 static uchar* decode(Header*, Rawimage*, Entry*);
48 static void interlace(Header*, Rawimage*);
62 giffreeall(Header *h, int freeimage)
73 clear(&h->new->chans[0]);
76 clear(&h->globalcmap);
77 if(freeimage && h->array!=nil){
78 for(i=0; h->array[i]; i++){
79 clear(&h->array[i]->cmap);
80 clear(&h->array[i]->chans[0]);
88 giferror(Header *h, char *fmt, ...)
93 vseprint(h->err, h->err+sizeof h->err, fmt, arg);
96 werrstr("%s", h->err);
98 longjmp(h->errlab, 1);
103 readgif(int fd, int colorspace)
112 if(Binit(&b, fd, OREAD) < 0)
114 h = malloc(sizeof(Header));
119 memset(h, 0, sizeof(Header));
121 errstr(buf, sizeof buf); /* throw it away */
122 if(setjmp(h->errlab))
139 for(i=0; i<258; i++) {
150 Rawimage *new, **array;
158 h->globalcmap = readcmap(h, (h->fields&7)+1);
159 array = malloc(sizeof(Rawimage*));
167 switch(c = Bgetc(h->fd)){
171 case 0x21: /* Extension (ignored) */
175 case 0x2C: /* Image Descriptor */
178 if(new->fields & 0x80){
179 new->cmaplen = 3*(1<<((new->fields&7)+1));
180 new->cmap = readcmap(h, (new->fields&7)+1);
182 if(h->globalcmap == nil)
183 giferror(h, "ReadGIF: globalcmap missing");
184 new->cmaplen = 3*(1<<((h->fields&7)+1));
185 new->cmap = malloc(new->cmaplen);
188 memmove(new->cmap, h->globalcmap, new->cmaplen);
191 new->chans[0] = decode(h, new, tbl);
192 if(new->fields & 0x40)
194 new->gifflags = h->flags;
195 new->gifdelay = h->delay;
196 new->giftrindex = h->trindex;
197 new->gifloopcount = h->loopcount;
198 array = realloc(h->array, (nimages+2)*sizeof(Rawimage*));
201 array[nimages++] = new;
202 array[nimages] = nil;
207 case 0x3B: /* Trailer */
211 fprint(2, "ReadGIF: unknown block type: 0x%.2x\n", c);
217 if(array[0]==nil || array[0]->chans[0] == nil)
218 giferror(h, "ReadGIF: no picture in file");
225 readheader(Header *h)
227 if(Bread(h->fd, h->buf, 13) != 13)
228 giferror(h, "ReadGIF: can't read header: %r");
229 memmove(h->vers, h->buf, 6);
230 if(strcmp(h->vers, "GIF87a")!=0 && strcmp(h->vers, "GIF89a")!=0)
231 giferror(h, "ReadGIF: can't recognize format %s", h->vers);
232 h->screenw = h->buf[6]+(h->buf[7]<<8);
233 h->screenh = h->buf[8]+(h->buf[9]<<8);
234 h->fields = h->buf[10];
235 h->bgrnd = h->buf[11];
236 h->aspect = h->buf[12];
245 readcmap(Header *h, int size)
250 giferror(h, "ReadGIF: can't handles %d bits per pixel", size);
252 if(Bread(h->fd, h->buf, size) != size)
253 giferror(h, "ReadGIF: short read on color map");
257 memmove(map, h->buf, size);
266 int left, top, width, height;
268 if(Bread(h->fd, h->buf, 9) != 9)
269 giferror(h, "ReadGIF: can't read image descriptor: %r");
270 i = malloc(sizeof(Rawimage));
273 left = h->buf[0]+(h->buf[1]<<8);
274 top = h->buf[2]+(h->buf[3]<<8);
275 width = h->buf[4]+(h->buf[5]<<8);
276 height = h->buf[6]+(h->buf[7]<<8);
277 i->fields = h->buf[8];
280 i->r.max.x = left+width;
281 i->r.max.y = top+height;
284 memset(i->chans, 0, sizeof(i->chans));
291 readdata(Header *h, uchar *data)
295 nbytes = Bgetc(h->fd);
297 giferror(h, "ReadGIF: can't read data: %r");
300 n = Bread(h->fd, data, nbytes);
302 giferror(h, "ReadGIF: can't read data: %r");
304 fprint(2, "ReadGIF: short data subblock\n");
310 graphiccontrol(Header *h)
312 if(Bread(h->fd, h->buf, 5+1) != 5+1)
313 giferror(h, readerr);
314 h->flags = h->buf[1];
315 h->delay = h->buf[2]+(h->buf[3]<<8);
316 h->trindex = h->buf[4];
321 skipextension(Header *h)
323 int type, hsize, hasdata, n;
332 giferror(h, extreaderr);
334 case 0x01: /* Plain Text Extension */
338 case 0xF9: /* Graphic Control Extension */
341 case 0xFE: /* Comment Extension */
344 case 0xFF: /* Application Extension */
345 hsize = Bgetc(h->fd);
346 /* standard says this must be 11, but Adobe likes to put out 10-byte ones,
347 * so we pay attention to the field. */
351 giferror(h, "ReadGIF: unknown extension");
353 if(hsize>0 && Bread(h->fd, h->buf, hsize) != hsize)
354 giferror(h, extreaderr);
357 * This code used to check h->buf[hsize-1] != 0
358 * and giferror if so, but if !hasdata, hsize == 0.
363 /* loop counter: Application Extension with NETSCAPE2.0 as string and 1 <loop.count> in data */
364 if(type == 0xFF && hsize==11 && memcmp(h->buf, "NETSCAPE2.0", 11)==0){
365 n = readdata(h, data);
368 if(n==3 && data[0]==1)
369 h->loopcount = data[1] | (data[2]<<8);
371 while(readdata(h, data) != 0)
377 decode(Header *h, Rawimage *i, Entry *tbl)
379 int c, doclip, incode, codesize, CTM, EOD, pici, datai, stacki, nbits, sreg, fc, code, piclen;
380 int csize, nentry, maxentry, first, ocode, ndata, nb;
381 uchar clip, *p, *pic;
382 uchar stack[4096], data[256];
384 if(Bread(h->fd, h->buf, 1) != 1)
385 giferror(h, "ReadGIF: can't read data: %r");
386 codesize = h->buf[0];
387 if(codesize>8 || 0>codesize)
388 giferror(h, "ReadGIF: can't handle codesize %d", codesize);
390 if(i->cmap!=nil && i->cmaplen!=3*(1<<codesize)
391 && (codesize!=2 || i->cmaplen!=3*2)) /* peculiar GIF bitmap files... */
397 piclen = (i->r.max.x-i->r.min.x)*(i->r.max.y-i->r.min.y);
399 pic = malloc(piclen);
414 maxentry = (1<<csize)-1;
418 for(;; ocode = incode) {
419 while(nbits < csize) {
421 ndata = readdata(h, data);
430 code = sreg & ((1<<csize) - 1);
435 ndata = readdata(h, data);
437 fprint(2, "ReadGIF: unexpected data past EOD\n");
444 stacki = (sizeof stack)-1;
448 /* special case for KwKwK */
450 stack[stacki--] = fc;
455 fprint(2, "ReadGIF: GIF invalid, code out of range, %x > %x\n", code, nentry);
458 for(c=code; stacki>0 && c>=0; c=tbl[c].prefix)
459 stack[stacki--] = tbl[c].exten;
461 nb = (sizeof stack)-(stacki+1);
462 if(pici+nb > piclen){
463 /* this common error is harmless
464 * we have to keep reading to keep the blocks in sync */
467 memmove(pic+pici, stack+stacki+1, sizeof stack - (stacki+1));
471 fc = stack[stacki+1];
477 #define early 0 /* peculiar tiff feature here for reference */
478 if(nentry == maxentry-early) {
482 maxentry = (1<<csize);
486 tbl[nentry].prefix = ocode;
487 tbl[nentry].exten = fc;
495 for(p = pic; p < pic+piclen; p++)
505 interlace(Header *h, Rawimage *image)
512 pic = image->chans[0];
514 dx = r.max.x-r.min.x;
515 ipic = malloc(dx*(r.max.y-r.min.y));
519 /* Group 1: every 8th row, starting with row 0 */
521 for(y=r.min.y; y<r.max.y; y+=8){
522 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
526 /* Group 2: every 8th row, starting with row 4 */
527 for(y=r.min.y+4; y<r.max.y; y+=8){
528 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
532 /* Group 3: every 4th row, starting with row 2 */
533 for(y=r.min.y+2; y<r.max.y; y+=4){
534 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
538 /* Group 4: every 2nd row, starting with row 1 */
539 for(y=r.min.y+1; y<r.max.y; y+=2){
540 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
544 free(image->chans[0]);
545 image->chans[0] = ipic;