ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.18
Committed: Mon Oct 27 10:24:51 2003 UTC (20 years, 6 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 3.17: +2 -2 lines
Log Message:
Various compatibility fixes.

File Contents

# User Rev Content
1 greg 3.1 #ifndef lint
2 schorsch 3.18 static const char RCSid[] = "$Id: pcond.c,v 3.17 2003/07/27 22:12:03 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.18 #include "rtprocess.h"
11 greg 3.1 #include "pcond.h"
12    
13    
14     #define LDMAX 100 /* default max. display luminance */
15 greg 3.11 #define LDDYN 32 /* default dynamic range */
16 greg 3.1
17     int what2do = 0; /* desired adjustments */
18    
19     double ldmax = LDMAX; /* maximum output luminance */
20 greg 3.11 double lddyn = LDDYN; /* display dynamic range */
21     double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */
22 greg 3.1
23     char *progname; /* global argv[0] */
24    
25     char *infn; /* input file name */
26     FILE *infp; /* input stream */
27 greg 3.6 FILE *mapfp = NULL; /* tone-mapping function stream */
28 greg 3.1 VIEW ourview = STDVIEW; /* picture view */
29     int gotview = 0; /* picture has view */
30     double pixaspect = 1.0; /* pixel aspect ratio */
31 greg 3.9 double fixfrac = 0.; /* histogram share due to fixations */
32 greg 3.1 RESOLU inpres; /* input picture resolution */
33    
34     COLOR *fovimg; /* foveal (1 degree) averaged image */
35 greg 3.14 int fvxr, fvyr; /* foveal image resolution */
36     float *crfimg; /* contrast reduction factors */
37 greg 3.9 short (*fixlst)[2]; /* fixation history list */
38     int nfixations; /* number of fixation points */
39 greg 3.14 double bwhist[HISTRES]; /* luminance histogram */
40 greg 3.8 double histot; /* total count of histogram */
41 greg 3.1 double bwmin, bwmax; /* histogram limits */
42     double bwavg; /* mean brightness */
43    
44     double scalef = 0.; /* linear scaling factor */
45    
46    
47     main(argc, argv)
48     int argc;
49     char *argv[];
50     {
51     static RGBPRIMS outprimS;
52     int i;
53     #define bool(flg) switch (argv[i][2]) { \
54     case '\0': what2do ^= flg; break; \
55     case 'y': case 'Y': case 't': case 'T': \
56     case '+': case '1': what2do |= flg; break; \
57     case 'n': case 'N': case 'f': case 'F': \
58     case '-': case '0': what2do &= ~(flg); break; \
59     default: goto userr; }
60    
61     progname = argv[0];
62    
63     for (i = 1; i < argc && argv[i][0] == '-'; i++)
64     switch (argv[i][1]) {
65     case 'h':
66     bool(DO_HUMAN);
67     break;
68     case 'a':
69     bool(DO_ACUITY);
70     break;
71     case 'v':
72     bool(DO_VEIL);
73     break;
74     case 's':
75     bool(DO_HSENS);
76     break;
77     case 'c':
78     bool(DO_COLOR);
79     break;
80     case 'w':
81     bool(DO_CWEIGHT);
82     break;
83 greg 3.9 case 'i':
84     if (i+1 >= argc) goto userr;
85     fixfrac = atof(argv[++i]);
86     if (fixfrac > FTINY) what2do |= DO_FIXHIST;
87     else what2do &= ~DO_FIXHIST;
88     break;
89 gwlarson 3.12 case 'I':
90     bool(DO_PREHIST);
91     break;
92 greg 3.1 case 'l':
93     bool(DO_LINEAR);
94     break;
95     case 'p':
96     if (i+8 >= argc) goto userr;
97     outprimS[RED][CIEX] = atof(argv[++i]);
98     outprimS[RED][CIEY] = atof(argv[++i]);
99     outprimS[GRN][CIEX] = atof(argv[++i]);
100     outprimS[GRN][CIEY] = atof(argv[++i]);
101     outprimS[BLU][CIEX] = atof(argv[++i]);
102     outprimS[BLU][CIEY] = atof(argv[++i]);
103     outprimS[WHT][CIEX] = atof(argv[++i]);
104     outprimS[WHT][CIEY] = atof(argv[++i]);
105     outprims = outprimS;
106     break;
107     case 'e':
108     if (i+1 >= argc) goto userr;
109     scalef = atof(argv[++i]);
110 schorsch 3.17 if ((argv[i][0] == '+') | (argv[i][0] == '-'))
111 greg 3.1 scalef = pow(2.0, scalef);
112     what2do |= DO_LINEAR;
113     break;
114     case 'f':
115     if (i+1 >= argc) goto userr;
116     mbcalfile = argv[++i];
117     break;
118 greg 3.10 case 'm':
119     if (i+1 >= argc) goto userr;
120     cwarpfile = argv[++i];
121     break;
122 greg 3.11 case 'u':
123 greg 3.1 if (i+1 >= argc) goto userr;
124     ldmax = atof(argv[++i]);
125     if (ldmax <= FTINY)
126     goto userr;
127     break;
128 greg 3.11 case 'd':
129 greg 3.1 if (i+1 >= argc) goto userr;
130 greg 3.11 lddyn = atof(argv[++i]);
131 greg 3.1 break;
132 greg 3.11 case 'x':
133 greg 3.6 if (i+1 >= argc) goto userr;
134     if ((mapfp = fopen(argv[++i], "w")) == NULL) {
135     fprintf(stderr,
136     "%s: cannot open for writing\n",
137     argv[i]);
138     exit(1);
139     }
140     break;
141 greg 3.1 default:
142     goto userr;
143     }
144 gwlarson 3.12 if ((what2do & (DO_FIXHIST|DO_PREHIST)) == (DO_FIXHIST|DO_PREHIST)) {
145     fprintf(stderr, "%s: only one of -i or -I option\n", progname);
146     exit(1);
147     }
148 greg 3.10 if ((mbcalfile != NULL) + (cwarpfile != NULL) +
149     (outprims != stdprims) > 1) {
150     fprintf(stderr,
151     "%s: only one of -p, -m or -f option supported\n",
152 greg 3.1 progname);
153     exit(1);
154     }
155 schorsch 3.17 if ((outprims == stdprims) & (inprims != stdprims))
156 greg 3.1 outprims = inprims;
157 greg 3.11 Bldmin = Bl(ldmax/lddyn);
158 greg 3.1 Bldmax = Bl(ldmax);
159     if (i >= argc || i+2 < argc)
160     goto userr;
161 greg 3.9 /* open input file */
162 greg 3.1 if ((infp = fopen(infn=argv[i], "r")) == NULL)
163     syserror(infn);
164 greg 3.9 /* open output file */
165 greg 3.1 if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL)
166     syserror(argv[i+1]);
167 schorsch 3.15 SET_FILE_BINARY(infp);
168     SET_FILE_BINARY(stdout);
169 greg 3.1 getahead(); /* load input header */
170     printargs(argc, argv, stdout); /* add to output header */
171 schorsch 3.17 if ((mbcalfile == NULL) & (outprims != stdprims))
172 greg 3.1 fputprims(outprims, stdout);
173 gwlarson 3.12 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 greg 3.9 getfixations(stdin);
179 greg 3.1 mapimage(); /* map the picture */
180 greg 3.6 if (mapfp != NULL) /* write out basic mapping */
181     putmapping(mapfp);
182 greg 3.1 exit(0);
183     userr:
184 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",
185 greg 3.1 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 gwlarson 3.13 return(0); /* don't echo */
211 greg 3.1 }
212     if (isprims(s)) { /* get input primaries */
213     primsval(inprimS, s);
214     inprims= inprimS;
215 gwlarson 3.13 return(0); /* don't echo */
216 greg 3.1 }
217     if (isexpos(s)) { /* picture exposure */
218     inpexp *= exposval(s);
219 gwlarson 3.13 return(0); /* don't echo */
220 greg 3.1 }
221     if (isaspect(s)) /* pixel aspect ratio */
222     pixaspect *= aspectval(s);
223     if (isview(s)) /* image view */
224     gotview += sscanview(&ourview, s);
225 gwlarson 3.13 return(fputs(s, stdout));
226 greg 3.1 }
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 greg 3.14 comprgb2xyzWBmat(inrgb2xyz, inprims);
241 greg 3.1 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 schorsch 3.16 ourview = stdview;
248 greg 3.1 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 greg 3.8 comphist(); /* generate adaptation histogram */
272 greg 3.1 check2do(); /* modify what2do flags */
273 greg 3.9 if (what2do&DO_VEIL)
274 greg 3.14 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 greg 3.1 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 greg 3.4 fputexpos(inpexp*scalef, stdout); /* record exposure */
292 greg 3.1 if (lumf == cielum) scalef /= WHTEFFICACY;
293     }
294 greg 3.9 fputformat(COLRFMT, stdout); /* complete header */
295     putchar('\n');
296 greg 3.1 fputsresolu(&inpres, stdout); /* resolution doesn't change */
297 greg 3.9 /* condition our image */
298 greg 3.1 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 greg 3.8 getfovimg() /* load foveal sampled image */
308 greg 3.1 {
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 schorsch 3.17 if (fgetresolu(&x, &y, fp) < 0 || (x != fvxr) | (y != fvyr))
329 greg 3.1 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     }