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 |
/************** VEILING STUFF *****************/ |
14 |
|
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 |
static float (*raydir)[3] = NULL; /* ray direction for each pixel */ |
27 |
|
28 |
#define rdirscan(y) (raydir+(y)*fvxr) |
29 |
|
30 |
|
31 |
compraydir() /* compute ray directions */ |
32 |
{ |
33 |
FVECT rorg, rdir; |
34 |
double h, v; |
35 |
register int x, y; |
36 |
|
37 |
if (raydir != NULL) /* already done? */ |
38 |
return; |
39 |
raydir = (float (*)[3])malloc(fvxr*fvyr*3*sizeof(float)); |
40 |
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 |
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 |
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 |
/* use approximation instead |
103 |
t2 = acos(t2); |
104 |
t2 = 1./(t2*t2); |
105 |
*/ |
106 |
t2 = .5 / (1. - t2); |
107 |
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 |
if (vy >= fvyr-1) vy--; |
130 |
dy -= (double)vy; |
131 |
for (x = 0; x < scanlen(&inpres); x++) { |
132 |
vx = dx = (x+.5)/scanlen(&inpres)*fvxr - .5; |
133 |
if (vx >= fvxr-1) vx--; |
134 |
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 |
} |
145 |
|
146 |
|
147 |
/****************** ACUITY STUFF *******************/ |
148 |
|
149 |
typedef struct scanbar { |
150 |
short sampr; /* sample area size (power of 2) */ |
151 |
short nscans; /* number of scanlines in this bar */ |
152 |
int len; /* individual scanline length */ |
153 |
struct scanbar *next; /* next higher resolution scanbar */ |
154 |
int nread; /* number of scanlines loaded */ |
155 |
/* followed by the scanline data */ |
156 |
} SCANBAR; |
157 |
|
158 |
#define bscan(sb,y) ((COLOR *)((sb)+1)+((y)%(sb)->nscans)*(sb)->len) |
159 |
|
160 |
SCANBAR *rootbar; /* root scan bar (lowest resolution) */ |
161 |
|
162 |
float *inpacuD; /* input acuity data (cycles/degree) */ |
163 |
|
164 |
#define tsampr(x,y) inpacuD[(y)*fvxr+(x)] |
165 |
|
166 |
|
167 |
double |
168 |
hacuity(La) /* return visual acuity in cycles/degree */ |
169 |
double La; |
170 |
{ /* data due to S. Shaler (we should fit it!) */ |
171 |
#define NPOINTS 20 |
172 |
static float l10lum[NPOINTS] = { |
173 |
-3.10503,-2.66403,-2.37703,-2.09303,-1.64403,-1.35803, |
174 |
-1.07403,-0.67203,-0.38503,-0.10103,0.29397,0.58097,0.86497, |
175 |
1.25697,1.54397,1.82797,2.27597,2.56297,2.84697,3.24897 |
176 |
}; |
177 |
static float resfreq[NPOINTS] = { |
178 |
2.09,3.28,3.79,4.39,6.11,8.83,10.94,18.66,23.88,31.05,37.42, |
179 |
37.68,41.60,43.16,45.30,47.00,48.43,48.32,51.06,51.09 |
180 |
}; |
181 |
double l10La; |
182 |
register int i; |
183 |
/* check limits */ |
184 |
if (La <= 7.85e-4) |
185 |
return(resfreq[0]); |
186 |
if (La >= 1.78e3) |
187 |
return(resfreq[NPOINTS-1]); |
188 |
/* interpolate data */ |
189 |
l10La = log10(La); |
190 |
for (i = 0; i < NPOINTS-2 && l10lum[i+1] <= l10La; i++) |
191 |
; |
192 |
return( ( (l10lum[i+1] - l10La)*resfreq[i] + |
193 |
(l10La - l10lum[i])*resfreq[i+1] ) / |
194 |
(l10lum[i+1] - l10lum[i]) ); |
195 |
#undef NPOINTS |
196 |
} |
197 |
|
198 |
|
199 |
COLOR * |
200 |
getascan(sb, y) /* find/read scanline y for scanbar sb */ |
201 |
register SCANBAR *sb; |
202 |
int y; |
203 |
{ |
204 |
register COLOR *sl0, *sl1, *mysl; |
205 |
register int i; |
206 |
|
207 |
if (y < sb->nread - sb->nscans) { |
208 |
fprintf(stderr, "%s: internal - cannot backspace in getascan\n", |
209 |
progname); |
210 |
exit(1); |
211 |
} |
212 |
for ( ; y >= sb->nread; sb->nread++) { /* read as necessary */ |
213 |
mysl = bscan(sb, sb->nread); |
214 |
if (sb->sampr == 1) { |
215 |
if (freadscan(mysl, sb->len, infp) < 0) { |
216 |
fprintf(stderr, "%s: %s: scanline read error\n", |
217 |
progname, infn); |
218 |
exit(1); |
219 |
} |
220 |
} else { |
221 |
sl0 = getascan(sb->next, 2*y); |
222 |
sl1 = getascan(sb->next, 2*y+1); |
223 |
for (i = 0; i < sb->len; i++) { |
224 |
copycolor(mysl[i], sl0[2*i]); |
225 |
addcolor(mysl[i], sl0[2*i+1]); |
226 |
addcolor(mysl[i], sl1[2*i]); |
227 |
addcolor(mysl[i], sl1[2*i+1]); |
228 |
scalecolor(mysl[i], 0.25); |
229 |
} |
230 |
} |
231 |
} |
232 |
return(bscan(sb, y)); |
233 |
} |
234 |
|
235 |
|
236 |
acuscan(scln, y) /* get acuity-sampled scanline */ |
237 |
COLOR *scln; |
238 |
int y; |
239 |
{ |
240 |
double sr; |
241 |
double dx, dy; |
242 |
int ix, iy; |
243 |
register int x; |
244 |
/* compute foveal y position */ |
245 |
iy = dy = (y+.5)/numscans(&inpres)*fvyr - .5; |
246 |
if (iy >= fvyr-1) iy--; |
247 |
dy -= (double)iy; |
248 |
for (x = 0; x < scanlen(&inpres); x++) { |
249 |
/* compute foveal x position */ |
250 |
ix = dx = (x+.5)/scanlen(&inpres)*fvxr - .5; |
251 |
if (ix >= fvxr-1) ix--; |
252 |
dx -= (double)ix; |
253 |
/* interpolate sample rate */ |
254 |
sr = (1.-dy)*((1.-dx)*tsampr(ix,iy) + dx*tsampr(ix+1,iy)) + |
255 |
dy*((1.-dx)*tsampr(ix,iy+1) + dx*tsampr(ix+1,iy+1)); |
256 |
|
257 |
acusample(scln[x], x, y, sr); /* compute sample */ |
258 |
} |
259 |
} |
260 |
|
261 |
|
262 |
acusample(col, x, y, sr) /* interpolate sample at (x,y) using rate sr */ |
263 |
COLOR col; |
264 |
int x, y; |
265 |
double sr; |
266 |
{ |
267 |
COLOR c1; |
268 |
double d; |
269 |
register SCANBAR *sb0; |
270 |
|
271 |
for (sb0 = rootbar; sb0->next != NULL && sb0->next->sampr > sr; |
272 |
sb0 = sb0->next) |
273 |
; |
274 |
ascanval(col, x, y, sb0); |
275 |
if (sb0->next == NULL) /* don't extrapolate highest */ |
276 |
return; |
277 |
ascanval(c1, x, y, sb0->next); |
278 |
d = (sb0->sampr - sr)/(sb0->sampr - sb0->next->sampr); |
279 |
scalecolor(col, 1.-d); |
280 |
scalecolor(c1, d); |
281 |
addcolor(col, c1); |
282 |
} |
283 |
|
284 |
|
285 |
ascanval(col, x, y, sb) /* interpolate scanbar at orig. coords (x,y) */ |
286 |
COLOR col; |
287 |
int x, y; |
288 |
SCANBAR *sb; |
289 |
{ |
290 |
COLOR *sl0, *sl1, c1, c1y; |
291 |
double dx, dy; |
292 |
int ix, iy; |
293 |
|
294 |
ix = dx = (x+.5)/sb->sampr - .5; |
295 |
if (ix >= sb->len-1) ix--; |
296 |
dx -= (double)ix; |
297 |
iy = dy = (y+.5)/sb->sampr - .5; |
298 |
if (iy >= numscans(&inpres)/sb->sampr-1) iy--; |
299 |
dy -= (double)iy; |
300 |
/* get scanlines */ |
301 |
sl0 = getascan(sb, iy); |
302 |
sl1 = getascan(sb, iy+1); |
303 |
/* 2D linear interpolation */ |
304 |
copycolor(col, sl0[ix]); |
305 |
scalecolor(col, 1.-dx); |
306 |
copycolor(c1, sl0[ix+1]); |
307 |
scalecolor(c1, dx); |
308 |
addcolor(col, c1); |
309 |
copycolor(c1y, sl1[ix]); |
310 |
scalecolor(c1y, 1.-dx); |
311 |
copycolor(c1, sl1[ix+1]); |
312 |
scalecolor(c1, dx); |
313 |
addcolor(c1y, c1); |
314 |
scalecolor(col, 1.-dy); |
315 |
scalecolor(c1y, dy); |
316 |
addcolor(col, c1y); |
317 |
} |
318 |
|
319 |
|
320 |
SCANBAR * |
321 |
sballoc(sr, ns, sl) /* allocate scanbar */ |
322 |
int sr; /* sampling rate */ |
323 |
int ns; /* number of scanlines */ |
324 |
int sl; /* original scanline length */ |
325 |
{ |
326 |
register SCANBAR *sb; |
327 |
|
328 |
sb = (SCANBAR *)malloc(sizeof(SCANBAR)+(sl/sr)*ns*sizeof(COLOR)); |
329 |
if (sb == NULL) |
330 |
syserror("malloc"); |
331 |
sb->nscans = ns; |
332 |
sb->len = sl/sr; |
333 |
sb->nread = 0; |
334 |
if ((sb->sampr = sr) > 1) |
335 |
sb->next = sballoc(sr/2, ns*2, sl); |
336 |
else |
337 |
sb->next = NULL; |
338 |
return(sb); |
339 |
} |
340 |
|
341 |
|
342 |
initacuity() /* initialize variable acuity sampling */ |
343 |
{ |
344 |
FVECT diffx, diffy, cp; |
345 |
double omega, maxsr; |
346 |
register int x, y, i; |
347 |
|
348 |
compraydir(); /* compute ray directions */ |
349 |
|
350 |
inpacuD = (float *)malloc(fvxr*fvyr*sizeof(float)); |
351 |
if (inpacuD == NULL) |
352 |
syserror("malloc"); |
353 |
maxsr = 1.; /* compute internal sample rates */ |
354 |
for (y = 1; y < fvyr-1; y++) |
355 |
for (x = 1; x < fvxr-1; x++) { |
356 |
for (i = 0; i < 3; i++) { |
357 |
diffx[i] = 0.5*fvxr/scanlen(&inpres) * |
358 |
(rdirscan(y)[x+1][i] - |
359 |
rdirscan(y)[x-1][i]); |
360 |
diffy[i] = 0.5*fvyr/numscans(&inpres) * |
361 |
(rdirscan(y+1)[x][i] - |
362 |
rdirscan(y-1)[x][i]); |
363 |
} |
364 |
fcross(cp, diffx, diffy); |
365 |
omega = 0.5 * sqrt(DOT(cp,cp)); |
366 |
if (omega <= FTINY) |
367 |
tsampr(x,y) = 1.; |
368 |
else if ((tsampr(x,y) = PI/180. / sqrt(omega) / |
369 |
hacuity(plum(fovscan(y)[x]))) > maxsr) |
370 |
maxsr = tsampr(x,y); |
371 |
} |
372 |
/* copy perimeter (easier) */ |
373 |
for (x = 1; x < fvxr-1; x++) { |
374 |
tsampr(x,0) = tsampr(x,1); |
375 |
tsampr(x,fvyr-1) = tsampr(x,fvyr-2); |
376 |
} |
377 |
for (y = 0; y < fvyr; y++) { |
378 |
tsampr(y,0) = tsampr(y,1); |
379 |
tsampr(y,fvxr-1) = tsampr(y,fvxr-2); |
380 |
} |
381 |
/* initialize with next power of two */ |
382 |
rootbar = sballoc(2<<(int)(log(maxsr)/log(2.)), 2, scanlen(&inpres)); |
383 |
} |