1 |
greg |
3.1 |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Routines for veiling glare and loss of acuity. |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include "pcond.h" |
12 |
|
|
|
13 |
greg |
3.3 |
/************** VEILING STUFF *****************/ |
14 |
greg |
3.1 |
|
15 |
|
|
#define VADAPT 0.08 /* fraction of adaptation from veil */ |
16 |
|
|
|
17 |
|
|
extern COLOR *fovimg; /* foveal (1 degree) averaged image */ |
18 |
|
|
extern short fvxr, fvyr; /* foveal image resolution */ |
19 |
|
|
|
20 |
|
|
#define fovscan(y) (fovimg+(y)*fvxr) |
21 |
|
|
|
22 |
|
|
static COLOR *veilimg; /* veiling image */ |
23 |
|
|
|
24 |
|
|
#define veilscan(y) (veilimg+(y)*fvxr) |
25 |
|
|
|
26 |
greg |
3.2 |
static float (*raydir)[3] = NULL; /* ray direction for each pixel */ |
27 |
greg |
3.1 |
|
28 |
|
|
#define rdirscan(y) (raydir+(y)*fvxr) |
29 |
|
|
|
30 |
|
|
|
31 |
|
|
compraydir() /* compute ray directions */ |
32 |
|
|
{ |
33 |
greg |
3.2 |
FVECT rorg, rdir; |
34 |
greg |
3.1 |
double h, v; |
35 |
|
|
register int x, y; |
36 |
|
|
|
37 |
|
|
if (raydir != NULL) /* already done? */ |
38 |
|
|
return; |
39 |
greg |
3.2 |
raydir = (float (*)[3])malloc(fvxr*fvyr*3*sizeof(float)); |
40 |
greg |
3.1 |
if (raydir == NULL) |
41 |
|
|
syserror("malloc"); |
42 |
|
|
|
43 |
|
|
for (y = 0; y < fvyr; y++) { |
44 |
|
|
switch (inpres.or) { |
45 |
|
|
case YMAJOR: case YMAJOR|XDECR: |
46 |
|
|
v = (y+.5)/fvyr; break; |
47 |
|
|
case YMAJOR|YDECR: case YMAJOR|YDECR|XDECR: |
48 |
|
|
v = 1. - (y+.5)/fvyr; break; |
49 |
|
|
case 0: case YDECR: |
50 |
|
|
h = (y+.5)/fvyr; break; |
51 |
|
|
case XDECR: case XDECR|YDECR: |
52 |
|
|
h = 1. - (y+.5)/fvyr; break; |
53 |
|
|
} |
54 |
|
|
for (x = 0; x < fvxr; x++) { |
55 |
|
|
switch (inpres.or) { |
56 |
|
|
case YMAJOR: case YMAJOR|YDECR: |
57 |
|
|
h = (x+.5)/fvxr; break; |
58 |
|
|
case YMAJOR|XDECR: case YMAJOR|XDECR|YDECR: |
59 |
|
|
h = 1. - (x+.5)/fvxr; break; |
60 |
|
|
case 0: case XDECR: |
61 |
|
|
v = (x+.5)/fvxr; break; |
62 |
|
|
case YDECR: case YDECR|XDECR: |
63 |
|
|
v = 1. - (x+.5)/fvxr; break; |
64 |
|
|
} |
65 |
greg |
3.2 |
if (viewray(rorg, rdir, &ourview, h, v) |
66 |
|
|
>= -FTINY) { |
67 |
|
|
rdirscan(y)[x][0] = rdir[0]; |
68 |
|
|
rdirscan(y)[x][1] = rdir[1]; |
69 |
|
|
rdirscan(y)[x][2] = rdir[2]; |
70 |
|
|
} else { |
71 |
greg |
3.1 |
rdirscan(y)[x][0] = |
72 |
|
|
rdirscan(y)[x][1] = |
73 |
|
|
rdirscan(y)[x][2] = 0.0; |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
|
80 |
|
|
compveil() /* compute veiling image */ |
81 |
|
|
{ |
82 |
|
|
double t2, t2sum; |
83 |
|
|
COLOR ctmp, vsum; |
84 |
|
|
int px, py; |
85 |
|
|
register int x, y; |
86 |
|
|
/* compute ray directions */ |
87 |
|
|
compraydir(); |
88 |
|
|
/* compute veil image */ |
89 |
|
|
veilimg = (COLOR *)malloc(fvxr*fvyr*sizeof(COLOR)); |
90 |
|
|
if (veilimg == NULL) |
91 |
|
|
syserror("malloc"); |
92 |
|
|
for (py = 0; py < fvyr; py++) |
93 |
|
|
for (px = 0; px < fvxr; px++) { |
94 |
|
|
t2sum = 0.; |
95 |
|
|
setcolor(vsum, 0., 0., 0.); |
96 |
|
|
for (y = 0; y < fvyr; y++) |
97 |
|
|
for (x = 0; x < fvxr; x++) { |
98 |
|
|
if (x == px && y == py) continue; |
99 |
|
|
t2 = DOT(rdirscan(py)[px], |
100 |
|
|
rdirscan(y)[x]); |
101 |
|
|
if (t2 <= FTINY) continue; |
102 |
greg |
3.4 |
/* use approximation instead |
103 |
greg |
3.1 |
t2 = acos(t2); |
104 |
|
|
t2 = 1./(t2*t2); |
105 |
greg |
3.4 |
*/ |
106 |
|
|
t2 = .5 / (1. - t2); |
107 |
greg |
3.1 |
copycolor(ctmp, fovscan(y)[x]); |
108 |
|
|
scalecolor(ctmp, t2); |
109 |
|
|
addcolor(vsum, ctmp); |
110 |
|
|
t2sum += t2; |
111 |
|
|
} |
112 |
|
|
/* VADAPT of original is subtracted in addveil() */ |
113 |
|
|
scalecolor(vsum, VADAPT/t2sum); |
114 |
|
|
copycolor(veilscan(py)[px], vsum); |
115 |
|
|
} |
116 |
|
|
} |
117 |
|
|
|
118 |
|
|
|
119 |
|
|
addveil(sl, y) /* add veil to scanline */ |
120 |
|
|
COLOR *sl; |
121 |
|
|
int y; |
122 |
|
|
{ |
123 |
|
|
int vx, vy; |
124 |
|
|
double dx, dy; |
125 |
|
|
double lv, uv; |
126 |
|
|
register int x, i; |
127 |
|
|
|
128 |
|
|
vy = dy = (y+.5)/numscans(&inpres)*fvyr - .5; |
129 |
greg |
3.7 |
while (vy >= fvyr-1) vy--; |
130 |
greg |
3.1 |
dy -= (double)vy; |
131 |
|
|
for (x = 0; x < scanlen(&inpres); x++) { |
132 |
|
|
vx = dx = (x+.5)/scanlen(&inpres)*fvxr - .5; |
133 |
greg |
3.7 |
while (vx >= fvxr-1) vx--; |
134 |
greg |
3.1 |
dx -= (double)vx; |
135 |
|
|
for (i = 0; i < 3; i++) { |
136 |
|
|
lv = (1.-dy)*colval(veilscan(vy)[vx],i) + |
137 |
|
|
dy*colval(veilscan(vy+1)[vx],i); |
138 |
|
|
uv = (1.-dy)*colval(veilscan(vy)[vx+1],i) + |
139 |
|
|
dy*colval(veilscan(vy+1)[vx+1],i); |
140 |
|
|
colval(sl[x],i) = (1.-VADAPT)*colval(sl[x],i) + |
141 |
|
|
(1.-dx)*lv + dx*uv; |
142 |
|
|
} |
143 |
|
|
} |
144 |
greg |
3.3 |
} |
145 |
|
|
|
146 |
|
|
|
147 |
|
|
/****************** ACUITY STUFF *******************/ |
148 |
|
|
|
149 |
greg |
3.8 |
typedef struct { |
150 |
|
|
short sampe; /* sample area size (exponent of 2) */ |
151 |
greg |
3.3 |
short nscans; /* number of scanlines in this bar */ |
152 |
|
|
int len; /* individual scanline length */ |
153 |
|
|
int nread; /* number of scanlines loaded */ |
154 |
greg |
3.8 |
COLOR *sdata; /* scanbar data */ |
155 |
greg |
3.3 |
} SCANBAR; |
156 |
|
|
|
157 |
greg |
3.8 |
#define bscan(sb,y) ((COLOR *)(sb)->sdata+((y)%(sb)->nscans)*(sb)->len) |
158 |
greg |
3.3 |
|
159 |
|
|
SCANBAR *rootbar; /* root scan bar (lowest resolution) */ |
160 |
|
|
|
161 |
|
|
float *inpacuD; /* input acuity data (cycles/degree) */ |
162 |
|
|
|
163 |
|
|
#define tsampr(x,y) inpacuD[(y)*fvxr+(x)] |
164 |
|
|
|
165 |
|
|
|
166 |
|
|
double |
167 |
|
|
hacuity(La) /* return visual acuity in cycles/degree */ |
168 |
|
|
double La; |
169 |
|
|
{ /* data due to S. Shaler (we should fit it!) */ |
170 |
|
|
#define NPOINTS 20 |
171 |
|
|
static float l10lum[NPOINTS] = { |
172 |
|
|
-3.10503,-2.66403,-2.37703,-2.09303,-1.64403,-1.35803, |
173 |
|
|
-1.07403,-0.67203,-0.38503,-0.10103,0.29397,0.58097,0.86497, |
174 |
|
|
1.25697,1.54397,1.82797,2.27597,2.56297,2.84697,3.24897 |
175 |
|
|
}; |
176 |
|
|
static float resfreq[NPOINTS] = { |
177 |
|
|
2.09,3.28,3.79,4.39,6.11,8.83,10.94,18.66,23.88,31.05,37.42, |
178 |
|
|
37.68,41.60,43.16,45.30,47.00,48.43,48.32,51.06,51.09 |
179 |
|
|
}; |
180 |
|
|
double l10La; |
181 |
|
|
register int i; |
182 |
greg |
3.5 |
/* check limits */ |
183 |
|
|
if (La <= 7.85e-4) |
184 |
|
|
return(resfreq[0]); |
185 |
|
|
if (La >= 1.78e3) |
186 |
|
|
return(resfreq[NPOINTS-1]); |
187 |
|
|
/* interpolate data */ |
188 |
greg |
3.3 |
l10La = log10(La); |
189 |
|
|
for (i = 0; i < NPOINTS-2 && l10lum[i+1] <= l10La; i++) |
190 |
|
|
; |
191 |
|
|
return( ( (l10lum[i+1] - l10La)*resfreq[i] + |
192 |
|
|
(l10La - l10lum[i])*resfreq[i+1] ) / |
193 |
|
|
(l10lum[i+1] - l10lum[i]) ); |
194 |
|
|
#undef NPOINTS |
195 |
|
|
} |
196 |
|
|
|
197 |
|
|
|
198 |
|
|
COLOR * |
199 |
|
|
getascan(sb, y) /* find/read scanline y for scanbar sb */ |
200 |
|
|
register SCANBAR *sb; |
201 |
|
|
int y; |
202 |
|
|
{ |
203 |
|
|
register COLOR *sl0, *sl1, *mysl; |
204 |
|
|
register int i; |
205 |
|
|
|
206 |
greg |
3.6 |
if (y < sb->nread - sb->nscans) /* too far back? */ |
207 |
|
|
return(NULL); |
208 |
greg |
3.3 |
for ( ; y >= sb->nread; sb->nread++) { /* read as necessary */ |
209 |
|
|
mysl = bscan(sb, sb->nread); |
210 |
greg |
3.8 |
if (sb->sampe == 0) { |
211 |
greg |
3.3 |
if (freadscan(mysl, sb->len, infp) < 0) { |
212 |
|
|
fprintf(stderr, "%s: %s: scanline read error\n", |
213 |
|
|
progname, infn); |
214 |
|
|
exit(1); |
215 |
|
|
} |
216 |
|
|
} else { |
217 |
greg |
3.8 |
sl0 = getascan(sb+1, 2*y); |
218 |
greg |
3.6 |
if (sl0 == NULL) |
219 |
|
|
return(NULL); |
220 |
greg |
3.8 |
sl1 = getascan(sb+1, 2*y+1); |
221 |
greg |
3.3 |
for (i = 0; i < sb->len; i++) { |
222 |
|
|
copycolor(mysl[i], sl0[2*i]); |
223 |
|
|
addcolor(mysl[i], sl0[2*i+1]); |
224 |
|
|
addcolor(mysl[i], sl1[2*i]); |
225 |
|
|
addcolor(mysl[i], sl1[2*i+1]); |
226 |
|
|
scalecolor(mysl[i], 0.25); |
227 |
|
|
} |
228 |
|
|
} |
229 |
|
|
} |
230 |
|
|
return(bscan(sb, y)); |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
|
234 |
|
|
acuscan(scln, y) /* get acuity-sampled scanline */ |
235 |
|
|
COLOR *scln; |
236 |
|
|
int y; |
237 |
|
|
{ |
238 |
|
|
double sr; |
239 |
|
|
double dx, dy; |
240 |
|
|
int ix, iy; |
241 |
|
|
register int x; |
242 |
|
|
/* compute foveal y position */ |
243 |
|
|
iy = dy = (y+.5)/numscans(&inpres)*fvyr - .5; |
244 |
greg |
3.9 |
while (iy >= fvyr-1) iy--; |
245 |
greg |
3.3 |
dy -= (double)iy; |
246 |
|
|
for (x = 0; x < scanlen(&inpres); x++) { |
247 |
|
|
/* compute foveal x position */ |
248 |
|
|
ix = dx = (x+.5)/scanlen(&inpres)*fvxr - .5; |
249 |
greg |
3.9 |
while (ix >= fvxr-1) ix--; |
250 |
greg |
3.3 |
dx -= (double)ix; |
251 |
|
|
/* interpolate sample rate */ |
252 |
|
|
sr = (1.-dy)*((1.-dx)*tsampr(ix,iy) + dx*tsampr(ix+1,iy)) + |
253 |
|
|
dy*((1.-dx)*tsampr(ix,iy+1) + dx*tsampr(ix+1,iy+1)); |
254 |
|
|
|
255 |
|
|
acusample(scln[x], x, y, sr); /* compute sample */ |
256 |
|
|
} |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
|
260 |
|
|
acusample(col, x, y, sr) /* interpolate sample at (x,y) using rate sr */ |
261 |
|
|
COLOR col; |
262 |
|
|
int x, y; |
263 |
|
|
double sr; |
264 |
|
|
{ |
265 |
|
|
COLOR c1; |
266 |
|
|
double d; |
267 |
|
|
register SCANBAR *sb0; |
268 |
|
|
|
269 |
greg |
3.8 |
for (sb0 = rootbar; sb0->sampe != 0 && 1<<sb0[1].sampe > sr; sb0++) |
270 |
greg |
3.3 |
; |
271 |
|
|
ascanval(col, x, y, sb0); |
272 |
greg |
3.8 |
if (sb0->sampe == 0) /* don't extrapolate highest */ |
273 |
greg |
3.3 |
return; |
274 |
greg |
3.8 |
ascanval(c1, x, y, sb0+1); |
275 |
|
|
d = ((1<<sb0->sampe) - sr)/(1<<sb0[1].sampe); |
276 |
greg |
3.3 |
scalecolor(col, 1.-d); |
277 |
|
|
scalecolor(c1, d); |
278 |
|
|
addcolor(col, c1); |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
|
282 |
|
|
ascanval(col, x, y, sb) /* interpolate scanbar at orig. coords (x,y) */ |
283 |
|
|
COLOR col; |
284 |
|
|
int x, y; |
285 |
|
|
SCANBAR *sb; |
286 |
|
|
{ |
287 |
|
|
COLOR *sl0, *sl1, c1, c1y; |
288 |
|
|
double dx, dy; |
289 |
|
|
int ix, iy; |
290 |
|
|
|
291 |
greg |
3.8 |
if (sb->sampe == 0) { /* no need to interpolate */ |
292 |
greg |
3.6 |
sl0 = getascan(sb, y); |
293 |
|
|
copycolor(col, sl0[x]); |
294 |
|
|
return; |
295 |
|
|
} |
296 |
|
|
/* compute coordinates for sb */ |
297 |
greg |
3.8 |
ix = dx = (x+.5)/(1<<sb->sampe) - .5; |
298 |
greg |
3.7 |
while (ix >= sb->len-1) ix--; |
299 |
greg |
3.3 |
dx -= (double)ix; |
300 |
greg |
3.8 |
iy = dy = (y+.5)/(1<<sb->sampe) - .5; |
301 |
|
|
while (iy >= (numscans(&inpres)>>sb->sampe)-1) iy--; |
302 |
greg |
3.3 |
dy -= (double)iy; |
303 |
|
|
/* get scanlines */ |
304 |
|
|
sl0 = getascan(sb, iy); |
305 |
greg |
3.9 |
#ifdef DEBUG |
306 |
greg |
3.6 |
if (sl0 == NULL) { |
307 |
|
|
fprintf(stderr, "%s: internal - cannot backspace in ascanval\n", |
308 |
|
|
progname); |
309 |
greg |
3.9 |
abort(); |
310 |
greg |
3.6 |
} |
311 |
greg |
3.9 |
#endif |
312 |
greg |
3.3 |
sl1 = getascan(sb, iy+1); |
313 |
|
|
/* 2D linear interpolation */ |
314 |
|
|
copycolor(col, sl0[ix]); |
315 |
|
|
scalecolor(col, 1.-dx); |
316 |
|
|
copycolor(c1, sl0[ix+1]); |
317 |
|
|
scalecolor(c1, dx); |
318 |
|
|
addcolor(col, c1); |
319 |
|
|
copycolor(c1y, sl1[ix]); |
320 |
|
|
scalecolor(c1y, 1.-dx); |
321 |
|
|
copycolor(c1, sl1[ix+1]); |
322 |
|
|
scalecolor(c1, dx); |
323 |
|
|
addcolor(c1y, c1); |
324 |
|
|
scalecolor(col, 1.-dy); |
325 |
|
|
scalecolor(c1y, dy); |
326 |
|
|
addcolor(col, c1y); |
327 |
greg |
3.9 |
for (ix = 0; ix < 3; ix++) /* make sure no negative */ |
328 |
|
|
if (colval(col,ix) < 0.) |
329 |
|
|
colval(col,ix) = 0.; |
330 |
greg |
3.3 |
} |
331 |
|
|
|
332 |
|
|
|
333 |
|
|
SCANBAR * |
334 |
greg |
3.9 |
sballoc(se, ns, sl) /* allocate scanbar */ |
335 |
|
|
int se; /* sampling rate exponent */ |
336 |
greg |
3.3 |
int ns; /* number of scanlines */ |
337 |
|
|
int sl; /* original scanline length */ |
338 |
|
|
{ |
339 |
greg |
3.8 |
SCANBAR *sbarr; |
340 |
greg |
3.3 |
register SCANBAR *sb; |
341 |
|
|
|
342 |
greg |
3.9 |
sbarr = sb = (SCANBAR *)malloc((se+1)*sizeof(SCANBAR)); |
343 |
greg |
3.3 |
if (sb == NULL) |
344 |
|
|
syserror("malloc"); |
345 |
greg |
3.8 |
do { |
346 |
greg |
3.9 |
sb->sampe = se; |
347 |
|
|
sb->len = sl>>se; |
348 |
|
|
sb->nscans = ns; |
349 |
|
|
sb->sdata = (COLOR *)malloc(sb->len*ns*sizeof(COLOR)); |
350 |
greg |
3.8 |
if (sb->sdata == NULL) |
351 |
|
|
syserror("malloc"); |
352 |
|
|
sb->nread = 0; |
353 |
|
|
ns <<= 1; |
354 |
|
|
sb++; |
355 |
greg |
3.9 |
} while (--se >= 0); |
356 |
greg |
3.8 |
return(sbarr); |
357 |
greg |
3.3 |
} |
358 |
|
|
|
359 |
|
|
|
360 |
|
|
initacuity() /* initialize variable acuity sampling */ |
361 |
|
|
{ |
362 |
|
|
FVECT diffx, diffy, cp; |
363 |
|
|
double omega, maxsr; |
364 |
|
|
register int x, y, i; |
365 |
|
|
|
366 |
|
|
compraydir(); /* compute ray directions */ |
367 |
|
|
|
368 |
|
|
inpacuD = (float *)malloc(fvxr*fvyr*sizeof(float)); |
369 |
|
|
if (inpacuD == NULL) |
370 |
|
|
syserror("malloc"); |
371 |
|
|
maxsr = 1.; /* compute internal sample rates */ |
372 |
|
|
for (y = 1; y < fvyr-1; y++) |
373 |
|
|
for (x = 1; x < fvxr-1; x++) { |
374 |
|
|
for (i = 0; i < 3; i++) { |
375 |
|
|
diffx[i] = 0.5*fvxr/scanlen(&inpres) * |
376 |
|
|
(rdirscan(y)[x+1][i] - |
377 |
|
|
rdirscan(y)[x-1][i]); |
378 |
|
|
diffy[i] = 0.5*fvyr/numscans(&inpres) * |
379 |
|
|
(rdirscan(y+1)[x][i] - |
380 |
|
|
rdirscan(y-1)[x][i]); |
381 |
|
|
} |
382 |
|
|
fcross(cp, diffx, diffy); |
383 |
|
|
omega = 0.5 * sqrt(DOT(cp,cp)); |
384 |
greg |
3.4 |
if (omega <= FTINY) |
385 |
|
|
tsampr(x,y) = 1.; |
386 |
|
|
else if ((tsampr(x,y) = PI/180. / sqrt(omega) / |
387 |
|
|
hacuity(plum(fovscan(y)[x]))) > maxsr) |
388 |
greg |
3.3 |
maxsr = tsampr(x,y); |
389 |
|
|
} |
390 |
|
|
/* copy perimeter (easier) */ |
391 |
|
|
for (x = 1; x < fvxr-1; x++) { |
392 |
|
|
tsampr(x,0) = tsampr(x,1); |
393 |
|
|
tsampr(x,fvyr-1) = tsampr(x,fvyr-2); |
394 |
|
|
} |
395 |
|
|
for (y = 0; y < fvyr; y++) { |
396 |
greg |
3.9 |
tsampr(0,y) = tsampr(1,y); |
397 |
|
|
tsampr(fvxr-1,y) = tsampr(fvxr-2,y); |
398 |
greg |
3.3 |
} |
399 |
|
|
/* initialize with next power of two */ |
400 |
greg |
3.8 |
rootbar = sballoc((int)(log(maxsr)/log(2.))+1, 2, scanlen(&inpres)); |
401 |
greg |
3.1 |
} |