File: mix_effect_test.cpp

package info (click to toggle)
movit 1.7.2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,248 kB
  • sloc: cpp: 16,677; sh: 3,940; makefile: 167
file content (144 lines) | stat: -rw-r--r-- 4,538 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Unit tests for MixEffect.

#include <epoxy/gl.h>

#include "effect_chain.h"
#include "gtest/gtest.h"
#include "image_format.h"
#include "input.h"
#include "mix_effect.h"
#include "test_util.h"

namespace movit {

TEST(MixEffectTest, FiftyFiftyMix) {
	float data_a[] = {
		0.0f, 0.25f,
		0.75f, 1.0f,
	};
	float data_b[] = {
		1.0f, 0.5f,
		0.75f, 0.6f,
	};
	float expected_data[] = {
		0.5f, 0.375f,
		0.75f, 0.8f,
	};
	float out_data[4];
	EffectChainTester tester(data_a, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
	Effect *input1 = tester.get_chain()->last_added_effect();
	Effect *input2 = tester.add_input(data_b, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);

	Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
	ASSERT_TRUE(mix_effect->set_float("strength_first", 0.5f));
	ASSERT_TRUE(mix_effect->set_float("strength_second", 0.5f));
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);

	expect_equal(expected_data, out_data, 2, 2);
}

TEST(MixEffectTest, OnlyA) {
	float data_a[] = {
		0.0f, 0.25f,
		0.75f, 1.0f,
	};
	float data_b[] = {
		1.0f, 0.5f,
		0.75f, 0.6f,
	};
	float out_data[4];
	EffectChainTester tester(data_a, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
	Effect *input1 = tester.get_chain()->last_added_effect();
	Effect *input2 = tester.add_input(data_b, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);

	Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
	ASSERT_TRUE(mix_effect->set_float("strength_first", 1.0f));
	ASSERT_TRUE(mix_effect->set_float("strength_second", 0.0f));
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);

	expect_equal(data_a, out_data, 2, 2);
}

TEST(MixEffectTest, DoesNotSumToOne) {
	float data_a[] = {
		1.0f, 0.5f, 0.75f, 0.333f,
	};
	float data_b[] = {
		1.0f, 0.25f, 0.15f, 0.333f,
	};

	// The fact that the RGB values don't sum but get averaged here might
	// actually be a surprising result, but when you think of it,
	// it does make physical sense.
	float expected_data[] = {
		1.0f, 0.375f, 0.45f, 0.666f,
	};

	float out_data[4];
	EffectChainTester tester(data_a, 1, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
	Effect *input1 = tester.get_chain()->last_added_effect();
	Effect *input2 = tester.add_input(data_b, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);

	Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
	ASSERT_TRUE(mix_effect->set_float("strength_first", 1.0f));
	ASSERT_TRUE(mix_effect->set_float("strength_second", 1.0f));
	tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);

	expect_equal(expected_data, out_data, 4, 1);
}

TEST(MixEffectTest, AdditiveBlendingWorksForBothTotallyOpaqueAndPartiallyTranslucent) {
	float data_a[] = {
		0.0f, 0.5f, 0.75f, 1.0f,
		1.0f, 1.0f, 1.0f, 0.2f,
	};
	float data_b[] = {
		1.0f, 0.25f, 0.15f, 1.0f,
		1.0f, 1.0f, 1.0f, 0.5f,
	};

	float expected_data[] = {
		1.0f, 0.75f, 0.9f, 1.0f,
		1.0f, 1.0f, 1.0f, 0.7f,
	};

	float out_data[8];
	EffectChainTester tester(data_a, 1, 2, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
	Effect *input1 = tester.get_chain()->last_added_effect();
	Effect *input2 = tester.add_input(data_b, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);

	Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
	ASSERT_TRUE(mix_effect->set_float("strength_first", 1.0f));
	ASSERT_TRUE(mix_effect->set_float("strength_second", 1.0f));
	tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);

	expect_equal(expected_data, out_data, 4, 2);
}

TEST(MixEffectTest, MixesLinearlyDespitesRGBInputsAndOutputs) {
	float data_a[] = {
		0.0f, 0.25f,
		0.75f, 1.0f,
	};
	float data_b[] = {
		0.0f, 0.0f,
		0.0f, 0.0f,
	};
	float expected_data[] = {  // sRGB(0.5 * inv_sRGB(a)).
		0.00000f, 0.17349f,
		0.54807f, 0.73536f,
	};
	float out_data[4];
	EffectChainTester tester(data_a, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
	Effect *input1 = tester.get_chain()->last_added_effect();
	Effect *input2 = tester.add_input(data_b, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);

	Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
	ASSERT_TRUE(mix_effect->set_float("strength_first", 0.5f));
	ASSERT_TRUE(mix_effect->set_float("strength_second", 0.5f));
	tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);

	expect_equal(expected_data, out_data, 2, 2);
}

}  // namespace movit