-
Notifications
You must be signed in to change notification settings - Fork 2
/
deconvolution_sharpen_effect_test.cpp
238 lines (208 loc) · 11.4 KB
/
deconvolution_sharpen_effect_test.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
// Unit tests for DeconvolutionSharpenEffect.
#include <epoxy/gl.h>
#include <math.h>
#include <stdlib.h>
#include "deconvolution_sharpen_effect.h"
#include "effect_chain.h"
#include "gtest/gtest.h"
#include "image_format.h"
#include "test_util.h"
namespace movit {
TEST(DeconvolutionSharpenEffectTest, IdentityTransformDoesNothing) {
const int size = 4;
float data[size * size] = {
0.0, 1.0, 0.0, 1.0,
0.0, 1.0, 1.0, 0.0,
0.0, 0.5, 1.0, 0.5,
0.0, 0.0, 0.0, 0.0,
};
float out_data[size * size];
EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
Effect *deconvolution_effect = tester.get_chain()->add_effect(new DeconvolutionSharpenEffect());
ASSERT_TRUE(deconvolution_effect->set_int("matrix_size", 5));
ASSERT_TRUE(deconvolution_effect->set_float("circle_radius", 0.0f));
ASSERT_TRUE(deconvolution_effect->set_float("gaussian_radius", 0.0f));
ASSERT_TRUE(deconvolution_effect->set_float("correlation", 0.0001f));
ASSERT_TRUE(deconvolution_effect->set_float("noise", 0.0f));
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
expect_equal(data, out_data, size, size);
}
TEST(DeconvolutionSharpenEffectTest, DeconvolvesCircularBlur) {
const int size = 13;
// Matches exactly a circular blur kernel with radius 2.0.
float data[size * size] = {
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.017016, 0.038115, 0.017016, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.017016, 0.078381, 0.079577, 0.078381, 0.017016, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.038115, 0.079577, 0.079577, 0.079577, 0.038115, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.017016, 0.078381, 0.079577, 0.078381, 0.017016, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.017016, 0.038115, 0.017016, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
};
float expected_data[size * size] = {
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
};
float out_data[size * size];
EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
Effect *deconvolution_effect = tester.get_chain()->add_effect(new DeconvolutionSharpenEffect());
ASSERT_TRUE(deconvolution_effect->set_int("matrix_size", 5));
ASSERT_TRUE(deconvolution_effect->set_float("circle_radius", 2.0f));
ASSERT_TRUE(deconvolution_effect->set_float("gaussian_radius", 0.0f));
ASSERT_TRUE(deconvolution_effect->set_float("correlation", 0.0001f));
ASSERT_TRUE(deconvolution_effect->set_float("noise", 0.0f));
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
// The limits have to be quite lax; deconvolution is not an exact operation.
expect_equal(expected_data, out_data, size, size, 0.15f, 0.005f);
}
TEST(DeconvolutionSharpenEffectTest, DeconvolvesGaussianBlur) {
const int size = 13;
const float sigma = 0.5f;
float data[size * size], out_data[size * size];
float expected_data[] = {
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
};
float sum = 0.0f;
for (int y = 0; y < size; ++y) {
for (int x = 0; x < size; ++x) {
float z = hypot(x - 6, y - 6);
data[y * size + x] = exp(-z*z / (2.0 * sigma * sigma)) / (2.0 * M_PI * sigma * sigma);
sum += data[y * size + x];
}
}
for (int y = 0; y < size; ++y) {
for (int x = 0; x < size; ++x) {
data[y * size + x] /= sum;
}
}
EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
Effect *deconvolution_effect = tester.get_chain()->add_effect(new DeconvolutionSharpenEffect());
ASSERT_TRUE(deconvolution_effect->set_int("matrix_size", 5));
ASSERT_TRUE(deconvolution_effect->set_float("circle_radius", 0.0f));
ASSERT_TRUE(deconvolution_effect->set_float("gaussian_radius", sigma));
ASSERT_TRUE(deconvolution_effect->set_float("correlation", 0.0001f));
ASSERT_TRUE(deconvolution_effect->set_float("noise", 0.0f));
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
// We don't actually need to adjust the limits here; deconvolution of
// this kernel is pretty much exact.
expect_equal(expected_data, out_data, size, size);
}
TEST(DeconvolutionSharpenEffectTest, NoiseAndCorrelationControlsReduceNoiseBoosting) {
const int size = 13;
const float sigma = 0.5f;
float data[size * size], out_data[size * size];
float expected_data[size * size] = {
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.0, 0.0, 0.0, 0.0,
};
// Gaussian kernel.
float sum = 0.0f;
for (int y = 0; y < size; ++y) {
for (int x = 0; x < size; ++x) {
float z = hypot(x - 6, y - 6);
data[y * size + x] = exp(-z*z / (2.0 * sigma * sigma)) / (2.0 * M_PI * sigma * sigma);
sum += data[y * size + x];
}
}
for (int y = 0; y < size; ++y) {
for (int x = 0; x < size; ++x) {
data[y * size + x] /= sum;
}
}
// Corrupt with some uniform noise.
srand(1234);
for (int i = 0; i < size * size; ++i) {
data[i] += 0.1 * ((float)rand() / RAND_MAX - 0.5);
}
EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
Effect *deconvolution_effect = tester.get_chain()->add_effect(new DeconvolutionSharpenEffect());
ASSERT_TRUE(deconvolution_effect->set_int("matrix_size", 5));
ASSERT_TRUE(deconvolution_effect->set_float("circle_radius", 0.0f));
ASSERT_TRUE(deconvolution_effect->set_float("gaussian_radius", 0.5f));
ASSERT_TRUE(deconvolution_effect->set_float("correlation", 0.5f));
ASSERT_TRUE(deconvolution_effect->set_float("noise", 0.1f));
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
float sumsq_in = 0.0f, sumsq_out = 0.0f;
for (int i = 0; i < size * size; ++i) {
sumsq_in += data[i] * data[i];
sumsq_out += out_data[i] * out_data[i];
}
// The limits have to be quite lax; deconvolution is not an exact operation.
// We special-case the center sample since it's the one with the largest error
// almost no matter what we do, so we don't want that to be the dominating
// factor in the outlier tests.
int center = size / 2;
EXPECT_GT(out_data[center * size + center], 0.5f);
out_data[center * size + center] = 1.0f;
expect_equal(expected_data, out_data, size, size, 0.20f, 0.005f);
// Check that we didn't boost total energy (which in this case means the noise) more than 10%.
EXPECT_LT(sumsq_out, sumsq_in * 1.1f);
}
TEST(DeconvolutionSharpenEffectTest, CircularDeconvolutionKeepsAlpha) {
// Somewhat bigger, to make sure we are much bigger than the matrix size.
const int size = 32;
float data[size * size * 4];
float out_data[size * size];
float expected_alpha[size * size];
// Checkerbox pattern.
for (int y = 0; y < size; ++y) {
for (int x = 0; x < size; ++x) {
int c = (y ^ x) & 1;
data[(y * size + x) * 4 + 0] = c;
data[(y * size + x) * 4 + 1] = c;
data[(y * size + x) * 4 + 2] = c;
data[(y * size + x) * 4 + 3] = 1.0;
expected_alpha[y * size + x] = 1.0;
}
}
EffectChainTester tester(data, size, size, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
Effect *deconvolution_effect = tester.get_chain()->add_effect(new DeconvolutionSharpenEffect());
ASSERT_TRUE(deconvolution_effect->set_int("matrix_size", 5));
ASSERT_TRUE(deconvolution_effect->set_float("circle_radius", 2.0f));
ASSERT_TRUE(deconvolution_effect->set_float("gaussian_radius", 0.0f));
ASSERT_TRUE(deconvolution_effect->set_float("correlation", 0.0001f));
ASSERT_TRUE(deconvolution_effect->set_float("noise", 0.0f));
tester.run(out_data, GL_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
expect_equal(expected_alpha, out_data, size, size);
}
} // namespace movit