/
test_weighting.cpp
257 lines (197 loc) · 10.8 KB
/
test_weighting.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "catch.hpp" // If this fails, download https://github.com/catchorg/Catch2/releases/download/v1.11.0/catch.hpp
#include "helpers.h"
#include "test_weighting_helpers.h"
extern "C" void keep9() {}
TEST_CASE("Test contrib windows", "[fastscaling]")
{
char msg[256];
flow_c context;
flow_context_initialize(&context);
bool r = test_contrib_windows(&context, msg);
if (!r)
FAIL(msg);
REQUIRE(r);
REQUIRE(flow_context_begin_terminate(&context) == true);
flow_context_end_terminate(&context);
}
TEST_CASE("Test Weighting", "[fastscaling]")
{
char msg[256];
flow_c context;
flow_context_initialize(&context);
// These have window = 1, and shouldnt' have negative values. They should also end at 1
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_Hermite, msg, 0, 0, 0.99, 0.08, 1)
== nullptr);
// Also called a linear filter
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_Triangle, msg, 0, 0, 0.99, 0.08, 1)
== nullptr);
// Box should only return a value from -0.5..0.5
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_Box, msg, 0, 0, 0.51, 0.001, 0.51)
== nullptr);
// These should go negative between x=1 and x=2, but should end at x=2
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_CatmullRom, msg, 1, 2, 1, 0.08, 2)
== nullptr);
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_CubicFast, msg, 1, 2, 1, 0.08, 2)
== nullptr);
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_Cubic, msg, 0, 0, 2.0, 0.08, 2)
== nullptr);
// BSpline is a smoothing filter, always positive
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_CubicBSpline, msg, 0, 0, 1.75,
0.08, 2) == nullptr);
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_Mitchell, msg, 8.0 / 7.0, 2.0, 1,
0.08, 2.0) == nullptr);
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_Robidoux, msg, 1.1685777620836932,
2, 1, 0.08, 2) == nullptr);
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_RobidouxSharp, msg,
1.105822933719019, 2, 1, 0.08, 2) == nullptr);
// Sinc filters. These have second crossings.
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_RawLanczos2, msg, 1, 2, 1, 0.08, 2)
== nullptr);
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_RawLanczos2Sharp, msg, 0.954, 1.86,
1, 0.08, 2) == nullptr);
// These should be negative between x=1 and x=2, positive between 2 and 3, but should end at 3
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_RawLanczos3, msg, 1, 2, 1, 0.1, 3)
== nullptr);
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_RawLanczos3Sharp, msg, 0.98,
1.9625, 1, 0.1, 3) == nullptr);
///
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_Lanczos2, msg, 1, 2, 1, 0.08, 2)
== nullptr);
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_Lanczos2Sharp, msg, 0.954, 1.86, 1,
0.08, 2) == nullptr);
// These should be negative between x=1 and x=2, positive between 2 and 3, but should end at 3
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_Lanczos, msg, 1, 2, 1, 0.1, 3)
== nullptr);
CHECK(test_filter(&context, flow_interpolation_filter::flow_interpolation_filter_LanczosSharp, msg, 0.98, 1.9625, 1,
0.1, 2.943) == nullptr);
REQUIRE(flow_context_begin_terminate(&context) == true);
flow_context_end_terminate(&context);
}
TEST_CASE("Verify weights are symmetric and bounded", "[fastscaling]")
{
flow_c context;
flow_c * c = &context;
flow_context_initialize(&context);
// Loop through every filter type, and for each filter type, try a variety of scaling ratios.
// For each scaling ratio, export a row where columns contain the weights for the input pixels
// filter, 2, from, 200, to, 300, weights, src, 0, (0.00001, 0.00200, 1.2200), 1, ...
int32_t filter_id = 1;
int32_t scalings[] = { /*downscale to 1px*/ 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 17, 1,
/*upscale from 2px*/ 2, 3, 2, 4, 2, 5, 2, 17,
/*other*/ 11, 7, 7, 3, 8, 4 };
flow_interpolation_filter first_filter = flow_interpolation_filter_NCubicSharp;
flow_interpolation_filter last_filter = flow_interpolation_filter_NCubicSharp;
uint32_t scaling_ix;
for (filter_id = (int32_t)first_filter; filter_id <= (int32_t)last_filter; filter_id++) {
for (scaling_ix = 0; scaling_ix < sizeof(scalings) / sizeof(int32_t); scaling_ix += 2) {
int32_t from_width = scalings[scaling_ix];
int32_t to_width = scalings[scaling_ix + 1];
flow_interpolation_filter filter = (flow_interpolation_filter)filter_id;
struct flow_interpolation_details * details = flow_interpolation_details_create_from(&context, filter);
ERR(c);
struct flow_interpolation_line_contributions * lct
= flow_interpolation_line_contributions_create(&context, to_width, from_width, details);
CAPTURE(filter);
CAPTURE(from_width);
CAPTURE(to_width);
if (flow_context_has_error(c)) {
ERR(c);
}
for (uint32_t output_pixel = 0; output_pixel < lct->LineLength / 2; output_pixel++) {
uint32_t opposite_output_pixel = lct->LineLength - 1 - output_pixel;
CAPTURE(output_pixel);
CAPTURE(opposite_output_pixel);
struct flow_interpolation_pixel_contributions * current = &lct->ContribRow[output_pixel];
struct flow_interpolation_pixel_contributions * opposite = &lct->ContribRow[opposite_output_pixel];
// printf("%d[%d,%d] vs %d[%d,%d]\n", output_pixel, current->Left, current->Right,
// opposite_output_pixel, opposite->Left, opposite->Right);
CAPTURE(current->Left);
CAPTURE(current->Right);
CAPTURE(opposite->Left);
CAPTURE(opposite->Right);
REQUIRE(from_width - 1 - opposite->Right == current->Left); // "Outer bounds must be symmetrical."
REQUIRE(from_width - 1 - current->Right == opposite->Left); // "Outer bounds must be symmetrical."
for (int32_t ix = current->Left; ix <= current->Right; ix++) {
REQUIRE(fabs(current->Weights[ix - current->Left] - opposite->Weights[current->Right - ix])
< 0.00001);
REQUIRE(fabs(current->Weights[ix - current->Left]) < 5);
}
}
FLOW_destroy(c, lct);
}
}
REQUIRE(flow_context_begin_terminate(&context) == true);
flow_context_end_terminate(&context);
}
TEST_CASE("Test output weights", "[fastscaling]")
{
flow_c context;
flow_c * c = &context;
flow_context_initialize(&context);
char filename[2048];
if (!create_path_from_relative(&context, __FILE__, true, filename, 2048, "/visuals/weights.txt")) {
ERR(c);
}
FILE * output;
if ((output = fopen(filename, "w")) == NULL) {
ERR(c);
}
fprintf(output, "filter, from_width, to_width, weights");
// Loop through every filter type, and for each filter type, try a variety of scaling ratios.
// For each scaling ratio, export a row where columns contain the weights for the input pixels
// filter, 2, from, 200, to, 300, weights, src, 0, (0.00001, 0.00200, 1.2200), 1, ...
int32_t filter_id = 1;
int32_t scalings[] = { /*downscale to 1px*/ 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 17, 1,
/*upscale from 2px*/ 2, 3, 2, 4, 2, 5, 2, 17,
/*other*/ 11, 7, 7, 3,
/* IDCT kernel sizes */ 8, 8, 8, 7, 8, 6, 8, 5, 8, 4, 8, 3, 8, 2, 8, 1 };
flow_interpolation_filter last_filter = flow_interpolation_filter_NCubicSharp;
uint32_t scaling_ix;
for (filter_id = 1; filter_id <= (int32_t)last_filter; filter_id++) {
for (scaling_ix = 0; scaling_ix < sizeof(scalings) / sizeof(int32_t); scaling_ix += 2) {
int32_t from_width = scalings[scaling_ix];
int32_t to_width = scalings[scaling_ix + 1];
flow_interpolation_filter filter = (flow_interpolation_filter)filter_id;
struct flow_interpolation_details * details = flow_interpolation_details_create_from(&context, filter);
ERR(c);
struct flow_interpolation_line_contributions * lct
= flow_interpolation_line_contributions_create(&context, to_width, from_width, details);
if (flow_context_has_error(c)) {
CAPTURE(filter);
ERR(c);
}
fprintf(output, "\r\nfilter_%02d (%2dpx to %2dpx):", filter_id, from_width, to_width);
for (uint32_t output_pixel = 0; output_pixel < lct->LineLength; output_pixel++) {
struct flow_interpolation_pixel_contributions * current = &lct->ContribRow[output_pixel];
fprintf(output, " x=%i from ", output_pixel);
for (int32_t ix = current->Left; ix <= current->Right; ix++) {
float weight = current->Weights[ix - current->Left];
fprintf(output, (ix == current->Left) ? "(" : " ");
fprintf(output, "%.06f", weight);
}
fprintf(output, "),");
}
FLOW_destroy(&context, lct);
}
}
fclose(output);
char reference_filename[2048];
if (!create_path_from_relative(&context, __FILE__, true, reference_filename, 2048,
"/visuals/reference_weights.txt")) {
ERR(c);
}
char result_buffer[2048];
memset(&result_buffer[0], 0, 2048);
bool are_equal;
REQUIRE(flow_compare_file_contents(&context, filename, reference_filename, &result_buffer[0], 2048, &are_equal));
ERR(c);
CAPTURE(result_buffer);
if (!are_equal) {
char diff_command[4096];
flow_snprintf(diff_command, 4096, "diff -w %s %s", filename, reference_filename);
int ignore_result = system(diff_command); // just for the benefit of STDOUT
}
REQUIRE(are_equal);
REQUIRE(flow_context_begin_terminate(&context) == true);
flow_context_end_terminate(&context);
}