1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
21 |
|
|
22 |
|
#include "standard.h" |
23 |
|
|
24 |
+ |
#include <signal.h> |
25 |
|
#include <X11/Xlib.h> |
26 |
|
#include <X11/cursorfont.h> |
27 |
|
#include <X11/Xutil.h> |
28 |
+ |
#include <X11/Xatom.h> |
29 |
|
|
30 |
|
#include "color.h" |
31 |
|
#include "view.h" |
33 |
|
#include "random.h" |
34 |
|
#include "resolu.h" |
35 |
|
|
36 |
+ |
#ifdef __alpha |
37 |
+ |
#define int4 int |
38 |
+ |
#endif |
39 |
+ |
#ifndef int4 |
40 |
+ |
#define int4 long |
41 |
+ |
#endif |
42 |
+ |
|
43 |
|
#define FONTNAME "8x13" /* text font we'll use */ |
44 |
|
|
45 |
|
#define CTRL(c) ((c)-'@') |
56 |
|
#define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) |
57 |
|
|
58 |
|
double gamcor = 2.2; /* gamma correction */ |
59 |
+ |
char *gamstr = NULL; /* gamma value override */ |
60 |
|
|
61 |
|
int dither = 1; /* dither colors? */ |
62 |
|
int fast = 0; /* keep picture in Pixmap? */ |
65 |
|
|
66 |
|
Window wind = 0; /* our output window */ |
67 |
|
unsigned long ourblack=0, ourwhite=1; /* black and white for this visual */ |
58 |
– |
Font fontid; /* our font */ |
59 |
– |
|
68 |
|
int maxcolors = 0; /* maximum colors */ |
69 |
|
int greyscale = 0; /* in grey */ |
70 |
|
|
73 |
|
int xoff = 0; /* x image offset */ |
74 |
|
int yoff = 0; /* y image offset */ |
75 |
|
|
76 |
+ |
int parent = 0; /* number of children, -1 if child */ |
77 |
+ |
int sequential = 0; /* display images in sequence */ |
78 |
+ |
|
79 |
+ |
char *tout = "od"; /* output of 't' command */ |
80 |
+ |
|
81 |
|
VIEW ourview = STDVIEW; /* image view parameters */ |
82 |
|
int gotview = 0; /* got parameters from file */ |
83 |
|
|
116 |
|
|
117 |
|
char errmsg[128]; |
118 |
|
|
119 |
< |
extern BYTE clrtab[256][3]; /* global color map */ |
119 |
> |
BYTE clrtab[256][3]; /* global color map */ |
120 |
|
|
121 |
|
extern long ftell(); |
122 |
|
|
123 |
< |
extern char *malloc(), *calloc(); |
123 |
> |
extern char *getenv(); |
124 |
|
|
125 |
|
Display *thedisplay; |
126 |
+ |
Atom closedownAtom, wmProtocolsAtom; |
127 |
|
|
128 |
+ |
int sigrecv; |
129 |
|
|
130 |
+ |
int onsig() { sigrecv++; } |
131 |
+ |
|
132 |
+ |
|
133 |
|
main(argc, argv) |
134 |
|
int argc; |
135 |
|
char *argv[]; |
136 |
|
{ |
119 |
– |
extern char *getenv(); |
120 |
– |
char *gv; |
137 |
|
int headline(); |
138 |
|
int i; |
139 |
+ |
int pid; |
140 |
|
|
141 |
|
progname = argv[0]; |
125 |
– |
if ((gv = getenv("GAMMA")) != NULL) |
126 |
– |
gamcor = atof(gv); |
142 |
|
|
143 |
|
for (i = 1; i < argc; i++) |
144 |
|
if (argv[i][0] == '-') |
145 |
|
switch (argv[i][1]) { |
146 |
< |
case 'c': |
146 |
> |
case 'c': /* number of colors */ |
147 |
|
maxcolors = atoi(argv[++i]); |
148 |
|
break; |
149 |
< |
case 'b': |
149 |
> |
case 'b': /* greyscale only */ |
150 |
|
greyscale = !greyscale; |
151 |
|
break; |
152 |
< |
case 'm': |
152 |
> |
case 'm': /* monochrome */ |
153 |
> |
greyscale = 1; |
154 |
|
maxcolors = 2; |
155 |
|
break; |
156 |
< |
case 'd': |
156 |
> |
case 'd': /* display or dither */ |
157 |
|
if (argv[i][2] == 'i') |
158 |
|
dispname = argv[++i]; |
159 |
|
else |
160 |
|
dither = !dither; |
161 |
|
break; |
162 |
< |
case 'f': |
162 |
> |
case 'f': /* save pixmap */ |
163 |
|
fast = !fast; |
164 |
|
break; |
165 |
< |
case 'e': |
165 |
> |
case 's': /* one at a time */ |
166 |
> |
sequential = !sequential; |
167 |
> |
break; |
168 |
> |
case 'o': /* 't' output */ |
169 |
> |
tout = argv[i]+2; |
170 |
> |
break; |
171 |
> |
case 'e': /* exposure comp. */ |
172 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
173 |
|
goto userr; |
174 |
|
scale = atoi(argv[++i]); |
175 |
|
break; |
176 |
< |
case 'g': |
176 |
> |
case 'g': /* gamma comp. */ |
177 |
|
if (argv[i][2] == 'e') |
178 |
|
geometry = argv[++i]; |
179 |
|
else |
180 |
< |
gamcor = atof(argv[++i]); |
180 |
> |
gamstr = argv[++i]; |
181 |
|
break; |
182 |
|
default: |
183 |
|
goto userr; |
187 |
|
else |
188 |
|
break; |
189 |
|
|
190 |
< |
if (i == argc-1) { |
190 |
> |
if (i > argc) |
191 |
> |
goto userr; |
192 |
> |
while (i < argc-1) { |
193 |
> |
sigrecv = 0; |
194 |
> |
signal(SIGCONT, onsig); |
195 |
> |
if ((pid=fork()) == 0) { /* a child for each picture */ |
196 |
> |
parent = -1; |
197 |
> |
break; |
198 |
> |
} |
199 |
> |
if (pid < 0) |
200 |
> |
quiterr("fork failed"); |
201 |
> |
parent++; |
202 |
> |
while (!sigrecv) |
203 |
> |
pause(); /* wait for wake-up call */ |
204 |
> |
i++; |
205 |
> |
} |
206 |
> |
if (i < argc) { /* open picture file */ |
207 |
|
fname = argv[i]; |
208 |
|
fin = fopen(fname, "r"); |
209 |
< |
if (fin == NULL) { |
210 |
< |
sprintf(errmsg, "cannot open file \"%s\"", fname); |
211 |
< |
quiterr(errmsg); |
174 |
< |
} |
175 |
< |
} else if (i != argc) |
176 |
< |
goto userr; |
209 |
> |
if (fin == NULL) |
210 |
> |
quiterr("cannot open picture file"); |
211 |
> |
} |
212 |
|
/* get header */ |
213 |
|
getheader(fin, headline, NULL); |
214 |
|
/* get picture dimensions */ |
222 |
|
if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
223 |
|
quiterr("out of memory"); |
224 |
|
|
225 |
< |
init(); /* get file and open window */ |
225 |
> |
init(argc, argv); /* get file and open window */ |
226 |
|
|
227 |
|
for ( ; ; ) |
228 |
|
getevent(); /* main loop */ |
229 |
|
userr: |
230 |
|
fprintf(stderr, |
231 |
< |
"Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
231 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor][-s] pic ..\n", |
232 |
|
progname); |
233 |
< |
quit(1); |
233 |
> |
exit(1); |
234 |
|
} |
235 |
|
|
236 |
|
|
249 |
|
} |
250 |
|
|
251 |
|
|
252 |
< |
init() /* get data and open window */ |
252 |
> |
init(argc, argv) /* get data and open window */ |
253 |
> |
int argc; |
254 |
> |
char **argv; |
255 |
|
{ |
219 |
– |
XWMHints ourxwmhints; |
256 |
|
XSetWindowAttributes ourwinattr; |
257 |
< |
XSizeHints oursizhints; |
257 |
> |
XClassHint xclshints; |
258 |
> |
XWMHints xwmhints; |
259 |
> |
XSizeHints xszhints; |
260 |
> |
XTextProperty windowName, iconName; |
261 |
> |
XGCValues xgcv; |
262 |
> |
char *name; |
263 |
|
register int i; |
264 |
|
|
265 |
|
if (fname != NULL) { |
266 |
|
scanpos = (long *)malloc(ymax*sizeof(long)); |
267 |
|
if (scanpos == NULL) |
268 |
< |
goto memerr; |
268 |
> |
quiterr("out of memory"); |
269 |
|
for (i = 0; i < ymax; i++) |
270 |
|
scanpos[i] = -1; |
271 |
< |
} |
271 |
> |
name = fname; |
272 |
> |
} else |
273 |
> |
name = progname; |
274 |
> |
/* remove directory prefix from name */ |
275 |
> |
for (i = strlen(name); i-- > 0; ) |
276 |
> |
if (name[i] == '/') |
277 |
> |
break; |
278 |
> |
name += i+1; |
279 |
|
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
280 |
|
quiterr("cannot open display"); |
281 |
+ |
/* set gamma value */ |
282 |
+ |
if (gamstr == NULL) /* get it from the X server */ |
283 |
+ |
gamstr = XGetDefault(thedisplay, "radiance", "gamma"); |
284 |
+ |
if (gamstr == NULL) /* get it from the environment */ |
285 |
+ |
gamstr = getenv("DISPLAY_GAMMA"); |
286 |
+ |
if (gamstr != NULL) |
287 |
+ |
gamcor = atof(gamstr); |
288 |
|
/* get best visual for default screen */ |
289 |
|
getbestvis(); |
290 |
|
/* store image */ |
291 |
|
getras(); |
292 |
< |
/* open window */ |
293 |
< |
ourwinattr.border_pixel = ourblack; |
294 |
< |
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); |
292 |
> |
/* get size and position */ |
293 |
> |
xszhints.flags = 0; |
294 |
> |
xszhints.width = xmax; xszhints.height = ymax; |
295 |
|
if (geometry != NULL) { |
296 |
< |
bzero((char *)&oursizhints, sizeof(oursizhints)); |
297 |
< |
i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, |
298 |
< |
(unsigned *)&oursizhints.width, |
265 |
< |
(unsigned *)&oursizhints.height); |
296 |
> |
i = XParseGeometry(geometry, &xszhints.x, &xszhints.y, |
297 |
> |
(unsigned *)&xszhints.width, |
298 |
> |
(unsigned *)&xszhints.height); |
299 |
|
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
300 |
< |
oursizhints.flags |= USSize; |
301 |
< |
else { |
302 |
< |
oursizhints.width = xmax; |
270 |
< |
oursizhints.height = ymax; |
271 |
< |
oursizhints.flags |= PSize; |
272 |
< |
} |
300 |
> |
xszhints.flags |= USSize; |
301 |
> |
else |
302 |
> |
xszhints.flags |= PSize; |
303 |
|
if ((i&(XValue|YValue)) == (XValue|YValue)) { |
304 |
< |
oursizhints.flags |= USPosition; |
304 |
> |
xszhints.flags |= USPosition; |
305 |
|
if (i & XNegative) |
306 |
< |
oursizhints.x += DisplayWidth(thedisplay, |
307 |
< |
ourscreen)-1-oursizhints.width-2*BORWIDTH; |
306 |
> |
xszhints.x += DisplayWidth(thedisplay, |
307 |
> |
ourscreen)-1-xszhints.width-2*BORWIDTH; |
308 |
|
if (i & YNegative) |
309 |
< |
oursizhints.y += DisplayHeight(thedisplay, |
310 |
< |
ourscreen)-1-oursizhints.height-2*BORWIDTH; |
309 |
> |
xszhints.y += DisplayHeight(thedisplay, |
310 |
> |
ourscreen)-1-xszhints.height-2*BORWIDTH; |
311 |
|
} |
282 |
– |
XSetNormalHints(thedisplay, wind, &oursizhints); |
312 |
|
} |
313 |
< |
ourxwmhints.flags = InputHint|IconPixmapHint; |
314 |
< |
ourxwmhints.input = True; |
315 |
< |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
313 |
> |
/* open window */ |
314 |
> |
i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; |
315 |
> |
ourwinattr.border_pixel = ourwhite; |
316 |
> |
ourwinattr.background_pixel = ourblack; |
317 |
> |
if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) { |
318 |
> |
ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual); |
319 |
> |
i |= CWColormap; |
320 |
> |
} |
321 |
> |
ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| |
322 |
> |
ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; |
323 |
> |
ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); |
324 |
> |
wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, |
325 |
> |
xszhints.width, xszhints.height, BORWIDTH, |
326 |
> |
ourvis.depth, InputOutput, ourvis.visual, |
327 |
> |
i, &ourwinattr); |
328 |
> |
if (wind == 0) |
329 |
> |
quiterr("cannot create window"); |
330 |
> |
width = xmax; |
331 |
> |
height = ymax; |
332 |
> |
/* prepare graphics drawing context */ |
333 |
> |
if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) |
334 |
> |
quiterr("cannot get font"); |
335 |
> |
xgcv.foreground = ourblack; |
336 |
> |
xgcv.background = ourwhite; |
337 |
> |
ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
338 |
> |
GCFont, &xgcv); |
339 |
> |
xgcv.function = GXinvert; |
340 |
> |
revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
341 |
> |
GCFunction, &xgcv); |
342 |
> |
|
343 |
> |
/* set up the window manager */ |
344 |
> |
xwmhints.flags = InputHint|IconPixmapHint; |
345 |
> |
xwmhints.input = True; |
346 |
> |
xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
347 |
|
wind, icondata, iconwidth, iconheight); |
348 |
< |
XSetWMHints(thedisplay, wind, &ourxwmhints); |
349 |
< |
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
350 |
< |
|ButtonMotionMask|StructureNotifyMask |
351 |
< |
|KeyPressMask|ExposureMask); |
348 |
> |
|
349 |
> |
windowName.encoding = iconName.encoding = XA_STRING; |
350 |
> |
windowName.format = iconName.format = 8; |
351 |
> |
windowName.value = (u_char *)name; |
352 |
> |
windowName.nitems = strlen(windowName.value); |
353 |
> |
iconName.value = (u_char *)name; |
354 |
> |
iconName.nitems = strlen(windowName.value); |
355 |
> |
|
356 |
> |
xclshints.res_name = NULL; |
357 |
> |
xclshints.res_class = "Ximage"; |
358 |
> |
XSetWMProperties(thedisplay, wind, &windowName, &iconName, |
359 |
> |
argv, argc, &xszhints, &xwmhints, &xclshints); |
360 |
> |
closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False); |
361 |
> |
wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False); |
362 |
> |
XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); |
363 |
> |
|
364 |
|
XMapWindow(thedisplay, wind); |
293 |
– |
return; |
294 |
– |
memerr: |
295 |
– |
quiterr("out of memory"); |
365 |
|
} /* end of init */ |
366 |
|
|
367 |
|
|
368 |
|
quiterr(err) /* print message and exit */ |
369 |
|
char *err; |
370 |
|
{ |
371 |
< |
if (err != NULL) { |
372 |
< |
fprintf(stderr, "%s: %s\n", progname, err); |
373 |
< |
exit(1); |
371 |
> |
register int es; |
372 |
> |
int cs; |
373 |
> |
|
374 |
> |
if (es = err != NULL) |
375 |
> |
fprintf(stderr, "%s: %s: %s\n", progname, |
376 |
> |
fname==NULL?"<stdin>":fname, err); |
377 |
> |
if (thedisplay != NULL) |
378 |
> |
XCloseDisplay(thedisplay); |
379 |
> |
if (parent < 0 & sigrecv == 0) |
380 |
> |
kill(getppid(), SIGCONT); |
381 |
> |
while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ |
382 |
> |
if (es == 0) |
383 |
> |
es = cs>>8 & 0xff; |
384 |
> |
parent--; |
385 |
|
} |
386 |
< |
exit(0); |
386 |
> |
exit(es); |
387 |
|
} |
388 |
|
|
389 |
|
|
524 |
|
goto fail; |
525 |
|
getmono(); |
526 |
|
} else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { |
527 |
< |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
527 |
> |
ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); |
528 |
|
if (ourdata == NULL) |
529 |
|
goto fail; |
530 |
< |
ourras = make_raster(thedisplay, &ourvis, 32, |
530 |
> |
ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, |
531 |
|
ourdata, xmax, ymax, 32); |
532 |
|
if (ourras == NULL) |
533 |
|
goto fail; |
555 |
|
|
556 |
|
getevent() /* process the next event */ |
557 |
|
{ |
558 |
< |
union { |
479 |
< |
XEvent u; |
480 |
< |
XConfigureEvent c; |
481 |
< |
XExposeEvent e; |
482 |
< |
XButtonPressedEvent b; |
483 |
< |
XKeyPressedEvent k; |
484 |
< |
} e; |
558 |
> |
XEvent xev; |
559 |
|
|
560 |
< |
XNextEvent(thedisplay, &e.u); |
561 |
< |
switch (e.u.type) { |
560 |
> |
XNextEvent(thedisplay, &xev); |
561 |
> |
switch ((int)xev.type) { |
562 |
|
case KeyPress: |
563 |
< |
docom(&e.k); |
563 |
> |
docom(&xev.xkey); |
564 |
|
break; |
565 |
|
case ConfigureNotify: |
566 |
< |
width = e.c.width; |
567 |
< |
height = e.c.height; |
566 |
> |
width = xev.xconfigure.width; |
567 |
> |
height = xev.xconfigure.height; |
568 |
|
break; |
569 |
|
case MapNotify: |
570 |
|
map_rcolors(ourras, wind); |
571 |
|
if (fast) |
572 |
|
make_rpixmap(ourras, wind); |
573 |
+ |
if (!sequential & parent < 0 & sigrecv == 0) { |
574 |
+ |
kill(getppid(), SIGCONT); |
575 |
+ |
sigrecv--; |
576 |
+ |
} |
577 |
|
break; |
578 |
|
case UnmapNotify: |
579 |
|
if (!fast) |
580 |
|
unmap_rcolors(ourras); |
581 |
|
break; |
582 |
|
case Expose: |
583 |
< |
redraw(e.e.x, e.e.y, e.e.width, e.e.height); |
583 |
> |
redraw(xev.xexpose.x, xev.xexpose.y, |
584 |
> |
xev.xexpose.width, xev.xexpose.height); |
585 |
|
break; |
586 |
|
case ButtonPress: |
587 |
< |
if (e.b.state & (ShiftMask|ControlMask)) |
588 |
< |
moveimage(&e.b); |
587 |
> |
if (xev.xbutton.state & (ShiftMask|ControlMask)) |
588 |
> |
moveimage(&xev.xbutton); |
589 |
> |
else if (xev.xbutton.button == Button2) |
590 |
> |
traceray(xev.xbutton.x, xev.xbutton.y); |
591 |
|
else |
592 |
< |
getbox(&e.b); |
592 |
> |
getbox(&xev.xbutton); |
593 |
|
break; |
594 |
+ |
case ClientMessage: |
595 |
+ |
if ((xev.xclient.message_type == wmProtocolsAtom) && |
596 |
+ |
(xev.xclient.data.l[0] == closedownAtom)) |
597 |
+ |
quiterr(NULL); |
598 |
+ |
break; |
599 |
|
} |
600 |
|
} |
601 |
|
|
602 |
|
|
603 |
< |
docom(ekey) /* execute command */ |
603 |
> |
traceray(xpos, ypos) /* print requested pixel data */ |
604 |
> |
int xpos, ypos; |
605 |
> |
{ |
606 |
> |
extern char *index(); |
607 |
> |
FLOAT hv[2]; |
608 |
> |
FVECT rorg, rdir; |
609 |
> |
COLOR cval; |
610 |
> |
register char *cp; |
611 |
> |
|
612 |
> |
box.xmin = xpos; box.xsiz = 1; |
613 |
> |
box.ymin = ypos; box.ysiz = 1; |
614 |
> |
avgbox(cval); |
615 |
> |
scalecolor(cval, 1./exposure); |
616 |
> |
pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); |
617 |
> |
if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
618 |
> |
rorg[0] = rorg[1] = rorg[2] = |
619 |
> |
rdir[0] = rdir[1] = rdir[2] = 0.; |
620 |
> |
|
621 |
> |
for (cp = tout; *cp; cp++) /* print what they asked for */ |
622 |
> |
switch (*cp) { |
623 |
> |
case 'o': /* origin */ |
624 |
> |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
625 |
> |
break; |
626 |
> |
case 'd': /* direction */ |
627 |
> |
printf("%e %e %e ", rdir[0], rdir[1], rdir[2]); |
628 |
> |
break; |
629 |
> |
case 'v': /* radiance value */ |
630 |
> |
printf("%e %e %e ", colval(cval,RED), |
631 |
> |
colval(cval,GRN), colval(cval,BLU)); |
632 |
> |
break; |
633 |
> |
case 'l': /* luminance */ |
634 |
> |
printf("%e ", luminance(cval)); |
635 |
> |
break; |
636 |
> |
case 'p': /* pixel position */ |
637 |
> |
printf("%d %d ", (int)(hv[0]*inpres.xr), |
638 |
> |
(int)(hv[1]*inpres.yr)); |
639 |
> |
break; |
640 |
> |
} |
641 |
> |
putchar('\n'); |
642 |
> |
fflush(stdout); |
643 |
> |
return(0); |
644 |
> |
} |
645 |
> |
|
646 |
> |
|
647 |
> |
docom(ekey) /* execute command */ |
648 |
|
XKeyPressedEvent *ekey; |
649 |
|
{ |
650 |
|
char buf[80]; |
653 |
|
int com, n; |
654 |
|
double comp; |
655 |
|
FLOAT hv[2]; |
526 |
– |
FVECT rorg, rdir; |
656 |
|
|
657 |
|
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); |
658 |
|
if (n == 0) |
660 |
|
com = buf[0]; |
661 |
|
switch (com) { /* interpret command */ |
662 |
|
case 'q': |
663 |
+ |
case 'Q': |
664 |
|
case CTRL('D'): /* quit */ |
665 |
< |
quit(0); |
665 |
> |
quiterr(NULL); |
666 |
|
case '\n': |
667 |
|
case '\r': |
668 |
|
case 'l': |
708 |
|
buf, strlen(buf)); |
709 |
|
return(0); |
710 |
|
case 't': /* trace */ |
711 |
< |
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); |
711 |
> |
return(traceray(ekey->x, ekey->y)); |
712 |
|
case '=': /* adjust exposure */ |
713 |
+ |
case '@': /* adaptation level */ |
714 |
|
if (avgbox(cval) == -1) |
715 |
|
return(-1); |
716 |
< |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
716 |
> |
comp = bright(cval); |
717 |
> |
if (comp < 1e-20) { |
718 |
> |
XBell(thedisplay, 0); |
719 |
> |
return(-1); |
720 |
> |
} |
721 |
> |
if (com == '@') |
722 |
> |
comp = 106./exposure/ |
723 |
> |
pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); |
724 |
> |
else |
725 |
> |
comp = .5/comp; |
726 |
> |
comp = log(comp)/.69315 - scale; |
727 |
> |
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
728 |
|
if (n == 0) |
729 |
|
return(0); |
730 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
746 |
|
make_rpixmap(ourras, wind); |
747 |
|
redraw(0, 0, width, height); |
748 |
|
return(0); |
749 |
+ |
case 'f': /* turn on fast redraw */ |
750 |
+ |
fast = 1; |
751 |
+ |
make_rpixmap(ourras, wind); |
752 |
+ |
return(0); |
753 |
+ |
case 'F': /* turn off fast redraw */ |
754 |
+ |
fast = 0; |
755 |
+ |
free_rpixmap(ourras); |
756 |
+ |
return(0); |
757 |
+ |
case '0': /* recenter origin */ |
758 |
+ |
if (xoff == 0 & yoff == 0) |
759 |
+ |
return(0); |
760 |
+ |
xoff = yoff = 0; |
761 |
+ |
XClearWindow(thedisplay, wind); |
762 |
+ |
redraw(0, 0, width, height); |
763 |
+ |
return(0); |
764 |
|
case ' ': /* clear */ |
765 |
|
redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); |
766 |
|
return(0); |
774 |
|
moveimage(ebut) /* shift the image */ |
775 |
|
XButtonPressedEvent *ebut; |
776 |
|
{ |
777 |
< |
union { |
631 |
< |
XEvent u; |
632 |
< |
XButtonReleasedEvent b; |
633 |
< |
XPointerMovedEvent m; |
634 |
< |
} e; |
777 |
> |
XEvent e; |
778 |
|
int mxo, myo; |
779 |
|
|
780 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
781 |
< |
while (e.u.type == MotionNotify) { |
782 |
< |
mxo = e.m.x; |
783 |
< |
myo = e.m.y; |
780 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); |
781 |
> |
while (e.type == MotionNotify) { |
782 |
> |
mxo = e.xmotion.x; |
783 |
> |
myo = e.xmotion.y; |
784 |
|
revline(ebut->x, ebut->y, mxo, myo); |
785 |
|
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
786 |
|
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
787 |
< |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
787 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); |
788 |
|
revline(ebut->x, ebut->y, mxo, myo); |
789 |
|
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
790 |
|
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
791 |
|
} |
792 |
< |
xoff += e.b.x - ebut->x; |
793 |
< |
yoff += e.b.y - ebut->y; |
792 |
> |
xoff += e.xbutton.x - ebut->x; |
793 |
> |
yoff += e.xbutton.y - ebut->y; |
794 |
|
XClearWindow(thedisplay, wind); |
795 |
|
redraw(0, 0, width, height); |
796 |
|
} |
799 |
|
getbox(ebut) /* get new box */ |
800 |
|
XButtonPressedEvent *ebut; |
801 |
|
{ |
802 |
< |
union { |
660 |
< |
XEvent u; |
661 |
< |
XButtonReleasedEvent b; |
662 |
< |
XPointerMovedEvent m; |
663 |
< |
} e; |
802 |
> |
XEvent e; |
803 |
|
|
804 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
805 |
< |
while (e.u.type == MotionNotify) { |
806 |
< |
revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); |
807 |
< |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
804 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); |
805 |
> |
while (e.type == MotionNotify) { |
806 |
> |
revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y); |
807 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); |
808 |
|
revbox(ebut->x, ebut->y, box.xmin, box.ymin); |
809 |
|
} |
810 |
< |
box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); |
811 |
< |
box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y); |
810 |
> |
box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); |
811 |
> |
box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); |
812 |
|
if (box.xmin > ebut->x) { |
813 |
|
box.xsiz = box.xmin - ebut->x + 1; |
814 |
|
box.xmin = ebut->x; |
921 |
|
static short cerr[ICONSIZ]; |
922 |
|
static int ynext; |
923 |
|
static char *dp; |
924 |
< |
double sf; |
786 |
< |
COLOR col; |
924 |
> |
COLR clr; |
925 |
|
register int err; |
926 |
|
register int x, ti; |
927 |
|
int errp; |
946 |
|
} |
947 |
|
if (y < ynext*ymax/iconheight) /* skip this one */ |
948 |
|
return; |
811 |
– |
sf = pow(2.0, (double)(scale+8)); |
949 |
|
err = 0; |
950 |
|
for (x = 0; x < iconwidth; x++) { |
951 |
|
if (!(x&7)) |
952 |
|
*++dp = 0; |
953 |
|
errp = err; |
954 |
|
ti = x*xmax/iconwidth; |
955 |
< |
colr_color(col, scan[ti]); |
956 |
< |
ti = sf*bright(col); |
957 |
< |
if (ti > 255) ti = 255; |
821 |
< |
err += ti + cerr[x]; |
955 |
> |
copycolr(clr, scan[ti]); |
956 |
> |
normcolrs(clr, 1, scale); |
957 |
> |
err += normbright(clr) + cerr[x]; |
958 |
|
if (err > 127) |
959 |
|
err -= 255; |
960 |
|
else |
969 |
|
getfull() /* get full (24-bit) data */ |
970 |
|
{ |
971 |
|
int y; |
972 |
< |
register unsigned long *dp; |
972 |
> |
register unsigned int4 *dp; |
973 |
|
register int x; |
974 |
|
/* set gamma correction */ |
975 |
|
setcolrgam(gamcor); |
976 |
|
/* read and convert file */ |
977 |
< |
dp = (unsigned long *)ourdata; |
977 |
> |
dp = (unsigned int4 *)ourdata; |
978 |
|
for (y = 0; y < ymax; y++) { |
979 |
|
getscan(y); |
980 |
|
add2icon(y, scanline); |
983 |
|
colrs_gambs(scanline, xmax); |
984 |
|
if (ourras->image->blue_mask & 1) |
985 |
|
for (x = 0; x < xmax; x++) |
986 |
< |
*dp++ = scanline[x][RED] << 16 | |
987 |
< |
scanline[x][GRN] << 8 | |
988 |
< |
scanline[x][BLU] ; |
986 |
> |
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
987 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
988 |
> |
(unsigned int4)scanline[x][BLU] ; |
989 |
|
else |
990 |
|
for (x = 0; x < xmax; x++) |
991 |
< |
*dp++ = scanline[x][RED] | |
992 |
< |
scanline[x][GRN] << 8 | |
993 |
< |
scanline[x][BLU] << 16 ; |
991 |
> |
*dp++ = (unsigned int4)scanline[x][RED] | |
992 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
993 |
> |
(unsigned int4)scanline[x][BLU] << 16 ; |
994 |
|
} |
995 |
|
} |
996 |
|
|
1009 |
|
add2icon(y, scanline); |
1010 |
|
if (scale) |
1011 |
|
shiftcolrs(scanline, xmax, scale); |
1012 |
+ |
for (x = 0; x < xmax; x++) |
1013 |
+ |
scanline[x][GRN] = normbright(scanline[x]); |
1014 |
|
colrs_gambs(scanline, xmax); |
1015 |
|
if (maxcolors < 256) |
1016 |
|
for (x = 0; x < xmax; x++) |
1017 |
< |
*dp++ = ((long)normbright(scanline[x]) * |
1018 |
< |
maxcolors + 128) >> 8; |
1017 |
> |
*dp++ = ((long)scanline[x][GRN] * |
1018 |
> |
maxcolors + maxcolors/2) >> 8; |
1019 |
|
else |
1020 |
|
for (x = 0; x < xmax; x++) |
1021 |
< |
*dp++ = normbright(scanline[x]); |
1021 |
> |
*dp++ = scanline[x][GRN]; |
1022 |
|
} |
1023 |
|
for (x = 0; x < maxcolors; x++) |
1024 |
|
clrtab[x][RED] = clrtab[x][GRN] = |
1025 |
< |
clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors; |
1025 |
> |
clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; |
1026 |
|
} |
1027 |
|
|
1028 |
|
|
1029 |
|
getmapped() /* get color-mapped data */ |
1030 |
|
{ |
1031 |
|
int y; |
1032 |
+ |
/* make sure we can do it first */ |
1033 |
+ |
if (fname == NULL) |
1034 |
+ |
quiterr("cannot map colors from standard input"); |
1035 |
|
/* set gamma correction */ |
1036 |
|
setcolrgam(gamcor); |
1037 |
|
/* make histogram */ |
1038 |
< |
new_histo(); |
1038 |
> |
if (new_histo((long)xmax*ymax) == -1) |
1039 |
> |
quiterr("cannot initialize histogram"); |
1040 |
|
for (y = 0; y < ymax; y++) { |
1041 |
|
if (getscan(y) < 0) |
1042 |
< |
quiterr("seek error in getmapped"); |
1042 |
> |
break; |
1043 |
|
add2icon(y, scanline); |
1044 |
|
if (scale) |
1045 |
|
shiftcolrs(scanline, xmax, scale); |
1050 |
|
if (!new_clrtab(maxcolors)) |
1051 |
|
quiterr("cannot create color map"); |
1052 |
|
for (y = 0; y < ymax; y++) { |
1053 |
< |
if (getscan(y) < 0) |
912 |
< |
quiterr("seek error in getmapped"); |
1053 |
> |
getscan(y); |
1054 |
|
if (scale) |
1055 |
|
shiftcolrs(scanline, xmax, scale); |
1056 |
|
colrs_gambs(scanline, xmax); |
1093 |
|
getscan(y) |
1094 |
|
int y; |
1095 |
|
{ |
1096 |
+ |
static int trunced = -1; /* truncated file? */ |
1097 |
+ |
skipit: |
1098 |
+ |
if (trunced >= 0 && y >= trunced) { |
1099 |
+ |
bzero(scanline, xmax*sizeof(COLR)); |
1100 |
+ |
return(-1); |
1101 |
+ |
} |
1102 |
|
if (y != cury) { |
1103 |
|
if (scanpos == NULL || scanpos[y] == -1) |
1104 |
|
return(-1); |
1108 |
|
} else if (scanpos != NULL && scanpos[y] == -1) |
1109 |
|
scanpos[y] = ftell(fin); |
1110 |
|
|
1111 |
< |
if (freadcolrs(scanline, xmax, fin) < 0) |
1112 |
< |
quiterr("read error"); |
1113 |
< |
|
1111 |
> |
if (freadcolrs(scanline, xmax, fin) < 0) { |
1112 |
> |
fprintf(stderr, "%s: %s: unfinished picture\n", |
1113 |
> |
progname, fname==NULL?"<stdin>":fname); |
1114 |
> |
trunced = y; |
1115 |
> |
goto skipit; |
1116 |
> |
} |
1117 |
|
cury++; |
1118 |
|
return(0); |
1119 |
|
} |