14 ulong outchan = CMAP8;
23 char *show(int, char*);
30 if(new && getwindow(display, Refnone) < 0){
31 fprint(2, "ppm: can't reattach to window\n");
36 r = rectaddpt(image->r, subpt(screen->r.min, image->r.min));
39 draw(screen, r, image, nil, image->r.min);
40 flushimage(display, 1);
46 fprint(2, "usage: ppm -39cdektv -W winsize [file.ppm ...]\n");
51 main(int argc, char *argv[])
58 winsize = EARGF(usage());
60 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
63 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
70 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
77 case 'd': /* suppress display of image */
80 case 'e': /* disable floyd-steinberg error diffusion */
83 case 'k': /* force black and white */
87 case 'v': /* force RGBV */
91 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
104 err = show(0, "<stdin>");
106 for(i=0; i<argc; i++){
107 fd = open(argv[i], OREAD);
109 fprint(2, "ppm: can't open %s: %r\n", argv[i]);
112 err = show(fd, argv[i]);
115 if((nineflag || cflag) && argc>1 && err==nil){
116 fprint(2, "ppm: exiting after one file\n");
130 if(initdraw(0, 0, 0) < 0){
131 fprint(2, "ppm: initdraw failed: %r");
134 einit(Ekeyboard|Emouse);
141 show(int fd, char *name)
143 Rawimage **array, *r, *c;
148 array = readpixmap(fd, CRGB);
149 if(array == nil || array[0]==nil){
150 fprint(2, "ppm: decode %s failed: %r\n", name);
156 if(defaultcolor && screen->depth>8)
161 c = torgbv(r, !eflag);
163 if(outchan==GREY8 || (r->chandesc==CY && threeflag==0))
164 c = totruecolor(r, CY);
166 c = totruecolor(r, CRGB24);
169 fprint(2, "ppm: converting %s to local format failed: %r\n", name);
173 if(r->chandesc == CY)
174 i = allocimage(display, c->r, GREY8, 0, 0);
176 i = allocimage(display, c->r, outchan, 0, 0);
178 fprint(2, "ppm: allocimage %s failed: %r\n", name);
181 if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
182 fprint(2, "ppm: loadimage %s failed: %r\n", name);
187 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
189 draw(screen, screen->clipr, display->white, nil, ZP);
194 chantostr(buf, outchan);
195 print("%11s %11d %11d %11d %11d ", buf,
196 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
197 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
198 fprint(2, "ppm: %s: write error %r\n", name);
202 if(writerawimage(1, c) < 0){
203 fprint(2, "ppm: %s: write error: %r\n", name);
207 for(j=0; j<r->nchans; j++)