-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathppmdiff.c
More file actions
99 lines (83 loc) · 2.61 KB
/
Copy pathppmdiff.c
File metadata and controls
99 lines (83 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "assert.h"
#include "compress40.h"
#include <math.h>
#include <pnm.h>
#include <a2methods.h>
#include "a2plain.h"
#include "a2blocked.h"
float square(int a){
return a *a;
}
float min(float a , float b){
if (a >= b){
return b;
}
else{
return a;
}
}
int main(int argc, char *argv[]){
A2Methods_T methods = uarray2_methods_plain;
assert(methods);
FILE *file;
if (argc != 3){
fprintf(stderr, "Incorrect number of arguments, please enter 2 ppm files\n");
exit(EXIT_FAILURE);
}
Pnm_ppm img2;
Pnm_ppm img1;
Pnm_ppm temp;
int check_dash = 0;
for (int i = 1; i < argc; i++){
char *ppmfile;
if(argv[i][0] == '-' && check_dash == 0){
file = stdin;
temp = Pnm_ppmread(file, methods);
check_dash = 1;
}
else{
ppmfile = argv[i];
file = fopen(ppmfile, "r");
temp = Pnm_ppmread(file, methods);
}
if (i == 1 ){
img1 = temp;
}
else if(i == 2){
img2 = temp;
}
}
int height1 = img1->height;
int width1 = img1->width;
int height2 = img2->height;
int width2 = img2->width;
if (abs(height1- height2) > 1 || abs(width1- width2) >1 ){
fprintf(stderr, "height or width differs by more than 1 pixel\n");
printf("Height difference: %d, Width difference: %d\n", abs(height1- height2), abs(width1- width2));
exit(1);
}
double nominator = 0;
unsigned denom = img2->denominator;
for (int i = 0; i < min(width1, width2); i++) {
for (int j = 0; j < min(height1, height2); j++) {
Pnm_rgb rgb1 = img1->methods->at(img1->pixels, i, j);
Pnm_rgb rgb2 = img2->methods->at(img2->pixels, i, j);
double red1 = (rgb1->red) / denom,
red2 = (rgb2->red) / denom,
green1 = (rgb1->green) / denom,
green2 = (rgb2->green) / denom,
blue1 = (rgb1->blue) / denom,
blue2 = (rgb2->blue) / denom;
nominator += square(red1 - red2) + square(green1 - green2) + square(blue1- blue2);
}
}
double denominator = 3*min(width1, width2)*min(height1, height2);
double root_mean_sq_diff = sqrt(nominator/denominator);
printf("Root mean square difference: %0.4f\n", root_mean_sq_diff);
Pnm_ppmfree(&img1);
Pnm_ppmfree(&img2);
fclose(file);
}