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)
52 border(screen, r, -Border, nil, ZP);
53 r.min.x += allims[which]->r.min.x - allims[0]->r.min.x;
54 r.min.y += allims[which]->r.min.y - allims[0]->r.min.y;
55 drawop(screen, r, allims[which], allmasks[which], allims[which]->r.min, S);
56 flushimage(display, 1);
60 main(int argc, char *argv[])
66 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
69 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
76 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
83 case 'd': /* suppress display of image */
86 case 'e': /* disable floyd-steinberg error diffusion */
89 case 'k': /* force black and white */
93 case 'v': /* force RGBV */
97 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
105 fprint(2, "usage: gif -39cdektv [file.gif ...]\n");
111 err = show(0, "<stdin>");
113 for(i=0; i<argc; i++){
114 fd = open(argv[i], OREAD);
116 fprint(2, "gif: can't open %s: %r\n", argv[i]);
119 err = show(fd, argv[i]);
122 if(output && argc>1 && err==nil){
123 fprint(2, "gif: exiting after one file\n");
132 transparency(Rawimage *r, char *name)
136 uchar *pic, *mpic, *mask;
138 if((r->gifflags&TRANSP) == 0)
140 i = allocimage(display, r->r, GREY8, 0, 0);
142 fprint(2, "gif: allocimage for mask of %s failed: %r\n", name);
146 mask = malloc(r->chanlen);
148 fprint(2, "gif: malloc for mask of %s failed: %r\n", name);
152 index = r->giftrindex;
154 for(j=0; j<r->chanlen; j++)
159 if(loadimage(i, i->r, mask, r->chanlen) < 0){
160 fprint(2, "gif: loadimage for mask of %s failed: %r\n", name);
169 /* interleave alpha values of 0xFF in data stream. alpha value comes first, then b g r */
171 expand(uchar *u, int chanlen, int nchan)
176 v = malloc(chanlen*(nchan+1));
178 fprint(2, "gif: malloc fails: %r\n");
183 for(j=0; j<chanlen; j++){
185 for(k=0; k<nchan; k++)
192 addalpha(Rawimage *i)
198 i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
199 i->chanlen = 2*(i->chanlen/1);
200 i->chandesc = CRGBVA16;
201 outchan = CHAN2(CMap, 8, CAlpha, 8);
205 i->chans[0] = expand(i->chans[0], i->chanlen/1, 1);
206 i->chanlen = 2*(i->chanlen/1);
208 outchan = CHAN2(CGrey, 8, CAlpha, 8);
212 i->chans[0] = expand(i->chans[0], i->chanlen/3, 3);
213 i->chanlen = 4*(i->chanlen/3);
214 i->chandesc = CRGBA32;
219 chantostr(buf, outchan);
220 fprint(2, "gif: can't add alpha to type %s\n", buf);
226 * Called only when writing output. If the output is RGBA32,
227 * we must write four bytes per pixel instead of two.
228 * There's always at least two: data plus alpha.
229 * r is used only for reference; the image is already in c.
232 whiteout(Rawimage *r, Rawimage *c)
239 trindex = r->giftrindex;
240 if(outchan == RGBA32)
241 for(i=0; i<r->chanlen; i++){
254 for(i=0; i<r->chanlen; i++){
272 if(initdraw(0, 0, 0) < 0){
273 fprint(2, "gif: initdraw failed: %r\n");
276 einit(Ekeyboard|Emouse);
283 show(int fd, char *name)
285 Rawimage **images, **rgbv;
286 Image **ims, **masks;
287 int j, k, n, ch, nloop, loopcount, dt;
292 images = readgif(fd, CRGB);
294 fprint(2, "gif: decode %s failed: %r\n", name);
297 for(n=0; images[n]; n++)
299 ims = malloc((n+1)*sizeof(Image*));
300 masks = malloc((n+1)*sizeof(Image*));
301 rgbv = malloc((n+1)*sizeof(Rawimage*));
302 if(masks==nil || rgbv==nil || ims==nil){
303 fprint(2, "gif: malloc of masks for %s failed: %r\n", name);
307 memset(masks, 0, (n+1)*sizeof(Image*));
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 masks[k] = transparency(images[k], name);
335 if(rgbv[k]->chandesc == CY)
336 ims[k] = allocimage(display, rgbv[k]->r, GREY8, 0, 0);
338 ims[k] = allocimage(display, rgbv[k]->r, outchan, 0, 0);
340 fprint(2, "gif: allocimage %s failed: %r\n", name);
344 if(loadimage(ims[k], ims[k]->r, rgbv[k]->chans[0], rgbv[k]->chanlen) < 0){
345 fprint(2, "gif: loadimage %s failed: %r\n", name);
354 loopcount = images[0]->gifloopcount;
361 dt = images[k]->gifdelay*10;
364 while(n==1 || ecankbd()){
365 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04) /* an odd, democratic list */
371 /* loopcount -1 means no loop (this code's rule), loopcount 0 means loop forever (netscape's rule)*/
372 }while(loopcount==0 || ++nloop<loopcount);
373 /* loop count has run out */
376 drawop(screen, screen->clipr, display->white, nil, ZP, S);
379 fprint(2, "gif: warning: only writing first image in %d-image GIF %s\n", n, name);
381 if(images[0]->gifflags&TRANSP){
383 whiteout(images[0], rgbv[0]);
385 chantostr(buf, outchan);
386 print("%11s %11d %11d %11d %11d ", buf,
387 rgbv[0]->r.min.x, rgbv[0]->r.min.y, rgbv[0]->r.max.x, rgbv[0]->r.max.y);
388 if(write(1, rgbv[0]->chans[0], rgbv[0]->chanlen) != rgbv[0]->chanlen){
389 fprint(2, "gif: %s: write error %r\n", name);
393 if(images[0]->gifflags&TRANSP){
395 whiteout(images[0], rgbv[0]);
397 if(writerawimage(1, rgbv[0]) < 0){
398 fprint(2, "gif: %s: write error: %r\n", name);
406 for(k=0; images[k]; k++){
407 for(j=0; j<images[k]->nchans; j++)
408 free(images[k]->chans[j]);
409 free(images[k]->cmap);
411 free(rgbv[k]->chans[0]);