X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=deconvolution_sharpen_effect_test.cpp;h=f08225b15a6575f741b9469d38a4ae6a50ee8170;hp=bf6dfb81b8f105dbbf7d5d0a96f9d8b5df5f9c86;hb=0490917aca6b8e6057c04c8becc820a8e849a801;hpb=2e3a9b8deae0d4635a9a97fec04f60f66e8a3682 diff --git a/deconvolution_sharpen_effect_test.cpp b/deconvolution_sharpen_effect_test.cpp index bf6dfb8..f08225b 100644 --- a/deconvolution_sharpen_effect_test.cpp +++ b/deconvolution_sharpen_effect_test.cpp @@ -4,7 +4,30 @@ #include "gtest/gtest.h" #include "deconvolution_sharpen_effect.h" -TEST(DeconvolutionSharpenEffect, DeconvolvesCircularBlur) { +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. @@ -53,7 +76,7 @@ TEST(DeconvolutionSharpenEffect, DeconvolvesCircularBlur) { expect_equal(expected_data, out_data, size, size, 0.15f, 0.005f); } -TEST(DeconvolutionSharpenEffect, DeconvolvesGaussianBlur) { +TEST(DeconvolutionSharpenEffectTest, DeconvolvesGaussianBlur) { const int size = 13; const float sigma = 0.5f; @@ -102,5 +125,72 @@ TEST(DeconvolutionSharpenEffect, DeconvolvesGaussianBlur) { expect_equal(expected_data, out_data, size, size); } -// TODO: Test no-op (both radii equal to zero). -// TODO: Test correlation and noise parameters. +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); +}