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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines