| 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)-'@') |
| 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? */ |
| 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 |
+ |
|
| 78 |
|
VIEW ourview = STDVIEW; /* image view parameters */ |
| 79 |
|
int gotview = 0; /* got parameters from file */ |
| 80 |
|
|
| 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 sigrecv; |
| 126 |
|
|
| 127 |
+ |
int onsig() { sigrecv++; } |
| 128 |
+ |
|
| 129 |
+ |
|
| 130 |
|
main(argc, argv) |
| 131 |
|
int argc; |
| 132 |
|
char *argv[]; |
| 133 |
|
{ |
| 117 |
– |
extern char *getenv(); |
| 118 |
– |
char *gv; |
| 134 |
|
int headline(); |
| 135 |
|
int i; |
| 136 |
+ |
int pid; |
| 137 |
|
|
| 138 |
|
progname = argv[0]; |
| 123 |
– |
if ((gv = getenv("GAMMA")) != NULL) |
| 124 |
– |
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; |
| 177 |
|
else |
| 178 |
|
break; |
| 179 |
|
|
| 180 |
< |
if (i == argc-1) { |
| 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; |
| 188 |
> |
} |
| 189 |
> |
if (pid < 0) |
| 190 |
> |
quiterr("fork failed"); |
| 191 |
> |
parent++; |
| 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); |
| 201 |
< |
quiterr(errmsg); |
| 172 |
< |
} |
| 173 |
< |
} else if (i != argc) |
| 174 |
< |
goto userr; |
| 199 |
> |
if (fin == NULL) |
| 200 |
> |
quiterr("cannot open picture file"); |
| 201 |
> |
} |
| 202 |
|
/* get header */ |
| 203 |
|
getheader(fin, headline, NULL); |
| 204 |
|
/* get picture dimensions */ |
| 214 |
|
|
| 215 |
|
init(argc, argv); /* get file and open window */ |
| 216 |
|
|
| 217 |
+ |
if (parent < 0) { |
| 218 |
+ |
kill(getppid(), SIGCONT); /* signal parent if child */ |
| 219 |
+ |
sigrecv--; |
| 220 |
+ |
} |
| 221 |
|
for ( ; ; ) |
| 222 |
|
getevent(); /* main loop */ |
| 223 |
|
userr: |
| 224 |
|
fprintf(stderr, |
| 225 |
< |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n", |
| 225 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor] pic ..\n", |
| 226 |
|
progname); |
| 227 |
|
exit(1); |
| 228 |
|
} |
| 272 |
|
name += i+1; |
| 273 |
|
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
| 274 |
|
quiterr("cannot open display"); |
| 275 |
+ |
/* set gamma value */ |
| 276 |
+ |
if (gamstr == NULL) /* get it from the X server */ |
| 277 |
+ |
gamstr = XGetDefault(thedisplay, "radiance", "gamma"); |
| 278 |
+ |
if (gamstr == NULL) /* get it from the environment */ |
| 279 |
+ |
gamstr = getenv("DISPLAY_GAMMA"); |
| 280 |
+ |
if (gamstr != NULL) |
| 281 |
+ |
gamcor = atof(gamstr); |
| 282 |
|
/* get best visual for default screen */ |
| 283 |
|
getbestvis(); |
| 284 |
|
/* store image */ |
| 320 |
|
quiterr("cannot create window"); |
| 321 |
|
width = xmax; |
| 322 |
|
height = ymax; |
| 323 |
< |
xgcv.foreground = ourblack; |
| 286 |
< |
xgcv.background = ourwhite; |
| 323 |
> |
/* prepare graphics drawing context */ |
| 324 |
|
if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) |
| 325 |
|
quiterr("cannot get font"); |
| 326 |
+ |
xgcv.foreground = ourblack; |
| 327 |
+ |
xgcv.background = ourwhite; |
| 328 |
|
ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| |
| 329 |
|
GCFont, &xgcv); |
| 330 |
|
xgcv.function = GXinvert; |
| 360 |
|
quiterr(err) /* print message and exit */ |
| 361 |
|
char *err; |
| 362 |
|
{ |
| 363 |
< |
if (err != NULL) { |
| 364 |
< |
fprintf(stderr, "%s: %s\n", progname, err); |
| 365 |
< |
exit(1); |
| 363 |
> |
register int es; |
| 364 |
> |
int cs; |
| 365 |
> |
|
| 366 |
> |
if (es = err != NULL) |
| 367 |
> |
fprintf(stderr, "%s: %s: %s\n", progname, |
| 368 |
> |
fname==NULL?"<stdin>":fname, err); |
| 369 |
> |
if (thedisplay != NULL) |
| 370 |
> |
XCloseDisplay(thedisplay); |
| 371 |
> |
if (parent < 0 & sigrecv == 0) |
| 372 |
> |
kill(getppid(), SIGCONT); |
| 373 |
> |
while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ |
| 374 |
> |
if (es == 0) |
| 375 |
> |
es = cs>>8 & 0xff; |
| 376 |
> |
parent--; |
| 377 |
|
} |
| 378 |
< |
exit(0); |
| 378 |
> |
exit(es); |
| 379 |
|
} |
| 380 |
|
|
| 381 |
|
|
| 516 |
|
goto fail; |
| 517 |
|
getmono(); |
| 518 |
|
} else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { |
| 519 |
< |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
| 519 |
> |
ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); |
| 520 |
|
if (ourdata == NULL) |
| 521 |
|
goto fail; |
| 522 |
< |
ourras = make_raster(thedisplay, &ourvis, 32, |
| 522 |
> |
ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, |
| 523 |
|
ourdata, xmax, ymax, 32); |
| 524 |
|
if (ourras == NULL) |
| 525 |
|
goto fail; |
| 579 |
|
else |
| 580 |
|
getbox(&xev.xbutton); |
| 581 |
|
break; |
| 582 |
< |
case ClientMessage: |
| 582 |
> |
case ClientMessage: |
| 583 |
|
if ((xev.xclient.message_type == wmProtocolsAtom) && |
| 584 |
|
(xev.xclient.data.l[0] == closedownAtom)) |
| 585 |
|
quiterr(NULL); |
| 677 |
|
case '@': /* adaptation level */ |
| 678 |
|
if (avgbox(cval) == -1) |
| 679 |
|
return(-1); |
| 680 |
< |
comp = com=='@' |
| 681 |
< |
? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure |
| 682 |
< |
: .5/bright(cval) ; |
| 680 |
> |
comp = bright(cval); |
| 681 |
> |
if (comp < 1e-20) { |
| 682 |
> |
XBell(thedisplay, 0); |
| 683 |
> |
return(-1); |
| 684 |
> |
} |
| 685 |
> |
if (com == '@') |
| 686 |
> |
comp = 106./exposure/ |
| 687 |
> |
pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); |
| 688 |
> |
else |
| 689 |
> |
comp = .5/comp; |
| 690 |
|
comp = log(comp)/.69315 - scale; |
| 691 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
| 692 |
|
if (n == 0) |
| 710 |
|
make_rpixmap(ourras, wind); |
| 711 |
|
redraw(0, 0, width, height); |
| 712 |
|
return(0); |
| 713 |
+ |
case 'f': /* turn on fast redraw */ |
| 714 |
+ |
fast = 1; |
| 715 |
+ |
make_rpixmap(ourras, wind); |
| 716 |
+ |
return(0); |
| 717 |
+ |
case 'F': /* turn off fast redraw */ |
| 718 |
+ |
fast = 0; |
| 719 |
+ |
free_rpixmap(ourras); |
| 720 |
+ |
return(0); |
| 721 |
|
case '0': /* recenter origin */ |
| 722 |
|
if (xoff == 0 & yoff == 0) |
| 723 |
|
return(0); |
| 933 |
|
getfull() /* get full (24-bit) data */ |
| 934 |
|
{ |
| 935 |
|
int y; |
| 936 |
< |
register unsigned long *dp; |
| 936 |
> |
register unsigned int4 *dp; |
| 937 |
|
register int x; |
| 938 |
|
/* set gamma correction */ |
| 939 |
|
setcolrgam(gamcor); |
| 940 |
|
/* read and convert file */ |
| 941 |
< |
dp = (unsigned long *)ourdata; |
| 941 |
> |
dp = (unsigned int4 *)ourdata; |
| 942 |
|
for (y = 0; y < ymax; y++) { |
| 943 |
|
getscan(y); |
| 944 |
|
add2icon(y, scanline); |
| 947 |
|
colrs_gambs(scanline, xmax); |
| 948 |
|
if (ourras->image->blue_mask & 1) |
| 949 |
|
for (x = 0; x < xmax; x++) |
| 950 |
< |
*dp++ = scanline[x][RED] << 16 | |
| 951 |
< |
scanline[x][GRN] << 8 | |
| 952 |
< |
scanline[x][BLU] ; |
| 950 |
> |
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
| 951 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
| 952 |
> |
(unsigned int4)scanline[x][BLU] ; |
| 953 |
|
else |
| 954 |
|
for (x = 0; x < xmax; x++) |
| 955 |
< |
*dp++ = scanline[x][RED] | |
| 956 |
< |
scanline[x][GRN] << 8 | |
| 957 |
< |
scanline[x][BLU] << 16 ; |
| 955 |
> |
*dp++ = (unsigned int4)scanline[x][RED] | |
| 956 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
| 957 |
> |
(unsigned int4)scanline[x][BLU] << 16 ; |
| 958 |
|
} |
| 959 |
|
} |
| 960 |
|
|
| 993 |
|
getmapped() /* get color-mapped data */ |
| 994 |
|
{ |
| 995 |
|
int y; |
| 996 |
+ |
/* make sure we can do it first */ |
| 997 |
+ |
if (fname == NULL) |
| 998 |
+ |
quiterr("cannot map colors from standard input"); |
| 999 |
|
/* set gamma correction */ |
| 1000 |
|
setcolrgam(gamcor); |
| 1001 |
|
/* make histogram */ |
| 1002 |
< |
new_histo(); |
| 1002 |
> |
if (new_histo((long)xmax*ymax) == -1) |
| 1003 |
> |
quiterr("cannot initialize histogram"); |
| 1004 |
|
for (y = 0; y < ymax; y++) { |
| 1005 |
|
if (getscan(y) < 0) |
| 1006 |
< |
quiterr("seek error in getmapped"); |
| 1006 |
> |
break; |
| 1007 |
|
add2icon(y, scanline); |
| 1008 |
|
if (scale) |
| 1009 |
|
shiftcolrs(scanline, xmax, scale); |
| 1014 |
|
if (!new_clrtab(maxcolors)) |
| 1015 |
|
quiterr("cannot create color map"); |
| 1016 |
|
for (y = 0; y < ymax; y++) { |
| 1017 |
< |
if (getscan(y) < 0) |
| 949 |
< |
quiterr("seek error in getmapped"); |
| 1017 |
> |
getscan(y); |
| 1018 |
|
if (scale) |
| 1019 |
|
shiftcolrs(scanline, xmax, scale); |
| 1020 |
|
colrs_gambs(scanline, xmax); |
| 1057 |
|
getscan(y) |
| 1058 |
|
int y; |
| 1059 |
|
{ |
| 1060 |
+ |
static int trunced = -1; /* truncated file? */ |
| 1061 |
+ |
skipit: |
| 1062 |
+ |
if (trunced >= 0 && y >= trunced) { |
| 1063 |
+ |
bzero(scanline, xmax*sizeof(COLR)); |
| 1064 |
+ |
return(-1); |
| 1065 |
+ |
} |
| 1066 |
|
if (y != cury) { |
| 1067 |
|
if (scanpos == NULL || scanpos[y] == -1) |
| 1068 |
|
return(-1); |
| 1072 |
|
} else if (scanpos != NULL && scanpos[y] == -1) |
| 1073 |
|
scanpos[y] = ftell(fin); |
| 1074 |
|
|
| 1075 |
< |
if (freadcolrs(scanline, xmax, fin) < 0) |
| 1076 |
< |
quiterr("read error"); |
| 1077 |
< |
|
| 1075 |
> |
if (freadcolrs(scanline, xmax, fin) < 0) { |
| 1076 |
> |
fprintf(stderr, "%s: %s: unfinished picture\n", |
| 1077 |
> |
progname, fname==NULL?"<stdin>":fname); |
| 1078 |
> |
trunced = y; |
| 1079 |
> |
goto skipit; |
| 1080 |
> |
} |
| 1081 |
|
cury++; |
| 1082 |
|
return(0); |
| 1083 |
|
} |