| 27 |
|
#include <X11/Xutil.h> |
| 28 |
|
#include <X11/Xatom.h> |
| 29 |
|
|
| 30 |
< |
#include "color.h" |
| 30 |
> |
#undef NOPROTO |
| 31 |
> |
#define NOPROTO 1 |
| 32 |
> |
#include "tonemap.h" |
| 33 |
|
#include "view.h" |
| 34 |
|
#include "x11raster.h" |
| 35 |
|
#include "random.h" |
| 36 |
|
#include "resolu.h" |
| 37 |
|
|
| 36 |
– |
#ifdef __alpha |
| 37 |
– |
#define int4 int |
| 38 |
– |
#endif |
| 38 |
|
#ifndef int4 |
| 39 |
< |
#define int4 long |
| 39 |
> |
#define int4 int /* most int's are 32-bit */ |
| 40 |
|
#endif |
| 41 |
|
|
| 42 |
|
#define FONTNAME "8x13" /* text font we'll use */ |
| 78 |
|
char *tout = "od"; /* output of 't' command */ |
| 79 |
|
int tinterv = 0; /* interval between mouse reports */ |
| 80 |
|
|
| 81 |
+ |
int tmflags = -1; /* tone mapping flags (-1 for none) */ |
| 82 |
+ |
|
| 83 |
|
VIEW ourview = STDVIEW; /* image view parameters */ |
| 84 |
|
int gotview = 0; /* got parameters from file */ |
| 85 |
|
|
| 86 |
|
COLR *scanline; /* scan line buffer */ |
| 87 |
+ |
TMbright *lscan; /* encoded luminance scanline */ |
| 88 |
+ |
BYTE *cscan; /* encoded chroma scanline */ |
| 89 |
+ |
BYTE *pscan; /* compute pixel scanline */ |
| 90 |
|
|
| 91 |
|
RESOLU inpres; /* input resolution and ordering */ |
| 92 |
|
int xmax, ymax; /* picture dimensions */ |
| 177 |
|
tinterv = atoi(argv[++i]); |
| 178 |
|
break; |
| 179 |
|
case 'e': /* exposure comp. */ |
| 180 |
< |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
| 180 |
> |
i++; |
| 181 |
> |
if (argv[i][0] == 'a') { |
| 182 |
> |
tmflags = TM_F_CAMERA; |
| 183 |
> |
break; |
| 184 |
> |
} |
| 185 |
> |
if (argv[i][0] == 'h') { |
| 186 |
> |
tmflags = TM_F_HUMAN; |
| 187 |
> |
break; |
| 188 |
> |
} |
| 189 |
> |
if (argv[i][0] != '+' && argv[i][0] != '-') |
| 190 |
|
goto userr; |
| 191 |
< |
scale = atoi(argv[++i]); |
| 191 |
> |
scale = atoi(argv[i]); |
| 192 |
|
break; |
| 193 |
|
case 'g': /* gamma comp. */ |
| 194 |
|
if (argv[i][2] == 'e') |
| 245 |
|
getevent(); /* main loop */ |
| 246 |
|
userr: |
| 247 |
|
fprintf(stderr, |
| 248 |
< |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor][-s][-ospec][-t intvl] pic ..\n", |
| 248 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n", |
| 249 |
|
progname); |
| 250 |
|
exit(1); |
| 251 |
|
} |
| 258 |
|
|
| 259 |
|
if (isexpos(s)) |
| 260 |
|
exposure *= exposval(s); |
| 261 |
< |
else if (isformat(s)) { |
| 249 |
< |
formatval(fmt, s); |
| 261 |
> |
else if (formatval(fmt, s)) |
| 262 |
|
wrongformat = strcmp(fmt, COLRFMT); |
| 263 |
< |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
| 263 |
> |
else if (isview(s) && sscanview(&ourview, s) > 0) |
| 264 |
|
gotview++; |
| 265 |
|
} |
| 266 |
|
|
| 556 |
|
xmax, ymax, 8); |
| 557 |
|
if (ourras == NULL) |
| 558 |
|
goto fail; |
| 559 |
< |
if (greyscale | ourvis.class == StaticGray) |
| 559 |
> |
if (greyscale) |
| 560 |
|
getgrey(); |
| 561 |
|
else |
| 562 |
|
getmapped(); |
| 733 |
|
return(0); |
| 734 |
|
case 't': /* trace */ |
| 735 |
|
return(traceray(ekey->x, ekey->y)); |
| 736 |
+ |
case 'a': /* auto exposure */ |
| 737 |
+ |
if (tmflags == TM_F_CAMERA) |
| 738 |
+ |
return(0); |
| 739 |
+ |
tmflags = TM_F_CAMERA; |
| 740 |
+ |
strcpy(buf, "auto exposure..."); |
| 741 |
+ |
goto remap; |
| 742 |
+ |
case 'h': /* human response */ |
| 743 |
+ |
if (tmflags == TM_F_HUMAN) |
| 744 |
+ |
return(0); |
| 745 |
+ |
tmflags = TM_F_HUMAN; |
| 746 |
+ |
strcpy(buf, "human exposure..."); |
| 747 |
+ |
goto remap; |
| 748 |
|
case '=': /* adjust exposure */ |
| 749 |
|
case '@': /* adaptation level */ |
| 750 |
|
if (avgbox(cval) == -1) |
| 761 |
|
comp = .5/comp; |
| 762 |
|
comp = log(comp)/.69315 - scale; |
| 763 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
| 764 |
< |
if (n == 0) |
| 765 |
< |
return(0); |
| 766 |
< |
scale_rcolors(ourras, pow(2.0, (double)n)); |
| 764 |
> |
if (tmflags != -1) |
| 765 |
> |
tmflags = -1; /* turn off tone mapping */ |
| 766 |
> |
else { |
| 767 |
> |
if (n == 0) /* else check if any change */ |
| 768 |
> |
return(0); |
| 769 |
> |
scale_rcolors(ourras, pow(2.0, (double)n)); |
| 770 |
> |
} |
| 771 |
|
scale += n; |
| 772 |
|
sprintf(buf, "%+d", scale); |
| 773 |
+ |
remap: |
| 774 |
|
XDrawImageString(thedisplay, wind, ourgc, |
| 775 |
|
box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); |
| 776 |
|
XFlush(thedisplay); |
| 942 |
|
} |
| 943 |
|
|
| 944 |
|
|
| 945 |
+ |
make_tonemap() /* initialize tone mapping */ |
| 946 |
+ |
{ |
| 947 |
+ |
int flags, y; |
| 948 |
+ |
|
| 949 |
+ |
if (tmflags != -1 && fname == NULL) { |
| 950 |
+ |
fprintf(stderr, "%s: cannot adjust tone of standard input\n", |
| 951 |
+ |
progname); |
| 952 |
+ |
tmflags = -1; |
| 953 |
+ |
} |
| 954 |
+ |
if (tmflags == -1) { |
| 955 |
+ |
setcolrcor(pow, 1.0/gamcor); |
| 956 |
+ |
return; |
| 957 |
+ |
} |
| 958 |
+ |
if (tmTop != NULL) /* already initialized? */ |
| 959 |
+ |
return; |
| 960 |
+ |
flags = tmflags; |
| 961 |
+ |
if (greyscale) flags |= TM_F_BW; |
| 962 |
+ |
/* initialize tm library */ |
| 963 |
+ |
if (tmInit(flags, stdprims, gamcor) == NULL) |
| 964 |
+ |
goto memerr; |
| 965 |
+ |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
| 966 |
+ |
goto tmerr; |
| 967 |
+ |
/* allocate encoding buffers */ |
| 968 |
+ |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
| 969 |
+ |
goto memerr; |
| 970 |
+ |
if (greyscale) { |
| 971 |
+ |
cscan = TM_NOCHROM; |
| 972 |
+ |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
| 973 |
+ |
goto memerr; |
| 974 |
+ |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL) |
| 975 |
+ |
goto memerr; |
| 976 |
+ |
/* compute picture histogram */ |
| 977 |
+ |
for (y = 0; y < ymax; y++) { |
| 978 |
+ |
getscan(y); |
| 979 |
+ |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
| 980 |
+ |
goto tmerr; |
| 981 |
+ |
if (tmAddHisto(lscan, xmax, 1)) |
| 982 |
+ |
goto tmerr; |
| 983 |
+ |
} |
| 984 |
+ |
/* compute tone mapping */ |
| 985 |
+ |
if (tmComputeMapping(gamcor, 0., 0.)) |
| 986 |
+ |
goto tmerr; |
| 987 |
+ |
return; |
| 988 |
+ |
memerr: |
| 989 |
+ |
quiterr("out of memory in make_tonemap"); |
| 990 |
+ |
tmerr: |
| 991 |
+ |
quiterr("tone mapping error"); |
| 992 |
+ |
} |
| 993 |
+ |
|
| 994 |
+ |
|
| 995 |
+ |
tmap_colrs(scn, len) /* apply tone mapping to scanline */ |
| 996 |
+ |
register COLR *scn; |
| 997 |
+ |
int len; |
| 998 |
+ |
{ |
| 999 |
+ |
register BYTE *ps; |
| 1000 |
+ |
|
| 1001 |
+ |
if (tmflags == -1) { |
| 1002 |
+ |
if (scale) |
| 1003 |
+ |
shiftcolrs(scn, len, scale); |
| 1004 |
+ |
colrs_gambs(scn, len); |
| 1005 |
+ |
return; |
| 1006 |
+ |
} |
| 1007 |
+ |
if (len > xmax) |
| 1008 |
+ |
quiterr("code error 1 in tmap_colrs"); |
| 1009 |
+ |
if (tmCvColrs(lscan, cscan, scn, len)) |
| 1010 |
+ |
goto tmerr; |
| 1011 |
+ |
if (tmMapPixels(pscan, lscan, cscan, len)) |
| 1012 |
+ |
goto tmerr; |
| 1013 |
+ |
ps = pscan; |
| 1014 |
+ |
if (greyscale) |
| 1015 |
+ |
while (len--) { |
| 1016 |
+ |
scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; |
| 1017 |
+ |
scn[0][EXP] = COLXS; |
| 1018 |
+ |
scn++; |
| 1019 |
+ |
} |
| 1020 |
+ |
else |
| 1021 |
+ |
while (len--) { |
| 1022 |
+ |
scn[0][RED] = *ps++; |
| 1023 |
+ |
scn[0][GRN] = *ps++; |
| 1024 |
+ |
scn[0][BLU] = *ps++; |
| 1025 |
+ |
scn[0][EXP] = COLXS; |
| 1026 |
+ |
scn++; |
| 1027 |
+ |
} |
| 1028 |
+ |
return; |
| 1029 |
+ |
tmerr: |
| 1030 |
+ |
quiterr("tone mapping error"); |
| 1031 |
+ |
} |
| 1032 |
+ |
|
| 1033 |
+ |
|
| 1034 |
|
getmono() /* get monochrome data */ |
| 1035 |
|
{ |
| 1036 |
|
register unsigned char *dp; |
| 1120 |
|
int y; |
| 1121 |
|
register unsigned int4 *dp; |
| 1122 |
|
register int x; |
| 1123 |
< |
/* set gamma correction */ |
| 1124 |
< |
setcolrgam(gamcor); |
| 1123 |
> |
/* initialize tone mapping */ |
| 1124 |
> |
make_tonemap(); |
| 1125 |
|
/* read and convert file */ |
| 1126 |
|
dp = (unsigned int4 *)ourdata; |
| 1127 |
|
for (y = 0; y < ymax; y++) { |
| 1128 |
|
getscan(y); |
| 1129 |
|
add2icon(y, scanline); |
| 1130 |
< |
if (scale) |
| 1013 |
< |
shiftcolrs(scanline, xmax, scale); |
| 1014 |
< |
colrs_gambs(scanline, xmax); |
| 1130 |
> |
tmap_colrs(scanline, xmax); |
| 1131 |
|
if (ourras->image->blue_mask & 1) |
| 1132 |
|
for (x = 0; x < xmax; x++) |
| 1133 |
|
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
| 1147 |
|
int y; |
| 1148 |
|
register unsigned char *dp; |
| 1149 |
|
register int x; |
| 1150 |
< |
/* set gamma correction */ |
| 1151 |
< |
setcolrgam(gamcor); |
| 1150 |
> |
/* initialize tone mapping */ |
| 1151 |
> |
make_tonemap(); |
| 1152 |
|
/* read and convert file */ |
| 1153 |
|
dp = ourdata; |
| 1154 |
|
for (y = 0; y < ymax; y++) { |
| 1155 |
|
getscan(y); |
| 1156 |
|
add2icon(y, scanline); |
| 1157 |
< |
if (scale) |
| 1042 |
< |
shiftcolrs(scanline, xmax, scale); |
| 1043 |
< |
for (x = 0; x < xmax; x++) |
| 1044 |
< |
scanline[x][GRN] = normbright(scanline[x]); |
| 1045 |
< |
colrs_gambs(scanline, xmax); |
| 1157 |
> |
tmap_colrs(scanline, xmax); |
| 1158 |
|
if (maxcolors < 256) |
| 1159 |
|
for (x = 0; x < xmax; x++) |
| 1160 |
< |
*dp++ = ((long)scanline[x][GRN] * |
| 1160 |
> |
*dp++ = ((int4)scanline[x][GRN] * |
| 1161 |
|
maxcolors + maxcolors/2) >> 8; |
| 1162 |
|
else |
| 1163 |
|
for (x = 0; x < xmax; x++) |
| 1165 |
|
} |
| 1166 |
|
for (x = 0; x < maxcolors; x++) |
| 1167 |
|
clrtab[x][RED] = clrtab[x][GRN] = |
| 1168 |
< |
clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; |
| 1168 |
> |
clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; |
| 1169 |
|
} |
| 1170 |
|
|
| 1171 |
|
|
| 1175 |
|
/* make sure we can do it first */ |
| 1176 |
|
if (fname == NULL) |
| 1177 |
|
quiterr("cannot map colors from standard input"); |
| 1178 |
< |
/* set gamma correction */ |
| 1179 |
< |
setcolrgam(gamcor); |
| 1178 |
> |
/* initialize tone mapping */ |
| 1179 |
> |
make_tonemap(); |
| 1180 |
|
/* make histogram */ |
| 1181 |
< |
if (new_histo((long)xmax*ymax) == -1) |
| 1181 |
> |
if (new_histo((int4)xmax*ymax) == -1) |
| 1182 |
|
quiterr("cannot initialize histogram"); |
| 1183 |
|
for (y = 0; y < ymax; y++) { |
| 1184 |
|
if (getscan(y) < 0) |
| 1185 |
|
break; |
| 1186 |
|
add2icon(y, scanline); |
| 1187 |
< |
if (scale) |
| 1076 |
< |
shiftcolrs(scanline, xmax, scale); |
| 1077 |
< |
colrs_gambs(scanline, xmax); |
| 1187 |
> |
tmap_colrs(scanline, xmax); |
| 1188 |
|
cnt_colrs(scanline, xmax); |
| 1189 |
|
} |
| 1190 |
|
/* map pixels */ |
| 1192 |
|
quiterr("cannot create color map"); |
| 1193 |
|
for (y = 0; y < ymax; y++) { |
| 1194 |
|
getscan(y); |
| 1195 |
< |
if (scale) |
| 1086 |
< |
shiftcolrs(scanline, xmax, scale); |
| 1087 |
< |
colrs_gambs(scanline, xmax); |
| 1195 |
> |
tmap_colrs(scanline, xmax); |
| 1196 |
|
if (dither) |
| 1197 |
|
dith_colrs(ourdata+y*xmax, scanline, xmax); |
| 1198 |
|
else |