update_size();
return true;
}
+ if (key == "top") {
+ // Compensate for the bottom-left origin.
+ return vpass->set_float("offset", -value);
+ }
+ if (key == "left") {
+ return hpass->set_float("offset", value);
+ }
return false;
}
direction(HORIZONTAL),
input_width(1280),
input_height(720),
+ offset(0.0),
last_input_width(-1),
last_input_height(-1),
last_output_width(-1),
- last_output_height(-1)
+ last_output_height(-1),
+ last_offset(0.0 / 0.0) // NaN.
{
register_int("direction", (int *)&direction);
register_int("input_width", &input_width);
register_int("input_height", &input_height);
register_int("output_width", &output_width);
register_int("output_height", &output_height);
+ register_float("offset", &offset);
glGenTextures(1, &texnum);
}
int int_radius = lrintf(LANCZOS_RADIUS / radius_scaling_factor);
int src_samples = int_radius * 2 + 1;
float *weights = new float[dst_samples * src_samples * 2];
+ float subpixel_offset = offset - lrintf(offset); // The part not covered by whole_pixel_offset.
+ assert(subpixel_offset >= -0.5f && subpixel_offset <= 0.5f);
for (unsigned y = 0; y < dst_samples; ++y) {
// Find the point around which we want to sample the source image,
// compensating for differing pixel centers as the scale changes.
- float center_src_y = (y + 0.5f) * float(src_size) / float(dst_size) - 0.5f;
+ float center_src_y = (y + subpixel_offset + 0.5f) * float(src_size) / float(dst_size) - 0.5f;
int base_src_y = lrintf(center_src_y);
// Now sample <int_radius> pixels on each side around that point.
weights[(y * src_samples + i) * 2 + 0] = weight * radius_scaling_factor;
weights[(y * src_samples + i) * 2 + 1] = (src_y + 0.5) / float(src_size);
}
-
}
// Now make use of the bilinear filtering in the GPU to reduce the number of samples
if (input_width != last_input_width ||
input_height != last_input_height ||
output_width != last_output_width ||
- output_height != last_output_height) {
+ output_height != last_output_height ||
+ offset != last_offset) {
update_texture(glsl_program_num, prefix, sampler_num);
last_input_width = input_width;
last_input_height = input_height;
last_output_width = output_width;
last_output_height = output_height;
+ last_offset = offset;
}
glActiveTexture(GL_TEXTURE0 + *sampler_num);
set_uniform_float(glsl_program_num, prefix, "sample_x_scale", 1.0f / src_bilinear_samples);
set_uniform_float(glsl_program_num, prefix, "sample_x_offset", 0.5f / src_bilinear_samples);
+ float whole_pixel_offset;
+ if (direction == SingleResamplePassEffect::VERTICAL) {
+ whole_pixel_offset = lrintf(offset) / float(input_height);
+ } else {
+ whole_pixel_offset = lrintf(offset) / float(input_width);
+ }
+ set_uniform_float(glsl_program_num, prefix, "whole_pixel_offset", whole_pixel_offset);
+
// We specifically do not want mipmaps on the input texture;
// they break minification.
Node *self = chain->find_node_for_effect(this);
expect_equal(expected_data, out_data, dwidth, dheight, 0.1 / 1023.0);
}
+TEST(ResampleEffectTest, ReadWholePixelFromLeft) {
+ const int size = 5;
+
+ float data[size * size] = {
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 1.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ };
+ float expected_data[size * size] = {
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 1.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ };
+ float out_data[size * size];
+
+ EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
+ Effect *resample_effect = tester.get_chain()->add_effect(new ResampleEffect());
+ ASSERT_TRUE(resample_effect->set_int("width", size));
+ ASSERT_TRUE(resample_effect->set_int("height", size));
+ ASSERT_TRUE(resample_effect->set_float("left", 1.0f));
+ tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ expect_equal(expected_data, out_data, size, size);
+}
+
+TEST(ResampleEffectTest, ReadQuarterPixelFromLeft) {
+ const int size = 5;
+
+ float data[size * size] = {
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 1.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ };
+
+ float expected_data[size * size] = {
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+
+ // sin(x*pi)/(x*pi) * sin(x*pi/3)/(x*pi/3) for
+ // x = -1.75, -0.75, 0.25, 1.25, 2.25.
+ // Note that the weight is mostly on the left side.
+ -0.06779, 0.27019, 0.89007, -0.13287, 0.03002,
+
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0, 0.0, 0.0,
+ };
+ float out_data[size * size];
+
+ EffectChainTester tester(data, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
+ Effect *resample_effect = tester.get_chain()->add_effect(new ResampleEffect());
+ ASSERT_TRUE(resample_effect->set_int("width", size));
+ ASSERT_TRUE(resample_effect->set_int("height", size));
+ ASSERT_TRUE(resample_effect->set_float("left", 0.25f));
+ tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ expect_equal(expected_data, out_data, size, size);
+}
+
+TEST(ResampleEffectTest, ReadQuarterPixelFromTop) {
+ const int width = 3;
+ const int height = 5;
+
+ float data[width * height] = {
+ 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0,
+ 1.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0,
+ 0.0, 0.0, 0.0,
+ };
+
+ // See ReadQuarterPixelFromLeft for explanation of the data.
+ float expected_data[width * height] = {
+ -0.06779, 0.0, 0.0,
+ 0.27019, 0.0, 0.0,
+ 0.89007, 0.0, 0.0,
+ -0.13287, 0.0, 0.0,
+ 0.03002, 0.0, 0.0,
+ };
+ float out_data[width * height];
+
+ EffectChainTester tester(data, width, height, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR);
+ Effect *resample_effect = tester.get_chain()->add_effect(new ResampleEffect());
+ ASSERT_TRUE(resample_effect->set_int("width", width));
+ ASSERT_TRUE(resample_effect->set_int("height", height));
+ ASSERT_TRUE(resample_effect->set_float("top", 0.25f));
+ tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR);
+
+ expect_equal(expected_data, out_data, width, height);
+}
+
} // namespace movit