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