ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.11
Committed: Wed Mar 19 13:04:09 1997 UTC (27 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.10: +9 -16 lines
Log Message:
changed -t option to -u, -d option to -x and -b option to -d

File Contents

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