7 typedef struct Entry Entry;
8 typedef struct Header Header;
39 static char readerr[] = "ReadGIF: read error: %r";
40 static char extreaderr[] = "ReadGIF: can't read extension: %r";
41 static char memerr[] = "ReadGIF: malloc failed: %r";
43 static Rawimage** readarray(Header*);
44 static Rawimage* readone(Header*);
45 static void readheader(Header*);
46 static void skipextension(Header*);
47 static uchar* readcmap(Header*, int);
48 static uchar* decode(Header*, Rawimage*, Entry*);
49 static void interlace(Header*, Rawimage*);
55 void **p = (void**)pp;
65 giffreeall(Header *h, int freeimage)
76 clear(&h->new->chans[0]);
79 clear(&h->globalcmap);
80 if(freeimage && h->array!=nil){
81 for(i=0; h->array[i]; i++){
82 clear(&h->array[i]->cmap);
83 clear(&h->array[i]->chans[0]);
91 giferror(Header *h, char *fmt, ...)
96 vseprint(h->err, h->err+sizeof h->err, fmt, arg);
101 longjmp(h->errlab, 1);
106 readgif(int fd, int colorspace)
115 if(Binit(&b, fd, OREAD) < 0)
117 h = malloc(sizeof(Header));
122 memset(h, 0, sizeof(Header));
124 errstr(buf, sizeof buf); /* throw it away */
125 if(setjmp(h->errlab))
142 for(i=0; i<258; i++) {
153 Rawimage *new, **array;
161 h->globalcmap = readcmap(h, (h->fields&7)+1);
163 array = malloc(sizeof(Rawimage**));
171 switch(c = Bgetc(h->fd)){
175 case 0x21: /* Extension (ignored) */
179 case 0x2C: /* Image Descriptor */
182 if(new->fields & 0x80){
183 new->cmaplen = 3*(1<<((new->fields&7)+1));
184 new->cmap = readcmap(h, (new->fields&7)+1);
186 new->cmaplen = 3*(1<<((h->fields&7)+1));
187 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;
290 readdata(Header *h, uchar *data)
294 nbytes = Bgetc(h->fd);
296 giferror(h, "ReadGIF: can't read data: %r");
299 n = Bread(h->fd, data, nbytes);
301 giferror(h, "ReadGIF: can't read data: %r");
303 fprint(2, "ReadGIF: short data subblock\n");
309 graphiccontrol(Header *h)
311 if(Bread(h->fd, h->buf, 5+1) != 5+1)
312 giferror(h, readerr);
313 h->flags = h->buf[1];
314 h->delay = h->buf[2]+(h->buf[3]<<8);
315 h->trindex = h->buf[4];
320 skipextension(Header *h)
322 int type, hsize, hasdata, n;
331 giferror(h, extreaderr);
333 case 0x01: /* Plain Text Extension */
337 case 0xF9: /* Graphic Control Extension */
340 case 0xFE: /* Comment Extension */
343 case 0xFF: /* Application Extension */
344 hsize = Bgetc(h->fd);
345 /* standard says this must be 11, but Adobe likes to put out 10-byte ones,
346 * so we pay attention to the field. */
350 giferror(h, "ReadGIF: unknown extension");
352 if(hsize>0 && Bread(h->fd, h->buf, hsize) != hsize)
353 giferror(h, extreaderr);
355 if(h->buf[hsize-1] != 0)
356 giferror(h, "ReadGIF: bad extension format");
360 /* loop counter: Application Extension with NETSCAPE2.0 as string and 1 <loop.count> in data */
361 if(type == 0xFF && hsize==11 && memcmp(h->buf, "NETSCAPE2.0", 11)==0){
362 n = readdata(h, data);
365 if(n==3 && data[0]==1)
366 h->loopcount = data[1] | (data[2]<<8);
368 while(readdata(h, data) != 0)
374 decode(Header *h, Rawimage *i, Entry *tbl)
376 int c, incode, codesize, CTM, EOD, pici, datai, stacki, nbits, sreg, fc, code, piclen;
377 int csize, nentry, maxentry, first, ocode, ndata, nb;
379 uchar stack[4096], data[256];
381 if(Bread(h->fd, h->buf, 1) != 1)
382 giferror(h, "ReadGIF: can't read data: %r");
383 codesize = h->buf[0];
384 if(codesize>8 || 0>codesize)
385 giferror(h, "ReadGIF: can't handle codesize %d", codesize);
386 if(i->cmap!=nil && i->cmaplen!=3*(1<<codesize)
387 && (codesize!=2 || i->cmaplen!=3*2)) /* peculiar GIF bitmap files... */
388 giferror(h, "ReadGIF: codesize %d doesn't match color map 3*%d", codesize, i->cmaplen/3);
393 piclen = (i->r.max.x-i->r.min.x)*(i->r.max.y-i->r.min.y);
395 pic = malloc(piclen);
410 maxentry = (1<<csize)-1;
414 for(;; ocode = incode) {
415 while(nbits < csize) {
417 ndata = readdata(h, data);
426 code = sreg & ((1<<csize) - 1);
431 ndata = readdata(h, data);
433 fprint(2, "ReadGIF: unexpected data past EOD");
440 stacki = (sizeof stack)-1;
444 /* special case for KwKwK */
446 stack[stacki--] = fc;
451 giferror(h, "ReadGIF: bad code %x %x", code, nentry);
453 for(c=code; c>=0; c=tbl[c].prefix)
454 stack[stacki--] = tbl[c].exten;
456 nb = (sizeof stack)-(stacki+1);
457 if(pici+nb > piclen){
458 /* this common error is harmless
459 * we have to keep reading to keep the blocks in sync */
462 memmove(pic+pici, stack+stacki+1, sizeof stack - (stacki+1));
466 fc = stack[stacki+1];
472 #define early 0 /* peculiar tiff feature here for reference */
473 if(nentry == maxentry-early) {
477 maxentry = (1<<csize);
481 tbl[nentry].prefix = ocode;
482 tbl[nentry].exten = fc;
494 interlace(Header *h, Rawimage *image)
501 pic = image->chans[0];
503 dx = r.max.x-r.min.x;
504 ipic = malloc(dx*(r.max.y-r.min.y));
508 /* Group 1: every 8th row, starting with row 0 */
510 for(y=r.min.y; y<r.max.y; y+=8){
511 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
515 /* Group 2: every 8th row, starting with row 4 */
516 for(y=r.min.y+4; y<r.max.y; y+=8){
517 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
521 /* Group 3: every 4th row, starting with row 2 */
522 for(y=r.min.y+2; y<r.max.y; y+=4){
523 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
527 /* Group 4: every 2nd row, starting with row 1 */
528 for(y=r.min.y+1; y<r.max.y; y+=2){
529 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
533 free(image->chans[0]);
534 image->chans[0] = ipic;