14 ulong outchan = CMAP8;
24 char *show(int, char*);
31 if(allims==nil || allims[0]==nil)
33 r = insetrect(screen->clipr, Edge+Border);
34 r.max.x = r.min.x+Dx(allims[0]->r);
35 r.max.y = r.min.y+Dy(allims[0]->r);
44 if(new && getwindow(display, Refnone) < 0){
45 fprint(2, "gif: can't reattach to window\n");
48 if(allims==nil || allims[which]==nil)
51 border(screen, r, -Border, nil, ZP);
52 draw(screen, r, allims[which], nil, allims[which]->r.min);
53 flushimage(display, 1);
57 main(int argc, char *argv[])
63 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
66 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
73 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
80 case 'd': /* suppress display of image */
83 case 'e': /* disable floyd-steinberg error diffusion */
86 case 'k': /* force black and white */
90 case 'v': /* force RGBV */
94 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
102 fprint(2, "usage: gif -39cdektv [file.gif ...]\n");
108 err = show(0, "<stdin>");
110 for(i=0; i<argc; i++){
111 fd = open(argv[i], OREAD);
113 fprint(2, "gif: can't open %s: %r\n", argv[i]);
116 err = show(fd, argv[i]);
119 if(output && argc>1 && err==nil){
120 fprint(2, "gif: exiting after one file\n");
129 transparency(Rawimage *r, char *name)
133 uchar *pic, *mpic, *mask;
135 if((r->gifflags&TRANSP) == 0)
137 i = allocimage(display, r->r, GREY8, 0, 0);
139 fprint(2, "gif: allocimage for mask of %s failed: %r\n", name);
143 mask = malloc(r->chanlen);
145 fprint(2, "gif: malloc for mask of %s failed: %r\n", name);
149 index = r->giftrindex;
151 for(j=0; j<r->chanlen; j++)
156 if(loadimage(i, i->r, mask, r->chanlen) < 0){
157 fprint(2, "gif: loadimage for mask of %s failed: %r\n", name);
166 /* interleave alpha values of 0xFF in data stream. alpha value comes first, then b g r */
168 expand(uchar *u, int chanlen, int nchan)
173 v = malloc(chanlen*(nchan+1));
175 fprint(2, "gif: malloc fails: %r\n");
180 for(j=0; j<chanlen; j++){
182 for(k=0; k<nchan; k++)
189 addalpha(Rawimage *i)
195 i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
196 i->chanlen = 2*(i->chanlen/1);
197 i->chandesc = CRGBVA16;
198 outchan = CHAN2(CMap, 8, CAlpha, 8);
202 i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
203 i->chanlen = 2*(i->chanlen/1);
205 outchan = CHAN2(CGrey, 8, CAlpha, 8);
209 i->chans[0] = expand(i->chans[0], i->chanlen/3, 3);
210 i->chanlen = 4*(i->chanlen/3);
211 i->chandesc = CRGBA32;
216 chantostr(buf, outchan);
217 fprint(2, "gif: can't add alpha to type %s\n", buf);
223 * Called only when writing output. If the output is RGBA32,
224 * we must write four bytes per pixel instead of two.
225 * There's always at least two: data plus alpha.
226 * r is used only for reference; the image is already in c.
229 blackout(Rawimage *r, Rawimage *c)
236 trindex = r->giftrindex;
237 if(outchan == RGBA32)
238 for(i=0; i<r->chanlen; i++){
251 for(i=0; i<r->chanlen; i++){
269 if(initdraw(0, 0, 0) < 0){
270 fprint(2, "gif: initdraw failed: %r\n");
273 einit(Ekeyboard|Emouse);
280 show(int fd, char *name)
282 Rawimage **images, **rgbv;
283 Image *tmp, *msk, *img, *dst, **ims;
285 int j, k, n, ch, nloop, loopcount, dt;
290 images = readgif(fd, CRGB);
292 fprint(2, "gif: decode %s failed: %r\n", name);
295 for(n=0; images[n]; n++)
299 combinerect(&r, images[n]->r);
301 ims = malloc((n+1)*sizeof(Image*));
302 rgbv = malloc((n+1)*sizeof(Rawimage*));
303 if(rgbv==nil || ims==nil){
304 fprint(2, "gif: malloc of masks for %s failed: %r\n", name);
308 memset(ims, 0, (n+1)*sizeof(Image*));
309 memset(rgbv, 0, (n+1)*sizeof(Rawimage*));
315 if(defaultcolor && screen->depth>8)
321 rgbv[k] = torgbv(images[k], !eflag);
323 if(outchan==GREY8 || (images[k]->chandesc==CY && threeflag==0))
324 rgbv[k] = totruecolor(images[k], CY);
326 rgbv[k] = totruecolor(images[k], CRGB24);
329 fprint(2, "gif: converting %s to local format failed: %r\n", name);
334 msk = transparency(images[k], name);
335 if(rgbv[k]->chandesc == CY)
336 img = allocimage(display, rgbv[k]->r, GREY8, 0, 0);
338 img = allocimage(display, rgbv[k]->r, outchan, 0, 0);
340 tmp = allocimage(display, r, img->chan, 0, DWhite);
341 ims[k]= dst = allocimage(display, r, tmp->chan, 0, DWhite);
342 if(tmp == nil || img == nil || dst == nil){
343 fprint(2, "gif: allocimage %s failed: %r\n", name);
347 if(loadimage(img, img->r, rgbv[k]->chans[0], rgbv[k]->chanlen) < 0){
348 fprint(2, "gif: loadimage %s failed: %r\n", name);
352 switch((images[k]->gifflags>>2)&7){
354 draw(tmp, img->r, img, msk, img->r.min);
355 draw(dst, tmp->r, tmp, nil, tmp->r.min);
358 draw(tmp, img->r, display->white, msk, img->r.min);
361 draw(dst, tmp->r, tmp, nil, tmp->r.min);
362 draw(dst, img->r, img, msk, img->r.min);
373 loopcount = images[0]->gifloopcount;
380 dt = images[k]->gifdelay*10;
383 while(n==1 || ecankbd()){
384 /* an odd, democratic list */
385 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
391 /* loopcount -1 means no loop (this code's rule), loopcount 0 means loop forever (netscape's rule)*/
392 }while(loopcount==0 || ++nloop<loopcount);
393 /* loop count has run out */
396 draw(screen, screen->clipr, display->white, nil, ZP);
399 fprint(2, "gif: warning: only writing first image in %d-image GIF %s\n", n, name);
401 if(images[0]->gifflags&TRANSP){
403 blackout(images[0], rgbv[0]);
405 chantostr(buf, outchan);
406 print("%11s %11d %11d %11d %11d ", buf,
407 rgbv[0]->r.min.x, rgbv[0]->r.min.y, rgbv[0]->r.max.x, rgbv[0]->r.max.y);
408 if(write(1, rgbv[0]->chans[0], rgbv[0]->chanlen) != rgbv[0]->chanlen){
409 fprint(2, "gif: %s: write error %r\n", name);
413 if(images[0]->gifflags&TRANSP){
415 blackout(images[0], rgbv[0]);
417 if(writerawimage(1, rgbv[0]) < 0){
418 fprint(2, "gif: %s: write error: %r\n", name);
425 for(k=0; images[k]; k++){
426 for(j=0; j<images[k]->nchans; j++)
427 free(images[k]->chans[j]);
428 free(images[k]->cmap);
430 free(rgbv[k]->chans[0]);