]> git.sesse.net Git - movit/blob - resample_effect.h
Fix initialisation on locale with comma as numerical separator
[movit] / resample_effect.h
1 #ifndef _MOVIT_RESAMPLE_EFFECT_H
2 #define _MOVIT_RESAMPLE_EFFECT_H 1
3
4 // High-quality image resizing, either up or down.
5 //
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.
13 //
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).
17
18 #include <epoxy/gl.h>
19 #include <assert.h>
20 #include <stddef.h>
21 #include <string>
22
23 #include "effect.h"
24
25 namespace movit {
26
27 class EffectChain;
28 class Node;
29 class SingleResamplePassEffect;
30
31 class ResampleEffect : public Effect {
32 public:
33         ResampleEffect();
34
35         virtual std::string effect_type_id() const { return "ResampleEffect"; }
36
37         // We want this for the same reason as ResizeEffect; we could end up scaling
38         // down quite a lot.
39         virtual bool needs_texture_bounce() const { return true; }
40         virtual bool needs_srgb_primaries() const { return false; }
41
42         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
43
44         virtual std::string output_fragment_shader() {
45                 assert(false);
46         }
47         virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
48                 assert(false);
49         }
50
51         virtual void rewrite_graph(EffectChain *graph, Node *self);
52         virtual bool set_float(const std::string &key, float value);
53         
54 private:
55         void update_size();
56         void update_offset_and_zoom();
57         
58         SingleResamplePassEffect *hpass, *vpass;
59         int input_width, input_height, output_width, output_height;
60
61         float offset_x, offset_y;
62         float zoom_x, zoom_y;
63         float zoom_center_x, zoom_center_y;
64 };
65
66 class SingleResamplePassEffect : public Effect {
67 public:
68         // If parent is non-NULL, calls to inform_input_size will be forwarded,
69         // so that it can inform both passes about the right input and output
70         // resolutions.
71         SingleResamplePassEffect(ResampleEffect *parent);
72         ~SingleResamplePassEffect();
73         virtual std::string effect_type_id() const { return "SingleResamplePassEffect"; }
74
75         std::string output_fragment_shader();
76
77         virtual bool needs_texture_bounce() const { return true; }
78         virtual bool needs_srgb_primaries() const { return false; }
79         virtual AlphaHandling alpha_handling() const { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; }
80
81         virtual void inform_added(EffectChain *chain) { this->chain = chain; }
82         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height) {
83                 if (parent != NULL) {
84                         parent->inform_input_size(input_num, width, height);
85                 }
86         }
87         virtual bool changes_output_size() const { return true; }
88         virtual bool sets_virtual_output_size() const { return false; }
89
90         virtual void get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const {
91                 *virtual_width = *width = this->output_width;
92                 *virtual_height = *height = this->output_height;
93         }
94
95         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
96         
97         enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
98
99 private:
100         void update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
101
102         ResampleEffect *parent;
103         EffectChain *chain;
104         Direction direction;
105         GLuint texnum;
106         GLint uniform_sample_tex;
107         float uniform_num_loops, uniform_slice_height, uniform_sample_x_scale, uniform_sample_x_offset;
108         float uniform_whole_pixel_offset;
109         int uniform_num_samples;
110
111         int input_width, input_height, output_width, output_height;
112         float offset, zoom;
113         int last_input_width, last_input_height, last_output_width, last_output_height;
114         float last_offset, last_zoom;
115         int src_bilinear_samples, num_loops;
116         float slice_height;
117         int last_texture_width, last_texture_height;
118         GLuint last_texture_internal_format;
119 };
120
121 }  // namespace movit
122
123 #endif // !defined(_MOVIT_RESAMPLE_EFFECT_H)