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.11 by greg, Wed Mar 19 13:04:09 1997 UTC

# Line 1 | Line 1
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";
# Line 10 | Line 10 | 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 */
15 > #define LDDYN           32              /* default dynamic range */
16  
17   int     what2do = 0;                    /* desired adjustments */
18  
19   double  ldmax = LDMAX;                  /* maximum output luminance */
20 < double  ldmin = 0.;                     /* minimum output luminance */
21 < double  Bldmin, Bldmax;                 /* Bl(ldmin) and Bl(ldmax) */
20 > double  lddyn = LDDYN;                  /* display dynamic range */
21 > double  Bldmin, Bldmax;                 /* Bl(ldmax/lddyn) and Bl(ldmax) */
22  
23   char    *progname;                      /* global argv[0] */
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 */
31 + double  fixfrac = 0.;                   /* histogram share due to fixations */
32   RESOLU  inpres;                         /* input picture resolution */
33  
34   COLOR   *fovimg;                        /* foveal (1 degree) averaged image */
35   short   fvxr, fvyr;                     /* foveal image resolution */
36 < int     bwhist[HISTRES];                /* luminance histogram */
37 < long    histot;                         /* total count of histogram */
36 > short   (*fixlst)[2];                   /* fixation history list */
37 > int     nfixations;                     /* number of fixation points */
38 > float   bwhist[HISTRES];                /* luminance histogram */
39 > double  histot;                         /* total count of histogram */
40   double  bwmin, bwmax;                   /* histogram limits */
41   double  bwavg;                          /* mean brightness */
42  
# Line 77 | Line 79 | char   *argv[];
79                  case 'w':
80                          bool(DO_CWEIGHT);
81                          break;
82 +                case 'i':
83 +                        if (i+1 >= argc) goto userr;
84 +                        fixfrac = atof(argv[++i]);
85 +                        if (fixfrac > FTINY) what2do |= DO_FIXHIST;
86 +                        else what2do &= ~DO_FIXHIST;
87 +                        break;
88                  case 'l':
89                          bool(DO_LINEAR);
90                          break;
# Line 103 | Line 111 | char   *argv[];
111                          if (i+1 >= argc) goto userr;
112                          mbcalfile = argv[++i];
113                          break;
114 <                case 't':
114 >                case 'm':
115                          if (i+1 >= argc) goto userr;
116 +                        cwarpfile = argv[++i];
117 +                        break;
118 +                case 'u':
119 +                        if (i+1 >= argc) goto userr;
120                          ldmax = atof(argv[++i]);
121                          if (ldmax <= FTINY)
122                                  goto userr;
123                          break;
124 <                case 'b':
124 >                case 'd':
125                          if (i+1 >= argc) goto userr;
126 <                        ldmin = atof(argv[++i]);
126 >                        lddyn = atof(argv[++i]);
127                          break;
128 +                case 'x':
129 +                        if (i+1 >= argc) goto userr;
130 +                        if ((mapfp = fopen(argv[++i], "w")) == NULL) {
131 +                                fprintf(stderr,
132 +                                        "%s: cannot open for writing\n",
133 +                                                argv[i]);
134 +                                exit(1);
135 +                        }
136 +                        break;
137                  default:
138                          goto userr;
139                  }
140 <        if (mbcalfile != NULL & outprims != stdprims) {
141 <                fprintf(stderr, "%s: only one of -p or -f option supported\n",
140 >        if ((mbcalfile != NULL) + (cwarpfile != NULL) +
141 >                        (outprims != stdprims) > 1) {
142 >                fprintf(stderr,
143 >                        "%s: only one of -p, -m or -f option supported\n",
144                                  progname);
145                  exit(1);
146          }
147          if (outprims == stdprims & inprims != stdprims)
148                  outprims = inprims;
149 <        if (ldmin <= FTINY)
127 <                ldmin = ldmax*LDMINF;
128 <        else if (ldmin >= ldmax) {
129 <                fprintf(stderr, "%s: Ldmin (%f) >= Ldmax (%f)!\n", progname,
130 <                                ldmin, ldmax);
131 <                exit(1);
132 <        }
133 <        Bldmin = Bl(ldmin);
149 >        Bldmin = Bl(ldmax/lddyn);
150          Bldmax = Bl(ldmax);
151          if (i >= argc || i+2 < argc)
152                  goto userr;
153 +                                        /* open input file */
154          if ((infp = fopen(infn=argv[i], "r")) == NULL)
155                  syserror(infn);
156 +                                        /* open output file */
157          if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL)
158                  syserror(argv[i+1]);
159   #ifdef MSDOS
# Line 144 | Line 162 | char   *argv[];
162   #endif
163          getahead();                     /* load input header */
164          printargs(argc, argv, stdout);  /* add to output header */
165 <        if (outprims != inprims)
165 >        if (mbcalfile == NULL & outprims != stdprims)
166                  fputprims(outprims, stdout);
167 +        getfovimg();                    /* get foveal sample image */
168 +        if (what2do&DO_FIXHIST)         /* get fixation history? */
169 +                getfixations(stdin);
170          mapimage();                     /* map the picture */
171 +        if (mapfp != NULL)              /* write out basic mapping */
172 +                putmapping(mapfp);
173          exit(0);
174   userr:
175 <        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",
175 >        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][-u Ldmax][-d Lddyn][-x mapfile] inpic [outpic]\n",
176                          progname);
177          exit(1);
178   #undef bool
# Line 236 | Line 259 | mapimage()                             /* map picture and send to stdout */
259   {
260          COLOR   *scan;
261  
262 < #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
262 >        comphist();                     /* generate adaptation histogram */
263          check2do();                     /* modify what2do flags */
264 <        if (what2do&DO_VEIL) {
248 < #ifdef DEBUG
249 <                fprintf(stderr, "%s: computing veiling...", progname);
250 < #endif
264 >        if (what2do&DO_VEIL)
265                  compveil();
266 < #ifdef DEBUG
267 <                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
266 >        if (!(what2do&DO_LINEAR) && mkbrmap() < 0)      /* make tone map */
267 >                what2do |= DO_LINEAR;   /* failed! -- use linear scaling */
268          if (what2do&DO_LINEAR) {
269                  if (scalef <= FTINY) {
270                          if (what2do&DO_HSENS)
# Line 274 | Line 274 | mapimage()                             /* map picture and send to stdout */
274                                  scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg);
275                          scalef *= WHTEFFICACY/(inpexp*ldmax);
276                  }
277 #ifdef DEBUG
278                fprintf(stderr, "%s: linear scaling factor = %f\n",
279                                progname, scalef);
280 #endif
277                  fputexpos(inpexp*scalef, stdout);       /* record exposure */
278                  if (lumf == cielum) scalef /= WHTEFFICACY;
279          }
280 <        putchar('\n');                  /* complete header */
280 >        fputformat(COLRFMT, stdout);    /* complete header */
281 >        putchar('\n');
282          fputsresolu(&inpres, stdout);   /* resolution doesn't change */
283 <
283 >                                        /* condition our image */
284          for (scan = firstscan(); scan != NULL; scan = nextscan())
285                  if (fwritescan(scan, scanlen(&inpres), stdout) < 0) {
286                          fprintf(stderr, "%s: scanline write error\n",
# Line 293 | Line 290 | mapimage()                             /* map picture and send to stdout */
290   }
291  
292  
293 < double
297 < centprob(x, y)                  /* center-weighting probability function */
298 < int     x, y;
293 > getfovimg()                     /* load foveal sampled image */
294   {
300        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 {
295          extern FILE     *popen();
296          char    combuf[128];
312        double  l, b, lwmin, lwmax;
297          FILE    *fp;
298          int     x, y;
299 <
299 >                                                /* compute image size */
300          fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5;
301          if (fvxr < 2) fvxr = 2;
302          fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5;
# Line 324 | Line 308 | fovhist()                      /* create foveal sampled image and histogr
308          }
309          if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL)
310                  syserror("malloc");
311 <        sprintf(combuf, "pfilt -1 -b -x %d -y %d %s", fvxr, fvyr, infn);
311 >        sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d %s", fvxr, fvyr, infn);
312          if ((fp = popen(combuf, "r")) == NULL)
313                  syserror("popen");
314          getheader(fp, NULL, NULL);      /* skip header */
# Line 334 | Line 318 | fovhist()                      /* create foveal sampled image and histogr
318                  if (freadscan(fovscan(y), fvxr, fp) < 0)
319                          goto readerr;
320          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;
321          return;
322   readerr:
323          fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
# Line 372 | Line 328 | readerr:
328  
329   check2do()              /* check histogram to see what isn't worth doing */
330   {
331 <        long    sum;
331 >        double  sum;
332          double  b, l;
333          register int    i;
334  
335                                          /* check for within display range */
336 <        l = Lb(bwmax)/Lb(bwmin);
381 <        if (l <= ldmax/ldmin)
336 >        if (bwmax - bwmin <= Bldmax - Bldmin)
337                  what2do |= DO_LINEAR;
338                                          /* determine if veiling significant */
339 <        if (l < 100.)                   /* heuristic */
339 >        if (bwmax - bwmin < 4.5)                /* heuristic */
340                  what2do &= ~DO_VEIL;
341  
342          if (!(what2do & (DO_ACUITY|DO_COLOR)))
343                  return;
344                                          /* find 5th percentile */
345 <        sum = histot*0.05 + .5;
345 >        sum = histot*0.05;
346          for (i = 0; i < HISTRES; i++)
347                  if ((sum -= bwhist[i]) <= 0)
348                          break;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines