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