15 ulong outchan = CMAP8;
25 char *show(int, char*);
32 if(allims==nil || allims[0]==nil)
35 p1 = addpt(divpt(subpt(allims[0]->r.max, allims[0]->r.min), 2), allims[0]->r.min);
36 p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
37 return rectaddpt(allims[0]->r, subpt(p2, p1));
45 if(new && getwindow(display, Refnone) < 0){
46 fprint(2, "gif: can't reattach to window\n");
49 if(allims==nil || allims[which]==nil)
52 border(screen, r, -Border, nil, ZP);
53 draw(screen, r, allims[which], nil, allims[which]->r.min);
54 flushimage(display, 1);
58 main(int argc, char *argv[])
64 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
67 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
74 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
81 case 'd': /* suppress display of image */
84 case 'e': /* disable floyd-steinberg error diffusion */
87 case 'k': /* force black and white */
91 case 'v': /* force RGBV */
95 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
103 fprint(2, "usage: gif -39cdektv [file.gif ...]\n");
109 err = show(0, "<stdin>");
111 for(i=0; i<argc; i++){
112 fd = open(argv[i], OREAD);
114 fprint(2, "gif: can't open %s: %r\n", argv[i]);
117 err = show(fd, argv[i]);
120 if(output && argc>1 && err==nil){
121 fprint(2, "gif: exiting after one file\n");
130 transparency(Rawimage *r, char *name)
134 uchar *pic, *mpic, *mask;
136 if((r->gifflags&TRANSP) == 0)
138 i = allocimage(display, r->r, GREY8, 0, 0);
140 fprint(2, "gif: allocimage for mask of %s failed: %r\n", name);
144 mask = malloc(r->chanlen);
146 fprint(2, "gif: malloc for mask of %s failed: %r\n", name);
150 index = r->giftrindex;
152 for(j=0; j<r->chanlen; j++)
157 if(loadimage(i, i->r, mask, r->chanlen) < 0){
158 fprint(2, "gif: loadimage for mask of %s failed: %r\n", name);
167 /* interleave alpha values of 0xFF in data stream. alpha value comes first, then b g r */
169 expand(uchar *u, int chanlen, int nchan)
174 v = malloc(chanlen*(nchan+1));
176 fprint(2, "gif: malloc fails: %r\n");
181 for(j=0; j<chanlen; j++){
183 for(k=0; k<nchan; k++)
190 addalpha(Rawimage *i)
196 i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
197 i->chanlen = 2*(i->chanlen/1);
198 i->chandesc = CRGBVA16;
199 outchan = CHAN2(CMap, 8, CAlpha, 8);
203 i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
204 i->chanlen = 2*(i->chanlen/1);
206 outchan = CHAN2(CGrey, 8, CAlpha, 8);
210 i->chans[0] = expand(i->chans[0], i->chanlen/3, 3);
211 i->chanlen = 4*(i->chanlen/3);
212 i->chandesc = CRGBA32;
217 chantostr(buf, outchan);
218 fprint(2, "gif: can't add alpha to type %s\n", buf);
224 * Called only when writing output. If the output is RGBA32,
225 * we must write four bytes per pixel instead of two.
226 * There's always at least two: data plus alpha.
227 * r is used only for reference; the image is already in c.
230 blackout(Rawimage *r, Rawimage *c)
237 trindex = r->giftrindex;
238 if(outchan == RGBA32)
239 for(i=0; i<r->chanlen; i++){
252 for(i=0; i<r->chanlen; i++){
270 if(initdraw(0, 0, 0) < 0){
271 fprint(2, "gif: initdraw failed: %r\n");
274 einit(Ekeyboard|Emouse);
281 show(int fd, char *name)
283 Rawimage **images, **rgbv;
284 Image *tmp, *msk, *img, *dst, **ims;
286 int j, k, n, ch, nloop, loopcount, dt;
291 images = readgif(fd, CRGB, dflag);
293 fprint(2, "gif: decode %s failed: %r\n", name);
296 for(n=0; images[n]; n++)
300 combinerect(&r, images[n]->r);
302 ims = malloc((n+1)*sizeof(Image*));
303 rgbv = malloc((n+1)*sizeof(Rawimage*));
304 if(rgbv==nil || ims==nil){
305 fprint(2, "gif: malloc of masks for %s failed: %r\n", name);
309 memset(ims, 0, (n+1)*sizeof(Image*));
310 memset(rgbv, 0, (n+1)*sizeof(Rawimage*));
316 if(defaultcolor && screen->depth>8)
322 rgbv[k] = torgbv(images[k], !eflag);
324 if(outchan==GREY8 || (images[k]->chandesc==CY && threeflag==0))
325 rgbv[k] = totruecolor(images[k], CY);
327 rgbv[k] = totruecolor(images[k], CRGB24);
330 fprint(2, "gif: converting %s to local format failed: %r\n", name);
335 msk = transparency(images[k], name);
336 if(rgbv[k]->chandesc == CY)
337 img = allocimage(display, rgbv[k]->r, GREY8, 0, 0);
339 img = allocimage(display, rgbv[k]->r, outchan, 0, 0);
341 tmp = allocimage(display, r, img->chan, 0, DWhite);
342 ims[k]= dst = allocimage(display, r, tmp->chan, 0, DWhite);
343 if(tmp == nil || img == nil || dst == nil){
344 fprint(2, "gif: allocimage %s failed: %r\n", name);
348 if(loadimage(img, img->r, rgbv[k]->chans[0], rgbv[k]->chanlen) < 0){
349 fprint(2, "gif: loadimage %s failed: %r\n", name);
353 switch((images[k]->gifflags>>2)&7){
355 draw(tmp, img->r, img, msk, img->r.min);
356 draw(dst, tmp->r, tmp, nil, tmp->r.min);
359 draw(tmp, img->r, display->white, msk, img->r.min);
362 draw(dst, tmp->r, tmp, nil, tmp->r.min);
363 draw(dst, img->r, img, msk, img->r.min);
374 loopcount = images[0]->gifloopcount;
381 dt = images[k]->gifdelay*10;
384 while(n==1 || ecankbd()){
385 /* an odd, democratic list */
386 if((ch=ekbd())=='q' || ch==Kdel || ch==Keof)
392 /* loopcount -1 means no loop (this code's rule), loopcount 0 means loop forever (netscape's rule)*/
393 }while(loopcount==0 || ++nloop<loopcount);
394 /* loop count has run out */
397 draw(screen, screen->clipr, display->white, nil, ZP);
400 if(images[0]->gifflags&TRANSP){
402 blackout(images[0], rgbv[0]);
404 chantostr(buf, outchan);
405 print("%11s %11d %11d %11d %11d ", buf,
406 rgbv[0]->r.min.x, rgbv[0]->r.min.y, rgbv[0]->r.max.x, rgbv[0]->r.max.y);
407 if(write(1, rgbv[0]->chans[0], rgbv[0]->chanlen) != rgbv[0]->chanlen){
408 fprint(2, "gif: %s: write error %r\n", name);
412 if(images[0]->gifflags&TRANSP){
414 blackout(images[0], rgbv[0]);
416 if(writerawimage(1, rgbv[0]) < 0){
417 fprint(2, "gif: %s: write error: %r\n", name);
424 for(k=0; images[k]; k++){
425 for(j=0; j<images[k]->nchans; j++)
426 free(images[k]->chans[j]);
427 free(images[k]->cmap);
429 free(rgbv[k]->chans[0]);