1 |
– |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* Condition Radiance picture for display/output |
6 |
+ |
* Added white-balance adjustment 10/01 (GW). |
7 |
|
*/ |
8 |
|
|
9 |
+ |
#include "platform.h" |
10 |
+ |
#include "paths.h" |
11 |
+ |
#include "rtprocess.h" |
12 |
|
#include "pcond.h" |
13 |
|
|
13 |
– |
#include "random.h" |
14 |
|
|
15 |
– |
|
15 |
|
#define LDMAX 100 /* default max. display luminance */ |
16 |
< |
#define LDMINF 0.01 /* default min. display lum. factor */ |
16 |
> |
#define LDDYN 32 /* default dynamic range */ |
17 |
|
|
18 |
|
int what2do = 0; /* desired adjustments */ |
19 |
|
|
20 |
|
double ldmax = LDMAX; /* maximum output luminance */ |
21 |
< |
double ldmin = 0.; /* minimum output luminance */ |
22 |
< |
double Bldmin, Bldmax; /* Bl(ldmin) and Bl(ldmax) */ |
21 |
> |
double lddyn = LDDYN; /* display dynamic range */ |
22 |
> |
double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */ |
23 |
|
|
24 |
|
char *progname; /* global argv[0] */ |
25 |
|
|
26 |
|
char *infn; /* input file name */ |
27 |
|
FILE *infp; /* input stream */ |
28 |
+ |
FILE *mapfp = NULL; /* tone-mapping function stream */ |
29 |
|
VIEW ourview = STDVIEW; /* picture view */ |
30 |
|
int gotview = 0; /* picture has view */ |
31 |
|
double pixaspect = 1.0; /* pixel aspect ratio */ |
32 |
+ |
double fixfrac = 0.; /* histogram share due to fixations */ |
33 |
|
RESOLU inpres; /* input picture resolution */ |
34 |
|
|
35 |
|
COLOR *fovimg; /* foveal (1 degree) averaged image */ |
36 |
< |
short fvxr, fvyr; /* foveal image resolution */ |
37 |
< |
int bwhist[HISTRES]; /* luminance histogram */ |
38 |
< |
long histot; /* total count of histogram */ |
36 |
> |
int fvxr, fvyr; /* foveal image resolution */ |
37 |
> |
float *crfimg; /* contrast reduction factors */ |
38 |
> |
short (*fixlst)[2]; /* fixation history list */ |
39 |
> |
int nfixations; /* number of fixation points */ |
40 |
> |
double bwhist[HISTRES]; /* luminance histogram */ |
41 |
> |
double histot; /* total count of histogram */ |
42 |
|
double bwmin, bwmax; /* histogram limits */ |
43 |
|
double bwavg; /* mean brightness */ |
44 |
|
|
45 |
|
double scalef = 0.; /* linear scaling factor */ |
46 |
|
|
47 |
+ |
static gethfunc headline; |
48 |
|
|
49 |
+ |
|
50 |
|
main(argc, argv) |
51 |
|
int argc; |
52 |
|
char *argv[]; |
83 |
|
case 'w': |
84 |
|
bool(DO_CWEIGHT); |
85 |
|
break; |
86 |
+ |
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 |
+ |
case 'I': |
93 |
+ |
bool(DO_PREHIST); |
94 |
+ |
break; |
95 |
|
case 'l': |
96 |
|
bool(DO_LINEAR); |
97 |
|
break; |
110 |
|
case 'e': |
111 |
|
if (i+1 >= argc) goto userr; |
112 |
|
scalef = atof(argv[++i]); |
113 |
< |
if (argv[i][0] == '+' | argv[i][0] == '-') |
113 |
> |
if ((argv[i][0] == '+') | (argv[i][0] == '-')) |
114 |
|
scalef = pow(2.0, scalef); |
115 |
|
what2do |= DO_LINEAR; |
116 |
|
break; |
118 |
|
if (i+1 >= argc) goto userr; |
119 |
|
mbcalfile = argv[++i]; |
120 |
|
break; |
121 |
< |
case 't': |
121 |
> |
case 'm': |
122 |
|
if (i+1 >= argc) goto userr; |
123 |
+ |
cwarpfile = argv[++i]; |
124 |
+ |
break; |
125 |
+ |
case 'u': |
126 |
+ |
if (i+1 >= argc) goto userr; |
127 |
|
ldmax = atof(argv[++i]); |
128 |
|
if (ldmax <= FTINY) |
129 |
|
goto userr; |
130 |
|
break; |
131 |
< |
case 'b': |
131 |
> |
case 'd': |
132 |
|
if (i+1 >= argc) goto userr; |
133 |
< |
ldmin = atof(argv[++i]); |
133 |
> |
lddyn = atof(argv[++i]); |
134 |
|
break; |
135 |
+ |
case 'x': |
136 |
+ |
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 |
|
default: |
145 |
|
goto userr; |
146 |
|
} |
147 |
< |
if (mbcalfile != NULL & outprims != stdprims) { |
148 |
< |
fprintf(stderr, "%s: only one of -p or -f option supported\n", |
121 |
< |
progname); |
147 |
> |
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 |
< |
if (outprims == stdprims & inprims != stdprims) |
152 |
< |
outprims = inprims; |
153 |
< |
if (ldmin <= FTINY) |
154 |
< |
ldmin = ldmax*LDMINF; |
155 |
< |
else if (ldmin >= ldmax) { |
129 |
< |
fprintf(stderr, "%s: Ldmin (%f) >= Ldmax (%f)!\n", progname, |
130 |
< |
ldmin, ldmax); |
151 |
> |
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 |
> |
progname); |
156 |
|
exit(1); |
157 |
|
} |
158 |
< |
Bldmin = Bl(ldmin); |
158 |
> |
if ((outprims == stdprims) & (inprims != stdprims)) |
159 |
> |
outprims = inprims; |
160 |
> |
Bldmin = Bl(ldmax/lddyn); |
161 |
|
Bldmax = Bl(ldmax); |
162 |
|
if (i >= argc || i+2 < argc) |
163 |
|
goto userr; |
164 |
+ |
/* open input file */ |
165 |
|
if ((infp = fopen(infn=argv[i], "r")) == NULL) |
166 |
|
syserror(infn); |
167 |
+ |
/* open output file */ |
168 |
|
if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL) |
169 |
|
syserror(argv[i+1]); |
170 |
< |
#ifdef MSDOS |
171 |
< |
setmode(fileno(infp), O_BINARY); |
143 |
< |
setmode(fileno(stdout), O_BINARY); |
144 |
< |
#endif |
170 |
> |
SET_FILE_BINARY(infp); |
171 |
> |
SET_FILE_BINARY(stdout); |
172 |
|
getahead(); /* load input header */ |
173 |
|
printargs(argc, argv, stdout); /* add to output header */ |
174 |
< |
if (outprims != inprims) |
174 |
> |
if ((mbcalfile == NULL) & (outprims != stdprims)) |
175 |
|
fputprims(outprims, stdout); |
176 |
+ |
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 |
+ |
getfixations(stdin); |
182 |
|
mapimage(); /* map the picture */ |
183 |
+ |
if (mapfp != NULL) /* write out basic mapping */ |
184 |
+ |
putmapping(mapfp); |
185 |
|
exit(0); |
186 |
|
userr: |
187 |
< |
fprintf(stderr, "Usage: %s [-{h|a|v|s|c|l|w}[+-]][-e ev][-p xr yr xg yg xb yb xw yw|-f mbf.cal][-t Ldmax][-b Ldmin] inpic [outpic]\n", |
187 |
> |
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 |
|
progname); |
189 |
|
exit(1); |
190 |
|
#undef bool |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
< |
headline(s) /* process header line */ |
204 |
< |
char *s; |
203 |
> |
static int |
204 |
> |
headline( /* process header line */ |
205 |
> |
char *s, |
206 |
> |
void *p |
207 |
> |
) |
208 |
|
{ |
209 |
|
static RGBPRIMS inprimS; |
210 |
|
char fmt[32]; |
213 |
|
if (!strcmp(fmt,COLRFMT)) lumf = rgblum; |
214 |
|
else if (!strcmp(fmt,CIEFMT)) lumf = cielum; |
215 |
|
else lumf = NULL; |
216 |
< |
return; /* don't echo */ |
216 |
> |
return(0); /* don't echo */ |
217 |
|
} |
218 |
|
if (isprims(s)) { /* get input primaries */ |
219 |
|
primsval(inprimS, s); |
220 |
|
inprims= inprimS; |
221 |
< |
return; /* don't echo */ |
221 |
> |
return(0); /* don't echo */ |
222 |
|
} |
223 |
|
if (isexpos(s)) { /* picture exposure */ |
224 |
|
inpexp *= exposval(s); |
225 |
< |
return; /* don't echo */ |
225 |
> |
return(0); /* don't echo */ |
226 |
|
} |
227 |
|
if (isaspect(s)) /* pixel aspect ratio */ |
228 |
|
pixaspect *= aspectval(s); |
229 |
|
if (isview(s)) /* image view */ |
230 |
|
gotview += sscanview(&ourview, s); |
231 |
< |
fputs(s, stdout); |
231 |
> |
return(fputs(s, stdout)); |
232 |
|
} |
233 |
|
|
234 |
|
|
243 |
|
exit(1); |
244 |
|
} |
245 |
|
if (lumf == rgblum) |
246 |
< |
comprgb2xyzmat(inrgb2xyz, inprims); |
246 |
> |
comprgb2xyzWBmat(inrgb2xyz, inprims); |
247 |
|
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 |
< |
copystruct(&ourview, &stdview); |
253 |
> |
ourview = stdview; |
254 |
|
ourview.type = VT_PER; |
255 |
|
if (pixaspect*inpres.yr < inpres.xr) { |
256 |
|
ourview.horiz = 40.0; |
274 |
|
{ |
275 |
|
COLOR *scan; |
276 |
|
|
277 |
< |
#ifdef DEBUG |
240 |
< |
fprintf(stderr, "%s: generating histogram...", progname); |
241 |
< |
#endif |
242 |
< |
fovhist(); /* generate adaptation histogram */ |
243 |
< |
#ifdef DEBUG |
244 |
< |
fputs("done\n", stderr); |
245 |
< |
#endif |
277 |
> |
comphist(); /* generate adaptation histogram */ |
278 |
|
check2do(); /* modify what2do flags */ |
279 |
< |
if (what2do&DO_VEIL) { |
280 |
< |
#ifdef DEBUG |
281 |
< |
fprintf(stderr, "%s: computing veiling...", progname); |
279 |
> |
if (what2do&DO_VEIL) |
280 |
> |
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 |
251 |
– |
compveil(); |
252 |
– |
#ifdef DEBUG |
253 |
– |
fputs("done\n", stderr); |
254 |
– |
#endif |
255 |
– |
} |
256 |
– |
#ifdef DEBUG |
257 |
– |
fprintf(stderr, "%s: computing brightness mapping...", progname); |
258 |
– |
#endif |
259 |
– |
if (!(what2do&DO_LINEAR) && mkbrmap() < 0) { /* make tone map */ |
260 |
– |
what2do |= DO_LINEAR; /* use linear scaling */ |
261 |
– |
#ifdef DEBUG |
262 |
– |
fputs("failed!\n", stderr); |
263 |
– |
} else |
264 |
– |
fputs("done\n", stderr); |
265 |
– |
#else |
266 |
– |
} |
267 |
– |
#endif |
288 |
|
if (what2do&DO_LINEAR) { |
289 |
|
if (scalef <= FTINY) { |
290 |
|
if (what2do&DO_HSENS) |
294 |
|
scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg); |
295 |
|
scalef *= WHTEFFICACY/(inpexp*ldmax); |
296 |
|
} |
297 |
< |
#ifdef DEBUG |
278 |
< |
fprintf(stderr, "%s: linear scaling factor = %f\n", |
279 |
< |
progname, scalef); |
280 |
< |
#endif |
281 |
< |
if (scalef < 0.99 | scalef > 1.01) |
282 |
< |
fputexpos(scalef, stdout); /* write in header */ |
297 |
> |
fputexpos(inpexp*scalef, stdout); /* record exposure */ |
298 |
|
if (lumf == cielum) scalef /= WHTEFFICACY; |
299 |
|
} |
300 |
< |
putchar('\n'); /* complete header */ |
300 |
> |
fputformat(COLRFMT, stdout); /* complete header */ |
301 |
> |
putchar('\n'); |
302 |
|
fputsresolu(&inpres, stdout); /* resolution doesn't change */ |
303 |
< |
|
303 |
> |
/* condition our image */ |
304 |
|
for (scan = firstscan(); scan != NULL; scan = nextscan()) |
305 |
|
if (fwritescan(scan, scanlen(&inpres), stdout) < 0) { |
306 |
|
fprintf(stderr, "%s: scanline write error\n", |
310 |
|
} |
311 |
|
|
312 |
|
|
313 |
< |
double |
298 |
< |
centprob(x, y) /* center-weighting probability function */ |
299 |
< |
int x, y; |
313 |
> |
getfovimg() /* load foveal sampled image */ |
314 |
|
{ |
315 |
< |
double xr, yr; |
302 |
< |
|
303 |
< |
xr = (x+.5)/fvxr - .5; |
304 |
< |
yr = (y+.5)/fvyr - .5; |
305 |
< |
return(1. - xr*xr - yr*yr); /* radial, == 0.5 at corners */ |
306 |
< |
} |
307 |
< |
|
308 |
< |
|
309 |
< |
fovhist() /* create foveal sampled image and histogram */ |
310 |
< |
{ |
311 |
< |
extern FILE *popen(); |
312 |
< |
char combuf[128]; |
313 |
< |
double l, b, lwmin, lwmax; |
315 |
> |
char combuf[PATH_MAX]; |
316 |
|
FILE *fp; |
317 |
|
int x, y; |
318 |
< |
|
318 |
> |
/* compute image size */ |
319 |
|
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 |
< |
if (!(inpres.or & YMAJOR)) { /* picture is rotated? */ |
323 |
> |
if (!(inpres.rt & YMAJOR)) { /* picture is rotated? */ |
324 |
|
y = fvyr; |
325 |
|
fvyr = fvxr; |
326 |
|
fvxr = y; |
327 |
|
} |
328 |
|
if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL) |
329 |
|
syserror("malloc"); |
330 |
< |
sprintf(combuf, "pfilt -1 -b -x %d -y %d %s", fvxr, fvyr, infn); |
330 |
> |
sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d \"%s\"", fvxr, fvyr, infn); |
331 |
|
if ((fp = popen(combuf, "r")) == NULL) |
332 |
|
syserror("popen"); |
333 |
|
getheader(fp, NULL, NULL); /* skip header */ |
334 |
< |
if (fgetresolu(&x, &y, fp) < 0 || x != fvxr | y != fvyr) |
334 |
> |
if (fgetresolu(&x, &y, fp) < 0 || (x != fvxr) | (y != fvyr)) |
335 |
|
goto readerr; |
336 |
|
for (y = 0; y < fvyr; y++) |
337 |
|
if (freadscan(fovscan(y), fvxr, fp) < 0) |
338 |
|
goto readerr; |
339 |
|
pclose(fp); |
338 |
– |
lwmin = 1e10; /* find extrema */ |
339 |
– |
lwmax = 0.; |
340 |
– |
for (y = 0; y < fvyr; y++) |
341 |
– |
for (x = 0; x < fvxr; x++) { |
342 |
– |
l = plum(fovscan(y)[x]); |
343 |
– |
if (l < lwmin) lwmin = l; |
344 |
– |
if (l > lwmax) lwmax = l; |
345 |
– |
} |
346 |
– |
if (lwmin < LMIN) lwmin = LMIN; |
347 |
– |
if (lwmax > LMAX) lwmax = LMAX; |
348 |
– |
/* compute histogram */ |
349 |
– |
bwmin = Bl(lwmin)*(1. - .01/HISTRES); |
350 |
– |
bwmax = Bl(lwmax)*(1. + .01/HISTRES); |
351 |
– |
bwavg = 0.; |
352 |
– |
for (y = 0; y < fvyr; y++) |
353 |
– |
for (x = 0; x < fvxr; x++) { |
354 |
– |
if (what2do & DO_CWEIGHT && |
355 |
– |
frandom() > centprob(x,y)) |
356 |
– |
continue; |
357 |
– |
l = plum(fovscan(y)[x]); |
358 |
– |
b = Bl(l); |
359 |
– |
if (b < bwmin) continue; |
360 |
– |
if (b > bwmax) continue; |
361 |
– |
bwavg += b; |
362 |
– |
bwhc(b)++; |
363 |
– |
histot++; |
364 |
– |
} |
365 |
– |
bwavg /= (double)histot; |
340 |
|
return; |
341 |
|
readerr: |
342 |
|
fprintf(stderr, "%s: error reading from pfilt process in fovimage\n", |
347 |
|
|
348 |
|
check2do() /* check histogram to see what isn't worth doing */ |
349 |
|
{ |
350 |
< |
long sum; |
350 |
> |
double sum; |
351 |
|
double b, l; |
352 |
|
register int i; |
353 |
|
|
354 |
|
/* check for within display range */ |
355 |
< |
if (!(what2do&DO_LINEAR) && Lb(bwmax)/Lb(bwmin) <= ldmax/ldmin) |
355 |
> |
if (bwmax - bwmin <= Bldmax - Bldmin) |
356 |
|
what2do |= DO_LINEAR; |
357 |
+ |
/* determine if veiling significant */ |
358 |
+ |
if (bwmax - bwmin < 4.5) /* heuristic */ |
359 |
+ |
what2do &= ~DO_VEIL; |
360 |
|
|
361 |
< |
if (!(what2do & (DO_ACUITY|DO_COLOR|DO_VEIL))) |
361 |
> |
if (!(what2do & (DO_ACUITY|DO_COLOR))) |
362 |
|
return; |
363 |
|
/* find 5th percentile */ |
364 |
< |
sum = histot*0.05 + .5; |
364 |
> |
sum = histot*0.05; |
365 |
|
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); |
393 |
– |
#if 0 |
370 |
|
/* determine if acuity adj. useful */ |
371 |
|
if (what2do&DO_ACUITY && |
372 |
|
hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) + |
373 |
< |
inpres.yr/sqrt(ourview.vn2))/(2.*180./PI*2.)) |
373 |
> |
inpres.yr/sqrt(ourview.vn2))/(2.*180./PI)) |
374 |
|
what2do &= ~DO_ACUITY; |
399 |
– |
#endif |
375 |
|
/* color sensitivity loss? */ |
376 |
< |
if (l >= 6.0) |
376 |
> |
if (l >= TopMesopic) |
377 |
|
what2do &= ~DO_COLOR; |
403 |
– |
if (!(what2do&DO_VEIL)) |
404 |
– |
return; |
405 |
– |
/* find 50th percentile (median) */ |
406 |
– |
sum = histot*0.50 + .5; |
407 |
– |
for (i = 0; i < HISTRES; i++) |
408 |
– |
if ((sum -= bwhist[i]) <= 0) |
409 |
– |
break; |
410 |
– |
/* determine if veiling significant */ |
411 |
– |
b = (i+.5)*(bwmax-bwmin)/HISTRES + bwmin; |
412 |
– |
if ((b-bwmin)/(bwmax-bwmin) >= 0.70) /* heuristic */ |
413 |
– |
what2do &= ~DO_VEIL; |
378 |
|
} |