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 (77 minutes, 12 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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pcond.c,v 3.34 2025/06/03 21:31:51 greg Exp $";
3 #endif
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
12
13 #define LDMAX 100 /* default max. display luminance */
14 #define LDDYN 100 /* default dynamic range */
15
16 int what2do = 0; /* desired adjustments */
17
18 double ldmax = LDMAX; /* maximum output luminance */
19 double lddyn = LDDYN; /* display dynamic range */
20 double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */
21
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 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
50
51 int
52 main(
53 int argc,
54 char *argv[]
55 )
56 {
57 static RGBPRIMS outprimS;
58 int i;
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; \
63 case 'n': case 'N': case 'f': case 'F': \
64 case '-': case '0': what2do &= ~(flg); break; \
65 default: goto userr; }
66
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 check_bool(DO_HUMAN);
73 break;
74 case 'a':
75 check_bool(DO_ACUITY);
76 break;
77 case 'v':
78 check_bool(DO_VEIL);
79 break;
80 case 's':
81 check_bool(DO_HSENS);
82 break;
83 case 'c':
84 check_bool(DO_COLOR);
85 break;
86 case 'w':
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 check_bool(DO_LINEAR);
100 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 if ((argv[i][0] == '+') | (argv[i][0] == '-'))
117 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 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 'd':
135 if (i+1 >= argc) goto userr;
136 lddyn = atof(argv[++i]);
137 break;
138 case 'x':
139 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 default:
148 goto userr;
149 }
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 ((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 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 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 ((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}[+-]][-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 return 1; /* pro forma return */
194 #undef check_bool
195 }
196
197
198 void
199 syserror( /* report system error and exit */
200 char *s
201 )
202 {
203 fprintf(stderr, "%s: ", progname);
204 perror(s);
205 exit(2);
206 }
207
208
209 static int
210 headline( /* process header line */
211 char *s,
212 void *p
213 )
214 {
215 static RGBPRIMS inprimS;
216 char fmt[MAXFMTLEN];
217
218 if (formatval(fmt, s)) { /* check if format string */
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 (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(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 return(fputs(s, stdout));
249 }
250
251
252 static void
253 getahead(void) /* load picture header */
254 {
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 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 (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;
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 static void
293 mapimage(void) /* map picture and send to stdout */
294 {
295 COLOR *scan;
296
297 comphist(); /* generate adaptation histogram */
298 check2do(); /* modify what2do flags */
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
308 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 fputexpos(inpexp*scalef, stdout); /* record exposure */
318 if (lumf == cielum) scalef /= WHTEFFICACY;
319 }
320 fputformat(COLRFMT, stdout); /* complete header */
321 putchar('\n');
322 fputsresolu(&inpres, stdout); /* resolution doesn't change */
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",
327 progname);
328 exit(1);
329 }
330 }
331
332
333 static void
334 getfovimg(void) /* load foveal sampled image */
335 {
336 char combuf[PATH_MAX];
337 FILE *fp;
338 int x, y;
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.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 -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))
357 goto readerr;
358 for (y = 0; y < fvyr; y++)
359 if (fread2scan(fovscan(y), fvxr, fp, NCSAMP, WLPART) < 0)
360 goto readerr;
361 pclose(fp);
362 return;
363 readerr:
364 fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
365 progname);
366 exit(1);
367 }
368
369
370 static void
371 check2do(void) /* check histogram to see what isn't worth doing */
372 {
373 double sum;
374 double b, l;
375 int i;
376
377 /* check for within display range */
378 if (bwmax - bwmin <= Bldmax - Bldmin)
379 what2do |= DO_LINEAR;
380 /* determine if veiling significant */
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;
388 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 inpres.yr/sqrt(ourview.vn2))/(2.*180./PI))
397 what2do &= ~DO_ACUITY;
398 /* color sensitivity loss? */
399 if (l >= TopMesopic)
400 what2do &= ~DO_COLOR;
401 }