1 // Three-lobed Lanczos, the most common choice.
2 #define LANCZOS_RADIUS 3.0
7 #include "resample_effect.h"
8 #include "effect_chain.h"
17 return 1.0f - fabs(x);
23 float lanczos_weight(float x, float a)
28 return sinc(M_PI * x) * sinc(M_PI * x / a);
34 ResampleEffect::ResampleEffect()
38 register_int("width", &output_width);
39 register_int("height", &output_height);
41 // The first blur pass will forward resolution information to us.
42 hpass = new SingleResamplePassEffect(this);
43 hpass->set_int("direction", SingleResamplePassEffect::HORIZONTAL);
44 vpass = new SingleResamplePassEffect(NULL);
45 vpass->set_int("direction", SingleResamplePassEffect::VERTICAL);
50 void ResampleEffect::rewrite_graph(EffectChain *graph, Node *self)
52 Node *hpass_node = graph->add_node(hpass);
53 Node *vpass_node = graph->add_node(vpass);
54 graph->connect_nodes(hpass_node, vpass_node);
55 graph->replace_receiver(self, hpass_node);
56 graph->replace_sender(self, vpass_node);
57 self->disabled = true;
60 // We get this information forwarded from the first blur pass,
61 // since we are not part of the chain ourselves.
62 void ResampleEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
64 assert(input_num == 0);
68 input_height = height;
72 void ResampleEffect::update_size()
75 ok |= hpass->set_int("input_width", input_width);
76 ok |= hpass->set_int("input_height", input_height);
77 ok |= hpass->set_int("output_width", output_width);
78 ok |= hpass->set_int("output_height", input_height);
80 ok |= vpass->set_int("input_width", output_width);
81 ok |= vpass->set_int("input_height", input_height);
82 ok |= vpass->set_int("output_width", output_width);
83 ok |= vpass->set_int("output_height", output_height);
88 bool ResampleEffect::set_float(const std::string &key, float value) {
94 if (key == "height") {
95 output_height = value;
102 SingleResamplePassEffect::SingleResamplePassEffect(ResampleEffect *parent)
104 direction(HORIZONTAL),
107 last_input_width(-1),
108 last_input_height(-1),
109 last_output_width(-1),
110 last_output_height(-1)
112 register_int("direction", (int *)&direction);
113 register_int("input_width", &input_width);
114 register_int("input_height", &input_height);
115 register_int("output_width", &output_width);
116 register_int("output_height", &output_height);
118 glGenTextures(1, &texnum);
121 SingleResamplePassEffect::~SingleResamplePassEffect()
123 glDeleteTextures(1, &texnum);
126 std::string SingleResamplePassEffect::output_fragment_shader()
129 sprintf(buf, "#define DIRECTION_VERTICAL %d\n", (direction == VERTICAL));
130 return buf + read_file("resample_effect.frag");
133 // Using vertical scaling as an example:
135 // Generally out[y] = w0 * in[yi] + w1 * in[yi + 1] + w2 * in[yi + 2] + ...
137 // Obviously, yi will depend on y (in a not-quite-linear way), but so will
138 // the weights w0, w1, w2, etc.. The easiest way of doing this is to encode,
139 // for each sample, the weight and the yi value, e.g. <yi, w0>, <yi + 1, w1>,
140 // and so on. For each y, we encode these along the x-axis (since that is spare),
141 // so out[0] will read from parameters <x,y> = <0,0>, <1,0>, <2,0> and so on.
143 // For horizontal scaling, we fill in the exact same texture;
144 // the shader just interprets is differently.
146 // TODO: Support optimization of wrapping the sample texture.
147 // TODO: Support optimization using free linear sampling, like in BlurEffect.
148 void SingleResamplePassEffect::update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
150 unsigned src_size, dst_size;
151 if (direction == SingleResamplePassEffect::HORIZONTAL) {
152 assert(input_height == output_height);
153 src_size = input_width;
154 dst_size = output_width;
155 } else if (direction == SingleResamplePassEffect::VERTICAL) {
156 assert(input_width == output_width);
157 src_size = input_height;
158 dst_size = output_height;
163 // Sample the kernel in the right place. A diagram with a triangular kernel
164 // (corresponding to linear filtering, and obviously with radius 1)
165 // for easier ASCII art drawing:
171 // x---x---x x x---x---x---x
173 // Scaling up (in this case, 2x) means sampling more densely:
179 // x-x-x-x-x-x x x x-x-x-x-x-x-x-x
181 // When scaling up, any destination pixel will only be influenced by a few
182 // (in this case, two) neighboring pixels, and more importantly, the number
183 // will not be influenced by the scaling factor. (Note, however, that the
184 // pixel centers have moved, due to OpenGL's center-pixel convention.)
185 // The only thing that changes is the weights themselves, as the sampling
186 // points are at different distances from the original pixels.
188 // Scaling down is a different story:
194 // --x------ x --x-------x--
196 // Again, the pixel centers have moved in a maybe unintuitive fashion,
197 // although when you consider that there are multiple source pixels around,
198 // it's not so bad as at first look:
204 // --x-------x-------x-------x--
206 // As you can see, the new pixels become averages of the two neighboring old
207 // ones (the situation for Lanczos is of course more complex).
209 // Anyhow, in this case we clearly need to look at more source pixels
210 // to compute the destination pixel, and how many depend on the scaling factor.
211 // Thus, the kernel width will vary with how much we scale.
212 float radius_scaling_factor = std::min(float(dst_size) / float(src_size), 1.0f);
213 int int_radius = lrintf(LANCZOS_RADIUS / radius_scaling_factor);
214 src_samples = int_radius * 2 + 1;
215 float *weights = new float[dst_size * src_samples * 2];
216 for (unsigned y = 0; y < dst_size; ++y) {
217 // Find the point around which we want to sample the source image,
218 // compensating for differing pixel centers as the scale changes.
219 float center_src_y = (y + 0.5f) * float(src_size) / float(dst_size) - 0.5f;
220 int base_src_y = lrintf(center_src_y);
222 // Now sample <int_radius> pixels on each side around that point.
223 for (int i = 0; i < src_samples; ++i) {
224 int src_y = base_src_y + i - int_radius;
225 float weight = lanczos_weight(radius_scaling_factor * (src_y - center_src_y), LANCZOS_RADIUS);
226 weights[(y * src_samples + i) * 2 + 0] = weight * radius_scaling_factor;
227 weights[(y * src_samples + i) * 2 + 1] = (src_y + 0.5) / float(src_size);
231 // Encode as a two-component texture. Note the GL_REPEAT, which is not relevant
232 // right now, but will be later.
233 glActiveTexture(GL_TEXTURE0 + *sampler_num);
235 glBindTexture(GL_TEXTURE_2D, texnum);
237 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
239 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
241 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
243 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16F, src_samples, dst_size, 0, GL_RG, GL_FLOAT, weights);
250 void SingleResamplePassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
252 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
254 if (input_width != last_input_width ||
255 input_height != last_input_height ||
256 output_width != last_output_width ||
257 output_height != last_output_height) {
258 update_texture(glsl_program_num, prefix, sampler_num);
259 last_input_width = input_width;
260 last_input_height = input_height;
261 last_output_width = output_width;
262 last_output_height = output_height;
265 glActiveTexture(GL_TEXTURE0 + *sampler_num);
267 glBindTexture(GL_TEXTURE_2D, texnum);
270 set_uniform_int(glsl_program_num, prefix, "sample_tex", *sampler_num);
272 set_uniform_int(glsl_program_num, prefix, "num_samples", src_samples);
274 // Instructions for how to convert integer sample numbers to positions in the weight texture.
275 set_uniform_float(glsl_program_num, prefix, "sample_x_scale", 1.0f / src_samples);
276 set_uniform_float(glsl_program_num, prefix, "sample_x_offset", 0.5f / src_samples);
278 // We specifically do not want mipmaps on the input texture;
279 // they break minification.
280 glActiveTexture(GL_TEXTURE0);
282 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);