Added an overlay effect, implementing the atop effect.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 12 Jan 2013 20:50:22 +0000 (21:50 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 12 Jan 2013 20:50:22 +0000 (21:50 +0100)
Makefile
overlay_effect.cpp [new file with mode: 0644]
overlay_effect.frag [new file with mode: 0644]
overlay_effect.h [new file with mode: 0644]
overlay_effect_test.cpp [new file with mode: 0644]

index 8396ac6..3d0a87c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -31,6 +31,7 @@ DEMO_OBJS=demo.o
 # Unit tests.
 TESTS=effect_chain_test
 TESTS += mix_effect_test
+TESTS += overlay_effect_test
 TESTS += gamma_expansion_effect_test
 TESTS += gamma_compression_effect_test
 TESTS += colorspace_conversion_effect_test
@@ -67,6 +68,7 @@ LIB_OBJS += diffusion_effect.o
 LIB_OBJS += glow_effect.o
 LIB_OBJS += unsharp_mask_effect.o
 LIB_OBJS += mix_effect.o
+LIB_OBJS += overlay_effect.o
 LIB_OBJS += resize_effect.o
 LIB_OBJS += resample_effect.o
 LIB_OBJS += dither_effect.o
diff --git a/overlay_effect.cpp b/overlay_effect.cpp
new file mode 100644 (file)
index 0000000..799c359
--- /dev/null
@@ -0,0 +1,9 @@
+#include "overlay_effect.h"
+#include "util.h"
+
+OverlayEffect::OverlayEffect() {}
+
+std::string OverlayEffect::output_fragment_shader()
+{
+       return read_file("overlay_effect.frag");
+}
diff --git a/overlay_effect.frag b/overlay_effect.frag
new file mode 100644 (file)
index 0000000..38970ea
--- /dev/null
@@ -0,0 +1,26 @@
+// 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);
+       }
+}
diff --git a/overlay_effect.h b/overlay_effect.h
new file mode 100644 (file)
index 0000000..06ff793
--- /dev/null
@@ -0,0 +1,25 @@
+#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)
diff --git a/overlay_effect_test.cpp b/overlay_effect_test.cpp
new file mode 100644 (file)
index 0000000..009a757
--- /dev/null
@@ -0,0 +1,88 @@
+// 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);
+}