ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.12
Committed: Thu Mar 12 15:47:33 1998 UTC (26 years, 1 month ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.11: +13 -3 lines
Log Message:
added -I option for precomputed histogram

File Contents

# User Rev Content
1 greg 3.7 /* Copyright (c) 1997 Regents of the University of California */
2 greg 3.1
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 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     short fvxr, fvyr; /* foveal image resolution */
36 greg 3.9 short (*fixlst)[2]; /* fixation history list */
37     int nfixations; /* number of fixation points */
38 greg 3.8 float bwhist[HISTRES]; /* luminance histogram */
39     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     #ifdef MSDOS
167     setmode(fileno(infp), O_BINARY);
168     setmode(fileno(stdout), O_BINARY);
169     #endif
170     getahead(); /* load input header */
171     printargs(argc, argv, stdout); /* add to output header */
172 greg 3.9 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     return; /* don't echo */
212     }
213     if (isprims(s)) { /* get input primaries */
214     primsval(inprimS, s);
215     inprims= inprimS;
216     return; /* don't echo */
217     }
218     if (isexpos(s)) { /* picture exposure */
219     inpexp *= exposval(s);
220     return; /* don't echo */
221     }
222     if (isaspect(s)) /* pixel aspect ratio */
223     pixaspect *= aspectval(s);
224     if (isview(s)) /* image view */
225     gotview += sscanview(&ourview, s);
226     fputs(s, stdout);
227     }
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     comprgb2xyzmat(inrgb2xyz, inprims);
242     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     copystruct(&ourview, &stdview);
249     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.1 compveil();
276 greg 3.9 if (!(what2do&DO_LINEAR) && mkbrmap() < 0) /* make tone map */
277     what2do |= DO_LINEAR; /* failed! -- use linear scaling */
278 greg 3.1 if (what2do&DO_LINEAR) {
279     if (scalef <= FTINY) {
280     if (what2do&DO_HSENS)
281     scalef = htcontrs(Lb(0.5*(Bldmax+Bldmin))) /
282     htcontrs(Lb(bwavg));
283     else
284     scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg);
285     scalef *= WHTEFFICACY/(inpexp*ldmax);
286     }
287 greg 3.4 fputexpos(inpexp*scalef, stdout); /* record exposure */
288 greg 3.1 if (lumf == cielum) scalef /= WHTEFFICACY;
289     }
290 greg 3.9 fputformat(COLRFMT, stdout); /* complete header */
291     putchar('\n');
292 greg 3.1 fputsresolu(&inpres, stdout); /* resolution doesn't change */
293 greg 3.9 /* condition our image */
294 greg 3.1 for (scan = firstscan(); scan != NULL; scan = nextscan())
295     if (fwritescan(scan, scanlen(&inpres), stdout) < 0) {
296     fprintf(stderr, "%s: scanline write error\n",
297     progname);
298     exit(1);
299     }
300     }
301    
302    
303 greg 3.8 getfovimg() /* load foveal sampled image */
304 greg 3.1 {
305     extern FILE *popen();
306     char combuf[128];
307     FILE *fp;
308     int x, y;
309 greg 3.8 /* compute image size */
310 greg 3.1 fvxr = sqrt(ourview.hn2)/FOVDIA + 0.5;
311     if (fvxr < 2) fvxr = 2;
312     fvyr = sqrt(ourview.vn2)/FOVDIA + 0.5;
313     if (fvyr < 2) fvyr = 2;
314     if (!(inpres.or & YMAJOR)) { /* picture is rotated? */
315     y = fvyr;
316     fvyr = fvxr;
317     fvxr = y;
318     }
319     if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL)
320     syserror("malloc");
321 greg 3.9 sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d %s", fvxr, fvyr, infn);
322 greg 3.1 if ((fp = popen(combuf, "r")) == NULL)
323     syserror("popen");
324     getheader(fp, NULL, NULL); /* skip header */
325     if (fgetresolu(&x, &y, fp) < 0 || x != fvxr | y != fvyr)
326     goto readerr;
327     for (y = 0; y < fvyr; y++)
328     if (freadscan(fovscan(y), fvxr, fp) < 0)
329     goto readerr;
330     pclose(fp);
331 greg 3.8 return;
332     readerr:
333     fprintf(stderr, "%s: error reading from pfilt process in fovimage\n",
334     progname);
335     exit(1);
336     }
337    
338    
339 greg 3.1 check2do() /* check histogram to see what isn't worth doing */
340     {
341 greg 3.8 double sum;
342 greg 3.1 double b, l;
343     register int i;
344    
345     /* check for within display range */
346 greg 3.9 if (bwmax - bwmin <= Bldmax - Bldmin)
347 greg 3.1 what2do |= DO_LINEAR;
348 greg 3.3 /* determine if veiling significant */
349 greg 3.9 if (bwmax - bwmin < 4.5) /* heuristic */
350 greg 3.3 what2do &= ~DO_VEIL;
351 greg 3.1
352 greg 3.3 if (!(what2do & (DO_ACUITY|DO_COLOR)))
353 greg 3.1 return;
354     /* find 5th percentile */
355 greg 3.8 sum = histot*0.05;
356 greg 3.1 for (i = 0; i < HISTRES; i++)
357     if ((sum -= bwhist[i]) <= 0)
358     break;
359     b = (i+.5)*(bwmax-bwmin)/HISTRES + bwmin;
360     l = Lb(b);
361     /* determine if acuity adj. useful */
362     if (what2do&DO_ACUITY &&
363     hacuity(l) >= (inpres.xr/sqrt(ourview.hn2) +
364 greg 3.5 inpres.yr/sqrt(ourview.vn2))/(2.*180./PI))
365 greg 3.1 what2do &= ~DO_ACUITY;
366     /* color sensitivity loss? */
367 greg 3.5 if (l >= TopMesopic)
368 greg 3.1 what2do &= ~DO_COLOR;
369     }