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> |
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)-'@') |
72 |
|
int xoff = 0; /* x image offset */ |
73 |
|
int yoff = 0; /* y image offset */ |
74 |
|
|
75 |
+ |
int parent = 0; /* number of children, -1 if child */ |
76 |
+ |
|
77 |
|
VIEW ourview = STDVIEW; /* image view parameters */ |
78 |
|
int gotview = 0; /* got parameters from file */ |
79 |
|
|
119 |
|
Display *thedisplay; |
120 |
|
Atom closedownAtom, wmProtocolsAtom; |
121 |
|
|
122 |
+ |
int sigrecv; |
123 |
|
|
124 |
+ |
int onsig() { sigrecv++; } |
125 |
+ |
|
126 |
+ |
|
127 |
|
main(argc, argv) |
128 |
|
int argc; |
129 |
|
char *argv[]; |
132 |
|
char *gv; |
133 |
|
int headline(); |
134 |
|
int i; |
135 |
+ |
int pid; |
136 |
|
|
137 |
|
progname = argv[0]; |
138 |
|
if ((gv = getenv("GAMMA")) != NULL) |
178 |
|
else |
179 |
|
break; |
180 |
|
|
181 |
< |
if (i == argc-1) { |
181 |
> |
if (i > argc) |
182 |
> |
goto userr; |
183 |
> |
while (i < argc-1) { |
184 |
> |
sigrecv = 0; |
185 |
> |
signal(SIGCONT, onsig); |
186 |
> |
if ((pid=fork()) == 0) { /* a child for each picture */ |
187 |
> |
parent = -1; |
188 |
> |
break; |
189 |
> |
} |
190 |
> |
if (pid < 0) |
191 |
> |
quiterr("fork failed"); |
192 |
> |
parent++; |
193 |
> |
while (!sigrecv) |
194 |
> |
pause(); /* wait for wake-up call */ |
195 |
> |
i++; |
196 |
> |
} |
197 |
> |
if (i < argc) { /* open picture file */ |
198 |
|
fname = argv[i]; |
199 |
|
fin = fopen(fname, "r"); |
200 |
< |
if (fin == NULL) { |
201 |
< |
sprintf(errmsg, "cannot open file \"%s\"", fname); |
202 |
< |
quiterr(errmsg); |
172 |
< |
} |
173 |
< |
} else if (i != argc) |
174 |
< |
goto userr; |
200 |
> |
if (fin == NULL) |
201 |
> |
quiterr("cannot open picture file"); |
202 |
> |
} |
203 |
|
/* get header */ |
204 |
|
getheader(fin, headline, NULL); |
205 |
|
/* get picture dimensions */ |
215 |
|
|
216 |
|
init(argc, argv); /* get file and open window */ |
217 |
|
|
218 |
+ |
if (parent < 0) { |
219 |
+ |
kill(getppid(), SIGCONT); /* signal parent if child */ |
220 |
+ |
sigrecv--; |
221 |
+ |
} |
222 |
|
for ( ; ; ) |
223 |
|
getevent(); /* main loop */ |
224 |
|
userr: |
225 |
|
fprintf(stderr, |
226 |
< |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n", |
226 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n", |
227 |
|
progname); |
228 |
|
exit(1); |
229 |
|
} |
314 |
|
quiterr("cannot create window"); |
315 |
|
width = xmax; |
316 |
|
height = ymax; |
317 |
< |
xgcv.foreground = ourblack; |
286 |
< |
xgcv.background = ourwhite; |
317 |
> |
/* prepare graphics drawing context */ |
318 |
|
if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) |
319 |
|
quiterr("cannot get font"); |
320 |
+ |
xgcv.foreground = ourblack; |
321 |
+ |
xgcv.background = ourwhite; |
322 |
|
ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
323 |
|
GCFont, &xgcv); |
324 |
|
xgcv.function = GXinvert; |
354 |
|
quiterr(err) /* print message and exit */ |
355 |
|
char *err; |
356 |
|
{ |
357 |
< |
if (err != NULL) { |
358 |
< |
fprintf(stderr, "%s: %s\n", progname, err); |
359 |
< |
exit(1); |
357 |
> |
register int es; |
358 |
> |
int cs; |
359 |
> |
|
360 |
> |
if (es = err != NULL) |
361 |
> |
fprintf(stderr, "%s: %s: %s\n", progname, |
362 |
> |
fname==NULL?"<stdin>":fname, err); |
363 |
> |
if (parent > 0 & wind != 0) { |
364 |
> |
XDestroyWindow(thedisplay, wind); |
365 |
> |
XFlush(thedisplay); |
366 |
> |
} else if (parent < 0 & sigrecv == 0) |
367 |
> |
kill(getppid(), SIGCONT); |
368 |
> |
while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ |
369 |
> |
if (es == 0) |
370 |
> |
es = cs>>8 & 0xff; |
371 |
> |
parent--; |
372 |
|
} |
373 |
< |
exit(0); |
373 |
> |
exit(es); |
374 |
|
} |
375 |
|
|
376 |
|
|
511 |
|
goto fail; |
512 |
|
getmono(); |
513 |
|
} else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { |
514 |
< |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
514 |
> |
ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); |
515 |
|
if (ourdata == NULL) |
516 |
|
goto fail; |
517 |
< |
ourras = make_raster(thedisplay, &ourvis, 32, |
517 |
> |
ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, |
518 |
|
ourdata, xmax, ymax, 32); |
519 |
|
if (ourras == NULL) |
520 |
|
goto fail; |
574 |
|
else |
575 |
|
getbox(&xev.xbutton); |
576 |
|
break; |
577 |
< |
case ClientMessage: |
577 |
> |
case ClientMessage: |
578 |
|
if ((xev.xclient.message_type == wmProtocolsAtom) && |
579 |
|
(xev.xclient.data.l[0] == closedownAtom)) |
580 |
|
quiterr(NULL); |
672 |
|
case '@': /* adaptation level */ |
673 |
|
if (avgbox(cval) == -1) |
674 |
|
return(-1); |
675 |
< |
comp = com=='@' |
676 |
< |
? 59.2/pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure |
677 |
< |
: .5/bright(cval) ; |
675 |
> |
comp = bright(cval); |
676 |
> |
if (comp < 1e-20) { |
677 |
> |
XBell(thedisplay, 0); |
678 |
> |
return(-1); |
679 |
> |
} |
680 |
> |
if (com == '@') |
681 |
> |
comp = 106./exposure/ |
682 |
> |
pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); |
683 |
> |
else |
684 |
> |
comp = .5/comp; |
685 |
|
comp = log(comp)/.69315 - scale; |
686 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
687 |
|
if (n == 0) |
705 |
|
make_rpixmap(ourras, wind); |
706 |
|
redraw(0, 0, width, height); |
707 |
|
return(0); |
708 |
+ |
case 'f': /* turn on fast redraw */ |
709 |
+ |
fast = 1; |
710 |
+ |
make_rpixmap(ourras, wind); |
711 |
+ |
return(0); |
712 |
+ |
case 'F': /* turn off fast redraw */ |
713 |
+ |
fast = 0; |
714 |
+ |
free_rpixmap(ourras); |
715 |
+ |
return(0); |
716 |
|
case '0': /* recenter origin */ |
717 |
|
if (xoff == 0 & yoff == 0) |
718 |
|
return(0); |
928 |
|
getfull() /* get full (24-bit) data */ |
929 |
|
{ |
930 |
|
int y; |
931 |
< |
register unsigned long *dp; |
931 |
> |
register unsigned int4 *dp; |
932 |
|
register int x; |
933 |
|
/* set gamma correction */ |
934 |
|
setcolrgam(gamcor); |
935 |
|
/* read and convert file */ |
936 |
< |
dp = (unsigned long *)ourdata; |
936 |
> |
dp = (unsigned int4 *)ourdata; |
937 |
|
for (y = 0; y < ymax; y++) { |
938 |
|
getscan(y); |
939 |
|
add2icon(y, scanline); |
988 |
|
getmapped() /* get color-mapped data */ |
989 |
|
{ |
990 |
|
int y; |
991 |
+ |
/* make sure we can do it first */ |
992 |
+ |
if (fname == NULL) |
993 |
+ |
quiterr("cannot map colors from standard input"); |
994 |
|
/* set gamma correction */ |
995 |
|
setcolrgam(gamcor); |
996 |
|
/* make histogram */ |
997 |
|
new_histo(); |
998 |
|
for (y = 0; y < ymax; y++) { |
999 |
|
if (getscan(y) < 0) |
1000 |
< |
quiterr("seek error in getmapped"); |
1000 |
> |
break; |
1001 |
|
add2icon(y, scanline); |
1002 |
|
if (scale) |
1003 |
|
shiftcolrs(scanline, xmax, scale); |
1008 |
|
if (!new_clrtab(maxcolors)) |
1009 |
|
quiterr("cannot create color map"); |
1010 |
|
for (y = 0; y < ymax; y++) { |
1011 |
< |
if (getscan(y) < 0) |
949 |
< |
quiterr("seek error in getmapped"); |
1011 |
> |
getscan(y); |
1012 |
|
if (scale) |
1013 |
|
shiftcolrs(scanline, xmax, scale); |
1014 |
|
colrs_gambs(scanline, xmax); |
1051 |
|
getscan(y) |
1052 |
|
int y; |
1053 |
|
{ |
1054 |
+ |
static int trunced = -1; /* truncated file? */ |
1055 |
+ |
skipit: |
1056 |
+ |
if (trunced >= 0 && y >= trunced) { |
1057 |
+ |
bzero(scanline, xmax*sizeof(COLR)); |
1058 |
+ |
return(-1); |
1059 |
+ |
} |
1060 |
|
if (y != cury) { |
1061 |
|
if (scanpos == NULL || scanpos[y] == -1) |
1062 |
|
return(-1); |
1066 |
|
} else if (scanpos != NULL && scanpos[y] == -1) |
1067 |
|
scanpos[y] = ftell(fin); |
1068 |
|
|
1069 |
< |
if (freadcolrs(scanline, xmax, fin) < 0) |
1070 |
< |
quiterr("read error"); |
1071 |
< |
|
1069 |
> |
if (freadcolrs(scanline, xmax, fin) < 0) { |
1070 |
> |
fprintf(stderr, "%s: %s: unfinished picture\n", |
1071 |
> |
progname, fname==NULL?"<stdin>":fname); |
1072 |
> |
trunced = y; |
1073 |
> |
goto skipit; |
1074 |
> |
} |
1075 |
|
cury++; |
1076 |
|
return(0); |
1077 |
|
} |