From: Steinar H. Gunderson Date: Sat, 15 Mar 2014 18:13:38 +0000 (+0100) Subject: Support pad/crop from bottom, not just from the top. X-Git-Tag: 1.0~9 X-Git-Url: https://git.sesse.net/?p=movit;a=commitdiff_plain;h=e92a5ffa19eb67b4db5af1db8559630139073668 Support pad/crop from bottom, not just from the top. This is convenient for the FFTs that are coming. --- diff --git a/padding_effect.cpp b/padding_effect.cpp index 15381a3..a70025d 100644 --- a/padding_effect.cpp +++ b/padding_effect.cpp @@ -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] = { diff --git a/padding_effect.h b/padding_effect.h index 3bd4aac..fdefbe0 100644 --- a/padding_effect.h +++ b/padding_effect.h @@ -11,6 +11,10 @@ // 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 #include @@ -39,6 +43,7 @@ private: int input_width, input_height; int output_width, output_height; float top, left; + int pad_from_bottom; }; } // namespace movit diff --git a/padding_effect_test.cpp b/padding_effect_test.cpp index 767f3e8..8c05a1e 100644 --- a/padding_effect_test.cpp +++ b/padding_effect_test.cpp @@ -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,