]> git.sesse.net Git - movit/blob - dither_effect_test.cpp
Revert "Support pad/crop from bottom, not just from the top."
[movit] / dither_effect_test.cpp
1 // Unit tests for DitherEffect.
2
3 #include <GL/glew.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
11 namespace movit {
12
13 TEST(DitherEffectTest, NoDitherOnExactValues) {
14         const int size = 4;
15
16         float data[size * size] = {
17                 0.0, 1.0, 0.0, 1.0,
18                 0.0, 1.0, 1.0, 0.0,
19                 0.0, 0.2, 1.0, 0.2,
20                 0.0, 0.0, 0.0, 0.0,
21         };
22         unsigned char expected_data[size * size] = {
23                 0, 255,   0, 255,
24                 0, 255, 255,   0,
25                 0,  51, 255,  51,
26                 0,   0,   0,   0,
27         };
28         unsigned char out_data[size * size];
29
30         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
31         tester.get_chain()->set_dither_bits(8);
32         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
33
34         expect_equal(expected_data, out_data, size, size);
35 }
36
37 TEST(DitherEffectTest, SinusoidBelowOneLevelComesThrough) {
38         const float frequency = 0.3f * M_PI;
39         const unsigned size = 2048;
40         const float amplitude = 0.25f / 255.0f;  // 6 dB below what can be represented without dithering.
41
42         float data[size];
43         for (unsigned i = 0; i < size; ++i) {
44                 data[i] = 0.2 + amplitude * sin(i * frequency);
45         }
46         unsigned char out_data[size];
47
48         EffectChainTester tester(data, size, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
49         tester.get_chain()->set_dither_bits(8);
50         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
51
52         // Measure how strong the given sinusoid is in the output.
53         float sum = 0.0f;       
54         for (unsigned i = 0; i < size; ++i) {
55                 sum += 2.0 * (int(out_data[i]) - 0.2*255.0) * sin(i * frequency);
56         }
57
58         EXPECT_NEAR(amplitude, sum / (size * 255.0f), 1.1e-5);
59 }
60
61 }  // namespace movit