--- ray/src/px/pcond.c 1997/01/29 13:22:08 3.9 +++ ray/src/px/pcond.c 2004/11/14 16:57:18 3.22 @@ -1,24 +1,25 @@ -/* Copyright (c) 1997 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: pcond.c,v 3.22 2004/11/14 16:57:18 greg Exp $"; #endif - /* * Condition Radiance picture for display/output + * Added white-balance adjustment 10/01 (GW). */ +#include "platform.h" +#include "paths.h" +#include "rtprocess.h" #include "pcond.h" #define LDMAX 100 /* default max. display luminance */ -#define LDMINF 0.01 /* default min. display lum. factor */ +#define LDDYN 32 /* default dynamic range */ int what2do = 0; /* desired adjustments */ double ldmax = LDMAX; /* maximum output luminance */ -double ldmin = 0.; /* minimum output luminance */ -double Bldmin, Bldmax; /* Bl(ldmin) and Bl(ldmax) */ +double lddyn = LDDYN; /* display dynamic range */ +double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */ char *progname; /* global argv[0] */ @@ -32,20 +33,30 @@ double fixfrac = 0.; /* histogram share due to fixat RESOLU inpres; /* input picture resolution */ COLOR *fovimg; /* foveal (1 degree) averaged image */ -short fvxr, fvyr; /* foveal image resolution */ +int fvxr, fvyr; /* foveal image resolution */ +float *crfimg; /* contrast reduction factors */ short (*fixlst)[2]; /* fixation history list */ int nfixations; /* number of fixation points */ -float bwhist[HISTRES]; /* luminance histogram */ +double bwhist[HISTRES]; /* luminance histogram */ double histot; /* total count of histogram */ double bwmin, bwmax; /* histogram limits */ double bwavg; /* mean brightness */ double scalef = 0.; /* linear scaling factor */ +static gethfunc headline; +static void getahead(void); +static void mapimage(void); +static void getfovimg(void); +static void check2do(void); -main(argc, argv) -int argc; -char *argv[]; + + +int +main( + int argc, + char *argv[] +) { static RGBPRIMS outprimS; int i; @@ -85,6 +96,9 @@ char *argv[]; if (fixfrac > FTINY) what2do |= DO_FIXHIST; else what2do &= ~DO_FIXHIST; break; + case 'I': + bool(DO_PREHIST); + break; case 'l': bool(DO_LINEAR); break; @@ -103,7 +117,7 @@ char *argv[]; case 'e': if (i+1 >= argc) goto userr; scalef = atof(argv[++i]); - if (argv[i][0] == '+' | argv[i][0] == '-') + if ((argv[i][0] == '+') | (argv[i][0] == '-')) scalef = pow(2.0, scalef); what2do |= DO_LINEAR; break; @@ -111,17 +125,21 @@ char *argv[]; if (i+1 >= argc) goto userr; mbcalfile = argv[++i]; break; - case 't': + case 'm': if (i+1 >= argc) goto userr; + cwarpfile = argv[++i]; + break; + case 'u': + if (i+1 >= argc) goto userr; ldmax = atof(argv[++i]); if (ldmax <= FTINY) goto userr; break; - case 'b': + case 'd': if (i+1 >= argc) goto userr; - ldmin = atof(argv[++i]); + lddyn = atof(argv[++i]); break; - case 'm': + case 'x': if (i+1 >= argc) goto userr; if ((mapfp = fopen(argv[++i], "w")) == NULL) { fprintf(stderr, @@ -133,21 +151,20 @@ char *argv[]; default: goto userr; } - if (mbcalfile != NULL & outprims != stdprims) { - fprintf(stderr, "%s: only one of -p or -f option supported\n", - progname); + if ((what2do & (DO_FIXHIST|DO_PREHIST)) == (DO_FIXHIST|DO_PREHIST)) { + fprintf(stderr, "%s: only one of -i or -I option\n", progname); exit(1); } - if (outprims == stdprims & inprims != stdprims) - outprims = inprims; - if (ldmin <= FTINY) - ldmin = ldmax*LDMINF; - else if (ldmin >= ldmax) { - fprintf(stderr, "%s: Ldmin (%f) >= Ldmax (%f)!\n", progname, - ldmin, ldmax); + if ((mbcalfile != NULL) + (cwarpfile != NULL) + + (outprims != stdprims) > 1) { + fprintf(stderr, + "%s: only one of -p, -m or -f option supported\n", + progname); exit(1); } - Bldmin = Bl(ldmin); + if ((outprims == stdprims) & (inprims != stdprims)) + outprims = inprims; + Bldmin = Bl(ldmax/lddyn); Bldmax = Bl(ldmax); if (i >= argc || i+2 < argc) goto userr; @@ -157,31 +174,35 @@ char *argv[]; /* open output file */ if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL) syserror(argv[i+1]); -#ifdef MSDOS - setmode(fileno(infp), O_BINARY); - setmode(fileno(stdout), O_BINARY); -#endif + SET_FILE_BINARY(infp); + SET_FILE_BINARY(stdout); getahead(); /* load input header */ printargs(argc, argv, stdout); /* add to output header */ - if (mbcalfile == NULL & outprims != stdprims) + if ((mbcalfile == NULL) & (outprims != stdprims)) fputprims(outprims, stdout); - getfovimg(); /* get foveal sample image */ - if (what2do&DO_FIXHIST) /* get fixation history? */ + if ((what2do & (DO_PREHIST|DO_VEIL|DO_ACUITY)) != DO_PREHIST) + getfovimg(); /* get foveal sample image? */ + if (what2do&DO_PREHIST) /* get histogram? */ + gethisto(stdin); + else if (what2do&DO_FIXHIST) /* get fixation history? */ getfixations(stdin); mapimage(); /* map the picture */ if (mapfp != NULL) /* write out basic mapping */ putmapping(mapfp); exit(0); userr: - 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][-t Ldmax][-b Ldmin][-m mapfile] inpic [outpic]\n", + 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", progname); exit(1); + return 1; /* pro forma return */ #undef bool } -syserror(s) /* report system error and exit */ -char *s; +extern void +syserror( /* report system error and exit */ + char *s +) { fprintf(stderr, "%s: ", progname); perror(s); @@ -189,8 +210,11 @@ char *s; } -headline(s) /* process header line */ -char *s; +static int +headline( /* process header line */ + char *s, + void *p +) { static RGBPRIMS inprimS; char fmt[32]; @@ -199,26 +223,27 @@ char *s; if (!strcmp(fmt,COLRFMT)) lumf = rgblum; else if (!strcmp(fmt,CIEFMT)) lumf = cielum; else lumf = NULL; - return; /* don't echo */ + return(0); /* don't echo */ } if (isprims(s)) { /* get input primaries */ primsval(inprimS, s); inprims= inprimS; - return; /* don't echo */ + return(0); /* don't echo */ } if (isexpos(s)) { /* picture exposure */ inpexp *= exposval(s); - return; /* don't echo */ + return(0); /* don't echo */ } if (isaspect(s)) /* pixel aspect ratio */ pixaspect *= aspectval(s); if (isview(s)) /* image view */ gotview += sscanview(&ourview, s); - fputs(s, stdout); + return(fputs(s, stdout)); } -getahead() /* load picture header */ +static void +getahead(void) /* load picture header */ { char *err; @@ -229,14 +254,15 @@ getahead() /* load picture header */ exit(1); } if (lumf == rgblum) - comprgb2xyzmat(inrgb2xyz, inprims); + comprgb2xyzWBmat(inrgb2xyz, inprims); else if (mbcalfile != NULL) { fprintf(stderr, "%s: macbethcal only works with RGB pictures\n", progname); exit(1); } - if (!gotview || ourview.type == VT_PAR) { - copystruct(&ourview, &stdview); + if (!gotview || ourview.type == VT_PAR || + ourview.horiz <= 3. | ourview.vert <= 3.) { + ourview = stdview; ourview.type = VT_PER; if (pixaspect*inpres.yr < inpres.xr) { ourview.horiz = 40.0; @@ -256,16 +282,22 @@ getahead() /* load picture header */ } -mapimage() /* map picture and send to stdout */ +static void +mapimage(void) /* map picture and send to stdout */ { COLOR *scan; comphist(); /* generate adaptation histogram */ check2do(); /* modify what2do flags */ if (what2do&DO_VEIL) - compveil(); - if (!(what2do&DO_LINEAR) && mkbrmap() < 0) /* make tone map */ - what2do |= DO_LINEAR; /* failed! -- use linear scaling */ + compveil(); /* compute veil image */ + if (!(what2do&DO_LINEAR)) + if (mkbrmap() < 0) /* make tone map */ + what2do |= DO_LINEAR; /* failed! -- use linear */ +#if ADJ_VEIL + else if (what2do&DO_VEIL) + adjveil(); /* else adjust veil image */ +#endif if (what2do&DO_LINEAR) { if (scalef <= FTINY) { if (what2do&DO_HSENS) @@ -291,10 +323,10 @@ mapimage() /* map picture and send to stdout */ } -getfovimg() /* load foveal sampled image */ +static void +getfovimg(void) /* load foveal sampled image */ { - extern FILE *popen(); - char combuf[128]; + char combuf[PATH_MAX]; FILE *fp; int x, y; /* compute image size */ @@ -302,18 +334,18 @@ getfovimg() /* load foveal sampled image */ if (fvxr < 2) fvxr = 2; fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5; if (fvyr < 2) fvyr = 2; - if (!(inpres.or & YMAJOR)) { /* picture is rotated? */ + if (!(inpres.rt & YMAJOR)) { /* picture is rotated? */ y = fvyr; fvyr = fvxr; fvxr = y; } if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL) syserror("malloc"); - sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d %s", fvxr, fvyr, infn); + sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d \"%s\"", fvxr, fvyr, infn); if ((fp = popen(combuf, "r")) == NULL) syserror("popen"); getheader(fp, NULL, NULL); /* skip header */ - if (fgetresolu(&x, &y, fp) < 0 || x != fvxr | y != fvyr) + if (fgetresolu(&x, &y, fp) < 0 || (x != fvxr) | (y != fvyr)) goto readerr; for (y = 0; y < fvyr; y++) if (freadscan(fovscan(y), fvxr, fp) < 0) @@ -327,7 +359,8 @@ readerr: } -check2do() /* check histogram to see what isn't worth doing */ +static void +check2do(void) /* check histogram to see what isn't worth doing */ { double sum; double b, l;