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.6 by greg, Tue Jan 7 14:48:50 1997 UTC vs.
Revision 3.15 by schorsch, Thu Jun 5 19:29:34 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 "pcond.h"
11  
13 #include "random.h"
12  
15
13   #define LDMAX           100             /* default max. display luminance */
14 < #define LDMINF          0.01            /* default min. display lum. factor */
14 > #define LDDYN           32              /* default dynamic range */
15  
16   int     what2do = 0;                    /* desired adjustments */
17  
18   double  ldmax = LDMAX;                  /* maximum output luminance */
19 < double  ldmin = 0.;                     /* minimum output luminance */
20 < double  Bldmin, Bldmax;                 /* Bl(ldmin) and Bl(ldmax) */
19 > double  lddyn = LDDYN;                  /* display dynamic range */
20 > double  Bldmin, Bldmax;                 /* Bl(ldmax/lddyn) and Bl(ldmax) */
21  
22   char    *progname;                      /* global argv[0] */
23  
# Line 30 | Line 27 | FILE   *mapfp = NULL;                  /* tone-mapping function stream
27   VIEW    ourview = STDVIEW;              /* picture view */
28   int     gotview = 0;                    /* picture has view */
29   double  pixaspect = 1.0;                /* pixel aspect ratio */
30 + double  fixfrac = 0.;                   /* histogram share due to fixations */
31   RESOLU  inpres;                         /* input picture resolution */
32  
33   COLOR   *fovimg;                        /* foveal (1 degree) averaged image */
34 < short   fvxr, fvyr;                     /* foveal image resolution */
35 < int     bwhist[HISTRES];                /* luminance histogram */
36 < long    histot;                         /* total count of histogram */
34 > int     fvxr, fvyr;                     /* foveal image resolution */
35 > float   *crfimg;                        /* contrast reduction factors */
36 > short   (*fixlst)[2];                   /* fixation history list */
37 > int     nfixations;                     /* number of fixation points */
38 > double  bwhist[HISTRES];                /* luminance histogram */
39 > double  histot;                         /* total count of histogram */
40   double  bwmin, bwmax;                   /* histogram limits */
41   double  bwavg;                          /* mean brightness */
42  
# Line 78 | 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 104 | 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 'm':
131 >                case 'x':
132                          if (i+1 >= argc) goto userr;
133                          if ((mapfp = fopen(argv[++i], "w")) == NULL) {
134                                  fprintf(stderr,
# Line 126 | Line 140 | char   *argv[];
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)
137 <                ldmin = ldmax*LDMINF;
138 <        else if (ldmin >= ldmax) {
139 <                fprintf(stderr, "%s: Ldmin (%f) >= Ldmax (%f)!\n", progname,
140 <                                ldmin, ldmax);
141 <                exit(1);
142 <        }
143 <        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
167 <        setmode(fileno(infp), O_BINARY);
153 <        setmode(fileno(stdout), O_BINARY);
154 < #endif
166 >        SET_FILE_BINARY(infp);
167 >        SET_FILE_BINARY(stdout);
168          getahead();                     /* load input header */
169          printargs(argc, argv, stdout);  /* add to output header */
170 <        if (outprims != inprims)
170 >        if (mbcalfile == NULL & outprims != stdprims)
171                  fputprims(outprims, stdout);
172 +        if ((what2do & (DO_PREHIST|DO_VEIL|DO_ACUITY)) != DO_PREHIST)
173 +                getfovimg();            /* get foveal sample image? */
174 +        if (what2do&DO_PREHIST)         /* get histogram? */
175 +                gethisto(stdin);
176 +        else if (what2do&DO_FIXHIST)    /* get fixation history? */
177 +                getfixations(stdin);
178          mapimage();                     /* map the picture */
179          if (mapfp != NULL)              /* write out basic mapping */
180                  putmapping(mapfp);
181          exit(0);
182   userr:
183 <        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][-m mapfile] inpic [outpic]\n",
183 >        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",
184                          progname);
185          exit(1);
186   #undef bool
# Line 187 | Line 206 | char   *s;
206                  if (!strcmp(fmt,COLRFMT)) lumf = rgblum;
207                  else if (!strcmp(fmt,CIEFMT)) lumf = cielum;
208                  else lumf = NULL;
209 <                return;                 /* don't echo */
209 >                return(0);              /* don't echo */
210          }
211          if (isprims(s)) {               /* get input primaries */
212                  primsval(inprimS, s);
213                  inprims= inprimS;
214 <                return;                 /* don't echo */
214 >                return(0);              /* don't echo */
215          }
216          if (isexpos(s)) {               /* picture exposure */
217                  inpexp *= exposval(s);
218 <                return;                 /* don't echo */
218 >                return(0);              /* don't echo */
219          }
220          if (isaspect(s))                /* pixel aspect ratio */
221                  pixaspect *= aspectval(s);
222          if (isview(s))                  /* image view */
223                  gotview += sscanview(&ourview, s);
224 <        fputs(s, stdout);
224 >        return(fputs(s, stdout));
225   }
226  
227  
# Line 217 | Line 236 | getahead()                     /* load picture header */
236                  exit(1);
237          }
238          if (lumf == rgblum)
239 <                comprgb2xyzmat(inrgb2xyz, inprims);
239 >                comprgb2xyzWBmat(inrgb2xyz, inprims);
240          else if (mbcalfile != NULL) {
241                  fprintf(stderr, "%s: macbethcal only works with RGB pictures\n",
242                                  progname);
# Line 248 | Line 267 | mapimage()                             /* map picture and send to stdout */
267   {
268          COLOR   *scan;
269  
270 < #ifdef DEBUG
252 <        fprintf(stderr, "%s: generating histogram...", progname);
253 < #endif
254 <        fovhist();                      /* generate adaptation histogram */
255 < #ifdef DEBUG
256 <        fputs("done\n", stderr);
257 < #endif
270 >        comphist();                     /* generate adaptation histogram */
271          check2do();                     /* modify what2do flags */
272 <        if (what2do&DO_VEIL) {
273 < #ifdef DEBUG
274 <                fprintf(stderr, "%s: computing veiling...", progname);
272 >        if (what2do&DO_VEIL)
273 >                compveil();             /* compute veil image */
274 >        if (!(what2do&DO_LINEAR))
275 >                if (mkbrmap() < 0)      /* make tone map */
276 >                        what2do |= DO_LINEAR;   /* failed! -- use linear */
277 > #if ADJ_VEIL
278 >                else if (what2do&DO_VEIL)
279 >                        adjveil();      /* else adjust veil image */
280   #endif
263                compveil();
264 #ifdef DEBUG
265                fputs("done\n", stderr);
266 #endif
267        }
268 #ifdef DEBUG
269        fprintf(stderr, "%s: computing brightness mapping...", progname);
270 #endif
271        if (!(what2do&DO_LINEAR) && mkbrmap() < 0) {    /* make tone map */
272                what2do |= DO_LINEAR;           /* use linear scaling */
273 #ifdef DEBUG
274                fputs("failed!\n", stderr);
275        } else
276                fputs("done\n", stderr);
277 #else
278        }
279 #endif
281          if (what2do&DO_LINEAR) {
282                  if (scalef <= FTINY) {
283                          if (what2do&DO_HSENS)
# Line 286 | Line 287 | mapimage()                             /* map picture and send to stdout */
287                                  scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg);
288                          scalef *= WHTEFFICACY/(inpexp*ldmax);
289                  }
289 #ifdef DEBUG
290                fprintf(stderr, "%s: linear scaling factor = %f\n",
291                                progname, scalef);
292 #endif
290                  fputexpos(inpexp*scalef, stdout);       /* record exposure */
291                  if (lumf == cielum) scalef /= WHTEFFICACY;
292          }
293 <        putchar('\n');                  /* complete header */
293 >        fputformat(COLRFMT, stdout);    /* complete header */
294 >        putchar('\n');
295          fputsresolu(&inpres, stdout);   /* resolution doesn't change */
296 <
296 >                                        /* condition our image */
297          for (scan = firstscan(); scan != NULL; scan = nextscan())
298                  if (fwritescan(scan, scanlen(&inpres), stdout) < 0) {
299                          fprintf(stderr, "%s: scanline write error\n",
# Line 305 | Line 303 | mapimage()                             /* map picture and send to stdout */
303   }
304  
305  
306 < double
309 < centprob(x, y)                  /* center-weighting probability function */
310 < int     x, y;
306 > getfovimg()                     /* load foveal sampled image */
307   {
312        double  xr, yr;
313
314        xr = (x+.5)/fvxr - .5;
315        yr = (y+.5)/fvyr - .5;
316        return(1. - xr*xr - yr*yr);     /* radial, == 0.5 at corners */
317 }
318
319
320 fovhist()                       /* create foveal sampled image and histogram */
321 {
308          extern FILE     *popen();
309          char    combuf[128];
324        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 */
# Line 346 | Line 331 | fovhist()                      /* create foveal sampled image and histogr
331                  if (freadscan(fovscan(y), fvxr, fp) < 0)
332                          goto readerr;
333          pclose(fp);
349        lwmin = 1e10;                   /* find extrema */
350        lwmax = 0.;
351        for (y = 0; y < fvyr; y++)
352                for (x = 0; x < fvxr; x++) {
353                        l = plum(fovscan(y)[x]);
354                        if (l < lwmin) lwmin = l;
355                        if (l > lwmax) lwmax = l;
356                }
357        if (lwmin < LMIN) lwmin = LMIN;
358        if (lwmax > LMAX) lwmax = LMAX;
359                                        /* compute histogram */
360        bwmin = Bl(lwmin)*(1. - .01/HISTRES);
361        bwmax = Bl(lwmax)*(1. + .01/HISTRES);
362        bwavg = 0.;
363        for (y = 0; y < fvyr; y++)
364                for (x = 0; x < fvxr; x++) {
365                        if (what2do & DO_CWEIGHT &&
366                                        frandom() > centprob(x,y))
367                                continue;
368                        l = plum(fovscan(y)[x]);
369                        b = Bl(l);
370                        if (b < bwmin) continue;
371                        if (b > bwmax) continue;
372                        bwavg += b;
373                        bwhc(b)++;
374                        histot++;
375                }
376        bwavg /= (double)histot;
334          return;
335   readerr:
336          fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
# Line 384 | 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);
393 <        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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines