16 ulong outchan = CMAP8;
25 char *show(int, char*, int);
32 p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
33 p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
34 return rectaddpt(i->r, subpt(p2, p1));
42 if(new && getwindow(display, Refnone) < 0){
43 fprint(2, "png: can't reattach to window\n");
49 border(screen, r, -Border, nil, ZP);
50 draw(screen, r, image, nil, image->r.min);
51 flushimage(display, 1);
55 main(int argc, char *argv[])
61 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
71 case 'd': /* suppress display of image */
74 case 'e': /* disable floyd-steinberg error diffusion */
77 case 'k': /* force black and white */
81 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
84 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
91 case 'v': /* force RGBV */
95 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
103 fprint(2, "usage: png [-39cdekrtv] [file.png ...]\n");
109 err = show(0, "<stdin>", outchan);
111 for(i=0; i<argc; i++){
112 fd = open(argv[i], OREAD);
114 fprint(2, "png: can't open %s: %r\n", argv[i]);
117 err = show(fd, argv[i], outchan);
120 if((nineflag || cflag) && argc>1 && err==nil){
121 fprint(2, "png: exiting after one file\n");
130 show(int fd, char *name, int outc)
132 Rawimage **array, *r, *c;
140 if(Binit(&b, fd, OREAD) < 0)
143 array = Breadpng(&b, CRGB);
144 if(array == nil || array[0]==nil){
145 fprint(2, "png: decode %s failed: %r\n", name);
153 if(initdraw(0, 0, 0) < 0){
154 fprint(2, "png: initdraw failed: %r\n");
157 einit(Ekeyboard|Emouse);
160 if(defaultcolor && screen->depth>8 && outchan==CMAP8)
164 c = torgbv(r, !eflag);
171 outchan = CHAN2(CGrey, 8, CAlpha, 8);
183 fprint(2, "png: conversion of %s failed: %r\n", name);
187 i = allocimage(display, c->r, outchan, 0, 0);
189 fprint(2, "png: allocimage %s failed: %r\n", name);
192 if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
193 fprint(2, "png: loadimage %s of %d bytes failed: %r\n",
197 i2 = allocimage(display, c->r, outchan, 0, 0);
198 draw(i2, i2->r, display->black, nil, ZP);
199 draw(i2, i2->r, i, nil, i->r.min);
202 if((ch=ekbd())=='q' || ch==Kdel || ch==Keof)
204 draw(screen, screen->clipr, display->white, nil, ZP);
209 chantostr(buf, outchan);
210 len = (c->r.max.x - c->r.min.x) * (c->r.max.y - c->r.min.y);
225 if(c->chanlen != len)
226 fprint(2, "%s: writing %d bytes for len %ld chan %s\n",
227 argv0, c->chanlen, len, buf);
228 print("%11s %11d %11d %11d %11d ", buf,
229 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
230 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
231 fprint(2, "png: %s: write error %r\n", name);
235 if(writerawimage(1, c) < 0){
236 fprint(2, "png: %s: write error: %r\n", name);
240 for(j=0; j<r->nchans; j++)