--- /dev/null
+// If we didn't have to worry about alpha in the bottom layer,
+// this would be a simple mix(). However, since people might
+// compose multiple layers together and we don't really have
+// any control over the order, it's better to do it right.
+//
+// These formulas come from Wikipedia:
+//
+// http://en.wikipedia.org/wiki/Alpha_compositing
+
+vec4 FUNCNAME(vec2 tc) {
+ vec4 bottom = INPUT1(tc);
+ vec4 top = INPUT2(tc);
+ float new_alpha = mix(bottom.a, 1.0, top.a);
+ if (new_alpha < 1e-6) {
+ // new_alpha = 0 only if top.a = bottom.a = 0, at least as long as
+ // both alphas are within range. (If they're not, the result is not
+ // meaningful anyway.) And if new_alpha = 0, we don't really have
+ // any meaningful output anyway, so just set it to zero instead of
+ // getting division-by-zero below.
+ return vec4(0.0);
+ } else {
+ vec3 premultiplied_color = mix(bottom.rgb * bottom.aaa, top.rgb, top.a);
+ vec3 color = premultiplied_color / new_alpha;
+ return vec4(color.r, color.g, color.b, new_alpha);
+ }
+}
--- /dev/null
+#ifndef _OVERLAY_EFFECT_H
+#define _OVERLAY_EFFECT_H 1
+
+// Put one image on top of another, using alpha where appropriate.
+// (If both images are the same aspect and the top image has alpha=1.0
+// for all pixels, you will not see anything of the one on the bottom.)
+//
+// This is the “atop” operation from Porter-Duff blending, also used
+// when merging layers in e.g. GIMP or Photoshop.
+//
+// The first input is the bottom, and the second is the top.
+
+#include "effect.h"
+
+class OverlayEffect : public Effect {
+public:
+ OverlayEffect();
+ virtual std::string effect_type_id() const { return "OverlayEffect"; }
+ std::string output_fragment_shader();
+
+ virtual bool needs_srgb_primaries() const { return false; }
+ virtual unsigned num_inputs() const { return 2; }
+};
+
+#endif // !defined(_OVERLAY_EFFECT_H)
--- /dev/null
+// Unit tests for OverlayEffect.
+
+#include "test_util.h"
+#include "gtest/gtest.h"
+#include "overlay_effect.h"
+
+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);
+}
+
+TEST(OverlayEffectTest, BottomDominatesTopWhenTopIsTransparent) {
+ float data_a[] = {
+ 1.0f, 0.0f, 0.0f, 0.5f,
+ };
+ float data_b[] = {
+ 0.5f, 0.5f, 0.5f, 0.0f,
+ };
+ float out_data[4];
+ EffectChainTester tester(data_a, 1, 1, FORMAT_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+ Effect *input1 = tester.get_chain()->last_added_effect();
+ Effect *input2 = tester.add_input(data_b, FORMAT_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ tester.get_chain()->add_effect(new OverlayEffect(), input1, input2);
+ tester.run(out_data, GL_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ expect_equal(data_a, out_data, 4, 1);
+}
+
+TEST(OverlayEffectTest, ZeroAlphaBecomesAllZero) {
+ float data_a[] = {
+ 0.0f, 0.25f, 0.5f, 0.0f
+ };
+ float data_b[] = {
+ 1.0f, 1.0f, 1.0f, 0.0f
+ };
+ float expected_data[] = {
+ 0.0f, 0.0f, 0.0f, 0.0f
+ };
+ float out_data[4];
+ EffectChainTester tester(data_a, 1, 1, FORMAT_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+ Effect *input1 = tester.get_chain()->last_added_effect();
+ Effect *input2 = tester.add_input(data_b, FORMAT_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ tester.get_chain()->add_effect(new OverlayEffect(), input1, input2);
+ tester.run(out_data, GL_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ expect_equal(expected_data, out_data, 4, 1);
+}
+
+// This is tested against what Photoshop does: (255,0,128, 0.25) over (128,255,0, 0.5)
+// becomes (179,153,51, 0.63). (Actually we fudge 0.63 to 0.625, because that's
+// what it should be.)
+TEST(OverlayEffectTest, PhotoshopReferenceTest) {
+ float data_a[] = {
+ 128.0f/255.0f, 1.0f, 0.0f, 0.5f
+ };
+ float data_b[] = {
+ 1.0f, 0.0f, 128.0f/255.0f, 0.25f
+ };
+ float expected_data[] = {
+ 179.0f/255.0f, 153.0f/255.0f, 51.0f/255.0f, 0.625f
+ };
+ float out_data[4];
+ EffectChainTester tester(data_a, 1, 1, FORMAT_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+ Effect *input1 = tester.get_chain()->last_added_effect();
+ Effect *input2 = tester.add_input(data_b, FORMAT_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ tester.get_chain()->add_effect(new OverlayEffect(), input1, input2);
+ tester.run(out_data, GL_BGRA, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ expect_equal(expected_data, out_data, 4, 1);
+}