]> git.tdb.fi Git - libs/gl.git/blob - source/effects/colorcurve.cpp
8811b065fd3b6f0dddddd86a8e091e08d97bc224
[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 "shader.h"
7 #include "texture2d.h"
8
9 using namespace std;
10
11 namespace Msp {
12 namespace GL {
13
14 ColorCurve::ColorCurve():
15         shprog("colorcurve.glsl"),
16         quad(get_fullscreen_quad()),
17         linear_sampler(get_linear_sampler()),
18         nearest_sampler(get_nearest_sampler())
19 {
20         shdata.uniform("source", 0);
21         shdata.uniform("curve", 1);
22
23         curve.storage(LUMINANCE8, 256, 1);
24         texturing.attach(1, curve, linear_sampler.get());
25
26         set_exposure_adjust(0.0f);
27         set_brightness_response(0.4f);
28         set_linear();
29 }
30
31 void ColorCurve::set_exposure_adjust(float e)
32 {
33         shdata.uniform("exposure", pow(2.0f, e));
34 }
35
36 void ColorCurve::set_brightness_response(float b)
37 {
38         if(b<=0 || b>1)
39                 throw invalid_argument("ColorCurve::set_brightness_response");
40         float t = (b<1 ? pow(b, 1/(1-b)) : 0.0f);
41         shdata.uniform("brightness_response", b, t, pow(t, b));
42 }
43
44 void ColorCurve::set_gamma(float g)
45 {
46         if(g<0.1 || g>10)
47                 throw invalid_argument("ColorCurve::set_gamma");
48
49         unsigned char curve_data[256];
50         for(unsigned i=0; i<256; ++i)
51                 curve_data[i] = pow(i/255.0f, 1/g)*255+0.5f;
52         curve.image(0, curve_data);
53 }
54
55 void ColorCurve::set_srgb()
56 {
57         unsigned char curve_data[256];
58         curve_data[0] = 0;
59         for(unsigned i=1; i<256; ++i)
60                 curve_data[i] = to_srgb(i/255.0f)*255+0.5f;
61         curve.image(0, curve_data);
62 }
63
64 void ColorCurve::set_linear()
65 {
66         unsigned char curve_data[256];
67         for(unsigned i=0; i<256; ++i)
68                 curve_data[i] = i;
69         curve.image(0, curve_data);
70 }
71
72 void ColorCurve::render(Renderer &renderer, const Texture2D &color_buf, const Texture2D &)
73 {
74         texturing.attach(0, color_buf, nearest_sampler.get());
75
76         Renderer::Push push(renderer);
77         renderer.set_shader_program(&shprog, &shdata);
78         renderer.set_texturing(&texturing);
79         quad->draw(renderer);
80 }
81
82
83 ColorCurve::Template::Template():
84         exposure_adjust(0.0f),
85         brightness_response(0.4f),
86         gamma(1.0f),
87         srgb(false)
88 { }
89
90 ColorCurve *ColorCurve::Template::create(unsigned, unsigned) const
91 {
92         RefPtr<ColorCurve> colorcurve = new ColorCurve;
93         colorcurve->set_exposure_adjust(exposure_adjust);
94         colorcurve->set_brightness_response(brightness_response);
95         if(srgb)
96                 colorcurve->set_srgb();
97         else
98                 colorcurve->set_gamma(gamma);
99         return colorcurve.release();
100 }
101
102
103 ColorCurve::Template::Loader::Loader(Template &t):
104         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
105 {
106         add("brightness_response", &Template::brightness_response);
107         add("exposure_adjust", &Template::exposure_adjust);
108         add("gamma", &Loader::gamma);
109         add("srgb", &Loader::srgb);
110 }
111
112 void ColorCurve::Template::Loader::gamma(float g)
113 {
114         obj.gamma = g;
115         obj.srgb = false;
116 }
117
118 void ColorCurve::Template::Loader::srgb()
119 {
120         obj.srgb = true;
121 }
122
123 } // namespace GL
124 } // namespace Msp