ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 1.3
Committed: Fri Mar 2 12:22:37 1990 UTC (34 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.2: +36 -24 lines
Log Message:
final bug fixes, working version of x11image

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1990 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * x11image.c - driver for X-windows
9     *
10     * 3/1/90
11     */
12    
13     /*
14     * Modified for X11
15     *
16     * January 1990
17     *
18     * Anat Grynberg and Greg Ward
19     */
20    
21    
22     #include "standard.h"
23    
24     #include <X11/Xlib.h>
25     #include <X11/cursorfont.h>
26     #include <X11/Xutil.h>
27    
28     #include "color.h"
29     #include "view.h"
30     #include "pic.h"
31     #include "x11raster.h"
32     #include "random.h"
33    
34     #define FONTNAME "8x13" /* text font we'll use */
35    
36     #define CTRL(c) ('c'-'@')
37    
38     #define BORWIDTH 5 /* border width */
39    
40     #define ourscreen DefaultScreen(thedisplay)
41     #define ourblack BlackPixel(thedisplay,ourscreen)
42     #define ourwhite WhitePixel(thedisplay,ourscreen)
43     #define ourroot RootWindow(thedisplay,ourscreen)
44     #define ourgc DefaultGC(thedisplay,ourscreen)
45    
46 greg 1.2 #define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras)
47    
48 greg 1.1 double gamcor = 2.2; /* gamma correction */
49    
50     int dither = 1; /* dither colors? */
51     int fast = 0; /* keep picture in Pixmap? */
52    
53     Window wind = 0; /* our output window */
54     Font fontid; /* our font */
55    
56 greg 1.2 int maxcolors = 0; /* maximum colors */
57 greg 1.1 int greyscale = 0; /* in grey */
58    
59     int scale = 0; /* scalefactor; power of two */
60    
61     int xoff = 0; /* x image offset */
62     int yoff = 0; /* y image offset */
63    
64     VIEW ourview = STDVIEW; /* image view parameters */
65     int gotview = 0; /* got parameters from file */
66    
67     COLR *scanline; /* scan line buffer */
68    
69     int xmax, ymax; /* picture resolution */
70     int width, height; /* window size */
71     char *fname = NULL; /* input file name */
72     FILE *fin = stdin; /* input file */
73     long *scanpos = NULL; /* scan line positions in file */
74     int cury = 0; /* current scan location */
75    
76     double exposure = 1.0; /* exposure compensation used */
77    
78     XRASTER *ourras; /* our stored image */
79     unsigned char *ourdata; /* our image data */
80    
81     struct {
82     int xmin, ymin, xsiz, ysiz;
83     } box = {0, 0, 0, 0}; /* current box */
84    
85     char *geometry = NULL; /* geometry specification */
86    
87     char *progname;
88    
89     char errmsg[128];
90    
91     extern long ftell();
92    
93     extern char *malloc(), *calloc();
94    
95     extern double atof(), pow(), log();
96    
97     Display *thedisplay;
98    
99     main(argc, argv)
100     int argc;
101     char *argv[];
102     {
103     int headline();
104     int i;
105    
106     progname = argv[0];
107    
108     for (i = 1; i < argc; i++)
109     if (argv[i][0] == '-')
110     switch (argv[i][1]) {
111     case 'c':
112     maxcolors = atoi(argv[++i]);
113     break;
114     case 'b':
115     greyscale = !greyscale;
116     break;
117     case 'm':
118     maxcolors = 2;
119     break;
120     case 'd':
121     dither = !dither;
122     break;
123     case 'f':
124     fast = !fast;
125     break;
126     case 'e':
127     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
128     goto userr;
129     scale = atoi(argv[++i]);
130     break;
131     case 'g':
132     if (!strcmp(argv[i], "-geometry"))
133     geometry = argv[++i];
134     else
135     gamcor = atof(argv[++i]);
136     break;
137     default:
138     goto userr;
139     }
140 greg 1.3 else if (argv[i][0] == '=')
141     geometry = argv[i];
142 greg 1.1 else
143     break;
144    
145 greg 1.3 if (i == argc-1) {
146 greg 1.1 fname = argv[i];
147     fin = fopen(fname, "r");
148     if (fin == NULL) {
149     sprintf(errmsg, "can't open file \"%s\"", fname);
150     quiterr(errmsg);
151     }
152 greg 1.3 } else if (i != argc)
153     goto userr;
154 greg 1.1 /* get header */
155     getheader(fin, headline);
156     /* get picture dimensions */
157     if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
158     quiterr("bad picture size");
159     /* set view parameters */
160     if (gotview && setview(&ourview) != NULL)
161     gotview = 0;
162     if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL)
163     quiterr("out of memory");
164    
165     init(); /* get file and open window */
166    
167     for ( ; ; )
168     getevent(); /* main loop */
169     userr:
170     fprintf(stderr,
171     "Usage: %s [-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
172     progname);
173     quit(1);
174     }
175    
176    
177     headline(s) /* get relevant info from header */
178     char *s;
179     {
180     static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL};
181     register char **an;
182    
183 greg 1.2 if (isexpos(s))
184     exposure *= exposval(s);
185 greg 1.1 else
186     for (an = altname; *an != NULL; an++)
187     if (!strncmp(*an, s, strlen(*an))) {
188 greg 1.2 if (sscanview(&ourview, s+strlen(*an)) > 0)
189 greg 1.1 gotview++;
190     return;
191     }
192     }
193    
194    
195     init() /* get data and open window */
196     {
197     XSizeHints oursizhints;
198     register int i;
199    
200     if (fname != NULL) {
201     scanpos = (long *)malloc(ymax*sizeof(long));
202     if (scanpos == NULL)
203     goto memerr;
204     for (i = 0; i < ymax; i++)
205     scanpos[i] = -1;
206     }
207     if ((thedisplay = XOpenDisplay(NULL)) == NULL)
208     quiterr("can't open display; DISPLAY variable set?");
209     wind = XCreateSimpleWindow(thedisplay,
210     ourroot, 0, 0, xmax, ymax, BORWIDTH, ourblack, ourwhite);
211     if (wind == 0)
212     quiterr("can't create window");
213     if (maxcolors == 0) { /* get number of available colors */
214 greg 1.2 i = DisplayPlanes(thedisplay,ourscreen);
215     maxcolors = i > 8 ? 256 : 1<<i;
216 greg 1.1 if (maxcolors > 4) maxcolors -= 2;
217     }
218     fontid = XLoadFont(thedisplay, FONTNAME);
219     if (fontid == 0)
220     quiterr("can't get font");
221     XSetFont(thedisplay, ourgc, fontid);
222     XStoreName(thedisplay, wind, fname == NULL ? progname : fname);
223     XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay,
224     XC_diamond_cross));
225     if (geometry != NULL) {
226     bzero((char *)&oursizhints, sizeof(oursizhints));
227     i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y,
228     &oursizhints.width, &oursizhints.height);
229 greg 1.3 if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue))
230     oursizhints.flags |= USSize;
231     else {
232     oursizhints.width = xmax;
233     oursizhints.height = ymax;
234     oursizhints.flags |= PSize;
235     }
236     if ((i&(XValue|YValue)) == (XValue|YValue)) {
237 greg 1.1 oursizhints.flags |= USPosition;
238     if (i & XNegative)
239 greg 1.3 oursizhints.x += DisplayWidth(thedisplay,
240     ourscreen)-1-oursizhints.width-2*BORWIDTH;
241 greg 1.1 if (i & YNegative)
242 greg 1.3 oursizhints.y += DisplayHeight(thedisplay,
243     ourscreen)-1-oursizhints.height-2*BORWIDTH;
244 greg 1.1 }
245     XSetNormalHints(thedisplay, wind, &oursizhints);
246     }
247     /* store image */
248     getras();
249 greg 1.2 XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask
250     |ButtonMotionMask|StructureNotifyMask
251     |KeyPressMask|ExposureMask);
252 greg 1.1 XMapWindow(thedisplay, wind);
253     return;
254     memerr:
255     quiterr("out of memory");
256     } /* end of init */
257    
258    
259     quiterr(err) /* print message and exit */
260     char *err;
261     {
262     if (err != NULL) {
263     fprintf(stderr, "%s: %s\n", progname, err);
264     exit(1);
265     }
266     exit(0);
267     }
268    
269    
270     eputs(s)
271     char *s;
272     {
273     fputs(s, stderr);
274     }
275    
276    
277     quit(code)
278     int code;
279     {
280     exit(code);
281     }
282    
283    
284     getras() /* get raster file */
285     {
286     colormap ourmap;
287     XVisualInfo vinfo;
288    
289     if (maxcolors <= 2) { /* monochrome */
290     ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8));
291     if (ourdata == NULL)
292     goto fail;
293     ourras = make_raster(thedisplay, ourscreen, 1, ourdata,
294     xmax, ymax, 8);
295     if (ourras == NULL)
296     goto fail;
297     getmono();
298     } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) {
299     ourdata = (unsigned char *)malloc(xmax*ymax*3);
300     if (ourdata == NULL)
301     goto fail;
302     ourras = make_raster(thedisplay, ourscreen, 24, ourdata,
303     xmax, ymax, 8);
304     if (ourras == NULL)
305     goto fail;
306     getfull();
307     } else {
308     ourdata = (unsigned char *)malloc(xmax*ymax);
309     if (ourdata == NULL)
310     goto fail;
311     ourras = make_raster(thedisplay, ourscreen, 8, ourdata,
312     xmax, ymax, 8);
313     if (ourras == NULL)
314     goto fail;
315     if (greyscale)
316     biq(dither,maxcolors,1,ourmap);
317     else
318     ciq(dither,maxcolors,1,ourmap);
319 greg 1.3 if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0)
320 greg 1.1 goto fail;
321     }
322     return;
323     fail:
324     quit("could not create raster image");
325     }
326    
327    
328     getevent() /* process the next event */
329     {
330     union {
331     XEvent u;
332     XConfigureEvent c;
333     XExposeEvent e;
334     XButtonPressedEvent b;
335     XKeyPressedEvent k;
336     } e;
337    
338     XNextEvent(thedisplay, &e.u);
339     switch (e.u.type) {
340     case KeyPress:
341     docom(&e.k);
342     break;
343     case ConfigureNotify:
344     width = e.c.width;
345     height = e.c.height;
346     break;
347     case MapNotify:
348     map_rcolors(ourras, wind);
349     if (fast)
350     make_rpixmap(ourras);
351     break;
352     case UnmapNotify:
353     unmap_rcolors(ourras);
354     break;
355     case Expose:
356     redraw(e.e.x, e.e.y, e.e.width, e.e.height);
357     break;
358     case ButtonPress:
359     if (e.b.state & (ShiftMask|ControlMask))
360     moveimage(&e.b);
361     else
362     getbox(&e.b);
363     break;
364     }
365     }
366    
367    
368     docom(ekey) /* execute command */
369     XKeyPressedEvent *ekey;
370     {
371     char buf[80];
372     COLOR cval;
373     XColor cvx;
374     int com, n;
375     double comp;
376     FVECT rorg, rdir;
377    
378     n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL);
379     if (n == 0)
380     return(0);
381     com = buf[0];
382     switch (com) { /* interpret command */
383     case 'q':
384     case CTRL(D): /* quit */
385     quit(0);
386     case '\n':
387     case '\r':
388     case 'l':
389     case 'c': /* value */
390     if (avgbox(cval) == -1)
391     return(-1);
392     switch (com) {
393     case '\n':
394     case '\r': /* radiance */
395     sprintf(buf, "%.3f", intens(cval)/exposure);
396     break;
397     case 'l': /* luminance */
398     sprintf(buf, "%.0fn", bright(cval)*683.0/exposure);
399     break;
400     case 'c': /* color */
401     comp = pow(2.0, (double)scale);
402     sprintf(buf, "(%.2f,%.2f,%.2f)",
403     colval(cval,RED)*comp,
404     colval(cval,GRN)*comp,
405     colval(cval,BLU)*comp);
406     break;
407     }
408 greg 1.3 XDrawImageString(thedisplay, wind, ourgc,
409     box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
410 greg 1.1 return(0);
411     case 'i': /* identify (contour) */
412     if (ourras->pixels == NULL)
413     return(-1);
414     n = ourdata[ekey->x-xoff+xmax*(ekey->y-yoff)];
415     n = ourras->pmap[n];
416     cvx.pixel = ourras->cdefs[n].pixel;
417     cvx.red = random() & 65535;
418     cvx.green = random() & 65535;
419     cvx.blue = random() & 65535;
420 greg 1.2 cvx.flags = DoRed|DoGreen|DoBlue;
421     XStoreColor(thedisplay, ourras->cmap, &cvx);
422 greg 1.1 return(0);
423     case 'p': /* position */
424     sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
425     XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y,
426     buf, strlen(buf));
427     return(0);
428     case 't': /* trace */
429     if (!gotview) {
430     XBell(thedisplay, 0);
431     return(-1);
432     }
433     viewray(rorg, rdir, &ourview,
434     (ekey->x-xoff+.5)/xmax,
435     (ymax-1-ekey->y+yoff+.5)/ymax);
436     printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
437     printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
438     fflush(stdout);
439     return(0);
440     case '=': /* adjust exposure */
441     if (avgbox(cval) == -1)
442     return(-1);
443     n = log(.5/bright(cval))/.69315 - scale; /* truncate */
444     if (n == 0)
445     return(0);
446     scale_rcolors(ourras, pow(2.0, (double)n));
447     scale += n;
448     sprintf(buf, "%+d", scale);
449 greg 1.3 XDrawImageString(thedisplay, wind, ourgc,
450     box.xmin, box.ymin+box.ysiz, buf, strlen(buf));
451 greg 1.1 XFlush(thedisplay);
452     free(ourdata);
453     free_raster(ourras);
454     getras();
455     /* fall through */
456     case CTRL(R): /* redraw */
457     case CTRL(L):
458     unmap_rcolors(ourras);
459     XClearWindow(thedisplay, wind);
460     map_rcolors(ourras, wind);
461     if (fast)
462     make_rpixmap(ourras);
463     redraw(0, 0, width, height);
464     return(0);
465     case ' ': /* clear */
466     redraw(box.xmin, box.ymin, box.xsiz, box.ysiz);
467     return(0);
468     default:
469 greg 1.2 XBell(thedisplay, 0);
470 greg 1.1 return(-1);
471     }
472     }
473    
474    
475 greg 1.2 moveimage(ebut) /* shift the image */
476     XButtonPressedEvent *ebut;
477 greg 1.1 {
478 greg 1.2 union {
479 greg 1.3 XEvent u;
480     XButtonReleasedEvent b;
481     XPointerMovedEvent m;
482     } e;
483     int nxo, nyo;
484 greg 1.1
485 greg 1.3 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
486     while (e.u.type == MotionNotify) {
487     nxo = xoff + e.m.x - ebut->x;
488     nyo = yoff + e.m.y - ebut->y;
489     revbox(nxo, nyo, nxo+xmax, nyo+ymax);
490     XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
491     revbox(nxo, nyo, nxo+xmax, nyo+ymax);
492     }
493 greg 1.2 xoff += e.b.x - ebut->x;
494     yoff += e.b.y - ebut->y;
495 greg 1.1 XClearWindow(thedisplay, wind);
496     redraw(0, 0, width, height);
497     }
498    
499    
500     getbox(ebut) /* get new box */
501     XButtonPressedEvent *ebut;
502     {
503     union {
504 greg 1.2 XEvent u;
505 greg 1.1 XButtonReleasedEvent b;
506     XPointerMovedEvent m;
507     } e;
508    
509 greg 1.2 XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u);
510     while (e.u.type == MotionNotify) {
511 greg 1.1 revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
512 greg 1.2 XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u);
513 greg 1.1 revbox(ebut->x, ebut->y, box.xmin, box.ymin);
514     }
515     box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
516     box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
517     if (box.xmin > ebut->x) {
518     box.xsiz = box.xmin - ebut->x + 1;
519     box.xmin = ebut->x;
520     } else {
521     box.xsiz = ebut->x - box.xmin + 1;
522     }
523     if (box.ymin > ebut->y) {
524     box.ysiz = box.ymin - ebut->y + 1;
525     box.ymin = ebut->y;
526     } else {
527     box.ysiz = ebut->y - box.ymin + 1;
528     }
529     }
530    
531    
532     revbox(x0, y0, x1, y1) /* draw box with reversed lines */
533     int x0, y0, x1, y1;
534     {
535     static GC mygc = 0;
536    
537     if (mygc == 0) {
538     mygc = XCreateGC(thedisplay, wind, 0, 0);
539     XSetFunction(thedisplay, mygc, GXinvert);
540     }
541     XDrawLine(thedisplay, wind, mygc, x0, y0, x1, y0);
542     XDrawLine(thedisplay, wind, mygc, x0, y1, x1, y1);
543     XDrawLine(thedisplay, wind, mygc, x0, y0, x0, y1);
544     XDrawLine(thedisplay, wind, mygc, x1, y0, x1, y1);
545     } /* end of revbox */
546    
547    
548     avgbox(clr) /* average color over current box */
549     COLOR clr;
550     {
551     int left, right, top, bottom;
552     int y;
553     double d;
554     COLOR ctmp;
555     register int x;
556    
557     setcolor(clr, 0.0, 0.0, 0.0);
558     left = box.xmin - xoff;
559     right = left + box.xsiz;
560     if (left < 0)
561     left = 0;
562     if (right > xmax)
563     right = xmax;
564     if (left >= right)
565     return(-1);
566     top = box.ymin - yoff;
567     bottom = top + box.ysiz;
568     if (top < 0)
569     top = 0;
570     if (bottom > ymax)
571     bottom = ymax;
572     if (top >= bottom)
573     return(-1);
574     for (y = top; y < bottom; y++) {
575     if (getscan(y) == -1)
576     return(-1);
577     for (x = left; x < right; x++) {
578     colr_color(ctmp, scanline[x]);
579     addcolor(clr, ctmp);
580     }
581     }
582     d = 1.0/((right-left)*(bottom-top));
583     scalecolor(clr, d);
584     return(0);
585     }
586    
587    
588     getmono() /* get monochrome data */
589     {
590     register unsigned char *dp;
591     register int x, err;
592     int y;
593     rgbpixel *inline;
594     short *cerr;
595    
596     if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
597     || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
598     quit("out of memory in getmono");
599     dp = ourdata - 1;
600     for (y = 0; y < ymax; y++) {
601     picreadline3(y, inline);
602     err = 0;
603     for (x = 0; x < xmax; x++) {
604     if (!(x&7))
605     *++dp = 0;
606     err += rgb_bright(&inline[x]) + cerr[x];
607     if (err > 127)
608     err -= 255;
609     else
610 greg 1.3 *dp |= 1<<(7-(x&07));
611 greg 1.1 cerr[x] = err >>= 1;
612     }
613     }
614     free((char *)inline);
615     free((char *)cerr);
616     }
617    
618    
619     getfull() /* get full (24-bit) data */
620     {
621     int y;
622    
623     for (y = 0; y < ymax; y++)
624     picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3));
625     }
626    
627    
628     scale_rcolors(xr, sf) /* scale color map */
629     register XRASTER *xr;
630     double sf;
631     {
632     register int i;
633     long maxv;
634    
635     if (xr->pixels == NULL)
636     return;
637    
638     sf = pow(sf, 1.0/gamcor);
639     maxv = 65535/sf;
640    
641     for (i = xr->ncolors; i--; ) {
642     xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
643     65535 :
644     xr->cdefs[i].red * sf;
645     xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
646     65535 :
647     xr->cdefs[i].green * sf;
648     xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
649     65535 :
650     xr->cdefs[i].blue * sf;
651     }
652     XStoreColors(thedisplay, xr->cmap, xr->cdefs, xr->ncolors);
653     }
654    
655    
656     getscan(y)
657     int y;
658     {
659     if (y != cury) {
660     if (scanpos == NULL || scanpos[y] == -1)
661     return(-1);
662     if (fseek(fin, scanpos[y], 0) == -1)
663     quit("fseek error");
664     cury = y;
665     } else if (scanpos != NULL)
666     scanpos[y] = ftell(fin);
667    
668     if (freadcolrs(scanline, xmax, fin) < 0)
669     quiterr("read error");
670    
671     cury++;
672     return(0);
673     }
674    
675    
676     picreadline3(y, l3) /* read in 3-byte scanline */
677     int y;
678     register rgbpixel *l3;
679     {
680     register int i;
681     /* read scanline */
682     if (getscan(y) < 0)
683     quiterr("cannot seek for picreadline");
684     /* convert scanline */
685     normcolrs(scanline, xmax, scale);
686     for (i = 0; i < xmax; i++) {
687     l3[i].r = scanline[i][RED];
688     l3[i].g = scanline[i][GRN];
689     l3[i].b = scanline[i][BLU];
690     }
691     }
692    
693    
694     picwriteline(y, l) /* add 8-bit scanline to image */
695     int y;
696     pixel *l;
697     {
698     bcopy((char *)l, (char *)ourdata+y*xmax, xmax);
699     }
700    
701    
702     picreadcm(map) /* do gamma correction */
703     colormap map;
704     {
705     extern double pow();
706     register int i, val;
707    
708     for (i = 0; i < 256; i++) {
709     val = pow(i/256.0, 1.0/gamcor) * 256.0;
710     map[0][i] = map[1][i] = map[2][i] = val;
711     }
712     }
713    
714    
715     picwritecm(map) /* handled elsewhere */
716     colormap map;
717     {
718     #ifdef DEBUG
719     register int i;
720    
721     for (i = 0; i < 256; i++)
722     printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
723     #endif
724     }