]> git.tdb.fi Git - libs/gl.git/blob - source/colorcurve.cpp
Be less eager to optimize constant conditions in loops
[libs/gl.git] / source / 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 {
18         shdata.uniform("source", 0);
19         shdata.uniform("curve", 1);
20
21         curve.storage(LUMINANCE, 256, 1);
22         curve.set_min_filter(LINEAR);
23         curve.set_wrap(CLAMP_TO_EDGE);
24         texturing.attach(1, curve);
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_peak(float)
45 {
46 }
47
48 void ColorCurve::set_brightness(float b)
49 {
50         set_brightness_response(1/b);
51 }
52
53 void ColorCurve::set_gamma(float g)
54 {
55         if(g<0.1 || g>10)
56                 throw invalid_argument("ColorCurve::set_gamma");
57
58         unsigned char curve_data[256];
59         for(unsigned i=0; i<256; ++i)
60                 curve_data[i] = pow(i/255.0f, 1/g)*255+0.5f;
61         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
62 }
63
64 void ColorCurve::set_srgb()
65 {
66         unsigned char curve_data[256];
67         curve_data[0] = 0;
68         for(unsigned i=1; i<256; ++i)
69                 curve_data[i] = to_srgb(i/255.0f)*255+0.5f;
70         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
71 }
72
73 void ColorCurve::set_linear()
74 {
75         unsigned char curve_data[256];
76         for(unsigned i=0; i<256; ++i)
77                 curve_data[i] = i;
78         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
79 }
80
81 void ColorCurve::render(Renderer &renderer, const Texture2D &color_buf, const Texture2D &)
82 {
83         texturing.attach(0, color_buf);
84
85         Renderer::Push push(renderer);
86         renderer.set_shader_program(&shprog, &shdata);
87         renderer.set_texturing(&texturing);
88         quad.draw(renderer);
89 }
90
91
92 ColorCurve::Template::Template():
93         exposure_adjust(0.0f),
94         brightness_response(0.4f),
95         gamma(1.0f),
96         srgb(false)
97 { }
98
99 ColorCurve *ColorCurve::Template::create(unsigned, unsigned) const
100 {
101         RefPtr<ColorCurve> colorcurve = new ColorCurve;
102         colorcurve->set_exposure_adjust(exposure_adjust);
103         colorcurve->set_brightness_response(brightness_response);
104         if(srgb)
105                 colorcurve->set_srgb();
106         else
107                 colorcurve->set_gamma(gamma);
108         return colorcurve.release();
109 }
110
111
112 ColorCurve::Template::Loader::Loader(Template &t):
113         DataFile::DerivedObjectLoader<Template, PostProcessor::Template::Loader>(t)
114 {
115         add("brightness_response", &Template::brightness_response);
116         add("exposure_adjust", &Template::exposure_adjust);
117         add("gamma", &Loader::gamma);
118         add("srgb", &Loader::srgb);
119 }
120
121 void ColorCurve::Template::Loader::gamma(float g)
122 {
123         obj.gamma = g;
124         obj.srgb = false;
125 }
126
127 void ColorCurve::Template::Loader::srgb()
128 {
129         obj.srgb = true;
130 }
131
132 } // namespace GL
133 } // namespace Msp