]> git.sesse.net Git - movit/blob - dither_effect_test.cpp
Use auto and range-based for loops a few places where it aids clarity.
[movit] / dither_effect_test.cpp
1 // Unit tests for DitherEffect.
2 //
3 // Note: Dithering of multiple outputs is tested (somewhat weakly)
4 // in YCbCrConversionEffectTest.
5
6 #include <epoxy/gl.h>
7 #include <math.h>
8
9 #include "effect_chain.h"
10 #include "gtest/gtest.h"
11 #include "image_format.h"
12 #include "test_util.h"
13 #include "util.h"
14
15 namespace movit {
16
17 TEST(DitherEffectTest, NoDitherOnExactValues) {
18         const int size = 4;
19
20         float data[size * size] = {
21                 0.0, 1.0, 0.0, 1.0,
22                 0.0, 1.0, 1.0, 0.0,
23                 0.0, 0.2, 1.0, 0.2,
24                 0.0, 0.0, 0.0, 0.0,
25         };
26         unsigned char expected_data[size * size] = {
27                 0, 255,   0, 255,
28                 0, 255, 255,   0,
29                 0,  51, 255,  51,
30                 0,   0,   0,   0,
31         };
32         unsigned char out_data[size * size];
33
34         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
35         check_error();
36         tester.get_chain()->set_dither_bits(8);
37         check_error();
38         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
39         check_error();
40
41         expect_equal(expected_data, out_data, size, size);
42 }
43
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.
48
49         float data[size];
50         for (unsigned i = 0; i < size; ++i) {
51                 data[i] = 0.2 + amplitude * sin(i * frequency);
52         }
53         unsigned char out_data[size];
54
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);
58
59         // Measure how strong the given sinusoid is in the output.
60         float sum = 0.0f;       
61         for (unsigned i = 0; i < size; ++i) {
62                 sum += 2.0 * (int(out_data[i]) - 0.2*255.0) * sin(i * frequency);
63         }
64
65         EXPECT_NEAR(amplitude, sum / (size * 255.0f), 1.1e-5);
66 }
67
68 }  // namespace movit