home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_08 / 9n08056b < prev    next >
Text File  |  1991-06-20  |  2KB  |  68 lines

  1. /***************** Listing 4 *************************
  2.  
  3.             Normalized Distance Interpolation
  4.  
  5.    double interp(image, x, y)
  6.      float *image, x, y;
  7.  
  8.    image: pointer to the four values of grid i.e.
  9.           a 2 x 2 array ( image[2][2] )
  10.    x    : sample coordinate
  11.    y    : line coordinate
  12.  
  13.           wts[0]             wts[1]  
  14.           image(0,0)         image(0,1)
  15.             *-------------------*
  16.             |                   |
  17.             |                   |
  18.             |                   |
  19.             |             o     |
  20.             |           (y,x)   |
  21.             |                   |
  22.             *-------------------*
  23.          image(1,0)          image(1,1)
  24.          wts[2]              wts[3]
  25.  
  26.     If point "o" falls on upper left corner, then 
  27.     return image(0,0). The point should never fall on 
  28.     any of the other corners because the calling 
  29.     program will ensure against this according to the
  30.     filling sequence of the array image[2][2].
  31.     The array wts[] is the normalized distance from 
  32.     point (y,x) to the four corners of a grid.
  33.  
  34. ******************************************************
  35. #include <stdio.h>
  36.  
  37. #define MAX_PTS               4
  38. #define PYTHAGOREAN_SQ(a,b)   ( (a)*(a) + (b)*(b) )
  39.  
  40. double interp(image, x, y)
  41.  float *image, x, y;
  42. {
  43.    register i;
  44.    float p, q;
  45.    double wts[MAX_PTS];
  46.    double sum_inv_wts = 0.0;
  47.    double sum_I = 0.0;
  48.  
  49.    p = x - (int) x;
  50.    q = y - (int) y;
  51.  
  52.    if( (p == 0.0) && (q == 0.0) )
  53.      return( (double) *image );   /* upper left */
  54.  
  55.    wts[0] = PYTHAGOREAN_SQ( p, q );
  56.    wts[1] = PYTHAGOREAN_SQ( 1-p, q );
  57.    wts[2] = PYTHAGOREAN_SQ( p, 1-q );
  58.    wts[3] = PYTHAGOREAN_SQ( 1-p, 1-q );
  59.  
  60.    for(i=0; i < MAX_PTS; i++)
  61.     {
  62.        sum_inv_wts += 1 / wts[i] ;
  63.        sum_I += *image++ / wts[i] ;
  64.     }
  65.  
  66.    return( sum_I / sum_inv_wts );
  67. }
  68.