45745673d3420c3152e14434a3ace4741ce208bd
[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         memset(expansion_curve, 0, sizeof(expansion_curve));
12         register_1d_texture("expansion_curve_tex", expansion_curve, EXPANSION_CURVE_SIZE);
13 }
14
15 std::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                 for (unsigned i = 0; i < EXPANSION_CURVE_SIZE; ++i) {
22                         float x = i / (float)(EXPANSION_CURVE_SIZE - 1);
23                         if (x < 0.04045f) {
24                                 expansion_curve[i] = (1.0/12.92f) * x;
25                         } else {
26                                 expansion_curve[i] = pow((x + 0.055) * (1.0/1.055f), 2.4);
27                         }
28                 }
29                 invalidate_1d_texture("expansion_curve_tex");
30                 return read_file("gamma_expansion_effect.frag");
31         }
32         if (source_curve == GAMMA_REC_709) {  // And Rec. 601.
33                 for (unsigned i = 0; i < EXPANSION_CURVE_SIZE; ++i) {
34                         float x = i / (float)(EXPANSION_CURVE_SIZE - 1);
35                         if (x < 0.081f) {
36                                 expansion_curve[i] = (1.0/4.5f) * x;
37                         } else {
38                                 expansion_curve[i] = pow((x + 0.099) * (1.0/1.099f), 1.0f/0.45f);
39                         }
40                 }
41                 invalidate_1d_texture("expansion_curve_tex");
42                 return read_file("gamma_expansion_effect.frag");
43         }
44         assert(false);
45 }