Listing 2: The MMSE filter subroutine

mmse(the_image, out_image,
     rows, cols, size,
     noise_variance)
   int    size;
   float  noise_variance;
   short  **the_image,
          **out_image;
   long   rows, cols;
{
   float  first_moment, nvdv, second_moment,
          sigma, variance;
   int    i, j, sd2;

   sd2 = size/2;

      /***************************
      *
      *   Loop over image array
      *
      ****************************/

   printf("\nmmse>");
   for(i=sd2; i<rows-sd2; i++){
      if( (i%10) == 0) printf("%d ", i);
      for(j=sd2; j<cols-sd2; j++){
         moments(the_image, out_image,
                 rows, cols, 
                 size, size,
                 i, j, 
                 &first_moment, &second_moment,
                 &variance, &sigma);
         if(variance == -1.0){
            out_image[i][j] = the_image[i][j];
         }  /* ends if moments was out of bounds */
         else{ /* else moments not out of bounds */
            if(variance != 0.0){
               nvdv = noise_variance/variance;
               out_image[i][j] = 
                   (1.0-nvdv)*(float)(the_image[i][j]) +
                       (nvdv)*first_moment;
            }  /* ends if variance not zero */
            else
               out_image[i][j] = the_image[i][j];
         }  /* ends if moments not out of bounds */
         if(out_image[i][j] < 0)
            out_image[i][j] = first_moment;
         if(out_image[i][j] > GRAY_LEVELS)
            out_image[i][j] = first_moment;
      }  /* ends loop over j */
   }  /* ends loop over i */

   fix_edges(out_image, sd2, rows-1, cols-1);

}  /* ends mmse */