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.7 by greg, Wed Jan 8 21:40:38 1997 UTC vs.
Revision 3.35 by greg, Sat Jun 7 05:09:46 2025 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines