From: Steinar H. Gunderson Date: Sun, 31 Jan 2016 12:35:38 +0000 (+0100) Subject: In OverlayEffect, add support for swapping the inputs. X-Git-Tag: 1.3.0~1 X-Git-Url: https://git.sesse.net/?p=movit;a=commitdiff_plain;h=2906256567cb3899b615d45e7d8a8fb482572147 In OverlayEffect, add support for swapping the inputs. --- diff --git a/overlay_effect.cpp b/overlay_effect.cpp index 3a42240..3440f76 100644 --- a/overlay_effect.cpp +++ b/overlay_effect.cpp @@ -5,11 +5,17 @@ using namespace std; namespace movit { -OverlayEffect::OverlayEffect() {} +OverlayEffect::OverlayEffect() + : swap_inputs(false) +{ + register_int("swap_inputs", (int *)&swap_inputs); +} string OverlayEffect::output_fragment_shader() { - return read_file("overlay_effect.frag"); + char buf[256]; + snprintf(buf, sizeof(buf), "#define SWAP_INPUTS %d\n", swap_inputs); + return buf + read_file("overlay_effect.frag"); } } // namespace movit diff --git a/overlay_effect.frag b/overlay_effect.frag index 9e5709d..d32cc06 100644 --- a/overlay_effect.frag +++ b/overlay_effect.frag @@ -13,7 +13,15 @@ // C_o without the division by alpha_o). vec4 FUNCNAME(vec2 tc) { +// SWAP_INPUTS will be #defined to 1 if we want to swap the two inputs, +#if SWAP_INPUTS + vec4 bottom = INPUT2(tc); + vec4 top = INPUT1(tc); +#else vec4 bottom = INPUT1(tc); vec4 top = INPUT2(tc); +#endif return top + (1.0 - top.a) * bottom; } + +#undef SWAP_INPUTS diff --git a/overlay_effect.h b/overlay_effect.h index c11569f..40a6fff 100644 --- a/overlay_effect.h +++ b/overlay_effect.h @@ -32,6 +32,11 @@ public: // However, understanding that would require changes // to EffectChain, so postpone that optimization for later. virtual AlphaHandling alpha_handling() const { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; } + +private: + // If true, overlays input1 on top of input2 instead of vice versa. + // Must be set before finalize. + bool swap_inputs; }; } // namespace movit diff --git a/overlay_effect_test.cpp b/overlay_effect_test.cpp index dfda93f..3f7ffd3 100644 --- a/overlay_effect_test.cpp +++ b/overlay_effect_test.cpp @@ -8,27 +8,36 @@ #include "input.h" #include "overlay_effect.h" #include "test_util.h" +#include "util.h" namespace movit { TEST(OverlayEffectTest, TopDominatesBottomWhenNoAlpha) { - 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); - - tester.get_chain()->add_effect(new OverlayEffect(), input1, input2); - tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); - - expect_equal(data_b, out_data, 2, 2); + for (int swap_inputs = 0; swap_inputs < 2; ++swap_inputs) { // false, true. + 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); + + OverlayEffect *effect = new OverlayEffect(); + CHECK(effect->set_int("swap_inputs", swap_inputs)); + tester.get_chain()->add_effect(effect, input1, input2); + tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); + + if (swap_inputs) { + expect_equal(data_a, out_data, 2, 2); + } else { + expect_equal(data_b, out_data, 2, 2); + } + } } TEST(OverlayEffectTest, BottomDominatesTopWhenTopIsTransparent) {