ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
(Generate patch)

Comparing ray/src/px/pcond.c (file contents):
Revision 3.5 by greg, Fri Oct 11 10:46:57 1996 UTC vs.
Revision 3.19 by schorsch, Mon Nov 10 11:54:23 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 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 "paths.h"
11 + #include "rtprocess.h"
12   #include "pcond.h"
13  
13 #include "random.h"
14  
15
15   #define LDMAX           100             /* default max. display luminance */
16 < #define LDMINF          0.01            /* default min. display lum. factor */
16 > #define LDDYN           32              /* default dynamic range */
17  
18   int     what2do = 0;                    /* desired adjustments */
19  
20   double  ldmax = LDMAX;                  /* maximum output luminance */
21 < double  ldmin = 0.;                     /* minimum output luminance */
22 < double  Bldmin, Bldmax;                 /* Bl(ldmin) and Bl(ldmax) */
21 > double  lddyn = LDDYN;                  /* display dynamic range */
22 > double  Bldmin, Bldmax;                 /* Bl(ldmax/lddyn) and Bl(ldmax) */
23  
24   char    *progname;                      /* global argv[0] */
25  
26   char    *infn;                          /* input file name */
27   FILE    *infp;                          /* input stream */
28 + FILE    *mapfp = NULL;                  /* tone-mapping function stream */
29   VIEW    ourview = STDVIEW;              /* picture view */
30   int     gotview = 0;                    /* picture has view */
31   double  pixaspect = 1.0;                /* pixel aspect ratio */
32 + double  fixfrac = 0.;                   /* histogram share due to fixations */
33   RESOLU  inpres;                         /* input picture resolution */
34  
35   COLOR   *fovimg;                        /* foveal (1 degree) averaged image */
36 < short   fvxr, fvyr;                     /* foveal image resolution */
37 < int     bwhist[HISTRES];                /* luminance histogram */
38 < long    histot;                         /* total count of histogram */
36 > int     fvxr, fvyr;                     /* foveal image resolution */
37 > float   *crfimg;                        /* contrast reduction factors */
38 > short   (*fixlst)[2];                   /* fixation history list */
39 > int     nfixations;                     /* number of fixation points */
40 > double  bwhist[HISTRES];                /* luminance histogram */
41 > double  histot;                         /* total count of histogram */
42   double  bwmin, bwmax;                   /* histogram limits */
43   double  bwavg;                          /* mean brightness */
44  
# Line 77 | Line 81 | char   *argv[];
81                  case 'w':
82                          bool(DO_CWEIGHT);
83                          break;
84 +                case 'i':
85 +                        if (i+1 >= argc) goto userr;
86 +                        fixfrac = atof(argv[++i]);
87 +                        if (fixfrac > FTINY) what2do |= DO_FIXHIST;
88 +                        else what2do &= ~DO_FIXHIST;
89 +                        break;
90 +                case 'I':
91 +                        bool(DO_PREHIST);
92 +                        break;
93                  case 'l':
94                          bool(DO_LINEAR);
95                          break;
# Line 95 | Line 108 | char   *argv[];
108                  case 'e':
109                          if (i+1 >= argc) goto userr;
110                          scalef = atof(argv[++i]);
111 <                        if (argv[i][0] == '+' | argv[i][0] == '-')
111 >                        if ((argv[i][0] == '+') | (argv[i][0] == '-'))
112                                  scalef = pow(2.0, scalef);
113                          what2do |= DO_LINEAR;
114                          break;
# Line 103 | Line 116 | char   *argv[];
116                          if (i+1 >= argc) goto userr;
117                          mbcalfile = argv[++i];
118                          break;
119 <                case 't':
119 >                case 'm':
120                          if (i+1 >= argc) goto userr;
121 +                        cwarpfile = argv[++i];
122 +                        break;
123 +                case 'u':
124 +                        if (i+1 >= argc) goto userr;
125                          ldmax = atof(argv[++i]);
126                          if (ldmax <= FTINY)
127                                  goto userr;
128                          break;
129 <                case 'b':
129 >                case 'd':
130                          if (i+1 >= argc) goto userr;
131 <                        ldmin = atof(argv[++i]);
131 >                        lddyn = atof(argv[++i]);
132                          break;
133 +                case 'x':
134 +                        if (i+1 >= argc) goto userr;
135 +                        if ((mapfp = fopen(argv[++i], "w")) == NULL) {
136 +                                fprintf(stderr,
137 +                                        "%s: cannot open for writing\n",
138 +                                                argv[i]);
139 +                                exit(1);
140 +                        }
141 +                        break;
142                  default:
143                          goto userr;
144                  }
145 <        if (mbcalfile != NULL & outprims != stdprims) {
146 <                fprintf(stderr, "%s: only one of -p or -f option supported\n",
121 <                                progname);
145 >        if ((what2do & (DO_FIXHIST|DO_PREHIST)) == (DO_FIXHIST|DO_PREHIST)) {
146 >                fprintf(stderr, "%s: only one of -i or -I option\n", progname);
147                  exit(1);
148          }
149 <        if (outprims == stdprims & inprims != stdprims)
150 <                outprims = inprims;
151 <        if (ldmin <= FTINY)
152 <                ldmin = ldmax*LDMINF;
153 <        else if (ldmin >= ldmax) {
129 <                fprintf(stderr, "%s: Ldmin (%f) >= Ldmax (%f)!\n", progname,
130 <                                ldmin, ldmax);
149 >        if ((mbcalfile != NULL) + (cwarpfile != NULL) +
150 >                        (outprims != stdprims) > 1) {
151 >                fprintf(stderr,
152 >                        "%s: only one of -p, -m or -f option supported\n",
153 >                                progname);
154                  exit(1);
155          }
156 <        Bldmin = Bl(ldmin);
156 >        if ((outprims == stdprims) & (inprims != stdprims))
157 >                outprims = inprims;
158 >        Bldmin = Bl(ldmax/lddyn);
159          Bldmax = Bl(ldmax);
160          if (i >= argc || i+2 < argc)
161                  goto userr;
162 +                                        /* open input file */
163          if ((infp = fopen(infn=argv[i], "r")) == NULL)
164                  syserror(infn);
165 +                                        /* open output file */
166          if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL)
167                  syserror(argv[i+1]);
168 < #ifdef MSDOS
169 <        setmode(fileno(infp), O_BINARY);
143 <        setmode(fileno(stdout), O_BINARY);
144 < #endif
168 >        SET_FILE_BINARY(infp);
169 >        SET_FILE_BINARY(stdout);
170          getahead();                     /* load input header */
171          printargs(argc, argv, stdout);  /* add to output header */
172 <        if (outprims != inprims)
172 >        if ((mbcalfile == NULL) & (outprims != stdprims))
173                  fputprims(outprims, stdout);
174 +        if ((what2do & (DO_PREHIST|DO_VEIL|DO_ACUITY)) != DO_PREHIST)
175 +                getfovimg();            /* get foveal sample image? */
176 +        if (what2do&DO_PREHIST)         /* get histogram? */
177 +                gethisto(stdin);
178 +        else if (what2do&DO_FIXHIST)    /* get fixation history? */
179 +                getfixations(stdin);
180          mapimage();                     /* map the picture */
181 +        if (mapfp != NULL)              /* write out basic mapping */
182 +                putmapping(mapfp);
183          exit(0);
184   userr:
185 <        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",
185 >        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",
186                          progname);
187          exit(1);
188   #undef bool
# Line 175 | Line 208 | char   *s;
208                  if (!strcmp(fmt,COLRFMT)) lumf = rgblum;
209                  else if (!strcmp(fmt,CIEFMT)) lumf = cielum;
210                  else lumf = NULL;
211 <                return;                 /* don't echo */
211 >                return(0);              /* don't echo */
212          }
213          if (isprims(s)) {               /* get input primaries */
214                  primsval(inprimS, s);
215                  inprims= inprimS;
216 <                return;                 /* don't echo */
216 >                return(0);              /* don't echo */
217          }
218          if (isexpos(s)) {               /* picture exposure */
219                  inpexp *= exposval(s);
220 <                return;                 /* don't echo */
220 >                return(0);              /* don't echo */
221          }
222          if (isaspect(s))                /* pixel aspect ratio */
223                  pixaspect *= aspectval(s);
224          if (isview(s))                  /* image view */
225                  gotview += sscanview(&ourview, s);
226 <        fputs(s, stdout);
226 >        return(fputs(s, stdout));
227   }
228  
229  
# Line 205 | Line 238 | getahead()                     /* load picture header */
238                  exit(1);
239          }
240          if (lumf == rgblum)
241 <                comprgb2xyzmat(inrgb2xyz, inprims);
241 >                comprgb2xyzWBmat(inrgb2xyz, inprims);
242          else if (mbcalfile != NULL) {
243                  fprintf(stderr, "%s: macbethcal only works with RGB pictures\n",
244                                  progname);
245                  exit(1);
246          }
247          if (!gotview || ourview.type == VT_PAR) {
248 <                copystruct(&ourview, &stdview);
248 >                ourview = stdview;
249                  ourview.type = VT_PER;
250                  if (pixaspect*inpres.yr < inpres.xr) {
251                          ourview.horiz = 40.0;
# Line 236 | Line 269 | mapimage()                             /* map picture and send to stdout */
269   {
270          COLOR   *scan;
271  
272 < #ifdef DEBUG
240 <        fprintf(stderr, "%s: generating histogram...", progname);
241 < #endif
242 <        fovhist();                      /* generate adaptation histogram */
243 < #ifdef DEBUG
244 <        fputs("done\n", stderr);
245 < #endif
272 >        comphist();                     /* generate adaptation histogram */
273          check2do();                     /* modify what2do flags */
274 <        if (what2do&DO_VEIL) {
275 < #ifdef DEBUG
276 <                fprintf(stderr, "%s: computing veiling...", progname);
274 >        if (what2do&DO_VEIL)
275 >                compveil();             /* compute veil image */
276 >        if (!(what2do&DO_LINEAR))
277 >                if (mkbrmap() < 0)      /* make tone map */
278 >                        what2do |= DO_LINEAR;   /* failed! -- use linear */
279 > #if ADJ_VEIL
280 >                else if (what2do&DO_VEIL)
281 >                        adjveil();      /* else adjust veil image */
282   #endif
251                compveil();
252 #ifdef DEBUG
253                fputs("done\n", stderr);
254 #endif
255        }
256 #ifdef DEBUG
257        fprintf(stderr, "%s: computing brightness mapping...", progname);
258 #endif
259        if (!(what2do&DO_LINEAR) && mkbrmap() < 0) {    /* make tone map */
260                what2do |= DO_LINEAR;           /* use linear scaling */
261 #ifdef DEBUG
262                fputs("failed!\n", stderr);
263        } else
264                fputs("done\n", stderr);
265 #else
266        }
267 #endif
283          if (what2do&DO_LINEAR) {
284                  if (scalef <= FTINY) {
285                          if (what2do&DO_HSENS)
# Line 274 | Line 289 | mapimage()                             /* map picture and send to stdout */
289                                  scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg);
290                          scalef *= WHTEFFICACY/(inpexp*ldmax);
291                  }
277 #ifdef DEBUG
278                fprintf(stderr, "%s: linear scaling factor = %f\n",
279                                progname, scalef);
280 #endif
292                  fputexpos(inpexp*scalef, stdout);       /* record exposure */
293                  if (lumf == cielum) scalef /= WHTEFFICACY;
294          }
295 <        putchar('\n');                  /* complete header */
295 >        fputformat(COLRFMT, stdout);    /* complete header */
296 >        putchar('\n');
297          fputsresolu(&inpres, stdout);   /* resolution doesn't change */
298 <
298 >                                        /* condition our image */
299          for (scan = firstscan(); scan != NULL; scan = nextscan())
300                  if (fwritescan(scan, scanlen(&inpres), stdout) < 0) {
301                          fprintf(stderr, "%s: scanline write error\n",
# Line 293 | Line 305 | mapimage()                             /* map picture and send to stdout */
305   }
306  
307  
308 < double
297 < centprob(x, y)                  /* center-weighting probability function */
298 < int     x, y;
308 > getfovimg()                     /* load foveal sampled image */
309   {
310 <        double  xr, yr;
301 <
302 <        xr = (x+.5)/fvxr - .5;
303 <        yr = (y+.5)/fvyr - .5;
304 <        return(1. - xr*xr - yr*yr);     /* radial, == 0.5 at corners */
305 < }
306 <
307 <
308 < fovhist()                       /* create foveal sampled image and histogram */
309 < {
310 <        extern FILE     *popen();
311 <        char    combuf[128];
312 <        double  l, b, lwmin, lwmax;
310 >        char    combuf[PATH_MAX];
311          FILE    *fp;
312          int     x, y;
313 <
313 >                                                /* compute image size */
314          fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5;
315          if (fvxr < 2) fvxr = 2;
316          fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5;
317          if (fvyr < 2) fvyr = 2;
318 <        if (!(inpres.or & YMAJOR)) {            /* picture is rotated? */
318 >        if (!(inpres.rt & YMAJOR)) {            /* picture is rotated? */
319                  y = fvyr;
320                  fvyr = fvxr;
321                  fvxr = y;
322          }
323          if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL)
324                  syserror("malloc");
325 <        sprintf(combuf, "pfilt -1 -b -x %d -y %d %s", fvxr, fvyr, infn);
325 >        sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d \"%s\"", fvxr, fvyr, infn);
326          if ((fp = popen(combuf, "r")) == NULL)
327                  syserror("popen");
328          getheader(fp, NULL, NULL);      /* skip header */
329 <        if (fgetresolu(&x, &y, fp) < 0 || x != fvxr | y != fvyr)
329 >        if (fgetresolu(&x, &y, fp) < 0 || (x != fvxr) | (y != fvyr))
330                  goto readerr;
331          for (y = 0; y < fvyr; y++)
332                  if (freadscan(fovscan(y), fvxr, fp) < 0)
333                          goto readerr;
334          pclose(fp);
337        lwmin = 1e10;                   /* find extrema */
338        lwmax = 0.;
339        for (y = 0; y < fvyr; y++)
340                for (x = 0; x < fvxr; x++) {
341                        l = plum(fovscan(y)[x]);
342                        if (l < lwmin) lwmin = l;
343                        if (l > lwmax) lwmax = l;
344                }
345        if (lwmin < LMIN) lwmin = LMIN;
346        if (lwmax > LMAX) lwmax = LMAX;
347                                        /* compute histogram */
348        bwmin = Bl(lwmin)*(1. - .01/HISTRES);
349        bwmax = Bl(lwmax)*(1. + .01/HISTRES);
350        bwavg = 0.;
351        for (y = 0; y < fvyr; y++)
352                for (x = 0; x < fvxr; x++) {
353                        if (what2do & DO_CWEIGHT &&
354                                        frandom() > centprob(x,y))
355                                continue;
356                        l = plum(fovscan(y)[x]);
357                        b = Bl(l);
358                        if (b < bwmin) continue;
359                        if (b > bwmax) continue;
360                        bwavg += b;
361                        bwhc(b)++;
362                        histot++;
363                }
364        bwavg /= (double)histot;
335          return;
336   readerr:
337          fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
# Line 372 | Line 342 | readerr:
342  
343   check2do()              /* check histogram to see what isn't worth doing */
344   {
345 <        long    sum;
345 >        double  sum;
346          double  b, l;
347          register int    i;
348  
349                                          /* check for within display range */
350 <        l = Lb(bwmax)/Lb(bwmin);
381 <        if (l <= ldmax/ldmin)
350 >        if (bwmax - bwmin <= Bldmax - Bldmin)
351                  what2do |= DO_LINEAR;
352                                          /* determine if veiling significant */
353 <        if (l < 100.)                   /* heuristic */
353 >        if (bwmax - bwmin < 4.5)                /* heuristic */
354                  what2do &= ~DO_VEIL;
355  
356          if (!(what2do & (DO_ACUITY|DO_COLOR)))
357                  return;
358                                          /* find 5th percentile */
359 <        sum = histot*0.05 + .5;
359 >        sum = histot*0.05;
360          for (i = 0; i < HISTRES; i++)
361                  if ((sum -= bwhist[i]) <= 0)
362                          break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines