ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/x11image.c
Revision: 1.5
Committed: Wed Mar 7 11:39:26 1990 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +22 -16 lines
Log Message:
improved interactivity in moveimage

File Contents

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