1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: pcond.c,v 3.15 2003/06/05 19:29:34 schorsch Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Condition Radiance picture for display/output |
6 |
* Added white-balance adjustment 10/01 (GW). |
7 |
*/ |
8 |
|
9 |
#include "platform.h" |
10 |
#include "pcond.h" |
11 |
|
12 |
|
13 |
#define LDMAX 100 /* default max. display luminance */ |
14 |
#define LDDYN 32 /* default dynamic range */ |
15 |
|
16 |
int what2do = 0; /* desired adjustments */ |
17 |
|
18 |
double ldmax = LDMAX; /* maximum output luminance */ |
19 |
double lddyn = LDDYN; /* display dynamic range */ |
20 |
double Bldmin, Bldmax; /* Bl(ldmax/lddyn) and Bl(ldmax) */ |
21 |
|
22 |
char *progname; /* global argv[0] */ |
23 |
|
24 |
char *infn; /* input file name */ |
25 |
FILE *infp; /* input stream */ |
26 |
FILE *mapfp = NULL; /* tone-mapping function stream */ |
27 |
VIEW ourview = STDVIEW; /* picture view */ |
28 |
int gotview = 0; /* picture has view */ |
29 |
double pixaspect = 1.0; /* pixel aspect ratio */ |
30 |
double fixfrac = 0.; /* histogram share due to fixations */ |
31 |
RESOLU inpres; /* input picture resolution */ |
32 |
|
33 |
COLOR *fovimg; /* foveal (1 degree) averaged image */ |
34 |
int fvxr, fvyr; /* foveal image resolution */ |
35 |
float *crfimg; /* contrast reduction factors */ |
36 |
short (*fixlst)[2]; /* fixation history list */ |
37 |
int nfixations; /* number of fixation points */ |
38 |
double bwhist[HISTRES]; /* luminance histogram */ |
39 |
double histot; /* total count of histogram */ |
40 |
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 |
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 |
case 'I': |
89 |
bool(DO_PREHIST); |
90 |
break; |
91 |
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 |
case 'm': |
118 |
if (i+1 >= argc) goto userr; |
119 |
cwarpfile = argv[++i]; |
120 |
break; |
121 |
case 'u': |
122 |
if (i+1 >= argc) goto userr; |
123 |
ldmax = atof(argv[++i]); |
124 |
if (ldmax <= FTINY) |
125 |
goto userr; |
126 |
break; |
127 |
case 'd': |
128 |
if (i+1 >= argc) goto userr; |
129 |
lddyn = atof(argv[++i]); |
130 |
break; |
131 |
case 'x': |
132 |
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 |
default: |
141 |
goto userr; |
142 |
} |
143 |
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 |
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 |
progname); |
152 |
exit(1); |
153 |
} |
154 |
if (outprims == stdprims & inprims != stdprims) |
155 |
outprims = inprims; |
156 |
Bldmin = Bl(ldmax/lddyn); |
157 |
Bldmax = Bl(ldmax); |
158 |
if (i >= argc || i+2 < argc) |
159 |
goto userr; |
160 |
/* open input file */ |
161 |
if ((infp = fopen(infn=argv[i], "r")) == NULL) |
162 |
syserror(infn); |
163 |
/* open output file */ |
164 |
if (i+2 == argc && freopen(argv[i+1], "w", stdout) == NULL) |
165 |
syserror(argv[i+1]); |
166 |
SET_FILE_BINARY(infp); |
167 |
SET_FILE_BINARY(stdout); |
168 |
getahead(); /* load input header */ |
169 |
printargs(argc, argv, stdout); /* add to output header */ |
170 |
if (mbcalfile == NULL & outprims != stdprims) |
171 |
fputprims(outprims, stdout); |
172 |
if ((what2do & (DO_PREHIST|DO_VEIL|DO_ACUITY)) != DO_PREHIST) |
173 |
getfovimg(); /* get foveal sample image? */ |
174 |
if (what2do&DO_PREHIST) /* get histogram? */ |
175 |
gethisto(stdin); |
176 |
else if (what2do&DO_FIXHIST) /* get fixation history? */ |
177 |
getfixations(stdin); |
178 |
mapimage(); /* map the picture */ |
179 |
if (mapfp != NULL) /* write out basic mapping */ |
180 |
putmapping(mapfp); |
181 |
exit(0); |
182 |
userr: |
183 |
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", |
184 |
progname); |
185 |
exit(1); |
186 |
#undef bool |
187 |
} |
188 |
|
189 |
|
190 |
syserror(s) /* report system error and exit */ |
191 |
char *s; |
192 |
{ |
193 |
fprintf(stderr, "%s: ", progname); |
194 |
perror(s); |
195 |
exit(2); |
196 |
} |
197 |
|
198 |
|
199 |
headline(s) /* process header line */ |
200 |
char *s; |
201 |
{ |
202 |
static RGBPRIMS inprimS; |
203 |
char fmt[32]; |
204 |
|
205 |
if (formatval(fmt, s)) { /* check if format string */ |
206 |
if (!strcmp(fmt,COLRFMT)) lumf = rgblum; |
207 |
else if (!strcmp(fmt,CIEFMT)) lumf = cielum; |
208 |
else lumf = NULL; |
209 |
return(0); /* don't echo */ |
210 |
} |
211 |
if (isprims(s)) { /* get input primaries */ |
212 |
primsval(inprimS, s); |
213 |
inprims= inprimS; |
214 |
return(0); /* don't echo */ |
215 |
} |
216 |
if (isexpos(s)) { /* picture exposure */ |
217 |
inpexp *= exposval(s); |
218 |
return(0); /* don't echo */ |
219 |
} |
220 |
if (isaspect(s)) /* pixel aspect ratio */ |
221 |
pixaspect *= aspectval(s); |
222 |
if (isview(s)) /* image view */ |
223 |
gotview += sscanview(&ourview, s); |
224 |
return(fputs(s, stdout)); |
225 |
} |
226 |
|
227 |
|
228 |
getahead() /* load picture header */ |
229 |
{ |
230 |
char *err; |
231 |
|
232 |
getheader(infp, headline, NULL); |
233 |
if (lumf == NULL || !fgetsresolu(&inpres, infp)) { |
234 |
fprintf(stderr, "%s: %s: not a Radiance picture\n", |
235 |
progname, infn); |
236 |
exit(1); |
237 |
} |
238 |
if (lumf == rgblum) |
239 |
comprgb2xyzWBmat(inrgb2xyz, inprims); |
240 |
else if (mbcalfile != NULL) { |
241 |
fprintf(stderr, "%s: macbethcal only works with RGB pictures\n", |
242 |
progname); |
243 |
exit(1); |
244 |
} |
245 |
if (!gotview || ourview.type == VT_PAR) { |
246 |
ourview = stdview; |
247 |
ourview.type = VT_PER; |
248 |
if (pixaspect*inpres.yr < inpres.xr) { |
249 |
ourview.horiz = 40.0; |
250 |
ourview.vert = 2.*180./PI * |
251 |
atan(.3639702*pixaspect*inpres.yr/inpres.xr); |
252 |
} else { |
253 |
ourview.vert = 40.0; |
254 |
ourview.horiz = 2.*180./PI * |
255 |
atan(.3639702*inpres.xr/pixaspect/inpres.yr); |
256 |
} |
257 |
} |
258 |
if ((err = setview(&ourview)) != NULL) { |
259 |
fprintf(stderr, "%s: view error in picture \"%s\": %s\n", |
260 |
progname, infn, err); |
261 |
exit(1); |
262 |
} |
263 |
} |
264 |
|
265 |
|
266 |
mapimage() /* map picture and send to stdout */ |
267 |
{ |
268 |
COLOR *scan; |
269 |
|
270 |
comphist(); /* generate adaptation histogram */ |
271 |
check2do(); /* modify what2do flags */ |
272 |
if (what2do&DO_VEIL) |
273 |
compveil(); /* compute veil image */ |
274 |
if (!(what2do&DO_LINEAR)) |
275 |
if (mkbrmap() < 0) /* make tone map */ |
276 |
what2do |= DO_LINEAR; /* failed! -- use linear */ |
277 |
#if ADJ_VEIL |
278 |
else if (what2do&DO_VEIL) |
279 |
adjveil(); /* else adjust veil image */ |
280 |
#endif |
281 |
if (what2do&DO_LINEAR) { |
282 |
if (scalef <= FTINY) { |
283 |
if (what2do&DO_HSENS) |
284 |
scalef = htcontrs(Lb(0.5*(Bldmax+Bldmin))) / |
285 |
htcontrs(Lb(bwavg)); |
286 |
else |
287 |
scalef = Lb(0.5*(Bldmax+Bldmin)) / Lb(bwavg); |
288 |
scalef *= WHTEFFICACY/(inpexp*ldmax); |
289 |
} |
290 |
fputexpos(inpexp*scalef, stdout); /* record exposure */ |
291 |
if (lumf == cielum) scalef /= WHTEFFICACY; |
292 |
} |
293 |
fputformat(COLRFMT, stdout); /* complete header */ |
294 |
putchar('\n'); |
295 |
fputsresolu(&inpres, stdout); /* resolution doesn't change */ |
296 |
/* condition our image */ |
297 |
for (scan = firstscan(); scan != NULL; scan = nextscan()) |
298 |
if (fwritescan(scan, scanlen(&inpres), stdout) < 0) { |
299 |
fprintf(stderr, "%s: scanline write error\n", |
300 |
progname); |
301 |
exit(1); |
302 |
} |
303 |
} |
304 |
|
305 |
|
306 |
getfovimg() /* load foveal sampled image */ |
307 |
{ |
308 |
extern FILE *popen(); |
309 |
char combuf[128]; |
310 |
FILE *fp; |
311 |
int x, y; |
312 |
/* compute image size */ |
313 |
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 |
if (!(inpres.rt & YMAJOR)) { /* picture is rotated? */ |
318 |
y = fvyr; |
319 |
fvyr = fvxr; |
320 |
fvxr = y; |
321 |
} |
322 |
if ((fovimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR))) == NULL) |
323 |
syserror("malloc"); |
324 |
sprintf(combuf, "pfilt -1 -b -pa 0 -x %d -y %d %s", fvxr, fvyr, infn); |
325 |
if ((fp = popen(combuf, "r")) == NULL) |
326 |
syserror("popen"); |
327 |
getheader(fp, NULL, NULL); /* skip header */ |
328 |
if (fgetresolu(&x, &y, fp) < 0 || x != fvxr | y != fvyr) |
329 |
goto readerr; |
330 |
for (y = 0; y < fvyr; y++) |
331 |
if (freadscan(fovscan(y), fvxr, fp) < 0) |
332 |
goto readerr; |
333 |
pclose(fp); |
334 |
return; |
335 |
readerr: |
336 |
fprintf(stderr, "%s: error reading from pfilt process in fovimage\n", |
337 |
progname); |
338 |
exit(1); |
339 |
} |
340 |
|
341 |
|
342 |
check2do() /* check histogram to see what isn't worth doing */ |
343 |
{ |
344 |
double sum; |
345 |
double b, l; |
346 |
register int i; |
347 |
|
348 |
/* check for within display range */ |
349 |
if (bwmax - bwmin <= Bldmax - Bldmin) |
350 |
what2do |= DO_LINEAR; |
351 |
/* determine if veiling significant */ |
352 |
if (bwmax - bwmin < 4.5) /* heuristic */ |
353 |
what2do &= ~DO_VEIL; |
354 |
|
355 |
if (!(what2do & (DO_ACUITY|DO_COLOR))) |
356 |
return; |
357 |
/* find 5th percentile */ |
358 |
sum = histot*0.05; |
359 |
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 |
inpres.yr/sqrt(ourview.vn2))/(2.*180./PI)) |
368 |
what2do &= ~DO_ACUITY; |
369 |
/* color sensitivity loss? */ |
370 |
if (l >= TopMesopic) |
371 |
what2do &= ~DO_COLOR; |
372 |
} |