15 ulong outchan = CMAP8;
24 char *show(int, char*);
31 p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
32 p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
33 return rectaddpt(i->r, subpt(p2, p1));
41 if(new && getwindow(display, Refnone) < 0){
42 fprint(2, "ppm: can't reattach to window\n");
48 border(screen, r, -Border, nil, ZP);
49 drawop(screen, r, image, nil, image->r.min, S);
50 flushimage(display, 1);
54 main(int argc, char *argv[])
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 */
99 fprint(2, "usage: ppm -39cdektv [file.ppm ...]\n");
105 err = show(0, "<stdin>");
107 for(i=0; i<argc; i++){
108 fd = open(argv[i], OREAD);
110 fprint(2, "ppm: can't open %s: %r\n", argv[i]);
113 err = show(fd, argv[i]);
116 if((nineflag || cflag) && argc>1 && err==nil){
117 fprint(2, "ppm: exiting after one file\n");
131 if(initdraw(0, 0, 0) < 0){
132 fprint(2, "ppm: initdraw failed: %r");
135 einit(Ekeyboard|Emouse);
142 show(int fd, char *name)
144 Rawimage **array, *r, *c;
149 array = readpixmap(fd, CRGB);
150 if(array == nil || array[0]==nil){
151 fprint(2, "ppm: decode %s failed: %r\n", name);
157 if(defaultcolor && screen->depth>8)
162 c = torgbv(r, !eflag);
164 if(outchan==GREY8 || (r->chandesc==CY && threeflag==0))
165 c = totruecolor(r, CY);
167 c = totruecolor(r, CRGB24);
170 fprint(2, "ppm: converting %s to local format failed: %r\n", name);
174 if(r->chandesc == CY)
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==Kdel || ch==Keof)
189 draw(screen, screen->clipr, display->white, nil, ZP);
194 if(r->chandesc == CY)
196 chantostr(buf, outchan);
197 print("%11s %11d %11d %11d %11d ", buf,
198 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
199 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
200 fprint(2, "ppm: %s: write error %r\n", name);
204 if(writerawimage(1, c) < 0){
205 fprint(2, "ppm: %s: write error: %r\n", name);
209 for(j=0; j<r->nchans; j++)