ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.35
Committed: Sat Jun 7 05:09:46 2025 UTC (18 minutes, 40 seconds ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 3.34: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

# User Rev Content
1 greg 3.1 #ifndef lint
2 greg 3.35 static const char RCSid[] = "$Id: pcond.c,v 3.34 2025/06/03 21:31:51 greg Exp $";
3 greg 3.1 #endif
4     /*
5     * Condition Radiance picture for display/output
6 greg 3.14 * Added white-balance adjustment 10/01 (GW).
7 greg 3.1 */
8    
9 schorsch 3.15 #include "platform.h"
10 greg 3.1 #include "pcond.h"
11    
12    
13     #define LDMAX 100 /* default max. display luminance */
14 greg 3.31 #define LDDYN 100 /* default dynamic range */
15 greg 3.1
16     int what2do = 0; /* desired adjustments */
17    
18     double ldmax = LDMAX; /* maximum output luminance */
19 greg 3.11 double lddyn = LDDYN; /* display dynamic range */
20     double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */
21 greg 3.1
22     char *infn; /* input file name */
23     FILE *infp; /* input stream */
24 greg 3.6 FILE *mapfp = NULL; /* tone-mapping function stream */
25 greg 3.1 VIEW ourview = STDVIEW; /* picture view */
26     int gotview = 0; /* picture has view */
27     double pixaspect = 1.0; /* pixel aspect ratio */
28 greg 3.9 double fixfrac = 0.; /* histogram share due to fixations */
29 greg 3.1 RESOLU inpres; /* input picture resolution */
30    
31     COLOR *fovimg; /* foveal (1 degree) averaged image */
32 greg 3.14 int fvxr, fvyr; /* foveal image resolution */
33     float *crfimg; /* contrast reduction factors */
34 greg 3.9 short (*fixlst)[2]; /* fixation history list */
35     int nfixations; /* number of fixation points */
36 greg 3.14 double bwhist[HISTRES]; /* luminance histogram */
37 greg 3.8 double histot; /* total count of histogram */
38 greg 3.1 double bwmin, bwmax; /* histogram limits */
39     double bwavg; /* mean brightness */
40    
41     double scalef = 0.; /* linear scaling factor */
42    
43 schorsch 3.20 static gethfunc headline;
44 schorsch 3.21 static void getahead(void);
45     static void mapimage(void);
46     static void getfovimg(void);
47     static void check2do(void);
48 schorsch 3.20
49 greg 3.1
50 schorsch 3.21
51     int
52     main(
53     int argc,
54     char *argv[]
55     )
56 greg 3.1 {
57     static RGBPRIMS outprimS;
58     int i;
59 schorsch 3.28 #define check_bool(flg) switch (argv[i][2]) { \
60 greg 3.1 case '\0': what2do ^= flg; break; \
61     case 'y': case 'Y': case 't': case 'T': \
62     case '+': case '1': what2do |= flg; break; \
63     case 'n': case 'N': case 'f': case 'F': \
64     case '-': case '0': what2do &= ~(flg); break; \
65     default: goto userr; }
66    
67 greg 3.34 fixargv0(argv[0]); /* sets global progname */
68 greg 3.1
69     for (i = 1; i < argc && argv[i][0] == '-'; i++)
70     switch (argv[i][1]) {
71     case 'h':
72 schorsch 3.28 check_bool(DO_HUMAN);
73 greg 3.1 break;
74     case 'a':
75 schorsch 3.28 check_bool(DO_ACUITY);
76 greg 3.1 break;
77     case 'v':
78 schorsch 3.28 check_bool(DO_VEIL);
79 greg 3.1 break;
80     case 's':
81 schorsch 3.28 check_bool(DO_HSENS);
82 greg 3.1 break;
83     case 'c':
84 schorsch 3.28 check_bool(DO_COLOR);
85 greg 3.1 break;
86     case 'w':
87 schorsch 3.28 check_bool(DO_CWEIGHT);
88 greg 3.1 break;
89 greg 3.9 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 gwlarson 3.12 case 'I':
96 schorsch 3.28 check_bool(DO_PREHIST);
97 gwlarson 3.12 break;
98 greg 3.1 case 'l':
99 schorsch 3.28 check_bool(DO_LINEAR);
100 greg 3.1 break;
101     case 'p':
102     if (i+8 >= argc) goto userr;
103     outprimS[RED][CIEX] = atof(argv[++i]);
104     outprimS[RED][CIEY] = atof(argv[++i]);
105     outprimS[GRN][CIEX] = atof(argv[++i]);
106     outprimS[GRN][CIEY] = atof(argv[++i]);
107     outprimS[BLU][CIEX] = atof(argv[++i]);
108     outprimS[BLU][CIEY] = atof(argv[++i]);
109     outprimS[WHT][CIEX] = atof(argv[++i]);
110     outprimS[WHT][CIEY] = atof(argv[++i]);
111     outprims = outprimS;
112     break;
113     case 'e':
114     if (i+1 >= argc) goto userr;
115     scalef = atof(argv[++i]);
116 schorsch 3.17 if ((argv[i][0] == '+') | (argv[i][0] == '-'))
117 greg 3.1 scalef = pow(2.0, scalef);
118     what2do |= DO_LINEAR;
119     break;
120     case 'f':
121     if (i+1 >= argc) goto userr;
122     mbcalfile = argv[++i];
123     break;
124 greg 3.10 case 'm':
125     if (i+1 >= argc) goto userr;
126     cwarpfile = argv[++i];
127     break;
128 greg 3.11 case 'u':
129 greg 3.1 if (i+1 >= argc) goto userr;
130     ldmax = atof(argv[++i]);
131     if (ldmax <= FTINY)
132     goto userr;
133     break;
134 greg 3.11 case 'd':
135 greg 3.1 if (i+1 >= argc) goto userr;
136 greg 3.11 lddyn = atof(argv[++i]);
137 greg 3.1 break;
138 greg 3.11 case 'x':
139 greg 3.6 if (i+1 >= argc) goto userr;
140     if ((mapfp = fopen(argv[++i], "w")) == NULL) {
141     fprintf(stderr,
142     "%s: cannot open for writing\n",
143     argv[i]);
144     exit(1);
145     }
146     break;
147 greg 3.1 default:
148     goto userr;
149     }
150 gwlarson 3.12 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 greg 3.10 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 greg 3.1 progname);
159     exit(1);
160     }
161 schorsch 3.17 if ((outprims == stdprims) & (inprims != stdprims))
162 greg 3.1 outprims = inprims;
163 greg 3.11 Bldmin = Bl(ldmax/lddyn);
164 greg 3.1 Bldmax = Bl(ldmax);
165     if (i >= argc || i+2 < argc)
166     goto userr;
167 greg 3.9 /* open input file */
168 greg 3.1 if ((infp = fopen(infn=argv[i], "r")) == NULL)
169     syserror(infn);
170 greg 3.9 /* open output file */
171 greg 3.1 if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL)
172     syserror(argv[i+1]);
173 schorsch 3.15 SET_FILE_BINARY(infp);
174     SET_FILE_BINARY(stdout);
175 greg 3.1 getahead(); /* load input header */
176     printargs(argc, argv, stdout); /* add to output header */
177 schorsch 3.17 if ((mbcalfile == NULL) & (outprims != stdprims))
178 greg 3.1 fputprims(outprims, stdout);
179 gwlarson 3.12 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 greg 3.9 getfixations(stdin);
185 greg 3.1 mapimage(); /* map the picture */
186 greg 3.6 if (mapfp != NULL) /* write out basic mapping */
187     putmapping(mapfp);
188 greg 3.1 exit(0);
189     userr:
190 gwlarson 3.12 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 greg 3.1 progname);
192     exit(1);
193 schorsch 3.21 return 1; /* pro forma return */
194 schorsch 3.28 #undef check_bool
195 greg 3.1 }
196    
197    
198 greg 3.26 void
199 schorsch 3.21 syserror( /* report system error and exit */
200     char *s
201     )
202 greg 3.1 {
203     fprintf(stderr, "%s: ", progname);
204     perror(s);
205     exit(2);
206     }
207    
208    
209 schorsch 3.20 static int
210     headline( /* process header line */
211     char *s,
212     void *p
213     )
214 greg 3.1 {
215     static RGBPRIMS inprimS;
216 greg 3.29 char fmt[MAXFMTLEN];
217 greg 3.1
218     if (formatval(fmt, s)) { /* check if format string */
219 greg 3.33 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 gwlarson 3.13 return(0); /* don't echo */
226 greg 3.1 }
227 greg 3.33 if (isncomp(s)) {
228     NCSAMP = ncompval(s);
229     return(0);
230     }
231     if (iswlsplit(s)) {
232     wlsplitval(WLPART, s);
233     return(0);
234     }
235 greg 3.30 /* get input primaries */
236     if (isprims(s) && primsval(inprimS, s)) {
237     inprims = inprimS;
238 gwlarson 3.13 return(0); /* don't echo */
239 greg 3.1 }
240     if (isexpos(s)) { /* picture exposure */
241     inpexp *= exposval(s);
242 gwlarson 3.13 return(0); /* don't echo */
243 greg 3.1 }
244     if (isaspect(s)) /* pixel aspect ratio */
245     pixaspect *= aspectval(s);
246     if (isview(s)) /* image view */
247     gotview += sscanview(&ourview, s);
248 gwlarson 3.13 return(fputs(s, stdout));
249 greg 3.1 }
250    
251    
252 schorsch 3.21 static void
253     getahead(void) /* load picture header */
254 greg 3.1 {
255     char *err;
256    
257     getheader(infp, headline, NULL);
258     if (lumf == NULL || !fgetsresolu(&inpres, infp)) {
259     fprintf(stderr, "%s: %s: not a Radiance picture\n",
260     progname, infn);
261     exit(1);
262     }
263     if (lumf == rgblum)
264 greg 3.14 comprgb2xyzWBmat(inrgb2xyz, inprims);
265 greg 3.1 else if (mbcalfile != NULL) {
266     fprintf(stderr, "%s: macbethcal only works with RGB pictures\n",
267     progname);
268     exit(1);
269     }
270 greg 3.22 if (!gotview || ourview.type == VT_PAR ||
271 schorsch 3.24 (ourview.horiz <= 5.) | (ourview.vert <= 5.)) {
272 schorsch 3.16 ourview = stdview;
273 greg 3.1 ourview.type = VT_PER;
274     if (pixaspect*inpres.yr < inpres.xr) {
275     ourview.horiz = 40.0;
276     ourview.vert = 2.*180./PI *
277     atan(.3639702*pixaspect*inpres.yr/inpres.xr);
278     } else {
279     ourview.vert = 40.0;
280     ourview.horiz = 2.*180./PI *
281     atan(.3639702*inpres.xr/pixaspect/inpres.yr);
282     }
283     }
284     if ((err = setview(&ourview)) != NULL) {
285     fprintf(stderr, "%s: view error in picture \"%s\": %s\n",
286     progname, infn, err);
287     exit(1);
288     }
289     }
290    
291    
292 schorsch 3.21 static void
293     mapimage(void) /* map picture and send to stdout */
294 greg 3.1 {
295     COLOR *scan;
296    
297 greg 3.8 comphist(); /* generate adaptation histogram */
298 greg 3.1 check2do(); /* modify what2do flags */
299 greg 3.9 if (what2do&DO_VEIL)
300 greg 3.14 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
308 greg 3.1 if (what2do&DO_LINEAR) {
309     if (scalef <= FTINY) {
310     if (what2do&DO_HSENS)
311     scalef = htcontrs(Lb(0.5*(Bldmax+Bldmin))) /
312     htcontrs(Lb(bwavg));
313     else
314     scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg);
315     scalef *= WHTEFFICACY/(inpexp*ldmax);
316     }
317 greg 3.4 fputexpos(inpexp*scalef, stdout); /* record exposure */
318 greg 3.1 if (lumf == cielum) scalef /= WHTEFFICACY;
319     }
320 greg 3.9 fputformat(COLRFMT, stdout); /* complete header */
321     putchar('\n');
322 greg 3.1 fputsresolu(&inpres, stdout); /* resolution doesn't change */
323 greg 3.9 /* condition our image */
324 greg 3.1 for (scan = firstscan(); scan != NULL; scan = nextscan())
325     if (fwritescan(scan, scanlen(&inpres), stdout) < 0) {
326     fprintf(stderr, "%s: scanline write error\n",
327     progname);
328     exit(1);
329     }
330     }
331    
332    
333 schorsch 3.21 static void
334     getfovimg(void) /* load foveal sampled image */
335 greg 3.1 {
336 schorsch 3.19 char combuf[PATH_MAX];
337 greg 3.1 FILE *fp;
338     int x, y;
339 greg 3.8 /* compute image size */
340 greg 3.1 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 greg 3.14 if (!(inpres.rt & YMAJOR)) { /* picture is rotated? */
345 greg 3.1 y = fvyr;
346     fvyr = fvxr;
347     fvxr = y;
348     }
349     if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL)
350     syserror("malloc");
351 schorsch 3.19 sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d \"%s\"", fvxr, fvyr, infn);
352 greg 3.1 if ((fp = popen(combuf, "r")) == NULL)
353     syserror("popen");
354 greg 3.25 SET_FILE_BINARY(fp);
355 greg 3.1 getheader(fp, NULL, NULL); /* skip header */
356 schorsch 3.17 if (fgetresolu(&x, &y, fp) < 0 || (x != fvxr) | (y != fvyr))
357 greg 3.1 goto readerr;
358     for (y = 0; y < fvyr; y++)
359 greg 3.33 if (fread2scan(fovscan(y), fvxr, fp, NCSAMP, WLPART) < 0)
360 greg 3.1 goto readerr;
361     pclose(fp);
362 greg 3.8 return;
363     readerr:
364     fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
365     progname);
366     exit(1);
367     }
368    
369    
370 schorsch 3.21 static void
371     check2do(void) /* check histogram to see what isn't worth doing */
372 greg 3.1 {
373 greg 3.8 double sum;
374 greg 3.1 double b, l;
375 greg 3.26 int i;
376 greg 3.1
377     /* check for within display range */
378 greg 3.9 if (bwmax - bwmin <= Bldmax - Bldmin)
379 greg 3.1 what2do |= DO_LINEAR;
380 greg 3.3 /* determine if veiling significant */
381 greg 3.9 if (bwmax - bwmin < 4.5) /* heuristic */
382 greg 3.3 what2do &= ~DO_VEIL;
383 greg 3.1
384 greg 3.3 if (!(what2do & (DO_ACUITY|DO_COLOR)))
385 greg 3.1 return;
386     /* find 5th percentile */
387 greg 3.8 sum = histot*0.05;
388 greg 3.1 for (i = 0; i < HISTRES; i++)
389     if ((sum -= bwhist[i]) <= 0)
390     break;
391     b = (i+.5)*(bwmax-bwmin)/HISTRES + bwmin;
392     l = Lb(b);
393     /* determine if acuity adj. useful */
394     if (what2do&DO_ACUITY &&
395     hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) +
396 greg 3.5 inpres.yr/sqrt(ourview.vn2))/(2.*180./PI))
397 greg 3.1 what2do &= ~DO_ACUITY;
398     /* color sensitivity loss? */
399 greg 3.5 if (l >= TopMesopic)
400 greg 3.1 what2do &= ~DO_COLOR;
401     }