]> git.sesse.net Git - movit/blob - padding_effect.cpp
Add some parameter asserts in DitherEffect, too.
[movit] / padding_effect.cpp
1 #include <math.h>
2 #include <GL/glew.h>
3
4 #include "padding_effect.h"
5 #include "util.h"
6
7 PaddingEffect::PaddingEffect()
8         : border_color(0.0f, 0.0f, 0.0f, 0.0f),
9           output_width(1280),
10           output_height(720),
11           top(0),
12           left(0)
13 {
14         register_vec4("border_color", (float *)&border_color);
15         register_int("width", &output_width);
16         register_int("height", &output_height);
17         register_float("top", &top);
18         register_float("left", &left);
19 }
20
21 std::string PaddingEffect::output_fragment_shader()
22 {
23         return read_file("padding_effect.frag");
24 }
25
26 void PaddingEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
27 {
28         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
29
30         float offset[2] = {
31                 left / output_width,
32                 (output_height - input_height - top) / output_height
33         };
34         set_uniform_vec2(glsl_program_num, prefix, "offset", offset);
35
36         float scale[2] = {
37                 float(output_width) / input_width,
38                 float(output_height) / input_height
39         };
40         set_uniform_vec2(glsl_program_num, prefix, "scale", scale);
41
42         // Due to roundoff errors, the test against 0.5 is seldom exact,
43         // even though we test for less than and not less-than-or-equal.
44         // We'd rather keep an extra border pixel in those very rare cases
45         // (where the image is shifted pretty much exactly a half-pixel)
46         // than losing a pixel in the common cases of integer shift.
47         // Thus the 1e-3 fudge factors.
48         float texcoord_min[2] = {
49                 (0.5f - 1e-3) / input_width,
50                 (0.5f - 1e-3) / input_height
51         };
52         set_uniform_vec2(glsl_program_num, prefix, "texcoord_min", texcoord_min);
53
54         float texcoord_max[2] = {
55                 1.0f - (0.5f - 1e-3) / input_width,
56                 1.0f - (0.5f - 1e-3) / input_height
57         };
58         set_uniform_vec2(glsl_program_num, prefix, "texcoord_max", texcoord_max);
59 }
60         
61 // We don't change the pixels of the image itself, so the only thing that 
62 // can make us less flexible is if the border color can be interpreted
63 // differently in different modes.
64
65 // 0.0 and 1.0 are interpreted the same, no matter the gamma ramp.
66 // Alpha is not affected by gamma.
67 bool PaddingEffect::needs_linear_light() const
68 {
69         if ((border_color.r == 0.0 || border_color.r == 1.0) &&
70             (border_color.g == 0.0 || border_color.g == 1.0) &&
71             (border_color.b == 0.0 || border_color.b == 1.0)) {
72                 return false;
73         }
74         return true;
75 }
76
77 // The white point is the same (D65) in all the color spaces we currently support,
78 // so any gray would be okay, but we don't really have a guarantee for that.
79 // Stay safe and say that only pure black and pure white is okay.
80 // Alpha is not affected by color space.
81 bool PaddingEffect::needs_srgb_primaries() const
82 {
83         if (border_color.r == 0.0 && border_color.g == 0.0 && border_color.b == 0.0) {
84                 return false;
85         }
86         if (border_color.r == 1.0 && border_color.g == 1.0 && border_color.b == 1.0) {
87                 return false;
88         }
89         return true;
90 }
91
92 // If the border color is black, it doesn't matter if we're pre- or postmultiplied
93 // (or even blank, as a hack). Otherwise, it does.
94 Effect::AlphaHandling PaddingEffect::alpha_handling() const
95 {
96         if (border_color.r == 0.0 && border_color.g == 0.0 && border_color.b == 0.0) {
97                 return DONT_CARE_ALPHA_TYPE;
98         }
99         return INPUT_AND_OUTPUT_ALPHA_PREMULTIPLIED;
100 }
101         
102 void PaddingEffect::get_output_size(unsigned *width, unsigned *height) const
103 {
104         *width = output_width;
105         *height = output_height;
106 }
107         
108 void PaddingEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
109 {
110         assert(input_num == 0);
111         input_width = width;
112         input_height = height;
113 }