]> git.sesse.net Git - movit/blob - padding_effect.cpp
Remove unused private members from FFTConvolutionEffect.
[movit] / padding_effect.cpp
1 #include <GL/glew.h>
2 #include <assert.h>
3
4 #include "effect_util.h"
5 #include "padding_effect.h"
6 #include "util.h"
7
8 using namespace std;
9
10 namespace movit {
11
12 PaddingEffect::PaddingEffect()
13         : border_color(0.0f, 0.0f, 0.0f, 0.0f),
14           output_width(1280),
15           output_height(720),
16           top(0),
17           left(0)
18 {
19         register_vec4("border_color", (float *)&border_color);
20         register_int("width", &output_width);
21         register_int("height", &output_height);
22         register_float("top", &top);
23         register_float("left", &left);
24 }
25
26 string PaddingEffect::output_fragment_shader()
27 {
28         return read_file("padding_effect.frag");
29 }
30
31 void PaddingEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
32 {
33         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
34
35         float offset[2] = {
36                 left / output_width,
37                 (output_height - input_height - top) / output_height
38         };
39         set_uniform_vec2(glsl_program_num, prefix, "offset", offset);
40
41         float scale[2] = {
42                 float(output_width) / input_width,
43                 float(output_height) / input_height
44         };
45         set_uniform_vec2(glsl_program_num, prefix, "scale", scale);
46
47         // Due to roundoff errors, the test against 0.5 is seldom exact,
48         // even though we test for less than and not less-than-or-equal.
49         // We'd rather keep an extra border pixel in those very rare cases
50         // (where the image is shifted pretty much exactly a half-pixel)
51         // than losing a pixel in the common cases of integer shift.
52         // Thus the 1e-3 fudge factors.
53         float texcoord_min[2] = {
54                 float((0.5f - 1e-3) / input_width),
55                 float((0.5f - 1e-3) / input_height)
56         };
57         set_uniform_vec2(glsl_program_num, prefix, "texcoord_min", texcoord_min);
58
59         float texcoord_max[2] = {
60                 float(1.0f - (0.5f - 1e-3) / input_width),
61                 float(1.0f - (0.5f - 1e-3) / input_height)
62         };
63         set_uniform_vec2(glsl_program_num, prefix, "texcoord_max", texcoord_max);
64 }
65         
66 // We don't change the pixels of the image itself, so the only thing that 
67 // can make us less flexible is if the border color can be interpreted
68 // differently in different modes.
69
70 // 0.0 and 1.0 are interpreted the same, no matter the gamma ramp.
71 // Alpha is not affected by gamma per se, but the combination of
72 // premultiplied alpha and non-linear gamma curve does not make sense,
73 // so if could possibly be converting blank alpha to non-blank
74 // (ie., premultiplied), we need our output to be in linear light.
75 bool PaddingEffect::needs_linear_light() const
76 {
77         if ((border_color.r == 0.0 || border_color.r == 1.0) &&
78             (border_color.g == 0.0 || border_color.g == 1.0) &&
79             (border_color.b == 0.0 || border_color.b == 1.0) &&
80             border_color.a == 1.0) {
81                 return false;
82         }
83         return true;
84 }
85
86 // The white point is the same (D65) in all the color spaces we currently support,
87 // so any gray would be okay, but we don't really have a guarantee for that.
88 // Stay safe and say that only pure black and pure white is okay.
89 // Alpha is not affected by color space.
90 bool PaddingEffect::needs_srgb_primaries() const
91 {
92         if (border_color.r == 0.0 && border_color.g == 0.0 && border_color.b == 0.0) {
93                 return false;
94         }
95         if (border_color.r == 1.0 && border_color.g == 1.0 && border_color.b == 1.0) {
96                 return false;
97         }
98         return true;
99 }
100
101 Effect::AlphaHandling PaddingEffect::alpha_handling() const
102 {
103         // If the border color is black, it doesn't matter if we're pre- or postmultiplied.
104         // Note that for non-solid black (i.e. alpha < 1.0), we're equally fine with
105         // pre- and postmultiplied, but later effects might change this status
106         // (consider e.g. blur), so setting DONT_CARE_ALPHA_TYPE is inappropriate,
107         // as it propagate blank alpha through this effect.
108         if (border_color.r == 0.0 && border_color.g == 0.0 && border_color.b == 0.0 && border_color.a == 1.0) {
109                 return DONT_CARE_ALPHA_TYPE;
110         }
111
112         // If the border color is solid, we preserve blank alpha, as we never output any
113         // new non-solid pixels.
114         if (border_color.a == 1.0) {
115                 return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK;
116         }
117
118         // Otherwise, we're going to output our border color in premultiplied alpha,
119         // so the other pixels better be premultiplied as well.
120         return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
121 }
122         
123 void PaddingEffect::get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const
124 {
125         *virtual_width = *width = output_width;
126         *virtual_height = *height = output_height;
127 }
128         
129 void PaddingEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
130 {
131         assert(input_num == 0);
132         input_width = width;
133         input_height = height;
134 }
135
136 }  // namespace movit