1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
24 |
|
#include <X11/Xlib.h> |
25 |
|
#include <X11/cursorfont.h> |
26 |
|
#include <X11/Xutil.h> |
27 |
+ |
#include <X11/Xatom.h> |
28 |
|
|
29 |
|
#include "color.h" |
30 |
|
#include "view.h" |
56 |
|
|
57 |
|
Window wind = 0; /* our output window */ |
58 |
|
unsigned long ourblack=0, ourwhite=1; /* black and white for this visual */ |
58 |
– |
Font fontid; /* our font */ |
59 |
– |
|
59 |
|
int maxcolors = 0; /* maximum colors */ |
60 |
|
int greyscale = 0; /* in grey */ |
61 |
|
|
106 |
|
|
107 |
|
extern long ftell(); |
108 |
|
|
110 |
– |
extern char *malloc(), *calloc(); |
111 |
– |
|
109 |
|
Display *thedisplay; |
110 |
+ |
Atom closedownAtom, wmProtocolsAtom; |
111 |
|
|
112 |
|
|
113 |
|
main(argc, argv) |
185 |
|
if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
186 |
|
quiterr("out of memory"); |
187 |
|
|
188 |
< |
init(); /* get file and open window */ |
188 |
> |
init(argc, argv); /* get file and open window */ |
189 |
|
|
190 |
|
for ( ; ; ) |
191 |
|
getevent(); /* main loop */ |
192 |
|
userr: |
193 |
|
fprintf(stderr, |
194 |
< |
"Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
194 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n", |
195 |
|
progname); |
196 |
< |
quit(1); |
196 |
> |
exit(1); |
197 |
|
} |
198 |
|
|
199 |
|
|
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
< |
init() /* get data and open window */ |
215 |
> |
init(argc, argv) /* get data and open window */ |
216 |
> |
int argc; |
217 |
> |
char **argv; |
218 |
|
{ |
219 |
– |
XWMHints ourxwmhints; |
219 |
|
XSetWindowAttributes ourwinattr; |
220 |
< |
XSizeHints oursizhints; |
220 |
> |
XClassHint xclshints; |
221 |
> |
XWMHints xwmhints; |
222 |
> |
XSizeHints xszhints; |
223 |
> |
XTextProperty windowName, iconName; |
224 |
> |
XGCValues xgcv; |
225 |
> |
char *name; |
226 |
|
register int i; |
227 |
|
|
228 |
|
if (fname != NULL) { |
229 |
|
scanpos = (long *)malloc(ymax*sizeof(long)); |
230 |
|
if (scanpos == NULL) |
231 |
< |
goto memerr; |
231 |
> |
quiterr("out of memory"); |
232 |
|
for (i = 0; i < ymax; i++) |
233 |
|
scanpos[i] = -1; |
234 |
< |
} |
234 |
> |
name = fname; |
235 |
> |
} else |
236 |
> |
name = progname; |
237 |
> |
/* remove directory prefix from name */ |
238 |
> |
for (i = strlen(name); i-- > 0; ) |
239 |
> |
if (name[i] == '/') |
240 |
> |
break; |
241 |
> |
name += i+1; |
242 |
|
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
243 |
|
quiterr("cannot open display"); |
244 |
|
/* get best visual for default screen */ |
245 |
|
getbestvis(); |
246 |
|
/* store image */ |
247 |
|
getras(); |
248 |
< |
/* open window */ |
249 |
< |
ourwinattr.border_pixel = ourblack; |
250 |
< |
ourwinattr.background_pixel = ourwhite; |
240 |
< |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, |
241 |
< |
ourvis.visual, AllocNone); |
242 |
< |
wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, |
243 |
< |
ourvis.depth, InputOutput, ourvis.visual, |
244 |
< |
CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
245 |
< |
if (wind == 0) |
246 |
< |
quiterr("cannot create window"); |
247 |
< |
XFreeColormap(thedisplay, ourwinattr.colormap); |
248 |
< |
width = xmax; |
249 |
< |
height = ymax; |
250 |
< |
ourgc = XCreateGC(thedisplay, wind, 0, 0); |
251 |
< |
XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); |
252 |
< |
revgc = XCreateGC(thedisplay, wind, 0, 0); |
253 |
< |
XSetFunction(thedisplay, revgc, GXinvert); |
254 |
< |
fontid = XLoadFont(thedisplay, FONTNAME); |
255 |
< |
if (fontid == 0) |
256 |
< |
quiterr("cannot get font"); |
257 |
< |
XSetFont(thedisplay, ourgc, fontid); |
258 |
< |
XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, |
259 |
< |
XC_diamond_cross)); |
260 |
< |
XStoreName(thedisplay, wind, fname == NULL ? progname : fname); |
248 |
> |
/* get size and position */ |
249 |
> |
xszhints.flags = 0; |
250 |
> |
xszhints.width = xmax; xszhints.height = ymax; |
251 |
|
if (geometry != NULL) { |
252 |
< |
bzero((char *)&oursizhints, sizeof(oursizhints)); |
253 |
< |
i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, |
254 |
< |
(unsigned *)&oursizhints.width, |
265 |
< |
(unsigned *)&oursizhints.height); |
252 |
> |
i = XParseGeometry(geometry, &xszhints.x, &xszhints.y, |
253 |
> |
(unsigned *)&xszhints.width, |
254 |
> |
(unsigned *)&xszhints.height); |
255 |
|
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
256 |
< |
oursizhints.flags |= USSize; |
257 |
< |
else { |
258 |
< |
oursizhints.width = xmax; |
270 |
< |
oursizhints.height = ymax; |
271 |
< |
oursizhints.flags |= PSize; |
272 |
< |
} |
256 |
> |
xszhints.flags |= USSize; |
257 |
> |
else |
258 |
> |
xszhints.flags |= PSize; |
259 |
|
if ((i&(XValue|YValue)) == (XValue|YValue)) { |
260 |
< |
oursizhints.flags |= USPosition; |
260 |
> |
xszhints.flags |= USPosition; |
261 |
|
if (i & XNegative) |
262 |
< |
oursizhints.x += DisplayWidth(thedisplay, |
263 |
< |
ourscreen)-1-oursizhints.width-2*BORWIDTH; |
262 |
> |
xszhints.x += DisplayWidth(thedisplay, |
263 |
> |
ourscreen)-1-xszhints.width-2*BORWIDTH; |
264 |
|
if (i & YNegative) |
265 |
< |
oursizhints.y += DisplayHeight(thedisplay, |
266 |
< |
ourscreen)-1-oursizhints.height-2*BORWIDTH; |
265 |
> |
xszhints.y += DisplayHeight(thedisplay, |
266 |
> |
ourscreen)-1-xszhints.height-2*BORWIDTH; |
267 |
|
} |
282 |
– |
XSetNormalHints(thedisplay, wind, &oursizhints); |
268 |
|
} |
269 |
< |
ourxwmhints.flags = InputHint|IconPixmapHint; |
270 |
< |
ourxwmhints.input = True; |
271 |
< |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
269 |
> |
/* open window */ |
270 |
> |
ourwinattr.border_pixel = ourwhite; |
271 |
> |
ourwinattr.background_pixel = ourblack; |
272 |
> |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, |
273 |
> |
ourvis.visual, AllocNone); |
274 |
> |
ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| |
275 |
> |
ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; |
276 |
> |
ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); |
277 |
> |
wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, |
278 |
> |
xszhints.width, xszhints.height, BORWIDTH, |
279 |
> |
ourvis.depth, InputOutput, ourvis.visual, CWEventMask| |
280 |
> |
CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
281 |
> |
if (wind == 0) |
282 |
> |
quiterr("cannot create window"); |
283 |
> |
width = xmax; |
284 |
> |
height = ymax; |
285 |
> |
xgcv.foreground = ourblack; |
286 |
> |
xgcv.background = ourwhite; |
287 |
> |
if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) |
288 |
> |
quiterr("cannot get font"); |
289 |
> |
ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
290 |
> |
GCFont, &xgcv); |
291 |
> |
xgcv.function = GXinvert; |
292 |
> |
revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
293 |
> |
GCFunction, &xgcv); |
294 |
> |
|
295 |
> |
/* set up the window manager */ |
296 |
> |
xwmhints.flags = InputHint|IconPixmapHint; |
297 |
> |
xwmhints.input = True; |
298 |
> |
xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
299 |
|
wind, icondata, iconwidth, iconheight); |
300 |
< |
XSetWMHints(thedisplay, wind, &ourxwmhints); |
301 |
< |
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
302 |
< |
|ButtonMotionMask|StructureNotifyMask |
303 |
< |
|KeyPressMask|ExposureMask); |
300 |
> |
|
301 |
> |
windowName.encoding = iconName.encoding = XA_STRING; |
302 |
> |
windowName.format = iconName.format = 8; |
303 |
> |
windowName.value = (u_char *)name; |
304 |
> |
windowName.nitems = strlen(windowName.value); |
305 |
> |
iconName.value = (u_char *)name; |
306 |
> |
iconName.nitems = strlen(windowName.value); |
307 |
> |
|
308 |
> |
xclshints.res_name = NULL; |
309 |
> |
xclshints.res_class = "Ximage"; |
310 |
> |
XSetWMProperties(thedisplay, wind, &windowName, &iconName, |
311 |
> |
argv, argc, &xszhints, &xwmhints, &xclshints); |
312 |
> |
closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False); |
313 |
> |
wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False); |
314 |
> |
XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); |
315 |
> |
|
316 |
|
XMapWindow(thedisplay, wind); |
317 |
|
return; |
294 |
– |
memerr: |
295 |
– |
quiterr("out of memory"); |
318 |
|
} /* end of init */ |
319 |
|
|
320 |
|
|
497 |
|
|
498 |
|
getevent() /* process the next event */ |
499 |
|
{ |
500 |
< |
union { |
479 |
< |
XEvent u; |
480 |
< |
XConfigureEvent c; |
481 |
< |
XExposeEvent e; |
482 |
< |
XButtonPressedEvent b; |
483 |
< |
XKeyPressedEvent k; |
484 |
< |
} e; |
500 |
> |
XEvent xev; |
501 |
|
|
502 |
< |
XNextEvent(thedisplay, &e.u); |
503 |
< |
switch (e.u.type) { |
502 |
> |
XNextEvent(thedisplay, &xev); |
503 |
> |
switch ((int)xev.type) { |
504 |
|
case KeyPress: |
505 |
< |
docom(&e.k); |
505 |
> |
docom(&xev.xkey); |
506 |
|
break; |
507 |
|
case ConfigureNotify: |
508 |
< |
width = e.c.width; |
509 |
< |
height = e.c.height; |
508 |
> |
width = xev.xconfigure.width; |
509 |
> |
height = xev.xconfigure.height; |
510 |
|
break; |
511 |
|
case MapNotify: |
512 |
|
map_rcolors(ourras, wind); |
518 |
|
unmap_rcolors(ourras); |
519 |
|
break; |
520 |
|
case Expose: |
521 |
< |
redraw(e.e.x, e.e.y, e.e.width, e.e.height); |
521 |
> |
redraw(xev.xexpose.x, xev.xexpose.y, |
522 |
> |
xev.xexpose.width, xev.xexpose.height); |
523 |
|
break; |
524 |
|
case ButtonPress: |
525 |
< |
if (e.b.state & (ShiftMask|ControlMask)) |
526 |
< |
moveimage(&e.b); |
525 |
> |
if (xev.xbutton.state & (ShiftMask|ControlMask)) |
526 |
> |
moveimage(&xev.xbutton); |
527 |
> |
else if (xev.xbutton.button == Button2) |
528 |
> |
traceray(xev.xbutton.x, xev.xbutton.y); |
529 |
|
else |
530 |
< |
getbox(&e.b); |
530 |
> |
getbox(&xev.xbutton); |
531 |
|
break; |
532 |
+ |
case ClientMessage: |
533 |
+ |
if ((xev.xclient.message_type == wmProtocolsAtom) && |
534 |
+ |
(xev.xclient.data.l[0] == closedownAtom)) |
535 |
+ |
quiterr(NULL); |
536 |
+ |
break; |
537 |
|
} |
538 |
|
} |
539 |
|
|
540 |
|
|
541 |
< |
docom(ekey) /* execute command */ |
541 |
> |
traceray(xpos, ypos) /* print ray corresponding to pixel */ |
542 |
> |
int xpos, ypos; |
543 |
> |
{ |
544 |
> |
FLOAT hv[2]; |
545 |
> |
FVECT rorg, rdir; |
546 |
> |
|
547 |
> |
if (!gotview) { /* no view, no can do */ |
548 |
> |
XBell(thedisplay, 0); |
549 |
> |
return(-1); |
550 |
> |
} |
551 |
> |
pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); |
552 |
> |
if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
553 |
> |
return(-1); |
554 |
> |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
555 |
> |
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
556 |
> |
fflush(stdout); |
557 |
> |
return(0); |
558 |
> |
} |
559 |
> |
|
560 |
> |
|
561 |
> |
docom(ekey) /* execute command */ |
562 |
|
XKeyPressedEvent *ekey; |
563 |
|
{ |
564 |
|
char buf[80]; |
567 |
|
int com, n; |
568 |
|
double comp; |
569 |
|
FLOAT hv[2]; |
526 |
– |
FVECT rorg, rdir; |
570 |
|
|
571 |
|
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); |
572 |
|
if (n == 0) |
574 |
|
com = buf[0]; |
575 |
|
switch (com) { /* interpret command */ |
576 |
|
case 'q': |
577 |
+ |
case 'Q': |
578 |
|
case CTRL('D'): /* quit */ |
579 |
< |
quit(0); |
579 |
> |
quiterr(NULL); |
580 |
|
case '\n': |
581 |
|
case '\r': |
582 |
|
case 'l': |
622 |
|
buf, strlen(buf)); |
623 |
|
return(0); |
624 |
|
case 't': /* trace */ |
625 |
< |
if (!gotview) { |
582 |
< |
XBell(thedisplay, 0); |
583 |
< |
return(-1); |
584 |
< |
} |
585 |
< |
pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); |
586 |
< |
if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
587 |
< |
return(-1); |
588 |
< |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
589 |
< |
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
590 |
< |
fflush(stdout); |
591 |
< |
return(0); |
625 |
> |
return(traceray(ekey->x, ekey->y)); |
626 |
|
case '=': /* adjust exposure */ |
627 |
+ |
case '@': /* adaptation level */ |
628 |
|
if (avgbox(cval) == -1) |
629 |
|
return(-1); |
630 |
< |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
630 |
> |
comp = com=='@' |
631 |
> |
? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure |
632 |
> |
: .5/bright(cval) ; |
633 |
> |
comp = log(comp)/.69315 - scale; |
634 |
> |
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
635 |
|
if (n == 0) |
636 |
|
return(0); |
637 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
653 |
|
make_rpixmap(ourras, wind); |
654 |
|
redraw(0, 0, width, height); |
655 |
|
return(0); |
656 |
+ |
case 'f': /* turn on fast redraw */ |
657 |
+ |
fast = 1; |
658 |
+ |
make_rpixmap(ourras, wind); |
659 |
+ |
return(0); |
660 |
+ |
case 'F': /* turn off fast redraw */ |
661 |
+ |
fast = 0; |
662 |
+ |
free_rpixmap(ourras); |
663 |
+ |
return(0); |
664 |
|
case '0': /* recenter origin */ |
665 |
|
if (xoff == 0 & yoff == 0) |
666 |
|
return(0); |
681 |
|
moveimage(ebut) /* shift the image */ |
682 |
|
XButtonPressedEvent *ebut; |
683 |
|
{ |
684 |
< |
union { |
638 |
< |
XEvent u; |
639 |
< |
XButtonReleasedEvent b; |
640 |
< |
XPointerMovedEvent m; |
641 |
< |
} e; |
684 |
> |
XEvent e; |
685 |
|
int mxo, myo; |
686 |
|
|
687 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
688 |
< |
while (e.u.type == MotionNotify) { |
689 |
< |
mxo = e.m.x; |
690 |
< |
myo = e.m.y; |
687 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); |
688 |
> |
while (e.type == MotionNotify) { |
689 |
> |
mxo = e.xmotion.x; |
690 |
> |
myo = e.xmotion.y; |
691 |
|
revline(ebut->x, ebut->y, mxo, myo); |
692 |
|
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
693 |
|
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
694 |
< |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
694 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); |
695 |
|
revline(ebut->x, ebut->y, mxo, myo); |
696 |
|
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
697 |
|
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
698 |
|
} |
699 |
< |
xoff += e.b.x - ebut->x; |
700 |
< |
yoff += e.b.y - ebut->y; |
699 |
> |
xoff += e.xbutton.x - ebut->x; |
700 |
> |
yoff += e.xbutton.y - ebut->y; |
701 |
|
XClearWindow(thedisplay, wind); |
702 |
|
redraw(0, 0, width, height); |
703 |
|
} |
706 |
|
getbox(ebut) /* get new box */ |
707 |
|
XButtonPressedEvent *ebut; |
708 |
|
{ |
709 |
< |
union { |
667 |
< |
XEvent u; |
668 |
< |
XButtonReleasedEvent b; |
669 |
< |
XPointerMovedEvent m; |
670 |
< |
} e; |
709 |
> |
XEvent e; |
710 |
|
|
711 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
712 |
< |
while (e.u.type == MotionNotify) { |
713 |
< |
revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); |
714 |
< |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
711 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); |
712 |
> |
while (e.type == MotionNotify) { |
713 |
> |
revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y); |
714 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); |
715 |
|
revbox(ebut->x, ebut->y, box.xmin, box.ymin); |
716 |
|
} |
717 |
< |
box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); |
718 |
< |
box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y); |
717 |
> |
box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); |
718 |
> |
box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); |
719 |
|
if (box.xmin > ebut->x) { |
720 |
|
box.xsiz = box.xmin - ebut->x + 1; |
721 |
|
box.xmin = ebut->x; |