]> git.sesse.net Git - movit/blob - dither_effect_test.cpp
In ResampleEffect, use a struct instead of manually fiddling with the two elements...
[movit] / dither_effect_test.cpp
1 // Unit tests for DitherEffect.
2
3 #include <epoxy/gl.h>
4 #include <math.h>
5
6 #include "effect_chain.h"
7 #include "gtest/gtest.h"
8 #include "image_format.h"
9 #include "test_util.h"
10 #include "util.h"
11
12 namespace movit {
13
14 TEST(DitherEffectTest, NoDitherOnExactValues) {
15         const int size = 4;
16
17         float data[size * size] = {
18                 0.0, 1.0, 0.0, 1.0,
19                 0.0, 1.0, 1.0, 0.0,
20                 0.0, 0.2, 1.0, 0.2,
21                 0.0, 0.0, 0.0, 0.0,
22         };
23         unsigned char expected_data[size * size] = {
24                 0, 255,   0, 255,
25                 0, 255, 255,   0,
26                 0,  51, 255,  51,
27                 0,   0,   0,   0,
28         };
29         unsigned char out_data[size * size];
30
31         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
32         check_error();
33         tester.get_chain()->set_dither_bits(8);
34         check_error();
35         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
36         check_error();
37
38         expect_equal(expected_data, out_data, size, size);
39 }
40
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.
45
46         float data[size];
47         for (unsigned i = 0; i < size; ++i) {
48                 data[i] = 0.2 + amplitude * sin(i * frequency);
49         }
50         unsigned char out_data[size];
51
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);
55
56         // Measure how strong the given sinusoid is in the output.
57         float sum = 0.0f;       
58         for (unsigned i = 0; i < size; ++i) {
59                 sum += 2.0 * (int(out_data[i]) - 0.2*255.0) * sin(i * frequency);
60         }
61
62         EXPECT_NEAR(amplitude, sum / (size * 255.0f), 1.1e-5);
63 }
64
65 }  // namespace movit