]> git.sesse.net Git - movit/blob - resample_effect.h
Add a new alpha handling method, INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK.
[movit] / resample_effect.h
1 #ifndef _RESAMPLE_EFFECT_H
2 #define _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 "effect.h"
19
20 class SingleResamplePassEffect;
21
22 class ResampleEffect : public Effect {
23 public:
24         ResampleEffect();
25
26         virtual std::string effect_type_id() const { return "ResampleEffect"; }
27
28         // We want this for the same reason as ResizeEffect; we could end up scaling
29         // down quite a lot.
30         virtual bool needs_texture_bounce() const { return true; }
31         virtual bool needs_srgb_primaries() const { return false; }
32         virtual AlphaHandling alpha_handling() const { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; }
33
34         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
35
36         virtual std::string output_fragment_shader() {
37                 assert(false);
38         }
39         virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
40                 assert(false);
41         }
42
43         virtual void rewrite_graph(EffectChain *graph, Node *self);
44         virtual bool set_float(const std::string &key, float value);
45         
46 private:
47         void update_size();
48         
49         SingleResamplePassEffect *hpass, *vpass;
50         int input_width, input_height, output_width, output_height;
51 };
52
53 class SingleResamplePassEffect : public Effect {
54 public:
55         // If parent is non-NULL, calls to inform_input_size will be forwarded,
56         // so that it can inform both passes about the right input and output
57         // resolutions.
58         SingleResamplePassEffect(ResampleEffect *parent);
59         ~SingleResamplePassEffect();
60         virtual std::string effect_type_id() const { return "SingleResamplePassEffect"; }
61
62         std::string output_fragment_shader();
63
64         virtual bool needs_texture_bounce() const { return true; }
65         virtual bool needs_srgb_primaries() const { return false; }
66
67         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height) {
68                 if (parent != NULL) {
69                         parent->inform_input_size(input_num, width, height);
70                 }
71         }
72         virtual bool changes_output_size() const { return true; }
73
74         virtual void get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const {
75                 *virtual_width = *width = this->output_width;
76                 *virtual_height = *height = this->output_height;
77         }
78
79         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
80         
81         enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
82
83 private:
84         void update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
85
86         ResampleEffect *parent;
87         Direction direction;
88         GLuint texnum;
89         int input_width, input_height, output_width, output_height;
90         int last_input_width, last_input_height, last_output_width, last_output_height;
91         int src_bilinear_samples, num_loops;
92         float slice_height;
93 };
94
95 #endif // !defined(_RESAMPLE_EFFECT_H)