ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.24
Committed: Wed Jun 7 17:52:04 2006 UTC (17 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
CVS Tags: rad4R2, rad4R1, rad4R0, rad3R8, rad3R9, rad4R2P1
Changes since 3.23: +2 -2 lines
Log Message:
Eliminated some compiler warnings.

File Contents

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