home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_06 / 1106079a < prev    next >
Text File  |  1993-04-08  |  6KB  |  232 lines

  1.      /***********************************************
  2.      *
  3.      *   perform_convolution(...
  4.      *
  5.      *   This function performs convolution 
  6.      *   between the input image and 8 3x3 masks.
  7.      *   The result is placed in the out_image.
  8.      *
  9.      ************************************************/
  10.  
  11. perform_convolution(image, out_image,
  12.                     detect_type, threshold,
  13.                     image_header, high)
  14.    short image[ROWS][COLS],
  15.          out_image[ROWS][COLS];
  16.    int   detect_type, high, threshold;
  17.    struct tiff_header_struct *image_header;
  18. {
  19.  
  20.    int a,
  21.        b,
  22.        i,
  23.        is_present,
  24.        j,
  25.        sum;
  26.  
  27.    short  mask_0[3][3],
  28.           mask_1[3][3],
  29.           mask_2[3][3],
  30.           mask_3[3][3],
  31.           mask_4[3][3],
  32.           mask_5[3][3],
  33.           mask_6[3][3],
  34.           mask_7[3][3],
  35.           max,
  36.           min,
  37.           new_hi,
  38.           new_low;
  39.  
  40.  
  41.    setup_masks(detect_type, mask_0, mask_1,
  42.                mask_2, mask_3, mask_4, mask_5,
  43.                mask_6, mask_7);
  44.  
  45.    new_hi  = 250;
  46.    new_low = 16;
  47.    if(image_header->bits_per_pixel == 4){
  48.        new_hi  = 10;
  49.        new_low = 3;
  50.    }
  51.  
  52.    min = 0;
  53.    max = 255;
  54.    if(image_header->bits_per_pixel == 4)
  55.       max = 16;
  56.  
  57.      /* clear output image array */
  58.    for(i=0; i<ROWS; i++)
  59.       for(j=0; j<COLS; j++)
  60.          out_image[i][j] = 0;
  61.  
  62.    printf("\n ");
  63.  
  64.    for(i=1; i<ROWS-1; i++){
  65.       if( (i%10) == 0) printf("%3d", i);
  66.       for(j=1; j<COLS-1; j++){
  67.  
  68.  
  69.          /* Convolve for all 8 directions */
  70.  
  71.          /* 0 direction */
  72.  
  73.       sum = 0;
  74.       for(a=-1; a<2; a++){
  75.          for(b=-1; b<2; b++){
  76.             sum = sum + image[i+a][j+b] *
  77.                   mask_0[a+1][b+1];
  78.          }
  79.       }
  80.          if(sum > max) sum = max;
  81.          if(sum < 0)   sum = 0;
  82.             /* Correction 12-27-92
  83.                see file header for
  84.                details. */         
  85.                                         /**/
  86.                                        /**/
  87.                                       /**/
  88.       if(sum > out_image[i][j])      /**************/
  89.          out_image[i][j]   = sum;    /**************/
  90.                                       /**/
  91.                                        /**/
  92.                                         /**/
  93.  
  94.          /* 1 direction */
  95.  
  96.       sum = 0;
  97.       for(a=-1; a<2; a++){
  98.          for(b=-1; b<2; b++){
  99.             sum = sum + image[i+a][j+b] * mask_1[a+1][b+1];
  100.          }
  101.       }
  102.          if(sum > max) sum = max;
  103.          if(sum < 0)   sum = 0;
  104.             /* Correction 12-27-92
  105.                see file header for
  106.                details. */
  107.       if(sum > out_image[i][j])
  108.          out_image[i][j]   = sum;
  109.  
  110.  
  111.          /* 2 direction */
  112.  
  113.       sum = 0;
  114.       for(a=-1; a<2; a++){
  115.          for(b=-1; b<2; b++){
  116.             sum = sum + image[i+a][j+b] * mask_2[a+1][b+1];
  117.          }
  118.       }
  119.          if(sum > max) sum = max;
  120.          if(sum < 0)   sum = 0;
  121.             /* Correction 12-27-92
  122.                see file header for
  123.                details. */
  124.       if(sum > out_image[i][j])
  125.          out_image[i][j]   = sum;
  126.  
  127.  
  128.          /* 3 direction */
  129.  
  130.       sum = 0;
  131.       for(a=-1; a<2; a++){
  132.          for(b=-1; b<2; b++){
  133.             sum = sum + image[i+a][j+b] * mask_3[a+1][b+1];
  134.          }
  135.       }
  136.          if(sum > max) sum = max;
  137.          if(sum < 0)   sum = 0;
  138.             /* Correction 12-27-92
  139.                see file header for
  140.                details. */
  141.       if(sum > out_image[i][j])
  142.          out_image[i][j]   = sum;
  143.  
  144.  
  145.          /* 4 direction */
  146.  
  147.       sum = 0;
  148.       for(a=-1; a<2; a++){
  149.          for(b=-1; b<2; b++){
  150.             sum = sum + image[i+a][j+b] * mask_4[a+1][b+1];
  151.          }
  152.       }
  153.          if(sum > max) sum = max;
  154.          if(sum < 0)   sum = 0;
  155.             /* Correction 12-27-92
  156.                see file header for
  157.                details. */
  158.       if(sum > out_image[i][j])
  159.          out_image[i][j]   = sum;
  160.  
  161.  
  162.          /* 5 direction */
  163.  
  164.       sum = 0;
  165.       for(a=-1; a<2; a++){
  166.          for(b=-1; b<2; b++){
  167.             sum = sum + image[i+a][j+b] * mask_5[a+1][b+1];
  168.          }
  169.       }
  170.          if(sum > max) sum = max;
  171.          if(sum < 0)   sum = 0;
  172.             /* Correction 12-27-92
  173.                see file header for
  174.                details. */
  175.       if(sum > out_image[i][j])
  176.          out_image[i][j]   = sum;
  177.  
  178.  
  179.          /* 6 direction */
  180.       sum = 0;
  181.       for(a=-1; a<2; a++){
  182.          for(b=-1; b<2; b++){
  183.             sum = sum + image[i+a][j+b] * mask_6[a+1][b+1];
  184.          }
  185.       }
  186.          if(sum > max) sum = max;
  187.          if(sum < 0)   sum = 0;
  188.             /* Correction 12-27-92
  189.                see file header for
  190.                details. */
  191.       if(sum > out_image[i][j])
  192.          out_image[i][j]   = sum;
  193.  
  194.  
  195.          /* 7 direction */
  196.  
  197.       sum = 0;
  198.       for(a=-1; a<2; a++){
  199.          for(b=-1; b<2; b++){
  200.             sum = sum + image[i+a][j+b] * mask_7[a+1][b+1];
  201.          }
  202.       }
  203.          if(sum > max) sum = max;
  204.          if(sum < 0)   sum = 0;
  205.             /* Correction 12-27-92
  206.                see file header for
  207.                details. */
  208.       if(sum > out_image[i][j])
  209.          out_image[i][j]   = sum;
  210.  
  211.  
  212.       }  /* ends loop over j */
  213.    }  /* ends loop over i */
  214.  
  215.  
  216.      /* if desired, threshold the output image */
  217.    if(threshold == 1){
  218.        for(i=0; i<ROWS; i++){
  219.           for(j=0; j<COLS; j++){
  220.              if(out_image[i][j] > high){
  221.                   out_image[i][j] = new_hi;
  222.              }
  223.              else{
  224.                   out_image[i][j] = new_low;
  225.              }
  226.           }
  227.        }
  228.    }  /* ends if threshold == 1 */
  229.  
  230. }  /* ends perform_convolution */
  231.  
  232.