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? */ |
113 |
|
|
114 |
|
char errmsg[128]; |
115 |
|
|
116 |
< |
extern BYTE clrtab[256][3]; /* global color map */ |
116 |
> |
BYTE clrtab[256][3]; /* global color map */ |
117 |
|
|
118 |
|
extern long ftell(); |
119 |
|
|
120 |
+ |
extern char *getenv(); |
121 |
+ |
|
122 |
|
Display *thedisplay; |
123 |
|
Atom closedownAtom, wmProtocolsAtom; |
124 |
|
|
125 |
< |
int noop() {} |
125 |
> |
int sigrecv; |
126 |
|
|
127 |
+ |
int onsig() { sigrecv++; } |
128 |
|
|
129 |
+ |
|
130 |
|
main(argc, argv) |
131 |
|
int argc; |
132 |
|
char *argv[]; |
133 |
|
{ |
129 |
– |
extern char *getenv(); |
130 |
– |
char *gv; |
134 |
|
int headline(); |
135 |
|
int i; |
136 |
|
int pid; |
137 |
|
|
138 |
|
progname = argv[0]; |
136 |
– |
if ((gv = getenv("GAMMA")) != NULL) |
137 |
– |
gamcor = atof(gv); |
139 |
|
|
140 |
|
for (i = 1; i < argc; i++) |
141 |
|
if (argv[i][0] == '-') |
167 |
|
if (argv[i][2] == 'e') |
168 |
|
geometry = argv[++i]; |
169 |
|
else |
170 |
< |
gamcor = atof(argv[++i]); |
170 |
> |
gamstr = argv[++i]; |
171 |
|
break; |
172 |
|
default: |
173 |
|
goto userr; |
180 |
|
if (i > argc) |
181 |
|
goto userr; |
182 |
|
while (i < argc-1) { |
183 |
+ |
sigrecv = 0; |
184 |
+ |
signal(SIGCONT, onsig); |
185 |
|
if ((pid=fork()) == 0) { /* a child for each picture */ |
186 |
|
parent = -1; |
187 |
|
break; |
189 |
|
if (pid < 0) |
190 |
|
quiterr("fork failed"); |
191 |
|
parent++; |
192 |
< |
signal(SIGCONT, noop); |
193 |
< |
pause(); /* wait for wake-up call */ |
192 |
> |
while (!sigrecv) |
193 |
> |
pause(); /* wait for wake-up call */ |
194 |
|
i++; |
195 |
|
} |
196 |
|
if (i < argc) { /* open picture file */ |
197 |
|
fname = argv[i]; |
198 |
|
fin = fopen(fname, "r"); |
199 |
< |
if (fin == NULL) { |
200 |
< |
sprintf(errmsg, "cannot open file \"%s\"", fname); |
198 |
< |
quiterr(errmsg); |
199 |
< |
} |
199 |
> |
if (fin == NULL) |
200 |
> |
quiterr("cannot open picture file"); |
201 |
|
} |
202 |
|
/* get header */ |
203 |
|
getheader(fin, headline, NULL); |
214 |
|
|
215 |
|
init(argc, argv); /* get file and open window */ |
216 |
|
|
216 |
– |
if (parent < 0) |
217 |
– |
kill(getppid(), SIGCONT); /* signal parent if child */ |
218 |
– |
|
217 |
|
for ( ; ; ) |
218 |
|
getevent(); /* main loop */ |
219 |
|
userr: |
220 |
|
fprintf(stderr, |
221 |
< |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n", |
221 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor] pic ..\n", |
222 |
|
progname); |
223 |
|
exit(1); |
224 |
|
} |
268 |
|
name += i+1; |
269 |
|
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
270 |
|
quiterr("cannot open display"); |
271 |
+ |
/* set gamma value */ |
272 |
+ |
if (gamstr == NULL) /* get it from the X server */ |
273 |
+ |
gamstr = XGetDefault(thedisplay, "radiance", "gamma"); |
274 |
+ |
if (gamstr == NULL) /* get it from the environment */ |
275 |
+ |
gamstr = getenv("DISPLAY_GAMMA"); |
276 |
+ |
if (gamstr != NULL) |
277 |
+ |
gamcor = atof(gamstr); |
278 |
|
/* get best visual for default screen */ |
279 |
|
getbestvis(); |
280 |
|
/* store image */ |
316 |
|
quiterr("cannot create window"); |
317 |
|
width = xmax; |
318 |
|
height = ymax; |
319 |
< |
xgcv.foreground = ourblack; |
315 |
< |
xgcv.background = ourwhite; |
319 |
> |
/* prepare graphics drawing context */ |
320 |
|
if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) |
321 |
|
quiterr("cannot get font"); |
322 |
+ |
xgcv.foreground = ourblack; |
323 |
+ |
xgcv.background = ourwhite; |
324 |
|
ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
325 |
|
GCFont, &xgcv); |
326 |
|
xgcv.function = GXinvert; |
349 |
|
XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); |
350 |
|
|
351 |
|
XMapWindow(thedisplay, wind); |
346 |
– |
return; |
352 |
|
} /* end of init */ |
353 |
|
|
354 |
|
|
355 |
|
quiterr(err) /* print message and exit */ |
356 |
|
char *err; |
357 |
|
{ |
358 |
< |
if (err != NULL) |
358 |
> |
register int es; |
359 |
> |
int cs; |
360 |
> |
|
361 |
> |
if (es = err != NULL) |
362 |
|
fprintf(stderr, "%s: %s: %s\n", progname, |
363 |
|
fname==NULL?"<stdin>":fname, err); |
364 |
< |
if (wind) { |
365 |
< |
XDestroyWindow(thedisplay, wind); |
366 |
< |
XFlush(thedisplay); |
367 |
< |
} |
368 |
< |
while (parent > 0 && wait(0) != -1) /* wait for any children */ |
364 |
> |
if (thedisplay != NULL) |
365 |
> |
XCloseDisplay(thedisplay); |
366 |
> |
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 |
< |
exit(err != NULL); |
372 |
> |
} |
373 |
> |
exit(es); |
374 |
|
} |
375 |
|
|
376 |
|
|
557 |
|
map_rcolors(ourras, wind); |
558 |
|
if (fast) |
559 |
|
make_rpixmap(ourras, wind); |
560 |
+ |
if (parent < 0 & sigrecv == 0) { /* notify parent */ |
561 |
+ |
kill(getppid(), SIGCONT); |
562 |
+ |
sigrecv--; |
563 |
+ |
} |
564 |
|
break; |
565 |
|
case UnmapNotify: |
566 |
|
if (!fast) |
676 |
|
case '@': /* adaptation level */ |
677 |
|
if (avgbox(cval) == -1) |
678 |
|
return(-1); |
679 |
< |
comp = com=='@' |
680 |
< |
? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure |
681 |
< |
: .5/bright(cval) ; |
679 |
> |
comp = bright(cval); |
680 |
> |
if (comp < 1e-20) { |
681 |
> |
XBell(thedisplay, 0); |
682 |
> |
return(-1); |
683 |
> |
} |
684 |
> |
if (com == '@') |
685 |
> |
comp = 106./exposure/ |
686 |
> |
pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); |
687 |
> |
else |
688 |
> |
comp = .5/comp; |
689 |
|
comp = log(comp)/.69315 - scale; |
690 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
691 |
|
if (n == 0) |
946 |
|
colrs_gambs(scanline, xmax); |
947 |
|
if (ourras->image->blue_mask & 1) |
948 |
|
for (x = 0; x < xmax; x++) |
949 |
< |
*dp++ = scanline[x][RED] << 16 | |
950 |
< |
scanline[x][GRN] << 8 | |
951 |
< |
scanline[x][BLU] ; |
949 |
> |
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
950 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
951 |
> |
(unsigned int4)scanline[x][BLU] ; |
952 |
|
else |
953 |
|
for (x = 0; x < xmax; x++) |
954 |
< |
*dp++ = scanline[x][RED] | |
955 |
< |
scanline[x][GRN] << 8 | |
956 |
< |
scanline[x][BLU] << 16 ; |
954 |
> |
*dp++ = (unsigned int4)scanline[x][RED] | |
955 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
956 |
> |
(unsigned int4)scanline[x][BLU] << 16 ; |
957 |
|
} |
958 |
|
} |
959 |
|
|
992 |
|
getmapped() /* get color-mapped data */ |
993 |
|
{ |
994 |
|
int y; |
995 |
+ |
/* make sure we can do it first */ |
996 |
+ |
if (fname == NULL) |
997 |
+ |
quiterr("cannot map colors from standard input"); |
998 |
|
/* set gamma correction */ |
999 |
|
setcolrgam(gamcor); |
1000 |
|
/* make histogram */ |
1001 |
< |
new_histo(); |
1001 |
> |
if (new_histo((long)xmax*ymax) == -1) |
1002 |
> |
quiterr("cannot initialize histogram"); |
1003 |
|
for (y = 0; y < ymax; y++) { |
1004 |
|
if (getscan(y) < 0) |
1005 |
< |
quiterr("seek error in getmapped"); |
1005 |
> |
break; |
1006 |
|
add2icon(y, scanline); |
1007 |
|
if (scale) |
1008 |
|
shiftcolrs(scanline, xmax, scale); |
1013 |
|
if (!new_clrtab(maxcolors)) |
1014 |
|
quiterr("cannot create color map"); |
1015 |
|
for (y = 0; y < ymax; y++) { |
1016 |
< |
if (getscan(y) < 0) |
991 |
< |
quiterr("seek error in getmapped"); |
1016 |
> |
getscan(y); |
1017 |
|
if (scale) |
1018 |
|
shiftcolrs(scanline, xmax, scale); |
1019 |
|
colrs_gambs(scanline, xmax); |
1056 |
|
getscan(y) |
1057 |
|
int y; |
1058 |
|
{ |
1059 |
+ |
static int trunced = -1; /* truncated file? */ |
1060 |
+ |
skipit: |
1061 |
+ |
if (trunced >= 0 && y >= trunced) { |
1062 |
+ |
bzero(scanline, xmax*sizeof(COLR)); |
1063 |
+ |
return(-1); |
1064 |
+ |
} |
1065 |
|
if (y != cury) { |
1066 |
|
if (scanpos == NULL || scanpos[y] == -1) |
1067 |
|
return(-1); |
1071 |
|
} else if (scanpos != NULL && scanpos[y] == -1) |
1072 |
|
scanpos[y] = ftell(fin); |
1073 |
|
|
1074 |
< |
if (freadcolrs(scanline, xmax, fin) < 0) |
1075 |
< |
quiterr("read error"); |
1076 |
< |
|
1074 |
> |
if (freadcolrs(scanline, xmax, fin) < 0) { |
1075 |
> |
fprintf(stderr, "%s: %s: unfinished picture\n", |
1076 |
> |
progname, fname==NULL?"<stdin>":fname); |
1077 |
> |
trunced = y; |
1078 |
> |
goto skipit; |
1079 |
> |
} |
1080 |
|
cury++; |
1081 |
|
return(0); |
1082 |
|
} |