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.3 by greg, Sat Oct 5 11:16:51 1996 UTC vs.
Revision 3.13 by gwlarson, Tue Oct 27 09:08:26 1998 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 'I':
89 +                        bool(DO_PREHIST);
90 +                        break;
91                  case 'l':
92                          bool(DO_LINEAR);
93                          break;
# Line 103 | Line 114 | char   *argv[];
114                          if (i+1 >= argc) goto userr;
115                          mbcalfile = argv[++i];
116                          break;
117 <                case 't':
117 >                case 'm':
118                          if (i+1 >= argc) goto userr;
119 +                        cwarpfile = argv[++i];
120 +                        break;
121 +                case 'u':
122 +                        if (i+1 >= argc) goto userr;
123                          ldmax = atof(argv[++i]);
124                          if (ldmax <= FTINY)
125                                  goto userr;
126                          break;
127 <                case 'b':
127 >                case 'd':
128                          if (i+1 >= argc) goto userr;
129 <                        ldmin = atof(argv[++i]);
129 >                        lddyn = atof(argv[++i]);
130                          break;
131 +                case 'x':
132 +                        if (i+1 >= argc) goto userr;
133 +                        if ((mapfp = fopen(argv[++i], "w")) == NULL) {
134 +                                fprintf(stderr,
135 +                                        "%s: cannot open for writing\n",
136 +                                                argv[i]);
137 +                                exit(1);
138 +                        }
139 +                        break;
140                  default:
141                          goto userr;
142                  }
143 <        if (mbcalfile != NULL & outprims != stdprims) {
144 <                fprintf(stderr, "%s: only one of -p or -f option supported\n",
143 >        if ((what2do & (DO_FIXHIST|DO_PREHIST)) == (DO_FIXHIST|DO_PREHIST)) {
144 >                fprintf(stderr, "%s: only one of -i or -I option\n", progname);
145 >                exit(1);
146 >        }
147 >        if ((mbcalfile != NULL) + (cwarpfile != NULL) +
148 >                        (outprims != stdprims) > 1) {
149 >                fprintf(stderr,
150 >                        "%s: only one of -p, -m or -f option supported\n",
151                                  progname);
152                  exit(1);
153          }
154          if (outprims == stdprims & inprims != stdprims)
155                  outprims = inprims;
156 <        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);
156 >        Bldmin = Bl(ldmax/lddyn);
157          Bldmax = Bl(ldmax);
158          if (i >= argc || i+2 < argc)
159                  goto userr;
160 +                                        /* open input file */
161          if ((infp = fopen(infn=argv[i], "r")) == NULL)
162                  syserror(infn);
163 +                                        /* open output file */
164          if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL)
165                  syserror(argv[i+1]);
166   #ifdef MSDOS
# Line 144 | Line 169 | char   *argv[];
169   #endif
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 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) {
248 < #ifdef DEBUG
249 <                fprintf(stderr, "%s: computing veiling...", progname);
250 < #endif
274 >        if (what2do&DO_VEIL)
275                  compveil();
276 < #ifdef DEBUG
277 <                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
276 >        if (!(what2do&DO_LINEAR) && mkbrmap() < 0)      /* make tone map */
277 >                what2do |= DO_LINEAR;   /* failed! -- use linear scaling */
278          if (what2do&DO_LINEAR) {
279                  if (scalef <= FTINY) {
280                          if (what2do&DO_HSENS)
# Line 274 | Line 284 | mapimage()                             /* map picture and send to stdout */
284                                  scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg);
285                          scalef *= WHTEFFICACY/(inpexp*ldmax);
286                  }
287 < #ifdef DEBUG
278 <                fprintf(stderr, "%s: linear scaling factor = %f\n",
279 <                                progname, scalef);
280 < #endif
281 <                if (scalef < 0.99 | scalef > 1.01)
282 <                        fputexpos(scalef, stdout);      /* write in header */
287 >                fputexpos(inpexp*scalef, stdout);       /* record exposure */
288                  if (lumf == cielum) scalef /= WHTEFFICACY;
289          }
290 <        putchar('\n');                  /* complete header */
290 >        fputformat(COLRFMT, stdout);    /* complete header */
291 >        putchar('\n');
292          fputsresolu(&inpres, stdout);   /* resolution doesn't change */
293 <
293 >                                        /* condition our image */
294          for (scan = firstscan(); scan != NULL; scan = nextscan())
295                  if (fwritescan(scan, scanlen(&inpres), stdout) < 0) {
296                          fprintf(stderr, "%s: scanline write error\n",
# Line 294 | Line 300 | mapimage()                             /* map picture and send to stdout */
300   }
301  
302  
303 < double
298 < centprob(x, y)                  /* center-weighting probability function */
299 < int     x, y;
303 > getfovimg()                     /* load foveal sampled image */
304   {
301        double  xr, yr;
302
303        xr = (x+.5)/fvxr - .5;
304        yr = (y+.5)/fvyr - .5;
305        return(1. - xr*xr - yr*yr);     /* radial, == 0.5 at corners */
306 }
307
308
309 fovhist()                       /* create foveal sampled image and histogram */
310 {
305          extern FILE     *popen();
306          char    combuf[128];
313        double  l, b, lwmin, lwmax;
307          FILE    *fp;
308          int     x, y;
309 <
309 >                                                /* compute image size */
310          fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5;
311          if (fvxr < 2) fvxr = 2;
312          fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5;
# Line 325 | Line 318 | fovhist()                      /* create foveal sampled image and histogr
318          }
319          if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL)
320                  syserror("malloc");
321 <        sprintf(combuf, "pfilt -1 -b -x %d -y %d %s", fvxr, fvyr, infn);
321 >        sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d %s", fvxr, fvyr, infn);
322          if ((fp = popen(combuf, "r")) == NULL)
323                  syserror("popen");
324          getheader(fp, NULL, NULL);      /* skip header */
# Line 335 | Line 328 | fovhist()                      /* create foveal sampled image and histogr
328                  if (freadscan(fovscan(y), fvxr, fp) < 0)
329                          goto readerr;
330          pclose(fp);
338        lwmin = 1e10;                   /* find extrema */
339        lwmax = 0.;
340        for (y = 0; y < fvyr; y++)
341                for (x = 0; x < fvxr; x++) {
342                        l = plum(fovscan(y)[x]);
343                        if (l < lwmin) lwmin = l;
344                        if (l > lwmax) lwmax = l;
345                }
346        if (lwmin < LMIN) lwmin = LMIN;
347        if (lwmax > LMAX) lwmax = LMAX;
348                                        /* compute histogram */
349        bwmin = Bl(lwmin)*(1. - .01/HISTRES);
350        bwmax = Bl(lwmax)*(1. + .01/HISTRES);
351        bwavg = 0.;
352        for (y = 0; y < fvyr; y++)
353                for (x = 0; x < fvxr; x++) {
354                        if (what2do & DO_CWEIGHT &&
355                                        frandom() > centprob(x,y))
356                                continue;
357                        l = plum(fovscan(y)[x]);
358                        b = Bl(l);
359                        if (b < bwmin) continue;
360                        if (b > bwmax) continue;
361                        bwavg += b;
362                        bwhc(b)++;
363                        histot++;
364                }
365        bwavg /= (double)histot;
331          return;
332   readerr:
333          fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
# Line 373 | Line 338 | readerr:
338  
339   check2do()              /* check histogram to see what isn't worth doing */
340   {
341 <        long    sum;
341 >        double  sum;
342          double  b, l;
343          register int    i;
344  
345                                          /* check for within display range */
346 <        l = Lb(bwmax)/Lb(bwmin);
382 <        if (l <= ldmax/ldmin)
346 >        if (bwmax - bwmin <= Bldmax - Bldmin)
347                  what2do |= DO_LINEAR;
348                                          /* determine if veiling significant */
349 <        if (l < 100.)                   /* heuristic */
349 >        if (bwmax - bwmin < 4.5)                /* heuristic */
350                  what2do &= ~DO_VEIL;
351  
352          if (!(what2do & (DO_ACUITY|DO_COLOR)))
353                  return;
354                                          /* find 5th percentile */
355 <        sum = histot*0.05 + .5;
355 >        sum = histot*0.05;
356          for (i = 0; i < HISTRES; i++)
357                  if ((sum -= bwhist[i]) <= 0)
358                          break;
# Line 397 | Line 361 | check2do()             /* check histogram to see what isn't worth
361                                          /* determine if acuity adj. useful */
362          if (what2do&DO_ACUITY &&
363                          hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) +
364 <                        inpres.yr/sqrt(ourview.vn2))/(2.*180./PI*2.))
364 >                        inpres.yr/sqrt(ourview.vn2))/(2.*180./PI))
365                  what2do &= ~DO_ACUITY;
366                                          /* color sensitivity loss? */
367 <        if (l >= 6.0)
367 >        if (l >= TopMesopic)
368                  what2do &= ~DO_COLOR;
369   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines