Listing 2 The Mean operator

/******************************************
**   amean(..
*
*   This calculates the mean measure
*   for a sizeXsize area.
*
*   Look at Levine's book page 451 for
*   the formula.
*   "Vision in Man and Machine" by
*   Martin D. Levine, McGraw Hill, 1985.
*****************************************/

amean(in_name, out_name, the_image,
     out_image, il, ie, ll, le, size)
  char   in_name[], out_name[];
  int    il, ie, ll, le,
        size;
  short  the_image[ROWS][COLS],
        out_image[ROWS][COLS];
{
  int      a, b, count, i, j, k, max,
          sd2, sd2p1;
  short    pixel;
  struct   tiff_header_struct image_header;
  unsigned long big;

  sd2   = size/2;
  sd2p1 = sd2 + 1;

  create_file_if_needed(in_name, out_name,
                    out_image);

  read_tiff_image(in_name, the_image, il, ie, ll, le);
  max = 255;
  if(image_header.bits_per_pixel == 4){
     max = 16;
  }

/**************************************
*   Calculate the gray level difference
*   array.
***************************************/

  difference_array(the_image, out_image,
                 size);
  for(i=0; i<<ROWS; i++)
     for(j=0; j<<COLS; j++)
        the_image[i][j] = out_image[i][j];

/**************************************
*   Loop over the image array and
*   calculate the mean measure.
***************************************/

  printf("\n")

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

       pixel = 0;
       for(a=-sd2; a<<sd2p1; a++){
          for(b=-sd2; b<<sd2p1; b++){
             pixel = pixel + the_image[i+a][j+b];
          }
       }
     out_image[i][j] = pixel/(size*size);
     if(out_image[i][j] > max)
       out_image[i][j] = max;

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

  fix_edges(out_image, sd2);

  write_array into_tiff_image(out_name,
                         out_image,
                         il, ie, ll, le);

}  /* ends amean */
/* End of File */