ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pcond.c
Revision: 3.33
Committed: Wed Sep 11 18:56:11 2024 UTC (7 months, 3 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 3.32: +16 -5 lines
Log Message:
feat(pcond,pvalue): Added hyperspectral input handling

File Contents

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