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 2.13 by greg, Thu Dec 12 14:24:00 1996 UTC vs.
Revision 2.21 by greg, Fri Dec 8 17:56:26 2023 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  pf3.c - routines for gaussian and box filtering
6   *
7   *     8/13/86
8   */
9  
10 < #include  "standard.h"
10 > #include  "pfilt.h"
11  
12 < #include  "color.h"
16 <
12 > #define  RSCA           1.13    /* square-radius multiplier: sqrt(4/PI) */
13   #define  TEPS           0.2     /* threshold proximity goal */
14   #define  REPS           0.1     /* radius proximity goal */
15  
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
29 extern int  xres, yres;         /* resolution of input */
30
31 extern double  x_c, y_r;        /* conversion factors */
32
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 int  wrapfilt;           /* wrap filter horizontally? */
47
48 extern char  *progname;
49
16   float  *gausstable;             /* gauss lookup table */
17  
18   float  *ringsum;                /* sum of ring values */
# Line 54 | Line 20 | short  *ringwt;                        /* weight (count) of ring values */
20   short  *ringndx;                /* ring index table */
21   float  *warr;                   /* array of pixel weights */
22  
23 < extern double  (*ourbright)();  /* brightness computation function */
23 > #define  lookgauss(x)           gausstable[(int)(20.*(x)+.5)]
24  
25 < double  pickfilt();
25 > static double pickfilt(double  p0);
26 > static void sumans(int  px, int  py, int  rcent, int  ccent, double  m);
27  
61 #define  lookgauss(x)           gausstable[(int)(10.*(x)+.5)]
28  
29 <
30 < initmask()                      /* initialize gaussian lookup table */
29 > void
30 > initmask(void)                  /* initialize gaussian lookup table */
31   {
32          int  gtabsiz;
33          double  gaussN;
34          double  d;
35 <        register int  x;
35 >        int  x;
36  
37 <        gtabsiz = 150*CHECKRAD;
37 >        gtabsiz = 444*CHECKRAD*CHECKRAD;
38          gausstable = (float *)malloc(gtabsiz*sizeof(float));
39          if (gausstable == NULL)
40                  goto memerr;
41          d = x_c*y_r*0.25/(rad*rad);
42 <        gausstable[0] = exp(-d)/sqrt(d);
42 >        gausstable[0] = exp(-d);
43          for (x = 1; x < gtabsiz; x++)
44 <                if ((gausstable[x] = exp(-x*0.1)/sqrt(x*0.1)) > gausstable[0])
44 >                if (x*0.05 <= d)
45                          gausstable[x] = gausstable[0];
46 +                else
47 +                        gausstable[x] = exp(-x*0.05);
48          if (obarsize == 0)
49                  return;
50                                          /* compute integral of filter */
51 <        gaussN = PI*sqrt(d)*exp(-d);            /* plateau */
52 <        for (d = sqrt(d)+0.05; d <= 1.25*CHECKRAD; d += 0.1)
53 <                gaussN += 0.1*2.0*PI*exp(-d*d);
51 >        gaussN = PI*d*exp(-d);                  /* plateau */
52 >        for (d = sqrt(d)+0.05; d <= RSCA*CHECKRAD; d += 0.1)
53 >                gaussN += 0.1*2.0*PI*d*exp(-d*d);
54                                          /* normalize filter */
55          gaussN = x_c*y_r/(rad*rad*gaussN);
56          for (x = 0; x < gtabsiz; x++)
# Line 99 | Line 67 | initmask()                     /* initialize gaussian lookup table */
67          ringsum = (float *)malloc((orad+1)*sizeof(float));
68          ringwt = (short *)malloc((orad+1)*sizeof(short));
69          warr = (float *)malloc(obarsize*obarsize*sizeof(float));
70 <        if (ringsum == NULL | ringwt == 0 | warr == NULL)
70 >        if ((ringsum == NULL) | (ringwt == 0) | (warr == NULL))
71                  goto memerr;
72          return;
73   memerr:
# Line 108 | Line 76 | memerr:
76   }
77  
78  
79 < dobox(csum, xcent, ycent, c, r)                 /* simple box filter */
80 < COLOR  csum;
81 < int  xcent, ycent;
82 < int  c, r;
79 > void
80 > dobox(                  /* simple box filter */
81 >        SCOLOR  csum,
82 >        int  xcent,
83 >        int  ycent,
84 >        int  c,
85 >        int  r
86 > )
87   {
88          int  wsum;
89          double  d;
90          int  y;
91 <        register int  x, offs;
92 <        register COLOR  *scan;
91 >        int  x, offs;
92 >        COLORV  *scan, *scp;
93          
94          wsum = 0;
95 <        setcolor(csum, 0.0, 0.0, 0.0);
95 >        scolorblack(csum);
96          for (y = ycent+1-ybrad; y <= ycent+ybrad; y++) {
97                  if (y < 0) continue;
98                  if (y >= yres) break;
# Line 136 | Line 108 | int  c, r;
108                          if (d < -0.5) continue;
109                          if (d >= 0.5) break;
110                          wsum++;
111 <                        addcolor(csum, scan[x+offs]);
111 >                        scp = scan + (x+offs)*NCSAMP;
112 >                        saddscolor(csum, scp);
113                  }
114          }
115 <        if (wsum > 1)
116 <                scalecolor(csum, 1.0/wsum);
115 >        if (wsum > 1) {
116 >                d = 1.0/wsum;
117 >                scalescolor(csum, d);
118 >        }
119   }
120  
121  
122 < dogauss(csum, xcent, ycent, c, r)               /* gaussian filter */
123 < COLOR  csum;
124 < int  xcent, ycent;
125 < int  c, r;
122 > void
123 > dogauss(                /* gaussian filter */
124 >        SCOLOR  csum,
125 >        int  xcent,
126 >        int  ycent,
127 >        int  c,
128 >        int  r
129 > )
130   {
131          double  dy, dx, weight, wsum;
132 <        COLOR  ctmp;
132 >        SCOLOR  ctmp;
133          int  y;
134 <        register int  x, offs;
135 <        register COLOR  *scan;
134 >        int  x, offs;
135 >        COLORV  *scan;
136  
137          wsum = FTINY;
138 <        setcolor(csum, 0.0, 0.0, 0.0);
138 >        scolorblack(csum);
139          for (y = ycent-yrad; y <= ycent+yrad; y++) {
140                  if (y < 0) continue;
141                  if (y >= yres) break;
# Line 169 | Line 148 | int  c, r;
148                          dx = (x_c*(x+.5) - (c+.5))/rad;
149                          weight = lookgauss(dx*dx + dy*dy);
150                          wsum += weight;
151 <                        copycolor(ctmp, scan[x+offs]);
152 <                        scalecolor(ctmp, weight);
153 <                        addcolor(csum, ctmp);
151 >                        copyscolor(ctmp, scan+(x+offs)*NCSAMP);
152 >                        scalescolor(ctmp, weight);
153 >                        saddscolor(csum, ctmp);
154                  }
155          }
156 <        scalecolor(csum, 1.0/wsum);
156 >        weight = 1.0/wsum;
157 >        scalescolor(csum, weight);
158   }
159  
160  
161 < dothresh(xcent, ycent, ccent, rcent)    /* gaussian threshold filter */
162 < int  xcent, ycent;
163 < int  ccent, rcent;
161 > void
162 > dothresh(       /* gaussian threshold filter */
163 >        int  xcent,
164 >        int  ycent,
165 >        int  ccent,
166 >        int  rcent
167 > )
168   {
169          double  d;
170          int  r, y, offs;
171 <        register int  c, x;
172 <        register float  *gscan;
171 >        int  c, x;
172 >        float  *gscan;
173                                          /* compute ring sums */
174 <        bzero((char *)ringsum, (orad+1)*sizeof(float));
175 <        bzero((char *)ringwt, (orad+1)*sizeof(short));
174 >        memset((char *)ringsum, '\0', (orad+1)*sizeof(float));
175 >        memset((char *)ringwt, '\0', (orad+1)*sizeof(short));
176          for (r = -orad; r <= orad; r++) {
177                  if (rcent+r < 0) continue;
178                  if (rcent+r >= nrows) break;
# Line 219 | Line 203 | int  ccent, rcent;
203                          if (d < -0.5) continue;
204                          if (d >= 0.5) break;
205                          sumans(x, y, rcent, ccent,
206 <                        pickfilt((*ourbright)(scanin[y%barsize][x+offs])));
206 >                        pickfilt((*ourbright)(scanin[y%barsize]+(x+offs)*NCSAMP)));
207                  }
208          }
209   }
210  
211  
212 < double
213 < pickfilt(p0)                    /* find filter multiplier for p0 */
214 < double  p0;
212 > static double
213 > pickfilt(                       /* find filter multiplier for p0 */
214 >        double  p0
215 > )
216   {
217          double  m = 1.0;
218          double  t, num, denom, avg, wsum;
219          double  mlimit[2];
220 <        int  ilimit = 4/TEPS;
221 <        register int  i;
220 >        int  ilimit = 4.0/TEPS;
221 >        int  i;
222                                  /* iterative search for m */
223          mlimit[0] = 1.0; mlimit[1] = orad/rad/CHECKRAD;
224          do {
225                                          /* compute grey weighted average */
226 <                i = 1.25*CHECKRAD*rad*m + .5;
226 >                i = RSCA*CHECKRAD*rad*m + .5;
227                  if (i > orad) i = orad;
228                  avg = wsum = 0.0;
229                  while (i--) {
# Line 285 | Line 270 | double  p0;
270   }
271  
272  
273 < sumans(px, py, rcent, ccent, m)         /* sum input pixel to output */
274 < int  px, py;
275 < int  rcent, ccent;
276 < double  m;
273 > static void
274 > sumans(         /* sum input pixel to output */
275 >        int  px,
276 >        int  py,
277 >        int  rcent,
278 >        int  ccent,
279 >        double  m
280 > )
281   {
282 <        double  dy, dx;
283 <        COLOR  pval, ctmp;
282 >        double  dy2, dx;
283 >        SCOLOR  pval, ctmp;
284          int  ksiz, r, offs;
285          double  pc, pr, norm;
286 <        register int  i, c;
287 <        register COLOR  *scan;
286 >        int  i, c;
287 >        COLORV  *scan, *scp;
288          /*
289           * This normalization method fails at the picture borders because
290           * a different number of input pixels contribute there.
291           */
292 <        scan = scanin[py%barsize] + (px < 0 ? xres : px >= xres ? -xres : 0);
293 <        copycolor(pval, scan[px]);
292 >        scan = scanin[py%barsize] + (px < 0 ? xres : px >= xres ? -xres : 0)*NCSAMP;
293 >        copyscolor(pval, scan+px*NCSAMP);
294          pc = x_c*(px+.5);
295          pr = y_r*(py+.5);
296          ksiz = CHECKRAD*m*rad + 1;
# Line 312 | Line 301 | double  m;
301          for (r = rcent-ksiz; r <= rcent+ksiz; r++) {
302                  if (r < 0) continue;
303                  if (r >= nrows) break;
304 <                dy = (pr - (r+.5))/(m*rad);
304 >                dy2 = (pr - (r+.5))/(m*rad);
305 >                dy2 *= dy2;
306                  for (c = ccent-ksiz; c <= ccent+ksiz; c++) {
307                          if (!wrapfilt) {
308                                  if (c < 0) continue;
309                                  if (c >= ncols) break;
310                          }
311                          dx = (pc - (c+.5))/(m*rad);
312 <                        norm += warr[i++] = lookgauss(dx*dx + dy*dy);
312 >                        norm += warr[i++] = lookgauss(dx*dx + dy2);
313                  }
314          }
315          norm = 1.0/norm;
# Line 333 | Line 323 | double  m;
323                  scan = scoutbar[r%obarsize];
324                  for (c = ccent-ksiz; c <= ccent+ksiz; c++) {
325                          offs = c < 0 ? ncols : c >= ncols ? -ncols : 0;
326 <                        if (offs && !wrapfilt)
326 >                        if ((offs != 0) & !wrapfilt)
327                                  continue;
328 <                        copycolor(ctmp, pval);
328 >                        copyscolor(ctmp, pval);
329                          dx = norm*warr[i++];
330 <                        scalecolor(ctmp, dx);
331 <                        addcolor(scan[c+offs], ctmp);
330 >                        scalescolor(ctmp, dx);
331 >                        scp = scan + (c+offs)*NCSAMP;
332 >                        saddscolor(scp, ctmp);
333                  }
334          }
335   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines