1 |
– |
/* Copyright (c) 1997 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 |
|
* Routines for computing and applying brightness mapping. |
6 |
|
*/ |
10 |
|
|
11 |
|
#define CVRATIO 0.025 /* fraction of samples allowed > env. */ |
12 |
|
|
13 |
< |
#define exp10(x) exp(2.302585093*(x)) |
13 |
> |
#define LN_10 2.30258509299404568402 |
14 |
> |
#define exp10(x) exp(LN_10*(x)) |
15 |
|
|
16 |
< |
float modhist[HISTRES]; /* modified histogram */ |
16 |
> |
double modhist[HISTRES]; /* modified histogram */ |
17 |
|
double mhistot; /* modified histogram total */ |
18 |
< |
float cumf[HISTRES+1]; /* cumulative distribution function */ |
18 |
> |
double cumf[HISTRES+1]; /* cumulative distribution function */ |
19 |
|
|
20 |
|
|
21 |
|
getfixations(fp) /* load fixation history list */ |
26 |
|
int pos[2]; |
27 |
|
register int px, py, i; |
28 |
|
/* initialize our resolution struct */ |
29 |
< |
if ((fvres.or=inpres.or)&YMAJOR) { |
29 |
> |
if ((fvres.rt=inpres.rt)&YMAJOR) { |
30 |
|
fvres.xr = fvxr; |
31 |
|
fvres.yr = fvyr; |
32 |
|
} else { |
54 |
|
if (nfixations % FIXHUNK == 0) { |
55 |
|
if (nfixations) |
56 |
|
fixlst = (short (*)[2]) |
57 |
< |
realloc((char *)fixlst, |
57 |
> |
realloc((void *)fixlst, |
58 |
|
(nfixations+FIXHUNK)* |
59 |
|
2*sizeof(short)); |
60 |
|
else |
79 |
|
} |
80 |
|
|
81 |
|
|
82 |
+ |
gethisto(fp) /* load precomputed luminance histogram */ |
83 |
+ |
FILE *fp; |
84 |
+ |
{ |
85 |
+ |
double histo[MAXPREHIST]; |
86 |
+ |
double histart, histep; |
87 |
+ |
double l, b, lastb, w; |
88 |
+ |
int n; |
89 |
+ |
register int i; |
90 |
+ |
/* load data */ |
91 |
+ |
for (i = 0; i < MAXPREHIST && |
92 |
+ |
fscanf(fp, "%lf %lf", &b, &histo[i]) == 2; i++) { |
93 |
+ |
if (i > 1 && fabs(b - lastb - histep) > .001) { |
94 |
+ |
fprintf(stderr, |
95 |
+ |
"%s: uneven step size in histogram data\n", |
96 |
+ |
progname); |
97 |
+ |
exit(1); |
98 |
+ |
} |
99 |
+ |
if (i == 1) |
100 |
+ |
if ((histep = b - (histart = lastb)) <= FTINY) { |
101 |
+ |
fprintf(stderr, |
102 |
+ |
"%s: illegal step in histogram data\n", |
103 |
+ |
progname); |
104 |
+ |
exit(1); |
105 |
+ |
} |
106 |
+ |
lastb = b; |
107 |
+ |
} |
108 |
+ |
if (i < 2 || !feof(fp)) { |
109 |
+ |
fprintf(stderr, |
110 |
+ |
"%s: format/length error loading histogram (log10L %f at %d)\n", |
111 |
+ |
progname, b, i); |
112 |
+ |
exit(1); |
113 |
+ |
} |
114 |
+ |
n = i; |
115 |
+ |
histart *= LN_10; |
116 |
+ |
histep *= LN_10; |
117 |
+ |
/* find extrema */ |
118 |
+ |
for (i = 0; i < n && histo[i] <= FTINY; i++) |
119 |
+ |
; |
120 |
+ |
bwmin = histart + (i-.001)*histep; |
121 |
+ |
for (i = n; i-- && histo[i] <= FTINY; ) |
122 |
+ |
; |
123 |
+ |
bwmax = histart + (i+1.001)*histep; |
124 |
+ |
if (bwmax > Bl(LMAX)) |
125 |
+ |
bwmax = Bl(LMAX); |
126 |
+ |
if (bwmin < Bl(LMIN)) |
127 |
+ |
bwmin = Bl(LMIN); |
128 |
+ |
else /* duplicate bottom bin */ |
129 |
+ |
bwmin = bwmax - (bwmax-bwmin)*HISTRES/(HISTRES-1); |
130 |
+ |
/* convert histogram */ |
131 |
+ |
bwavg = 0.; histot = 0.; |
132 |
+ |
for (i = 0; i < HISTRES; i++) |
133 |
+ |
bwhist[i] = 0.; |
134 |
+ |
for (i = 0, b = histart; i < n; i++, b += histep) { |
135 |
+ |
if (b < bwmin+FTINY) continue; |
136 |
+ |
if (b >= bwmax-FTINY) break; |
137 |
+ |
w = histo[i]; |
138 |
+ |
bwavg += w*b; |
139 |
+ |
bwhist[bwhi(b)] += w; |
140 |
+ |
histot += w; |
141 |
+ |
} |
142 |
+ |
bwavg /= histot; |
143 |
+ |
if (bwmin > Bl(LMIN)+FTINY) { /* add false samples at bottom */ |
144 |
+ |
bwhist[1] *= 0.5; |
145 |
+ |
bwhist[0] += bwhist[1]; |
146 |
+ |
} |
147 |
+ |
} |
148 |
+ |
|
149 |
+ |
|
150 |
|
double |
151 |
|
centprob(x, y) /* center-weighting probability function */ |
152 |
|
int x, y; |
164 |
|
{ |
165 |
|
double l, b, w, lwmin, lwmax; |
166 |
|
register int x, y; |
167 |
< |
|
167 |
> |
/* check for precalculated histogram */ |
168 |
> |
if (what2do&DO_PREHIST) |
169 |
> |
return; |
170 |
|
lwmin = 1e10; /* find extrema */ |
171 |
|
lwmax = 0.; |
172 |
|
for (y = 0; y < fvyr; y++) |
196 |
|
for (y = 0; y < fvyr; y++) |
197 |
|
for (x = 0; x < fvxr; x++) { |
198 |
|
l = plum(fovscan(y)[x]); |
199 |
< |
if (l < lwmin) continue; |
200 |
< |
if (l > lwmax) continue; |
199 |
> |
if (l < lwmin+FTINY) continue; |
200 |
> |
if (l >= lwmax-FTINY) continue; |
201 |
|
b = Bl(l); |
134 |
– |
bwavg += b; |
202 |
|
w = what2do&DO_CWEIGHT ? centprob(x,y) : 1.; |
203 |
+ |
bwavg += w*b; |
204 |
|
bwhist[bwhi(b)] += w; |
205 |
|
histot += w; |
206 |
|
} |
212 |
|
w = 1.; |
213 |
|
for (x = 0; x < nfixations; x++) { |
214 |
|
l = plum(fovscan(fixlst[x][1])[fixlst[x][0]]); |
215 |
< |
if (l < lwmin) continue; |
216 |
< |
if (l > lwmax) continue; |
215 |
> |
if (l < lwmin+FTINY) continue; |
216 |
> |
if (l >= lwmax-FTINY) continue; |
217 |
|
b = Bl(l); |
218 |
< |
bwavg += b; |
218 |
> |
bwavg += w*b; |
219 |
|
bwhist[bwhi(b)] += w; |
220 |
|
histot += w; |
221 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
double |
299 |
< |
clampf(Lw) /* derivative clamping function */ |
299 |
> |
clampf(Lw) /* histogram clamping function */ |
300 |
|
double Lw; |
301 |
|
{ |
302 |
|
double bLw, ratio; |
306 |
|
return(ratio/(Lb1(bLw)*(Bldmax-Bldmin)*Bl1(Lw))); |
307 |
|
} |
308 |
|
|
309 |
+ |
double |
310 |
+ |
crfactor(Lw) /* contrast reduction factor */ |
311 |
+ |
double Lw; |
312 |
+ |
{ |
313 |
+ |
int i = HISTRES*(Bl(Lw) - bwmin)/(bwmax - bwmin); |
314 |
+ |
double bLw, ratio, Tdb; |
315 |
|
|
316 |
+ |
if (i <= 0) |
317 |
+ |
return(1.0); |
318 |
+ |
if (i >= HISTRES) |
319 |
+ |
return(1.0); |
320 |
+ |
bLw = BLw(Lw); |
321 |
+ |
ratio = what2do&DO_HSENS ? htcontrs(Lb(bLw))/htcontrs(Lw) : Lb(bLw)/Lw; |
322 |
+ |
Tdb = mhistot * (bwmax - bwmin) / HISTRES; |
323 |
+ |
return(modhist[i]*Lb1(bLw)*(Bldmax-Bldmin)*Bl1(Lw)/(Tdb*ratio)); |
324 |
+ |
} |
325 |
+ |
|
326 |
+ |
|
327 |
+ |
#if ADJ_VEIL |
328 |
+ |
mkcrfimage() /* compute contrast reduction factor image */ |
329 |
+ |
{ |
330 |
+ |
int i; |
331 |
+ |
float *crfptr; |
332 |
+ |
COLOR *fovptr; |
333 |
+ |
|
334 |
+ |
if (crfimg == NULL) |
335 |
+ |
crfimg = (float *)malloc(fvxr*fvyr*sizeof(float)); |
336 |
+ |
if (crfimg == NULL) |
337 |
+ |
syserror("malloc"); |
338 |
+ |
crfptr = crfimg; |
339 |
+ |
fovptr = fovimg; |
340 |
+ |
for (i = fvxr*fvyr; i--; crfptr++, fovptr++) |
341 |
+ |
crfptr[0] = crfactor(plum(fovptr[0])); |
342 |
+ |
} |
343 |
+ |
#endif |
344 |
+ |
|
345 |
+ |
|
346 |
|
int |
347 |
|
mkbrmap() /* make dynamic range map */ |
348 |
|
{ |
349 |
< |
double T, b, s; |
349 |
> |
double Tdb, b, s; |
350 |
|
double ceiling, trimmings; |
351 |
|
register int i; |
352 |
|
/* copy initial histogram */ |
353 |
|
bcopy((char *)bwhist, (char *)modhist, sizeof(modhist)); |
354 |
< |
s = (bwmax - bwmin)/HISTRES; |
354 |
> |
s = (bwmax - bwmin)/HISTRES; /* s is delta b */ |
355 |
|
/* loop until satisfactory */ |
356 |
|
do { |
357 |
|
mkcumf(); /* sync brightness mapping */ |
358 |
|
if (mhistot <= histot*CVRATIO) |
359 |
|
return(-1); /* no compression needed! */ |
360 |
< |
T = mhistot * (bwmax - bwmin) / HISTRES; |
360 |
> |
Tdb = mhistot * s; |
361 |
|
trimmings = 0.; /* clip to envelope */ |
362 |
|
for (i = 0, b = bwmin + .5*s; i < HISTRES; i++, b += s) { |
363 |
< |
ceiling = T*clampf(Lb(b)); |
363 |
> |
ceiling = Tdb*clampf(Lb(b)); |
364 |
|
if (modhist[i] > ceiling) { |
365 |
|
trimmings += modhist[i] - ceiling; |
366 |
|
modhist[i] = ceiling; |
368 |
|
} |
369 |
|
} while (trimmings > histot*CVRATIO); |
370 |
|
|
371 |
+ |
#if ADJ_VEIL |
372 |
+ |
mkcrfimage(); /* contrast reduction image */ |
373 |
+ |
#endif |
374 |
+ |
|
375 |
|
return(0); /* we got it */ |
376 |
|
} |
377 |
|
|
412 |
|
int xres; |
413 |
|
{ |
414 |
|
double mult, Lw, b; |
415 |
< |
register int i; |
415 |
> |
register int x; |
416 |
|
|
417 |
< |
for (i = 0; i < xres; i++) { |
418 |
< |
Lw = plum(scan[i]); |
417 |
> |
for (x = 0; x < xres; x++) { |
418 |
> |
Lw = plum(scan[x]); |
419 |
|
if (Lw < LMIN) { |
420 |
< |
setcolor(scan[i], 0., 0., 0.); |
420 |
> |
setcolor(scan[x], 0., 0., 0.); |
421 |
|
continue; |
422 |
|
} |
423 |
< |
b = BLw(Lw); |
424 |
< |
mult = (Lb(b) - ldmin)/(ldmax - ldmin) / (Lw*inpexp); |
423 |
> |
b = BLw(Lw); /* apply brightness mapping */ |
424 |
> |
mult = (Lb(b) - ldmin)/(ldmax - ldmin)/(Lw*inpexp); |
425 |
|
if (lumf == rgblum) mult *= WHTEFFICACY; |
426 |
< |
scalecolor(scan[i], mult); |
426 |
> |
scalecolor(scan[x], mult); |
427 |
|
} |
428 |
|
} |
429 |
|
|