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 "pcond.h" |
11 |
|
|
13 |
– |
#include "random.h" |
12 |
|
|
15 |
– |
|
13 |
|
#define LDMAX 100 /* default max. display luminance */ |
14 |
< |
#define LDMINF 0.01 /* default min. display lum. factor */ |
14 |
> |
#define LDDYN 32 /* default dynamic range */ |
15 |
|
|
16 |
|
int what2do = 0; /* desired adjustments */ |
17 |
|
|
18 |
|
double ldmax = LDMAX; /* maximum output luminance */ |
19 |
< |
double ldmin = 0.; /* minimum output luminance */ |
20 |
< |
double Bldmin, Bldmax; /* Bl(ldmin) and Bl(ldmax) */ |
19 |
> |
double lddyn = LDDYN; /* display dynamic range */ |
20 |
> |
double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */ |
21 |
|
|
22 |
|
char *progname; /* global argv[0] */ |
23 |
|
|
24 |
|
char *infn; /* input file name */ |
25 |
|
FILE *infp; /* input stream */ |
26 |
+ |
FILE *mapfp = NULL; /* tone-mapping function stream */ |
27 |
|
VIEW ourview = STDVIEW; /* picture view */ |
28 |
|
int gotview = 0; /* picture has view */ |
29 |
|
double pixaspect = 1.0; /* pixel aspect ratio */ |
30 |
+ |
double fixfrac = 0.; /* histogram share due to fixations */ |
31 |
|
RESOLU inpres; /* input picture resolution */ |
32 |
|
|
33 |
|
COLOR *fovimg; /* foveal (1 degree) averaged image */ |
34 |
< |
short fvxr, fvyr; /* foveal image resolution */ |
35 |
< |
int bwhist[HISTRES]; /* luminance histogram */ |
36 |
< |
long histot; /* total count of histogram */ |
34 |
> |
int fvxr, fvyr; /* foveal image resolution */ |
35 |
> |
float *crfimg; /* contrast reduction factors */ |
36 |
> |
short (*fixlst)[2]; /* fixation history list */ |
37 |
> |
int nfixations; /* number of fixation points */ |
38 |
> |
double bwhist[HISTRES]; /* luminance histogram */ |
39 |
> |
double histot; /* total count of histogram */ |
40 |
|
double bwmin, bwmax; /* histogram limits */ |
41 |
|
double bwavg; /* mean brightness */ |
42 |
|
|
79 |
|
case 'w': |
80 |
|
bool(DO_CWEIGHT); |
81 |
|
break; |
82 |
+ |
case 'i': |
83 |
+ |
if (i+1 >= argc) goto userr; |
84 |
+ |
fixfrac = atof(argv[++i]); |
85 |
+ |
if (fixfrac > FTINY) what2do |= DO_FIXHIST; |
86 |
+ |
else what2do &= ~DO_FIXHIST; |
87 |
+ |
break; |
88 |
+ |
case 'I': |
89 |
+ |
bool(DO_PREHIST); |
90 |
+ |
break; |
91 |
|
case 'l': |
92 |
|
bool(DO_LINEAR); |
93 |
|
break; |
106 |
|
case 'e': |
107 |
|
if (i+1 >= argc) goto userr; |
108 |
|
scalef = atof(argv[++i]); |
109 |
< |
if (argv[i][0] == '+' | argv[i][0] == '-') |
109 |
> |
if ((argv[i][0] == '+') | (argv[i][0] == '-')) |
110 |
|
scalef = pow(2.0, scalef); |
111 |
|
what2do |= DO_LINEAR; |
112 |
|
break; |
114 |
|
if (i+1 >= argc) goto userr; |
115 |
|
mbcalfile = argv[++i]; |
116 |
|
break; |
117 |
< |
case 't': |
117 |
> |
case 'm': |
118 |
|
if (i+1 >= argc) goto userr; |
119 |
+ |
cwarpfile = argv[++i]; |
120 |
+ |
break; |
121 |
+ |
case 'u': |
122 |
+ |
if (i+1 >= argc) goto userr; |
123 |
|
ldmax = atof(argv[++i]); |
124 |
|
if (ldmax <= FTINY) |
125 |
|
goto userr; |
126 |
|
break; |
127 |
< |
case 'b': |
127 |
> |
case 'd': |
128 |
|
if (i+1 >= argc) goto userr; |
129 |
< |
ldmin = atof(argv[++i]); |
129 |
> |
lddyn = atof(argv[++i]); |
130 |
|
break; |
131 |
+ |
case 'x': |
132 |
+ |
if (i+1 >= argc) goto userr; |
133 |
+ |
if ((mapfp = fopen(argv[++i], "w")) == NULL) { |
134 |
+ |
fprintf(stderr, |
135 |
+ |
"%s: cannot open for writing\n", |
136 |
+ |
argv[i]); |
137 |
+ |
exit(1); |
138 |
+ |
} |
139 |
+ |
break; |
140 |
|
default: |
141 |
|
goto userr; |
142 |
|
} |
143 |
< |
if (mbcalfile != NULL & outprims != stdprims) { |
144 |
< |
fprintf(stderr, "%s: only one of -p or -f option supported\n", |
121 |
< |
progname); |
143 |
> |
if ((what2do & (DO_FIXHIST|DO_PREHIST)) == (DO_FIXHIST|DO_PREHIST)) { |
144 |
> |
fprintf(stderr, "%s: only one of -i or -I option\n", progname); |
145 |
|
exit(1); |
146 |
|
} |
147 |
< |
if (outprims == stdprims & inprims != stdprims) |
148 |
< |
outprims = inprims; |
149 |
< |
if (ldmin <= FTINY) |
150 |
< |
ldmin = ldmax*LDMINF; |
151 |
< |
else if (ldmin >= ldmax) { |
129 |
< |
fprintf(stderr, "%s: Ldmin (%f) >= Ldmax (%f)!\n", progname, |
130 |
< |
ldmin, ldmax); |
147 |
> |
if ((mbcalfile != NULL) + (cwarpfile != NULL) + |
148 |
> |
(outprims != stdprims) > 1) { |
149 |
> |
fprintf(stderr, |
150 |
> |
"%s: only one of -p, -m or -f option supported\n", |
151 |
> |
progname); |
152 |
|
exit(1); |
153 |
|
} |
154 |
< |
Bldmin = Bl(ldmin); |
154 |
> |
if ((outprims == stdprims) & (inprims != stdprims)) |
155 |
> |
outprims = inprims; |
156 |
> |
Bldmin = Bl(ldmax/lddyn); |
157 |
|
Bldmax = Bl(ldmax); |
158 |
|
if (i >= argc || i+2 < argc) |
159 |
|
goto userr; |
160 |
+ |
/* open input file */ |
161 |
|
if ((infp = fopen(infn=argv[i], "r")) == NULL) |
162 |
|
syserror(infn); |
163 |
+ |
/* open output file */ |
164 |
|
if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL) |
165 |
|
syserror(argv[i+1]); |
166 |
< |
#ifdef MSDOS |
167 |
< |
setmode(fileno(infp), O_BINARY); |
143 |
< |
setmode(fileno(stdout), O_BINARY); |
144 |
< |
#endif |
166 |
> |
SET_FILE_BINARY(infp); |
167 |
> |
SET_FILE_BINARY(stdout); |
168 |
|
getahead(); /* load input header */ |
169 |
|
printargs(argc, argv, stdout); /* add to output header */ |
170 |
< |
if (outprims != inprims) |
170 |
> |
if ((mbcalfile == NULL) & (outprims != stdprims)) |
171 |
|
fputprims(outprims, stdout); |
172 |
+ |
if ((what2do & (DO_PREHIST|DO_VEIL|DO_ACUITY)) != DO_PREHIST) |
173 |
+ |
getfovimg(); /* get foveal sample image? */ |
174 |
+ |
if (what2do&DO_PREHIST) /* get histogram? */ |
175 |
+ |
gethisto(stdin); |
176 |
+ |
else if (what2do&DO_FIXHIST) /* get fixation history? */ |
177 |
+ |
getfixations(stdin); |
178 |
|
mapimage(); /* map the picture */ |
179 |
+ |
if (mapfp != NULL) /* write out basic mapping */ |
180 |
+ |
putmapping(mapfp); |
181 |
|
exit(0); |
182 |
|
userr: |
183 |
< |
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", |
183 |
> |
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", |
184 |
|
progname); |
185 |
|
exit(1); |
186 |
|
#undef bool |
206 |
|
if (!strcmp(fmt,COLRFMT)) lumf = rgblum; |
207 |
|
else if (!strcmp(fmt,CIEFMT)) lumf = cielum; |
208 |
|
else lumf = NULL; |
209 |
< |
return; /* don't echo */ |
209 |
> |
return(0); /* don't echo */ |
210 |
|
} |
211 |
|
if (isprims(s)) { /* get input primaries */ |
212 |
|
primsval(inprimS, s); |
213 |
|
inprims= inprimS; |
214 |
< |
return; /* don't echo */ |
214 |
> |
return(0); /* don't echo */ |
215 |
|
} |
216 |
|
if (isexpos(s)) { /* picture exposure */ |
217 |
|
inpexp *= exposval(s); |
218 |
< |
return; /* don't echo */ |
218 |
> |
return(0); /* don't echo */ |
219 |
|
} |
220 |
|
if (isaspect(s)) /* pixel aspect ratio */ |
221 |
|
pixaspect *= aspectval(s); |
222 |
|
if (isview(s)) /* image view */ |
223 |
|
gotview += sscanview(&ourview, s); |
224 |
< |
fputs(s, stdout); |
224 |
> |
return(fputs(s, stdout)); |
225 |
|
} |
226 |
|
|
227 |
|
|
236 |
|
exit(1); |
237 |
|
} |
238 |
|
if (lumf == rgblum) |
239 |
< |
comprgb2xyzmat(inrgb2xyz, inprims); |
239 |
> |
comprgb2xyzWBmat(inrgb2xyz, inprims); |
240 |
|
else if (mbcalfile != NULL) { |
241 |
|
fprintf(stderr, "%s: macbethcal only works with RGB pictures\n", |
242 |
|
progname); |
243 |
|
exit(1); |
244 |
|
} |
245 |
|
if (!gotview || ourview.type == VT_PAR) { |
246 |
< |
copystruct(&ourview, &stdview); |
246 |
> |
ourview = stdview; |
247 |
|
ourview.type = VT_PER; |
248 |
|
if (pixaspect*inpres.yr < inpres.xr) { |
249 |
|
ourview.horiz = 40.0; |
267 |
|
{ |
268 |
|
COLOR *scan; |
269 |
|
|
270 |
< |
#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 |
270 |
> |
comphist(); /* generate adaptation histogram */ |
271 |
|
check2do(); /* modify what2do flags */ |
272 |
< |
if (what2do&DO_VEIL) { |
273 |
< |
#ifdef DEBUG |
274 |
< |
fprintf(stderr, "%s: computing veiling...", progname); |
272 |
> |
if (what2do&DO_VEIL) |
273 |
> |
compveil(); /* compute veil image */ |
274 |
> |
if (!(what2do&DO_LINEAR)) |
275 |
> |
if (mkbrmap() < 0) /* make tone map */ |
276 |
> |
what2do |= DO_LINEAR; /* failed! -- use linear */ |
277 |
> |
#if ADJ_VEIL |
278 |
> |
else if (what2do&DO_VEIL) |
279 |
> |
adjveil(); /* else adjust veil image */ |
280 |
|
#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 |
281 |
|
if (what2do&DO_LINEAR) { |
282 |
|
if (scalef <= FTINY) { |
283 |
|
if (what2do&DO_HSENS) |
287 |
|
scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg); |
288 |
|
scalef *= WHTEFFICACY/(inpexp*ldmax); |
289 |
|
} |
290 |
< |
#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 */ |
290 |
> |
fputexpos(inpexp*scalef, stdout); /* record exposure */ |
291 |
|
if (lumf == cielum) scalef /= WHTEFFICACY; |
292 |
|
} |
293 |
< |
putchar('\n'); /* complete header */ |
293 |
> |
fputformat(COLRFMT, stdout); /* complete header */ |
294 |
> |
putchar('\n'); |
295 |
|
fputsresolu(&inpres, stdout); /* resolution doesn't change */ |
296 |
< |
|
296 |
> |
/* condition our image */ |
297 |
|
for (scan = firstscan(); scan != NULL; scan = nextscan()) |
298 |
|
if (fwritescan(scan, scanlen(&inpres), stdout) < 0) { |
299 |
|
fprintf(stderr, "%s: scanline write error\n", |
303 |
|
} |
304 |
|
|
305 |
|
|
306 |
< |
double |
298 |
< |
centprob(x, y) /* center-weighting probability function */ |
299 |
< |
int x, y; |
306 |
> |
getfovimg() /* load foveal sampled image */ |
307 |
|
{ |
301 |
– |
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 |
– |
{ |
308 |
|
extern FILE *popen(); |
309 |
|
char combuf[128]; |
313 |
– |
double l, b, lwmin, lwmax; |
310 |
|
FILE *fp; |
311 |
|
int x, y; |
312 |
< |
|
312 |
> |
/* compute image size */ |
313 |
|
fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5; |
314 |
|
if (fvxr < 2) fvxr = 2; |
315 |
|
fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5; |
316 |
|
if (fvyr < 2) fvyr = 2; |
317 |
< |
if (!(inpres.or & YMAJOR)) { /* picture is rotated? */ |
317 |
> |
if (!(inpres.rt & YMAJOR)) { /* picture is rotated? */ |
318 |
|
y = fvyr; |
319 |
|
fvyr = fvxr; |
320 |
|
fvxr = y; |
321 |
|
} |
322 |
|
if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL) |
323 |
|
syserror("malloc"); |
324 |
< |
sprintf(combuf, "pfilt -1 -b -x %d -y %d %s", fvxr, fvyr, infn); |
324 |
> |
sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d %s", fvxr, fvyr, infn); |
325 |
|
if ((fp = popen(combuf, "r")) == NULL) |
326 |
|
syserror("popen"); |
327 |
|
getheader(fp, NULL, NULL); /* skip header */ |
328 |
< |
if (fgetresolu(&x, &y, fp) < 0 || x != fvxr | y != fvyr) |
328 |
> |
if (fgetresolu(&x, &y, fp) < 0 || (x != fvxr) | (y != fvyr)) |
329 |
|
goto readerr; |
330 |
|
for (y = 0; y < fvyr; y++) |
331 |
|
if (freadscan(fovscan(y), fvxr, fp) < 0) |
332 |
|
goto readerr; |
333 |
|
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; |
334 |
|
return; |
335 |
|
readerr: |
336 |
|
fprintf(stderr, "%s: error reading from pfilt process in fovimage\n", |
341 |
|
|
342 |
|
check2do() /* check histogram to see what isn't worth doing */ |
343 |
|
{ |
344 |
< |
long sum; |
344 |
> |
double sum; |
345 |
|
double b, l; |
346 |
|
register int i; |
347 |
|
|
348 |
|
/* check for within display range */ |
349 |
< |
if (!(what2do&DO_LINEAR) && Lb(bwmax)/Lb(bwmin) <= ldmax/ldmin) |
349 |
> |
if (bwmax - bwmin <= Bldmax - Bldmin) |
350 |
|
what2do |= DO_LINEAR; |
351 |
+ |
/* determine if veiling significant */ |
352 |
+ |
if (bwmax - bwmin < 4.5) /* heuristic */ |
353 |
+ |
what2do &= ~DO_VEIL; |
354 |
|
|
355 |
< |
if (!(what2do & (DO_ACUITY|DO_COLOR|DO_VEIL))) |
355 |
> |
if (!(what2do & (DO_ACUITY|DO_COLOR))) |
356 |
|
return; |
357 |
|
/* find 5th percentile */ |
358 |
< |
sum = histot*0.05 + .5; |
358 |
> |
sum = histot*0.05; |
359 |
|
for (i = 0; i < HISTRES; i++) |
360 |
|
if ((sum -= bwhist[i]) <= 0) |
361 |
|
break; |
364 |
|
/* determine if acuity adj. useful */ |
365 |
|
if (what2do&DO_ACUITY && |
366 |
|
hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) + |
367 |
< |
inpres.yr/sqrt(ourview.vn2))/(2.*180./PI*2.)) |
367 |
> |
inpres.yr/sqrt(ourview.vn2))/(2.*180./PI)) |
368 |
|
what2do &= ~DO_ACUITY; |
369 |
|
/* color sensitivity loss? */ |
370 |
< |
if (l >= 6.0) |
370 |
> |
if (l >= TopMesopic) |
371 |
|
what2do &= ~DO_COLOR; |
401 |
– |
if (!(what2do&DO_VEIL)) |
402 |
– |
return; |
403 |
– |
/* find 50th percentile (median) */ |
404 |
– |
sum = histot*0.50 + .5; |
405 |
– |
for (i = 0; i < HISTRES; i++) |
406 |
– |
if ((sum -= bwhist[i]) <= 0) |
407 |
– |
break; |
408 |
– |
/* determine if veiling significant */ |
409 |
– |
b = (i+.5)*(bwmax-bwmin)/HISTRES + bwmin; |
410 |
– |
if ((b-bwmin)/(bwmax-bwmin) >= 0.70) /* heuristic */ |
411 |
– |
what2do &= ~DO_VEIL; |
372 |
|
} |