Listing 3 Corrected code used in the Sobel, Kirsch, and Prewitt directional edge detectors

    /***********************************************
    *
    *   perform_convolution (...
    *
    *   This function performs convolution
    *   between the input image and 8 3x3 masks.
    *   The result is placed in the out_image.
    *
    ***********************************************/

perform_convolution(image, out_image,
                 detect_type, threshold,
                 image_header, high)
   short image[ROWS][COLS],
        out_image [ROWS][COLS];
   int   detect_type, high, threshold;
   struct tiff_header_struct *image_header;
{
   int  a,
       b,
       i,
       is_present,
       j,
       sum;

   short  mask_0[3][3],
         mask_1[3][3],
         mask_213][3],
         mask_3[3][3],
         mask_4[3][3],
         mask_5[3][3],
         mask_6[3][3],
         mask_7[3][3],
         max,
         min,
         new_hi,
         new_low;

   setup_masks(detect_type, mask_0, mask_1,
             mask_2, mask_3, mask_4, mask_5,
             mask_6, mask_7);

   new_hi  = 250;
   new_low = 16;
   if (image_header->bits_per_pixel == 4){
       new_hi  = 10;
       new_low = 3;
   }

   min = 0;
   max = 225;
   if(image_header->bits_per_pixel == 4)
      max = 16;

     /* clear output image array */
   for(i-0; i<ROWS; i++)
      for(j=0; j<COLS; j++)
         out_image[i][j] = 0;

   printf("\n ");

   for(i=1; i<ROWS-1; i++){
      if( (i%10) == 0) printf("%3d", i);
      for(j=1; j<COLS-1; j++){

         /* Convolve for all 8 directions */

         /* 0 direction */

      sum = 0;
      for(a=-1; a<2; a++){
         for(b=-1; b<2; b++){
            sum = sum + image[i+a][j+b] *
                mask_0[a+1][b+1];
         }
      }
         if(sum > max) sum = max;
         if(sum < 0)   sum = 0;
            /* Correction 12-27-92 see file header for details. */
                                     /**/
                                    /**/
                                   /**/
      if(sum > out_image[i][j])         /**************/
         out_image[i][j] = sum;        /**************/
                                   /**/
                                    /**/
                                     /**/

         /* 1 direction */

      sum = 0;
      for(a=-1; a<2; a++){
         for(b=-1; b<2; b++) {
            sum = sum + image[i+a][j+b] * mask_1[a+1][b+1];
         }
      }
         if(sum > max) sum = max;
         if(sum < 0)   sum = 0;
            /* Correction 12-27-92 see file header for details. */
      if(sum > out_image[i][j])
         out_image[i][j]   = sum;

         /* 2 direction */

      sum = 0;
      for(a=-1; a<2; a++){
         for(b=-1; b<2; b++) {
            sum = sum + image[i+a][j+b] * mask_2[a+1][b+1];
         }
      }
         if(sum > max) sum = max;
         if(sum < 0)   sum = 0;
            /* Correction 12-27-92 see file header for details. */
      if(sum > out_image[i][j])
         out_image[i][j]   = sum;

         /* 3 direction */

      sum = 0;
      for(a=-1; a<2; a++){
         for(b=-1; b<2; b++){
            sum = sum + image[i+a][j+b] * mask_3[a+1][b+1];
         }
      }
         if(sum > max) sum = max;
         if(sum < 0)   sum = 0;
            /* Correction 12-27-92 see file header for details. */
      if(sum > out_image[i][j])
         out_image[i][j]   = sum;

         /* 4 direction */

      sum = 0;
      for(a=-1; a<2; a++){
         for(b=-1; b<2; b++){
            sum = sum + image[i+a][j+b] * mask_4[a+1][b+1];
         }
      }
         if(sum > max) sum = max;
         if(sum < 0)   sum = 0;
            /* Correction 12-27-92 see file header for details. */
      if(sum > out_image[i][j])
         out_image[i][j]   = sum;

         /* 5 direction */

      sum = 0;
      for(a=-1; a<2; a++){
         for(b=-1; b<2; b++){
            sum = sum + image[i+a][j+b] * mask_5[a+1][b+1];
         }
      }
         if(sum > max) sum = max;
         if(sum < 0)   sum = 0;
            /* Correction 12-27-92 see file header for details. */
      if(sum > out_image[i][j])
         out_image[i][j]   = sum;

         /* 6 direction */
      sum = 0;
      for(a=-1; a<2; a++){
         for(b=-1; b<2; b++){
            sum = sum + image[i+a][j+b] * mask_6[a+1][b+1];
         }
      }
         if(sum > max) sum = max;
         if(sum < 0)   sum = 0;
            /* Correction 12-27-92 see file header for details. */
      if(sum > out_image[i][j])
         out_image[i][j] = sum;

         /* 7 direction */

      sum = 0;
      for(a=-1; a<2; a++){
         for(b=-1; b<2; b++){
            sum = sum + image[i+a][j+b] * mask_7[a+1][b+1];
         }
      }
         if(sum > max) sum = max;
         if(sum < 0)   sum = 0;
            /* Correction 12-27-92 see file header for details. */
      if(sum > out_image[i][j])
         out_image[i][j]   = sum;

      }  /* ends loop over j */
   }  /* ends loop over i */

     /* if desired, threshold the output image */
   if(threshold == 1){
      for(i=0; i<ROWS; i++){
         for(j=0; j<COLS; j++){
            if(out_image[i][j] > high) {
                 out_image[i][j] = new_hi;
            }
            else{
                 out_image[i][j] = new_low;
            }
         }
      }

   }  /* ends if threshold == 1 */

}  /* ends perform_convolution */

/* End of File */