1 #ifndef _MOVIT_RESAMPLE_EFFECT_H
2 #define _MOVIT_RESAMPLE_EFFECT_H 1
4 // High-quality image resizing, either up or down.
6 // The default scaling offered by the GPU (and as used in ResizeEffect)
7 // is bilinear (optionally mipmapped), which is not the highest-quality
8 // choice, especially for upscaling. ResampleEffect offers the three-lobed
9 // Lanczos kernel, which is among the most popular choices in image
10 // processing. While it does have its weaknesses, in particular a certain
11 // ringing/sharpening effect with artifacts that accumulate over several
12 // consecutive resizings, it is generally regarded as the best tradeoff.
14 // Works in two passes; first horizontal, then vertical (ResampleEffect,
15 // which is what the user is intended to use, instantiates two copies of
16 // SingleResamplePassEffect behind the scenes).
31 class SingleResamplePassEffect;
33 // Public so that it can be benchmarked externally.
39 struct ScalingWeights {
40 unsigned src_bilinear_samples;
41 unsigned dst_samples, num_loops;
43 // Exactly one of these is set.
44 std::unique_ptr<Tap<fp16_int_t>[]> bilinear_weights_fp16;
45 std::unique_ptr<Tap<float>[]> bilinear_weights_fp32;
47 ScalingWeights calculate_scaling_weights(unsigned src_size, unsigned dst_size, float zoom, float offset);
49 class ResampleEffect : public Effect {
53 virtual std::string effect_type_id() const { return "ResampleEffect"; }
55 // We want this for the same reason as ResizeEffect; we could end up scaling
57 virtual bool needs_texture_bounce() const { return true; }
58 virtual bool needs_srgb_primaries() const { return false; }
60 virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
62 virtual std::string output_fragment_shader() {
65 virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
69 virtual void rewrite_graph(EffectChain *graph, Node *self);
70 virtual bool set_float(const std::string &key, float value);
74 void update_offset_and_zoom();
76 SingleResamplePassEffect *hpass, *vpass;
77 int input_width, input_height, output_width, output_height;
79 float offset_x, offset_y;
81 float zoom_center_x, zoom_center_y;
84 class SingleResamplePassEffect : public Effect {
86 // If parent is non-NULL, calls to inform_input_size will be forwarded,
87 // so that it can inform both passes about the right input and output
89 SingleResamplePassEffect(ResampleEffect *parent);
90 ~SingleResamplePassEffect();
91 virtual std::string effect_type_id() const { return "SingleResamplePassEffect"; }
93 std::string output_fragment_shader();
95 virtual bool needs_texture_bounce() const { return true; }
96 virtual bool needs_srgb_primaries() const { return false; }
97 virtual AlphaHandling alpha_handling() const { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; }
99 virtual void inform_added(EffectChain *chain) { this->chain = chain; }
100 virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height) {
101 if (parent != NULL) {
102 parent->inform_input_size(input_num, width, height);
105 virtual bool changes_output_size() const { return true; }
106 virtual bool sets_virtual_output_size() const { return false; }
108 virtual void get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const {
109 *virtual_width = *width = this->output_width;
110 *virtual_height = *height = this->output_height;
113 void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
115 enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
118 void update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
120 ResampleEffect *parent;
124 GLint uniform_sample_tex;
125 float uniform_num_loops, uniform_slice_height, uniform_sample_x_scale, uniform_sample_x_offset;
126 float uniform_whole_pixel_offset;
127 int uniform_num_samples;
129 int input_width, input_height, output_width, output_height;
131 int last_input_width, last_input_height, last_output_width, last_output_height;
132 float last_offset, last_zoom;
133 int src_bilinear_samples, num_loops;
135 int last_texture_width, last_texture_height;
136 GLuint last_texture_internal_format;
141 #endif // !defined(_MOVIT_RESAMPLE_EFFECT_H)