1 |
< |
/* Copyright (c) 1992 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1993 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
15 |
|
#include "color.h" |
16 |
|
|
17 |
|
#define TEPS 0.2 /* threshold proximity goal */ |
18 |
+ |
#define REPS 0.1 /* radius proximity goal */ |
19 |
|
|
20 |
|
extern double CHECKRAD; /* radius over which gaussian is summed */ |
21 |
|
|
43 |
|
extern int obarsize; /* size of output scan bar */ |
44 |
|
extern int orad; /* output window radius */ |
45 |
|
|
46 |
+ |
extern int wrapfilt; /* wrap filter horizontally? */ |
47 |
+ |
|
48 |
|
extern char *progname; |
49 |
|
|
50 |
|
float *gausstable; /* gauss lookup table */ |
54 |
|
short *ringndx; /* ring index table */ |
55 |
|
float *warr; /* array of pixel weights */ |
56 |
|
|
57 |
+ |
extern double (*ourbright)(); /* brightness computation function */ |
58 |
+ |
|
59 |
|
double pickfilt(); |
60 |
|
|
61 |
|
#define lookgauss(x) gausstable[(int)(10.*(x)+.5)] |
116 |
|
int wsum; |
117 |
|
double d; |
118 |
|
int y; |
119 |
< |
register int x; |
119 |
> |
register int x, offs; |
120 |
|
register COLOR *scan; |
121 |
|
|
122 |
|
wsum = 0; |
129 |
|
if (d >= 0.5) break; |
130 |
|
scan = scanin[y%barsize]; |
131 |
|
for (x = xcent+1-xbrad; x <= xcent+xbrad; x++) { |
132 |
< |
if (x < 0) continue; |
133 |
< |
if (x >= xres) break; |
132 |
> |
offs = x < 0 ? xres : x >= xres ? -xres : 0; |
133 |
> |
if (offs && !wrapfilt) |
134 |
> |
continue; |
135 |
|
d = x_c < 1.0 ? x_c*x - c : (double)(x - xcent); |
136 |
|
if (d < -0.5) continue; |
137 |
|
if (d >= 0.5) break; |
138 |
|
wsum++; |
139 |
< |
addcolor(csum, scan[x]); |
139 |
> |
addcolor(csum, scan[x+offs]); |
140 |
|
} |
141 |
|
} |
142 |
|
if (wsum > 1) |
152 |
|
double dy, dx, weight, wsum; |
153 |
|
COLOR ctmp; |
154 |
|
int y; |
155 |
< |
register int x; |
155 |
> |
register int x, offs; |
156 |
|
register COLOR *scan; |
157 |
|
|
158 |
|
wsum = FTINY; |
163 |
|
dy = (y_r*(y+.5) - (r+.5))/rad; |
164 |
|
scan = scanin[y%barsize]; |
165 |
|
for (x = xcent-xrad; x <= xcent+xrad; x++) { |
166 |
< |
if (x < 0) continue; |
167 |
< |
if (x >= xres) break; |
166 |
> |
offs = x < 0 ? xres : x >= xres ? -xres : 0; |
167 |
> |
if (offs && !wrapfilt) |
168 |
> |
continue; |
169 |
|
dx = (x_c*(x+.5) - (c+.5))/rad; |
170 |
|
weight = lookgauss(dx*dx + dy*dy); |
171 |
|
wsum += weight; |
172 |
< |
copycolor(ctmp, scan[x]); |
172 |
> |
copycolor(ctmp, scan[x+offs]); |
173 |
|
scalecolor(ctmp, weight); |
174 |
|
addcolor(csum, ctmp); |
175 |
|
} |
183 |
|
int ccent, rcent; |
184 |
|
{ |
185 |
|
double d; |
186 |
< |
int r, y; |
186 |
> |
int r, y, offs; |
187 |
|
register int c, x; |
188 |
|
register float *gscan; |
182 |
– |
#define pval gscan |
189 |
|
/* compute ring sums */ |
190 |
|
bzero((char *)ringsum, (orad+1)*sizeof(float)); |
191 |
|
bzero((char *)ringwt, (orad+1)*sizeof(short)); |
194 |
|
if (rcent+r >= nrows) break; |
195 |
|
gscan = greybar[(rcent+r)%obarsize]; |
196 |
|
for (c = -orad; c <= orad; c++) { |
197 |
< |
if (ccent+c < 0) continue; |
198 |
< |
if (ccent+c >= ncols) break; |
197 |
> |
offs = ccent+c < 0 ? ncols : |
198 |
> |
ccent+c >= ncols ? -ncols : 0; |
199 |
> |
if (offs && !wrapfilt) |
200 |
> |
continue; |
201 |
|
x = ringndx[c*c + r*r]; |
202 |
|
if (x < 0) continue; |
203 |
< |
ringsum[x] += gscan[ccent+c]; |
203 |
> |
ringsum[x] += gscan[ccent+c+offs]; |
204 |
|
ringwt[x]++; |
205 |
|
} |
206 |
|
} |
212 |
|
if (d < -0.5) continue; |
213 |
|
if (d >= 0.5) break; |
214 |
|
for (x = xcent+1-xbrad; x <= xcent+xbrad; x++) { |
215 |
< |
if (x < 0) continue; |
216 |
< |
if (x >= xres) break; |
215 |
> |
offs = x < 0 ? xres : x >= xres ? -xres : 0; |
216 |
> |
if (offs && !wrapfilt) |
217 |
> |
continue; |
218 |
|
d = x_c < 1.0 ? x_c*x - ccent : (double)(x - xcent); |
219 |
|
if (d < -0.5) continue; |
220 |
|
if (d >= 0.5) break; |
221 |
< |
pval = scanin[y%barsize][x]; |
222 |
< |
sumans(x, y, rcent, ccent, pickfilt(bright(pval))); |
221 |
> |
sumans(x, y, rcent, ccent, |
222 |
> |
pickfilt((*ourbright)(scanin[y%barsize][x+offs]))); |
223 |
|
} |
224 |
|
} |
216 |
– |
#undef pval |
225 |
|
} |
226 |
|
|
227 |
|
|
230 |
|
double p0; |
231 |
|
{ |
232 |
|
double m = 1.0; |
233 |
< |
double t, avg, wsum; |
233 |
> |
double t, num, denom, avg, wsum; |
234 |
> |
double mlimit[2]; |
235 |
|
int ilimit = 4/TEPS; |
236 |
|
register int i; |
237 |
|
/* iterative search for m */ |
238 |
+ |
mlimit[0] = 1.0; mlimit[1] = orad/rad/CHECKRAD; |
239 |
|
do { |
240 |
|
/* compute grey weighted average */ |
241 |
|
i = 1.25*CHECKRAD*rad*m + .5; |
247 |
|
avg += t*ringsum[i]; |
248 |
|
wsum += t*ringwt[i]; |
249 |
|
} |
250 |
+ |
if (avg < 1e-20) /* zero inclusive average */ |
251 |
+ |
return(1.0); |
252 |
|
avg /= wsum; |
253 |
|
/* check threshold */ |
254 |
< |
t = p0 - avg; |
255 |
< |
if (t < 0.0) t = -t; |
256 |
< |
t *= gausstable[0]/(m*m*avg); |
257 |
< |
if (t <= thresh && (m <= 1.0+FTINY || |
258 |
< |
(thresh-t)/thresh <= TEPS)) |
259 |
< |
break; |
260 |
< |
if (t > thresh && (m*CHECKRAD*rad >= orad-FTINY || |
261 |
< |
(t-thresh)/thresh <= TEPS)) |
262 |
< |
break; |
254 |
> |
denom = m*m/gausstable[0] - p0/avg; |
255 |
> |
if (denom <= FTINY) { /* zero exclusive average */ |
256 |
> |
if (m >= mlimit[1]-REPS) |
257 |
> |
break; |
258 |
> |
m = mlimit[1]; |
259 |
> |
continue; |
260 |
> |
} |
261 |
> |
num = p0/avg - 1.0; |
262 |
> |
if (num < 0.0) num = -num; |
263 |
> |
t = num/denom; |
264 |
> |
if (t <= thresh) { |
265 |
> |
if (m <= mlimit[0]+REPS || (thresh-t)/thresh <= TEPS) |
266 |
> |
break; |
267 |
> |
} else { |
268 |
> |
if (m >= mlimit[1]-REPS || (t-thresh)/thresh <= TEPS) |
269 |
> |
break; |
270 |
> |
} |
271 |
> |
t = m; /* remember current m */ |
272 |
|
/* next guesstimate */ |
273 |
< |
m *= sqrt(t/thresh); |
274 |
< |
if (m < 1.0) m = 1.0; |
275 |
< |
else if (m*CHECKRAD*rad > orad) m = orad/rad/CHECKRAD; |
273 |
> |
m = sqrt(gausstable[0]*(num/thresh + p0/avg)); |
274 |
> |
if (m < t) { /* bound it */ |
275 |
> |
if (m <= mlimit[0]+FTINY) |
276 |
> |
m = 0.5*(mlimit[0] + t); |
277 |
> |
mlimit[1] = t; |
278 |
> |
} else { |
279 |
> |
if (m >= mlimit[1]-FTINY) |
280 |
> |
m = 0.5*(mlimit[1] + t); |
281 |
> |
mlimit[0] = t; |
282 |
> |
} |
283 |
|
} while (--ilimit > 0); |
284 |
|
return(m); |
285 |
|
} |
292 |
|
{ |
293 |
|
double dy, dx; |
294 |
|
COLOR pval, ctmp; |
295 |
< |
int ksiz, r; |
295 |
> |
int ksiz, r, offs; |
296 |
|
double pc, pr, norm; |
297 |
|
register int i, c; |
298 |
|
register COLOR *scan; |
299 |
< |
|
300 |
< |
copycolor(pval, scanin[py%barsize][px]); |
299 |
> |
/* |
300 |
> |
* This normalization method fails at the picture borders because |
301 |
> |
* a different number of input pixels contribute there. |
302 |
> |
*/ |
303 |
> |
scan = scanin[py%barsize] + (px < 0 ? xres : px >= xres ? -xres : 0); |
304 |
> |
copycolor(pval, scan[px]); |
305 |
|
pc = x_c*(px+.5); |
306 |
|
pr = y_r*(py+.5); |
307 |
|
ksiz = CHECKRAD*m*rad + 1; |
314 |
|
if (r >= nrows) break; |
315 |
|
dy = (pr - (r+.5))/(m*rad); |
316 |
|
for (c = ccent-ksiz; c <= ccent+ksiz; c++) { |
317 |
< |
if (c < 0) continue; |
318 |
< |
if (c >= ncols) break; |
317 |
> |
if (!wrapfilt) { |
318 |
> |
if (c < 0) continue; |
319 |
> |
if (c >= ncols) break; |
320 |
> |
} |
321 |
|
dx = (pc - (c+.5))/(m*rad); |
322 |
|
norm += warr[i++] = lookgauss(dx*dx + dy*dy); |
323 |
|
} |
332 |
|
if (r >= nrows) break; |
333 |
|
scan = scoutbar[r%obarsize]; |
334 |
|
for (c = ccent-ksiz; c <= ccent+ksiz; c++) { |
335 |
< |
if (c < 0) continue; |
336 |
< |
if (c >= ncols) break; |
335 |
> |
offs = c < 0 ? ncols : c >= ncols ? -ncols : 0; |
336 |
> |
if (offs && !wrapfilt) |
337 |
> |
continue; |
338 |
|
copycolor(ctmp, pval); |
339 |
|
dx = norm*warr[i++]; |
340 |
|
scalecolor(ctmp, dx); |
341 |
< |
addcolor(scan[c], ctmp); |
341 |
> |
addcolor(scan[c+offs], ctmp); |
342 |
|
} |
343 |
|
} |
344 |
|
} |