2 #include "colorcurve.h"
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"
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"
23 " vec3 saturated = sample.rgb/maxc;\n"
24 " if(maxc>1.0+peak)\n"
26 " sample.rgb = mix(vec3(1.0), saturated, 1.0/pow(brightness, maxc-1.0-peak));\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"
34 " gl_FragColor = vec4(texture1D(curve, sample.r).r, texture1D(curve, sample.g).r, texture1D(curve, sample.b).r, sample.a);\n"
42 ColorCurve::ColorCurve():
43 quad(get_fullscreen_quad())
45 shprog.attach_shader(get_fullscreen_vertex_shader());
46 shprog.attach_shader_owned(new FragmentShader(fragment_src));
49 shdata.uniform("texture", 0);
50 shdata.uniform("curve", 1);
52 curve.storage(LUMINANCE, 256);
53 curve.set_min_filter(LINEAR);
54 curve.set_wrap(CLAMP_TO_EDGE);
61 void ColorCurve::set_peak(float p)
64 throw invalid_argument("ColorCurve::set_peak");
65 shdata.uniform("peak", p);
68 void ColorCurve::set_brightness(float b)
71 throw invalid_argument("ColorCurve::set_brightness");
72 shdata.uniform("brightness", b);
75 void ColorCurve::set_gamma(float g)
78 throw invalid_argument("ColorCurve::set_gamma");
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);
86 void ColorCurve::set_srgb()
88 unsigned char curve_data[256];
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);
95 void ColorCurve::set_linear()
97 unsigned char curve_data[256];
98 for(unsigned i=0; i<256; ++i)
100 curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
103 void ColorCurve::render(const Texture2D &color_buf, const Texture2D &)
105 Bind _bind_shader(shprog);
107 Bind _bind_tex(color_buf);
108 Bind _bind_curve(curve, 1);