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++){
69 return "WriteGIF: can't handle channel type";
73 writeheader(fd, r, depth, chan, loopcount);
78 startgif(Biobuf *fd, Image *image, int loopcount)
80 return startgif0(fd, image->chan, image->r, image->depth, loopcount);
84 memstartgif(Biobuf *fd, Memimage *memimage, int loopcount)
86 return startgif0(fd, memimage->chan, memimage->r, memimage->depth, loopcount);
91 writegif0(Biobuf *fd, Image *image, Memimage *memimage, ulong chan, Rectangle r, char *comment, int dt, int trans)
103 return "WriteGIF: can't handle channel type";
106 writecomment(fd, comment);
107 writegraphiccontrol(fd, dt, trans);
108 writedescriptor(fd, r);
110 err = writedata(fd, image, memimage);
118 writegif(Biobuf *fd, Image *image, char *comment, int dt, int trans)
120 return writegif0(fd, image, nil, image->chan, image->r, comment, dt, trans);
124 memwritegif(Biobuf *fd, Memimage *memimage, char *comment, int dt, int trans)
126 return writegif0(fd, nil, memimage, memimage->chan, memimage->r, comment, dt, trans);
130 * Write little-endian 16-bit integer
134 put2(Biobuf *fd, int i)
141 * Get color map for all ldepths, in format suitable for writing out
151 if(colormap[0] != nil)
153 for(i=0; i<nelem(colormap); i++)
154 colormap[i] = gifmalloc(3* colormapsize[i]);
155 c = colormap[GREYMAP]; /* GREY8 */
156 for(i=0; i<256; i++){
157 c[3*i+0] = i; /* red */
158 c[3*i+1] = i; /* green */
159 c[3*i+2] = i; /* blue */
161 c = colormap[3]; /* RGBV */
162 for(i=0; i<256; i++){
164 c[3*i+0] = (rgb>>16) & 0xFF; /* red */
165 c[3*i+1] = (rgb>> 8) & 0xFF; /* green */
166 c[3*i+2] = (rgb>> 0) & 0xFF; /* blue */
168 c = colormap[2]; /* GREY4 */
172 c[3*i+0] = (rgb>>16) & 0xFF; /* red */
173 c[3*i+1] = (rgb>> 8) & 0xFF; /* green */
174 c[3*i+2] = (rgb>> 0) & 0xFF; /* blue */
176 c = colormap[1]; /* GREY2 */
178 col = (i<<6)|(i<<4)|(i<<2)|i;
180 c[3*i+0] = (rgb>>16) & 0xFF; /* red */
181 c[3*i+1] = (rgb>> 8) & 0xFF; /* green */
182 c[3*i+2] = (rgb>> 0) & 0xFF; /* blue */
184 c = colormap[0]; /* GREY1 */
191 c[3*i+0] = (rgb>>16) & 0xFF; /* red */
192 c[3*i+1] = (rgb>> 8) & 0xFF; /* green */
193 c[3*i+2] = (rgb>> 0) & 0xFF; /* blue */
198 * Write header, logical screen descriptor, and color map
202 writeheader(Biobuf *fd, Rectangle r, int depth, ulong chan, int loopcount)
205 Bprint(fd, "%s", "GIF89a");
207 /* Logical Screen Descriptor */
211 /* Color table present, 4 bits per color (for RGBV best case), size of color map */
212 Bputc(fd, (1<<7)|(3<<4)|(depth-1)); /* not right for GREY8, but GIF doesn't let us specify enough bits */
213 Bputc(fd, 0xFF); /* white background (doesn't matter anyway) */
214 Bputc(fd, 0); /* pixel aspect ratio - unused */
216 /* Global Color Table */
221 depth = drawlog2[depth];
222 Bwrite(fd, colormap[depth], 3*colormapsize[depth]);
224 if(loopcount >= 0){ /* hard-to-discover way to force cycled animation */
225 /* Application Extension with (1 loopcountlo loopcounthi) as data */
229 Bwrite(fd, "NETSCAPE2.0", 11);
238 * Write optional comment block
242 writecomment(Biobuf *fd, char *comment)
246 if(comment==nil || comment[0]=='\0')
249 /* Comment extension and label */
258 Bwrite(fd, comment, n);
260 /* Block terminator */
265 * Write optional control block (sets Delay Time)
269 writegraphiccontrol(Biobuf *fd, int dt, int trans)
271 if(dt < 0 && trans < 0)
274 /* Comment extension and label and block size*/
279 /* Disposal method and other flags (none) */
285 /* Delay time, in centisec (argument is millisec for sanity) */
294 /* Transparency index */
299 /* Block terminator */
304 * Write image descriptor
308 writedescriptor(Biobuf *fd, Rectangle r)
310 /* Image Separator */
313 /* Left, top, width, height */
314 put2(fd, r.min.x-mainrect.min.x);
315 put2(fd, r.min.y-mainrect.min.y);
318 /* no special processing */
327 writedata(Biobuf *fd, Image *image, Memimage *memimage)
336 depth = memimage->depth;
339 depth = image->depth;
342 /* LZW Minimum code size */
349 * Read image data into memory
350 * potentially one extra byte on each end of each scan line
352 ndata = Dy(r)*(2+(Dx(r)>>(3-drawlog2[depth])));
353 data = gifmalloc(ndata);
355 ndata = unloadmemimage(memimage, r, data, ndata);
357 ndata = unloadimage(image, r, data, ndata);
359 err = gifmalloc(ERRMAX);
360 snprint(err, ERRMAX, "WriteGIF: %r");
365 /* Encode and emit the data */
366 encode(fd, r, depth, data, ndata);
369 /* Block Terminator */
385 memendgif(Biobuf *fd)
391 * Put n bits of c into output at io.buf[i];
395 output(IO *io, int c, int n)
399 io->buf[io->i++] = io->sreg;
400 Bputc(io->fd, io->i);
401 Bwrite(io->fd, io->buf, io->i);
406 if(io->nbits+n >= 31){
407 fprint(2, "panic: WriteGIF sr overflow\n");
408 exits("WriteGIF panic");
410 io->sreg |= c<<io->nbits;
413 while(io->nbits >= 8){
414 io->buf[io->i++] = io->sreg;
421 Bwrite(io->fd, io->buf, 255);
422 memmove(io->buf, io->buf+255, io->i-255);
432 encode(Biobuf *fd, Rectangle r, int depth, uchar *data, uint ndata)
434 int i, c, h, csize, prefix, first, sreg, nbits, bitsperpixel;
435 int CTM, EOD, codesize, ld0, datai, x, ld, pm;
436 int nentry, maxentry, early;
442 ld = drawlog2[depth];
443 /* ldepth 0 must generate codesize 2 with values 0 and 1 (see the spec.) */
451 io = gifmalloc(sizeof(IO));
455 bitsperpixel = 1<<ld;
456 pm = (1<<bitsperpixel)-1;
460 hash = gifmalloc(Nhash*sizeof(Entry*));
463 memset(hash, 0, Nhash*sizeof(Entry*));
466 maxentry = (1<<csize);
467 for(i = 0; i<nentry; i++){
469 h = (e->prefix<<24) | (e->exten<<8);
478 output(io, CTM, csize);
482 * Scan over pixels. Because of partially filled bytes on ends of scan lines,
483 * which must be ignored in the data stream passed to GIF, this is more
484 * complex than we'd like.
489 /* beginning of scan line is difficult; prime the shift register */
493 sreg = data[datai++];
494 nbits = 8-((x&(7>>ld))<<ld);
503 sreg = data[datai++];
506 nbits -= bitsperpixel;
507 c = sreg>>nbits & pm;
508 h = prefix<<24 | c<<8;
513 for(e = hash[h]; e!=nil; e=e->next){
514 if(e->prefix == prefix && e->exten == c){
526 output(io, prefix, csize);
527 early = 0; /* peculiar tiff feature here for reference */
528 if(nentry == maxentry-early){
530 nbits += bitsperpixel; /* unget pixel */
532 if(ld != 3 && x == r.min.x)
534 output(io, CTM, csize);
538 maxentry = (1<<csize);
551 output(io, prefix, csize);
552 output(io, EOD, csize);
553 output(io, -1, csize);
565 fprint(2, "WriteGIF: out of memory allocating %ld\n", sz);