ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.14
Committed: Sat Feb 22 02:07:27 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 3.13: +15 -11 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id$";
3 #endif
4 /*
5 * Condition Radiance picture for display/output
6 * Added white-balance adjustment 10/01 (GW).
7 */
8
9 #include "pcond.h"
10
11
12 #define LDMAX 100 /* default max. display luminance */
13 #define LDDYN 32 /* default dynamic range */
14
15 int what2do = 0; /* desired adjustments */
16
17 double ldmax = LDMAX; /* maximum output luminance */
18 double lddyn = LDDYN; /* display dynamic range */
19 double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */
20
21 char *progname; /* global argv[0] */
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
45 main(argc, argv)
46 int argc;
47 char *argv[];
48 {
49 static RGBPRIMS outprimS;
50 int i;
51 #define bool(flg) switch (argv[i][2]) { \
52 case '\0': what2do ^= flg; break; \
53 case 'y': case 'Y': case 't': case 'T': \
54 case '+': case '1': what2do |= flg; break; \
55 case 'n': case 'N': case 'f': case 'F': \
56 case '-': case '0': what2do &= ~(flg); break; \
57 default: goto userr; }
58
59 progname = argv[0];
60
61 for (i = 1; i < argc && argv[i][0] == '-'; i++)
62 switch (argv[i][1]) {
63 case 'h':
64 bool(DO_HUMAN);
65 break;
66 case 'a':
67 bool(DO_ACUITY);
68 break;
69 case 'v':
70 bool(DO_VEIL);
71 break;
72 case 's':
73 bool(DO_HSENS);
74 break;
75 case 'c':
76 bool(DO_COLOR);
77 break;
78 case 'w':
79 bool(DO_CWEIGHT);
80 break;
81 case 'i':
82 if (i+1 >= argc) goto userr;
83 fixfrac = atof(argv[++i]);
84 if (fixfrac > FTINY) what2do |= DO_FIXHIST;
85 else what2do &= ~DO_FIXHIST;
86 break;
87 case 'I':
88 bool(DO_PREHIST);
89 break;
90 case 'l':
91 bool(DO_LINEAR);
92 break;
93 case 'p':
94 if (i+8 >= argc) goto userr;
95 outprimS[RED][CIEX] = atof(argv[++i]);
96 outprimS[RED][CIEY] = atof(argv[++i]);
97 outprimS[GRN][CIEX] = atof(argv[++i]);
98 outprimS[GRN][CIEY] = atof(argv[++i]);
99 outprimS[BLU][CIEX] = atof(argv[++i]);
100 outprimS[BLU][CIEY] = atof(argv[++i]);
101 outprimS[WHT][CIEX] = atof(argv[++i]);
102 outprimS[WHT][CIEY] = atof(argv[++i]);
103 outprims = outprimS;
104 break;
105 case 'e':
106 if (i+1 >= argc) goto userr;
107 scalef = atof(argv[++i]);
108 if (argv[i][0] == '+' | argv[i][0] == '-')
109 scalef = pow(2.0, scalef);
110 what2do |= DO_LINEAR;
111 break;
112 case 'f':
113 if (i+1 >= argc) goto userr;
114 mbcalfile = argv[++i];
115 break;
116 case 'm':
117 if (i+1 >= argc) goto userr;
118 cwarpfile = argv[++i];
119 break;
120 case 'u':
121 if (i+1 >= argc) goto userr;
122 ldmax = atof(argv[++i]);
123 if (ldmax <= FTINY)
124 goto userr;
125 break;
126 case 'd':
127 if (i+1 >= argc) goto userr;
128 lddyn = atof(argv[++i]);
129 break;
130 case 'x':
131 if (i+1 >= argc) goto userr;
132 if ((mapfp = fopen(argv[++i], "w")) == NULL) {
133 fprintf(stderr,
134 "%s: cannot open for writing\n",
135 argv[i]);
136 exit(1);
137 }
138 break;
139 default:
140 goto userr;
141 }
142 if ((what2do & (DO_FIXHIST|DO_PREHIST)) == (DO_FIXHIST|DO_PREHIST)) {
143 fprintf(stderr, "%s: only one of -i or -I option\n", progname);
144 exit(1);
145 }
146 if ((mbcalfile != NULL) + (cwarpfile != NULL) +
147 (outprims != stdprims) > 1) {
148 fprintf(stderr,
149 "%s: only one of -p, -m or -f option supported\n",
150 progname);
151 exit(1);
152 }
153 if (outprims == stdprims & inprims != stdprims)
154 outprims = inprims;
155 Bldmin = Bl(ldmax/lddyn);
156 Bldmax = Bl(ldmax);
157 if (i >= argc || i+2 < argc)
158 goto userr;
159 /* open input file */
160 if ((infp = fopen(infn=argv[i], "r")) == NULL)
161 syserror(infn);
162 /* open output file */
163 if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL)
164 syserror(argv[i+1]);
165 #ifdef MSDOS
166 setmode(fileno(infp), O_BINARY);
167 setmode(fileno(stdout), O_BINARY);
168 #endif
169 getahead(); /* load input header */
170 printargs(argc, argv, stdout); /* add to output header */
171 if (mbcalfile == NULL & outprims != stdprims)
172 fputprims(outprims, stdout);
173 if ((what2do & (DO_PREHIST|DO_VEIL|DO_ACUITY)) != DO_PREHIST)
174 getfovimg(); /* get foveal sample image? */
175 if (what2do&DO_PREHIST) /* get histogram? */
176 gethisto(stdin);
177 else if (what2do&DO_FIXHIST) /* get fixation history? */
178 getfixations(stdin);
179 mapimage(); /* map the picture */
180 if (mapfp != NULL) /* write out basic mapping */
181 putmapping(mapfp);
182 exit(0);
183 userr:
184 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",
185 progname);
186 exit(1);
187 #undef bool
188 }
189
190
191 syserror(s) /* report system error and exit */
192 char *s;
193 {
194 fprintf(stderr, "%s: ", progname);
195 perror(s);
196 exit(2);
197 }
198
199
200 headline(s) /* process header line */
201 char *s;
202 {
203 static RGBPRIMS inprimS;
204 char fmt[32];
205
206 if (formatval(fmt, s)) { /* check if format string */
207 if (!strcmp(fmt,COLRFMT)) lumf = rgblum;
208 else if (!strcmp(fmt,CIEFMT)) lumf = cielum;
209 else lumf = NULL;
210 return(0); /* don't echo */
211 }
212 if (isprims(s)) { /* get input primaries */
213 primsval(inprimS, s);
214 inprims= inprimS;
215 return(0); /* don't echo */
216 }
217 if (isexpos(s)) { /* picture exposure */
218 inpexp *= exposval(s);
219 return(0); /* don't echo */
220 }
221 if (isaspect(s)) /* pixel aspect ratio */
222 pixaspect *= aspectval(s);
223 if (isview(s)) /* image view */
224 gotview += sscanview(&ourview, s);
225 return(fputs(s, stdout));
226 }
227
228
229 getahead() /* load picture header */
230 {
231 char *err;
232
233 getheader(infp, headline, NULL);
234 if (lumf == NULL || !fgetsresolu(&inpres, infp)) {
235 fprintf(stderr, "%s: %s: not a Radiance picture\n",
236 progname, infn);
237 exit(1);
238 }
239 if (lumf == rgblum)
240 comprgb2xyzWBmat(inrgb2xyz, inprims);
241 else if (mbcalfile != NULL) {
242 fprintf(stderr, "%s: macbethcal only works with RGB pictures\n",
243 progname);
244 exit(1);
245 }
246 if (!gotview || ourview.type == VT_PAR) {
247 copystruct(&ourview, &stdview);
248 ourview.type = VT_PER;
249 if (pixaspect*inpres.yr < inpres.xr) {
250 ourview.horiz = 40.0;
251 ourview.vert = 2.*180./PI *
252 atan(.3639702*pixaspect*inpres.yr/inpres.xr);
253 } else {
254 ourview.vert = 40.0;
255 ourview.horiz = 2.*180./PI *
256 atan(.3639702*inpres.xr/pixaspect/inpres.yr);
257 }
258 }
259 if ((err = setview(&ourview)) != NULL) {
260 fprintf(stderr, "%s: view error in picture \"%s\": %s\n",
261 progname, infn, err);
262 exit(1);
263 }
264 }
265
266
267 mapimage() /* map picture and send to stdout */
268 {
269 COLOR *scan;
270
271 comphist(); /* generate adaptation histogram */
272 check2do(); /* modify what2do flags */
273 if (what2do&DO_VEIL)
274 compveil(); /* compute veil image */
275 if (!(what2do&DO_LINEAR))
276 if (mkbrmap() < 0) /* make tone map */
277 what2do |= DO_LINEAR; /* failed! -- use linear */
278 #if ADJ_VEIL
279 else if (what2do&DO_VEIL)
280 adjveil(); /* else adjust veil image */
281 #endif
282 if (what2do&DO_LINEAR) {
283 if (scalef <= FTINY) {
284 if (what2do&DO_HSENS)
285 scalef = htcontrs(Lb(0.5*(Bldmax+Bldmin))) /
286 htcontrs(Lb(bwavg));
287 else
288 scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg);
289 scalef *= WHTEFFICACY/(inpexp*ldmax);
290 }
291 fputexpos(inpexp*scalef, stdout); /* record exposure */
292 if (lumf == cielum) scalef /= WHTEFFICACY;
293 }
294 fputformat(COLRFMT, stdout); /* complete header */
295 putchar('\n');
296 fputsresolu(&inpres, stdout); /* resolution doesn't change */
297 /* condition our image */
298 for (scan = firstscan(); scan != NULL; scan = nextscan())
299 if (fwritescan(scan, scanlen(&inpres), stdout) < 0) {
300 fprintf(stderr, "%s: scanline write error\n",
301 progname);
302 exit(1);
303 }
304 }
305
306
307 getfovimg() /* load foveal sampled image */
308 {
309 extern FILE *popen();
310 char combuf[128];
311 FILE *fp;
312 int x, y;
313 /* compute image size */
314 fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5;
315 if (fvxr < 2) fvxr = 2;
316 fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5;
317 if (fvyr < 2) fvyr = 2;
318 if (!(inpres.rt & YMAJOR)) { /* picture is rotated? */
319 y = fvyr;
320 fvyr = fvxr;
321 fvxr = y;
322 }
323 if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL)
324 syserror("malloc");
325 sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d %s", fvxr, fvyr, infn);
326 if ((fp = popen(combuf, "r")) == NULL)
327 syserror("popen");
328 getheader(fp, NULL, NULL); /* skip header */
329 if (fgetresolu(&x, &y, fp) < 0 || x != fvxr | y != fvyr)
330 goto readerr;
331 for (y = 0; y < fvyr; y++)
332 if (freadscan(fovscan(y), fvxr, fp) < 0)
333 goto readerr;
334 pclose(fp);
335 return;
336 readerr:
337 fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
338 progname);
339 exit(1);
340 }
341
342
343 check2do() /* check histogram to see what isn't worth doing */
344 {
345 double sum;
346 double b, l;
347 register int i;
348
349 /* check for within display range */
350 if (bwmax - bwmin <= Bldmax - Bldmin)
351 what2do |= DO_LINEAR;
352 /* determine if veiling significant */
353 if (bwmax - bwmin < 4.5) /* heuristic */
354 what2do &= ~DO_VEIL;
355
356 if (!(what2do & (DO_ACUITY|DO_COLOR)))
357 return;
358 /* find 5th percentile */
359 sum = histot*0.05;
360 for (i = 0; i < HISTRES; i++)
361 if ((sum -= bwhist[i]) <= 0)
362 break;
363 b = (i+.5)*(bwmax-bwmin)/HISTRES + bwmin;
364 l = Lb(b);
365 /* determine if acuity adj. useful */
366 if (what2do&DO_ACUITY &&
367 hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) +
368 inpres.yr/sqrt(ourview.vn2))/(2.*180./PI))
369 what2do &= ~DO_ACUITY;
370 /* color sensitivity loss? */
371 if (l >= TopMesopic)
372 what2do &= ~DO_COLOR;
373 }