Debian Codegraph @

src/SPERR/utilities/raw_tools/compare_raw.cpp

raw
1 #include <sys/stat.h>
2 #include <algorithm>
3 #include <cmath>
4 #include <cstdio>
5 #include <cstdlib>
6 #include <memory>
7 #include <numeric>
8 #include <vector>
9
10 using FLOAT = double;
11
12 int sam_read_n_bytes(const char* filename,
13 size_t n_bytes, /* input */
14 void* buffer) /* output */
15 {
16 FILE* f = fopen(filename, "r");
17 if (f == NULL) {
18 fprintf(stderr, "Error! Cannot open input file: %s\n", filename);
19 return 1;
20 }
21 fseek(f, 0, SEEK_END);
22 if (ftell(f) < n_bytes) {
23 fprintf(stderr, "Error! Input file size error: %s\n", filename);
24 fprintf(stderr, " Expecting %lu bytes, got %ld bytes.\n", n_bytes, ftell(f));
25 fclose(f);
26 return 1;
27 }
28 fseek(f, 0, SEEK_SET);
29
30 if (fread(buffer, 1, n_bytes, f) != n_bytes) {
31 fprintf(stderr, "Error! Input file read error: %s\n", filename);
32 fclose(f);
33 return 1;
34 }
35
36 fclose(f);
37 return 0;
38 }
39
40 template <typename T>
41 void calc_stats(const T* arr1,
42 const T* arr2,
43 size_t len,
44 T& rmse,
45 T& linfty,
46 T& psnr,
47 T& arr1min,
48 T& arr1max)
49 {
50 //
51 // Calculate min and max of arr1
52 //
53 const auto minmax = std::minmax_element(arr1, arr1 + len);
54 arr1min = *minmax.first;
55 arr1max = *minmax.second;
56
57 //
58 // In rare cases, the two input arrays are identical.
59 //
60 auto mism = std::mismatch(arr1, arr1 + len, arr2, arr2 + len);
61 if (mism.first == arr1 + len && mism.second == arr2 + len) {
62 rmse = 0.0;
63 linfty = 0.0;
64 psnr = std::numeric_limits<T>::infinity();
65 return;
66 }
67
68 const size_t stride_size = 4096;
69 const size_t num_of_strides = len / stride_size;
70 const size_t remainder_size = len - stride_size * num_of_strides;
71
72 auto sum_vec = std::vector<T>(num_of_strides + 1);
73 auto linfty_vec = std::vector<T>(num_of_strides + 1);
74
75 //
76 // Calculate summation and l-infty of each stride
77 //
78 for (size_t stride_i = 0; stride_i < num_of_strides; stride_i++) {
79 T maxerr = 0.0;
80 auto buf = std::array<T, stride_size>();
81 for (size_t i = 0; i < stride_size; i++) {
82 const size_t idx = stride_i * stride_size + i;
83 auto diff = std::abs(arr1[idx] - arr2[idx]);
84 maxerr = std::max(maxerr, diff);
85 buf[i] = diff * diff;
86 }
87 sum_vec[stride_i] = std::accumulate(buf.begin(), buf.end(), T{0.0});
88 linfty_vec[stride_i] = maxerr;
89 }
90
91 //
92 // Calculate summation and l-infty of the remaining elements
93 //
94 T last_linfty = 0.0;
95 auto last_buf = std::array<T, stride_size>{}; // must be enough for
96 // `remainder_size` elements.
97 for (size_t i = 0; i < remainder_size; i++) {
98 const size_t idx = stride_size * num_of_strides + i;
99 auto diff = std::abs(arr1[idx] - arr2[idx]);
100 last_linfty = std::max(last_linfty, diff);
101 last_buf[i] = diff * diff;
102 }
103 sum_vec[num_of_strides] =
104 std::accumulate(last_buf.begin(), last_buf.begin() + remainder_size, T{0.0});
105 linfty_vec[num_of_strides] = last_linfty;
106
107 //
108 // Now calculate linfty
109 //
110 linfty = *(std::max_element(linfty_vec.begin(), linfty_vec.end()));
111
112 //
113 // Now calculate rmse and psnr
114 // Note: psnr is calculated in dB, and follows the equation described in:
115 // http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/VELDHUIZEN/node18.html
116 // Also refer to https://www.mathworks.com/help/vision/ref/psnr.html
117 //
118 const auto msr = std::accumulate(sum_vec.begin(), sum_vec.end(), T{0.0}) / T(len);
119 rmse = std::sqrt(msr);
120 auto range_sq = *minmax.first - *minmax.second;
121 range_sq *= range_sq;
122 psnr = std::log10(range_sq / msr) * 10.0;
123 }
124
125 int main(int argc, char* argv[])
126 {
127 if (argc != 3) {
128 printf("Usage: ./a.out file1 file2\n");
129 return 1;
130 }
131
132 const char* file1 = argv[1];
133 const char* file2 = argv[2];
134
135 struct stat st1, st2;
136 stat(file1, &st1);
137 stat(file2, &st2);
138 if (st1.st_size != st2.st_size) {
139 printf("Two files have different sizes!\n");
140 return 1;
141 }
142
143 auto n_bytes = st1.st_size;
144 auto n_vals = n_bytes / sizeof(FLOAT);
145
146 auto buf1 = std::make_unique<FLOAT[]>(n_vals);
147 auto buf2 = std::make_unique<FLOAT[]>(n_vals);
148
149 sam_read_n_bytes(file1, n_bytes, buf1.get());
150 sam_read_n_bytes(file2, n_bytes, buf2.get());
151
152 FLOAT rmse, lmax, psnr, arr1min, arr1max;
153 calc_stats(buf1.get(), buf2.get(), n_vals, rmse, lmax, psnr, arr1min, arr1max);
154 printf("rmse = %e, lmax = %e, psnr = %fdB, orig_min = %f, orig_max = %f\n", rmse, lmax, psnr,
155 arr1min, arr1max);
156 }