home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_08 / phillips / geomfunc.c < prev   
Encoding:
C/C++ Source or Header  |  1994-02-26  |  4.0 KB  |  150 lines

  1.  
  2. #include "cips.h"
  3.  
  4. #define FILL 150
  5.  
  6.      /*******************************************
  7.      *
  8.      *   geometry(..
  9.      *
  10.      *   This routine performs geometric
  11.      *   transformations on the pixels in an
  12.      *   image array.  It performs basic
  13.      *   displacement, stretching, and rotation.
  14.      *
  15.      *   The basic equations are:
  16.      *
  17.      *   new x = x.cos(a) + y.sin(a) + x_displace
  18.      *           + x.x_stretch +x.y.x_cross
  19.      *
  20.      *   new y = y.cos(a) - x.sin(a) + y_displace
  21.      *           + y.y_stretch +x.y.y_cross
  22.      *
  23.      *******************************************/
  24.  
  25. geometry(in_name, out_name, the_image, out_image,
  26.         il, ie, ll, le, x_angle,
  27.         x_stretch, y_stretch,
  28.         x_displace, y_displace,
  29.         x_cross, y_cross,
  30.         bilinear)
  31.  
  32.    char   in_name[], out_name[];
  33.    float  x_angle, x_stretch, y_stretch,
  34.           x_cross, y_cross;
  35.    int    bilinear, il, ie, ll, le;
  36.    short  the_image[ROWS][COLS],
  37.           out_image[ROWS][COLS],
  38.           x_displace, y_displace;
  39. {
  40.    double cosa, sina, radian_angle, tmpx, tmpy;
  41.    float  fi, fj, x_div, y_div, x_num, y_num;
  42.    int    i, j, new_i, new_j;
  43.    struct tiff_header_struct image_header;
  44.  
  45.    create_file_if_needed(in_name, out_name, out_image);
  46.  
  47.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  48.  
  49.       /******************************
  50.       *
  51.       *   Load the terms array with
  52.       *   the correct parameters.
  53.       *
  54.       *******************************/
  55.  
  56.       /* the following magic number is from
  57.          180 degrees divided by pi */
  58.    radian_angle = x_angle/57.29577951;
  59.    cosa  = cos(radian_angle);
  60.    sina  = sin(radian_angle);
  61.  
  62.       /************************************
  63.       *
  64.       *   NOTE: You divide by the
  65.       *   stretching factors. Therefore, if
  66.       *   they are zero, you divide by 1.
  67.       *   You do this with the x_div y_div
  68.       *   variables. You also need a
  69.       *   numerator term to create a zero
  70.       *   product.  You do this with the
  71.       *   x_num and y_num variables.
  72.       *
  73.       *************************************/
  74.  
  75.    if(x_stretch < 0.00001){
  76.       x_div = 1.0;
  77.       x_num = 0.0;
  78.    }
  79.    else{
  80.       x_div = x_stretch;
  81.       x_num = 1.0;
  82.    }
  83.  
  84.    if(y_stretch < 0.00001){
  85.       y_div = 1.0;
  86.       y_num = 0.0;
  87.    }
  88.    else{
  89.       y_div = y_stretch;
  90.       y_num = 1.0;
  91.    }
  92.  
  93.       /**************************
  94.       *
  95.       *   Loop over image array
  96.       *
  97.       **************************/
  98.  
  99.    printf("\n");
  100.    for(i=0; i<ROWS; i++){
  101.       if( (i%10) == 0) printf("%d ", i);
  102.       for(j=0; j<COLS; j++){
  103.  
  104.          fi = i;
  105.          fj = j;
  106.  
  107.          tmpx = (double)(j)*cosa         +
  108.                 (double)(i)*sina         +
  109.                 (double)(x_displace)     +
  110.                 (double)(x_num*fj/x_div) +
  111.                 (double)(x_cross*i*j);
  112.  
  113.          tmpy = (double)(i)*cosa         -
  114.                 (double)(j)*sina         +
  115.                 (double)(y_displace)     +
  116.                 (double)(y_num*fi/y_div) +
  117.                 (double)(y_cross*i*j);
  118.  
  119.          if(x_stretch != 0.0)
  120.             tmpx = tmpx - (double)(fj*cosa + fi*sina);
  121.          if(y_stretch != 0.0)
  122.             tmpy = tmpy - (double)(fi*cosa - fj*sina);
  123.  
  124.          new_j = tmpx;
  125.          new_i = tmpy;
  126.  
  127.          if(bilinear == 0){
  128.             if(new_j < 0       ||
  129.                new_j >= COLS   ||
  130.                new_i < 0       ||
  131.                new_i >= ROWS)
  132.                out_image[i][j] = FILL;
  133.             else
  134.                out_image[i][j] =
  135.                 the_image[new_i][new_j];
  136.          }  /* ends if bilinear */
  137.          else{
  138.             out_image[i][j] = 
  139.                bilinear_interpolate(the_image,
  140.                                     tmpx, tmpy);
  141.          }  /* ends bilinear if */
  142.  
  143.       }  /* ends loop over j */
  144.    }  /* ends loop over i */
  145.  
  146.    write_array_into_tiff_image(out_name, out_image,
  147.                                il, ie, ll, le);
  148.  
  149. }  /* ends geometry */
  150.