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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines