--- /dev/null
-#include <GL/glew.h>
++#include <epoxy/gl.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
--- /dev/null
-#include <GL/glew.h>
+ #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 <epoxy/gl.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)
--- /dev/null
-#include <GL/glew.h>
+ // Unit tests for ComplexModulateEffect.
+
++#include <epoxy/gl.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