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 = insetrect(screen->clipr, Edge+Border);
39 r.max.x = r.min.x+Dx(image->r);
40 r.max.y = r.min.y+Dy(image->r);
41 border(screen, r, -Border, nil, ZP);
42 draw(screen, r, image, nil, image->r.min);
43 flushimage(display, 1);
49 fprint(2, "usage: png -39cdekrtv -W winsize [file.png ...]\n");
54 main(int argc, char *argv[])
62 winsize = EARGF(usage());
64 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
74 case 'd': /* suppress display of image */
77 case 'e': /* disable floyd-steinberg error diffusion */
80 case 'k': /* force black and white */
87 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
90 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
97 case 'v': /* force RGBV */
101 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
112 if(dflag==0 && colorspace==CYCbCr){ /* see if we should convert right to RGB */
113 fd = open("/dev/screen", OREAD);
116 if(read(fd, buf, 12)==12 && chantodepth(strtochan(buf))>8)
124 err = show(0, "<stdin>", outchan);
126 for(i=0; i<argc; i++){
127 fd = open(argv[i], OREAD);
129 fprint(2, "png: can't open %s: %r\n", argv[i]);
132 err = show(fd, argv[i], outchan);
135 if((nineflag || cflag) && argc>1 && err==nil){
136 fprint(2, "png: exiting after one file\n");
145 show(int fd, char *name, int outc)
147 Rawimage **array, *r, *c;
154 if(Binit(&b, fd, OREAD) < 0)
157 array = Breadpng(&b, colorspace);
158 if(array == nil || array[0]==nil){
159 fprint(2, "png: decode %s failed: %r\n", name);
165 if(!dflag && !inited){
166 if(initdraw(0, 0, 0) < 0){
167 fprint(2, "png: initdraw failed: %r\n");
170 einit(Ekeyboard|Emouse);
171 if(defaultcolor && screen->depth>8 && outchan==CMAP8)
176 c = torgbv(r, !eflag);
178 if(outchan==GREY8 || (r->chandesc==CY && threeflag==0)){
179 c = totruecolor(r, CY);
182 c = totruecolor(r, CRGB24);
185 fprint(2, "png: conversion of %s failed: %r\n", name);
189 if(c->chandesc == CY)
190 i = allocimage(display, c->r, GREY8, 0, 0);
192 i = allocimage(display, c->r, outchan, 0, 0);
194 fprint(2, "png: allocimage %s failed: %r\n", name);
197 if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
198 fprint(2, "png: loadimage %s failed: %r\n", name);
203 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
205 draw(screen, screen->clipr, display->white, nil, ZP);
210 chantostr(buf, outchan);
211 print("%11s %11d %11d %11d %11d ", buf,
212 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
213 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
214 fprint(2, "png: %s: write error %r\n", name);
218 if(writerawimage(1, c) < 0){
219 fprint(2, "png: %s: write error: %r\n", name);
223 for(j=0; j<r->nchans; j++)