14 int colorspace = CRGB;
16 ulong outchan = CMAP8;
25 char *show(int, char*, int);
32 if(new && getwindow(display, Refnone) < 0){
33 fprint(2, "png: can't reattach to window\n");
38 r = rectaddpt(image->r, subpt(screen->r.min, image->r.min));
41 draw(screen, r, image, nil, image->r.min);
42 flushimage(display, 1);
48 fprint(2, "usage: png -39cdekrtv -W winsize [file.png ...]\n");
53 main(int argc, char *argv[])
61 winsize = EARGF(usage());
63 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
73 case 'd': /* suppress display of image */
76 case 'e': /* disable floyd-steinberg error diffusion */
79 case 'k': /* force black and white */
86 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
89 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
96 case 'v': /* force RGBV */
100 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
111 if(dflag==0 && colorspace==CYCbCr){ /* see if we should convert right to RGB */
112 fd = open("/dev/screen", OREAD);
115 if(read(fd, buf, 12)==12 && chantodepth(strtochan(buf))>8)
123 err = show(0, "<stdin>", outchan);
125 for(i=0; i<argc; i++){
126 fd = open(argv[i], OREAD);
128 fprint(2, "png: can't open %s: %r\n", argv[i]);
131 err = show(fd, argv[i], outchan);
134 if((nineflag || cflag) && argc>1 && err==nil){
135 fprint(2, "png: exiting after one file\n");
144 show(int fd, char *name, int outc)
146 Rawimage **array, *r, *c;
153 if(Binit(&b, fd, OREAD) < 0)
156 array = Breadpng(&b, colorspace);
157 if(array == nil || array[0]==nil){
158 fprint(2, "png: decode %s failed: %r\n", name);
164 if(!dflag && !inited){
165 if(initdraw(0, 0, 0) < 0){
166 fprint(2, "png: initdraw failed: %r\n");
169 einit(Ekeyboard|Emouse);
170 if(defaultcolor && screen->depth>8 && outchan==CMAP8)
175 c = torgbv(r, !eflag);
177 if(outchan==GREY8 || (r->chandesc==CY && threeflag==0)){
178 c = totruecolor(r, CY);
181 c = totruecolor(r, CRGB24);
184 fprint(2, "png: conversion of %s failed: %r\n", name);
188 if(c->chandesc == CY)
189 i = allocimage(display, c->r, GREY8, 0, 0);
191 i = allocimage(display, c->r, outchan, 0, 0);
193 fprint(2, "png: allocimage %s failed: %r\n", name);
196 if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
197 fprint(2, "png: loadimage %s failed: %r\n", name);
202 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
204 draw(screen, screen->clipr, display->white, nil, ZP);
209 chantostr(buf, outchan);
210 print("%11s %11d %11d %11d %11d ", buf,
211 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
212 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
213 fprint(2, "png: %s: write error %r\n", name);
217 if(writerawimage(1, c) < 0){
218 fprint(2, "png: %s: write error: %r\n", name);
222 for(j=0; j<r->nchans; j++)