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 = insetrect(screen->clipr, Edge+Border);
37 r.max.x = r.min.x+Dx(image->r);
38 r.max.y = r.min.y+Dy(image->r);
39 border(screen, r, -Border, nil, ZP);
40 draw(screen, r, image, nil, image->r.min);
41 flushimage(display, 1);
47 fprint(2, "usage: ppm -39cdektv -W winsize [file.ppm ...]\n");
52 main(int argc, char *argv[])
59 winsize = EARGF(usage());
61 case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
64 case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
71 case 'c': /* produce encoded, compressed, bitmap file; no display by default */
78 case 'd': /* suppress display of image */
81 case 'e': /* disable floyd-steinberg error diffusion */
84 case 'k': /* force black and white */
88 case 'v': /* force RGBV */
92 case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
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)
175 i = allocimage(display, c->r, GREY8, 0, 0);
177 i = allocimage(display, c->r, outchan, 0, 0);
179 fprint(2, "ppm: allocimage %s failed: %r\n", name);
182 if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
183 fprint(2, "ppm: loadimage %s failed: %r\n", name);
188 if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
190 draw(screen, screen->clipr, display->white, nil, ZP);
195 chantostr(buf, outchan);
196 print("%11s %11d %11d %11d %11d ", buf,
197 c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
198 if(write(1, c->chans[0], c->chanlen) != c->chanlen){
199 fprint(2, "ppm: %s: write error %r\n", name);
203 if(writerawimage(1, c) < 0){
204 fprint(2, "ppm: %s: write error: %r\n", name);
208 for(j=0; j<r->nchans; j++)