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
|
// Unit tests for FFTConvolutionEffect.
#include <epoxy/gl.h>
#include <math.h>
#include "effect_chain.h"
#include "gtest/gtest.h"
#include "image_format.h"
#include "test_util.h"
#include "fft_convolution_effect.h"
namespace movit {
TEST(FFTConvolutionEffectTest, Identity) {
const int size = 4;
float data[size * size] = {
0.1, 1.1, 2.1, 3.1,
0.2, 1.2, 2.2, 3.2,
0.3, 1.3, 2.3, 3.3,
0.4, 1.4, 2.4, 3.4,
};
float out_data[size * size];
for (int convolve_size = 1; convolve_size < 10; ++convolve_size) {
float kernel[convolve_size * convolve_size];
for (int i = 0; i < convolve_size * convolve_size; ++i) {
kernel[i] = 0.0f;
}
kernel[0] = 1.0f;
EffectChainTester tester(nullptr, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, size, size);
FFTConvolutionEffect *fft_effect = new FFTConvolutionEffect(size, size, convolve_size, convolve_size);
tester.get_chain()->add_effect(fft_effect);
fft_effect->set_convolution_kernel(kernel);
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
expect_equal(data, out_data, size, size, 0.02, 0.003);
}
}
TEST(FFTConvolutionEffectTest, Constant) {
const int size = 4, convolve_size = 17;
const float f = 7.0f;
float data[size * size] = {
0.1, 1.1, 2.1, 3.1,
0.2, 1.2, 2.2, 3.2,
0.3, 1.3, 2.3, 3.3,
0.4, 1.4, 2.4, 3.4,
};
float expected_data[size * size] = {
f * 0.1f, f * 1.1f, f * 2.1f, f * 3.1f,
f * 0.2f, f * 1.2f, f * 2.2f, f * 3.2f,
f * 0.3f, f * 1.3f, f * 2.3f, f * 3.3f,
f * 0.4f, f * 1.4f, f * 2.4f, f * 3.4f,
};
float out_data[size * size];
float kernel[convolve_size * convolve_size];
for (int i = 0; i < convolve_size * convolve_size; ++i) {
kernel[i] = 0.0f;
}
kernel[0] = f;
EffectChainTester tester(nullptr, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, size, size);
FFTConvolutionEffect *fft_effect = new FFTConvolutionEffect(size, size, convolve_size, convolve_size);
tester.get_chain()->add_effect(fft_effect);
fft_effect->set_convolution_kernel(kernel);
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
// Somewhat looser bounds due to the higher magnitude.
expect_equal(expected_data, out_data, size, size, f * 0.03, f * 0.004);
}
TEST(FFTConvolutionEffectTest, MoveRight) {
const int size = 4, convolve_size = 3;
float data[size * size] = {
0.1, 1.1, 2.1, 3.1,
0.2, 1.2, 2.2, 3.2,
0.3, 1.3, 2.3, 3.3,
0.4, 1.4, 2.4, 3.4,
};
float kernel[convolve_size * convolve_size] = {
0.0, 1.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
};
float expected_data[size * size] = {
0.1, 0.1, 1.1, 2.1,
0.2, 0.2, 1.2, 2.2,
0.3, 0.3, 1.3, 2.3,
0.4, 0.4, 1.4, 2.4,
};
float out_data[size * size];
EffectChainTester tester(nullptr, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, size, size);
FFTConvolutionEffect *fft_effect = new FFTConvolutionEffect(size, size, convolve_size, convolve_size);
tester.get_chain()->add_effect(fft_effect);
fft_effect->set_convolution_kernel(kernel);
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
expect_equal(expected_data, out_data, size, size, 0.02, 0.003);
}
TEST(FFTConvolutionEffectTest, MoveDown) {
const int size = 4, convolve_size = 3;
float data[size * size] = {
0.1, 1.1, 2.1, 3.1,
0.2, 1.2, 2.2, 3.2,
0.3, 1.3, 2.3, 3.3,
0.4, 1.4, 2.4, 3.4,
};
float kernel[convolve_size * convolve_size] = {
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 0.0, 0.0,
};
float expected_data[size * size] = {
0.1, 1.1, 2.1, 3.1,
0.1, 1.1, 2.1, 3.1,
0.2, 1.2, 2.2, 3.2,
0.3, 1.3, 2.3, 3.3,
};
float out_data[size * size];
EffectChainTester tester(nullptr, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, size, size);
FFTConvolutionEffect *fft_effect = new FFTConvolutionEffect(size, size, convolve_size, convolve_size);
tester.get_chain()->add_effect(fft_effect);
fft_effect->set_convolution_kernel(kernel);
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
expect_equal(expected_data, out_data, size, size, 0.02, 0.003);
}
TEST(FFTConvolutionEffectTest, MergeWithLeft) {
const int size = 4, convolve_size = 3;
float data[size * size] = {
0.1, 1.1, 2.1, 3.1,
0.2, 1.2, 2.2, 3.2,
0.3, 1.3, 2.3, 3.3,
0.4, 1.4, 2.4, 3.4,
};
float kernel[convolve_size * convolve_size] = {
1.0, 1.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0,
};
float expected_data[size * size] = {
0.1 + 0.1, 0.1 + 1.1, 1.1 + 2.1, 2.1 + 3.1,
0.2 + 0.2, 0.2 + 1.2, 1.2 + 2.2, 2.2 + 3.2,
0.3 + 0.3, 0.3 + 1.3, 1.3 + 2.3, 2.3 + 3.3,
0.4 + 0.4, 0.4 + 1.4, 1.4 + 2.4, 2.4 + 3.4,
};
float out_data[size * size];
EffectChainTester tester(nullptr, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, size, size);
FFTConvolutionEffect *fft_effect = new FFTConvolutionEffect(size, size, convolve_size, convolve_size);
tester.get_chain()->add_effect(fft_effect);
fft_effect->set_convolution_kernel(kernel);
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
expect_equal(expected_data, out_data, size, size, 0.02, 0.003);
}
TEST(FFTConvolutionEffectTest, NegativeCoefficients) {
const int size = 4;
const int convolve_width = 3, convolve_height = 2;
float data[size * size] = {
0.1, 1.1, 2.1, 3.1,
0.2, 1.2, 2.2, 3.2,
0.3, 1.3, 2.3, 3.3,
0.4, 1.4, 2.4, 3.4,
};
float kernel[convolve_width * convolve_height] = {
1.0, 0.0, 0.0,
0.0, 0.0, -0.5,
};
float expected_data[size * size] = {
0.1 - 0.5 * 0.1, 1.1 - 0.5 * 0.1, 2.1 - 0.5 * 0.1, 3.1 - 0.5 * 1.1,
0.2 - 0.5 * 0.1, 1.2 - 0.5 * 0.1, 2.2 - 0.5 * 0.1, 3.2 - 0.5 * 1.1,
0.3 - 0.5 * 0.2, 1.3 - 0.5 * 0.2, 2.3 - 0.5 * 0.2, 3.3 - 0.5 * 1.2,
0.4 - 0.5 * 0.3, 1.4 - 0.5 * 0.3, 2.4 - 0.5 * 0.3, 3.4 - 0.5 * 1.3,
};
float out_data[size * size];
EffectChainTester tester(nullptr, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, size, size);
FFTConvolutionEffect *fft_effect = new FFTConvolutionEffect(size, size, convolve_width, convolve_height);
tester.get_chain()->add_effect(fft_effect);
fft_effect->set_convolution_kernel(kernel);
tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
expect_equal(expected_data, out_data, size, size, 0.02, 0.003);
}
} // namespace movit
|