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.4 by greg, Thu Oct 10 17:09:24 1996 UTC vs.
Revision 3.22 by greg, Sun Nov 14 16:57:18 2004 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  
45   double  scalef = 0.;                    /* linear scaling factor */
46  
47 + static gethfunc headline;
48 + static void getahead(void);
49 + static void mapimage(void);
50 + static void getfovimg(void);
51 + static void check2do(void);
52  
53 < main(argc, argv)
54 < int     argc;
55 < char    *argv[];
53 >
54 >
55 > int
56 > main(
57 >        int     argc,
58 >        char    *argv[]
59 > )
60   {
61          static RGBPRIMS outprimS;
62          int     i;
# Line 77 | Line 90 | char   *argv[];
90                  case 'w':
91                          bool(DO_CWEIGHT);
92                          break;
93 +                case 'i':
94 +                        if (i+1 >= argc) goto userr;
95 +                        fixfrac = atof(argv[++i]);
96 +                        if (fixfrac > FTINY) what2do |= DO_FIXHIST;
97 +                        else what2do &= ~DO_FIXHIST;
98 +                        break;
99 +                case 'I':
100 +                        bool(DO_PREHIST);
101 +                        break;
102                  case 'l':
103                          bool(DO_LINEAR);
104                          break;
# Line 95 | Line 117 | char   *argv[];
117                  case 'e':
118                          if (i+1 >= argc) goto userr;
119                          scalef = atof(argv[++i]);
120 <                        if (argv[i][0] == '+' | argv[i][0] == '-')
120 >                        if ((argv[i][0] == '+') | (argv[i][0] == '-'))
121                                  scalef = pow(2.0, scalef);
122                          what2do |= DO_LINEAR;
123                          break;
# Line 103 | Line 125 | char   *argv[];
125                          if (i+1 >= argc) goto userr;
126                          mbcalfile = argv[++i];
127                          break;
128 <                case 't':
128 >                case 'm':
129                          if (i+1 >= argc) goto userr;
130 +                        cwarpfile = argv[++i];
131 +                        break;
132 +                case 'u':
133 +                        if (i+1 >= argc) goto userr;
134                          ldmax = atof(argv[++i]);
135                          if (ldmax <= FTINY)
136                                  goto userr;
137                          break;
138 <                case 'b':
138 >                case 'd':
139                          if (i+1 >= argc) goto userr;
140 <                        ldmin = atof(argv[++i]);
140 >                        lddyn = atof(argv[++i]);
141                          break;
142 +                case 'x':
143 +                        if (i+1 >= argc) goto userr;
144 +                        if ((mapfp = fopen(argv[++i], "w")) == NULL) {
145 +                                fprintf(stderr,
146 +                                        "%s: cannot open for writing\n",
147 +                                                argv[i]);
148 +                                exit(1);
149 +                        }
150 +                        break;
151                  default:
152                          goto userr;
153                  }
154 <        if (mbcalfile != NULL & outprims != stdprims) {
155 <                fprintf(stderr, "%s: only one of -p or -f option supported\n",
121 <                                progname);
154 >        if ((what2do & (DO_FIXHIST|DO_PREHIST)) == (DO_FIXHIST|DO_PREHIST)) {
155 >                fprintf(stderr, "%s: only one of -i or -I option\n", progname);
156                  exit(1);
157          }
158 <        if (outprims == stdprims & inprims != stdprims)
159 <                outprims = inprims;
160 <        if (ldmin <= FTINY)
161 <                ldmin = ldmax*LDMINF;
162 <        else if (ldmin >= ldmax) {
129 <                fprintf(stderr, "%s: Ldmin (%f) >= Ldmax (%f)!\n", progname,
130 <                                ldmin, ldmax);
158 >        if ((mbcalfile != NULL) + (cwarpfile != NULL) +
159 >                        (outprims != stdprims) > 1) {
160 >                fprintf(stderr,
161 >                        "%s: only one of -p, -m or -f option supported\n",
162 >                                progname);
163                  exit(1);
164          }
165 <        Bldmin = Bl(ldmin);
165 >        if ((outprims == stdprims) & (inprims != stdprims))
166 >                outprims = inprims;
167 >        Bldmin = Bl(ldmax/lddyn);
168          Bldmax = Bl(ldmax);
169          if (i >= argc || i+2 < argc)
170                  goto userr;
171 +                                        /* open input file */
172          if ((infp = fopen(infn=argv[i], "r")) == NULL)
173                  syserror(infn);
174 +                                        /* open output file */
175          if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL)
176                  syserror(argv[i+1]);
177 < #ifdef MSDOS
178 <        setmode(fileno(infp), O_BINARY);
143 <        setmode(fileno(stdout), O_BINARY);
144 < #endif
177 >        SET_FILE_BINARY(infp);
178 >        SET_FILE_BINARY(stdout);
179          getahead();                     /* load input header */
180          printargs(argc, argv, stdout);  /* add to output header */
181 <        if (outprims != inprims)
181 >        if ((mbcalfile == NULL) & (outprims != stdprims))
182                  fputprims(outprims, stdout);
183 +        if ((what2do & (DO_PREHIST|DO_VEIL|DO_ACUITY)) != DO_PREHIST)
184 +                getfovimg();            /* get foveal sample image? */
185 +        if (what2do&DO_PREHIST)         /* get histogram? */
186 +                gethisto(stdin);
187 +        else if (what2do&DO_FIXHIST)    /* get fixation history? */
188 +                getfixations(stdin);
189          mapimage();                     /* map the picture */
190 +        if (mapfp != NULL)              /* write out basic mapping */
191 +                putmapping(mapfp);
192          exit(0);
193   userr:
194 <        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",
194 >        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",
195                          progname);
196          exit(1);
197 +        return 1; /* pro forma return */
198   #undef bool
199   }
200  
201  
202 < syserror(s)                             /* report system error and exit */
203 < char    *s;
202 > extern void
203 > syserror(                               /* report system error and exit */
204 >        char    *s
205 > )
206   {
207          fprintf(stderr, "%s: ", progname);
208          perror(s);
# Line 165 | Line 210 | char   *s;
210   }
211  
212  
213 < headline(s)                             /* process header line */
214 < char    *s;
213 > static int
214 > headline(                               /* process header line */
215 >        char    *s,
216 >        void    *p
217 > )
218   {
219          static RGBPRIMS inprimS;
220          char    fmt[32];
# Line 175 | Line 223 | char   *s;
223                  if (!strcmp(fmt,COLRFMT)) lumf = rgblum;
224                  else if (!strcmp(fmt,CIEFMT)) lumf = cielum;
225                  else lumf = NULL;
226 <                return;                 /* don't echo */
226 >                return(0);              /* don't echo */
227          }
228          if (isprims(s)) {               /* get input primaries */
229                  primsval(inprimS, s);
230                  inprims= inprimS;
231 <                return;                 /* don't echo */
231 >                return(0);              /* don't echo */
232          }
233          if (isexpos(s)) {               /* picture exposure */
234                  inpexp *= exposval(s);
235 <                return;                 /* don't echo */
235 >                return(0);              /* don't echo */
236          }
237          if (isaspect(s))                /* pixel aspect ratio */
238                  pixaspect *= aspectval(s);
239          if (isview(s))                  /* image view */
240                  gotview += sscanview(&ourview, s);
241 <        fputs(s, stdout);
241 >        return(fputs(s, stdout));
242   }
243  
244  
245 < getahead()                      /* load picture header */
245 > static void
246 > getahead(void)                  /* load picture header */
247   {
248          char    *err;
249  
# Line 205 | Line 254 | getahead()                     /* load picture header */
254                  exit(1);
255          }
256          if (lumf == rgblum)
257 <                comprgb2xyzmat(inrgb2xyz, inprims);
257 >                comprgb2xyzWBmat(inrgb2xyz, inprims);
258          else if (mbcalfile != NULL) {
259                  fprintf(stderr, "%s: macbethcal only works with RGB pictures\n",
260                                  progname);
261                  exit(1);
262          }
263 <        if (!gotview || ourview.type == VT_PAR) {
264 <                copystruct(&ourview, &stdview);
263 >        if (!gotview || ourview.type == VT_PAR ||
264 >                        ourview.horiz <= 3. | ourview.vert <= 3.) {
265 >                ourview = stdview;
266                  ourview.type = VT_PER;
267                  if (pixaspect*inpres.yr < inpres.xr) {
268                          ourview.horiz = 40.0;
# Line 232 | Line 282 | getahead()                     /* load picture header */
282   }
283  
284  
285 < mapimage()                              /* map picture and send to stdout */
285 > static void
286 > mapimage(void)                          /* map picture and send to stdout */
287   {
288          COLOR   *scan;
289  
290 < #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
290 >        comphist();                     /* generate adaptation histogram */
291          check2do();                     /* modify what2do flags */
292 <        if (what2do&DO_VEIL) {
293 < #ifdef DEBUG
294 <                fprintf(stderr, "%s: computing veiling...", progname);
292 >        if (what2do&DO_VEIL)
293 >                compveil();             /* compute veil image */
294 >        if (!(what2do&DO_LINEAR))
295 >                if (mkbrmap() < 0)      /* make tone map */
296 >                        what2do |= DO_LINEAR;   /* failed! -- use linear */
297 > #if ADJ_VEIL
298 >                else if (what2do&DO_VEIL)
299 >                        adjveil();      /* else adjust veil image */
300   #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
301          if (what2do&DO_LINEAR) {
302                  if (scalef <= FTINY) {
303                          if (what2do&DO_HSENS)
# Line 274 | Line 307 | mapimage()                             /* map picture and send to stdout */
307                                  scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg);
308                          scalef *= WHTEFFICACY/(inpexp*ldmax);
309                  }
277 #ifdef DEBUG
278                fprintf(stderr, "%s: linear scaling factor = %f\n",
279                                progname, scalef);
280 #endif
310                  fputexpos(inpexp*scalef, stdout);       /* record exposure */
311                  if (lumf == cielum) scalef /= WHTEFFICACY;
312          }
313 <        putchar('\n');                  /* complete header */
313 >        fputformat(COLRFMT, stdout);    /* complete header */
314 >        putchar('\n');
315          fputsresolu(&inpres, stdout);   /* resolution doesn't change */
316 <
316 >                                        /* condition our image */
317          for (scan = firstscan(); scan != NULL; scan = nextscan())
318                  if (fwritescan(scan, scanlen(&inpres), stdout) < 0) {
319                          fprintf(stderr, "%s: scanline write error\n",
# Line 293 | Line 323 | mapimage()                             /* map picture and send to stdout */
323   }
324  
325  
326 < double
327 < centprob(x, y)                  /* center-weighting probability function */
298 < int     x, y;
326 > static void
327 > getfovimg(void)                 /* load foveal sampled image */
328   {
329 <        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;
329 >        char    combuf[PATH_MAX];
330          FILE    *fp;
331          int     x, y;
332 <
332 >                                                /* compute image size */
333          fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5;
334          if (fvxr < 2) fvxr = 2;
335          fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5;
336          if (fvyr < 2) fvyr = 2;
337 <        if (!(inpres.or & YMAJOR)) {            /* picture is rotated? */
337 >        if (!(inpres.rt & YMAJOR)) {            /* picture is rotated? */
338                  y = fvyr;
339                  fvyr = fvxr;
340                  fvxr = y;
341          }
342          if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL)
343                  syserror("malloc");
344 <        sprintf(combuf, "pfilt -1 -b -x %d -y %d %s", fvxr, fvyr, infn);
344 >        sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d \"%s\"", fvxr, fvyr, infn);
345          if ((fp = popen(combuf, "r")) == NULL)
346                  syserror("popen");
347          getheader(fp, NULL, NULL);      /* skip header */
348 <        if (fgetresolu(&x, &y, fp) < 0 || x != fvxr | y != fvyr)
348 >        if (fgetresolu(&x, &y, fp) < 0 || (x != fvxr) | (y != fvyr))
349                  goto readerr;
350          for (y = 0; y < fvyr; y++)
351                  if (freadscan(fovscan(y), fvxr, fp) < 0)
352                          goto readerr;
353          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;
354          return;
355   readerr:
356          fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
# Line 370 | Line 359 | readerr:
359   }
360  
361  
362 < check2do()              /* check histogram to see what isn't worth doing */
362 > static void
363 > check2do(void)          /* check histogram to see what isn't worth doing */
364   {
365 <        long    sum;
365 >        double  sum;
366          double  b, l;
367          register int    i;
368  
369                                          /* check for within display range */
370 <        l = Lb(bwmax)/Lb(bwmin);
381 <        if (l <= ldmax/ldmin)
370 >        if (bwmax - bwmin <= Bldmax - Bldmin)
371                  what2do |= DO_LINEAR;
372                                          /* determine if veiling significant */
373 <        if (l < 100.)                   /* heuristic */
373 >        if (bwmax - bwmin < 4.5)                /* heuristic */
374                  what2do &= ~DO_VEIL;
375  
376          if (!(what2do & (DO_ACUITY|DO_COLOR)))
377                  return;
378                                          /* find 5th percentile */
379 <        sum = histot*0.05 + .5;
379 >        sum = histot*0.05;
380          for (i = 0; i < HISTRES; i++)
381                  if ((sum -= bwhist[i]) <= 0)
382                          break;
# Line 396 | Line 385 | check2do()             /* check histogram to see what isn't worth
385                                          /* determine if acuity adj. useful */
386          if (what2do&DO_ACUITY &&
387                          hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) +
388 <                        inpres.yr/sqrt(ourview.vn2))/(2.*180./PI*2.))
388 >                        inpres.yr/sqrt(ourview.vn2))/(2.*180./PI))
389                  what2do &= ~DO_ACUITY;
390                                          /* color sensitivity loss? */
391 <        if (l >= 6.0)
391 >        if (l >= TopMesopic)
392                  what2do &= ~DO_COLOR;
393   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines