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