]> git.sesse.net Git - movit/blob - gamma_expansion_effect.cpp
Don't dither alpha.
[movit] / gamma_expansion_effect.cpp
1 #include <assert.h>
2
3 #include "effect_util.h"
4 #include "gamma_expansion_effect.h"
5 #include "util.h"
6
7 using namespace std;
8
9 GammaExpansionEffect::GammaExpansionEffect()
10         : source_curve(GAMMA_LINEAR)
11 {
12         register_int("source_curve", (int *)&source_curve);
13 }
14
15 string GammaExpansionEffect::output_fragment_shader()
16 {
17         if (source_curve == GAMMA_LINEAR) {
18                 return read_file("identity.frag");
19         }
20         if (source_curve == GAMMA_sRGB ||
21             source_curve == GAMMA_REC_709 ||  // Also includes Rec. 601, and 10-bit Rec. 2020.
22             source_curve == GAMMA_REC_2020_12_BIT) {
23                 return read_file("gamma_expansion_effect.frag");
24         }
25         assert(false);
26 }
27
28 void GammaExpansionEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num)
29 {
30         Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
31
32         // All of these curves follow a continuous curve that's piecewise defined;
33         // very low values (up to some β) are linear. Above β, we have a power curve
34         // that looks like this:
35         //
36         //   y = ((x + ɑ - 1) / ɑ)^ɣ
37         //
38         // However, pow() is relatively slow in GLSL, so we approximate this
39         // part by a minimax polynomial, whose coefficients are precalculated
40         // in Maple. (It is very hard to accurately model the curve as a whole
41         // using minimax polynomials; both Maple and Mathematically generally
42         // just error out if you ask them to optimize over 0..1 with a higher-degree
43         // polynomial.)
44         //
45         // We put some extra weight on areas near β to keep a continuous curve,
46         // and near 1.0, since we'd really like f(1.0) = 1.0, or approximately so.
47         // The following Maple commands, using sRGB below as an example, will
48         // compute the coefficients:
49         //
50         // > alpha := 1.055;
51         // > beta := 0.04045;
52         // > gamma_ := 2.4;
53         // > w := x -> piecewise(x < beta + 0.001, 10, x > 0.999, 10, 1);
54         // > numapprox[minimax](((x + alpha - 1) / alpha)^gamma_, x=beta..1, [4,0], w(x), 'maxerror');
55         //
56         // The variable 'maxerror' will then contain the maximum absolute error
57         // at any point of the curve, and we report this along with the absolute
58         // error at beta and at 1.0. Keep in mind that along this curve,
59         // the smallest minimum difference between any two 8-bit sRGB pixel levels
60         // (in the exponential part of the curve) in linear light is that
61         // between 11/255 and 12/255, which is about 0.00033 (or three to four
62         // times of the sRGB maxerror below). The choice of a fourth-degree
63         // polynomial was made with this in mind; we have not cared equally
64         // much about 10- and 12-bit Rec. 2020.
65         //
66         // NOTE: The error at beta is compared to the _linear_ part of the curve.
67         // Since the standards give these with only a few decimals, it means that
68         // the linear and exponential parts will not match up exactly, and even
69         // a perfect approximation will have error > 0 here; sometimes, even larger
70         // than maxerror for the curve itself.
71
72         if (source_curve == GAMMA_sRGB) {
73                 // From the Wikipedia article on sRGB; ɑ (called a+1 there) = 1.055,
74                 // β = 0.04045, ɣ = 2.4.
75                 // maxerror      = 0.000094
76                 // error at beta = 0.000012
77                 // error at 1.0  = 0.000012
78                 //
79                 // Note that the worst _relative_ error by far is just at the beginning
80                 // of the exponential curve, ie., just around β.
81                 set_uniform_float(glsl_program_num, prefix, "linear_scale", 1.0 / 12.92);
82                 set_uniform_float(glsl_program_num, prefix, "c0", 0.001324469581);
83                 set_uniform_float(glsl_program_num, prefix, "c1", 0.02227416690);
84                 set_uniform_float(glsl_program_num, prefix, "c2", 0.5917615253);
85                 set_uniform_float(glsl_program_num, prefix, "c3", 0.4733532353);
86                 set_uniform_float(glsl_program_num, prefix, "c4", -0.08880738120);
87                 set_uniform_float(glsl_program_num, prefix, "beta", 0.04045);
88         }
89         if (source_curve == GAMMA_REC_709) {  // Also includes Rec. 601, and 10-bit Rec. 2020.
90                 // Rec. 2020, page 3; ɑ = 1.099, β = 0.018 * 4.5, ɣ = 1/0.45.
91                 // maxerror      = 0.000043
92                 // error at beta = 0.000051 (see note above!)
93                 // error at 1.0  = 0.000004
94                 //
95                 // Note that Rec. 2020 only gives the other direction, which is why
96                 // our beta and gamma are different from the numbers mentioned
97                 // (we've inverted the formula).
98                 set_uniform_float(glsl_program_num, prefix, "linear_scale", 1.0 / 4.5);
99                 set_uniform_float(glsl_program_num, prefix, "c0", 0.005137028744);
100                 set_uniform_float(glsl_program_num, prefix, "c1", 0.09802596889);
101                 set_uniform_float(glsl_program_num, prefix, "c2", 0.7255768864);
102                 set_uniform_float(glsl_program_num, prefix, "c3", 0.2135067966);
103                 set_uniform_float(glsl_program_num, prefix, "c4", -0.04225094667);
104                 set_uniform_float(glsl_program_num, prefix, "beta", 0.018 * 4.5);
105         }
106         if (source_curve == GAMMA_REC_2020_12_BIT) {
107                 // Rec. 2020, page 3; ɑ = 1.0993, β = 0.0181 * 4.5, ɣ = 1/0.45.
108                 // maxerror      = 0.000042
109                 // error at beta = 0.000005
110                 // error at 1.0  = 0.000004
111                 //
112                 // Note that Rec. 2020 only gives the other direction, which is why
113                 // our beta and gamma are different from the numbers mentioned
114                 // (we've inverted the formula).
115                 set_uniform_float(glsl_program_num, prefix, "linear_scale", 1.0 / 4.5);
116                 set_uniform_float(glsl_program_num, prefix, "c0", 0.005167545928);
117                 set_uniform_float(glsl_program_num, prefix, "c1", 0.09835585809);
118                 set_uniform_float(glsl_program_num, prefix, "c2", 0.7254820139);
119                 set_uniform_float(glsl_program_num, prefix, "c3", 0.2131291155);
120                 set_uniform_float(glsl_program_num, prefix, "c4", -0.04213877222);
121                 set_uniform_float(glsl_program_num, prefix, "beta", 0.0181 * 4.5);
122         }
123 }