14 typedef struct Entry Entry;
31 int nbits; /* bits in right side of shift register */
32 int sreg; /* shift register */
35 static Rectangle mainrect;
36 static Entry tbl[4096];
37 static uchar *colormap[5]; /* one for each ldepth: GREY1 GREY2 GREY4 CMAP8=rgbv plus GREY8 */
39 static int colormapsize[] = { 2, 4, 16, 256, 256 }; /* 2 for zero is an odd property of GIF */
41 static void writeheader(Biobuf*, Rectangle, int, ulong, int);
42 static void writedescriptor(Biobuf*, Rectangle);
43 static char* writedata(Biobuf*, Image*, Memimage*);
44 static void writecomment(Biobuf *fd, char*);
45 static void writegraphiccontrol(Biobuf *fd, int, int);
46 static void* gifmalloc(ulong);
47 static void encode(Biobuf*, Rectangle, int, uchar*, uint);
51 startgif0(Biobuf *fd, ulong chan, Rectangle r, int depth, int loopcount)
55 for(i=0; i<nelem(tbl); i++)
56 tbl[i] = (Entry){i, -1, i, nil};
66 return "WriteGIF: can't handle channel type";
70 writeheader(fd, r, depth, chan, loopcount);
75 startgif(Biobuf *fd, Image *image, int loopcount)
77 return startgif0(fd, image->chan, image->r, image->depth, loopcount);
81 memstartgif(Biobuf *fd, Memimage *memimage, int loopcount)
83 return startgif0(fd, memimage->chan, memimage->r, memimage->depth, loopcount);
88 writegif0(Biobuf *fd, Image *image, Memimage *memimage, ulong chan, Rectangle r, char *comment, int dt, int trans)
100 return "WriteGIF: can't handle channel type";
103 writecomment(fd, comment);
104 writegraphiccontrol(fd, dt, trans);
105 writedescriptor(fd, r);
107 err = writedata(fd, image, memimage);
115 writegif(Biobuf *fd, Image *image, char *comment, int dt, int trans)
117 return writegif0(fd, image, nil, image->chan, image->r, comment, dt, trans);
121 memwritegif(Biobuf *fd, Memimage *memimage, char *comment, int dt, int trans)
123 return writegif0(fd, nil, memimage, memimage->chan, memimage->r, comment, dt, trans);
127 * Write little-endian 16-bit integer
131 put2(Biobuf *fd, int i)
138 * Get color map for all ldepths, in format suitable for writing out
148 if(colormap[0] != nil)
150 for(i=0; i<nelem(colormap); i++)
151 colormap[i] = gifmalloc(3* colormapsize[i]);
152 c = colormap[GREYMAP]; /* GREY8 */
153 for(i=0; i<256; i++){
154 c[3*i+0] = i; /* red */
155 c[3*i+1] = i; /* green */
156 c[3*i+2] = i; /* blue */
158 c = colormap[3]; /* RGBV */
159 for(i=0; i<256; i++){
161 c[3*i+0] = (rgb>>16) & 0xFF; /* red */
162 c[3*i+1] = (rgb>> 8) & 0xFF; /* green */
163 c[3*i+2] = (rgb>> 0) & 0xFF; /* blue */
165 c = colormap[2]; /* GREY4 */
169 c[3*i+0] = (rgb>>16) & 0xFF; /* red */
170 c[3*i+1] = (rgb>> 8) & 0xFF; /* green */
171 c[3*i+2] = (rgb>> 0) & 0xFF; /* blue */
173 c = colormap[1]; /* GREY2 */
175 col = (i<<6)|(i<<4)|(i<<2)|i;
177 c[3*i+0] = (rgb>>16) & 0xFF; /* red */
178 c[3*i+1] = (rgb>> 8) & 0xFF; /* green */
179 c[3*i+2] = (rgb>> 0) & 0xFF; /* blue */
181 c = colormap[0]; /* GREY1 */
188 c[3*i+0] = (rgb>>16) & 0xFF; /* red */
189 c[3*i+1] = (rgb>> 8) & 0xFF; /* green */
190 c[3*i+2] = (rgb>> 0) & 0xFF; /* blue */
194 /* imported from libdraw/arith.c to permit an extern log2 function */
195 static int log2[] = {
196 -1, 0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4,
197 -1, -1, -1, -1, -1, -1, -1, 4 /* BUG */, -1, -1, -1, -1, -1, -1, -1, 5
201 * Write header, logical screen descriptor, and color map
205 writeheader(Biobuf *fd, Rectangle r, int depth, ulong chan, int loopcount)
208 Bprint(fd, "%s", "GIF89a");
210 /* Logical Screen Descriptor */
214 /* Color table present, 4 bits per color (for RGBV best case), size of color map */
215 Bputc(fd, (1<<7)|(3<<4)|(depth-1)); /* not right for GREY8, but GIF doesn't let us specify enough bits */
216 Bputc(fd, 0xFF); /* white background (doesn't matter anyway) */
217 Bputc(fd, 0); /* pixel aspect ratio - unused */
219 /* Global Color Table */
225 Bwrite(fd, colormap[depth], 3*colormapsize[depth]);
227 if(loopcount >= 0){ /* hard-to-discover way to force cycled animation */
228 /* Application Extension with (1 loopcountlo loopcounthi) as data */
232 Bwrite(fd, "NETSCAPE2.0", 11);
241 * Write optional comment block
245 writecomment(Biobuf *fd, char *comment)
249 if(comment==nil || comment[0]=='\0')
252 /* Comment extension and label */
261 Bwrite(fd, comment, n);
263 /* Block terminator */
268 * Write optional control block (sets Delay Time)
272 writegraphiccontrol(Biobuf *fd, int dt, int trans)
274 if(dt < 0 && trans < 0)
277 /* Comment extension and label and block size*/
282 /* Disposal method and other flags (none) */
288 /* Delay time, in centisec (argument is millisec for sanity) */
297 /* Transparency index */
302 /* Block terminator */
307 * Write image descriptor
311 writedescriptor(Biobuf *fd, Rectangle r)
313 /* Image Separator */
316 /* Left, top, width, height */
317 put2(fd, r.min.x-mainrect.min.x);
318 put2(fd, r.min.y-mainrect.min.y);
321 /* no special processing */
330 writedata(Biobuf *fd, Image *image, Memimage *memimage)
339 depth = memimage->depth;
342 depth = image->depth;
345 /* LZW Minimum code size */
352 * Read image data into memory
353 * potentially one extra byte on each end of each scan line
355 ndata = Dy(r)*(2+(Dx(r)>>(3-log2[depth])));
356 data = gifmalloc(ndata);
358 ndata = unloadmemimage(memimage, r, data, ndata);
360 ndata = unloadimage(image, r, data, ndata);
362 err = gifmalloc(ERRMAX);
363 snprint(err, ERRMAX, "WriteGIF: %r");
368 /* Encode and emit the data */
369 encode(fd, r, depth, data, ndata);
372 /* Block Terminator */
388 memendgif(Biobuf *fd)
394 * Put n bits of c into output at io.buf[i];
398 output(IO *io, int c, int n)
402 io->buf[io->i++] = io->sreg;
403 Bputc(io->fd, io->i);
404 Bwrite(io->fd, io->buf, io->i);
409 if(io->nbits+n >= 31){
410 fprint(2, "panic: WriteGIF sr overflow\n");
411 exits("WriteGIF panic");
413 io->sreg |= c<<io->nbits;
416 while(io->nbits >= 8){
417 io->buf[io->i++] = io->sreg;
424 Bwrite(io->fd, io->buf, 255);
425 memmove(io->buf, io->buf+255, io->i-255);
435 encode(Biobuf *fd, Rectangle r, int depth, uchar *data, uint ndata)
437 int i, c, h, csize, prefix, first, sreg, nbits, bitsperpixel;
438 int CTM, EOD, codesize, ld0, datai, x, ld, pm;
439 int nentry, maxentry, early;
446 /* ldepth 0 must generate codesize 2 with values 0 and 1 (see the spec.) */
454 io = gifmalloc(sizeof(IO));
458 bitsperpixel = 1<<ld;
459 pm = (1<<bitsperpixel)-1;
463 hash = gifmalloc(Nhash*sizeof(Entry*));
466 memset(hash, 0, Nhash*sizeof(Entry*));
469 maxentry = (1<<csize);
470 for(i = 0; i<nentry; i++){
472 h = (e->prefix<<24) | (e->exten<<8);
481 output(io, CTM, csize);
485 * Scan over pixels. Because of partially filled bytes on ends of scan lines,
486 * which must be ignored in the data stream passed to GIF, this is more
487 * complex than we'd like.
492 /* beginning of scan line is difficult; prime the shift register */
496 sreg = data[datai++];
497 nbits = 8-((x&(7>>ld))<<ld);
506 sreg = data[datai++];
509 nbits -= bitsperpixel;
510 c = sreg>>nbits & pm;
511 h = prefix<<24 | c<<8;
516 for(e = hash[h]; e!=nil; e=e->next){
517 if(e->prefix == prefix && e->exten == c){
529 output(io, prefix, csize);
530 early = 0; /* peculiar tiff feature here for reference */
531 if(nentry == maxentry-early){
533 nbits += bitsperpixel; /* unget pixel */
535 if(ld != 3 && x == r.min.x)
537 output(io, CTM, csize);
541 maxentry = (1<<csize);
554 output(io, prefix, csize);
555 output(io, EOD, csize);
556 output(io, -1, csize);
568 fprint(2, "WriteGIF: out of memory allocating %ld\n", sz);