/* Program to calculate reconstruction error in binary images. That is, what 
	* number and percentage of pixels are different betwen two images.
 *
 * Jerod Weinman
 * 9 June 2008
 */

#include "pbmio.h"
#include <stdio.h>

/* Calculate the total number of pixels with diffeing values between two images */
int calcError(const struct pbm_t *img, const struct pbm_t *restImg)
{

     if (img->rows != restImg->rows || img->cols != restImg->cols )
         
     {
          fprintf(stderr,
                  "Image buffers must be the same size to calculate error");
          return -1;
     }
     
     int err = 0;

     int i,j;

     double ener = 0;
     for (i=0 ; i < img->rows ; i++)
          for (j=0 ; j < img->cols ; j++)
               err += (restImg->bits[i][j] != img->bits[i][j]);

     return err;

}

int main(int argc, char* argv[])
{

     char *origFile, *cleanFile;
     int error;

     struct pbm_t origImg, cleanImg;


     /* Process command line arguments */
     if (argc<3)
     {
          fprintf(stderr,"Usage: %s original clean", argv[0]);
          return -1;
     }
     
     origFile = argv[1];
     cleanFile = argv[2];

     if (pbmread(origFile, &origImg)<0)
          exit (-1);

     if (pbmread(cleanFile, &cleanImg)<0)
          exit (-1);

     error = calcError(&origImg, &cleanImg);

     printf("Error: %d pixels, %2.2f%%\n",error, 
            100*(double)error/((double)(origImg.rows * origImg.cols)));
}
