1 |
< |
/* Copyright (c) 1996 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1997 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
10 |
|
|
11 |
|
#include "pcond.h" |
12 |
|
|
13 |
– |
#include "random.h" |
13 |
|
|
15 |
– |
|
14 |
|
#define LDMAX 100 /* default max. display luminance */ |
15 |
|
#define LDMINF 0.01 /* default min. display lum. factor */ |
16 |
|
|
24 |
|
|
25 |
|
char *infn; /* input file name */ |
26 |
|
FILE *infp; /* input stream */ |
27 |
+ |
FILE *mapfp = NULL; /* tone-mapping function stream */ |
28 |
|
VIEW ourview = STDVIEW; /* picture view */ |
29 |
|
int gotview = 0; /* picture has view */ |
30 |
|
double pixaspect = 1.0; /* pixel aspect ratio */ |
31 |
+ |
double fixfrac = 0.; /* histogram share due to fixations */ |
32 |
|
RESOLU inpres; /* input picture resolution */ |
33 |
|
|
34 |
|
COLOR *fovimg; /* foveal (1 degree) averaged image */ |
35 |
|
short fvxr, fvyr; /* foveal image resolution */ |
36 |
< |
int bwhist[HISTRES]; /* luminance histogram */ |
37 |
< |
long histot; /* total count of histogram */ |
36 |
> |
short (*fixlst)[2]; /* fixation history list */ |
37 |
> |
int nfixations; /* number of fixation points */ |
38 |
> |
float 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 'l': |
89 |
|
bool(DO_LINEAR); |
90 |
|
break; |
121 |
|
if (i+1 >= argc) goto userr; |
122 |
|
ldmin = atof(argv[++i]); |
123 |
|
break; |
124 |
+ |
case 'm': |
125 |
+ |
if (i+1 >= argc) goto userr; |
126 |
+ |
if ((mapfp = fopen(argv[++i], "w")) == NULL) { |
127 |
+ |
fprintf(stderr, |
128 |
+ |
"%s: cannot open for writing\n", |
129 |
+ |
argv[i]); |
130 |
+ |
exit(1); |
131 |
+ |
} |
132 |
+ |
break; |
133 |
|
default: |
134 |
|
goto userr; |
135 |
|
} |
151 |
|
Bldmax = Bl(ldmax); |
152 |
|
if (i >= argc || i+2 < argc) |
153 |
|
goto userr; |
154 |
+ |
/* open input file */ |
155 |
|
if ((infp = fopen(infn=argv[i], "r")) == NULL) |
156 |
|
syserror(infn); |
157 |
+ |
/* open output file */ |
158 |
|
if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL) |
159 |
|
syserror(argv[i+1]); |
160 |
|
#ifdef MSDOS |
163 |
|
#endif |
164 |
|
getahead(); /* load input header */ |
165 |
|
printargs(argc, argv, stdout); /* add to output header */ |
166 |
< |
if (outprims != inprims) |
166 |
> |
if (mbcalfile == NULL & outprims != stdprims) |
167 |
|
fputprims(outprims, stdout); |
168 |
+ |
getfovimg(); /* get foveal sample image */ |
169 |
+ |
if (what2do&DO_FIXHIST) /* get fixation history? */ |
170 |
+ |
getfixations(stdin); |
171 |
|
mapimage(); /* map the picture */ |
172 |
+ |
if (mapfp != NULL) /* write out basic mapping */ |
173 |
+ |
putmapping(mapfp); |
174 |
|
exit(0); |
175 |
|
userr: |
176 |
< |
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", |
176 |
> |
fprintf(stderr, "Usage: %s [-{h|a|v|s|c|l|w}[+-]][-i ffrac][-e ev][-p xr yr xg yg xb yb xw yw|-f mbf.cal][-t Ldmax][-b Ldmin][-m mapfile] inpic [outpic]\n", |
177 |
|
progname); |
178 |
|
exit(1); |
179 |
|
#undef bool |
260 |
|
{ |
261 |
|
COLOR *scan; |
262 |
|
|
263 |
< |
#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 |
263 |
> |
comphist(); /* generate adaptation histogram */ |
264 |
|
check2do(); /* modify what2do flags */ |
265 |
< |
if (what2do&DO_VEIL) { |
248 |
< |
#ifdef DEBUG |
249 |
< |
fprintf(stderr, "%s: computing veiling...", progname); |
250 |
< |
#endif |
265 |
> |
if (what2do&DO_VEIL) |
266 |
|
compveil(); |
267 |
< |
#ifdef DEBUG |
268 |
< |
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 |
267 |
> |
if (!(what2do&DO_LINEAR) && mkbrmap() < 0) /* make tone map */ |
268 |
> |
what2do |= DO_LINEAR; /* failed! -- use linear scaling */ |
269 |
|
if (what2do&DO_LINEAR) { |
270 |
|
if (scalef <= FTINY) { |
271 |
|
if (what2do&DO_HSENS) |
275 |
|
scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg); |
276 |
|
scalef *= WHTEFFICACY/(inpexp*ldmax); |
277 |
|
} |
278 |
< |
#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 */ |
278 |
> |
fputexpos(inpexp*scalef, stdout); /* record exposure */ |
279 |
|
if (lumf == cielum) scalef /= WHTEFFICACY; |
280 |
|
} |
281 |
< |
putchar('\n'); /* complete header */ |
281 |
> |
fputformat(COLRFMT, stdout); /* complete header */ |
282 |
> |
putchar('\n'); |
283 |
|
fputsresolu(&inpres, stdout); /* resolution doesn't change */ |
284 |
< |
|
284 |
> |
/* condition our image */ |
285 |
|
for (scan = firstscan(); scan != NULL; scan = nextscan()) |
286 |
|
if (fwritescan(scan, scanlen(&inpres), stdout) < 0) { |
287 |
|
fprintf(stderr, "%s: scanline write error\n", |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
< |
double |
298 |
< |
centprob(x, y) /* center-weighting probability function */ |
299 |
< |
int x, y; |
294 |
> |
getfovimg() /* load foveal sampled image */ |
295 |
|
{ |
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 |
– |
{ |
296 |
|
extern FILE *popen(); |
297 |
|
char combuf[128]; |
313 |
– |
double l, b, lwmin, lwmax; |
298 |
|
FILE *fp; |
299 |
|
int x, y; |
300 |
< |
|
300 |
> |
/* compute image size */ |
301 |
|
fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5; |
302 |
|
if (fvxr < 2) fvxr = 2; |
303 |
|
fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5; |
309 |
|
} |
310 |
|
if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL) |
311 |
|
syserror("malloc"); |
312 |
< |
sprintf(combuf, "pfilt -1 -b -x %d -y %d %s", fvxr, fvyr, infn); |
312 |
> |
sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d %s", fvxr, fvyr, infn); |
313 |
|
if ((fp = popen(combuf, "r")) == NULL) |
314 |
|
syserror("popen"); |
315 |
|
getheader(fp, NULL, NULL); /* skip header */ |
319 |
|
if (freadscan(fovscan(y), fvxr, fp) < 0) |
320 |
|
goto readerr; |
321 |
|
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; |
322 |
|
return; |
323 |
|
readerr: |
324 |
|
fprintf(stderr, "%s: error reading from pfilt process in fovimage\n", |
329 |
|
|
330 |
|
check2do() /* check histogram to see what isn't worth doing */ |
331 |
|
{ |
332 |
< |
long sum; |
332 |
> |
double sum; |
333 |
|
double b, l; |
334 |
|
register int i; |
335 |
|
|
336 |
|
/* check for within display range */ |
337 |
< |
if (!(what2do&DO_LINEAR) && Lb(bwmax)/Lb(bwmin) <= ldmax/ldmin) |
337 |
> |
if (bwmax - bwmin <= Bldmax - Bldmin) |
338 |
|
what2do |= DO_LINEAR; |
339 |
+ |
/* determine if veiling significant */ |
340 |
+ |
if (bwmax - bwmin < 4.5) /* heuristic */ |
341 |
+ |
what2do &= ~DO_VEIL; |
342 |
|
|
343 |
< |
if (!(what2do & (DO_ACUITY|DO_COLOR|DO_VEIL))) |
343 |
> |
if (!(what2do & (DO_ACUITY|DO_COLOR))) |
344 |
|
return; |
345 |
|
/* find 5th percentile */ |
346 |
< |
sum = histot*0.05 + .5; |
346 |
> |
sum = histot*0.05; |
347 |
|
for (i = 0; i < HISTRES; i++) |
348 |
|
if ((sum -= bwhist[i]) <= 0) |
349 |
|
break; |
350 |
|
b = (i+.5)*(bwmax-bwmin)/HISTRES + bwmin; |
351 |
|
l = Lb(b); |
393 |
– |
#if 0 |
352 |
|
/* determine if acuity adj. useful */ |
353 |
|
if (what2do&DO_ACUITY && |
354 |
|
hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) + |
355 |
< |
inpres.yr/sqrt(ourview.vn2))/(2.*180./PI*2.)) |
355 |
> |
inpres.yr/sqrt(ourview.vn2))/(2.*180./PI)) |
356 |
|
what2do &= ~DO_ACUITY; |
399 |
– |
#endif |
357 |
|
/* color sensitivity loss? */ |
358 |
< |
if (l >= 6.0) |
358 |
> |
if (l >= TopMesopic) |
359 |
|
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; |
360 |
|
} |