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