ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ximage.c
Revision: 1.17
Committed: Fri Dec 1 15:50:26 1989 UTC (34 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.16: +2 -2 lines
Log Message:
Changed precision of display values

File Contents

# Content
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 #include "random.h"
33
34 #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 double gamcor = 2.2; /* gamma correction */
44
45 XRASTER *ourras = NULL; /* our stored raster image */
46
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 int scale = 0; /* scalefactor; power of two */
57
58 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 extern double atof(), pow(), log();
90
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 case 'e':
120 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
121 goto userr;
122 scale = atoi(argv[++i]);
123 break;
124 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 }
143 /* get header */
144 getheader(fin, headline);
145 /* get picture dimensions */
146 if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR))
147 quiterr("bad picture size");
148 /* set view parameters */
149 if (gotview) {
150 ourview.hresolu = xmax;
151 ourview.vresolu = ymax;
152 if (setview(&ourview) != NULL)
153 gotview = 0;
154 }
155 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 "Usage: %s [=geometry][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n",
165 progname);
166 quit(1);
167 }
168
169
170 headline(s) /* get relevant info from header */
171 char *s;
172 {
173 static char *altname[] = {"rview","rpict",VIEWSTR,NULL};
174 register char **an;
175
176 if (!strncmp(s, "EXPOSURE=", 9))
177 exposure *= atof(s+9);
178 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 char *
189 sskip(s) /* skip a word */
190 register char *s;
191 {
192 while (isspace(*s)) s++;
193 while (*s && !isspace(*s)) s++;
194 return(s);
195 }
196
197
198 init() /* get data and open window */
199 {
200 register int i;
201 OpaqueFrame mainframe;
202 char defgeom[32];
203
204 if (fname != NULL) {
205 scanpos = (long *)malloc(ymax*sizeof(long));
206 if (scanpos == NULL)
207 goto memerr;
208 for (i = 0; i < ymax; i++)
209 scanpos[i] = -1;
210 }
211 if (XOpenDisplay(NULL) == NULL)
212 quiterr("can't open display; DISPLAY variable set?");
213 #ifdef notdef
214 if (DisplayWidth() - 2*BORWIDTH < xmax ||
215 DisplayHeight() - 2*BORWIDTH - BARHEIGHT < ymax)
216 quiterr("resolution mismatch");
217 #endif
218 if (maxcolors == 0) { /* get number of available colors */
219 maxcolors = 1<<DisplayPlanes();
220 if (maxcolors > 4) maxcolors -= 2;
221 }
222 /* store image */
223 getras();
224
225 mainframe.bdrwidth = BORWIDTH;
226 mainframe.border = WhitePixmap;
227 mainframe.background = BlackPixmap;
228 sprintf(defgeom, "=%dx%d+0+22", xmax, ymax);
229 wind = XCreate("Radiance Image", progname, geometry, defgeom,
230 &mainframe, 16, 16);
231 if (wind == 0)
232 quiterr("can't create window");
233 width = mainframe.width;
234 height = mainframe.height;
235 fontid = XGetFont(FONTNAME);
236 if (fontid == 0)
237 quiterr("can't get font");
238 XStoreName(wind, fname == NULL ? progname : fname);
239 XDefineCursor(wind, XCreateCursor(bcross_width, bcross_height,
240 bcross_bits, bcross_mask_bits,
241 bcross_x_hot, bcross_y_hot,
242 BlackPixel, WhitePixel, GXcopy));
243 XSelectInput(wind, ButtonPressed|ButtonReleased|UnmapWindow
244 |RightDownMotion|MiddleDownMotion|LeftDownMotion
245 |KeyPressed|ExposeWindow|ExposeRegion);
246 XMapWindow(wind);
247 return;
248 memerr:
249 quiterr("out of memory");
250 }
251
252
253 quiterr(err) /* print message and exit */
254 char *err;
255 {
256 if (err != NULL)
257 fprintf(stderr, "%s: %s\n", progname, err);
258
259 exit(err == NULL ? 0 : 1);
260 }
261
262
263 eputs(s)
264 char *s;
265 {
266 fputs(s, stderr);
267 }
268
269
270 quit(code)
271 int code;
272 {
273 exit(code);
274 }
275
276
277 getras() /* get raster file */
278 {
279 colormap ourmap;
280
281 ourras = (XRASTER *)calloc(1, sizeof(XRASTER));
282 if (ourras == NULL)
283 goto memerr;
284 ourras->width = xmax;
285 ourras->height = ymax;
286 if (maxcolors <= 2) { /* monochrome */
287 ourras->data.m = (unsigned short *)malloc(BitmapSize(xmax,ymax));
288 if (ourras->data.m == NULL)
289 goto memerr;
290 getmono();
291 } else {
292 ourras->data.bz = (unsigned char *)malloc(BZPixmapSize(xmax,ymax));
293 if (ourras->data.bz == NULL)
294 goto memerr;
295 if (greyscale)
296 biq(dither,maxcolors,1,ourmap);
297 else
298 ciq(dither,maxcolors,1,ourmap);
299 if (init_rcolors(ourras, ourmap) == 0)
300 goto memerr;
301 }
302 return;
303 memerr:
304 quit("out of memory");
305 }
306
307
308 getevent() /* process the next event */
309 {
310 WindowInfo info;
311 XEvent e;
312
313 XNextEvent(&e);
314 switch (e.type) {
315 case KeyPressed:
316 docom(&e);
317 break;
318 case ExposeWindow:
319 XQueryWindow(wind, &info); /* in case resized */
320 width = info.width;
321 height = info.height;
322 /* fall through */
323 case ExposeRegion:
324 fixwindow(&e);
325 break;
326 case UnmapWindow:
327 unmap_rcolors(ourras);
328 break;
329 case ButtonPressed:
330 if (controlshift(&e))
331 moveimage(&e);
332 else
333 getbox(&e);
334 break;
335 }
336 }
337
338
339 fixwindow(eexp) /* fix new or stepped-on window */
340 XExposeEvent *eexp;
341 {
342 redraw(eexp->x, eexp->y, eexp->width, eexp->height);
343 }
344
345
346 redraw(x, y, w, h) /* redraw section of window */
347 int x, y;
348 int w, h;
349 {
350 if (ourras->ncolors && map_rcolors(ourras) == NULL) {
351 fprintf(stderr, "%s: cannot allocate colors\n", progname);
352 return(-1);
353 }
354 if (fast)
355 make_rpixmap(ourras);
356 return(patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras) ? 0 : -1);
357 }
358
359
360 docom(ekey) /* execute command */
361 XKeyEvent *ekey;
362 {
363 char buf[80];
364 COLOR cval;
365 Color cvx;
366 char *cp;
367 int n;
368 double comp;
369 FVECT rorg, rdir;
370
371 cp = XLookupMapping(ekey, &n);
372 if (n == 0)
373 return(0);
374 switch (*cp) { /* interpret command */
375 case 'q':
376 case CTRL(D): /* quit */
377 quit(0);
378 case '\n':
379 case '\r':
380 case 'l':
381 case 'c': /* value */
382 if (avgbox(cval) == -1)
383 return(-1);
384 switch (*cp) {
385 case '\n':
386 case '\r': /* radiance */
387 sprintf(buf, "%.3f", intens(cval)/exposure);
388 break;
389 case 'l': /* luminance */
390 sprintf(buf, "%.0fn", bright(cval)*683.0/exposure);
391 break;
392 case 'c': /* color */
393 comp = pow(2.0, (double)scale);
394 sprintf(buf, "(%.2f,%.2f,%.2f)",
395 colval(cval,RED)*comp,
396 colval(cval,GRN)*comp,
397 colval(cval,BLU)*comp);
398 break;
399 }
400 XText(wind, box.xmin, box.ymin, buf, strlen(buf),
401 fontid, BlackPixel, WhitePixel);
402 return(0);
403 case 'i': /* identify (contour) */
404 if (ourras->pixels == NULL)
405 return(-1);
406 n = ourras->data.bz[ekey->x-xoff+BZPixmapSize(xmax,ekey->y-yoff)];
407 n = ourras->pmap[n];
408 cvx.pixel = ourras->cdefs[n].pixel;
409 cvx.red = random() & 65535;
410 cvx.green = random() & 65535;
411 cvx.blue = random() & 65535;
412 XStoreColor(&cvx);
413 return(0);
414 case 'p': /* position */
415 sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff);
416 XText(wind, ekey->x, ekey->y, buf, strlen(buf),
417 fontid, BlackPixel, WhitePixel);
418 return(0);
419 case 't': /* trace */
420 if (!gotview) {
421 XFeep(0);
422 return(-1);
423 }
424 rayview(rorg, rdir, &ourview,
425 ekey->x-xoff + .5, ymax-1-ekey->y+yoff + .5);
426 printf("%e %e %e ", rorg[0], rorg[1], rorg[2]);
427 printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]);
428 fflush(stdout);
429 return(0);
430 case '=': /* adjust exposure */
431 if (avgbox(cval) == -1)
432 return(-1);
433 n = log(.5/bright(cval))/.69315 - scale; /* truncate */
434 if (n == 0)
435 return(0);
436 scale_rcolors(ourras, pow(2.0, (double)n));
437 scale += n;
438 sprintf(buf, "%+d", scale);
439 XText(wind, box.xmin, box.ymin, buf, strlen(buf),
440 fontid, BlackPixel, WhitePixel);
441 XFlush();
442 free_raster(ourras);
443 getras();
444 /* fall through */
445 case CTRL(R): /* redraw */
446 case CTRL(L):
447 unmap_rcolors(ourras);
448 XClear(wind);
449 return(redraw(0, 0, width, height));
450 case ' ': /* clear */
451 return(redraw(box.xmin, box.ymin, box.xsiz, box.ysiz));
452 default:
453 XFeep(0);
454 return(-1);
455 }
456 }
457
458
459 moveimage(ep) /* shift the image */
460 XButtonEvent *ep;
461 {
462 XButtonEvent eb;
463
464 XMaskEvent(ButtonReleased, &eb);
465 xoff += eb.x - ep->x;
466 yoff += eb.y - ep->y;
467 XClear(wind);
468 return(redraw(0, 0, width, height));
469 }
470
471
472 getbox(ebut) /* get new box */
473 XButtonEvent *ebut;
474 {
475 union {
476 XEvent e;
477 XButtonEvent b;
478 XMouseMovedEvent m;
479 } e;
480
481 XMaskEvent(ButtonReleased|MouseMoved, &e.e);
482 while (e.e.type == MouseMoved) {
483 revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y);
484 XMaskEvent(ButtonReleased|MouseMoved, &e.e);
485 revbox(ebut->x, ebut->y, box.xmin, box.ymin);
486 }
487 box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x);
488 box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y);
489 if (box.xmin > ebut->x) {
490 box.xsiz = box.xmin - ebut->x + 1;
491 box.xmin = ebut->x;
492 } else {
493 box.xsiz = ebut->x - box.xmin + 1;
494 }
495 if (box.ymin > ebut->y) {
496 box.ysiz = box.ymin - ebut->y + 1;
497 box.ymin = ebut->y;
498 } else {
499 box.ysiz = ebut->y - box.ymin + 1;
500 }
501 }
502
503
504 revbox(x0, y0, x1, y1) /* draw box with reversed lines */
505 int x0, y0, x1, y1;
506 {
507 XLine(wind, x0, y0, x1, y0, 1, 1, 0, GXinvert, AllPlanes);
508 XLine(wind, x0, y1, x1, y1, 1, 1, 0, GXinvert, AllPlanes);
509 XLine(wind, x0, y0, x0, y1, 1, 1, 0, GXinvert, AllPlanes);
510 XLine(wind, x1, y0, x1, y1, 1, 1, 0, GXinvert, AllPlanes);
511 }
512
513
514 avgbox(clr) /* average color over current box */
515 COLOR clr;
516 {
517 int left, right, top, bottom;
518 int y;
519 double d;
520 COLOR ctmp;
521 register int x;
522
523 setcolor(clr, 0.0, 0.0, 0.0);
524 left = box.xmin - xoff;
525 right = left + box.xsiz;
526 if (left < 0)
527 left = 0;
528 if (right > xmax)
529 right = xmax;
530 if (left >= right)
531 return(-1);
532 top = box.ymin - yoff;
533 bottom = top + box.ysiz;
534 if (top < 0)
535 top = 0;
536 if (bottom > ymax)
537 bottom = ymax;
538 if (top >= bottom)
539 return(-1);
540 for (y = top; y < bottom; y++) {
541 if (getscan(y) == -1)
542 return(-1);
543 for (x = left; x < right; x++) {
544 colr_color(ctmp, scanline[x]);
545 addcolor(clr, ctmp);
546 }
547 }
548 d = 1.0/((right-left)*(bottom-top));
549 scalecolor(clr, d);
550 return(0);
551 }
552
553
554 getmono() /* get monochrome data */
555 {
556 register unsigned short *dp;
557 register int x, err;
558 int y;
559 rgbpixel *inline;
560 short *cerr;
561
562 if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL
563 || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL)
564 quit("out of memory in getmono");
565 dp = ourras->data.m - 1;
566 for (y = 0; y < ymax; y++) {
567 picreadline3(y, inline);
568 err = 0;
569 for (x = 0; x < xmax; x++) {
570 if (!(x&0xf))
571 *++dp = 0;
572 err += rgb_bright(&inline[x]) + cerr[x];
573 if (err > 127)
574 err -= 255;
575 else
576 *dp |= 1<<(x&0xf);
577 cerr[x] = err >>= 1;
578 }
579 }
580 free((char *)inline);
581 free((char *)cerr);
582 }
583
584
585 init_rcolors(xr, cmap) /* (re)assign color values */
586 register XRASTER *xr;
587 colormap cmap;
588 {
589 register int i;
590 register unsigned char *p;
591
592 xr->pmap = (int *)malloc(256*sizeof(int));
593 if (xr->pmap == NULL)
594 return(0);
595 xr->cdefs = (Color *)malloc(256*sizeof(Color));
596 if (xr->cdefs == NULL)
597 return(0);
598 for (i = 0; i < 256; i++)
599 xr->pmap[i] = -1;
600 xr->ncolors = 0;
601 for (p = xr->data.bz, i = BZPixmapSize(xr->width,xr->height); i--; p++)
602 if (xr->pmap[*p] == -1) {
603 xr->cdefs[xr->ncolors].red = cmap[0][*p] << 8;
604 xr->cdefs[xr->ncolors].green = cmap[1][*p] << 8;
605 xr->cdefs[xr->ncolors].blue = cmap[2][*p] << 8;
606 xr->cdefs[xr->ncolors].pixel = *p;
607 xr->pmap[*p] = xr->ncolors++;
608 }
609 xr->cdefs = (Color *)realloc((char *)xr->cdefs, xr->ncolors*sizeof(Color));
610 if (xr->cdefs == NULL)
611 return(0);
612 return(1);
613 }
614
615
616 scale_rcolors(xr, sf) /* scale color map */
617 register XRASTER *xr;
618 double sf;
619 {
620 register int i;
621 long maxv;
622
623 if (xr->pixels == NULL)
624 return;
625
626 sf = pow(sf, 1.0/gamcor);
627 maxv = 65535/sf;
628
629 for (i = xr->ncolors; i--; ) {
630 xr->cdefs[i].red = xr->cdefs[i].red > maxv ?
631 65535 :
632 xr->cdefs[i].red * sf;
633 xr->cdefs[i].green = xr->cdefs[i].green > maxv ?
634 65535 :
635 xr->cdefs[i].green * sf;
636 xr->cdefs[i].blue = xr->cdefs[i].blue > maxv ?
637 65535 :
638 xr->cdefs[i].blue * sf;
639 }
640 XStoreColors(xr->ncolors, xr->cdefs);
641 }
642
643
644 getscan(y)
645 int y;
646 {
647 if (y != cury) {
648 if (scanpos == NULL || scanpos[y] == -1)
649 return(-1);
650 if (fseek(fin, scanpos[y], 0) == -1)
651 quit("fseek error");
652 cury = y;
653 } else if (scanpos != NULL)
654 scanpos[y] = ftell(fin);
655
656 if (freadcolrs(scanline, xmax, fin) < 0)
657 quiterr("read error");
658
659 cury++;
660 return(0);
661 }
662
663
664 picreadline3(y, l3) /* read in 3-byte scanline */
665 int y;
666 register rgbpixel *l3;
667 {
668 register int i;
669 /* read scanline */
670 if (getscan(y) < 0)
671 quiterr("cannot seek for picreadline");
672 /* convert scanline */
673 if (scale != 0)
674 for (i = 0; i < xmax; i++)
675 if (scanline[i][EXP]+scale >= 0)
676 scanline[i][EXP] += scale;
677 normcolrs(scanline, xmax);
678 for (i = 0; i < xmax; i++) {
679 l3[i].r = scanline[i][RED];
680 l3[i].g = scanline[i][GRN];
681 l3[i].b = scanline[i][BLU];
682 }
683 }
684
685
686 picwriteline(y, l) /* add 8-bit scanline to image */
687 int y;
688 pixel *l;
689 {
690 bcopy(l, ourras->data.bz+BZPixmapSize(xmax,y), BZPixmapSize(xmax,1));
691 }
692
693
694 picreadcm(map) /* do gamma correction */
695 colormap map;
696 {
697 extern double pow();
698 register int i, val;
699
700 for (i = 0; i < 256; i++) {
701 val = pow(i/256.0, 1.0/gamcor) * 256.0;
702 map[0][i] = map[1][i] = map[2][i] = val;
703 }
704 }
705
706
707 picwritecm(map) /* handled elsewhere */
708 colormap map;
709 {
710 #ifdef DEBUG
711 register int i;
712
713 for (i = 0; i < 256; i++)
714 printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]);
715 #endif
716 }