14 ulong outchan = CMAP8;
25 char *show(int, char*);
32 if(allims==nil || allims[0]==nil)
34 r = insetrect(screen->clipr, Edge+Border);
35 r.max.x = r.min.x+Dx(allims[0]->r);
36 r.max.y = r.min.y+Dy(allims[0]->r);
45 if(new && getwindow(display, Refnone) < 0){
46 fprint(2, "gif: can't reattach to window\n");
49 if(allims==nil || allims[which]==nil)
51 r = rectaddpt(allims[0]->r, subpt(screen->r.min, allims[0]->r.min));
54 r = rectaddpt(r, subpt(allims[which]->r.min, allims[0]->r.min));
55 drawop(screen, r, allims[which], allmasks[which], allims[which]->r.min, S);
56 flushimage(display, 1);
62 fprint(2, "usage: gif -39cdektv -W winsize [file.gif ...]\n");
68 main(int argc, char *argv[])
75 winsize = EARGF(usage());
77 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
80 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
87 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
94 case 'd': /* suppress display of image */
97 case 'e': /* disable floyd-steinberg error diffusion */
100 case 'k': /* force black and white */
104 case 'v': /* force RGBV */
108 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
121 err = show(0, "<stdin>");
123 for(i=0; i<argc; i++){
124 fd = open(argv[i], OREAD);
126 fprint(2, "gif: can't open %s: %r\n", argv[i]);
129 err = show(fd, argv[i]);
132 if(output && argc>1 && err==nil){
133 fprint(2, "gif: exiting after one file\n");
142 transparency(Rawimage *r, char *name)
146 uchar *pic, *mpic, *mask;
148 if((r->gifflags&TRANSP) == 0)
150 i = allocimage(display, r->r, GREY8, 0, 0);
152 fprint(2, "gif: allocimage for mask of %s failed: %r\n", name);
156 mask = malloc(r->chanlen);
158 fprint(2, "gif: malloc for mask of %s failed: %r\n", name);
162 index = r->giftrindex;
164 for(j=0; j<r->chanlen; j++)
169 if(loadimage(i, i->r, mask, r->chanlen) < 0){
170 fprint(2, "gif: loadimage for mask of %s failed: %r\n", name);
179 /* interleave alpha values of 0xFF in data stream. alpha value comes first, then b g r */
181 expand(uchar *u, int chanlen, int nchan)
186 v = malloc(chanlen*(nchan+1));
188 fprint(2, "gif: malloc fails: %r\n");
193 for(j=0; j<chanlen; j++){
195 for(k=0; k<nchan; k++)
202 addalpha(Rawimage *i)
208 i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
209 i->chanlen = 2*(i->chanlen/1);
210 i->chandesc = CRGBVA16;
211 outchan = CHAN2(CMap, 8, CAlpha, 8);
215 i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
216 i->chanlen = 2*(i->chanlen/1);
218 outchan = CHAN2(CGrey, 8, CAlpha, 8);
222 i->chans[0] = expand(i->chans[0], i->chanlen/3, 3);
223 i->chanlen = 4*(i->chanlen/3);
224 i->chandesc = CRGBA32;
229 chantostr(buf, outchan);
230 fprint(2, "gif: can't add alpha to type %s\n", buf);
236 * Called only when writing output. If the output is RGBA32,
237 * we must write four bytes per pixel instead of two.
238 * There's always at least two: data plus alpha.
239 * r is used only for reference; the image is already in c.
242 blackout(Rawimage *r, Rawimage *c)
249 trindex = r->giftrindex;
250 if(outchan == RGBA32)
251 for(i=0; i<r->chanlen; i++){
264 for(i=0; i<r->chanlen; i++){
282 if(initdraw(0, 0, 0) < 0){
283 fprint(2, "gif: initdraw failed: %r\n");
286 einit(Ekeyboard|Emouse);
293 show(int fd, char *name)
295 Rawimage **images, **rgbv;
296 Image **ims, **masks;
297 int j, k, n, ch, nloop, loopcount, dt;
302 images = readgif(fd, CRGB);
304 fprint(2, "gif: decode %s failed: %r\n", name);
307 for(n=0; images[n]; n++)
309 ims = malloc((n+1)*sizeof(Image*));
310 masks = malloc((n+1)*sizeof(Image*));
311 rgbv = malloc((n+1)*sizeof(Rawimage*));
312 if(masks==nil || rgbv==nil || ims==nil){
313 fprint(2, "gif: malloc of masks for %s failed: %r\n", name);
317 memset(masks, 0, (n+1)*sizeof(Image*));
318 memset(ims, 0, (n+1)*sizeof(Image*));
319 memset(rgbv, 0, (n+1)*sizeof(Rawimage*));
325 if(defaultcolor && screen->depth>8)
331 rgbv[k] = torgbv(images[k], !eflag);
333 if(outchan==GREY8 || (images[k]->chandesc==CY && threeflag==0))
334 rgbv[k] = totruecolor(images[k], CY);
336 rgbv[k] = totruecolor(images[k], CRGB24);
339 fprint(2, "gif: converting %s to local format failed: %r\n", name);
344 masks[k] = transparency(images[k], name);
345 if(rgbv[k]->chandesc == CY)
346 ims[k] = allocimage(display, rgbv[k]->r, GREY8, 0, 0);
348 ims[k] = allocimage(display, rgbv[k]->r, outchan, 0, 0);
350 fprint(2, "gif: allocimage %s failed: %r\n", name);
354 if(loadimage(ims[k], ims[k]->r, rgbv[k]->chans[0], rgbv[k]->chanlen) < 0){
355 fprint(2, "gif: loadimage %s failed: %r\n", name);
364 loopcount = images[0]->gifloopcount;
371 dt = images[k]->gifdelay*10;
374 while(n==1 || ecankbd()){
375 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04) /* an odd, democratic list */
381 /* loopcount -1 means no loop (this code's rule), loopcount 0 means loop forever (netscape's rule)*/
382 }while(loopcount==0 || ++nloop<loopcount);
383 /* loop count has run out */
386 drawop(screen, screen->clipr, display->white, nil, ZP, S);
389 fprint(2, "gif: warning: only writing first image in %d-image GIF %s\n", n, name);
391 if(images[0]->gifflags&TRANSP){
393 blackout(images[0], rgbv[0]);
395 chantostr(buf, outchan);
396 print("%11s %11d %11d %11d %11d ", buf,
397 rgbv[0]->r.min.x, rgbv[0]->r.min.y, rgbv[0]->r.max.x, rgbv[0]->r.max.y);
398 if(write(1, rgbv[0]->chans[0], rgbv[0]->chanlen) != rgbv[0]->chanlen){
399 fprint(2, "gif: %s: write error %r\n", name);
403 if(images[0]->gifflags&TRANSP){
405 blackout(images[0], rgbv[0]);
407 if(writerawimage(1, rgbv[0]) < 0){
408 fprint(2, "gif: %s: write error: %r\n", name);
416 for(k=0; images[k]; k++){
417 for(j=0; j<images[k]->nchans; j++)
418 free(images[k]->chans[j]);
419 free(images[k]->cmap);
421 free(rgbv[k]->chans[0]);