home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Raytrace & Morphing / SOS-RAYTRACE.ISO / programm / source / radsrc22 / src / px / pf3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-02  |  2.8 KB  |  127 lines

  1. /* Copyright (c) 1992 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)pf3.c 2.3 10/2/92 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  pf3.c - routines for gaussian and box filtering
  9.  *
  10.  *     8/13/86
  11.  */
  12.  
  13. #include  <stdio.h>
  14.  
  15. #include  <math.h>
  16.  
  17. #include  "color.h"
  18.  
  19. #define     FTINY        1e-7
  20.  
  21. extern double  rad;        /* output pixel radius for filtering */
  22.  
  23. extern int  nrows;        /* number of rows for output */
  24. extern int  ncols;        /* number of columns for output */
  25.  
  26. extern int  boxfilt;        /* do box filtering? */
  27.  
  28. extern int  xres, yres;        /* resolution of input */
  29.  
  30. extern double  x_c, y_r;    /* conversion factors */
  31.  
  32. extern int  xrad;        /* x window size */
  33. extern int  yrad;        /* y window size */
  34.  
  35. extern int  barsize;        /* size of input scan bar */
  36. extern COLOR  **scanin;        /* input scan bar */
  37. extern COLOR  *scanout;        /* output scan line */
  38.  
  39. extern char  *progname;
  40.  
  41. float  *exptable;        /* exponent table */
  42.  
  43. #define     lookexp(x)        exptable[(int)(-10.*(x)+.5)]
  44.  
  45.  
  46. initmask()            /* initialize gaussian lookup table */
  47. {
  48.     extern char  *malloc();
  49.     register int  x;
  50.  
  51.     exptable = (float *)malloc(100*sizeof(float));
  52.     if (exptable == NULL) {
  53.         fprintf(stderr, "%s: out of memory in initmask\n", progname);
  54.         quit(1);
  55.     }
  56.     for (x = 0; x < 100; x++)
  57.         exptable[x] = exp(-x*0.1);
  58. }
  59.  
  60.  
  61. dobox(csum, xcent, ycent, c, r)            /* simple box filter */
  62. COLOR  csum;
  63. int  xcent, ycent;
  64. int  c, r;
  65. {
  66.     static int  wsum;
  67.     static double  d;
  68.     static int  y;
  69.     register int  x;
  70.     register COLOR    *scan;
  71.  
  72.     wsum = 0;
  73.     setcolor(csum, 0.0, 0.0, 0.0);
  74.     for (y = ycent+1-yrad; y <= ycent+yrad; y++) {
  75.         if (y < 0 || y >= yres)
  76.             continue;
  77.         d = y_r < 1.0 ? y_r*y - r : y - ycent;
  78.         if (d > 0.5+FTINY || d < -0.5-FTINY)
  79.             continue;
  80.         scan = scanin[y%barsize];
  81.         for (x = xcent+1-xrad; x <= xcent+xrad; x++) {
  82.             if (x < 0 || x >= xres)
  83.                 continue;
  84.             d = x_c < 1.0 ? x_c*x - c : x - xcent;
  85.             if (d > 0.5+FTINY || d < -0.5-FTINY)
  86.                 continue;
  87.             wsum++;
  88.             addcolor(csum, scan[x]);
  89.         }
  90.     }
  91.     if (wsum > 1)
  92.         scalecolor(csum, 1.0/wsum);
  93. }
  94.  
  95.  
  96. dogauss(csum, xcent, ycent, c, r)        /* gaussian filter */
  97. COLOR  csum;
  98. int  xcent, ycent;
  99. int  c, r;
  100. {
  101.     static double  dy, dx, weight, wsum;
  102.     static COLOR  ctmp;
  103.     static int  y;
  104.     register int  x;
  105.     register COLOR    *scan;
  106.  
  107.     wsum = FTINY;
  108.     setcolor(csum, 0.0, 0.0, 0.0);
  109.     for (y = ycent-yrad; y <= ycent+yrad; y++) {
  110.         if (y < 0 || y >= yres)
  111.             continue;
  112.         dy = (y_r*(y+.5) - (r+.5))/rad;
  113.         scan = scanin[y%barsize];
  114.         for (x = xcent-xrad; x <= xcent+xrad; x++) {
  115.             if (x < 0 || x >= xres)
  116.                 continue;
  117.             dx = (x_c*(x+.5) - (c+.5))/rad;
  118.             weight = lookexp(-(dx*dx + dy*dy));
  119.             wsum += weight;
  120.             copycolor(ctmp, scan[x]);
  121.             scalecolor(ctmp, weight);
  122.             addcolor(csum, ctmp);
  123.         }
  124.     }
  125.     scalecolor(csum, 1.0/wsum);
  126. }
  127.