1 // Unit tests for MixEffect.
5 #include "effect_chain.h"
6 #include "gtest/gtest.h"
7 #include "image_format.h"
9 #include "mix_effect.h"
10 #include "test_util.h"
14 TEST(MixEffectTest, FiftyFiftyMix) {
23 float expected_data[] = {
28 EffectChainTester tester(data_a, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
29 Effect *input1 = tester.get_chain()->last_added_effect();
30 Effect *input2 = tester.add_input(data_b, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
32 Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
33 ASSERT_TRUE(mix_effect->set_float("strength_first", 0.5f));
34 ASSERT_TRUE(mix_effect->set_float("strength_second", 0.5f));
35 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
37 expect_equal(expected_data, out_data, 2, 2);
40 TEST(MixEffectTest, OnlyA) {
50 EffectChainTester tester(data_a, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
51 Effect *input1 = tester.get_chain()->last_added_effect();
52 Effect *input2 = tester.add_input(data_b, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
54 Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
55 ASSERT_TRUE(mix_effect->set_float("strength_first", 1.0f));
56 ASSERT_TRUE(mix_effect->set_float("strength_second", 0.0f));
57 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
59 expect_equal(data_a, out_data, 2, 2);
62 TEST(MixEffectTest, DoesNotSumToOne) {
64 1.0f, 0.5f, 0.75f, 0.333f,
67 1.0f, 0.25f, 0.15f, 0.333f,
70 // The fact that the RGB values don't sum but get averaged here might
71 // actually be a surprising result, but when you think of it,
72 // it does make physical sense.
73 float expected_data[] = {
74 1.0f, 0.375f, 0.45f, 0.666f,
78 EffectChainTester tester(data_a, 1, 1, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
79 Effect *input1 = tester.get_chain()->last_added_effect();
80 Effect *input2 = tester.add_input(data_b, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
82 Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
83 ASSERT_TRUE(mix_effect->set_float("strength_first", 1.0f));
84 ASSERT_TRUE(mix_effect->set_float("strength_second", 1.0f));
85 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
87 expect_equal(expected_data, out_data, 4, 1);
90 TEST(MixEffectTest, AdditiveBlendingWorksForBothTotallyOpaqueAndPartiallyTranslucent) {
92 0.0f, 0.5f, 0.75f, 1.0f,
93 1.0f, 1.0f, 1.0f, 0.2f,
96 1.0f, 0.25f, 0.15f, 1.0f,
97 1.0f, 1.0f, 1.0f, 0.5f,
100 float expected_data[] = {
101 1.0f, 0.75f, 0.9f, 1.0f,
102 1.0f, 1.0f, 1.0f, 0.7f,
106 EffectChainTester tester(data_a, 1, 2, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
107 Effect *input1 = tester.get_chain()->last_added_effect();
108 Effect *input2 = tester.add_input(data_b, FORMAT_RGBA_POSTMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
110 Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
111 ASSERT_TRUE(mix_effect->set_float("strength_first", 1.0f));
112 ASSERT_TRUE(mix_effect->set_float("strength_second", 1.0f));
113 tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR);
115 expect_equal(expected_data, out_data, 4, 2);
118 TEST(MixEffectTest, MixesLinearlyDespitesRGBInputsAndOutputs) {
127 float expected_data[] = { // sRGB(0.5 * inv_sRGB(a)).
132 EffectChainTester tester(data_a, 2, 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
133 Effect *input1 = tester.get_chain()->last_added_effect();
134 Effect *input2 = tester.add_input(data_b, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_sRGB);
136 Effect *mix_effect = tester.get_chain()->add_effect(new MixEffect(), input1, input2);
137 ASSERT_TRUE(mix_effect->set_float("strength_first", 0.5f));
138 ASSERT_TRUE(mix_effect->set_float("strength_second", 0.5f));
139 tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_sRGB);
141 expect_equal(expected_data, out_data, 2, 2);