]> git.sesse.net Git - movit/blob - slice_effect_test.cpp
Add some asserts to EffectChain::execute_phase, so that we do not inadvertedly insert...
[movit] / slice_effect_test.cpp
1 // Unit tests for SliceEffect.
2
3 #include <epoxy/gl.h>
4
5 #include "effect_chain.h"
6 #include "flat_input.h"
7 #include "gtest/gtest.h"
8 #include "image_format.h"
9 #include "input.h"
10 #include "slice_effect.h"
11 #include "test_util.h"
12
13 namespace movit {
14
15 TEST(SliceEffectTest, Identity) {
16         const int size = 3, output_size = 4;
17         float data[size * size] = {
18                 0.0f, 0.1f, 0.2f,
19                 0.4f, 0.3f, 0.8f,
20                 0.5f, 0.2f, 0.1f,
21         };
22         float expected_data[output_size * size] = {
23                 0.0f, 0.1f, 0.2f, 0.2f,
24                 0.4f, 0.3f, 0.8f, 0.8f,
25                 0.5f, 0.2f, 0.1f, 0.1f,
26         };
27         float out_data[output_size * size];
28
29         EffectChainTester tester(nullptr, output_size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
30         tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, size, size);
31
32         Effect *slice_effect = tester.get_chain()->add_effect(new SliceEffect());
33         ASSERT_TRUE(slice_effect->set_int("input_slice_size", 2));
34         ASSERT_TRUE(slice_effect->set_int("output_slice_size", 2));
35         ASSERT_TRUE(slice_effect->set_int("direction", SliceEffect::HORIZONTAL));
36         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
37
38         expect_equal(expected_data, out_data, output_size, size);
39 }
40
41 TEST(SliceEffectTest, HorizontalOverlap) {
42         float data[5 * 2] = {
43                 0.0f, 0.1f,  0.2f, 0.3f,  0.4f,
44                 0.4f, 0.3f,  0.2f, 0.1f,  0.0f,
45         };
46         float expected_data[9 * 2] = {
47                 0.0f, 0.1f, 0.2f,  0.2f, 0.3f, 0.4f,  0.4f, 0.4f, 0.4f,
48                 0.4f, 0.3f, 0.2f,  0.2f, 0.1f, 0.0f,  0.0f, 0.0f, 0.0f,
49         };
50         float out_data[9 * 2];
51
52         EffectChainTester tester(nullptr, 9, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
53         tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, 5, 2);
54
55         Effect *slice_effect = tester.get_chain()->add_effect(new SliceEffect());
56         ASSERT_TRUE(slice_effect->set_int("input_slice_size", 2));
57         ASSERT_TRUE(slice_effect->set_int("output_slice_size", 3));
58         ASSERT_TRUE(slice_effect->set_int("direction", SliceEffect::HORIZONTAL));
59         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
60
61         expect_equal(expected_data, out_data, 9, 2);
62 }
63
64 TEST(SliceEffectTest, HorizontalDiscard) {
65         float data[6 * 2] = {
66                 0.0f, 0.1f, 0.2f,  0.2f, 0.3f, 0.4f,
67                 0.4f, 0.3f, 0.2f,  0.2f, 0.1f, 0.0f,
68         };
69         float expected_data[4 * 2] = {
70                 0.0f, 0.1f,  0.2f, 0.3f,
71                 0.4f, 0.3f,  0.2f, 0.1f,
72         };
73         float out_data[4 * 2];
74
75         EffectChainTester tester(nullptr, 4, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
76         tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, 6, 2);
77
78         Effect *slice_effect = tester.get_chain()->add_effect(new SliceEffect());
79         ASSERT_TRUE(slice_effect->set_int("input_slice_size", 3));
80         ASSERT_TRUE(slice_effect->set_int("output_slice_size", 2));
81         ASSERT_TRUE(slice_effect->set_int("direction", SliceEffect::HORIZONTAL));
82         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
83
84         expect_equal(expected_data, out_data, 4, 2);
85 }
86
87 TEST(SliceEffectTest, HorizontalOverlapWithOffset) {
88         float data[5 * 2] = {
89                 /* 0.0f, */ 0.0f,  0.1f, 0.2f,  0.3f, 0.4f,
90                 /* 0.4f, */ 0.4f,  0.3f, 0.2f,  0.1f, 0.0f,
91         };
92         float expected_data[9 * 2] = {
93                 0.0f, 0.0f, 0.1f,  0.1f, 0.2f, 0.3f,  0.3f, 0.4f, 0.4f,
94                 0.4f, 0.4f, 0.3f,  0.3f, 0.2f, 0.1f,  0.1f, 0.0f, 0.0f,
95         };
96         float out_data[9 * 2];
97
98         EffectChainTester tester(nullptr, 9, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
99         tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, 5, 2);
100
101         Effect *slice_effect = tester.get_chain()->add_effect(new SliceEffect());
102         ASSERT_TRUE(slice_effect->set_int("input_slice_size", 2));
103         ASSERT_TRUE(slice_effect->set_int("output_slice_size", 3));
104         ASSERT_TRUE(slice_effect->set_int("offset", -1));
105         ASSERT_TRUE(slice_effect->set_int("direction", SliceEffect::HORIZONTAL));
106         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
107
108         expect_equal(expected_data, out_data, 9, 2);
109 }
110
111 TEST(SliceEffectTest, VerticalOverlapSlicesFromTop) {
112         float data[2 * 3] = {
113                 0.0f, 0.1f,
114                 0.4f, 0.3f,
115
116                 0.6f, 0.2f,
117         };
118         float expected_data[2 * 6] = {
119                 0.0f, 0.1f,
120                 0.4f, 0.3f,
121                 0.6f, 0.2f,
122
123                 0.6f, 0.2f,
124                 0.6f, 0.2f,
125                 0.6f, 0.2f,
126         };
127         float out_data[2 * 6];
128
129         EffectChainTester tester(nullptr, 2, 6, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
130         tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, 2, 3);
131
132         Effect *slice_effect = tester.get_chain()->add_effect(new SliceEffect());
133         ASSERT_TRUE(slice_effect->set_int("input_slice_size", 2));
134         ASSERT_TRUE(slice_effect->set_int("output_slice_size", 3));
135         ASSERT_TRUE(slice_effect->set_int("direction", SliceEffect::VERTICAL));
136         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
137
138         expect_equal(expected_data, out_data, 2, 6);
139 }
140
141 TEST(SliceEffectTest, VerticalOverlapOffsetsFromTop) {
142         float data[2 * 3] = {
143                 0.0f, 0.1f,
144                 0.4f, 0.3f,
145
146                 0.6f, 0.2f,
147         };
148         float expected_data[2 * 6] = {
149                 0.4f, 0.3f,
150                 0.6f, 0.2f,
151                 0.6f, 0.2f,
152
153                 0.6f, 0.2f,
154                 0.6f, 0.2f,
155                 0.6f, 0.2f,
156         };
157         float out_data[2 * 6];
158
159         EffectChainTester tester(nullptr, 2, 6, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
160         tester.add_input(data, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR, 2, 3);
161
162         Effect *slice_effect = tester.get_chain()->add_effect(new SliceEffect());
163         ASSERT_TRUE(slice_effect->set_int("input_slice_size", 2));
164         ASSERT_TRUE(slice_effect->set_int("output_slice_size", 3));
165         ASSERT_TRUE(slice_effect->set_int("offset", 1));
166         ASSERT_TRUE(slice_effect->set_int("direction", SliceEffect::VERTICAL));
167         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
168
169         expect_equal(expected_data, out_data, 2, 6);
170 }
171
172 }  // namespace movit