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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines