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