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);
47 main(int argc, char *argv[])
54 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
64 case 'd': /* suppress display of image */
67 case 'e': /* disable floyd-steinberg error diffusion */
70 case 'k': /* force black and white */
77 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
80 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
87 case 'v': /* force RGBV */
91 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
99 fprint(2, "usage: png [-39cdekrtv] [file.png ...]\n");
103 if(dflag==0 && colorspace==CYCbCr){ /* see if we should convert right to RGB */
104 fd = open("/dev/screen", OREAD);
107 if(read(fd, buf, 12)==12 && chantodepth(strtochan(buf))>8)
115 err = show(0, "<stdin>", outchan);
117 for(i=0; i<argc; i++){
118 fd = open(argv[i], OREAD);
120 fprint(2, "png: can't open %s: %r\n", argv[i]);
123 err = show(fd, argv[i], outchan);
126 if((nineflag || cflag) && argc>1 && err==nil){
127 fprint(2, "png: exiting after one file\n");
136 show(int fd, char *name, int outc)
138 Rawimage **array, *r, *c;
146 if(Binit(&b, fd, OREAD) < 0)
149 array = Breadpng(&b, colorspace);
150 if(array == nil || array[0]==nil){
151 fprint(2, "png: decode %s failed: %r\n", name);
159 if(initdraw(0, 0, 0) < 0){
160 fprint(2, "png: initdraw failed: %r\n");
163 einit(Ekeyboard|Emouse);
166 if(defaultcolor && screen->depth>8 && outchan==CMAP8)
170 c = torgbv(r, !eflag);
177 outchan = CHAN2(CGrey, 8, CAlpha, 8);
189 fprint(2, "png: conversion of %s failed: %r\n", name);
193 i = allocimage(display, c->r, outchan, 0, 0);
195 fprint(2, "png: allocimage %s failed: %r\n", name);
198 if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
199 fprint(2, "png: loadimage %s of %d bytes failed: %r\n",
203 i2 = allocimage(display, c->r, outchan, 0, 0);
204 draw(i2, i2->r, display->black, nil, ZP);
205 draw(i2, i2->r, i, nil, i->r.min);
208 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
210 draw(screen, screen->clipr, display->white, nil, ZP);
215 chantostr(buf, outchan);
216 len = (c->r.max.x - c->r.min.x) * (c->r.max.y - c->r.min.y);
231 if(c->chanlen != len)
232 fprint(2, "%s: writing %d bytes for len %ld chan %s\n",
233 argv0, c->chanlen, len, buf);
234 print("%11s %11d %11d %11d %11d ", buf,
235 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
236 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
237 fprint(2, "png: %s: write error %r\n", name);
241 if(writerawimage(1, c) < 0){
242 fprint(2, "png: %s: write error: %r\n", name);
246 for(j=0; j<r->nchans; j++)