/* calculate the PSNR difference between two image files */ #include #include #include #include #include int main(int argc, char *argv[]) { /* declaring variables */ FILE *file1, *file2; register long i; long file_size; char *buf1, *buf2; struct stat stat1; struct stat stat2; double x, error, psnr; /* parse the command line */ if (argc!=3) { fprintf(stderr, "Invalid number of inputs!\n"); fprintf(stderr, "Usage: %s file1 file2\n", argv[0]); fprintf(stderr, "file1 and file2 have to be of same size.\n"); exit(1); } else { /* check the file size */ if ( (stat(argv[1], &stat1)) ) { fprintf(stderr, "Cannot open file %s for reading\n", argv[1]); exit(2); } if ( (stat(argv[2], &stat2)) ) { fprintf(stderr, "Cannot open file %s for reading\n", argv[2]); exit(2); } if ( stat1.st_size != stat2.st_size ) { fprintf(stderr, "file %s and file %s are not the same size.\n", argv[1], argv[2]); exit(2); } } /* allocating memory for the two files, of course we don't really need to do * that, but I am lazy... */ if ( (buf1=(char *)malloc(stat1.st_size*sizeof(char))) == NULL || (buf2=(char *)malloc(stat2.st_size*sizeof(char))) == NULL ) { fprintf(stderr, "Cannot allocate memory!\n"); exit(3); } /* open the two files */ file1 = fopen(argv[1], "rb"); file2 = fopen(argv[2], "rb"); /* read the two files */ fread(buf1, sizeof(char), stat1.st_size, file1); fread(buf2, sizeof(char), stat1.st_size, file2); fclose(file1); fclose(file2); /* calculating distortion */ error = 0.0; for (i=0;i