]> git.tdb.fi Git - libs/gl.git/blob - source/colorcurve.cpp
Add gamma correction and sRGB conversion support to ColorCurve
[libs/gl.git] / source / colorcurve.cpp
1 #include <cmath>
2 #include "colorcurve.h"
3 #include "mesh.h"
4 #include "shader.h"
5 #include "texture2d.h"
6
7 using namespace std;
8
9 namespace {
10
11 static const char fragment_src[] =
12         "uniform sampler2D texture;\n"
13         "uniform sampler1D curve;\n"
14         "uniform float peak;\n"
15         "uniform float brightness;\n"
16         "varying vec2 texcoord;\n"
17         "void main()\n"
18         "{\n"
19         "       vec4 sample = texture2D(texture, texcoord);\n"
20         "       float maxc = max(sample.r, max(sample.g, sample.b));\n"
21         "       if(maxc>1.0-peak)\n"
22         "       {\n"
23         "               vec3 saturated = sample.rgb/maxc;\n"
24         "               if(maxc>1.0+peak)\n"
25         "               {\n"
26         "                       sample.rgb = mix(vec3(1.0), saturated, 1.0/pow(brightness, maxc-1.0-peak));\n"
27         "               }\n"
28         "               else\n"
29         "               {\n"
30         "                       float x = (1.0+peak-maxc)/(2.0*peak);\n"
31         "                       sample.rgb = saturated.rgb*(1.0-peak+(1.0-x*x)*peak);\n"
32         "               }\n"
33         "       }\n"
34         "       gl_FragColor = vec4(texture1D(curve, sample.r).r, texture1D(curve, sample.g).r, texture1D(curve, sample.b).r, sample.a);\n"
35         "}";
36
37 }
38
39 namespace Msp {
40 namespace GL {
41
42 ColorCurve::ColorCurve():
43         quad(get_fullscreen_quad())
44 {
45         shprog.attach_shader(get_fullscreen_vertex_shader());
46         shprog.attach_shader_owned(new FragmentShader(fragment_src));
47         shprog.link();
48
49         shdata.uniform("texture", 0);
50         shdata.uniform("curve", 1);
51
52         curve.storage(LUMINANCE, 256);
53         curve.set_min_filter(LINEAR);
54         curve.set_wrap(CLAMP_TO_EDGE);
55
56         set_peak(0.2);
57         set_brightness(1.5);
58         set_linear();
59 }
60
61 void ColorCurve::set_peak(float p)
62 {
63         if(p<0 || p>1)
64                 throw invalid_argument("ColorCurve::set_peak");
65         shdata.uniform("peak", p);
66 }
67
68 void ColorCurve::set_brightness(float b)
69 {
70         if(b<1)
71                 throw invalid_argument("ColorCurve::set_brightness");
72         shdata.uniform("brightness", b);
73 }
74
75 void ColorCurve::set_gamma(float g)
76 {
77         if(g<0.1 || g>10)
78                 throw invalid_argument("ColorCurve::set_gamma");
79
80         unsigned char curve_data[256];
81         for(unsigned i=0; i<256; ++i)
82                 curve_data[i] = pow(i/255.0f, 1/g)*255+0.5f;
83         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
84 }
85
86 void ColorCurve::set_srgb()
87 {
88         unsigned char curve_data[256];
89         curve_data[0] = 0;
90         for(unsigned i=1; i<256; ++i)
91                 curve_data[i] = (1.055*pow(i/255.0f, 1/2.4f)-0.055)*255+0.5;
92         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
93 }
94
95 void ColorCurve::set_linear()
96 {
97         unsigned char curve_data[256];
98         for(unsigned i=0; i<256; ++i)
99                 curve_data[i] = i;
100         curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
101 }
102
103 void ColorCurve::render(const Texture2D &color_buf, const Texture2D &)
104 {
105         Bind _bind_shader(shprog);
106         shdata.apply();
107         Bind _bind_tex(color_buf);
108         Bind _bind_curve(curve, 1);
109         quad.draw();
110 }
111
112 } // namespace GL
113 } // namespace Msp