| 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] == '-') |
| 147 |
|
greyscale = !greyscale; |
| 148 |
|
break; |
| 149 |
|
case 'm': |
| 150 |
+ |
greyscale = 1; |
| 151 |
|
maxcolors = 2; |
| 152 |
|
break; |
| 153 |
|
case 'd': |
| 168 |
|
if (argv[i][2] == 'e') |
| 169 |
|
geometry = argv[++i]; |
| 170 |
|
else |
| 171 |
< |
gamcor = atof(argv[++i]); |
| 171 |
> |
gamstr = argv[++i]; |
| 172 |
|
break; |
| 173 |
|
default: |
| 174 |
|
goto userr; |
| 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 */ |
| 219 |
|
getevent(); /* main loop */ |
| 220 |
|
userr: |
| 221 |
|
fprintf(stderr, |
| 222 |
< |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic\n", |
| 222 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor] pic ..\n", |
| 223 |
|
progname); |
| 224 |
|
exit(1); |
| 225 |
|
} |
| 269 |
|
name += i+1; |
| 270 |
|
if ((thedisplay = XOpenDisplay(dispname)) == NULL) |
| 271 |
|
quiterr("cannot open display"); |
| 272 |
+ |
/* set gamma value */ |
| 273 |
+ |
if (gamstr == NULL) /* get it from the X server */ |
| 274 |
+ |
gamstr = XGetDefault(thedisplay, "radiance", "gamma"); |
| 275 |
+ |
if (gamstr == NULL) /* get it from the environment */ |
| 276 |
+ |
gamstr = getenv("DISPLAY_GAMMA"); |
| 277 |
+ |
if (gamstr != NULL) |
| 278 |
+ |
gamcor = atof(gamstr); |
| 279 |
|
/* get best visual for default screen */ |
| 280 |
|
getbestvis(); |
| 281 |
|
/* store image */ |
| 302 |
|
} |
| 303 |
|
} |
| 304 |
|
/* open window */ |
| 305 |
+ |
i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; |
| 306 |
|
ourwinattr.border_pixel = ourwhite; |
| 307 |
|
ourwinattr.background_pixel = ourblack; |
| 308 |
< |
ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, |
| 309 |
< |
ourvis.visual, AllocNone); |
| 308 |
> |
if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) { |
| 309 |
> |
ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual); |
| 310 |
> |
i |= CWColormap; |
| 311 |
> |
} |
| 312 |
|
ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| |
| 313 |
|
ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; |
| 314 |
|
ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); |
| 315 |
|
wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, |
| 316 |
|
xszhints.width, xszhints.height, BORWIDTH, |
| 317 |
< |
ourvis.depth, InputOutput, ourvis.visual, CWEventMask| |
| 318 |
< |
CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); |
| 317 |
> |
ourvis.depth, InputOutput, ourvis.visual, |
| 318 |
> |
i, &ourwinattr); |
| 319 |
|
if (wind == 0) |
| 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; |
| 353 |
|
XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); |
| 354 |
|
|
| 355 |
|
XMapWindow(thedisplay, wind); |
| 317 |
– |
return; |
| 356 |
|
} /* end of init */ |
| 357 |
|
|
| 358 |
|
|
| 359 |
|
quiterr(err) /* print message and exit */ |
| 360 |
|
char *err; |
| 361 |
|
{ |
| 362 |
< |
if (err != NULL) { |
| 363 |
< |
fprintf(stderr, "%s: %s\n", progname, err); |
| 364 |
< |
exit(1); |
| 362 |
> |
register int es; |
| 363 |
> |
int cs; |
| 364 |
> |
|
| 365 |
> |
if (es = err != NULL) |
| 366 |
> |
fprintf(stderr, "%s: %s: %s\n", progname, |
| 367 |
> |
fname==NULL?"<stdin>":fname, err); |
| 368 |
> |
if (thedisplay != NULL) |
| 369 |
> |
XCloseDisplay(thedisplay); |
| 370 |
> |
if (parent < 0 & sigrecv == 0) |
| 371 |
> |
kill(getppid(), SIGCONT); |
| 372 |
> |
while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ |
| 373 |
> |
if (es == 0) |
| 374 |
> |
es = cs>>8 & 0xff; |
| 375 |
> |
parent--; |
| 376 |
|
} |
| 377 |
< |
exit(0); |
| 377 |
> |
exit(es); |
| 378 |
|
} |
| 379 |
|
|
| 380 |
|
|
| 515 |
|
goto fail; |
| 516 |
|
getmono(); |
| 517 |
|
} else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { |
| 518 |
< |
ourdata = (unsigned char *)malloc(4*xmax*ymax); |
| 518 |
> |
ourdata = (unsigned char *)malloc(sizeof(int4)*xmax*ymax); |
| 519 |
|
if (ourdata == NULL) |
| 520 |
|
goto fail; |
| 521 |
< |
ourras = make_raster(thedisplay, &ourvis, 32, |
| 521 |
> |
ourras = make_raster(thedisplay, &ourvis, sizeof(int4)*8, |
| 522 |
|
ourdata, xmax, ymax, 32); |
| 523 |
|
if (ourras == NULL) |
| 524 |
|
goto fail; |
| 561 |
|
map_rcolors(ourras, wind); |
| 562 |
|
if (fast) |
| 563 |
|
make_rpixmap(ourras, wind); |
| 564 |
+ |
if (parent < 0 & sigrecv == 0) { /* notify parent */ |
| 565 |
+ |
kill(getppid(), SIGCONT); |
| 566 |
+ |
sigrecv--; |
| 567 |
+ |
} |
| 568 |
|
break; |
| 569 |
|
case UnmapNotify: |
| 570 |
|
if (!fast) |
| 582 |
|
else |
| 583 |
|
getbox(&xev.xbutton); |
| 584 |
|
break; |
| 585 |
< |
case ClientMessage: |
| 585 |
> |
case ClientMessage: |
| 586 |
|
if ((xev.xclient.message_type == wmProtocolsAtom) && |
| 587 |
|
(xev.xclient.data.l[0] == closedownAtom)) |
| 588 |
|
quiterr(NULL); |
| 677 |
|
case 't': /* trace */ |
| 678 |
|
return(traceray(ekey->x, ekey->y)); |
| 679 |
|
case '=': /* adjust exposure */ |
| 680 |
+ |
case '@': /* adaptation level */ |
| 681 |
|
if (avgbox(cval) == -1) |
| 682 |
|
return(-1); |
| 683 |
< |
n = log(.5/bright(cval))/.69315 - scale; /* truncate */ |
| 683 |
> |
comp = bright(cval); |
| 684 |
> |
if (comp < 1e-20) { |
| 685 |
> |
XBell(thedisplay, 0); |
| 686 |
> |
return(-1); |
| 687 |
> |
} |
| 688 |
> |
if (com == '@') |
| 689 |
> |
comp = 106./exposure/ |
| 690 |
> |
pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); |
| 691 |
> |
else |
| 692 |
> |
comp = .5/comp; |
| 693 |
> |
comp = log(comp)/.69315 - scale; |
| 694 |
> |
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
| 695 |
|
if (n == 0) |
| 696 |
|
return(0); |
| 697 |
|
scale_rcolors(ourras, pow(2.0, (double)n)); |
| 713 |
|
make_rpixmap(ourras, wind); |
| 714 |
|
redraw(0, 0, width, height); |
| 715 |
|
return(0); |
| 716 |
+ |
case 'f': /* turn on fast redraw */ |
| 717 |
+ |
fast = 1; |
| 718 |
+ |
make_rpixmap(ourras, wind); |
| 719 |
+ |
return(0); |
| 720 |
+ |
case 'F': /* turn off fast redraw */ |
| 721 |
+ |
fast = 0; |
| 722 |
+ |
free_rpixmap(ourras); |
| 723 |
+ |
return(0); |
| 724 |
|
case '0': /* recenter origin */ |
| 725 |
|
if (xoff == 0 & yoff == 0) |
| 726 |
|
return(0); |
| 936 |
|
getfull() /* get full (24-bit) data */ |
| 937 |
|
{ |
| 938 |
|
int y; |
| 939 |
< |
register unsigned long *dp; |
| 939 |
> |
register unsigned int4 *dp; |
| 940 |
|
register int x; |
| 941 |
|
/* set gamma correction */ |
| 942 |
|
setcolrgam(gamcor); |
| 943 |
|
/* read and convert file */ |
| 944 |
< |
dp = (unsigned long *)ourdata; |
| 944 |
> |
dp = (unsigned int4 *)ourdata; |
| 945 |
|
for (y = 0; y < ymax; y++) { |
| 946 |
|
getscan(y); |
| 947 |
|
add2icon(y, scanline); |
| 950 |
|
colrs_gambs(scanline, xmax); |
| 951 |
|
if (ourras->image->blue_mask & 1) |
| 952 |
|
for (x = 0; x < xmax; x++) |
| 953 |
< |
*dp++ = scanline[x][RED] << 16 | |
| 954 |
< |
scanline[x][GRN] << 8 | |
| 955 |
< |
scanline[x][BLU] ; |
| 953 |
> |
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
| 954 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
| 955 |
> |
(unsigned int4)scanline[x][BLU] ; |
| 956 |
|
else |
| 957 |
|
for (x = 0; x < xmax; x++) |
| 958 |
< |
*dp++ = scanline[x][RED] | |
| 959 |
< |
scanline[x][GRN] << 8 | |
| 960 |
< |
scanline[x][BLU] << 16 ; |
| 958 |
> |
*dp++ = (unsigned int4)scanline[x][RED] | |
| 959 |
> |
(unsigned int4)scanline[x][GRN] << 8 | |
| 960 |
> |
(unsigned int4)scanline[x][BLU] << 16 ; |
| 961 |
|
} |
| 962 |
|
} |
| 963 |
|
|
| 996 |
|
getmapped() /* get color-mapped data */ |
| 997 |
|
{ |
| 998 |
|
int y; |
| 999 |
+ |
/* make sure we can do it first */ |
| 1000 |
+ |
if (fname == NULL) |
| 1001 |
+ |
quiterr("cannot map colors from standard input"); |
| 1002 |
|
/* set gamma correction */ |
| 1003 |
|
setcolrgam(gamcor); |
| 1004 |
|
/* make histogram */ |
| 1005 |
< |
new_histo(); |
| 1005 |
> |
if (new_histo((long)xmax*ymax) == -1) |
| 1006 |
> |
quiterr("cannot initialize histogram"); |
| 1007 |
|
for (y = 0; y < ymax; y++) { |
| 1008 |
|
if (getscan(y) < 0) |
| 1009 |
< |
quiterr("seek error in getmapped"); |
| 1009 |
> |
break; |
| 1010 |
|
add2icon(y, scanline); |
| 1011 |
|
if (scale) |
| 1012 |
|
shiftcolrs(scanline, xmax, scale); |
| 1017 |
|
if (!new_clrtab(maxcolors)) |
| 1018 |
|
quiterr("cannot create color map"); |
| 1019 |
|
for (y = 0; y < ymax; y++) { |
| 1020 |
< |
if (getscan(y) < 0) |
| 944 |
< |
quiterr("seek error in getmapped"); |
| 1020 |
> |
getscan(y); |
| 1021 |
|
if (scale) |
| 1022 |
|
shiftcolrs(scanline, xmax, scale); |
| 1023 |
|
colrs_gambs(scanline, xmax); |
| 1060 |
|
getscan(y) |
| 1061 |
|
int y; |
| 1062 |
|
{ |
| 1063 |
+ |
static int trunced = -1; /* truncated file? */ |
| 1064 |
+ |
skipit: |
| 1065 |
+ |
if (trunced >= 0 && y >= trunced) { |
| 1066 |
+ |
bzero(scanline, xmax*sizeof(COLR)); |
| 1067 |
+ |
return(-1); |
| 1068 |
+ |
} |
| 1069 |
|
if (y != cury) { |
| 1070 |
|
if (scanpos == NULL || scanpos[y] == -1) |
| 1071 |
|
return(-1); |
| 1075 |
|
} else if (scanpos != NULL && scanpos[y] == -1) |
| 1076 |
|
scanpos[y] = ftell(fin); |
| 1077 |
|
|
| 1078 |
< |
if (freadcolrs(scanline, xmax, fin) < 0) |
| 1079 |
< |
quiterr("read error"); |
| 1080 |
< |
|
| 1078 |
> |
if (freadcolrs(scanline, xmax, fin) < 0) { |
| 1079 |
> |
fprintf(stderr, "%s: %s: unfinished picture\n", |
| 1080 |
> |
progname, fname==NULL?"<stdin>":fname); |
| 1081 |
> |
trunced = y; |
| 1082 |
> |
goto skipit; |
| 1083 |
> |
} |
| 1084 |
|
cury++; |
| 1085 |
|
return(0); |
| 1086 |
|
} |