27 |
|
#include <X11/Xutil.h> |
28 |
|
#include <X11/Xatom.h> |
29 |
|
|
30 |
< |
#include "color.h" |
30 |
> |
#include "tonemap.h" |
31 |
|
#include "view.h" |
32 |
|
#include "x11raster.h" |
33 |
|
#include "random.h" |
34 |
|
#include "resolu.h" |
35 |
|
|
36 |
– |
#ifdef __alpha |
37 |
– |
#define int4 int |
38 |
– |
#endif |
36 |
|
#ifndef int4 |
37 |
< |
#define int4 long |
37 |
> |
#define int4 int /* most int's are 32-bit */ |
38 |
|
#endif |
39 |
|
|
40 |
|
#define FONTNAME "8x13" /* text font we'll use */ |
76 |
|
char *tout = "od"; /* output of 't' command */ |
77 |
|
int tinterv = 0; /* interval between mouse reports */ |
78 |
|
|
79 |
+ |
int tmflags = -1; /* tone mapping flags (-1 for none) */ |
80 |
+ |
|
81 |
|
VIEW ourview = STDVIEW; /* image view parameters */ |
82 |
|
int gotview = 0; /* got parameters from file */ |
83 |
|
|
84 |
|
COLR *scanline; /* scan line buffer */ |
85 |
+ |
TMbright *lscan; /* encoded luminance scanline */ |
86 |
+ |
BYTE *cscan; /* encoded chroma scanline */ |
87 |
+ |
BYTE *pscan; /* compute pixel scanline */ |
88 |
|
|
89 |
|
RESOLU inpres; /* input resolution and ordering */ |
90 |
|
int xmax, ymax; /* picture dimensions */ |
175 |
|
tinterv = atoi(argv[++i]); |
176 |
|
break; |
177 |
|
case 'e': /* exposure comp. */ |
178 |
< |
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
178 |
> |
i++; |
179 |
> |
if (!strcmp(argv[i], "auto")) { |
180 |
> |
tmflags = TM_F_CAMERA; |
181 |
> |
break; |
182 |
> |
} |
183 |
> |
if (!strcmp(argv[i], "human")) { |
184 |
> |
tmflags = TM_F_HUMAN; |
185 |
> |
break; |
186 |
> |
} |
187 |
> |
if (argv[i][0] != '+' && argv[i][0] != '-') |
188 |
|
goto userr; |
189 |
< |
scale = atoi(argv[++i]); |
189 |
> |
scale = atoi(argv[i]); |
190 |
|
break; |
191 |
|
case 'g': /* gamma comp. */ |
192 |
|
if (argv[i][2] == 'e') |
243 |
|
getevent(); /* main loop */ |
244 |
|
userr: |
245 |
|
fprintf(stderr, |
246 |
< |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops][-g gamcor][-s][-ospec][-t intvl] pic ..\n", |
246 |
> |
"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n", |
247 |
|
progname); |
248 |
|
exit(1); |
249 |
|
} |
256 |
|
|
257 |
|
if (isexpos(s)) |
258 |
|
exposure *= exposval(s); |
259 |
< |
else if (isformat(s)) { |
249 |
< |
formatval(fmt, s); |
259 |
> |
else if (formatval(fmt, s)) |
260 |
|
wrongformat = strcmp(fmt, COLRFMT); |
261 |
< |
} else if (isview(s) && sscanview(&ourview, s) > 0) |
261 |
> |
else if (isview(s) && sscanview(&ourview, s) > 0) |
262 |
|
gotview++; |
263 |
|
} |
264 |
|
|
554 |
|
xmax, ymax, 8); |
555 |
|
if (ourras == NULL) |
556 |
|
goto fail; |
557 |
< |
if (greyscale | ourvis.class == StaticGray) |
557 |
> |
if (greyscale) |
558 |
|
getgrey(); |
559 |
|
else |
560 |
|
getmapped(); |
747 |
|
comp = .5/comp; |
748 |
|
comp = log(comp)/.69315 - scale; |
749 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
750 |
< |
if (n == 0) |
751 |
< |
return(0); |
752 |
< |
scale_rcolors(ourras, pow(2.0, (double)n)); |
750 |
> |
if (tmflags != -1) |
751 |
> |
tmflags = -1; /* turn off tone mapping */ |
752 |
> |
else { |
753 |
> |
if (n == 0) /* else check if any change */ |
754 |
> |
return(0); |
755 |
> |
scale_rcolors(ourras, pow(2.0, (double)n)); |
756 |
> |
} |
757 |
|
scale += n; |
758 |
|
sprintf(buf, "%+d", scale); |
759 |
|
XDrawImageString(thedisplay, wind, ourgc, |
927 |
|
} |
928 |
|
|
929 |
|
|
930 |
+ |
make_tonemap() /* initialize tone mapping */ |
931 |
+ |
{ |
932 |
+ |
int y; |
933 |
+ |
|
934 |
+ |
if (tmflags != -1 && fname == NULL) { |
935 |
+ |
fprintf(stderr, "%s: cannot adjust tone of standard input\n", |
936 |
+ |
progname); |
937 |
+ |
tmflags = -1; |
938 |
+ |
} |
939 |
+ |
if (tmflags == -1) { |
940 |
+ |
setcolrcor(pow, 1.0/gamcor); |
941 |
+ |
return; |
942 |
+ |
} |
943 |
+ |
if (greyscale) |
944 |
+ |
tmflags |= TM_F_BW; |
945 |
+ |
/* initialize tm library */ |
946 |
+ |
if (tmInit(tmflags, stdprims, gamcor) == NULL) |
947 |
+ |
goto memerr; |
948 |
+ |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
949 |
+ |
goto tmerr; |
950 |
+ |
/* allocate encoding buffers */ |
951 |
+ |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
952 |
+ |
goto memerr; |
953 |
+ |
if (tmflags & TM_F_BW) { |
954 |
+ |
cscan = TM_NOCHROM; |
955 |
+ |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
956 |
+ |
goto memerr; |
957 |
+ |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL) |
958 |
+ |
goto memerr; |
959 |
+ |
/* compute picture histogram */ |
960 |
+ |
for (y = 0; y < ymax; y++) { |
961 |
+ |
getscan(y); |
962 |
+ |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
963 |
+ |
goto tmerr; |
964 |
+ |
if (tmAddHisto(lscan, xmax, 1)) |
965 |
+ |
goto tmerr; |
966 |
+ |
} |
967 |
+ |
/* compute tone mapping */ |
968 |
+ |
if (tmComputeMapping(gamcor, 0., 0.)) |
969 |
+ |
goto tmerr; |
970 |
+ |
free((char *)lscan); |
971 |
+ |
return; |
972 |
+ |
memerr: |
973 |
+ |
quiterr("out of memory in make_tonemap"); |
974 |
+ |
tmerr: |
975 |
+ |
quiterr("tone mapping error"); |
976 |
+ |
} |
977 |
+ |
|
978 |
+ |
|
979 |
+ |
tmap_colrs(scn, len) /* apply tone mapping to scanline */ |
980 |
+ |
register COLR *scn; |
981 |
+ |
int len; |
982 |
+ |
{ |
983 |
+ |
register BYTE *ps; |
984 |
+ |
|
985 |
+ |
if (tmflags == -1) { |
986 |
+ |
if (scale) |
987 |
+ |
shiftcolrs(scn, len, scale); |
988 |
+ |
colrs_gambs(scn, len); |
989 |
+ |
return; |
990 |
+ |
} |
991 |
+ |
if (len > xmax) |
992 |
+ |
quiterr("code error 1 in tmap_colrs"); |
993 |
+ |
if (tmCvColrs(lscan, cscan, scn, len)) |
994 |
+ |
goto tmerr; |
995 |
+ |
if (tmMapPixels(pscan, lscan, cscan, len)) |
996 |
+ |
goto tmerr; |
997 |
+ |
ps = pscan; |
998 |
+ |
if (tmflags & TM_F_BW) |
999 |
+ |
while (len--) { |
1000 |
+ |
scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; |
1001 |
+ |
scn[0][EXP] = COLXS; |
1002 |
+ |
scn++; |
1003 |
+ |
} |
1004 |
+ |
else |
1005 |
+ |
while (len--) { |
1006 |
+ |
scn[0][RED] = *ps++; |
1007 |
+ |
scn[0][GRN] = *ps++; |
1008 |
+ |
scn[0][BLU] = *ps++; |
1009 |
+ |
scn[0][EXP] = COLXS; |
1010 |
+ |
scn++; |
1011 |
+ |
} |
1012 |
+ |
return; |
1013 |
+ |
tmerr: |
1014 |
+ |
quiterr("tone mapping error"); |
1015 |
+ |
} |
1016 |
+ |
|
1017 |
+ |
|
1018 |
+ |
done_tonemap() /* clean up after tone mapping is done */ |
1019 |
+ |
{ |
1020 |
+ |
if (tmflags == -1 || tmTop == NULL) |
1021 |
+ |
return; |
1022 |
+ |
tmDone(tmTop); /* clear old mapping */ |
1023 |
+ |
free((char *)lscan); /* free memory */ |
1024 |
+ |
free((char *)pscan); |
1025 |
+ |
} |
1026 |
+ |
|
1027 |
+ |
|
1028 |
|
getmono() /* get monochrome data */ |
1029 |
|
{ |
1030 |
|
register unsigned char *dp; |
1114 |
|
int y; |
1115 |
|
register unsigned int4 *dp; |
1116 |
|
register int x; |
1117 |
< |
/* set gamma correction */ |
1118 |
< |
setcolrgam(gamcor); |
1117 |
> |
/* initialize tone mapping */ |
1118 |
> |
make_tonemap(); |
1119 |
|
/* read and convert file */ |
1120 |
|
dp = (unsigned int4 *)ourdata; |
1121 |
|
for (y = 0; y < ymax; y++) { |
1122 |
|
getscan(y); |
1123 |
|
add2icon(y, scanline); |
1124 |
< |
if (scale) |
1013 |
< |
shiftcolrs(scanline, xmax, scale); |
1014 |
< |
colrs_gambs(scanline, xmax); |
1124 |
> |
tmap_colrs(scanline, xmax); |
1125 |
|
if (ourras->image->blue_mask & 1) |
1126 |
|
for (x = 0; x < xmax; x++) |
1127 |
|
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
1133 |
|
(unsigned int4)scanline[x][GRN] << 8 | |
1134 |
|
(unsigned int4)scanline[x][BLU] << 16 ; |
1135 |
|
} |
1136 |
+ |
done_tonemap(); |
1137 |
|
} |
1138 |
|
|
1139 |
|
|
1142 |
|
int y; |
1143 |
|
register unsigned char *dp; |
1144 |
|
register int x; |
1145 |
< |
/* set gamma correction */ |
1146 |
< |
setcolrgam(gamcor); |
1145 |
> |
/* initialize tone mapping */ |
1146 |
> |
make_tonemap(); |
1147 |
|
/* read and convert file */ |
1148 |
|
dp = ourdata; |
1149 |
|
for (y = 0; y < ymax; y++) { |
1150 |
|
getscan(y); |
1151 |
|
add2icon(y, scanline); |
1152 |
< |
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); |
1152 |
> |
tmap_colrs(scanline, xmax); |
1153 |
|
if (maxcolors < 256) |
1154 |
|
for (x = 0; x < xmax; x++) |
1155 |
< |
*dp++ = ((long)scanline[x][GRN] * |
1155 |
> |
*dp++ = ((int4)scanline[x][GRN] * |
1156 |
|
maxcolors + maxcolors/2) >> 8; |
1157 |
|
else |
1158 |
|
for (x = 0; x < xmax; x++) |
1160 |
|
} |
1161 |
|
for (x = 0; x < maxcolors; x++) |
1162 |
|
clrtab[x][RED] = clrtab[x][GRN] = |
1163 |
< |
clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; |
1163 |
> |
clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; |
1164 |
> |
done_tonemap(); |
1165 |
|
} |
1166 |
|
|
1167 |
|
|
1171 |
|
/* make sure we can do it first */ |
1172 |
|
if (fname == NULL) |
1173 |
|
quiterr("cannot map colors from standard input"); |
1174 |
< |
/* set gamma correction */ |
1175 |
< |
setcolrgam(gamcor); |
1174 |
> |
/* initialize tone mapping */ |
1175 |
> |
make_tonemap(); |
1176 |
|
/* make histogram */ |
1177 |
< |
if (new_histo((long)xmax*ymax) == -1) |
1177 |
> |
if (new_histo((int4)xmax*ymax) == -1) |
1178 |
|
quiterr("cannot initialize histogram"); |
1179 |
|
for (y = 0; y < ymax; y++) { |
1180 |
|
if (getscan(y) < 0) |
1181 |
|
break; |
1182 |
|
add2icon(y, scanline); |
1183 |
< |
if (scale) |
1076 |
< |
shiftcolrs(scanline, xmax, scale); |
1077 |
< |
colrs_gambs(scanline, xmax); |
1183 |
> |
tmap_colrs(scanline, xmax); |
1184 |
|
cnt_colrs(scanline, xmax); |
1185 |
|
} |
1186 |
|
/* map pixels */ |
1188 |
|
quiterr("cannot create color map"); |
1189 |
|
for (y = 0; y < ymax; y++) { |
1190 |
|
getscan(y); |
1191 |
< |
if (scale) |
1086 |
< |
shiftcolrs(scanline, xmax, scale); |
1087 |
< |
colrs_gambs(scanline, xmax); |
1191 |
> |
tmap_colrs(scanline, xmax); |
1192 |
|
if (dither) |
1193 |
|
dith_colrs(ourdata+y*xmax, scanline, xmax); |
1194 |
|
else |
1195 |
|
map_colrs(ourdata+y*xmax, scanline, xmax); |
1196 |
|
} |
1197 |
+ |
done_tonemap(); |
1198 |
|
} |
1199 |
|
|
1200 |
|
|