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