1 |
greg |
3.1 |
#ifndef lint |
2 |
schorsch |
3.20 |
static const char RCSid[] = "$Id: pcond.c,v 3.19 2003/11/10 11:54:23 schorsch Exp $"; |
3 |
greg |
3.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Condition Radiance picture for display/output |
6 |
greg |
3.14 |
* Added white-balance adjustment 10/01 (GW). |
7 |
greg |
3.1 |
*/ |
8 |
|
|
|
9 |
schorsch |
3.15 |
#include "platform.h" |
10 |
schorsch |
3.19 |
#include "paths.h" |
11 |
schorsch |
3.18 |
#include "rtprocess.h" |
12 |
greg |
3.1 |
#include "pcond.h" |
13 |
|
|
|
14 |
|
|
|
15 |
|
|
#define LDMAX 100 /* default max. display luminance */ |
16 |
greg |
3.11 |
#define LDDYN 32 /* default dynamic range */ |
17 |
greg |
3.1 |
|
18 |
|
|
int what2do = 0; /* desired adjustments */ |
19 |
|
|
|
20 |
|
|
double ldmax = LDMAX; /* maximum output luminance */ |
21 |
greg |
3.11 |
double lddyn = LDDYN; /* display dynamic range */ |
22 |
|
|
double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */ |
23 |
greg |
3.1 |
|
24 |
|
|
char *progname; /* global argv[0] */ |
25 |
|
|
|
26 |
|
|
char *infn; /* input file name */ |
27 |
|
|
FILE *infp; /* input stream */ |
28 |
greg |
3.6 |
FILE *mapfp = NULL; /* tone-mapping function stream */ |
29 |
greg |
3.1 |
VIEW ourview = STDVIEW; /* picture view */ |
30 |
|
|
int gotview = 0; /* picture has view */ |
31 |
|
|
double pixaspect = 1.0; /* pixel aspect ratio */ |
32 |
greg |
3.9 |
double fixfrac = 0.; /* histogram share due to fixations */ |
33 |
greg |
3.1 |
RESOLU inpres; /* input picture resolution */ |
34 |
|
|
|
35 |
|
|
COLOR *fovimg; /* foveal (1 degree) averaged image */ |
36 |
greg |
3.14 |
int fvxr, fvyr; /* foveal image resolution */ |
37 |
|
|
float *crfimg; /* contrast reduction factors */ |
38 |
greg |
3.9 |
short (*fixlst)[2]; /* fixation history list */ |
39 |
|
|
int nfixations; /* number of fixation points */ |
40 |
greg |
3.14 |
double bwhist[HISTRES]; /* luminance histogram */ |
41 |
greg |
3.8 |
double histot; /* total count of histogram */ |
42 |
greg |
3.1 |
double bwmin, bwmax; /* histogram limits */ |
43 |
|
|
double bwavg; /* mean brightness */ |
44 |
|
|
|
45 |
|
|
double scalef = 0.; /* linear scaling factor */ |
46 |
|
|
|
47 |
schorsch |
3.20 |
static gethfunc headline; |
48 |
|
|
|
49 |
greg |
3.1 |
|
50 |
|
|
main(argc, argv) |
51 |
|
|
int argc; |
52 |
|
|
char *argv[]; |
53 |
|
|
{ |
54 |
|
|
static RGBPRIMS outprimS; |
55 |
|
|
int i; |
56 |
|
|
#define bool(flg) switch (argv[i][2]) { \ |
57 |
|
|
case '\0': what2do ^= flg; break; \ |
58 |
|
|
case 'y': case 'Y': case 't': case 'T': \ |
59 |
|
|
case '+': case '1': what2do |= flg; break; \ |
60 |
|
|
case 'n': case 'N': case 'f': case 'F': \ |
61 |
|
|
case '-': case '0': what2do &= ~(flg); break; \ |
62 |
|
|
default: goto userr; } |
63 |
|
|
|
64 |
|
|
progname = argv[0]; |
65 |
|
|
|
66 |
|
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
67 |
|
|
switch (argv[i][1]) { |
68 |
|
|
case 'h': |
69 |
|
|
bool(DO_HUMAN); |
70 |
|
|
break; |
71 |
|
|
case 'a': |
72 |
|
|
bool(DO_ACUITY); |
73 |
|
|
break; |
74 |
|
|
case 'v': |
75 |
|
|
bool(DO_VEIL); |
76 |
|
|
break; |
77 |
|
|
case 's': |
78 |
|
|
bool(DO_HSENS); |
79 |
|
|
break; |
80 |
|
|
case 'c': |
81 |
|
|
bool(DO_COLOR); |
82 |
|
|
break; |
83 |
|
|
case 'w': |
84 |
|
|
bool(DO_CWEIGHT); |
85 |
|
|
break; |
86 |
greg |
3.9 |
case 'i': |
87 |
|
|
if (i+1 >= argc) goto userr; |
88 |
|
|
fixfrac = atof(argv[++i]); |
89 |
|
|
if (fixfrac > FTINY) what2do |= DO_FIXHIST; |
90 |
|
|
else what2do &= ~DO_FIXHIST; |
91 |
|
|
break; |
92 |
gwlarson |
3.12 |
case 'I': |
93 |
|
|
bool(DO_PREHIST); |
94 |
|
|
break; |
95 |
greg |
3.1 |
case 'l': |
96 |
|
|
bool(DO_LINEAR); |
97 |
|
|
break; |
98 |
|
|
case 'p': |
99 |
|
|
if (i+8 >= argc) goto userr; |
100 |
|
|
outprimS[RED][CIEX] = atof(argv[++i]); |
101 |
|
|
outprimS[RED][CIEY] = atof(argv[++i]); |
102 |
|
|
outprimS[GRN][CIEX] = atof(argv[++i]); |
103 |
|
|
outprimS[GRN][CIEY] = atof(argv[++i]); |
104 |
|
|
outprimS[BLU][CIEX] = atof(argv[++i]); |
105 |
|
|
outprimS[BLU][CIEY] = atof(argv[++i]); |
106 |
|
|
outprimS[WHT][CIEX] = atof(argv[++i]); |
107 |
|
|
outprimS[WHT][CIEY] = atof(argv[++i]); |
108 |
|
|
outprims = outprimS; |
109 |
|
|
break; |
110 |
|
|
case 'e': |
111 |
|
|
if (i+1 >= argc) goto userr; |
112 |
|
|
scalef = atof(argv[++i]); |
113 |
schorsch |
3.17 |
if ((argv[i][0] == '+') | (argv[i][0] == '-')) |
114 |
greg |
3.1 |
scalef = pow(2.0, scalef); |
115 |
|
|
what2do |= DO_LINEAR; |
116 |
|
|
break; |
117 |
|
|
case 'f': |
118 |
|
|
if (i+1 >= argc) goto userr; |
119 |
|
|
mbcalfile = argv[++i]; |
120 |
|
|
break; |
121 |
greg |
3.10 |
case 'm': |
122 |
|
|
if (i+1 >= argc) goto userr; |
123 |
|
|
cwarpfile = argv[++i]; |
124 |
|
|
break; |
125 |
greg |
3.11 |
case 'u': |
126 |
greg |
3.1 |
if (i+1 >= argc) goto userr; |
127 |
|
|
ldmax = atof(argv[++i]); |
128 |
|
|
if (ldmax <= FTINY) |
129 |
|
|
goto userr; |
130 |
|
|
break; |
131 |
greg |
3.11 |
case 'd': |
132 |
greg |
3.1 |
if (i+1 >= argc) goto userr; |
133 |
greg |
3.11 |
lddyn = atof(argv[++i]); |
134 |
greg |
3.1 |
break; |
135 |
greg |
3.11 |
case 'x': |
136 |
greg |
3.6 |
if (i+1 >= argc) goto userr; |
137 |
|
|
if ((mapfp = fopen(argv[++i], "w")) == NULL) { |
138 |
|
|
fprintf(stderr, |
139 |
|
|
"%s: cannot open for writing\n", |
140 |
|
|
argv[i]); |
141 |
|
|
exit(1); |
142 |
|
|
} |
143 |
|
|
break; |
144 |
greg |
3.1 |
default: |
145 |
|
|
goto userr; |
146 |
|
|
} |
147 |
gwlarson |
3.12 |
if ((what2do & (DO_FIXHIST|DO_PREHIST)) == (DO_FIXHIST|DO_PREHIST)) { |
148 |
|
|
fprintf(stderr, "%s: only one of -i or -I option\n", progname); |
149 |
|
|
exit(1); |
150 |
|
|
} |
151 |
greg |
3.10 |
if ((mbcalfile != NULL) + (cwarpfile != NULL) + |
152 |
|
|
(outprims != stdprims) > 1) { |
153 |
|
|
fprintf(stderr, |
154 |
|
|
"%s: only one of -p, -m or -f option supported\n", |
155 |
greg |
3.1 |
progname); |
156 |
|
|
exit(1); |
157 |
|
|
} |
158 |
schorsch |
3.17 |
if ((outprims == stdprims) & (inprims != stdprims)) |
159 |
greg |
3.1 |
outprims = inprims; |
160 |
greg |
3.11 |
Bldmin = Bl(ldmax/lddyn); |
161 |
greg |
3.1 |
Bldmax = Bl(ldmax); |
162 |
|
|
if (i >= argc || i+2 < argc) |
163 |
|
|
goto userr; |
164 |
greg |
3.9 |
/* open input file */ |
165 |
greg |
3.1 |
if ((infp = fopen(infn=argv[i], "r")) == NULL) |
166 |
|
|
syserror(infn); |
167 |
greg |
3.9 |
/* open output file */ |
168 |
greg |
3.1 |
if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL) |
169 |
|
|
syserror(argv[i+1]); |
170 |
schorsch |
3.15 |
SET_FILE_BINARY(infp); |
171 |
|
|
SET_FILE_BINARY(stdout); |
172 |
greg |
3.1 |
getahead(); /* load input header */ |
173 |
|
|
printargs(argc, argv, stdout); /* add to output header */ |
174 |
schorsch |
3.17 |
if ((mbcalfile == NULL) & (outprims != stdprims)) |
175 |
greg |
3.1 |
fputprims(outprims, stdout); |
176 |
gwlarson |
3.12 |
if ((what2do & (DO_PREHIST|DO_VEIL|DO_ACUITY)) != DO_PREHIST) |
177 |
|
|
getfovimg(); /* get foveal sample image? */ |
178 |
|
|
if (what2do&DO_PREHIST) /* get histogram? */ |
179 |
|
|
gethisto(stdin); |
180 |
|
|
else if (what2do&DO_FIXHIST) /* get fixation history? */ |
181 |
greg |
3.9 |
getfixations(stdin); |
182 |
greg |
3.1 |
mapimage(); /* map the picture */ |
183 |
greg |
3.6 |
if (mapfp != NULL) /* write out basic mapping */ |
184 |
|
|
putmapping(mapfp); |
185 |
greg |
3.1 |
exit(0); |
186 |
|
|
userr: |
187 |
gwlarson |
3.12 |
fprintf(stderr, "Usage: %s [-{h|a|v|s|c|l|w}[+-]][-I|-i ffrac][-e ev][-p xr yr xg yg xb yb xw yw|-f mbf.cal|-m rgb.cwp][-u Ldmax][-d Lddyn][-x mapfile] inpic [outpic]\n", |
188 |
greg |
3.1 |
progname); |
189 |
|
|
exit(1); |
190 |
|
|
#undef bool |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
|
194 |
|
|
syserror(s) /* report system error and exit */ |
195 |
|
|
char *s; |
196 |
|
|
{ |
197 |
|
|
fprintf(stderr, "%s: ", progname); |
198 |
|
|
perror(s); |
199 |
|
|
exit(2); |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
|
203 |
schorsch |
3.20 |
static int |
204 |
|
|
headline( /* process header line */ |
205 |
|
|
char *s, |
206 |
|
|
void *p |
207 |
|
|
) |
208 |
greg |
3.1 |
{ |
209 |
|
|
static RGBPRIMS inprimS; |
210 |
|
|
char fmt[32]; |
211 |
|
|
|
212 |
|
|
if (formatval(fmt, s)) { /* check if format string */ |
213 |
|
|
if (!strcmp(fmt,COLRFMT)) lumf = rgblum; |
214 |
|
|
else if (!strcmp(fmt,CIEFMT)) lumf = cielum; |
215 |
|
|
else lumf = NULL; |
216 |
gwlarson |
3.13 |
return(0); /* don't echo */ |
217 |
greg |
3.1 |
} |
218 |
|
|
if (isprims(s)) { /* get input primaries */ |
219 |
|
|
primsval(inprimS, s); |
220 |
|
|
inprims= inprimS; |
221 |
gwlarson |
3.13 |
return(0); /* don't echo */ |
222 |
greg |
3.1 |
} |
223 |
|
|
if (isexpos(s)) { /* picture exposure */ |
224 |
|
|
inpexp *= exposval(s); |
225 |
gwlarson |
3.13 |
return(0); /* don't echo */ |
226 |
greg |
3.1 |
} |
227 |
|
|
if (isaspect(s)) /* pixel aspect ratio */ |
228 |
|
|
pixaspect *= aspectval(s); |
229 |
|
|
if (isview(s)) /* image view */ |
230 |
|
|
gotview += sscanview(&ourview, s); |
231 |
gwlarson |
3.13 |
return(fputs(s, stdout)); |
232 |
greg |
3.1 |
} |
233 |
|
|
|
234 |
|
|
|
235 |
|
|
getahead() /* load picture header */ |
236 |
|
|
{ |
237 |
|
|
char *err; |
238 |
|
|
|
239 |
|
|
getheader(infp, headline, NULL); |
240 |
|
|
if (lumf == NULL || !fgetsresolu(&inpres, infp)) { |
241 |
|
|
fprintf(stderr, "%s: %s: not a Radiance picture\n", |
242 |
|
|
progname, infn); |
243 |
|
|
exit(1); |
244 |
|
|
} |
245 |
|
|
if (lumf == rgblum) |
246 |
greg |
3.14 |
comprgb2xyzWBmat(inrgb2xyz, inprims); |
247 |
greg |
3.1 |
else if (mbcalfile != NULL) { |
248 |
|
|
fprintf(stderr, "%s: macbethcal only works with RGB pictures\n", |
249 |
|
|
progname); |
250 |
|
|
exit(1); |
251 |
|
|
} |
252 |
|
|
if (!gotview || ourview.type == VT_PAR) { |
253 |
schorsch |
3.16 |
ourview = stdview; |
254 |
greg |
3.1 |
ourview.type = VT_PER; |
255 |
|
|
if (pixaspect*inpres.yr < inpres.xr) { |
256 |
|
|
ourview.horiz = 40.0; |
257 |
|
|
ourview.vert = 2.*180./PI * |
258 |
|
|
atan(.3639702*pixaspect*inpres.yr/inpres.xr); |
259 |
|
|
} else { |
260 |
|
|
ourview.vert = 40.0; |
261 |
|
|
ourview.horiz = 2.*180./PI * |
262 |
|
|
atan(.3639702*inpres.xr/pixaspect/inpres.yr); |
263 |
|
|
} |
264 |
|
|
} |
265 |
|
|
if ((err = setview(&ourview)) != NULL) { |
266 |
|
|
fprintf(stderr, "%s: view error in picture \"%s\": %s\n", |
267 |
|
|
progname, infn, err); |
268 |
|
|
exit(1); |
269 |
|
|
} |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
|
273 |
|
|
mapimage() /* map picture and send to stdout */ |
274 |
|
|
{ |
275 |
|
|
COLOR *scan; |
276 |
|
|
|
277 |
greg |
3.8 |
comphist(); /* generate adaptation histogram */ |
278 |
greg |
3.1 |
check2do(); /* modify what2do flags */ |
279 |
greg |
3.9 |
if (what2do&DO_VEIL) |
280 |
greg |
3.14 |
compveil(); /* compute veil image */ |
281 |
|
|
if (!(what2do&DO_LINEAR)) |
282 |
|
|
if (mkbrmap() < 0) /* make tone map */ |
283 |
|
|
what2do |= DO_LINEAR; /* failed! -- use linear */ |
284 |
|
|
#if ADJ_VEIL |
285 |
|
|
else if (what2do&DO_VEIL) |
286 |
|
|
adjveil(); /* else adjust veil image */ |
287 |
|
|
#endif |
288 |
greg |
3.1 |
if (what2do&DO_LINEAR) { |
289 |
|
|
if (scalef <= FTINY) { |
290 |
|
|
if (what2do&DO_HSENS) |
291 |
|
|
scalef = htcontrs(Lb(0.5*(Bldmax+Bldmin))) / |
292 |
|
|
htcontrs(Lb(bwavg)); |
293 |
|
|
else |
294 |
|
|
scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg); |
295 |
|
|
scalef *= WHTEFFICACY/(inpexp*ldmax); |
296 |
|
|
} |
297 |
greg |
3.4 |
fputexpos(inpexp*scalef, stdout); /* record exposure */ |
298 |
greg |
3.1 |
if (lumf == cielum) scalef /= WHTEFFICACY; |
299 |
|
|
} |
300 |
greg |
3.9 |
fputformat(COLRFMT, stdout); /* complete header */ |
301 |
|
|
putchar('\n'); |
302 |
greg |
3.1 |
fputsresolu(&inpres, stdout); /* resolution doesn't change */ |
303 |
greg |
3.9 |
/* condition our image */ |
304 |
greg |
3.1 |
for (scan = firstscan(); scan != NULL; scan = nextscan()) |
305 |
|
|
if (fwritescan(scan, scanlen(&inpres), stdout) < 0) { |
306 |
|
|
fprintf(stderr, "%s: scanline write error\n", |
307 |
|
|
progname); |
308 |
|
|
exit(1); |
309 |
|
|
} |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
|
313 |
greg |
3.8 |
getfovimg() /* load foveal sampled image */ |
314 |
greg |
3.1 |
{ |
315 |
schorsch |
3.19 |
char combuf[PATH_MAX]; |
316 |
greg |
3.1 |
FILE *fp; |
317 |
|
|
int x, y; |
318 |
greg |
3.8 |
/* compute image size */ |
319 |
greg |
3.1 |
fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5; |
320 |
|
|
if (fvxr < 2) fvxr = 2; |
321 |
|
|
fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5; |
322 |
|
|
if (fvyr < 2) fvyr = 2; |
323 |
greg |
3.14 |
if (!(inpres.rt & YMAJOR)) { /* picture is rotated? */ |
324 |
greg |
3.1 |
y = fvyr; |
325 |
|
|
fvyr = fvxr; |
326 |
|
|
fvxr = y; |
327 |
|
|
} |
328 |
|
|
if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL) |
329 |
|
|
syserror("malloc"); |
330 |
schorsch |
3.19 |
sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d \"%s\"", fvxr, fvyr, infn); |
331 |
greg |
3.1 |
if ((fp = popen(combuf, "r")) == NULL) |
332 |
|
|
syserror("popen"); |
333 |
|
|
getheader(fp, NULL, NULL); /* skip header */ |
334 |
schorsch |
3.17 |
if (fgetresolu(&x, &y, fp) < 0 || (x != fvxr) | (y != fvyr)) |
335 |
greg |
3.1 |
goto readerr; |
336 |
|
|
for (y = 0; y < fvyr; y++) |
337 |
|
|
if (freadscan(fovscan(y), fvxr, fp) < 0) |
338 |
|
|
goto readerr; |
339 |
|
|
pclose(fp); |
340 |
greg |
3.8 |
return; |
341 |
|
|
readerr: |
342 |
|
|
fprintf(stderr, "%s: error reading from pfilt process in fovimage\n", |
343 |
|
|
progname); |
344 |
|
|
exit(1); |
345 |
|
|
} |
346 |
|
|
|
347 |
|
|
|
348 |
greg |
3.1 |
check2do() /* check histogram to see what isn't worth doing */ |
349 |
|
|
{ |
350 |
greg |
3.8 |
double sum; |
351 |
greg |
3.1 |
double b, l; |
352 |
|
|
register int i; |
353 |
|
|
|
354 |
|
|
/* check for within display range */ |
355 |
greg |
3.9 |
if (bwmax - bwmin <= Bldmax - Bldmin) |
356 |
greg |
3.1 |
what2do |= DO_LINEAR; |
357 |
greg |
3.3 |
/* determine if veiling significant */ |
358 |
greg |
3.9 |
if (bwmax - bwmin < 4.5) /* heuristic */ |
359 |
greg |
3.3 |
what2do &= ~DO_VEIL; |
360 |
greg |
3.1 |
|
361 |
greg |
3.3 |
if (!(what2do & (DO_ACUITY|DO_COLOR))) |
362 |
greg |
3.1 |
return; |
363 |
|
|
/* find 5th percentile */ |
364 |
greg |
3.8 |
sum = histot*0.05; |
365 |
greg |
3.1 |
for (i = 0; i < HISTRES; i++) |
366 |
|
|
if ((sum -= bwhist[i]) <= 0) |
367 |
|
|
break; |
368 |
|
|
b = (i+.5)*(bwmax-bwmin)/HISTRES + bwmin; |
369 |
|
|
l = Lb(b); |
370 |
|
|
/* determine if acuity adj. useful */ |
371 |
|
|
if (what2do&DO_ACUITY && |
372 |
|
|
hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) + |
373 |
greg |
3.5 |
inpres.yr/sqrt(ourview.vn2))/(2.*180./PI)) |
374 |
greg |
3.1 |
what2do &= ~DO_ACUITY; |
375 |
|
|
/* color sensitivity loss? */ |
376 |
greg |
3.5 |
if (l >= TopMesopic) |
377 |
greg |
3.1 |
what2do &= ~DO_COLOR; |
378 |
|
|
} |