]> git.sesse.net Git - movit/blob - dither_effect_test.cpp
Remove stray file movit_pch.h, which was never meant to be checked in.
[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 TEST(DitherEffectTest, NoDitherOnExactValues) {
12         const int size = 4;
13
14         float data[size * size] = {
15                 0.0, 1.0, 0.0, 1.0,
16                 0.0, 1.0, 1.0, 0.0,
17                 0.0, 0.2, 1.0, 0.2,
18                 0.0, 0.0, 0.0, 0.0,
19         };
20         unsigned char expected_data[size * size] = {
21                 0, 255,   0, 255,
22                 0, 255, 255,   0,
23                 0,  51, 255,  51,
24                 0,   0,   0,   0,
25         };
26         unsigned char out_data[size * size];
27
28         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
29         tester.get_chain()->set_dither_bits(8);
30         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
31
32         expect_equal(expected_data, out_data, size, size);
33 }
34
35 TEST(DitherEffectTest, SinusoidBelowOneLevelComesThrough) {
36         const float frequency = 0.3f * M_PI;
37         const unsigned size = 2048;
38         const float amplitude = 0.25f / 255.0f;  // 6 dB below what can be represented without dithering.
39
40         float data[size];
41         for (unsigned i = 0; i < size; ++i) {
42                 data[i] = 0.2 + amplitude * sin(i * frequency);
43         }
44         unsigned char out_data[size];
45
46         EffectChainTester tester(data, size, 1, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, GL_RGBA8);
47         tester.get_chain()->set_dither_bits(8);
48         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
49
50         // Measure how strong the given sinusoid is in the output.
51         float sum = 0.0f;       
52         for (unsigned i = 0; i < size; ++i) {
53                 sum += 2.0 * (int(out_data[i]) - 0.2*255.0) * sin(i * frequency);
54         }
55
56         EXPECT_NEAR(amplitude, sum / (size * 255.0f), 1.1e-5);
57 }