| 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 |
|
* Condition Radiance picture for display/output |
| 6 |
+ |
* Added white-balance adjustment 10/01 (GW). |
| 7 |
|
*/ |
| 8 |
|
|
| 9 |
+ |
#include "platform.h" |
| 10 |
|
#include "pcond.h" |
| 11 |
|
|
| 12 |
|
|
| 13 |
|
#define LDMAX 100 /* default max. display luminance */ |
| 14 |
< |
#define LDMINF 0.01 /* default min. display lum. factor */ |
| 14 |
> |
#define LDDYN 100 /* default dynamic range */ |
| 15 |
|
|
| 16 |
|
int what2do = 0; /* desired adjustments */ |
| 17 |
|
|
| 18 |
|
double ldmax = LDMAX; /* maximum output luminance */ |
| 19 |
< |
double ldmin = 0.; /* minimum output luminance */ |
| 20 |
< |
double Bldmin, Bldmax; /* Bl(ldmin) and Bl(ldmax) */ |
| 19 |
> |
double lddyn = LDDYN; /* display dynamic range */ |
| 20 |
> |
double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */ |
| 21 |
|
|
| 23 |
– |
char *progname; /* global argv[0] */ |
| 24 |
– |
|
| 22 |
|
char *infn; /* input file name */ |
| 23 |
|
FILE *infp; /* input stream */ |
| 24 |
|
FILE *mapfp = NULL; /* tone-mapping function stream */ |
| 29 |
|
RESOLU inpres; /* input picture resolution */ |
| 30 |
|
|
| 31 |
|
COLOR *fovimg; /* foveal (1 degree) averaged image */ |
| 32 |
< |
short fvxr, fvyr; /* foveal image resolution */ |
| 32 |
> |
int fvxr, fvyr; /* foveal image resolution */ |
| 33 |
> |
float *crfimg; /* contrast reduction factors */ |
| 34 |
|
short (*fixlst)[2]; /* fixation history list */ |
| 35 |
|
int nfixations; /* number of fixation points */ |
| 36 |
< |
float bwhist[HISTRES]; /* luminance histogram */ |
| 36 |
> |
double bwhist[HISTRES]; /* luminance histogram */ |
| 37 |
|
double histot; /* total count of histogram */ |
| 38 |
|
double bwmin, bwmax; /* histogram limits */ |
| 39 |
|
double bwavg; /* mean brightness */ |
| 40 |
|
|
| 41 |
|
double scalef = 0.; /* linear scaling factor */ |
| 42 |
|
|
| 43 |
+ |
static gethfunc headline; |
| 44 |
+ |
static void getahead(void); |
| 45 |
+ |
static void mapimage(void); |
| 46 |
+ |
static void getfovimg(void); |
| 47 |
+ |
static void check2do(void); |
| 48 |
|
|
| 49 |
< |
main(argc, argv) |
| 50 |
< |
int argc; |
| 51 |
< |
char *argv[]; |
| 49 |
> |
|
| 50 |
> |
|
| 51 |
> |
int |
| 52 |
> |
main( |
| 53 |
> |
int argc, |
| 54 |
> |
char *argv[] |
| 55 |
> |
) |
| 56 |
|
{ |
| 57 |
|
static RGBPRIMS outprimS; |
| 58 |
|
int i; |
| 59 |
< |
#define bool(flg) switch (argv[i][2]) { \ |
| 59 |
> |
#define check_bool(flg) switch (argv[i][2]) { \ |
| 60 |
|
case '\0': what2do ^= flg; break; \ |
| 61 |
|
case 'y': case 'Y': case 't': case 'T': \ |
| 62 |
|
case '+': case '1': what2do |= flg; break; \ |
| 64 |
|
case '-': case '0': what2do &= ~(flg); break; \ |
| 65 |
|
default: goto userr; } |
| 66 |
|
|
| 67 |
< |
progname = argv[0]; |
| 67 |
> |
fixargv0(argv[0]); /* sets global progname */ |
| 68 |
|
|
| 69 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
| 70 |
|
switch (argv[i][1]) { |
| 71 |
|
case 'h': |
| 72 |
< |
bool(DO_HUMAN); |
| 72 |
> |
check_bool(DO_HUMAN); |
| 73 |
|
break; |
| 74 |
|
case 'a': |
| 75 |
< |
bool(DO_ACUITY); |
| 75 |
> |
check_bool(DO_ACUITY); |
| 76 |
|
break; |
| 77 |
|
case 'v': |
| 78 |
< |
bool(DO_VEIL); |
| 78 |
> |
check_bool(DO_VEIL); |
| 79 |
|
break; |
| 80 |
|
case 's': |
| 81 |
< |
bool(DO_HSENS); |
| 81 |
> |
check_bool(DO_HSENS); |
| 82 |
|
break; |
| 83 |
|
case 'c': |
| 84 |
< |
bool(DO_COLOR); |
| 84 |
> |
check_bool(DO_COLOR); |
| 85 |
|
break; |
| 86 |
|
case 'w': |
| 87 |
< |
bool(DO_CWEIGHT); |
| 87 |
> |
check_bool(DO_CWEIGHT); |
| 88 |
|
break; |
| 89 |
|
case 'i': |
| 90 |
|
if (i+1 >= argc) goto userr; |
| 92 |
|
if (fixfrac > FTINY) what2do |= DO_FIXHIST; |
| 93 |
|
else what2do &= ~DO_FIXHIST; |
| 94 |
|
break; |
| 95 |
+ |
case 'I': |
| 96 |
+ |
check_bool(DO_PREHIST); |
| 97 |
+ |
break; |
| 98 |
|
case 'l': |
| 99 |
< |
bool(DO_LINEAR); |
| 99 |
> |
check_bool(DO_LINEAR); |
| 100 |
|
break; |
| 101 |
|
case 'p': |
| 102 |
|
if (i+8 >= argc) goto userr; |
| 113 |
|
case 'e': |
| 114 |
|
if (i+1 >= argc) goto userr; |
| 115 |
|
scalef = atof(argv[++i]); |
| 116 |
< |
if (argv[i][0] == '+' | argv[i][0] == '-') |
| 116 |
> |
if ((argv[i][0] == '+') | (argv[i][0] == '-')) |
| 117 |
|
scalef = pow(2.0, scalef); |
| 118 |
|
what2do |= DO_LINEAR; |
| 119 |
|
break; |
| 125 |
|
if (i+1 >= argc) goto userr; |
| 126 |
|
cwarpfile = argv[++i]; |
| 127 |
|
break; |
| 128 |
< |
case 't': |
| 128 |
> |
case 'u': |
| 129 |
|
if (i+1 >= argc) goto userr; |
| 130 |
|
ldmax = atof(argv[++i]); |
| 131 |
|
if (ldmax <= FTINY) |
| 132 |
|
goto userr; |
| 133 |
|
break; |
| 134 |
< |
case 'b': |
| 134 |
> |
case 'd': |
| 135 |
|
if (i+1 >= argc) goto userr; |
| 136 |
< |
ldmin = atof(argv[++i]); |
| 136 |
> |
lddyn = atof(argv[++i]); |
| 137 |
|
break; |
| 138 |
< |
case 'd': |
| 138 |
> |
case 'x': |
| 139 |
|
if (i+1 >= argc) goto userr; |
| 140 |
|
if ((mapfp = fopen(argv[++i], "w")) == NULL) { |
| 141 |
|
fprintf(stderr, |
| 147 |
|
default: |
| 148 |
|
goto userr; |
| 149 |
|
} |
| 150 |
+ |
if ((what2do & (DO_FIXHIST|DO_PREHIST)) == (DO_FIXHIST|DO_PREHIST)) { |
| 151 |
+ |
fprintf(stderr, "%s: only one of -i or -I option\n", progname); |
| 152 |
+ |
exit(1); |
| 153 |
+ |
} |
| 154 |
|
if ((mbcalfile != NULL) + (cwarpfile != NULL) + |
| 155 |
|
(outprims != stdprims) > 1) { |
| 156 |
|
fprintf(stderr, |
| 158 |
|
progname); |
| 159 |
|
exit(1); |
| 160 |
|
} |
| 161 |
< |
if (outprims == stdprims & inprims != stdprims) |
| 161 |
> |
if ((outprims == stdprims) & (inprims != stdprims)) |
| 162 |
|
outprims = inprims; |
| 163 |
< |
if (ldmin <= FTINY) |
| 150 |
< |
ldmin = ldmax*LDMINF; |
| 151 |
< |
else if (ldmin >= ldmax) { |
| 152 |
< |
fprintf(stderr, "%s: Ldmin (%f) >= Ldmax (%f)!\n", progname, |
| 153 |
< |
ldmin, ldmax); |
| 154 |
< |
exit(1); |
| 155 |
< |
} |
| 156 |
< |
Bldmin = Bl(ldmin); |
| 163 |
> |
Bldmin = Bl(ldmax/lddyn); |
| 164 |
|
Bldmax = Bl(ldmax); |
| 165 |
|
if (i >= argc || i+2 < argc) |
| 166 |
|
goto userr; |
| 170 |
|
/* open output file */ |
| 171 |
|
if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL) |
| 172 |
|
syserror(argv[i+1]); |
| 173 |
< |
#ifdef MSDOS |
| 174 |
< |
setmode(fileno(infp), O_BINARY); |
| 168 |
< |
setmode(fileno(stdout), O_BINARY); |
| 169 |
< |
#endif |
| 173 |
> |
SET_FILE_BINARY(infp); |
| 174 |
> |
SET_FILE_BINARY(stdout); |
| 175 |
|
getahead(); /* load input header */ |
| 176 |
|
printargs(argc, argv, stdout); /* add to output header */ |
| 177 |
< |
if (mbcalfile == NULL & outprims != stdprims) |
| 177 |
> |
if ((mbcalfile == NULL) & (outprims != stdprims)) |
| 178 |
|
fputprims(outprims, stdout); |
| 179 |
< |
getfovimg(); /* get foveal sample image */ |
| 180 |
< |
if (what2do&DO_FIXHIST) /* get fixation history? */ |
| 179 |
> |
if ((what2do & (DO_PREHIST|DO_VEIL|DO_ACUITY)) != DO_PREHIST) |
| 180 |
> |
getfovimg(); /* get foveal sample image? */ |
| 181 |
> |
if (what2do&DO_PREHIST) /* get histogram? */ |
| 182 |
> |
gethisto(stdin); |
| 183 |
> |
else if (what2do&DO_FIXHIST) /* get fixation history? */ |
| 184 |
|
getfixations(stdin); |
| 185 |
|
mapimage(); /* map the picture */ |
| 186 |
|
if (mapfp != NULL) /* write out basic mapping */ |
| 187 |
|
putmapping(mapfp); |
| 188 |
|
exit(0); |
| 189 |
|
userr: |
| 190 |
< |
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|-m rgb.cwp][-t Ldmax][-b Ldmin][-d mapfile] inpic [outpic]\n", |
| 190 |
> |
fprintf(stderr, "Usage: %s [-{h|a|v|s|c|l|w}[+-]][-I|-i ffrac][-e ev][-p xr yr xg yg xb yb xw yw|-f mbf.cal|-m rgb.cwp][-u Ldmax][-d Lddyn][-x mapfile] inpic [outpic]\n", |
| 191 |
|
progname); |
| 192 |
|
exit(1); |
| 193 |
< |
#undef bool |
| 193 |
> |
return 1; /* pro forma return */ |
| 194 |
> |
#undef check_bool |
| 195 |
|
} |
| 196 |
|
|
| 197 |
|
|
| 198 |
< |
syserror(s) /* report system error and exit */ |
| 199 |
< |
char *s; |
| 198 |
> |
void |
| 199 |
> |
syserror( /* report system error and exit */ |
| 200 |
> |
char *s |
| 201 |
> |
) |
| 202 |
|
{ |
| 203 |
|
fprintf(stderr, "%s: ", progname); |
| 204 |
|
perror(s); |
| 206 |
|
} |
| 207 |
|
|
| 208 |
|
|
| 209 |
< |
headline(s) /* process header line */ |
| 210 |
< |
char *s; |
| 209 |
> |
static int |
| 210 |
> |
headline( /* process header line */ |
| 211 |
> |
char *s, |
| 212 |
> |
void *p |
| 213 |
> |
) |
| 214 |
|
{ |
| 215 |
|
static RGBPRIMS inprimS; |
| 216 |
< |
char fmt[32]; |
| 216 |
> |
char fmt[MAXFMTLEN]; |
| 217 |
|
|
| 218 |
|
if (formatval(fmt, s)) { /* check if format string */ |
| 219 |
< |
if (!strcmp(fmt,COLRFMT)) lumf = rgblum; |
| 220 |
< |
else if (!strcmp(fmt,CIEFMT)) lumf = cielum; |
| 221 |
< |
else lumf = NULL; |
| 222 |
< |
return; /* don't echo */ |
| 219 |
> |
if (!strcmp(fmt,COLRFMT) || !strcmp(fmt,SPECFMT)) |
| 220 |
> |
lumf = rgblum; |
| 221 |
> |
else if (!strcmp(fmt,CIEFMT)) |
| 222 |
> |
lumf = cielum; |
| 223 |
> |
else |
| 224 |
> |
lumf = NULL; |
| 225 |
> |
return(0); /* don't echo */ |
| 226 |
|
} |
| 227 |
< |
if (isprims(s)) { /* get input primaries */ |
| 228 |
< |
primsval(inprimS, s); |
| 229 |
< |
inprims= inprimS; |
| 213 |
< |
return; /* don't echo */ |
| 227 |
> |
if (isncomp(s)) { |
| 228 |
> |
NCSAMP = ncompval(s); |
| 229 |
> |
return(0); |
| 230 |
|
} |
| 231 |
+ |
if (iswlsplit(s)) { |
| 232 |
+ |
wlsplitval(WLPART, s); |
| 233 |
+ |
return(0); |
| 234 |
+ |
} |
| 235 |
+ |
/* get input primaries */ |
| 236 |
+ |
if (isprims(s) && primsval(inprimS, s)) { |
| 237 |
+ |
inprims = inprimS; |
| 238 |
+ |
return(0); /* don't echo */ |
| 239 |
+ |
} |
| 240 |
|
if (isexpos(s)) { /* picture exposure */ |
| 241 |
|
inpexp *= exposval(s); |
| 242 |
< |
return; /* don't echo */ |
| 242 |
> |
return(0); /* don't echo */ |
| 243 |
|
} |
| 244 |
|
if (isaspect(s)) /* pixel aspect ratio */ |
| 245 |
|
pixaspect *= aspectval(s); |
| 246 |
|
if (isview(s)) /* image view */ |
| 247 |
|
gotview += sscanview(&ourview, s); |
| 248 |
< |
fputs(s, stdout); |
| 248 |
> |
return(fputs(s, stdout)); |
| 249 |
|
} |
| 250 |
|
|
| 251 |
|
|
| 252 |
< |
getahead() /* load picture header */ |
| 252 |
> |
static void |
| 253 |
> |
getahead(void) /* load picture header */ |
| 254 |
|
{ |
| 255 |
|
char *err; |
| 256 |
|
|
| 261 |
|
exit(1); |
| 262 |
|
} |
| 263 |
|
if (lumf == rgblum) |
| 264 |
< |
comprgb2xyzmat(inrgb2xyz, inprims); |
| 264 |
> |
comprgb2xyzWBmat(inrgb2xyz, inprims); |
| 265 |
|
else if (mbcalfile != NULL) { |
| 266 |
|
fprintf(stderr, "%s: macbethcal only works with RGB pictures\n", |
| 267 |
|
progname); |
| 268 |
|
exit(1); |
| 269 |
|
} |
| 270 |
< |
if (!gotview || ourview.type == VT_PAR) { |
| 271 |
< |
copystruct(&ourview, &stdview); |
| 270 |
> |
if (!gotview || ourview.type == VT_PAR || |
| 271 |
> |
(ourview.horiz <= 5.) | (ourview.vert <= 5.)) { |
| 272 |
> |
ourview = stdview; |
| 273 |
|
ourview.type = VT_PER; |
| 274 |
|
if (pixaspect*inpres.yr < inpres.xr) { |
| 275 |
|
ourview.horiz = 40.0; |
| 289 |
|
} |
| 290 |
|
|
| 291 |
|
|
| 292 |
< |
mapimage() /* map picture and send to stdout */ |
| 292 |
> |
static void |
| 293 |
> |
mapimage(void) /* map picture and send to stdout */ |
| 294 |
|
{ |
| 295 |
|
COLOR *scan; |
| 296 |
|
|
| 297 |
|
comphist(); /* generate adaptation histogram */ |
| 298 |
|
check2do(); /* modify what2do flags */ |
| 299 |
|
if (what2do&DO_VEIL) |
| 300 |
< |
compveil(); |
| 301 |
< |
if (!(what2do&DO_LINEAR) && mkbrmap() < 0) /* make tone map */ |
| 302 |
< |
what2do |= DO_LINEAR; /* failed! -- use linear scaling */ |
| 300 |
> |
compveil(); /* compute veil image */ |
| 301 |
> |
if (!(what2do&DO_LINEAR)) |
| 302 |
> |
if (mkbrmap() < 0) /* make tone map */ |
| 303 |
> |
what2do |= DO_LINEAR; /* failed! -- use linear */ |
| 304 |
> |
#if ADJ_VEIL |
| 305 |
> |
else if (what2do&DO_VEIL) |
| 306 |
> |
adjveil(); /* else adjust veil image */ |
| 307 |
> |
#endif |
| 308 |
|
if (what2do&DO_LINEAR) { |
| 309 |
|
if (scalef <= FTINY) { |
| 310 |
|
if (what2do&DO_HSENS) |
| 330 |
|
} |
| 331 |
|
|
| 332 |
|
|
| 333 |
< |
getfovimg() /* load foveal sampled image */ |
| 333 |
> |
static void |
| 334 |
> |
getfovimg(void) /* load foveal sampled image */ |
| 335 |
|
{ |
| 336 |
< |
extern FILE *popen(); |
| 303 |
< |
char combuf[128]; |
| 336 |
> |
char combuf[PATH_MAX]; |
| 337 |
|
FILE *fp; |
| 338 |
|
int x, y; |
| 339 |
|
/* compute image size */ |
| 341 |
|
if (fvxr < 2) fvxr = 2; |
| 342 |
|
fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5; |
| 343 |
|
if (fvyr < 2) fvyr = 2; |
| 344 |
< |
if (!(inpres.or & YMAJOR)) { /* picture is rotated? */ |
| 344 |
> |
if (!(inpres.rt & YMAJOR)) { /* picture is rotated? */ |
| 345 |
|
y = fvyr; |
| 346 |
|
fvyr = fvxr; |
| 347 |
|
fvxr = y; |
| 348 |
|
} |
| 349 |
|
if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL) |
| 350 |
|
syserror("malloc"); |
| 351 |
< |
sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d %s", fvxr, fvyr, infn); |
| 351 |
> |
sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d \"%s\"", fvxr, fvyr, infn); |
| 352 |
|
if ((fp = popen(combuf, "r")) == NULL) |
| 353 |
|
syserror("popen"); |
| 354 |
+ |
SET_FILE_BINARY(fp); |
| 355 |
|
getheader(fp, NULL, NULL); /* skip header */ |
| 356 |
< |
if (fgetresolu(&x, &y, fp) < 0 || x != fvxr | y != fvyr) |
| 356 |
> |
if (fgetresolu(&x, &y, fp) < 0 || (x != fvxr) | (y != fvyr)) |
| 357 |
|
goto readerr; |
| 358 |
|
for (y = 0; y < fvyr; y++) |
| 359 |
< |
if (freadscan(fovscan(y), fvxr, fp) < 0) |
| 359 |
> |
if (fread2scan(fovscan(y), fvxr, fp, NCSAMP, WLPART) < 0) |
| 360 |
|
goto readerr; |
| 361 |
|
pclose(fp); |
| 362 |
|
return; |
| 367 |
|
} |
| 368 |
|
|
| 369 |
|
|
| 370 |
< |
check2do() /* check histogram to see what isn't worth doing */ |
| 370 |
> |
static void |
| 371 |
> |
check2do(void) /* check histogram to see what isn't worth doing */ |
| 372 |
|
{ |
| 373 |
|
double sum; |
| 374 |
|
double b, l; |
| 375 |
< |
register int i; |
| 375 |
> |
int i; |
| 376 |
|
|
| 377 |
|
/* check for within display range */ |
| 378 |
|
if (bwmax - bwmin <= Bldmax - Bldmin) |