home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_11 / 1011093a < prev    next >
Text File  |  1992-09-02  |  6KB  |  197 lines

  1.        /***********************************************
  2.        *
  3.        *       file d:\cips\rotate.c
  4.        *
  5.        *       Functions: This file contains
  6.        *          rotate_flip_image_array
  7.        *
  8.        *       Purpose:
  9.        *          This function rotates or flips an image
  10.        *          array in one of five ways.
  11.        *
  12.        *       External Calls:
  13.        *          wtiff.c - does_not_exist
  14.        *                    round_off_image_size
  15.        *                    create_allocate_tiff_file
  16.        *                    write_array_into_tiff_image
  17.        *          tiff.c - read_tiff_header
  18.        *          rtiff.c - read_tiff_image
  19.        *
  20.        *       Modifications:
  21.        *          1 April 1992 - created
  22.        *
  23.        *************************************************/
  24.  
  25. #include "d:\cips\cips.h"
  26.  
  27.      /*******************************************
  28.      *
  29.      *   rotate_flip_image_array(...
  30.      *
  31.      *   This function rotates an image array
  32.      *   in one of three ways or flips an image
  33.      *   array either vertically or horizontally.
  34.      *   The rotation_type parameter specifies
  35.      *   the operation.  When rotation_type is 
  36.      *   1, 2, or 3 you rotate.  When it is
  37.      *   4 or 5 you flip.
  38.      *
  39.      *   I define rotation as this:  Pin down the
  40.      *   lower left hand corner of the image array
  41.      *   and rotate the image 90 degrees clockwise.
  42.      *   1 rotation is 90 degrees, 2 rotations are
  43.      *   180 degrees, and 3 rotations are 270 degrees.
  44.      *   4 rotations bring you back to where you
  45.      *   started.
  46.      *
  47.      *   The in_file is the source image with
  48.      *   parameters given by il1, ie1, ll1, le1.
  49.      *
  50.      *   The out_file is the destination image with
  51.      *   parameters given by il2, ie2, ll2, le2.
  52.      *
  53.      *******************************************/
  54.  
  55.  
  56. rotate_flip_image_array(in_name, out_name, the_image,
  57.                    out_image, il1, ie1, ll1, le1,
  58.                    il2, ie2, ll2, le2, rotation_type)
  59.    char  in_name[], out_name[];
  60.    int   il1, ie1, ll1, le1,
  61.          il2, ie2, ll2, le2, rotation_type;
  62.    short the_image[ROWS][COLS],
  63.          out_image[ROWS][COLS];
  64. {
  65.    int    cd2, i, j, length, rd2, type, width;
  66.    struct tiff_header_struct image_header;
  67.  
  68.    if(does_not_exist(out_name)){
  69.       printf("\n\n output file does not exist %s", out_name);
  70.       read_tiff_header(in_name, &image_header);
  71.       round_off_image_size(&image_header,
  72.                            &length, &width);
  73.       image_header.image_length = length*ROWS;
  74.       image_header.image_width  = width*COLS;
  75.       create_allocate_tiff_file(out_name, &image_header,
  76.                                 out_image);
  77.    }  /* ends if does_not_exist */
  78.  
  79.      /*******************************************
  80.      *
  81.      *   Check the rotation_type.  If it is not
  82.      *   a valid value, set it to 1.
  83.      *
  84.      *******************************************/
  85.  
  86.    type = rotation_type;
  87.    if(type != 1  &&
  88.       type != 2  &&
  89.       type != 3  &&
  90.       type != 4  &&
  91.       type != 5) type = 1;
  92.  
  93.    read_tiff_image(in_name, the_image, il1, ie1, ll1, le1);
  94.  
  95.      /*******************************************
  96.      *
  97.      *   Rotate the image array as desired.
  98.      *
  99.      *******************************************/
  100.  
  101.      /*******************************************
  102.      *
  103.      *   1 90 degree rotation
  104.      *
  105.      *******************************************/
  106.  
  107.    if(type == 1  ||  type == 2  ||  type == 3){
  108.       for(i=0; i<ROWS; i++){
  109.          for(j=0; j<COLS; j++)
  110.             out_image[j][COLS-1-i] = the_image[i][j];
  111.       }  /* ends loop over i */
  112.    }  /* ends if type == 1 or 2 or 3 */
  113.  
  114.  
  115.      /*******************************************
  116.      *
  117.      *   a second 90 degree rotation
  118.      *
  119.      *******************************************/
  120.  
  121.    if(type == 2  ||  type == 3){
  122.       for(i=0; i<ROWS; i++)
  123.          for(j=0; j<COLS; j++)
  124.             the_image[i][j] = out_image[i][j];
  125.       for(i=0; i<ROWS; i++){
  126.          for(j=0; j<COLS; j++)
  127.             out_image[j][COLS-1-i] = the_image[i][j];
  128.       }  /* ends loop over i */
  129.    }  /* ends if type == 2 or 3 */
  130.  
  131.  
  132.      /*******************************************
  133.      *
  134.      *   a third 90 degree rotation
  135.      *
  136.      *******************************************/
  137.    if(type == 3){
  138.       for(i=0; i<ROWS; i++)
  139.          for(j=0; j<COLS; j++)
  140.             the_image[i][j] = out_image[i][j];
  141.       for(i=0; i<ROWS; i++){
  142.          for(j=0; j<COLS; j++)
  143.             out_image[j][COLS-1-i] = the_image[i][j];
  144.       }  /* ends loop over i */
  145.    }  /* ends if type == 3 */
  146.  
  147.  
  148.      /*******************************************
  149.      *
  150.      *   Flip the image array horizontally
  151.      *   about the center vertical axis.
  152.      *
  153.      *******************************************/
  154.  
  155.    if(type == 4){
  156.       cd2 = COLS/2;
  157.       for(j=0; j<cd2; j++){
  158.          for(i=0; i<ROWS; i++){
  159.             out_image[i][COLS-1-j] = the_image[i][j];
  160.          }  /* ends loop over i */
  161.       }  /* ends loop over j */
  162.  
  163.       for(j=cd2; j<COLS; j++){
  164.          for(i=0; i<ROWS; i++){
  165.             out_image[i][COLS-1-j] = the_image[i][j];
  166.          }  /* ends loop over i */
  167.       }  /* ends loop over j */
  168.    }  /* ends if type == 4 */
  169.  
  170.  
  171.      /*******************************************
  172.      *
  173.      *   Flip the image array vertically
  174.      *   about the center horizontal axis.
  175.      *
  176.      *******************************************/
  177.  
  178.    if(type == 5){
  179.       rd2 = ROWS/2;
  180.       for(i=0; i<rd2; i++){
  181.          for(j=0; j<COLS; j++){
  182.             out_image[ROWS-1-i][j] = the_image[i][j];
  183.          }  /* ends loop over j */
  184.       }  /* ends loop over i */
  185.  
  186.       for(i=rd2; i<ROWS; i++){
  187.          for(j=0; j<COLS; j++){
  188.             out_image[ROWS-1-i][j] = the_image[i][j];
  189.          }  /* ends loop over j */
  190.       }  /* ends loop over i */
  191.    }  /* ends if type == 5 */
  192.  
  193.    write_array_into_tiff_image(out_name, out_image,
  194.                                il2, ie2, ll2, le2);
  195.  
  196. }  /* ends rotate_flip_image_array */
  197.