ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.19
Committed: Mon Nov 10 11:54:23 2003 UTC (20 years, 5 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.18: +4 -3 lines
Log Message:
Fixed size of command line buffer, and allow spaces in input file path.

File Contents

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