ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.34
Committed: Tue Jun 3 21:31:51 2025 UTC (2 days, 10 hours ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 3.33: +2 -4 lines
Log Message:
refactor: More consistent use of global char * progname and fixargv0()

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pcond.c,v 3.33 2024/09/11 18:56:11 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 "paths.h"
11 #include "pcond.h"
12
13
14 #define LDMAX 100 /* default max. display luminance */
15 #define LDDYN 100 /* default dynamic range */
16
17 int what2do = 0; /* desired adjustments */
18
19 double ldmax = LDMAX; /* maximum output luminance */
20 double lddyn = LDDYN; /* display dynamic range */
21 double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */
22
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 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
51
52 int
53 main(
54 int argc,
55 char *argv[]
56 )
57 {
58 static RGBPRIMS outprimS;
59 int i;
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; \
64 case 'n': case 'N': case 'f': case 'F': \
65 case '-': case '0': what2do &= ~(flg); break; \
66 default: goto userr; }
67
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 check_bool(DO_HUMAN);
74 break;
75 case 'a':
76 check_bool(DO_ACUITY);
77 break;
78 case 'v':
79 check_bool(DO_VEIL);
80 break;
81 case 's':
82 check_bool(DO_HSENS);
83 break;
84 case 'c':
85 check_bool(DO_COLOR);
86 break;
87 case 'w':
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 check_bool(DO_LINEAR);
101 break;
102 case 'p':
103 if (i+8 >= argc) goto userr;
104 outprimS[RED][CIEX] = atof(argv[++i]);
105 outprimS[RED][CIEY] = atof(argv[++i]);
106 outprimS[GRN][CIEX] = atof(argv[++i]);
107 outprimS[GRN][CIEY] = atof(argv[++i]);
108 outprimS[BLU][CIEX] = atof(argv[++i]);
109 outprimS[BLU][CIEY] = atof(argv[++i]);
110 outprimS[WHT][CIEX] = atof(argv[++i]);
111 outprimS[WHT][CIEY] = atof(argv[++i]);
112 outprims = outprimS;
113 break;
114 case 'e':
115 if (i+1 >= argc) goto userr;
116 scalef = atof(argv[++i]);
117 if ((argv[i][0] == '+') | (argv[i][0] == '-'))
118 scalef = pow(2.0, scalef);
119 what2do |= DO_LINEAR;
120 break;
121 case 'f':
122 if (i+1 >= argc) goto userr;
123 mbcalfile = argv[++i];
124 break;
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 'd':
136 if (i+1 >= argc) goto userr;
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 ((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 ((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 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 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 ((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}[+-]][-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 return 1; /* pro forma return */
195 #undef check_bool
196 }
197
198
199 void
200 syserror( /* report system error and exit */
201 char *s
202 )
203 {
204 fprintf(stderr, "%s: ", progname);
205 perror(s);
206 exit(2);
207 }
208
209
210 static int
211 headline( /* process header line */
212 char *s,
213 void *p
214 )
215 {
216 static RGBPRIMS inprimS;
217 char fmt[MAXFMTLEN];
218
219 if (formatval(fmt, s)) { /* check if format string */
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 (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(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 return(fputs(s, stdout));
250 }
251
252
253 static void
254 getahead(void) /* load picture header */
255 {
256 char *err;
257
258 getheader(infp, headline, NULL);
259 if (lumf == NULL || !fgetsresolu(&inpres, infp)) {
260 fprintf(stderr, "%s: %s: not a Radiance picture\n",
261 progname, infn);
262 exit(1);
263 }
264 if (lumf == rgblum)
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 (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;
277 ourview.vert = 2.*180./PI *
278 atan(.3639702*pixaspect*inpres.yr/inpres.xr);
279 } else {
280 ourview.vert = 40.0;
281 ourview.horiz = 2.*180./PI *
282 atan(.3639702*inpres.xr/pixaspect/inpres.yr);
283 }
284 }
285 if ((err = setview(&ourview)) != NULL) {
286 fprintf(stderr, "%s: view error in picture \"%s\": %s\n",
287 progname, infn, err);
288 exit(1);
289 }
290 }
291
292
293 static void
294 mapimage(void) /* map picture and send to stdout */
295 {
296 COLOR *scan;
297
298 comphist(); /* generate adaptation histogram */
299 check2do(); /* modify what2do flags */
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
309 if (what2do&DO_LINEAR) {
310 if (scalef <= FTINY) {
311 if (what2do&DO_HSENS)
312 scalef = htcontrs(Lb(0.5*(Bldmax+Bldmin))) /
313 htcontrs(Lb(bwavg));
314 else
315 scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg);
316 scalef *= WHTEFFICACY/(inpexp*ldmax);
317 }
318 fputexpos(inpexp*scalef, stdout); /* record exposure */
319 if (lumf == cielum) scalef /= WHTEFFICACY;
320 }
321 fputformat(COLRFMT, stdout); /* complete header */
322 putchar('\n');
323 fputsresolu(&inpres, stdout); /* resolution doesn't change */
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",
328 progname);
329 exit(1);
330 }
331 }
332
333
334 static void
335 getfovimg(void) /* load foveal sampled image */
336 {
337 char combuf[PATH_MAX];
338 FILE *fp;
339 int x, y;
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.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 -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))
358 goto readerr;
359 for (y = 0; y < fvyr; y++)
360 if (fread2scan(fovscan(y), fvxr, fp, NCSAMP, WLPART) < 0)
361 goto readerr;
362 pclose(fp);
363 return;
364 readerr:
365 fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
366 progname);
367 exit(1);
368 }
369
370
371 static void
372 check2do(void) /* check histogram to see what isn't worth doing */
373 {
374 double sum;
375 double b, l;
376 int i;
377
378 /* check for within display range */
379 if (bwmax - bwmin <= Bldmax - Bldmin)
380 what2do |= DO_LINEAR;
381 /* determine if veiling significant */
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;
389 for (i = 0; i < HISTRES; i++)
390 if ((sum -= bwhist[i]) <= 0)
391 break;
392 b = (i+.5)*(bwmax-bwmin)/HISTRES + bwmin;
393 l = Lb(b);
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))
398 what2do &= ~DO_ACUITY;
399 /* color sensitivity loss? */
400 if (l >= TopMesopic)
401 what2do &= ~DO_COLOR;
402 }