1 // Unit tests for DitherEffect.
3 // Note: Dithering of multiple outputs is tested (somewhat weakly)
4 // in YCbCrConversionEffectTest.
9 #include "effect_chain.h"
10 #include "gtest/gtest.h"
11 #include "image_format.h"
12 #include "test_util.h"
17 TEST(DitherEffectTest, NoDitherOnExactValues) {
20 float data[size * size] = {
26 unsigned char expected_data[size * size] = {
32 unsigned char out_data[size * size];
34 EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
36 tester.get_chain()->set_dither_bits(8);
38 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
41 expect_equal(expected_data, out_data, size, size);
44 TEST(DitherEffectTest, SinusoidBelowOneLevelComesThrough) {
45 const float frequency = 0.3f * M_PI;
46 const unsigned size = 2048;
47 const float amplitude = 0.25f / 255.0f; // 6 dB below what can be represented without dithering.
50 for (unsigned i = 0; i < size; ++i) {
51 data[i] = 0.2 + amplitude * sin(i * frequency);
53 unsigned char out_data[size];
55 EffectChainTester tester(data, size, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
56 tester.get_chain()->set_dither_bits(8);
57 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
59 // Measure how strong the given sinusoid is in the output.
61 for (unsigned i = 0; i < size; ++i) {
62 sum += 2.0 * (int(out_data[i]) - 0.2*255.0) * sin(i * frequency);
65 EXPECT_NEAR(amplitude, sum / (size * 255.0f), 1.1e-5);