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

  1.        /***********************************************
  2.        *
  3.        *       file d:\cips\addsub.c
  4.        *
  5.        *       Functions: This file contains
  6.        *          add_image_array
  7.        *          subtract_image_array
  8.        *
  9.        *       Purpose:
  10.        *          These functions implement
  11.        *          image addition and subtraction.
  12.        *
  13.        *       External Calls:
  14.        *          wtiff.c - does_not_exist
  15.        *                    round_off_image_size
  16.        *                    create_allocate_tiff_file
  17.        *                    write_array_into_tiff_image
  18.        *          tiff.c - read_tiff_header
  19.        *          rtiff.c - read_tiff_image
  20.        *
  21.        *       Modifications:
  22.        *          1 April 1992 - created
  23.        *
  24.        *************************************************/
  25.  
  26. #include "d:\cips\cips.h"
  27.  
  28.      /*******************************************
  29.      *
  30.      *   add_image_array(...
  31.      *
  32.      *   This function adds two ROWSxCOLS image
  33.      *   sections.  The image file named out_name
  34.      *   will receive the sum of the image file
  35.      *   named in1_name and the image file
  36.      *   named in2_name.
  37.      *
  38.      *******************************************/
  39.  
  40.  
  41. add_image_array(in1_name, in2_name, out_name, the_image, out_image,
  42.           il1, ie1, ll1, le1,
  43.           il2, ie2, ll2, le2,
  44.           il3, ie3, ll3, le3)
  45.    char   in1_name[], in2_name[], out_name[];
  46.    int    il1, ie1, ll1, le1,
  47.           il2, ie2, ll2, le2,
  48.           il3, ie3, ll3, le3;
  49.    short  the_image[ROWS][COLS],
  50.           out_image[ROWS][COLS];
  51.  
  52. {
  53.    int    i, j, length, max, width;
  54.    struct tiff_header_struct image_header;
  55.  
  56.  
  57.    if(does_not_exist(out_name)){
  58.       printf("\n\n output file does not exist %s", out_name);
  59.       read_tiff_header(in1_name, &image_header);
  60.       round_off_image_size(&image_header,
  61.                            &length, &width);
  62.       image_header.image_length = length*ROWS;
  63.       image_header.image_width  = width*COLS;
  64.       create_allocate_tiff_file(out_name, &image_header,
  65.                                 out_image);
  66.    }  /* ends if does_not_exist */
  67.  
  68.    read_tiff_header(in1_name, &image_header);
  69.  
  70.    max = 255;
  71.    if(image_header.bits_per_pixel == 4)
  72.       max = 16;
  73.  
  74.    read_tiff_image(in1_name, the_image, il1, ie1, ll1, le1);
  75.    read_tiff_image(in2_name, out_image, il2, ie2, ll2, le2);
  76.  
  77.    for(i=0; i<ROWS; i++){
  78.       for(j=0; j<COLS; j++){
  79.          out_image[i][j] = the_image[i][j] + out_image[i][j];
  80.          if(out_image[i][j] > max)
  81.             out_image[i][j] = max;
  82.       }  /* ends loop over j */
  83.    }  /* ends loop over i */
  84.  
  85.    write_array_into_tiff_image(out_name, out_image,
  86.                                il3, ie3, ll3, le3);
  87.  
  88. }  /* ends add_image_array */
  89.  
  90.  
  91.  
  92.  
  93.      /*******************************************
  94.      *
  95.      *   subtract_image_array(...
  96.      *
  97.      *   This function subtracts two ROWSxCOLS image
  98.      *   sections.  The image file named out_name
  99.      *   will receive the difference of the image file
  100.      *   named in1_name and the image file
  101.      *   named in2_name.
  102.      *
  103.      *   out_name = in1_name - in2_name
  104.      *
  105.      *******************************************/
  106.  
  107.  
  108. subtract_image_array(in1_name, in2_name, out_name, the_image, out_image,
  109.           il1, ie1, ll1, le1,
  110.           il2, ie2, ll2, le2,
  111.           il3, ie3, ll3, le3)
  112.    char   in1_name[], in2_name[], out_name[];
  113.    int    il1, ie1, ll1, le1,
  114.           il2, ie2, ll2, le2,
  115.           il3, ie3, ll3, le3;
  116.    short  the_image[ROWS][COLS],
  117.           out_image[ROWS][COLS];
  118.  
  119. {
  120.    int    i, j, length, width;
  121.    struct tiff_header_struct image_header;
  122.  
  123.  
  124.    if(does_not_exist(out_name)){
  125.       printf("\n\n output file does not exist %s", out_name);
  126.       read_tiff_header(in1_name, &image_header);
  127.       round_off_image_size(&image_header,
  128.                            &length, &width);
  129.       image_header.image_length = length*ROWS;
  130.       image_header.image_width  = width*COLS;
  131.       create_allocate_tiff_file(out_name, &image_header,
  132.                                 out_image);
  133.    }  /* ends if does_not_exist */
  134.  
  135.    read_tiff_header(in1_name, &image_header);
  136.  
  137.    read_tiff_image(in1_name, the_image, il1, ie1, ll1, le1);
  138.    read_tiff_image(in2_name, out_image, il2, ie2, ll2, le2);
  139.  
  140.    for(i=0; i<ROWS; i++){
  141.       for(j=0; j<COLS; j++){
  142.          out_image[i][j] = the_image[i][j] - out_image[i][j];
  143.          if(out_image[i][j] < 0)
  144.             out_image[i][j] = 0;
  145.       }  /* ends loop over j */
  146.    }  /* ends loop over i */
  147.  
  148.    write_array_into_tiff_image(out_name, out_image,
  149.                                il3, ie3, ll3, le3);
  150.  
  151. }  /* ends subtract_image_array */
  152.