]> git.sesse.net Git - movit/blob - dither_effect_test.cpp
Give the alpha enums somewhat better/more consistent names, and shuffle them around...
[movit] / dither_effect_test.cpp
1 // Unit tests for DitherEffect.
2
3 #include <math.h>
4
5 #include "test_util.h"
6 #include "gtest/gtest.h"
7
8 TEST(DitherEffectTest, NoDitherOnExactValues) {
9         const int size = 4;
10
11         float data[size * size] = {
12                 0.0, 1.0, 0.0, 1.0,
13                 0.0, 1.0, 1.0, 0.0,
14                 0.0, 0.2, 1.0, 0.2,
15                 0.0, 0.0, 0.0, 0.0,
16         };
17         unsigned char expected_data[size * size] = {
18                 0, 255,   0, 255,
19                 0, 255, 255,   0,
20                 0,  51, 255,  51,
21                 0,   0,   0,   0,
22         };
23         unsigned char out_data[size * size];
24
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);
28
29         expect_equal(expected_data, out_data, size, size);
30 }
31
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.
36
37         float data[size];
38         for (unsigned i = 0; i < size; ++i) {
39                 data[i] = 0.2 + amplitude * sin(i * frequency);
40         }
41         unsigned char out_data[size];
42
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);
46
47         // Measure how strong the given sinusoid is in the output.
48         float sum = 0.0f;       
49         for (unsigned i = 0; i < size; ++i) {
50                 sum += 2.0 * (int(out_data[i]) - 0.2*255.0) * sin(i * frequency);
51         }
52
53         EXPECT_NEAR(amplitude, sum / (size * 255.0f), 1.1e-5);
54 }