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*);
63 giffreeall(Header *h, int freeimage)
74 clear(&h->new->chans[0]);
77 clear(&h->globalcmap);
78 if(freeimage && h->array!=nil){
79 for(i=0; h->array[i]; i++){
80 clear(&h->array[i]->cmap);
81 clear(&h->array[i]->chans[0]);
89 giferror(Header *h, char *fmt, ...)
94 vseprint(h->err, h->err+sizeof h->err, fmt, arg);
99 longjmp(h->errlab, 1);
104 readgif(int fd, int colorspace)
113 if(Binit(&b, fd, OREAD) < 0)
115 h = malloc(sizeof(Header));
120 memset(h, 0, sizeof(Header));
122 errstr(buf, sizeof buf); /* throw it away */
123 if(setjmp(h->errlab))
140 for(i=0; i<258; i++) {
151 Rawimage *new, **array;
159 h->globalcmap = readcmap(h, (h->fields&7)+1);
161 array = malloc(sizeof(Rawimage**));
169 switch(c = Bgetc(h->fd)){
173 case 0x21: /* Extension (ignored) */
177 case 0x2C: /* Image Descriptor */
180 if(new->fields & 0x80){
181 new->cmaplen = 3*(1<<((new->fields&7)+1));
182 new->cmap = readcmap(h, (new->fields&7)+1);
184 new->cmaplen = 3*(1<<((h->fields&7)+1));
185 new->cmap = malloc(new->cmaplen);
186 memmove(new->cmap, h->globalcmap, new->cmaplen);
189 new->chans[0] = decode(h, new, tbl);
190 if(new->fields & 0x40)
192 new->gifflags = h->flags;
193 new->gifdelay = h->delay;
194 new->giftrindex = h->trindex;
195 new->gifloopcount = h->loopcount;
196 array = realloc(h->array, (nimages+2)*sizeof(Rawimage*));
199 array[nimages++] = new;
200 array[nimages] = nil;
205 case 0x3B: /* Trailer */
209 fprint(2, "ReadGIF: unknown block type: 0x%.2x\n", c);
215 if(array[0]==nil || array[0]->chans[0] == nil)
216 giferror(h, "ReadGIF: no picture in file");
223 readheader(Header *h)
225 if(Bread(h->fd, h->buf, 13) != 13)
226 giferror(h, "ReadGIF: can't read header: %r");
227 memmove(h->vers, h->buf, 6);
228 if(strcmp(h->vers, "GIF87a")!=0 && strcmp(h->vers, "GIF89a")!=0)
229 giferror(h, "ReadGIF: can't recognize format %s", h->vers);
230 h->screenw = h->buf[6]+(h->buf[7]<<8);
231 h->screenh = h->buf[8]+(h->buf[9]<<8);
232 h->fields = h->buf[10];
233 h->bgrnd = h->buf[11];
234 h->aspect = h->buf[12];
243 readcmap(Header *h, int size)
248 giferror(h, "ReadGIF: can't handles %d bits per pixel", size);
250 if(Bread(h->fd, h->buf, size) != size)
251 giferror(h, "ReadGIF: short read on color map");
255 memmove(map, h->buf, size);
264 int left, top, width, height;
266 if(Bread(h->fd, h->buf, 9) != 9)
267 giferror(h, "ReadGIF: can't read image descriptor: %r");
268 i = malloc(sizeof(Rawimage));
271 left = h->buf[0]+(h->buf[1]<<8);
272 top = h->buf[2]+(h->buf[3]<<8);
273 width = h->buf[4]+(h->buf[5]<<8);
274 height = h->buf[6]+(h->buf[7]<<8);
275 i->fields = h->buf[8];
278 i->r.max.x = left+width;
279 i->r.max.y = top+height;
282 memset(i->chans, 0, sizeof(i->chans));
289 readdata(Header *h, uchar *data)
293 nbytes = Bgetc(h->fd);
295 giferror(h, "ReadGIF: can't read data: %r");
298 n = Bread(h->fd, data, nbytes);
300 giferror(h, "ReadGIF: can't read data: %r");
302 fprint(2, "ReadGIF: short data subblock\n");
308 graphiccontrol(Header *h)
310 if(Bread(h->fd, h->buf, 5+1) != 5+1)
311 giferror(h, readerr);
312 h->flags = h->buf[1];
313 h->delay = h->buf[2]+(h->buf[3]<<8);
314 h->trindex = h->buf[4];
319 skipextension(Header *h)
321 int type, hsize, hasdata, n;
330 giferror(h, extreaderr);
332 case 0x01: /* Plain Text Extension */
336 case 0xF9: /* Graphic Control Extension */
339 case 0xFE: /* Comment Extension */
342 case 0xFF: /* Application Extension */
343 hsize = Bgetc(h->fd);
344 /* standard says this must be 11, but Adobe likes to put out 10-byte ones,
345 * so we pay attention to the field. */
349 giferror(h, "ReadGIF: unknown extension");
351 if(hsize>0 && Bread(h->fd, h->buf, hsize) != hsize)
352 giferror(h, extreaderr);
355 * This code used to check h->buf[hsize-1] != 0
356 * and giferror if so, but if !hasdata, hsize == 0.
361 /* loop counter: Application Extension with NETSCAPE2.0 as string and 1 <loop.count> in data */
362 if(type == 0xFF && hsize==11 && memcmp(h->buf, "NETSCAPE2.0", 11)==0){
363 n = readdata(h, data);
366 if(n==3 && data[0]==1)
367 h->loopcount = data[1] | (data[2]<<8);
369 while(readdata(h, data) != 0)
375 decode(Header *h, Rawimage *i, Entry *tbl)
377 int c, doclip, incode, codesize, CTM, EOD, pici, datai, stacki, nbits, sreg, fc, code, piclen;
378 int csize, nentry, maxentry, first, ocode, ndata, nb;
379 uchar clip, *p, *pic;
380 uchar stack[4096], data[256];
382 if(Bread(h->fd, h->buf, 1) != 1)
383 giferror(h, "ReadGIF: can't read data: %r");
384 codesize = h->buf[0];
385 if(codesize>8 || 0>codesize)
386 giferror(h, "ReadGIF: can't handle codesize %d", codesize);
388 if(i->cmap!=nil && i->cmaplen!=3*(1<<codesize)
389 && (codesize!=2 || i->cmaplen!=3*2)) /* peculiar GIF bitmap files... */
395 piclen = (i->r.max.x-i->r.min.x)*(i->r.max.y-i->r.min.y);
397 pic = malloc(piclen);
412 maxentry = (1<<csize)-1;
416 for(;; ocode = incode) {
417 while(nbits < csize) {
419 ndata = readdata(h, data);
428 code = sreg & ((1<<csize) - 1);
433 ndata = readdata(h, data);
435 fprint(2, "ReadGIF: unexpected data past EOD\n");
442 stacki = (sizeof stack)-1;
446 /* special case for KwKwK */
448 stack[stacki--] = fc;
453 fprint(2, "ReadGIF: GIF invalid, code out of range, %x > %x\n", code, nentry);
456 for(c=code; stacki>0 && c>=0; c=tbl[c].prefix)
457 stack[stacki--] = tbl[c].exten;
459 nb = (sizeof stack)-(stacki+1);
460 if(pici+nb > piclen){
461 /* this common error is harmless
462 * we have to keep reading to keep the blocks in sync */
465 memmove(pic+pici, stack+stacki+1, sizeof stack - (stacki+1));
469 fc = stack[stacki+1];
475 #define early 0 /* peculiar tiff feature here for reference */
476 if(nentry == maxentry-early) {
480 maxentry = (1<<csize);
484 tbl[nentry].prefix = ocode;
485 tbl[nentry].exten = fc;
493 for(p = pic; p < pic+piclen; p++)
503 interlace(Header *h, Rawimage *image)
510 pic = image->chans[0];
512 dx = r.max.x-r.min.x;
513 ipic = malloc(dx*(r.max.y-r.min.y));
517 /* Group 1: every 8th row, starting with row 0 */
519 for(y=r.min.y; y<r.max.y; y+=8){
520 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
524 /* Group 2: every 8th row, starting with row 4 */
525 for(y=r.min.y+4; y<r.max.y; y+=8){
526 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
530 /* Group 3: every 4th row, starting with row 2 */
531 for(y=r.min.y+2; y<r.max.y; y+=4){
532 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
536 /* Group 4: every 2nd row, starting with row 1 */
537 for(y=r.min.y+1; y<r.max.y; y+=2){
538 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
542 free(image->chans[0]);
543 image->chans[0] = ipic;