]> git.sesse.net Git - movit/blob - vignette_effect_test.cpp
Hard-assert on something that has bitten me too many times now.
[movit] / vignette_effect_test.cpp
1 // Unit tests for VignetteEffect.
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 "vignette_effect.h"
11
12 namespace movit {
13
14 TEST(VignetteEffectTest, HugeInnerRadiusDoesNothing) {
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.5, 1.0, 0.5,
21                 0.0, 0.0, 0.0, 0.0,
22         };
23         float out_data[size * size];
24
25         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
26         Effect *vignette_effect = tester.get_chain()->add_effect(new VignetteEffect());
27         ASSERT_TRUE(vignette_effect->set_float("inner_radius", 10.0f));
28         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
29
30         expect_equal(data, out_data, size, size);
31 }
32
33 TEST(VignetteEffectTest, HardCircle) {
34         const int size = 16;
35
36         float data[size * size], out_data[size * size], expected_data[size * size];
37         for (int y = 0; y < size; ++y) {
38                 for (int x = 0; x < size; ++x) {
39                         data[y * size + x] = 1.0f;
40                 }
41         }
42         for (int y = 0; y < size; ++y) {
43                 const float yf = (y + 0.5f) / size;
44                 for (int x = 0; x < size; ++x) {
45                         const float xf = (x + 0.5f) / size;
46                         if (hypot(xf - 0.5, yf - 0.5) < 0.3) {
47                                 expected_data[y * size + x] = 1.0f;
48                         } else {
49                                 expected_data[y * size + x] = 0.0f;
50                         }
51                 }
52         }
53
54         EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
55         Effect *vignette_effect = tester.get_chain()->add_effect(new VignetteEffect());
56         ASSERT_TRUE(vignette_effect->set_float("radius", 0.0f));
57         ASSERT_TRUE(vignette_effect->set_float("inner_radius", 0.3f));
58         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
59
60         expect_equal(expected_data, out_data, size, size);
61 }
62
63 TEST(VignetteEffectTest, BurstFromUpperLeftCorner) {
64         const int width = 16, height = 24;
65         float radius = 0.5f;
66
67         float data[width * height], out_data[width * height], expected_data[width * height];
68         for (int y = 0; y < height; ++y) {
69                 for (int x = 0; x < width; ++x) {
70                         data[y * width + x] = 1.0f;
71                 }
72         }
73         for (int y = 0; y < height; ++y) {
74                 const float yf = (y + 0.5f) / width;  // Note: Division by width.
75                 for (int x = 0; x < width; ++x) {
76                         const float xf = (x + 0.5f) / width;
77                         const float d = hypot(xf, yf) / radius;
78                         if (d >= 1.0f) {
79                                 expected_data[y * width + x] = 0.0f;
80                         } else {
81                                 expected_data[y * width + x] = cos(d * 0.5 * M_PI) * cos(d * 0.5 * M_PI);
82                         }
83                 }
84         }
85
86         EffectChainTester tester(data, width, height, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
87         Effect *vignette_effect = tester.get_chain()->add_effect(new VignetteEffect());
88         float center[] = { 0.0f, 0.0f };
89         ASSERT_TRUE(vignette_effect->set_vec2("center", center));
90         ASSERT_TRUE(vignette_effect->set_float("radius", radius));
91         ASSERT_TRUE(vignette_effect->set_float("inner_radius", 0.0f));
92         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
93
94         expect_equal(expected_data, out_data, width, height);
95 }
96
97 }  // namespace movit