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" |
30 |
– |
#include "pic.h" |
32 |
|
#include "x11raster.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 */ |
59 |
– |
Font fontid; /* our font */ |
60 |
– |
|
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 |
|
VIEW ourview = STDVIEW; /* image view parameters */ |
80 |
|
int gotview = 0; /* got parameters from file */ |
81 |
|
|
114 |
|
|
115 |
|
char errmsg[128]; |
116 |
|
|
117 |
+ |
BYTE clrtab[256][3]; /* global color map */ |
118 |
+ |
|
119 |
|
extern long ftell(); |
120 |
|
|
121 |
< |
extern char *malloc(), *calloc(); |
121 |
> |
extern char *getenv(); |
122 |
|
|
111 |
– |
extern double pow(), log(); |
112 |
– |
|
123 |
|
Display *thedisplay; |
124 |
+ |
Atom closedownAtom, wmProtocolsAtom; |
125 |
|
|
126 |
+ |
int sigrecv; |
127 |
+ |
|
128 |
+ |
int onsig() { sigrecv++; } |
129 |
+ |
|
130 |
+ |
|
131 |
|
main(argc, argv) |
132 |
|
int argc; |
133 |
|
char *argv[]; |
134 |
|
{ |
119 |
– |
extern char *getenv(); |
120 |
– |
char *gv; |
135 |
|
int headline(); |
136 |
|
int i; |
137 |
+ |
int pid; |
138 |
|
|
139 |
|
progname = argv[0]; |
125 |
– |
if ((gv = getenv("GAMMA")) != NULL) |
126 |
– |
gamcor = atof(gv); |
140 |
|
|
141 |
|
for (i = 1; i < argc; i++) |
142 |
|
if (argv[i][0] == '-') |
148 |
|
greyscale = !greyscale; |
149 |
|
break; |
150 |
|
case 'm': |
151 |
+ |
greyscale = 1; |
152 |
|
maxcolors = 2; |
153 |
|
break; |
154 |
|
case 'd': |
160 |
|
case 'f': |
161 |
|
fast = !fast; |
162 |
|
break; |
163 |
+ |
case 's': |
164 |
+ |
sequential = !sequential; |
165 |
+ |
break; |
166 |
|
case 'e': |
167 |
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
168 |
|
goto userr; |
172 |
|
if (argv[i][2] == 'e') |
173 |
|
geometry = argv[++i]; |
174 |
|
else |
175 |
< |
gamcor = atof(argv[++i]); |
175 |
> |
gamstr = argv[++i]; |
176 |
|
break; |
177 |
|
default: |
178 |
|
goto userr; |
182 |
|
else |
183 |
|
break; |
184 |
|
|
185 |
< |
if (i == argc-1) { |
185 |
> |
if (i > argc) |
186 |
> |
goto userr; |
187 |
> |
while (i < argc-1) { |
188 |
> |
sigrecv = 0; |
189 |
> |
signal(SIGCONT, onsig); |
190 |
> |
if ((pid=fork()) == 0) { /* a child for each picture */ |
191 |
> |
parent = -1; |
192 |
> |
break; |
193 |
> |
} |
194 |
> |
if (pid < 0) |
195 |
> |
quiterr("fork failed"); |
196 |
> |
parent++; |
197 |
> |
while (!sigrecv) |
198 |
> |
pause(); /* wait for wake-up call */ |
199 |
> |
i++; |
200 |
> |
} |
201 |
> |
if (i < argc) { /* open picture file */ |
202 |
|
fname = argv[i]; |
203 |
|
fin = fopen(fname, "r"); |
204 |
< |
if (fin == NULL) { |
205 |
< |
sprintf(errmsg, "cannot open file \"%s\"", fname); |
206 |
< |
quiterr(errmsg); |
174 |
< |
} |
175 |
< |
} else if (i != argc) |
176 |
< |
goto userr; |
204 |
> |
if (fin == NULL) |
205 |
> |
quiterr("cannot open picture file"); |
206 |
> |
} |
207 |
|
/* get header */ |
208 |
|
getheader(fin, headline, NULL); |
209 |
|
/* get picture dimensions */ |
217 |
|
if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) |
218 |
|
quiterr("out of memory"); |
219 |
|
|
220 |
< |
init(); /* get file and open window */ |
220 |
> |
init(argc, argv); /* get file and open window */ |
221 |
|
|
222 |
|
for ( ; ; ) |
223 |
|
getevent(); /* main loop */ |
224 |
|
userr: |
225 |
|
fprintf(stderr, |
226 |
< |
"Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", |
226 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor][-s] pic ..\n", |
227 |
|
progname); |
228 |
< |
quit(1); |
228 |
> |
exit(1); |
229 |
|
} |
230 |
|
|
231 |
|
|
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
< |
init() /* get data and open window */ |
247 |
> |
init(argc, argv) /* get data and open window */ |
248 |
> |
int argc; |
249 |
> |
char **argv; |
250 |
|
{ |
219 |
– |
XWMHints ourxwmhints; |
251 |
|
XSetWindowAttributes ourwinattr; |
252 |
< |
XSizeHints oursizhints; |
252 |
> |
XClassHint xclshints; |
253 |
> |
XWMHints xwmhints; |
254 |
> |
XSizeHints xszhints; |
255 |
> |
XTextProperty windowName, iconName; |
256 |
> |
XGCValues xgcv; |
257 |
> |
char *name; |
258 |
|
register int i; |
259 |
|
|
260 |
|
if (fname != NULL) { |
261 |
|
scanpos = (long *)malloc(ymax*sizeof(long)); |
262 |
|
if (scanpos == NULL) |
263 |
< |
goto memerr; |
263 |
> |
quiterr("out of memory"); |
264 |
|
for (i = 0; i < ymax; i++) |
265 |
|
scanpos[i] = -1; |
266 |
< |
} |
266 |
> |
name = fname; |
267 |
> |
} else |
268 |
> |
name = progname; |
269 |
> |
/* remove directory prefix from name */ |
270 |
> |
for (i = strlen(name); i-- > 0; ) |
271 |
> |
if (name[i] == '/') |
272 |
> |
break; |
273 |
> |
name += i+1; |
274 |
|
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
275 |
|
quiterr("cannot open display"); |
276 |
+ |
/* set gamma value */ |
277 |
+ |
if (gamstr == NULL) /* get it from the X server */ |
278 |
+ |
gamstr = XGetDefault(thedisplay, "radiance", "gamma"); |
279 |
+ |
if (gamstr == NULL) /* get it from the environment */ |
280 |
+ |
gamstr = getenv("DISPLAY_GAMMA"); |
281 |
+ |
if (gamstr != NULL) |
282 |
+ |
gamcor = atof(gamstr); |
283 |
|
/* get best visual for default screen */ |
284 |
|
getbestvis(); |
285 |
|
/* store image */ |
286 |
|
getras(); |
287 |
< |
/* open window */ |
288 |
< |
ourwinattr.border_pixel = ourblack; |
289 |
< |
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); |
287 |
> |
/* get size and position */ |
288 |
> |
xszhints.flags = 0; |
289 |
> |
xszhints.width = xmax; xszhints.height = ymax; |
290 |
|
if (geometry != NULL) { |
291 |
< |
bzero((char *)&oursizhints, sizeof(oursizhints)); |
292 |
< |
i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, |
293 |
< |
(unsigned *)&oursizhints.width, |
265 |
< |
(unsigned *)&oursizhints.height); |
291 |
> |
i = XParseGeometry(geometry, &xszhints.x, &xszhints.y, |
292 |
> |
(unsigned *)&xszhints.width, |
293 |
> |
(unsigned *)&xszhints.height); |
294 |
|
if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) |
295 |
< |
oursizhints.flags |= USSize; |
296 |
< |
else { |
297 |
< |
oursizhints.width = xmax; |
270 |
< |
oursizhints.height = ymax; |
271 |
< |
oursizhints.flags |= PSize; |
272 |
< |
} |
295 |
> |
xszhints.flags |= USSize; |
296 |
> |
else |
297 |
> |
xszhints.flags |= PSize; |
298 |
|
if ((i&(XValue|YValue)) == (XValue|YValue)) { |
299 |
< |
oursizhints.flags |= USPosition; |
299 |
> |
xszhints.flags |= USPosition; |
300 |
|
if (i & XNegative) |
301 |
< |
oursizhints.x += DisplayWidth(thedisplay, |
302 |
< |
ourscreen)-1-oursizhints.width-2*BORWIDTH; |
301 |
> |
xszhints.x += DisplayWidth(thedisplay, |
302 |
> |
ourscreen)-1-xszhints.width-2*BORWIDTH; |
303 |
|
if (i & YNegative) |
304 |
< |
oursizhints.y += DisplayHeight(thedisplay, |
305 |
< |
ourscreen)-1-oursizhints.height-2*BORWIDTH; |
304 |
> |
xszhints.y += DisplayHeight(thedisplay, |
305 |
> |
ourscreen)-1-xszhints.height-2*BORWIDTH; |
306 |
|
} |
282 |
– |
XSetNormalHints(thedisplay, wind, &oursizhints); |
307 |
|
} |
308 |
< |
ourxwmhints.flags = InputHint|IconPixmapHint; |
309 |
< |
ourxwmhints.input = True; |
310 |
< |
ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
308 |
> |
/* open window */ |
309 |
> |
i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; |
310 |
> |
ourwinattr.border_pixel = ourwhite; |
311 |
> |
ourwinattr.background_pixel = ourblack; |
312 |
> |
if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) { |
313 |
> |
ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual); |
314 |
> |
i |= CWColormap; |
315 |
> |
} |
316 |
> |
ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| |
317 |
> |
ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; |
318 |
> |
ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); |
319 |
> |
wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, |
320 |
> |
xszhints.width, xszhints.height, BORWIDTH, |
321 |
> |
ourvis.depth, InputOutput, ourvis.visual, |
322 |
> |
i, &ourwinattr); |
323 |
> |
if (wind == 0) |
324 |
> |
quiterr("cannot create window"); |
325 |
> |
width = xmax; |
326 |
> |
height = ymax; |
327 |
> |
/* prepare graphics drawing context */ |
328 |
> |
if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) |
329 |
> |
quiterr("cannot get font"); |
330 |
> |
xgcv.foreground = ourblack; |
331 |
> |
xgcv.background = ourwhite; |
332 |
> |
ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
333 |
> |
GCFont, &xgcv); |
334 |
> |
xgcv.function = GXinvert; |
335 |
> |
revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
336 |
> |
GCFunction, &xgcv); |
337 |
> |
|
338 |
> |
/* set up the window manager */ |
339 |
> |
xwmhints.flags = InputHint|IconPixmapHint; |
340 |
> |
xwmhints.input = True; |
341 |
> |
xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, |
342 |
|
wind, icondata, iconwidth, iconheight); |
343 |
< |
XSetWMHints(thedisplay, wind, &ourxwmhints); |
344 |
< |
XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |
345 |
< |
|ButtonMotionMask|StructureNotifyMask |
346 |
< |
|KeyPressMask|ExposureMask); |
343 |
> |
|
344 |
> |
windowName.encoding = iconName.encoding = XA_STRING; |
345 |
> |
windowName.format = iconName.format = 8; |
346 |
> |
windowName.value = (u_char *)name; |
347 |
> |
windowName.nitems = strlen(windowName.value); |
348 |
> |
iconName.value = (u_char *)name; |
349 |
> |
iconName.nitems = strlen(windowName.value); |
350 |
> |
|
351 |
> |
xclshints.res_name = NULL; |
352 |
> |
xclshints.res_class = "Ximage"; |
353 |
> |
XSetWMProperties(thedisplay, wind, &windowName, &iconName, |
354 |
> |
argv, argc, &xszhints, &xwmhints, &xclshints); |
355 |
> |
closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False); |
356 |
> |
wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False); |
357 |
> |
XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); |
358 |
> |
|
359 |
|
XMapWindow(thedisplay, wind); |
293 |
– |
return; |
294 |
– |
memerr: |
295 |
– |
quiterr("out of memory"); |
360 |
|
} /* end of init */ |
361 |
|
|
362 |
|
|
363 |
|
quiterr(err) /* print message and exit */ |
364 |
|
char *err; |
365 |
|
{ |
366 |
< |
if (err != NULL) { |
367 |
< |
fprintf(stderr, "%s: %s\n", progname, err); |
368 |
< |
exit(1); |
366 |
> |
register int es; |
367 |
> |
int cs; |
368 |
> |
|
369 |
> |
if (es = err != NULL) |
370 |
> |
fprintf(stderr, "%s: %s: %s\n", progname, |
371 |
> |
fname==NULL?"<stdin>":fname, err); |
372 |
> |
if (thedisplay != NULL) |
373 |
> |
XCloseDisplay(thedisplay); |
374 |
> |
if (parent < 0 & sigrecv == 0) |
375 |
> |
kill(getppid(), SIGCONT); |
376 |
> |
while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ |
377 |
> |
if (es == 0) |
378 |
> |
es = cs>>8 & 0xff; |
379 |
> |
parent--; |
380 |
|
} |
381 |
< |
exit(0); |
381 |
> |
exit(es); |
382 |
|
} |
383 |
|
|
384 |
|
|
310 |
– |
eputs(s) |
311 |
– |
char *s; |
312 |
– |
{ |
313 |
– |
fputs(s, stderr); |
314 |
– |
} |
315 |
– |
|
316 |
– |
|
317 |
– |
quit(code) |
318 |
– |
int code; |
319 |
– |
{ |
320 |
– |
exit(code); |
321 |
– |
} |
322 |
– |
|
323 |
– |
|
385 |
|
static int |
386 |
|
viscmp(v1,v2) /* compare visual to see which is better, descending */ |
387 |
|
register XVisualInfo *v1, *v2; |
485 |
|
greyscale = 1; |
486 |
|
if (ourvis.depth <= 8 && ourvis.colormap_size < maxcolors) |
487 |
|
maxcolors = ourvis.colormap_size; |
427 |
– |
if (maxcolors > 4) |
428 |
– |
maxcolors -= 2; |
488 |
|
if (ourvis.class == StaticGray) { |
489 |
|
ourblack = 0; |
490 |
|
ourwhite = 255; |
495 |
|
ourblack = 0; |
496 |
|
ourwhite = 1; |
497 |
|
} |
498 |
+ |
if (maxcolors > 4) |
499 |
+ |
maxcolors -= 2; |
500 |
|
} else { |
501 |
|
ourblack = 0; |
502 |
|
ourwhite = ourvis.red_mask|ourvis.green_mask|ourvis.blue_mask; |
507 |
|
|
508 |
|
getras() /* get raster file */ |
509 |
|
{ |
449 |
– |
colormap ourmap; |
510 |
|
XVisualInfo vinfo; |
511 |
|
|
512 |
|
if (maxcolors <= 2) { /* monochrome */ |
518 |
|
if (ourras == NULL) |
519 |
|
goto fail; |
520 |
|
getmono(); |
521 |
< |
} else if (ourvis.class == TrueColor || ourvis.class == DirectColor) { |
522 |
< |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
521 |
> |
} else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { |
522 |
> |
ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); |
523 |
|
if (ourdata == NULL) |
524 |
|
goto fail; |
525 |
< |
ourras = make_raster(thedisplay, &ourvis, 32, |
525 |
> |
ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, |
526 |
|
ourdata, xmax, ymax, 32); |
527 |
|
if (ourras == NULL) |
528 |
|
goto fail; |
535 |
|
xmax, ymax, 8); |
536 |
|
if (ourras == NULL) |
537 |
|
goto fail; |
538 |
< |
if (ourvis.class == StaticGray) |
538 |
> |
if (greyscale | ourvis.class == StaticGray) |
539 |
|
getgrey(); |
540 |
< |
else { |
541 |
< |
if (greyscale) |
542 |
< |
biq(dither,maxcolors,1,ourmap); |
543 |
< |
else |
484 |
< |
ciq(dither,maxcolors,1,ourmap); |
485 |
< |
if (init_rcolors(ourras, ourmap[0], |
486 |
< |
ourmap[1], ourmap[2]) == 0) |
487 |
< |
goto fail; |
488 |
< |
} |
540 |
> |
else |
541 |
> |
getmapped(); |
542 |
> |
if (ourvis.class != StaticGray && !init_rcolors(ourras,clrtab)) |
543 |
> |
goto fail; |
544 |
|
} |
545 |
< |
return; |
545 |
> |
return; |
546 |
|
fail: |
547 |
|
quiterr("could not create raster image"); |
548 |
|
} |
550 |
|
|
551 |
|
getevent() /* process the next event */ |
552 |
|
{ |
553 |
< |
union { |
499 |
< |
XEvent u; |
500 |
< |
XConfigureEvent c; |
501 |
< |
XExposeEvent e; |
502 |
< |
XButtonPressedEvent b; |
503 |
< |
XKeyPressedEvent k; |
504 |
< |
} e; |
553 |
> |
XEvent xev; |
554 |
|
|
555 |
< |
XNextEvent(thedisplay, &e.u); |
556 |
< |
switch (e.u.type) { |
555 |
> |
XNextEvent(thedisplay, &xev); |
556 |
> |
switch ((int)xev.type) { |
557 |
|
case KeyPress: |
558 |
< |
docom(&e.k); |
558 |
> |
docom(&xev.xkey); |
559 |
|
break; |
560 |
|
case ConfigureNotify: |
561 |
< |
width = e.c.width; |
562 |
< |
height = e.c.height; |
561 |
> |
width = xev.xconfigure.width; |
562 |
> |
height = xev.xconfigure.height; |
563 |
|
break; |
564 |
|
case MapNotify: |
565 |
|
map_rcolors(ourras, wind); |
566 |
|
if (fast) |
567 |
|
make_rpixmap(ourras, wind); |
568 |
+ |
if (!sequential & parent < 0 & sigrecv == 0) { |
569 |
+ |
kill(getppid(), SIGCONT); |
570 |
+ |
sigrecv--; |
571 |
+ |
} |
572 |
|
break; |
573 |
|
case UnmapNotify: |
574 |
|
if (!fast) |
575 |
|
unmap_rcolors(ourras); |
576 |
|
break; |
577 |
|
case Expose: |
578 |
< |
redraw(e.e.x, e.e.y, e.e.width, e.e.height); |
578 |
> |
redraw(xev.xexpose.x, xev.xexpose.y, |
579 |
> |
xev.xexpose.width, xev.xexpose.height); |
580 |
|
break; |
581 |
|
case ButtonPress: |
582 |
< |
if (e.b.state & (ShiftMask|ControlMask)) |
583 |
< |
moveimage(&e.b); |
582 |
> |
if (xev.xbutton.state & (ShiftMask|ControlMask)) |
583 |
> |
moveimage(&xev.xbutton); |
584 |
> |
else if (xev.xbutton.button == Button2) |
585 |
> |
traceray(xev.xbutton.x, xev.xbutton.y); |
586 |
|
else |
587 |
< |
getbox(&e.b); |
587 |
> |
getbox(&xev.xbutton); |
588 |
|
break; |
589 |
+ |
case ClientMessage: |
590 |
+ |
if ((xev.xclient.message_type == wmProtocolsAtom) && |
591 |
+ |
(xev.xclient.data.l[0] == closedownAtom)) |
592 |
+ |
quiterr(NULL); |
593 |
+ |
break; |
594 |
|
} |
595 |
|
} |
596 |
|
|
597 |
|
|
598 |
< |
docom(ekey) /* execute command */ |
598 |
> |
traceray(xpos, ypos) /* print ray corresponding to pixel */ |
599 |
> |
int xpos, ypos; |
600 |
> |
{ |
601 |
> |
FLOAT hv[2]; |
602 |
> |
FVECT rorg, rdir; |
603 |
> |
|
604 |
> |
if (!gotview) { /* no view, no can do */ |
605 |
> |
XBell(thedisplay, 0); |
606 |
> |
return(-1); |
607 |
> |
} |
608 |
> |
pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); |
609 |
> |
if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
610 |
> |
return(-1); |
611 |
> |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
612 |
> |
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
613 |
> |
fflush(stdout); |
614 |
> |
return(0); |
615 |
> |
} |
616 |
> |
|
617 |
> |
|
618 |
> |
docom(ekey) /* execute command */ |
619 |
|
XKeyPressedEvent *ekey; |
620 |
|
{ |
621 |
|
char buf[80]; |
624 |
|
int com, n; |
625 |
|
double comp; |
626 |
|
FLOAT hv[2]; |
546 |
– |
FVECT rorg, rdir; |
627 |
|
|
628 |
|
n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); |
629 |
|
if (n == 0) |
631 |
|
com = buf[0]; |
632 |
|
switch (com) { /* interpret command */ |
633 |
|
case 'q': |
634 |
+ |
case 'Q': |
635 |
|
case CTRL('D'): /* quit */ |
636 |
< |
quit(0); |
636 |
> |
quiterr(NULL); |
637 |
|
case '\n': |
638 |
|
case '\r': |
639 |
|
case 'l': |
679 |
|
buf, strlen(buf)); |
680 |
|
return(0); |
681 |
|
case 't': /* trace */ |
682 |
< |
if (!gotview) { |
602 |
< |
XBell(thedisplay, 0); |
603 |
< |
return(-1); |
604 |
< |
} |
605 |
< |
pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); |
606 |
< |
if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) |
607 |
< |
return(-1); |
608 |
< |
printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); |
609 |
< |
printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); |
610 |
< |
fflush(stdout); |
611 |
< |
return(0); |
682 |
> |
return(traceray(ekey->x, ekey->y)); |
683 |
|
case '=': /* adjust exposure */ |
684 |
+ |
case '@': /* adaptation level */ |
685 |
|
if (avgbox(cval) == -1) |
686 |
|
return(-1); |
687 |
< |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
687 |
> |
comp = bright(cval); |
688 |
> |
if (comp < 1e-20) { |
689 |
> |
XBell(thedisplay, 0); |
690 |
> |
return(-1); |
691 |
> |
} |
692 |
> |
if (com == '@') |
693 |
> |
comp = 106./exposure/ |
694 |
> |
pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); |
695 |
> |
else |
696 |
> |
comp = .5/comp; |
697 |
> |
comp = log(comp)/.69315 - scale; |
698 |
> |
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
699 |
|
if (n == 0) |
700 |
|
return(0); |
701 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
714 |
|
XClearWindow(thedisplay, wind); |
715 |
|
map_rcolors(ourras, wind); |
716 |
|
if (fast) |
717 |
< |
make_rpixmap(ourras); |
717 |
> |
make_rpixmap(ourras, wind); |
718 |
|
redraw(0, 0, width, height); |
719 |
|
return(0); |
720 |
+ |
case 'f': /* turn on fast redraw */ |
721 |
+ |
fast = 1; |
722 |
+ |
make_rpixmap(ourras, wind); |
723 |
+ |
return(0); |
724 |
+ |
case 'F': /* turn off fast redraw */ |
725 |
+ |
fast = 0; |
726 |
+ |
free_rpixmap(ourras); |
727 |
+ |
return(0); |
728 |
+ |
case '0': /* recenter origin */ |
729 |
+ |
if (xoff == 0 & yoff == 0) |
730 |
+ |
return(0); |
731 |
+ |
xoff = yoff = 0; |
732 |
+ |
XClearWindow(thedisplay, wind); |
733 |
+ |
redraw(0, 0, width, height); |
734 |
+ |
return(0); |
735 |
|
case ' ': /* clear */ |
736 |
|
redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); |
737 |
|
return(0); |
745 |
|
moveimage(ebut) /* shift the image */ |
746 |
|
XButtonPressedEvent *ebut; |
747 |
|
{ |
748 |
< |
union { |
651 |
< |
XEvent u; |
652 |
< |
XButtonReleasedEvent b; |
653 |
< |
XPointerMovedEvent m; |
654 |
< |
} e; |
748 |
> |
XEvent e; |
749 |
|
int mxo, myo; |
750 |
|
|
751 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
752 |
< |
while (e.u.type == MotionNotify) { |
753 |
< |
mxo = e.m.x; |
754 |
< |
myo = e.m.y; |
751 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); |
752 |
> |
while (e.type == MotionNotify) { |
753 |
> |
mxo = e.xmotion.x; |
754 |
> |
myo = e.xmotion.y; |
755 |
|
revline(ebut->x, ebut->y, mxo, myo); |
756 |
|
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
757 |
|
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
758 |
< |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
758 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); |
759 |
|
revline(ebut->x, ebut->y, mxo, myo); |
760 |
|
revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, |
761 |
|
xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); |
762 |
|
} |
763 |
< |
xoff += e.b.x - ebut->x; |
764 |
< |
yoff += e.b.y - ebut->y; |
763 |
> |
xoff += e.xbutton.x - ebut->x; |
764 |
> |
yoff += e.xbutton.y - ebut->y; |
765 |
|
XClearWindow(thedisplay, wind); |
766 |
|
redraw(0, 0, width, height); |
767 |
|
} |
770 |
|
getbox(ebut) /* get new box */ |
771 |
|
XButtonPressedEvent *ebut; |
772 |
|
{ |
773 |
< |
union { |
680 |
< |
XEvent u; |
681 |
< |
XButtonReleasedEvent b; |
682 |
< |
XPointerMovedEvent m; |
683 |
< |
} e; |
773 |
> |
XEvent e; |
774 |
|
|
775 |
< |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); |
776 |
< |
while (e.u.type == MotionNotify) { |
777 |
< |
revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); |
778 |
< |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); |
775 |
> |
XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); |
776 |
> |
while (e.type == MotionNotify) { |
777 |
> |
revbox(ebut->x, ebut->y, box.xmin = e.xmotion.x, box.ymin = e.xmotion.y); |
778 |
> |
XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); |
779 |
|
revbox(ebut->x, ebut->y, box.xmin, box.ymin); |
780 |
|
} |
781 |
< |
box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); |
782 |
< |
box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y); |
781 |
> |
box.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); |
782 |
> |
box.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); |
783 |
|
if (box.xmin > ebut->x) { |
784 |
|
box.xsiz = box.xmin - ebut->x + 1; |
785 |
|
box.xmin = ebut->x; |
835 |
|
return(-1); |
836 |
|
if (left == ll && right == lr && top == lt && bottom == lb) { |
837 |
|
copycolor(clr, lc); |
838 |
< |
return; |
838 |
> |
return(0); |
839 |
|
} |
840 |
|
for (y = top; y < bottom; y++) { |
841 |
|
if (getscan(y) == -1) |
864 |
|
quiterr("out of memory in getmono"); |
865 |
|
dp = ourdata - 1; |
866 |
|
for (y = 0; y < ymax; y++) { |
867 |
< |
if (getscan(y) < 0) |
778 |
< |
quiterr("seek error in getmono"); |
779 |
< |
normcolrs(scanline, xmax, scale); |
867 |
> |
getscan(y); |
868 |
|
add2icon(y, scanline); |
869 |
+ |
normcolrs(scanline, xmax, scale); |
870 |
|
err = 0; |
871 |
|
for (x = 0; x < xmax; x++) { |
872 |
|
if (!(x&7)) |
892 |
|
static short cerr[ICONSIZ]; |
893 |
|
static int ynext; |
894 |
|
static char *dp; |
895 |
+ |
COLR clr; |
896 |
|
register int err; |
897 |
|
register int x, ti; |
898 |
|
int errp; |
923 |
|
*++dp = 0; |
924 |
|
errp = err; |
925 |
|
ti = x*xmax/iconwidth; |
926 |
< |
err += normbright(scan[ti]) + cerr[x]; |
926 |
> |
copycolr(clr, scan[ti]); |
927 |
> |
normcolrs(clr, 1, scale); |
928 |
> |
err += normbright(clr) + cerr[x]; |
929 |
|
if (err > 127) |
930 |
|
err -= 255; |
931 |
|
else |
940 |
|
getfull() /* get full (24-bit) data */ |
941 |
|
{ |
942 |
|
int y; |
943 |
< |
register unsigned long *dp; |
943 |
> |
register unsigned int4 *dp; |
944 |
|
register int x; |
945 |
|
/* set gamma correction */ |
946 |
|
setcolrgam(gamcor); |
947 |
|
/* read and convert file */ |
948 |
< |
dp = (unsigned long *)ourdata; |
948 |
> |
dp = (unsigned int4 *)ourdata; |
949 |
|
for (y = 0; y < ymax; y++) { |
950 |
< |
if (getscan(y) < 0) |
951 |
< |
quiterr("seek error in getfull"); |
950 |
> |
getscan(y); |
951 |
> |
add2icon(y, scanline); |
952 |
|
if (scale) |
953 |
|
shiftcolrs(scanline, xmax, scale); |
954 |
|
colrs_gambs(scanline, xmax); |
863 |
– |
add2icon(y, scanline); |
955 |
|
if (ourras->image->blue_mask & 1) |
956 |
|
for (x = 0; x < xmax; x++) |
957 |
< |
*dp++ = scanline[x][RED] << 16 | |
958 |
< |
scanline[x][GRN] << 8 | |
959 |
< |
scanline[x][BLU] ; |
957 |
> |
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
958 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
959 |
> |
(unsigned int4)scanline[x][BLU] ; |
960 |
|
else |
961 |
|
for (x = 0; x < xmax; x++) |
962 |
< |
*dp++ = scanline[x][RED] | |
963 |
< |
scanline[x][GRN] << 8 | |
964 |
< |
scanline[x][BLU] << 16 ; |
962 |
> |
*dp++ = (unsigned int4)scanline[x][RED] | |
963 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
964 |
> |
(unsigned int4)scanline[x][BLU] << 16 ; |
965 |
|
} |
966 |
|
} |
967 |
|
|
976 |
|
/* read and convert file */ |
977 |
|
dp = ourdata; |
978 |
|
for (y = 0; y < ymax; y++) { |
979 |
< |
if (getscan(y) < 0) |
980 |
< |
quiterr("seek error in getfull"); |
979 |
> |
getscan(y); |
980 |
> |
add2icon(y, scanline); |
981 |
|
if (scale) |
982 |
|
shiftcolrs(scanline, xmax, scale); |
983 |
+ |
for (x = 0; x < xmax; x++) |
984 |
+ |
scanline[x][GRN] = normbright(scanline[x]); |
985 |
|
colrs_gambs(scanline, xmax); |
986 |
< |
add2icon(y, scanline); |
894 |
< |
if (ourvis.colormap_size < 256) |
986 |
> |
if (maxcolors < 256) |
987 |
|
for (x = 0; x < xmax; x++) |
988 |
< |
*dp++ = ((long)normbright(scanline[x]) * |
989 |
< |
ourvis.colormap_size + 128) >> 8; |
988 |
> |
*dp++ = ((long)scanline[x][GRN] * |
989 |
> |
maxcolors + maxcolors/2) >> 8; |
990 |
|
else |
991 |
|
for (x = 0; x < xmax; x++) |
992 |
< |
*dp++ = normbright(scanline[x]); |
992 |
> |
*dp++ = scanline[x][GRN]; |
993 |
|
} |
994 |
+ |
for (x = 0; x < maxcolors; x++) |
995 |
+ |
clrtab[x][RED] = clrtab[x][GRN] = |
996 |
+ |
clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; |
997 |
|
} |
998 |
|
|
999 |
|
|
1000 |
+ |
getmapped() /* get color-mapped data */ |
1001 |
+ |
{ |
1002 |
+ |
int y; |
1003 |
+ |
/* make sure we can do it first */ |
1004 |
+ |
if (fname == NULL) |
1005 |
+ |
quiterr("cannot map colors from standard input"); |
1006 |
+ |
/* set gamma correction */ |
1007 |
+ |
setcolrgam(gamcor); |
1008 |
+ |
/* make histogram */ |
1009 |
+ |
if (new_histo((long)xmax*ymax) == -1) |
1010 |
+ |
quiterr("cannot initialize histogram"); |
1011 |
+ |
for (y = 0; y < ymax; y++) { |
1012 |
+ |
if (getscan(y) < 0) |
1013 |
+ |
break; |
1014 |
+ |
add2icon(y, scanline); |
1015 |
+ |
if (scale) |
1016 |
+ |
shiftcolrs(scanline, xmax, scale); |
1017 |
+ |
colrs_gambs(scanline, xmax); |
1018 |
+ |
cnt_colrs(scanline, xmax); |
1019 |
+ |
} |
1020 |
+ |
/* map pixels */ |
1021 |
+ |
if (!new_clrtab(maxcolors)) |
1022 |
+ |
quiterr("cannot create color map"); |
1023 |
+ |
for (y = 0; y < ymax; y++) { |
1024 |
+ |
getscan(y); |
1025 |
+ |
if (scale) |
1026 |
+ |
shiftcolrs(scanline, xmax, scale); |
1027 |
+ |
colrs_gambs(scanline, xmax); |
1028 |
+ |
if (dither) |
1029 |
+ |
dith_colrs(ourdata+y*xmax, scanline, xmax); |
1030 |
+ |
else |
1031 |
+ |
map_colrs(ourdata+y*xmax, scanline, xmax); |
1032 |
+ |
} |
1033 |
+ |
} |
1034 |
+ |
|
1035 |
+ |
|
1036 |
|
scale_rcolors(xr, sf) /* scale color map */ |
1037 |
|
register XRASTER *xr; |
1038 |
|
double sf; |
1064 |
|
getscan(y) |
1065 |
|
int y; |
1066 |
|
{ |
1067 |
+ |
static int trunced = -1; /* truncated file? */ |
1068 |
+ |
skipit: |
1069 |
+ |
if (trunced >= 0 && y >= trunced) { |
1070 |
+ |
bzero(scanline, xmax*sizeof(COLR)); |
1071 |
+ |
return(-1); |
1072 |
+ |
} |
1073 |
|
if (y != cury) { |
1074 |
|
if (scanpos == NULL || scanpos[y] == -1) |
1075 |
|
return(-1); |
1079 |
|
} else if (scanpos != NULL && scanpos[y] == -1) |
1080 |
|
scanpos[y] = ftell(fin); |
1081 |
|
|
1082 |
< |
if (freadcolrs(scanline, xmax, fin) < 0) |
1083 |
< |
quiterr("read error"); |
1084 |
< |
|
1082 |
> |
if (freadcolrs(scanline, xmax, fin) < 0) { |
1083 |
> |
fprintf(stderr, "%s: %s: unfinished picture\n", |
1084 |
> |
progname, fname==NULL?"<stdin>":fname); |
1085 |
> |
trunced = y; |
1086 |
> |
goto skipit; |
1087 |
> |
} |
1088 |
|
cury++; |
1089 |
|
return(0); |
950 |
– |
} |
951 |
– |
|
952 |
– |
|
953 |
– |
picreadline3(y, l3) /* read in 3-byte scanline */ |
954 |
– |
int y; |
955 |
– |
register rgbpixel *l3; |
956 |
– |
{ |
957 |
– |
register int i; |
958 |
– |
/* read scanline */ |
959 |
– |
if (getscan(y) < 0) |
960 |
– |
quiterr("cannot seek for picreadline"); |
961 |
– |
/* convert scanline */ |
962 |
– |
normcolrs(scanline, xmax, scale); |
963 |
– |
add2icon(y, scanline); |
964 |
– |
for (i = 0; i < xmax; i++) { |
965 |
– |
l3[i].r = scanline[i][RED]; |
966 |
– |
l3[i].g = scanline[i][GRN]; |
967 |
– |
l3[i].b = scanline[i][BLU]; |
968 |
– |
} |
969 |
– |
} |
970 |
– |
|
971 |
– |
|
972 |
– |
picwriteline(y, l) /* add 8-bit scanline to image */ |
973 |
– |
int y; |
974 |
– |
pixel *l; |
975 |
– |
{ |
976 |
– |
bcopy((char *)l, (char *)ourdata+y*xmax, xmax); |
977 |
– |
} |
978 |
– |
|
979 |
– |
|
980 |
– |
picreadcm(map) /* do gamma correction */ |
981 |
– |
colormap map; |
982 |
– |
{ |
983 |
– |
extern double pow(); |
984 |
– |
register int i, val; |
985 |
– |
|
986 |
– |
for (i = 0; i < 256; i++) { |
987 |
– |
val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; |
988 |
– |
map[0][i] = map[1][i] = map[2][i] = val; |
989 |
– |
} |
990 |
– |
} |
991 |
– |
|
992 |
– |
|
993 |
– |
picwritecm(map) /* handled elsewhere */ |
994 |
– |
colormap map; |
995 |
– |
{ |
996 |
– |
#if 0 |
997 |
– |
register int i; |
998 |
– |
|
999 |
– |
for (i = 0; i < 256; i++) |
1000 |
– |
printf("%d %d %d\n", map[0][i],map[1][i],map[2][i]); |
1001 |
– |
#endif |
1090 |
|
} |