c64f85317ed2822424db8fc95e5e1a6a1f813e2d
[libs/gl.git] / source / effects / colorcurve.cpp
1 #include <cmath>
2 #include "color.h"
3 #include "colorcurve.h"
4 #include "mesh.h"
5 #include "renderer.h"
6 #include "resources.h"
7 #include "texture2d.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 ColorCurve::ColorCurve():
15         shprog(Resources::get_global().get<Program>("_colorcurve.glsl.shader")),
16         quad(Resources::get_global().get<Mesh>("_fullscreen_quad.mesh")),
17         linear_sampler(Resources::get_global().get<Sampler>("_linear_clamp.samp")),
18         nearest_sampler(Resources::get_global().get<Sampler>("_nearest_clamp.samp"))
19 {
20         curve.storage(LUMINANCE8, 256, 1);
21
22         set_exposure_adjust(0.0f);
23         set_brightness_response(0.4f);
24         set_linear();
25 }
26
27 void ColorCurve::set_exposure_adjust(float e)
28 {
29         shdata.uniform("exposure", pow(2.0f, e));
30 }
31
32 void ColorCurve::set_brightness_response(float b)
33 {
34         if(b<=0 || b>1)
35                 throw invalid_argument("ColorCurve::set_brightness_response");
36         // Calculate an offset value to obtain a derivative of 1 at zero intensity.
37         float t = (b<1 ? pow(b, 1/(1-b)) : 0.0f);
38         shdata.uniform("brightness_response", b, t, pow(t, b));
39 }
40
41 void ColorCurve::set_gamma(float g)
42 {
43         if(g<0.1 || g>10)
44                 throw invalid_argument("ColorCurve::set_gamma");
45
46         unsigned char curve_data[256];
47         for(unsigned i=0; i<256; ++i)
48                 curve_data[i] = pow(i/255.0f, 1/g)*255+0.5f;
49         curve.image(0, curve_data);
50 }
51
52 void ColorCurve::set_srgb()
53 {
54         unsigned char curve_data[256];
55         curve_data[0] = 0;
56         for(unsigned i=1; i<256; ++i)
57                 curve_data[i] = to_srgb(i/255.0f)*255+0.5f;
58         curve.image(0, curve_data);
59 }
60
61 void ColorCurve::set_linear()
62 {
63         unsigned char curve_data[256];
64         for(unsigned i=0; i<256; ++i)
65                 curve_data[i] = i;
66         curve.image(0, curve_data);
67 }
68
69 void ColorCurve::render(Renderer &renderer, const Texture2D &color_buf, const Texture2D &)
70 {
71         Renderer::Push push(renderer);
72         renderer.set_shader_program(&shprog, &shdata);
73         renderer.set_texture("source", &color_buf, &nearest_sampler);
74         renderer.set_texture("curve", &curve, &linear_sampler);
75         quad.draw(renderer);
76 }
77
78 void ColorCurve::set_debug_name(const string &name)
79 {
80 #ifdef DEBUG
81         shdata.set_debug_name(name+" [UBO]");
82         curve.set_debug_name(name+"/curve.tex1d");
83 #else
84         (void)name;
85 #endif
86 }
87
88
89 ColorCurve *ColorCurve::Template::create(unsigned, unsigned) const
90 {
91         RefPtr<ColorCurve> colorcurve = new ColorCurve();
92         colorcurve->set_exposure_adjust(exposure_adjust);
93         colorcurve->set_brightness_response(brightness_response);
94         if(srgb)
95                 colorcurve->set_srgb();
96         else
97                 colorcurve->set_gamma(gamma);
98         return colorcurve.release();
99 }
100
101
102 ColorCurve::Template::Loader::Loader(Template &t):
103         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
104 {
105         add("brightness_response", &Template::brightness_response);
106         add("exposure_adjust", &Template::exposure_adjust);
107         add("gamma", &Loader::gamma);
108         add("srgb", &Loader::srgb);
109 }
110
111 void ColorCurve::Template::Loader::gamma(float g)
112 {
113         obj.gamma = g;
114         obj.srgb = false;
115 }
116
117 void ColorCurve::Template::Loader::srgb()
118 {
119         obj.srgb = true;
120 }
121
122 } // namespace GL
123 } // namespace Msp