4 #include "effect_util.h"
5 #include "padding_effect.h"
10 PaddingEffect::PaddingEffect()
11 : border_color(0.0f, 0.0f, 0.0f, 0.0f),
17 register_vec4("border_color", (float *)&border_color);
18 register_int("width", &output_width);
19 register_int("height", &output_height);
20 register_float("top", &top);
21 register_float("left", &left);
24 string PaddingEffect::output_fragment_shader()
26 return read_file("padding_effect.frag");
29 void PaddingEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
31 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
35 (output_height - input_height - top) / output_height
37 set_uniform_vec2(glsl_program_num, prefix, "offset", offset);
40 float(output_width) / input_width,
41 float(output_height) / input_height
43 set_uniform_vec2(glsl_program_num, prefix, "scale", scale);
45 // Due to roundoff errors, the test against 0.5 is seldom exact,
46 // even though we test for less than and not less-than-or-equal.
47 // We'd rather keep an extra border pixel in those very rare cases
48 // (where the image is shifted pretty much exactly a half-pixel)
49 // than losing a pixel in the common cases of integer shift.
50 // Thus the 1e-3 fudge factors.
51 float texcoord_min[2] = {
52 float((0.5f - 1e-3) / input_width),
53 float((0.5f - 1e-3) / input_height)
55 set_uniform_vec2(glsl_program_num, prefix, "texcoord_min", texcoord_min);
57 float texcoord_max[2] = {
58 float(1.0f - (0.5f - 1e-3) / input_width),
59 float(1.0f - (0.5f - 1e-3) / input_height)
61 set_uniform_vec2(glsl_program_num, prefix, "texcoord_max", texcoord_max);
64 // We don't change the pixels of the image itself, so the only thing that
65 // can make us less flexible is if the border color can be interpreted
66 // differently in different modes.
68 // 0.0 and 1.0 are interpreted the same, no matter the gamma ramp.
69 // Alpha is not affected by gamma per se, but the combination of
70 // premultiplied alpha and non-linear gamma curve does not make sense,
71 // so if could possibly be converting blank alpha to non-blank
72 // (ie., premultiplied), we need our output to be in linear light.
73 bool PaddingEffect::needs_linear_light() const
75 if ((border_color.r == 0.0 || border_color.r == 1.0) &&
76 (border_color.g == 0.0 || border_color.g == 1.0) &&
77 (border_color.b == 0.0 || border_color.b == 1.0) &&
78 border_color.a == 1.0) {
84 // The white point is the same (D65) in all the color spaces we currently support,
85 // so any gray would be okay, but we don't really have a guarantee for that.
86 // Stay safe and say that only pure black and pure white is okay.
87 // Alpha is not affected by color space.
88 bool PaddingEffect::needs_srgb_primaries() const
90 if (border_color.r == 0.0 && border_color.g == 0.0 && border_color.b == 0.0) {
93 if (border_color.r == 1.0 && border_color.g == 1.0 && border_color.b == 1.0) {
99 Effect::AlphaHandling PaddingEffect::alpha_handling() const
101 // If the border color is black, it doesn't matter if we're pre- or postmultiplied.
102 // Note that for non-solid black (i.e. alpha < 1.0), we're equally fine with
103 // pre- and postmultiplied, but later effects might change this status
104 // (consider e.g. blur), so setting DONT_CARE_ALPHA_TYPE is inappropriate,
105 // as it propagate blank alpha through this effect.
106 if (border_color.r == 0.0 && border_color.g == 0.0 && border_color.b == 0.0 && border_color.a == 1.0) {
107 return DONT_CARE_ALPHA_TYPE;
110 // If the border color is solid, we preserve blank alpha, as we never output any
111 // new non-solid pixels.
112 if (border_color.a == 1.0) {
113 return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK;
116 // Otherwise, we're going to output our border color in premultiplied alpha,
117 // so the other pixels better be premultiplied as well.
118 return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
121 void PaddingEffect::get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const
123 *virtual_width = *width = output_width;
124 *virtual_height = *height = output_height;
127 void PaddingEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
129 assert(input_num == 0);
131 input_height = height;