Support pad/crop from bottom, not just from the top.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 15 Mar 2014 18:13:38 +0000 (19:13 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sat, 15 Mar 2014 18:13:38 +0000 (19:13 +0100)
This is convenient for the FFTs that are coming.

padding_effect.cpp
padding_effect.h
padding_effect_test.cpp

index 15381a3..a70025d 100644 (file)
@@ -14,13 +14,15 @@ PaddingEffect::PaddingEffect()
          output_width(1280),
          output_height(720),
          top(0),
-         left(0)
+         left(0),
+         pad_from_bottom(0)
 {
        register_vec4("border_color", (float *)&border_color);
        register_int("width", &output_width);
        register_int("height", &output_height);
        register_float("top", &top);
        register_float("left", &left);
+       register_int("pad_from_bottom", &pad_from_bottom);
 }
 
 string PaddingEffect::output_fragment_shader()
@@ -32,10 +34,13 @@ void PaddingEffect::set_gl_state(GLuint glsl_program_num, const string &prefix,
 {
        Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
 
-       float offset[2] = {
-               left / output_width,
-               (output_height - input_height - top) / output_height
-       };
+       float offset[2];
+       offset[0] = left / output_width;
+       if (pad_from_bottom) {
+               offset[1] = top / output_height;
+       } else {
+               offset[1] = (output_height - input_height - top) / output_height;
+       }
        set_uniform_vec2(glsl_program_num, prefix, "offset", offset);
 
        float scale[2] = {
index 3bd4aac..fdefbe0 100644 (file)
 // The border color is taken to be in linear gamma, sRGB, with premultiplied alpha.
 // You may not change it after calling finalize(), since that could change the
 // graph (need_linear_light() etc. depend on the border color you choose).
+//
+// As a convenience, if the flag “pad_from_bottom” is nonzero, the “top” parameter
+// will mean pixels from the bottom (matching OpenGL's usual bottom-left convention),
+// instead of from the top as usual.
 
 #include <GL/glew.h>
 #include <string>
@@ -39,6 +43,7 @@ private:
        int input_width, input_height;
        int output_width, output_height;
        float top, left;
+       int pad_from_bottom;
 };
 
 }  // namespace movit
index 767f3e8..8c05a1e 100644 (file)
@@ -208,6 +208,37 @@ TEST(PaddingEffectTest, Crop) {
        expect_equal(expected_data, out_data, 1, 1);
 }
 
+TEST(PaddingEffectTest, CropFromBottom) {
+       float data[2 * 2] = {
+               1.0f, 0.5f,
+               0.8f, 0.3f,
+       };
+       float expected_data[1 * 1] = {
+               0.5f,
+       };
+       float out_data[1 * 1];
+
+       EffectChainTester tester(NULL, 1, 1);
+
+       ImageFormat format;
+       format.color_space = COLORSPACE_sRGB;
+       format.gamma_curve = GAMMA_LINEAR;
+
+       FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, 2, 2);
+       input->set_pixel_data(data);
+       tester.get_chain()->add_input(input);
+
+       Effect *effect = tester.get_chain()->add_effect(new PaddingEffect());
+       CHECK(effect->set_int("width", 1));
+       CHECK(effect->set_int("height", 1));
+       CHECK(effect->set_float("left", -1.0f));
+       CHECK(effect->set_float("top", -1.0f));
+       CHECK(effect->set_int("pad_from_bottom", 1));
+
+       tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR, OUTPUT_ALPHA_FORMAT_PREMULTIPLIED);
+       expect_equal(expected_data, out_data, 1, 1);
+}
+
 TEST(PaddingEffectTest, AlphaIsCorrectEvenWithNonLinearInputsAndOutputs) {
        float data[2 * 1] = {
                1.0f,