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