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 |
|
|
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; |
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][-m mapfile] 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 |
252 |
< |
fprintf(stderr, "%s: generating histogram...", progname); |
253 |
< |
#endif |
254 |
< |
fovhist(); /* generate adaptation histogram */ |
255 |
< |
#ifdef DEBUG |
256 |
< |
fputs("done\n", stderr); |
257 |
< |
#endif |
263 |
> |
comphist(); /* generate adaptation histogram */ |
264 |
|
check2do(); /* modify what2do flags */ |
265 |
< |
if (what2do&DO_VEIL) { |
260 |
< |
#ifdef DEBUG |
261 |
< |
fprintf(stderr, "%s: computing veiling...", progname); |
262 |
< |
#endif |
265 |
> |
if (what2do&DO_VEIL) |
266 |
|
compveil(); |
267 |
< |
#ifdef DEBUG |
268 |
< |
fputs("done\n", stderr); |
266 |
< |
#endif |
267 |
< |
} |
268 |
< |
#ifdef DEBUG |
269 |
< |
fprintf(stderr, "%s: computing brightness mapping...", progname); |
270 |
< |
#endif |
271 |
< |
if (!(what2do&DO_LINEAR) && mkbrmap() < 0) { /* make tone map */ |
272 |
< |
what2do |= DO_LINEAR; /* use linear scaling */ |
273 |
< |
#ifdef DEBUG |
274 |
< |
fputs("failed!\n", stderr); |
275 |
< |
} else |
276 |
< |
fputs("done\n", stderr); |
277 |
< |
#else |
278 |
< |
} |
279 |
< |
#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 |
|
} |
289 |
– |
#ifdef DEBUG |
290 |
– |
fprintf(stderr, "%s: linear scaling factor = %f\n", |
291 |
– |
progname, scalef); |
292 |
– |
#endif |
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 |
309 |
< |
centprob(x, y) /* center-weighting probability function */ |
310 |
< |
int x, y; |
294 |
> |
getfovimg() /* load foveal sampled image */ |
295 |
|
{ |
312 |
– |
double xr, yr; |
313 |
– |
|
314 |
– |
xr = (x+.5)/fvxr - .5; |
315 |
– |
yr = (y+.5)/fvyr - .5; |
316 |
– |
return(1. - xr*xr - yr*yr); /* radial, == 0.5 at corners */ |
317 |
– |
} |
318 |
– |
|
319 |
– |
|
320 |
– |
fovhist() /* create foveal sampled image and histogram */ |
321 |
– |
{ |
296 |
|
extern FILE *popen(); |
297 |
|
char combuf[128]; |
324 |
– |
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); |
349 |
– |
lwmin = 1e10; /* find extrema */ |
350 |
– |
lwmax = 0.; |
351 |
– |
for (y = 0; y < fvyr; y++) |
352 |
– |
for (x = 0; x < fvxr; x++) { |
353 |
– |
l = plum(fovscan(y)[x]); |
354 |
– |
if (l < lwmin) lwmin = l; |
355 |
– |
if (l > lwmax) lwmax = l; |
356 |
– |
} |
357 |
– |
if (lwmin < LMIN) lwmin = LMIN; |
358 |
– |
if (lwmax > LMAX) lwmax = LMAX; |
359 |
– |
/* compute histogram */ |
360 |
– |
bwmin = Bl(lwmin); |
361 |
– |
bwmax = Bl(lwmax); |
362 |
– |
bwavg = 0.; |
363 |
– |
for (y = 0; y < fvyr; y++) |
364 |
– |
for (x = 0; x < fvxr; x++) { |
365 |
– |
if (what2do & DO_CWEIGHT && |
366 |
– |
frandom() > centprob(x,y)) |
367 |
– |
continue; |
368 |
– |
l = plum(fovscan(y)[x]); |
369 |
– |
if (l < lwmin) continue; |
370 |
– |
if (l > lwmax) continue; |
371 |
– |
b = Bl(l); |
372 |
– |
bwavg += b; |
373 |
– |
bwhc(b)++; |
374 |
– |
histot++; |
375 |
– |
} |
376 |
– |
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 |
< |
l = Lb(bwmax)/Lb(bwmin); |
393 |
< |
if (l <= ldmax/ldmin) |
337 |
> |
if (bwmax - bwmin <= Bldmax - Bldmin) |
338 |
|
what2do |= DO_LINEAR; |
339 |
|
/* determine if veiling significant */ |
340 |
< |
if (l < 100.) /* heuristic */ |
340 |
> |
if (bwmax - bwmin < 4.5) /* heuristic */ |
341 |
|
what2do &= ~DO_VEIL; |
342 |
|
|
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; |