8 |
|
#include <string.h> |
9 |
|
|
10 |
|
#include "pcond.h" |
11 |
+ |
#include "random.h" |
12 |
|
|
13 |
|
|
14 |
|
#define CVRATIO 0.025 /* fraction of samples allowed > env. */ |
20 |
|
double mhistot; /* modified histogram total */ |
21 |
|
double cumf[HISTRES+1]; /* cumulative distribution function */ |
22 |
|
|
23 |
< |
static double centprob(int x, int y); |
23 |
> |
static double centprob(int x, int y); |
24 |
|
static void mkcumf(void); |
25 |
< |
static double cf(double b); |
26 |
< |
static double BLw(double Lw); |
25 |
> |
static double cf(double b); |
26 |
> |
static double BLw(double Lw); |
27 |
> |
static float *getlumsamp(int n); |
28 |
|
#if ADJ_VEIL |
29 |
|
static void mkcrfimage(void); |
30 |
|
#endif |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
+ |
static float * |
183 |
+ |
getlumsamp(int n) /* get set of random point sample luminances */ |
184 |
+ |
{ |
185 |
+ |
float *ls = (float *)malloc(n*sizeof(float)); |
186 |
+ |
COLR *cscan = (COLR *)malloc(scanlen(&inpres)*sizeof(COLR)); |
187 |
+ |
long startpos = ftell(infp); |
188 |
+ |
long npleft = (long)inpres.xr*inpres.yr; |
189 |
+ |
int x; |
190 |
+ |
|
191 |
+ |
if ((ls == NULL) | (cscan == NULL)) |
192 |
+ |
syserror("malloc"); |
193 |
+ |
x = 0; /* read/convert samples */ |
194 |
+ |
while (n > 0) { |
195 |
+ |
COLOR col; |
196 |
+ |
int sv = 0; |
197 |
+ |
double rval, cumprob = 0; |
198 |
+ |
|
199 |
+ |
if (x <= 0 && fread2colrs(cscan, x=scanlen(&inpres), infp, |
200 |
+ |
NCSAMP, WLPART) < 0) { |
201 |
+ |
fprintf(stderr, "%s: %s: scanline read error\n", |
202 |
+ |
progname, infn); |
203 |
+ |
exit(1); |
204 |
+ |
} |
205 |
+ |
rval = frandom(); /* random distance to next sample */ |
206 |
+ |
while ((cumprob += (1.-cumprob)*n/(npleft-sv)) < rval) |
207 |
+ |
sv++; |
208 |
+ |
if (x < ++sv) { /* out of pixels in this scanline */ |
209 |
+ |
npleft -= x; |
210 |
+ |
x = 0; |
211 |
+ |
continue; |
212 |
+ |
} |
213 |
+ |
x -= sv; |
214 |
+ |
colr_color(col, cscan[x]); |
215 |
+ |
ls[--n] = plum(col); |
216 |
+ |
npleft -= sv; |
217 |
+ |
} |
218 |
+ |
free(cscan); /* clean up and reset file pointer */ |
219 |
+ |
if (fseek(infp, startpos, SEEK_SET) < 0) |
220 |
+ |
syserror("fseek"); |
221 |
+ |
return(ls); |
222 |
+ |
} |
223 |
+ |
|
224 |
+ |
|
225 |
|
void |
226 |
|
comphist(void) /* create foveal sampling histogram */ |
227 |
|
{ |
228 |
|
double l, b, w, lwmin, lwmax; |
229 |
+ |
float *lumsamp; |
230 |
+ |
int nlumsamp; |
231 |
|
int x, y; |
232 |
|
/* check for precalculated histogram */ |
233 |
|
if (what2do&DO_PREHIST) |
237 |
|
for (y = 0; y < fvyr; y++) |
238 |
|
for (x = 0; x < fvxr; x++) { |
239 |
|
l = plum(fovscan(y)[x]); |
240 |
< |
if ((l < lwmin) & (l > LMIN)) lwmin = l; |
241 |
< |
if ((l > lwmax) & (l < LMAX)) lwmax = l; |
240 |
> |
if (l < lwmin) lwmin = l; |
241 |
> |
if (l > lwmax) lwmax = l; |
242 |
|
} |
243 |
+ |
/* sample luminance pixels */ |
244 |
+ |
nlumsamp = fvxr*fvyr*16; |
245 |
+ |
if (nlumsamp > inpres.xr*inpres.yr) |
246 |
+ |
nlumsamp = inpres.xr*inpres.yr; |
247 |
+ |
lumsamp = getlumsamp(nlumsamp); |
248 |
+ |
for (x = nlumsamp; x--; ) { |
249 |
+ |
l = lumsamp[x]; |
250 |
+ |
if (l < lwmin) lwmin = l; |
251 |
+ |
if (l > lwmax) lwmax = l; |
252 |
+ |
} |
253 |
|
lwmax *= 1.01; |
254 |
|
if (lwmax > LMAX) |
255 |
|
lwmax = LMAX; |
264 |
|
/* (re)compute histogram */ |
265 |
|
bwavg = 0.; |
266 |
|
histot = 0.; |
267 |
< |
for (x = 0; x < HISTRES; x++) |
211 |
< |
bwhist[x] = 0.; |
267 |
> |
memset(bwhist, 0, sizeof(bwhist)); |
268 |
|
/* global average */ |
269 |
< |
if (!(what2do&DO_FIXHIST) || fixfrac < 1.-FTINY) |
269 |
> |
if (!(what2do&DO_FIXHIST) || fixfrac < 1.-FTINY) { |
270 |
|
for (y = 0; y < fvyr; y++) |
271 |
|
for (x = 0; x < fvxr; x++) { |
272 |
|
l = plum(fovscan(y)[x]); |
278 |
|
bwhist[bwhi(b)] += w; |
279 |
|
histot += w; |
280 |
|
} |
281 |
+ |
/* weight for point luminances */ |
282 |
+ |
w = 1. * histot / nlumsamp; |
283 |
+ |
for (x = nlumsamp; x--; ) { |
284 |
+ |
l = lumsamp[x]; |
285 |
+ |
if (l < lwmin+FTINY) continue; |
286 |
+ |
if (l >= lwmax-FTINY) continue; |
287 |
+ |
b = Bl(l); |
288 |
+ |
bwavg += w*b; |
289 |
+ |
bwhist[bwhi(b)] += w; |
290 |
+ |
histot += w; |
291 |
+ |
} |
292 |
+ |
} |
293 |
|
/* average fixation points */ |
294 |
|
if (what2do&DO_FIXHIST && nfixations > 0) { |
295 |
|
if (histot > FTINY) |
311 |
|
bwhist[1] *= 0.5; |
312 |
|
bwhist[0] += bwhist[1]; |
313 |
|
} |
314 |
+ |
free(lumsamp); |
315 |
|
} |
316 |
|
|
317 |
|
|
460 |
|
modhist[i] = ceiling; |
461 |
|
} |
462 |
|
} |
463 |
< |
} while (trimmings > histot*CVRATIO); |
463 |
> |
} while (trimmings > mhistot*CVRATIO); |
464 |
|
|
465 |
|
#if ADJ_VEIL |
466 |
|
mkcrfimage(); /* contrast reduction image */ |