ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.15
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.14: +4 -5 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

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