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);
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);
160 array = malloc(sizeof(Rawimage**));
168 switch(c = Bgetc(h->fd)){
172 case 0x21: /* Extension (ignored) */
176 case 0x2C: /* Image Descriptor */
179 if(new->fields & 0x80){
180 new->cmaplen = 3*(1<<((new->fields&7)+1));
181 new->cmap = readcmap(h, (new->fields&7)+1);
183 new->cmaplen = 3*(1<<((h->fields&7)+1));
184 new->cmap = malloc(new->cmaplen);
185 memmove(new->cmap, h->globalcmap, new->cmaplen);
188 new->chans[0] = decode(h, new, tbl);
189 if(new->fields & 0x40)
191 new->gifflags = h->flags;
192 new->gifdelay = h->delay;
193 new->giftrindex = h->trindex;
194 new->gifloopcount = h->loopcount;
195 array = realloc(h->array, (nimages+2)*sizeof(Rawimage*));
198 array[nimages++] = new;
199 array[nimages] = nil;
204 case 0x3B: /* Trailer */
208 fprint(2, "ReadGIF: unknown block type: 0x%.2x\n", c);
214 if(array[0]==nil || array[0]->chans[0] == nil)
215 giferror(h, "ReadGIF: no picture in file");
222 readheader(Header *h)
224 if(Breadn(h->fd, h->buf, 13) != 13)
225 giferror(h, "ReadGIF: can't read header: %r");
226 memmove(h->vers, h->buf, 6);
227 if(strcmp(h->vers, "GIF87a")!=0 && strcmp(h->vers, "GIF89a")!=0)
228 giferror(h, "ReadGIF: can't recognize format %s", h->vers);
229 h->screenw = h->buf[6]+(h->buf[7]<<8);
230 h->screenh = h->buf[8]+(h->buf[9]<<8);
231 h->fields = h->buf[10];
232 h->bgrnd = h->buf[11];
233 h->aspect = h->buf[12];
242 readcmap(Header *h, int size)
247 giferror(h, "ReadGIF: can't handles %d bits per pixel", size);
249 if(Breadn(h->fd, h->buf, size) != size)
250 giferror(h, "ReadGIF: short read on color map");
254 memmove(map, h->buf, size);
263 int left, top, width, height;
265 if(Breadn(h->fd, h->buf, 9) != 9)
266 giferror(h, "ReadGIF: can't read image descriptor: %r");
267 i = malloc(sizeof(Rawimage));
270 left = h->buf[0]+(h->buf[1]<<8);
271 top = h->buf[2]+(h->buf[3]<<8);
272 width = h->buf[4]+(h->buf[5]<<8);
273 height = h->buf[6]+(h->buf[7]<<8);
274 i->fields = h->buf[8];
277 i->r.max.x = left+width;
278 i->r.max.y = top+height;
281 memset(i->chans, 0, sizeof(i->chans));
288 readdata(Header *h, uchar *data)
292 nbytes = Bgetc(h->fd);
294 giferror(h, "ReadGIF: can't read data: %r");
297 n = Breadn(h->fd, data, nbytes);
299 giferror(h, "ReadGIF: can't read data: %r");
301 fprint(2, "ReadGIF: short data subblock\n");
307 graphiccontrol(Header *h)
309 if(Breadn(h->fd, h->buf, 5+1) != 5+1)
310 giferror(h, readerr);
311 h->flags = h->buf[1];
312 h->delay = h->buf[2]+(h->buf[3]<<8);
313 h->trindex = h->buf[4];
318 skipextension(Header *h)
320 int type, hsize, hasdata, n;
329 giferror(h, extreaderr);
331 case 0x01: /* Plain Text Extension */
335 case 0xF9: /* Graphic Control Extension */
338 case 0xFE: /* Comment Extension */
341 case 0xFF: /* Application Extension */
342 hsize = Bgetc(h->fd);
343 /* standard says this must be 11, but Adobe likes to put out 10-byte ones,
344 * so we pay attention to the field. */
348 giferror(h, "ReadGIF: unknown extension");
350 if(hsize>0 && Breadn(h->fd, h->buf, hsize) != hsize)
351 giferror(h, extreaderr);
354 * This code used to check h->buf[hsize-1] != 0
355 * and giferror if so, but if !hasdata, hsize == 0.
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, doclip, incode, codesize, CTM, EOD, pici, datai, stacki, nbits, sreg, fc, code, piclen;
377 int csize, nentry, maxentry, first, ocode, ndata, nb;
378 uchar clip, *p, *pic;
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);
387 if(i->cmap!=nil && i->cmaplen!=3*(1<<codesize)
388 && (codesize!=2 || i->cmaplen!=3*2)) /* peculiar GIF bitmap files... */
394 piclen = (i->r.max.x-i->r.min.x)*(i->r.max.y-i->r.min.y);
396 pic = malloc(piclen);
411 maxentry = (1<<csize)-1;
415 for(;; ocode = incode) {
416 while(nbits < csize) {
418 ndata = readdata(h, data);
427 code = sreg & ((1<<csize) - 1);
432 ndata = readdata(h, data);
434 fprint(2, "ReadGIF: unexpected data past EOD\n");
441 stacki = (sizeof stack)-1;
445 /* special case for KwKwK */
447 stack[stacki--] = fc;
452 fprint(2, "ReadGIF: GIF invalid, code out of range, %x > %x\n", code, nentry);
455 for(c=code; stacki>0 && c>=0; c=tbl[c].prefix)
456 stack[stacki--] = tbl[c].exten;
458 nb = (sizeof stack)-(stacki+1);
459 if(pici+nb > piclen){
460 /* this common error is harmless
461 * we have to keep reading to keep the blocks in sync */
464 memmove(pic+pici, stack+stacki+1, sizeof stack - (stacki+1));
468 fc = stack[stacki+1];
474 #define early 0 /* peculiar tiff feature here for reference */
475 if(nentry == maxentry-early) {
479 maxentry = (1<<csize);
483 tbl[nentry].prefix = ocode;
484 tbl[nentry].exten = fc;
492 for(p = pic; p < pic+piclen; p++)
502 interlace(Header *h, Rawimage *image)
509 pic = image->chans[0];
511 dx = r.max.x-r.min.x;
512 ipic = malloc(dx*(r.max.y-r.min.y));
516 /* Group 1: every 8th row, starting with row 0 */
518 for(y=r.min.y; y<r.max.y; y+=8){
519 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
523 /* Group 2: every 8th row, starting with row 4 */
524 for(y=r.min.y+4; y<r.max.y; y+=8){
525 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
529 /* Group 3: every 4th row, starting with row 2 */
530 for(y=r.min.y+2; y<r.max.y; y+=4){
531 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
535 /* Group 4: every 2nd row, starting with row 1 */
536 for(y=r.min.y+1; y<r.max.y; y+=2){
537 memmove(&ipic[(y-r.min.y)*dx], &pic[yy*dx], dx);
541 free(image->chans[0]);
542 image->chans[0] = ipic;