]> git.sesse.net Git - movit/blobdiff - padding_effect.cpp
Convert a loop to range-based for.
[movit] / padding_effect.cpp
index f576baa2d5c3f8cb043a0395cdd3d3314a7bdd90..48ad54ff3e83264cb6d2016a74e3753336e3074f 100644 (file)
@@ -14,13 +14,26 @@ PaddingEffect::PaddingEffect()
          output_width(1280),
          output_height(720),
          top(0),
-         left(0)
+         left(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_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);
+       register_uniform_vec2("offset", uniform_offset);
+       register_uniform_vec2("scale", uniform_scale);
+       register_uniform_vec2("normalized_coords_to_texels", uniform_normalized_coords_to_texels);
+       register_uniform_vec2("offset_bottomleft", uniform_offset_bottomleft);
+       register_uniform_vec2("offset_topright", uniform_offset_topright);
 }
 
 string PaddingEffect::output_fragment_shader()
@@ -32,35 +45,23 @@ 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
-       };
-       set_uniform_vec2(glsl_program_num, prefix, "offset", offset);
-
-       float scale[2] = {
-               float(output_width) / input_width,
-               float(output_height) / input_height
-       };
-       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)
-       };
-       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)
-       };
-       set_uniform_vec2(glsl_program_num, prefix, "texcoord_max", texcoord_max);
+       uniform_offset[0] = left / output_width;
+       uniform_offset[1] = (output_height - input_height - top) / output_height;
+
+       uniform_scale[0] = float(output_width) / input_width;
+       uniform_scale[1] = float(output_height) / input_height;
+
+       uniform_normalized_coords_to_texels[0] = float(input_width);
+       uniform_normalized_coords_to_texels[1] = float(input_height);
+
+       // Texels -0.5..0.5 should map to light level 0..1 (and then we
+       // clamp the rest).
+       uniform_offset_bottomleft[0] = 0.5f - border_offset_left;
+       uniform_offset_bottomleft[1] = 0.5f + border_offset_bottom;
+
+       // Texels size-0.5..size+0.5 should map to light level 1..0 (and then clamp).
+       uniform_offset_topright[0] = input_width + 0.5f + border_offset_right;
+       uniform_offset_topright[1] = input_height + 0.5f - border_offset_top;
 }
        
 // We don't change the pixels of the image itself, so the only thing that 
@@ -100,23 +101,16 @@ bool PaddingEffect::needs_srgb_primaries() const
 
 Effect::AlphaHandling PaddingEffect::alpha_handling() const
 {
-       // If the border color is black, it doesn't matter if we're pre- or postmultiplied.
-       // Note that for non-solid black (i.e. alpha < 1.0), we're equally fine with
-       // pre- and postmultiplied, but later effects might change this status
-       // (consider e.g. blur), so setting DONT_CARE_ALPHA_TYPE is inappropriate,
-       // as it propagate blank alpha through this effect.
-       if (border_color.r == 0.0 && border_color.g == 0.0 && border_color.b == 0.0 && border_color.a == 1.0) {
-               return DONT_CARE_ALPHA_TYPE;
-       }
-
-       // If the border color is solid, we preserve blank alpha, as we never output any
-       // new non-solid pixels.
+       // If the border color is solid, it doesn't matter if we're pre- or postmultiplied.
        if (border_color.a == 1.0) {
-               return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK;
+               return DONT_CARE_ALPHA_TYPE;
        }
 
        // Otherwise, we're going to output our border color in premultiplied alpha,
        // so the other pixels better be premultiplied as well.
+       // Note that for non-solid black (i.e. alpha < 1.0), we're equally fine with
+       // pre- and postmultiplied, but we are _not_ fine with blank being passed through,
+       // and we don't have a way to specify that.
        return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
 }
        
@@ -133,4 +127,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