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 */ |
32 |
|
|
33 |
|
COLOR *fovimg; /* foveal (1 degree) averaged image */ |
34 |
|
short fvxr, fvyr; /* foveal image resolution */ |
35 |
< |
int bwhist[HISTRES]; /* luminance histogram */ |
36 |
< |
long histot; /* total count of histogram */ |
35 |
> |
float bwhist[HISTRES]; /* luminance histogram */ |
36 |
> |
double histot; /* total count of histogram */ |
37 |
|
double bwmin, bwmax; /* histogram limits */ |
38 |
|
double bwavg; /* mean brightness */ |
39 |
|
|
112 |
|
if (i+1 >= argc) goto userr; |
113 |
|
ldmin = atof(argv[++i]); |
114 |
|
break; |
115 |
+ |
case 'm': |
116 |
+ |
if (i+1 >= argc) goto userr; |
117 |
+ |
if ((mapfp = fopen(argv[++i], "w")) == NULL) { |
118 |
+ |
fprintf(stderr, |
119 |
+ |
"%s: cannot open for writing\n", |
120 |
+ |
argv[i]); |
121 |
+ |
exit(1); |
122 |
+ |
} |
123 |
+ |
break; |
124 |
|
default: |
125 |
|
goto userr; |
126 |
|
} |
155 |
|
if (outprims != inprims) |
156 |
|
fputprims(outprims, stdout); |
157 |
|
mapimage(); /* map the picture */ |
158 |
+ |
if (mapfp != NULL) /* write out basic mapping */ |
159 |
+ |
putmapping(mapfp); |
160 |
|
exit(0); |
161 |
|
userr: |
162 |
< |
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", |
162 |
> |
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", |
163 |
|
progname); |
164 |
|
exit(1); |
165 |
|
#undef bool |
249 |
|
#ifdef DEBUG |
250 |
|
fprintf(stderr, "%s: generating histogram...", progname); |
251 |
|
#endif |
252 |
< |
fovhist(); /* generate adaptation histogram */ |
252 |
> |
getfovimg(); /* get foveal sample image */ |
253 |
> |
comphist(); /* generate adaptation histogram */ |
254 |
|
#ifdef DEBUG |
255 |
|
fputs("done\n", stderr); |
256 |
|
#endif |
308 |
|
centprob(x, y) /* center-weighting probability function */ |
309 |
|
int x, y; |
310 |
|
{ |
311 |
< |
double xr, yr; |
312 |
< |
|
313 |
< |
xr = (x+.5)/fvxr - .5; |
314 |
< |
yr = (y+.5)/fvyr - .5; |
315 |
< |
return(1. - xr*xr - yr*yr); /* radial, == 0.5 at corners */ |
311 |
> |
double xr, yr, p; |
312 |
> |
/* paraboloid, 0 at 90 degrees from center */ |
313 |
> |
xr = (x - .5*(fvxr-1))/90.; /* 180 degree fisheye has fv?r == 90 */ |
314 |
> |
yr = (y - .5*(fvyr-1))/90.; |
315 |
> |
p = 1. - xr*xr - yr*yr; |
316 |
> |
return(p < 0. ? 0. : p); |
317 |
|
} |
318 |
|
|
319 |
|
|
320 |
< |
fovhist() /* create foveal sampled image and histogram */ |
320 |
> |
getfovimg() /* load foveal sampled image */ |
321 |
|
{ |
322 |
|
extern FILE *popen(); |
323 |
|
char combuf[128]; |
312 |
– |
double l, b, lwmin, lwmax; |
324 |
|
FILE *fp; |
325 |
|
int x, y; |
326 |
< |
|
326 |
> |
/* compute image size */ |
327 |
|
fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5; |
328 |
|
if (fvxr < 2) fvxr = 2; |
329 |
|
fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5; |
345 |
|
if (freadscan(fovscan(y), fvxr, fp) < 0) |
346 |
|
goto readerr; |
347 |
|
pclose(fp); |
348 |
+ |
return; |
349 |
+ |
readerr: |
350 |
+ |
fprintf(stderr, "%s: error reading from pfilt process in fovimage\n", |
351 |
+ |
progname); |
352 |
+ |
exit(1); |
353 |
+ |
} |
354 |
+ |
|
355 |
+ |
|
356 |
+ |
comphist() /* create foveal sampled image and histogram */ |
357 |
+ |
{ |
358 |
+ |
double l, b, lwmin, lwmax; |
359 |
+ |
register int x, y; |
360 |
+ |
|
361 |
|
lwmin = 1e10; /* find extrema */ |
362 |
|
lwmax = 0.; |
363 |
|
for (y = 0; y < fvyr; y++) |
366 |
|
if (l < lwmin) lwmin = l; |
367 |
|
if (l > lwmax) lwmax = l; |
368 |
|
} |
369 |
+ |
lwmin -= FTINY; |
370 |
+ |
lwmax += FTINY; |
371 |
|
if (lwmin < LMIN) lwmin = LMIN; |
372 |
|
if (lwmax > LMAX) lwmax = LMAX; |
373 |
|
/* compute histogram */ |
374 |
< |
bwmin = Bl(lwmin)*(1. - .01/HISTRES); |
375 |
< |
bwmax = Bl(lwmax)*(1. + .01/HISTRES); |
374 |
> |
bwmin = Bl(lwmin); |
375 |
> |
bwmax = Bl(lwmax); |
376 |
|
bwavg = 0.; |
377 |
|
for (y = 0; y < fvyr; y++) |
378 |
|
for (x = 0; x < fvxr; x++) { |
353 |
– |
if (what2do & DO_CWEIGHT && |
354 |
– |
frandom() > centprob(x,y)) |
355 |
– |
continue; |
379 |
|
l = plum(fovscan(y)[x]); |
380 |
+ |
if (l < lwmin) continue; |
381 |
+ |
if (l > lwmax) continue; |
382 |
|
b = Bl(l); |
358 |
– |
if (b < bwmin) continue; |
359 |
– |
if (b > bwmax) continue; |
383 |
|
bwavg += b; |
384 |
< |
bwhc(b)++; |
385 |
< |
histot++; |
384 |
> |
l = what2do&DO_CWEIGHT ? centprob(x,y) : 1.; |
385 |
> |
bwhist[bwhi(b)] += l; |
386 |
> |
histot += l; |
387 |
|
} |
388 |
< |
bwavg /= (double)histot; |
365 |
< |
return; |
366 |
< |
readerr: |
367 |
< |
fprintf(stderr, "%s: error reading from pfilt process in fovimage\n", |
368 |
< |
progname); |
369 |
< |
exit(1); |
388 |
> |
bwavg /= histot; |
389 |
|
} |
390 |
|
|
391 |
|
|
392 |
|
check2do() /* check histogram to see what isn't worth doing */ |
393 |
|
{ |
394 |
< |
long sum; |
394 |
> |
double sum; |
395 |
|
double b, l; |
396 |
|
register int i; |
397 |
|
|
406 |
|
if (!(what2do & (DO_ACUITY|DO_COLOR))) |
407 |
|
return; |
408 |
|
/* find 5th percentile */ |
409 |
< |
sum = histot*0.05 + .5; |
409 |
> |
sum = histot*0.05; |
410 |
|
for (i = 0; i < HISTRES; i++) |
411 |
|
if ((sum -= bwhist[i]) <= 0) |
412 |
|
break; |