d4e532f8b816f1cf594a0f82ba199f5133f944cb
[movit] / gamma_expansion_effect.cpp
1 #include <math.h>
2 #include <assert.h>
3
4 #include "gamma_expansion_effect.h"
5 #include "util.h"
6
7 GammaExpansionEffect::GammaExpansionEffect()
8         : source_curve(GAMMA_LINEAR)
9 {
10         register_int("source_curve", (int *)&source_curve);
11         register_1d_texture("expansion_curve_tex", expansion_curve, EXPANSION_CURVE_SIZE);
12 }
13
14 std::string GammaExpansionEffect::output_fragment_shader()
15 {
16         if (source_curve == GAMMA_LINEAR) {
17                 return read_file("identity.frag");
18         }
19         if (source_curve == GAMMA_sRGB) {
20                 for (unsigned i = 0; i < EXPANSION_CURVE_SIZE; ++i) {
21                         float x = i / (float)(EXPANSION_CURVE_SIZE - 1);
22                         if (x < 0.04045f) {
23                                 expansion_curve[i] = (1.0/12.92f) * x;
24                         } else {
25                                 expansion_curve[i] = pow((x + 0.055) * (1.0/1.055f), 2.4);
26                         }
27                 }
28                 invalidate_1d_texture("expansion_curve_tex");
29                 return read_file("gamma_expansion_effect.frag");
30         }
31         if (source_curve == GAMMA_REC_709) {  // And Rec. 601.
32                 for (unsigned i = 0; i < EXPANSION_CURVE_SIZE; ++i) {
33                         float x = i / (float)(EXPANSION_CURVE_SIZE - 1);
34                         if (x < 0.081f) {
35                                 expansion_curve[i] = (1.0/4.5f) * x;
36                         } else {
37                                 expansion_curve[i] = pow((x + 0.099) * (1.0/1.099f), 1.0f/0.45f);
38                         }
39                 }
40                 invalidate_1d_texture("expansion_curve_tex");
41                 return read_file("gamma_expansion_effect.frag");
42         }
43         assert(false);
44 }