X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=dither_effect_test.cpp;fp=dither_effect_test.cpp;h=81f6a2402d6d677e25c2e22727308ead45c491fd;hp=0000000000000000000000000000000000000000;hb=ff9e68a3f5abb179bd7bf9fb84df48327f148583;hpb=3a1e190a989a29edffdfa79bad7994149fc5d27c diff --git a/dither_effect_test.cpp b/dither_effect_test.cpp new file mode 100644 index 0000000..81f6a24 --- /dev/null +++ b/dither_effect_test.cpp @@ -0,0 +1,54 @@ +// Unit tests for DitherEffect. + +#include + +#include "test_util.h" +#include "gtest/gtest.h" + +TEST(DitherEffectTest, NoDitherOnExactValues) { + 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.2, 1.0, 0.2, + 0.0, 0.0, 0.0, 0.0, + }; + unsigned char expected_data[size * size] = { + 0, 255, 0, 255, + 0, 255, 255, 0, + 0, 51, 255, 51, + 0, 0, 0, 0, + }; + unsigned char out_data[size * size]; + + EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8); + tester.get_chain()->set_dither_bits(8); + tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); + + expect_equal(expected_data, out_data, size, size); +} + +TEST(DitherEffectTest, SinusoidBelowOneLevelComesThrough) { + const float frequency = 0.3f * M_PI; + const unsigned size = 2048; + const float amplitude = 0.25f / 255.0f; // 6 dB below what can be represented without dithering. + + float data[size]; + for (unsigned i = 0; i < size; ++i) { + data[i] = 0.2 + amplitude * sin(i * frequency); + } + unsigned char out_data[size]; + + EffectChainTester tester(data, size, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8); + tester.get_chain()->set_dither_bits(8); + tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); + + // Measure how strong the given sinusoid is in the output. + float sum = 0.0f; + for (unsigned i = 0; i < size; ++i) { + sum += 2.0 * (int(out_data[i]) - 0.2*255.0) * sin(i * frequency); + } + + EXPECT_NEAR(amplitude, sum / (size * 255.0f), 1e-5); +}