1 // Unit tests for DitherEffect.
6 #include "gtest/gtest.h"
8 TEST(DitherEffectTest, NoDitherOnExactValues) {
11 float data[size * size] = {
17 unsigned char expected_data[size * size] = {
23 unsigned char out_data[size * size];
25 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
26 tester.get_chain()->set_dither_bits(8);
27 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
29 expect_equal(expected_data, out_data, size, size);
32 TEST(DitherEffectTest, SinusoidBelowOneLevelComesThrough) {
33 const float frequency = 0.3f * M_PI;
34 const unsigned size = 2048;
35 const float amplitude = 0.25f / 255.0f; // 6 dB below what can be represented without dithering.
38 for (unsigned i = 0; i < size; ++i) {
39 data[i] = 0.2 + amplitude * sin(i * frequency);
41 unsigned char out_data[size];
43 EffectChainTester tester(data, size, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
44 tester.get_chain()->set_dither_bits(8);
45 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
47 // Measure how strong the given sinusoid is in the output.
49 for (unsigned i = 0; i < size; ++i) {
50 sum += 2.0 * (int(out_data[i]) - 0.2*255.0) * sin(i * frequency);
53 EXPECT_NEAR(amplitude, sum / (size * 255.0f), 1e-5);