]> git.sesse.net Git - movit/blob - dither_effect.h
Give the alpha enums somewhat better/more consistent names, and shuffle them around...
[movit] / dither_effect.h
1 #ifndef _DITHER_EFFECT_H
2 #define _DITHER_EFFECT_H 1
3
4 // Implements simple rectangular-PDF dither.
5 //
6 // Although all of our processing internally is in floating-point (a mix of 16-
7 // and 32-bit), eventually most pipelines will end up downconverting to a fixed-point
8 // format, typically 8-bits unsigned integer (GL_RGBA8).
9 //
10 // The hardware will typically do proper rounding for us, so that we minimize
11 // quantization noise, but for some applications, if you look closely, you can still
12 // see some banding; 8 bits is not really all that much (and if we didn't have the
13 // perceptual gamma curve, it would be a lot worse).
14 //
15 // The standard solution to this is dithering; in short, to add a small random component
16 // to each pixel before quantization. This increases the overall noise floor slightly,
17 // but allows us to represent frequency components with an amplitude lower than 1/256.
18 // 
19 // My standard reference on dither is:
20 //
21 //   Cameron Nicklaus Christou: “Optimal Dither and Noise Shaping in Image Processing”
22 //   http://uwspace.uwaterloo.ca/bitstream/10012/3867/1/thesis.pdf
23 //
24 // However, we need to make two significant deviations from the recommendations it makes.
25 // First of all, it recommends using a triangular-PDF (TPDF) dither (which can be synthesized
26 // effectively by adding two uniformly distributed random numbers) instead of rectangular-PDF
27 // (RPDF; using one uniformly distributed random number), in order to make the second moment
28 // of the error signal independent from the original image. However, since the recommended
29 // TPDF must be twice as wide as the RPDF, it means it can go to +/- 1, which means that
30 // some of the time, it will add enough noise to change a pixel just by itself. Given that
31 // a very common use case for us is converting 8-bit -> 8-bit (ie., no bit reduction at all),
32 // it would seem like a more important goal to have no noise in that situation than to
33 // improve the dither further.
34 //
35 // Second, the thesis recommends noise shaping (also known as error diffusion in the image
36 // processing world). This is, however, very hard to implement properly on a GPU, since it
37 // almost by definition feeds the value of output pixels into the neighboring input pixels.
38 // Maybe one could make a version that implemented the noise shapers by way of FIR filters
39 // instead of IIR like this, but it would seem a lot of work for very subtle gain.
40 //
41 // We keep the dither noise fixed as long as the output resolution doesn't change;
42 // this ensures we don't upset video codecs too much. (One could also dither in time,
43 // like many LCD monitors do, but it starts to get very hairy, again, for limited gains.)
44 // The dither is also deterministic across runs.
45
46 #include "effect.h"
47
48 class DitherEffect : public Effect {
49 private:
50         // Should not be instantiated by end users;
51         // call EffectChain::set_dither_bits() instead.
52         DitherEffect();
53         friend class EffectChain;
54
55 public:
56         ~DitherEffect();
57         virtual std::string effect_type_id() const { return "DitherEffect"; }
58         std::string output_fragment_shader();
59
60         // Note that if we did error diffusion, we'd actually want to diffuse the
61         // premultiplied error. However, we need to do dithering in the same
62         // space as quantization, whether that be pre- or postmultiply.
63         virtual AlphaHandling alpha_handling() const { return DONT_CARE_ALPHA_TYPE; }
64
65         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
66
67 private:
68         void update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
69
70         int width, height, num_bits;
71         int last_width, last_height, last_num_bits;
72         int texture_width, texture_height;
73
74         GLuint texnum;
75         bool need_texture_update;
76 };
77
78 #endif // !defined(_DITHER_EFFECT_H)