]> git.sesse.net Git - movit/blobdiff - padding_effect.cpp
Make the PaddingEffect border 1-pixel soft.
[movit] / padding_effect.cpp
index a70025d015475f885a9841d96e40c08fe3ac762e..5ca976c2f9fca6d8d606a469e981456c7141c56a 100644 (file)
@@ -1,4 +1,4 @@
-#include <GL/glew.h>
+#include <epoxy/gl.h>
 #include <assert.h>
 
 #include "effect_util.h"
@@ -15,14 +15,20 @@ PaddingEffect::PaddingEffect()
          output_height(720),
          top(0),
          left(0),
-         pad_from_bottom(0)
+         border_offset_top(0.0f),
+         border_offset_left(0.0f),
+         border_offset_bottom(0.0f),
+         border_offset_right(0.0f)
 {
        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);
+       register_float("border_offset_top", &border_offset_top);
+       register_float("border_offset_left", &border_offset_left);
+       register_float("border_offset_bottom", &border_offset_bottom);
+       register_float("border_offset_right", &border_offset_right);
 }
 
 string PaddingEffect::output_fragment_shader()
@@ -34,13 +40,10 @@ 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];
-       offset[0] = left / output_width;
-       if (pad_from_bottom) {
-               offset[1] = top / output_height;
-       } else {
-               offset[1] = (output_height - input_height - top) / output_height;
-       }
+       float offset[2] = {
+               left / output_width,
+               (output_height - input_height - top) / output_height
+       };
        set_uniform_vec2(glsl_program_num, prefix, "offset", offset);
 
        float scale[2] = {
@@ -49,23 +52,24 @@ void PaddingEffect::set_gl_state(GLuint glsl_program_num, const string &prefix,
        };
        set_uniform_vec2(glsl_program_num, prefix, "scale", scale);
 
-       // Due to roundoff errors, the test against 0.5 is seldom exact,
-       // even though we test for less than and not less-than-or-equal.
-       // We'd rather keep an extra border pixel in those very rare cases
-       // (where the image is shifted pretty much exactly a half-pixel)
-       // than losing a pixel in the common cases of integer shift.
-       // Thus the 1e-3 fudge factors.
-       float texcoord_min[2] = {
-               float((0.5f - 1e-3) / input_width),
-               float((0.5f - 1e-3) / input_height)
+       float normalized_coords_to_texels[2] = {
+               float(input_width), float(input_height)
+       };
+       set_uniform_vec2(glsl_program_num, prefix, "normalized_coords_to_texels", normalized_coords_to_texels);
+
+       // Texels -0.5..0.5 should map to light level 0..1 (and then we
+       // clamp the rest).
+       float offset_bottomleft[2] = {
+               0.5f - border_offset_left, 0.5f + border_offset_bottom,
        };
-       set_uniform_vec2(glsl_program_num, prefix, "texcoord_min", texcoord_min);
 
-       float texcoord_max[2] = {
-               float(1.0f - (0.5f - 1e-3) / input_width),
-               float(1.0f - (0.5f - 1e-3) / input_height)
+       // Texels size-0.5..size+0.5 should map to light level 1..0 (and then clamp).
+       float offset_topright[2] = {
+               input_width + 0.5f + border_offset_right, input_height + 0.5f - border_offset_top,
        };
-       set_uniform_vec2(glsl_program_num, prefix, "texcoord_max", texcoord_max);
+
+       set_uniform_vec2(glsl_program_num, prefix, "offset_bottomleft", offset_bottomleft);
+       set_uniform_vec2(glsl_program_num, prefix, "offset_topright", offset_topright);
 }
        
 // We don't change the pixels of the image itself, so the only thing that 
@@ -138,4 +142,25 @@ void PaddingEffect::inform_input_size(unsigned input_num, unsigned width, unsign
        input_height = height;
 }
 
+IntegralPaddingEffect::IntegralPaddingEffect() {}
+
+bool IntegralPaddingEffect::set_int(const std::string &key, int value)
+{
+       if (key == "top" || key == "left") {
+               return PaddingEffect::set_float(key, value);
+       } else {
+               return PaddingEffect::set_int(key, value);
+       }
+}
+
+bool IntegralPaddingEffect::set_float(const std::string &key, float value)
+{
+       if (key == "top" || key == "left") {
+               // These are removed as float parameters from this version.
+               return false;
+       } else {
+               return PaddingEffect::set_float(key, value);
+       }
+}
+
 }  // namespace movit