Add an effect for complex multiplication.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 10 Mar 2014 23:33:58 +0000 (00:33 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 10 Mar 2014 23:33:58 +0000 (00:33 +0100)
This is another building block to make the FFTs useful.

Makefile.in
complex_modulate_effect.cpp [new file with mode: 0644]
complex_modulate_effect.frag [new file with mode: 0644]
complex_modulate_effect.h [new file with mode: 0644]
complex_modulate_effect_test.cpp [new file with mode: 0644]

index db9628d..3a1674b 100644 (file)
@@ -56,6 +56,7 @@ TESTED_EFFECTS += deconvolution_sharpen_effect
 TESTED_EFFECTS += fft_pass_effect
 TESTED_EFFECTS += vignette_effect
 TESTED_EFFECTS += slice_effect
+TESTED_EFFECTS += complex_modulate_effect
 
 UNTESTED_EFFECTS = sandbox_effect
 UNTESTED_EFFECTS += mirror_effect
diff --git a/complex_modulate_effect.cpp b/complex_modulate_effect.cpp
new file mode 100644 (file)
index 0000000..7048608
--- /dev/null
@@ -0,0 +1,60 @@
+#include <GL/glew.h>
+
+#include "complex_modulate_effect.h"
+#include "effect_chain.h"
+#include "effect_util.h"
+#include "util.h"
+
+using namespace std;
+
+namespace movit {
+
+ComplexModulateEffect::ComplexModulateEffect()
+       : num_repeats_x(1), num_repeats_y(1)
+{
+       register_int("num_repeats_x", &num_repeats_x);
+       register_int("num_repeats_y", &num_repeats_y);
+}
+
+string ComplexModulateEffect::output_fragment_shader()
+{
+       return read_file("complex_modulate_effect.frag");
+}
+
+void ComplexModulateEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
+{
+       Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
+
+       float num_repeats[] = { num_repeats_x, num_repeats_y };
+       set_uniform_vec2(glsl_program_num, prefix, "num_repeats", num_repeats);
+
+       // Set the secondary input to repeat (and nearest while we're at it).
+       Node *self = chain->find_node_for_effect(this);
+       glActiveTexture(chain->get_input_sampler(self, 1));
+       check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+       check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+       check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+       check_error();
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+       check_error();
+}
+
+void ComplexModulateEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
+{
+       if (input_num == 0) {
+               primary_input_width = width;
+               primary_input_height = height;
+       }
+}
+
+void ComplexModulateEffect::get_output_size(unsigned *width, unsigned *height,
+                                            unsigned *virtual_width, unsigned *virtual_height) const
+{
+       *width = *virtual_width = primary_input_width;
+       *height = *virtual_height = primary_input_height;
+}
+
+}  // namespace movit
diff --git a/complex_modulate_effect.frag b/complex_modulate_effect.frag
new file mode 100644 (file)
index 0000000..46de5da
--- /dev/null
@@ -0,0 +1,9 @@
+uniform vec2 PREFIX(num_repeats);
+
+vec4 FUNCNAME(vec2 tc) {
+       vec4 pixel = INPUT1(tc);
+       vec2 pattern = INPUT2(tc * PREFIX(num_repeats)).xy;
+
+       // Complex multiplication between each of (pixel.xy, pixel.zw) and pattern.xy.
+       return pattern.x * pixel + pattern.y * vec4(-pixel.y, pixel.x, -pixel.w, pixel.z);
+}
diff --git a/complex_modulate_effect.h b/complex_modulate_effect.h
new file mode 100644 (file)
index 0000000..52de229
--- /dev/null
@@ -0,0 +1,61 @@
+#ifndef _MOVIT_COMPLEX_MODULATE_EFFECT_H
+#define _MOVIT_COMPLEX_MODULATE_EFFECT_H 1
+
+// An effect that treats each pixel as two complex numbers (xy and zw),
+// and multiplies it with some other complex number (xy and xy, so the
+// same in both cases). The latter can be repeated both horizontally and
+// vertically if desired.
+//
+// The typical use is to implement convolution by way of FFT; since
+// FFT(A ⊙ B) = FFT(A) * FFT(B), you can FFT both inputs (where B
+// would often even be a constant, so you'd only need to do FFT once),
+// multiply them together and then IFFT the result to get a convolution.
+//
+// It is in a sense “wrong” to do this directly on pixels, since the color
+// channels are independent and real-valued (ie., not complex numbers), but
+// since convolution is a linear operation, it's unproblematic to treat R + Gi
+// as a single complex number and B + Ai and another one; barring numerical
+// errors, there should be no leakage between the channels as long as you're
+// convolving with a real quantity. (There are more sophisticated ways of doing
+// two real FFTs with a single complex one, but we won't need them, as we
+// don't care about the actual FFT result, just that the convolution property
+// holds.)
+
+#include <GL/glew.h>
+#include <string>
+
+#include "effect.h"
+
+namespace movit {
+
+class EffectChain;
+
+class ComplexModulateEffect : public Effect {
+public:
+       ComplexModulateEffect();
+       virtual std::string effect_type_id() const { return "ComplexModulateEffect"; }
+       std::string output_fragment_shader();
+
+       // Technically we only need texture bounce for the second input
+       // (to be allowed to mess with its sampler state), but there's
+       // no way of expressing that currently.
+       virtual bool needs_texture_bounce() const { return true; }
+       virtual bool changes_output_size() const { return true; }
+
+       virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
+       virtual void get_output_size(unsigned *width, unsigned *height,
+                                    unsigned *virtual_width, unsigned *virtual_height) const;
+       virtual unsigned num_inputs() const { return 2; }
+       virtual void inform_added(EffectChain *chain) { this->chain = chain; }
+
+       void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
+
+private:
+       EffectChain *chain;
+       int primary_input_width, primary_input_height;
+       int num_repeats_x, num_repeats_y;
+};
+
+}  // namespace movit
+
+#endif // !defined(_MOVIT_COMPLEX_MODULATE_EFFECT_H)
diff --git a/complex_modulate_effect_test.cpp b/complex_modulate_effect_test.cpp
new file mode 100644 (file)
index 0000000..8c49d8a
--- /dev/null
@@ -0,0 +1,99 @@
+// Unit tests for ComplexModulateEffect.
+
+#include <GL/glew.h>
+
+#include "effect_chain.h"
+#include "gtest/gtest.h"
+#include "complex_modulate_effect.h"
+#include "image_format.h"
+#include "input.h"
+#include "test_util.h"
+
+namespace movit {
+
+TEST(ComplexModulateEffectTest, Identity) {
+       const int size = 3;
+       float data_a[size * 4] = {
+               0.0f, 0.1f, 0.2f, 0.1f,
+               0.4f, 0.3f, 0.8f, 2.0f,
+               0.5f, 0.2f, 0.1f, 0.0f,
+       };
+       float data_b[size * 2] = {
+               1.0f, 0.0f,
+               1.0f, 0.0f,
+               1.0f, 0.0f,
+       };
+       float out_data[size * 4];
+
+       EffectChainTester tester(data_a, 1, size, FORMAT_RGBA_PREMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
+       Effect *input1 = tester.get_chain()->last_added_effect();
+       Effect *input2 = tester.add_input(data_b, FORMAT_RG, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       tester.get_chain()->add_effect(new ComplexModulateEffect(), input1, input2);
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
+
+       expect_equal(data_a, out_data, 4, size);
+}
+
+TEST(ComplexModulateEffectTest, ComplexMultiplication) {
+       const int size = 2;
+       float data_a[size * 4] = {
+               0.0f, 0.1f, 0.2f, 0.1f,
+               0.4f, 0.3f, 0.8f, 2.0f,
+       };
+       float data_b[size * 2] = {
+               0.0f,  1.0f,
+               0.5f, -0.8f,
+       };
+       float expected_data[size * 4] = {
+               -0.1f,   0.0f,  -0.1f, 0.2f,
+                0.44f, -0.17f,  2.0f, 0.36f,
+       };
+       float out_data[size * 4];
+
+       EffectChainTester tester(data_a, 1, size, FORMAT_RGBA_PREMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
+       Effect *input1 = tester.get_chain()->last_added_effect();
+       Effect *input2 = tester.add_input(data_b, FORMAT_RG, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+       tester.get_chain()->add_effect(new ComplexModulateEffect(), input1, input2);
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
+
+       expect_equal(expected_data, out_data, 4, size);
+}
+
+TEST(ComplexModulateEffectTest, Repeat) {
+       const int size = 2, repeats = 3;
+       float data_a[size * repeats * 4] = {
+               0.0f, 0.1f, 0.2f, 0.3f,
+               1.0f, 1.1f, 1.2f, 1.3f,
+               2.0f, 2.1f, 2.2f, 2.3f,
+               3.0f, 3.1f, 3.2f, 3.3f,
+               4.0f, 4.1f, 4.2f, 4.3f,
+               5.0f, 5.1f, 5.2f, 5.3f,
+       };
+       float data_b[size * 2] = {
+               1.0f,  0.0f,
+               0.0f, -1.0f,
+       };
+       float expected_data[size * repeats * 4] = {
+               0.0f,  0.1f, 0.2f,  0.3f,
+               1.1f, -1.0f, 1.3f, -1.2f,
+               2.0f,  2.1f, 2.2f,  2.3f,
+               3.1f, -3.0f, 3.3f, -3.2f,
+               4.0f,  4.1f, 4.2f,  4.3f,
+               5.1f, -5.0f, 5.3f, -5.2f,
+       };
+       float out_data[size * repeats * 4];
+
+       EffectChainTester tester(data_a, 1, repeats * size, FORMAT_RGBA_PREMULTIPLIED_ALPHA, COLORSPACE_sRGB, GAMMA_LINEAR);
+       Effect *input1 = tester.get_chain()->last_added_effect();
+       Effect *input2 = tester.add_input(data_b, FORMAT_RG, COLORSPACE_sRGB, GAMMA_LINEAR, 1, size);
+
+       Effect *effect = tester.get_chain()->add_effect(new ComplexModulateEffect(), input1, input2);
+       ASSERT_TRUE(effect->set_int("num_repeats_y", repeats));
+       tester.run(out_data, GL_RGBA, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
+
+       expect_equal(expected_data, out_data, 4, size * repeats);
+}
+
+}  // namespace movit