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