1 // Unit tests for SliceEffect.
5 #include "effect_chain.h"
6 #include "flat_input.h"
7 #include "gtest/gtest.h"
8 #include "image_format.h"
10 #include "slice_effect.h"
11 #include "test_util.h"
15 TEST(SliceEffectTest, Identity) {
16 const int size = 3, output_size = 4;
17 float data[size * size] = {
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,
27 float out_data[output_size * size];
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);
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);
38 expect_equal(expected_data, out_data, output_size, size);
41 TEST(SliceEffectTest, HorizontalOverlap) {
43 0.0f, 0.1f, 0.2f, 0.3f, 0.4f,
44 0.4f, 0.3f, 0.2f, 0.1f, 0.0f,
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,
50 float out_data[9 * 2];
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);
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);
61 expect_equal(expected_data, out_data, 9, 2);
64 TEST(SliceEffectTest, HorizontalDiscard) {
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,
69 float expected_data[4 * 2] = {
70 0.0f, 0.1f, 0.2f, 0.3f,
71 0.4f, 0.3f, 0.2f, 0.1f,
73 float out_data[4 * 2];
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);
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);
84 expect_equal(expected_data, out_data, 4, 2);
87 TEST(SliceEffectTest, HorizontalOverlapWithOffset) {
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,
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,
96 float out_data[9 * 2];
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);
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);
108 expect_equal(expected_data, out_data, 9, 2);
111 TEST(SliceEffectTest, VerticalOverlapSlicesFromTop) {
112 float data[2 * 3] = {
118 float expected_data[2 * 6] = {
127 float out_data[2 * 6];
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);
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);
138 expect_equal(expected_data, out_data, 2, 6);
141 TEST(SliceEffectTest, VerticalOverlapOffsetsFromTop) {
142 float data[2 * 3] = {
148 float expected_data[2 * 6] = {
157 float out_data[2 * 6];
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);
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);
169 expect_equal(expected_data, out_data, 2, 6);