]> git.sesse.net Git - movit/blob - overlay_effect_test.cpp
Factor out RTT sampler setting in its own function.
[movit] / overlay_effect_test.cpp
1 // Unit tests for OverlayEffect.
2
3 #include <GL/glew.h>
4
5 #include "effect_chain.h"
6 #include "gtest/gtest.h"
7 #include "image_format.h"
8 #include "input.h"
9 #include "overlay_effect.h"
10 #include "test_util.h"
11
12 namespace movit {
13
14 TEST(OverlayEffectTest, TopDominatesBottomWhenNoAlpha) {
15         float data_a[] = {
16                 0.0f, 0.25f,
17                 0.75f, 1.0f,
18         };
19         float data_b[] = {
20                 1.0f, 0.5f,
21                 0.75f, 0.6f,
22         };
23         float out_data[4];
24         EffectChainTester tester(data_a, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
25         Effect *input1 = tester.get_chain()->last_added_effect();
26         Effect *input2 = tester.add_input(data_b, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
27
28         tester.get_chain()->add_effect(new OverlayEffect(), input1, input2);
29         tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
30
31         expect_equal(data_b, out_data, 2, 2);
32 }
33
34 TEST(OverlayEffectTest, BottomDominatesTopWhenTopIsTransparent) {
35         float data_a[] = {
36                 1.0f, 0.0f, 0.0f, 0.5f,
37         };
38         float data_b[] = {
39                 0.5f, 0.5f, 0.5f, 0.0f,
40         };
41         float out_data[4];
42         EffectChainTester tester(data_a, 1, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
43         Effect *input1 = tester.get_chain()->last_added_effect();
44         Effect *input2 = tester.add_input(data_b, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
45
46         tester.get_chain()->add_effect(new OverlayEffect(), input1, input2);
47         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
48
49         expect_equal(data_a, out_data, 4, 1);
50 }
51
52 TEST(OverlayEffectTest, ZeroAlphaRemainsZeroAlpha) {
53         float data_a[] = {
54                 0.0f, 0.25f, 0.5f, 0.0f
55         };
56         float data_b[] = {
57                 1.0f, 1.0f, 1.0f, 0.0f
58         };
59         float expected_data[] = {
60                 0.0f, 0.0f, 0.0f, 0.0f
61         };
62         float out_data[4];
63         EffectChainTester tester(data_a, 1, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
64         Effect *input1 = tester.get_chain()->last_added_effect();
65         Effect *input2 = tester.add_input(data_b, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
66
67         tester.get_chain()->add_effect(new OverlayEffect(), input1, input2);
68         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
69
70         EXPECT_FLOAT_EQ(0.0f, expected_data[3]);
71 }
72
73 // This is tested against what Photoshop does: (255,0,128, 0.25) over (128,255,0, 0.5)
74 // becomes (179,153,51, 0.63). (Actually we fudge 0.63 to 0.625, because that's
75 // what it should be.)
76 TEST(OverlayEffectTest, PhotoshopReferenceTest) {
77         float data_a[] = {
78                 128.0f/255.0f, 1.0f, 0.0f, 0.5f
79         };
80         float data_b[] = {
81                 1.0f, 0.0f, 128.0f/255.0f, 0.25f
82         };
83         float expected_data[] = {
84                 179.0f/255.0f, 153.0f/255.0f, 51.0f/255.0f, 0.625f
85         };
86         float out_data[4];
87         EffectChainTester tester(data_a, 1, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
88         Effect *input1 = tester.get_chain()->last_added_effect();
89         Effect *input2 = tester.add_input(data_b, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
90
91         tester.get_chain()->add_effect(new OverlayEffect(), input1, input2);
92         tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
93
94         expect_equal(expected_data, out_data, 4, 1);
95 }
96
97 }  // namespace movit