ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/pf3.c
(Generate patch)

Comparing ray/src/px/pf3.c (file contents):
Revision 1.1 by greg, Thu Feb 2 10:49:26 1989 UTC vs.
Revision 2.10 by greg, Thu Jul 15 18:32:06 1993 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 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";
# Line 10 | Line 10 | static char SCCSid[] = "$SunId$ LBL";
10   *     8/13/86
11   */
12  
13 < #include  <stdio.h>
13 > #include  "standard.h"
14  
15   #include  "color.h"
16  
17 < #define  FTINY          1e-7
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 +
22   extern double  rad;             /* output pixel radius for filtering */
23  
24 + extern double  thresh;          /* maximum contribution for subpixel */
25 +
26   extern int  nrows;              /* number of rows for output */
27   extern int  ncols;              /* number of columns for output */
28  
24 extern int  boxfilt;            /* do box filtering? */
25
29   extern int  xres, yres;         /* resolution of input */
30  
31   extern double  x_c, y_r;        /* conversion factors */
32  
33 < extern int  xrad;               /* x window size */
34 < extern int  yrad;               /* y window size */
33 > extern int  xrad;               /* x search radius */
34 > extern int  yrad;               /* y search radius */
35 > extern int  xbrad;              /* x box size */
36 > extern int  ybrad;              /* y box size */
37  
38   extern int  barsize;            /* size of input scan bar */
39   extern COLOR  **scanin;         /* input scan bar */
40   extern COLOR  *scanout;         /* output scan line */
41 + extern COLOR  **scoutbar;       /* output scan bar (if thresh > 0) */
42 + extern float  **greybar;        /* grey-averaged input values */
43 + extern int  obarsize;           /* size of output scan bar */
44 + extern int  orad;               /* output window radius */
45  
46   extern char  *progname;
47  
48 < float  *exptable;               /* exponent table */
48 > float  *gausstable;             /* gauss lookup table */
49  
50 < #define  lookexp(x)             exptable[(int)(-10.*(x)+.5)]
50 > float  *ringsum;                /* sum of ring values */
51 > short  *ringwt;                 /* weight (count) of ring values */
52 > short  *ringndx;                /* ring index table */
53 > float  *warr;                   /* array of pixel weights */
54  
55 + double  pickfilt();
56  
57 + #define  lookgauss(x)           gausstable[(int)(10.*(x)+.5)]
58 +
59 +
60   initmask()                      /* initialize gaussian lookup table */
61   {
62 <        extern char  *malloc();
63 <        extern double  exp();
62 >        int  gtabsiz;
63 >        double  gaussN;
64 >        double  d;
65          register int  x;
66  
67 <        exptable = (float *)malloc(100*sizeof(float));
68 <        if (exptable == NULL) {
69 <                fprintf(stderr, "%s: out of memory in initmask\n", progname);
70 <                quit(1);
71 <        }
72 <        for (x = 0; x < 100; x++)
73 <                exptable[x] = exp(-x*0.1);
67 >        gtabsiz = 150*CHECKRAD;
68 >        gausstable = (float *)malloc(gtabsiz*sizeof(float));
69 >        if (gausstable == NULL)
70 >                goto memerr;
71 >        d = x_c*y_r*0.25/(rad*rad);
72 >        gausstable[0] = exp(-d)/sqrt(d);
73 >        for (x = 1; x < gtabsiz; x++)
74 >                if ((gausstable[x] = exp(-x*0.1)/sqrt(x*0.1)) > gausstable[0])
75 >                        gausstable[x] = gausstable[0];
76 >        if (obarsize == 0)
77 >                return;
78 >                                        /* compute integral of filter */
79 >        gaussN = PI*sqrt(d)*exp(-d);            /* plateau */
80 >        for (d = sqrt(d)+0.05; d <= 1.25*CHECKRAD; d += 0.1)
81 >                gaussN += 0.1*2.0*PI*exp(-d*d);
82 >                                        /* normalize filter */
83 >        gaussN = x_c*y_r/(rad*rad*gaussN);
84 >        for (x = 0; x < gtabsiz; x++)
85 >                gausstable[x] *= gaussN;
86 >                                        /* create ring averages table */
87 >        ringndx = (short *)malloc((2*orad*orad+1)*sizeof(short));
88 >        if (ringndx == NULL)
89 >                goto memerr;
90 >        for (x = 2*orad*orad+1; --x > orad*orad; )
91 >                ringndx[x] = -1;
92 >        do
93 >                ringndx[x] = sqrt((double)x);
94 >        while (x--);
95 >        ringsum = (float *)malloc((orad+1)*sizeof(float));
96 >        ringwt = (short *)malloc((orad+1)*sizeof(short));
97 >        warr = (float *)malloc(obarsize*obarsize*sizeof(float));
98 >        if (ringsum == NULL | ringwt == 0 | warr == NULL)
99 >                goto memerr;
100 >        return;
101 > memerr:
102 >        fprintf(stderr, "%s: out of memory in initmask\n", progname);
103 >        quit(1);
104   }
105  
106  
# Line 62 | Line 109 | COLOR  csum;
109   int  xcent, ycent;
110   int  c, r;
111   {
112 <        static int  wsum;
113 <        static double  d;
114 <        static int  y;
112 >        int  wsum;
113 >        double  d;
114 >        int  y;
115          register int  x;
116 <        register COLOR  *scan;
117 <
116 >        register COLOR  *scan;
117 >        
118          wsum = 0;
119          setcolor(csum, 0.0, 0.0, 0.0);
120 <        for (y = ycent+1-yrad; y <= ycent+yrad; y++) {
121 <                if (y < 0 || y >= yres)
122 <                        continue;
123 <                d = y_r < 1.0 ? y_r*y - r : y - ycent;
124 <                if (d > 0.5+FTINY || d < -0.5-FTINY)
125 <                        continue;
120 >        for (y = ycent+1-ybrad; y <= ycent+ybrad; y++) {
121 >                if (y < 0) continue;
122 >                if (y >= yres) break;
123 >                d = y_r < 1.0 ? y_r*y - r : (double)(y - ycent);
124 >                if (d < -0.5) continue;
125 >                if (d >= 0.5) break;
126                  scan = scanin[y%barsize];
127 <                for (x = xcent+1-xrad; x <= xcent+xrad; x++) {
128 <                        if (x < 0 || x >= xres)
129 <                                continue;
130 <                        d = x_c < 1.0 ? x_c*x - c : x - xcent;
131 <                        if (d > 0.5+FTINY || d < -0.5-FTINY)
132 <                                continue;
127 >                for (x = xcent+1-xbrad; x <= xcent+xbrad; x++) {
128 >                        if (x < 0) continue;
129 >                        if (x >= xres) break;
130 >                        d = x_c < 1.0 ? x_c*x - c : (double)(x - xcent);
131 >                        if (d < -0.5) continue;
132 >                        if (d >= 0.5) break;
133                          wsum++;
134                          addcolor(csum, scan[x]);
135                  }
# Line 97 | Line 144 | COLOR  csum;
144   int  xcent, ycent;
145   int  c, r;
146   {
147 <        static double  dy, dx, weight, wsum;
148 <        static COLOR  ctmp;
149 <        static int  y;
147 >        double  dy, dx, weight, wsum;
148 >        COLOR  ctmp;
149 >        int  y;
150          register int  x;
151 <        register COLOR  *scan;
151 >        register COLOR  *scan;
152  
153          wsum = FTINY;
154          setcolor(csum, 0.0, 0.0, 0.0);
155 <        for (y = ycent+1-yrad; y <= ycent+yrad; y++) {
156 <                if (y < 0 || y >= yres)
157 <                        continue;
158 <                dy = (y_r*y - r)/rad;
155 >        for (y = ycent-yrad; y <= ycent+yrad; y++) {
156 >                if (y < 0) continue;
157 >                if (y >= yres) break;
158 >                dy = (y_r*(y+.5) - (r+.5))/rad;
159                  scan = scanin[y%barsize];
160 <                for (x = xcent+1-xrad; x <= xcent+xrad; x++) {
161 <                        if (x < 0 || x >= xres)
162 <                                continue;
163 <                        dx = (x_c*x - c)/rad;
164 <                        weight = lookexp(-(dx*dx + dy*dy));
160 >                for (x = xcent-xrad; x <= xcent+xrad; x++) {
161 >                        if (x < 0) continue;
162 >                        if (x >= xres) break;
163 >                        dx = (x_c*(x+.5) - (c+.5))/rad;
164 >                        weight = lookgauss(dx*dx + dy*dy);
165                          wsum += weight;
166                          copycolor(ctmp, scan[x]);
167                          scalecolor(ctmp, weight);
# Line 122 | Line 169 | int  c, r;
169                  }
170          }
171          scalecolor(csum, 1.0/wsum);
172 + }
173 +
174 +
175 + dothresh(xcent, ycent, ccent, rcent)    /* gaussian threshold filter */
176 + int  xcent, ycent;
177 + int  ccent, rcent;
178 + {
179 +        double  d;
180 +        int  r, y;
181 +        register int  c, x;
182 +        register float  *gscan;
183 + #define pval gscan
184 +                                        /* compute ring sums */
185 +        bzero((char *)ringsum, (orad+1)*sizeof(float));
186 +        bzero((char *)ringwt, (orad+1)*sizeof(short));
187 +        for (r = -orad; r <= orad; r++) {
188 +                if (rcent+r < 0) continue;
189 +                if (rcent+r >= nrows) break;
190 +                gscan = greybar[(rcent+r)%obarsize];
191 +                for (c = -orad; c <= orad; c++) {
192 +                        if (ccent+c < 0) continue;
193 +                        if (ccent+c >= ncols) break;
194 +                        x = ringndx[c*c + r*r];
195 +                        if (x < 0) continue;
196 +                        ringsum[x] += gscan[ccent+c];
197 +                        ringwt[x]++;
198 +                }
199 +        }
200 +                                        /* filter each subpixel */
201 +        for (y = ycent+1-ybrad; y <= ycent+ybrad; y++) {
202 +                if (y < 0) continue;
203 +                if (y >= yres) break;
204 +                d = y_r < 1.0 ? y_r*y - rcent : (double)(y - ycent);
205 +                if (d < -0.5) continue;
206 +                if (d >= 0.5) break;
207 +                for (x = xcent+1-xbrad; x <= xcent+xbrad; x++) {
208 +                        if (x < 0) continue;
209 +                        if (x >= xres) break;
210 +                        d = x_c < 1.0 ? x_c*x - ccent : (double)(x - xcent);
211 +                        if (d < -0.5) continue;
212 +                        if (d >= 0.5) break;
213 +                        pval = scanin[y%barsize][x];
214 +                        sumans(x, y, rcent, ccent, pickfilt(bright(pval)));
215 +                }
216 +        }
217 + #undef pval
218 + }
219 +
220 +
221 + double
222 + pickfilt(p0)                    /* find filter multiplier for p0 */
223 + double  p0;
224 + {
225 +        double  m = 1.0;
226 +        double  t, num, denom, avg, wsum;
227 +        double  maxm = orad/rad/CHECKRAD;
228 +        double  mlimit[2];
229 +        int  ilimit = 4/TEPS;
230 +        register int  i;
231 +                                /* iterative search for m */
232 +        mlimit[0] = 1.0; mlimit[1] = maxm;
233 +        do {
234 +                                        /* compute grey weighted average */
235 +                i = 1.25*CHECKRAD*rad*m + .5;
236 +                if (i > orad) i = orad;
237 +                avg = wsum = 0.0;
238 +                while (i--) {
239 +                        t = (i+.5)/(m*rad);
240 +                        t = lookgauss(t*t);
241 +                        avg += t*ringsum[i];
242 +                        wsum += t*ringwt[i];
243 +                }
244 +                if (avg < 1e-20)                /* zero inclusive average */
245 +                        return(1.0);
246 +                avg /= wsum;
247 +                                        /* check threshold */
248 +                denom = m*m/gausstable[0] - p0/avg;
249 +                if (denom <= FTINY)             /* zero exclusive average */
250 +                        return(maxm);
251 +                num = p0/avg - 1.0;
252 +                if (num < 0.0) num = -num;
253 +                t = num/denom;
254 +                if (t <= thresh) {
255 +                        if (m <= mlimit[0]+REPS || (thresh-t)/thresh <= TEPS)
256 +                                break;
257 +                } else {
258 +                        if (m >= mlimit[1]-REPS || (t-thresh)/thresh <= TEPS)
259 +                                break;
260 +                }
261 +                t = m;                  /* remember current m */
262 +                                        /* next guesstimate */
263 +                m = sqrt(gausstable[0]*(num/thresh + p0/avg));
264 +                if (m < t) {            /* bound it */
265 +                        if (m <= mlimit[0]+FTINY)
266 +                                m = 0.5*(mlimit[0] + t);
267 +                        mlimit[1] = t;
268 +                } else {
269 +                        if (m >= mlimit[1]-FTINY)
270 +                                m = 0.5*(mlimit[1] + t);
271 +                        mlimit[0] = t;
272 +                }
273 +        } while (--ilimit > 0);
274 +        return(m);
275 + }
276 +
277 +
278 + sumans(px, py, rcent, ccent, m)         /* sum input pixel to output */
279 + int  px, py;
280 + int  rcent, ccent;
281 + double  m;
282 + {
283 +        double  dy, dx;
284 +        COLOR  pval, ctmp;
285 +        int  ksiz, r;
286 +        double  pc, pr, norm;
287 +        register int  i, c;
288 +        register COLOR  *scan;
289 +
290 +        copycolor(pval, scanin[py%barsize][px]);
291 +        pc = x_c*(px+.5);
292 +        pr = y_r*(py+.5);
293 +        ksiz = CHECKRAD*m*rad + 1;
294 +        if (ksiz > orad) ksiz = orad;
295 +                                                /* compute normalization */
296 +        norm = 0.0;
297 +        i = 0;
298 +        for (r = rcent-ksiz; r <= rcent+ksiz; r++) {
299 +                if (r < 0) continue;
300 +                if (r >= nrows) break;
301 +                dy = (pr - (r+.5))/(m*rad);
302 +                for (c = ccent-ksiz; c <= ccent+ksiz; c++) {
303 +                        if (c < 0) continue;
304 +                        if (c >= ncols) break;
305 +                        dx = (pc - (c+.5))/(m*rad);
306 +                        norm += warr[i++] = lookgauss(dx*dx + dy*dy);
307 +                }
308 +        }
309 +        norm = 1.0/norm;
310 +        if (x_c < 1.0) norm *= x_c;
311 +        if (y_r < 1.0) norm *= y_r;
312 +                                                /* sum pixels */
313 +        i = 0;
314 +        for (r = rcent-ksiz; r <= rcent+ksiz; r++) {
315 +                if (r < 0) continue;
316 +                if (r >= nrows) break;
317 +                scan = scoutbar[r%obarsize];
318 +                for (c = ccent-ksiz; c <= ccent+ksiz; c++) {
319 +                        if (c < 0) continue;
320 +                        if (c >= ncols) break;
321 +                        copycolor(ctmp, pval);
322 +                        dx = norm*warr[i++];
323 +                        scalecolor(ctmp, dx);
324 +                        addcolor(scan[c], ctmp);
325 +                }
326 +        }
327   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines