ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ximage.c
Revision: 1.20
Committed: Fri Jan 5 17:52:57 1990 UTC (34 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.19: +2 -2 lines
Log Message:
changed EXPOSURE information interface

File Contents

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