ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.9
Committed: Wed Jan 29 13:22:08 1997 UTC (27 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.8: +25 -88 lines
Log Message:
added -i option to take fixation points on standard input

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