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 (!strcmp(argv[i], "auto")) { |
182 |
> |
tmflags = TM_F_CAMERA; |
183 |
> |
break; |
184 |
> |
} |
185 |
> |
if (!strcmp(argv[i], "human")) { |
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(); |
749 |
|
comp = .5/comp; |
750 |
|
comp = log(comp)/.69315 - scale; |
751 |
|
n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ |
752 |
< |
if (n == 0) |
753 |
< |
return(0); |
754 |
< |
scale_rcolors(ourras, pow(2.0, (double)n)); |
752 |
> |
if (tmflags != -1) |
753 |
> |
tmflags = -1; /* turn off tone mapping */ |
754 |
> |
else { |
755 |
> |
if (n == 0) /* else check if any change */ |
756 |
> |
return(0); |
757 |
> |
scale_rcolors(ourras, pow(2.0, (double)n)); |
758 |
> |
} |
759 |
|
scale += n; |
760 |
|
sprintf(buf, "%+d", scale); |
761 |
|
XDrawImageString(thedisplay, wind, ourgc, |
929 |
|
} |
930 |
|
|
931 |
|
|
932 |
+ |
make_tonemap() /* initialize tone mapping */ |
933 |
+ |
{ |
934 |
+ |
int y; |
935 |
+ |
|
936 |
+ |
if (tmflags != -1 && fname == NULL) { |
937 |
+ |
fprintf(stderr, "%s: cannot adjust tone of standard input\n", |
938 |
+ |
progname); |
939 |
+ |
tmflags = -1; |
940 |
+ |
} |
941 |
+ |
if (tmflags == -1) { |
942 |
+ |
setcolrcor(pow, 1.0/gamcor); |
943 |
+ |
return; |
944 |
+ |
} |
945 |
+ |
if (greyscale) |
946 |
+ |
tmflags |= TM_F_BW; |
947 |
+ |
/* initialize tm library */ |
948 |
+ |
if (tmInit(tmflags, stdprims, gamcor) == NULL) |
949 |
+ |
goto memerr; |
950 |
+ |
if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) |
951 |
+ |
goto tmerr; |
952 |
+ |
/* allocate encoding buffers */ |
953 |
+ |
if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) |
954 |
+ |
goto memerr; |
955 |
+ |
if (tmflags & TM_F_BW) { |
956 |
+ |
cscan = TM_NOCHROM; |
957 |
+ |
if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) |
958 |
+ |
goto memerr; |
959 |
+ |
} else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) == NULL) |
960 |
+ |
goto memerr; |
961 |
+ |
/* compute picture histogram */ |
962 |
+ |
for (y = 0; y < ymax; y++) { |
963 |
+ |
getscan(y); |
964 |
+ |
if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) |
965 |
+ |
goto tmerr; |
966 |
+ |
if (tmAddHisto(lscan, xmax, 1)) |
967 |
+ |
goto tmerr; |
968 |
+ |
} |
969 |
+ |
/* compute tone mapping */ |
970 |
+ |
if (tmComputeMapping(gamcor, 0., 0.)) |
971 |
+ |
goto tmerr; |
972 |
+ |
free((char *)lscan); |
973 |
+ |
return; |
974 |
+ |
memerr: |
975 |
+ |
quiterr("out of memory in make_tonemap"); |
976 |
+ |
tmerr: |
977 |
+ |
quiterr("tone mapping error"); |
978 |
+ |
} |
979 |
+ |
|
980 |
+ |
|
981 |
+ |
tmap_colrs(scn, len) /* apply tone mapping to scanline */ |
982 |
+ |
register COLR *scn; |
983 |
+ |
int len; |
984 |
+ |
{ |
985 |
+ |
register BYTE *ps; |
986 |
+ |
|
987 |
+ |
if (tmflags == -1) { |
988 |
+ |
if (scale) |
989 |
+ |
shiftcolrs(scn, len, scale); |
990 |
+ |
colrs_gambs(scn, len); |
991 |
+ |
return; |
992 |
+ |
} |
993 |
+ |
if (len > xmax) |
994 |
+ |
quiterr("code error 1 in tmap_colrs"); |
995 |
+ |
if (tmCvColrs(lscan, cscan, scn, len)) |
996 |
+ |
goto tmerr; |
997 |
+ |
if (tmMapPixels(pscan, lscan, cscan, len)) |
998 |
+ |
goto tmerr; |
999 |
+ |
ps = pscan; |
1000 |
+ |
if (tmflags & TM_F_BW) |
1001 |
+ |
while (len--) { |
1002 |
+ |
scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; |
1003 |
+ |
scn[0][EXP] = COLXS; |
1004 |
+ |
scn++; |
1005 |
+ |
} |
1006 |
+ |
else |
1007 |
+ |
while (len--) { |
1008 |
+ |
scn[0][RED] = *ps++; |
1009 |
+ |
scn[0][GRN] = *ps++; |
1010 |
+ |
scn[0][BLU] = *ps++; |
1011 |
+ |
scn[0][EXP] = COLXS; |
1012 |
+ |
scn++; |
1013 |
+ |
} |
1014 |
+ |
return; |
1015 |
+ |
tmerr: |
1016 |
+ |
quiterr("tone mapping error"); |
1017 |
+ |
} |
1018 |
+ |
|
1019 |
+ |
|
1020 |
+ |
done_tonemap() /* clean up after tone mapping is done */ |
1021 |
+ |
{ |
1022 |
+ |
if (tmflags == -1 || tmTop == NULL) |
1023 |
+ |
return; |
1024 |
+ |
tmDone(tmTop); /* clear old mapping */ |
1025 |
+ |
free((char *)lscan); /* free memory */ |
1026 |
+ |
free((char *)pscan); |
1027 |
+ |
} |
1028 |
+ |
|
1029 |
+ |
|
1030 |
|
getmono() /* get monochrome data */ |
1031 |
|
{ |
1032 |
|
register unsigned char *dp; |
1116 |
|
int y; |
1117 |
|
register unsigned int4 *dp; |
1118 |
|
register int x; |
1119 |
< |
/* set gamma correction */ |
1120 |
< |
setcolrgam(gamcor); |
1119 |
> |
/* initialize tone mapping */ |
1120 |
> |
make_tonemap(); |
1121 |
|
/* read and convert file */ |
1122 |
|
dp = (unsigned int4 *)ourdata; |
1123 |
|
for (y = 0; y < ymax; y++) { |
1124 |
|
getscan(y); |
1125 |
|
add2icon(y, scanline); |
1126 |
< |
if (scale) |
1013 |
< |
shiftcolrs(scanline, xmax, scale); |
1014 |
< |
colrs_gambs(scanline, xmax); |
1126 |
> |
tmap_colrs(scanline, xmax); |
1127 |
|
if (ourras->image->blue_mask & 1) |
1128 |
|
for (x = 0; x < xmax; x++) |
1129 |
|
*dp++ = (unsigned int4)scanline[x][RED] << 16 | |
1135 |
|
(unsigned int4)scanline[x][GRN] << 8 | |
1136 |
|
(unsigned int4)scanline[x][BLU] << 16 ; |
1137 |
|
} |
1138 |
+ |
done_tonemap(); |
1139 |
|
} |
1140 |
|
|
1141 |
|
|
1144 |
|
int y; |
1145 |
|
register unsigned char *dp; |
1146 |
|
register int x; |
1147 |
< |
/* set gamma correction */ |
1148 |
< |
setcolrgam(gamcor); |
1147 |
> |
/* initialize tone mapping */ |
1148 |
> |
make_tonemap(); |
1149 |
|
/* read and convert file */ |
1150 |
|
dp = ourdata; |
1151 |
|
for (y = 0; y < ymax; y++) { |
1152 |
|
getscan(y); |
1153 |
|
add2icon(y, scanline); |
1154 |
< |
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); |
1154 |
> |
tmap_colrs(scanline, xmax); |
1155 |
|
if (maxcolors < 256) |
1156 |
|
for (x = 0; x < xmax; x++) |
1157 |
< |
*dp++ = ((long)scanline[x][GRN] * |
1157 |
> |
*dp++ = ((int4)scanline[x][GRN] * |
1158 |
|
maxcolors + maxcolors/2) >> 8; |
1159 |
|
else |
1160 |
|
for (x = 0; x < xmax; x++) |
1162 |
|
} |
1163 |
|
for (x = 0; x < maxcolors; x++) |
1164 |
|
clrtab[x][RED] = clrtab[x][GRN] = |
1165 |
< |
clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; |
1165 |
> |
clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; |
1166 |
> |
done_tonemap(); |
1167 |
|
} |
1168 |
|
|
1169 |
|
|
1173 |
|
/* make sure we can do it first */ |
1174 |
|
if (fname == NULL) |
1175 |
|
quiterr("cannot map colors from standard input"); |
1176 |
< |
/* set gamma correction */ |
1177 |
< |
setcolrgam(gamcor); |
1176 |
> |
/* initialize tone mapping */ |
1177 |
> |
make_tonemap(); |
1178 |
|
/* make histogram */ |
1179 |
< |
if (new_histo((long)xmax*ymax) == -1) |
1179 |
> |
if (new_histo((int4)xmax*ymax) == -1) |
1180 |
|
quiterr("cannot initialize histogram"); |
1181 |
|
for (y = 0; y < ymax; y++) { |
1182 |
|
if (getscan(y) < 0) |
1183 |
|
break; |
1184 |
|
add2icon(y, scanline); |
1185 |
< |
if (scale) |
1076 |
< |
shiftcolrs(scanline, xmax, scale); |
1077 |
< |
colrs_gambs(scanline, xmax); |
1185 |
> |
tmap_colrs(scanline, xmax); |
1186 |
|
cnt_colrs(scanline, xmax); |
1187 |
|
} |
1188 |
|
/* map pixels */ |
1190 |
|
quiterr("cannot create color map"); |
1191 |
|
for (y = 0; y < ymax; y++) { |
1192 |
|
getscan(y); |
1193 |
< |
if (scale) |
1086 |
< |
shiftcolrs(scanline, xmax, scale); |
1087 |
< |
colrs_gambs(scanline, xmax); |
1193 |
> |
tmap_colrs(scanline, xmax); |
1194 |
|
if (dither) |
1195 |
|
dith_colrs(ourdata+y*xmax, scanline, xmax); |
1196 |
|
else |
1197 |
|
map_colrs(ourdata+y*xmax, scanline, xmax); |
1198 |
|
} |
1199 |
+ |
done_tonemap(); |
1200 |
|
} |
1201 |
|
|
1202 |
|
|