3 #include "colorcurve.h"
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"
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"
24 " vec3 saturated = sample.rgb/maxc;\n"
25 " if(maxc>1.0+peak)\n"
27 " sample.rgb = mix(vec3(1.0), saturated, 1.0/pow(brightness, maxc-1.0-peak));\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"
35 " gl_FragColor = vec4(texture1D(curve, sample.r).r, texture1D(curve, sample.g).r, texture1D(curve, sample.b).r, sample.a);\n"
43 ColorCurve::ColorCurve():
44 quad(get_fullscreen_quad())
46 shprog.attach_shader(get_fullscreen_vertex_shader());
47 shprog.attach_shader_owned(new FragmentShader(fragment_src));
48 shprog.bind_attribute(get_component_type(VERTEX2), "vertex");
51 shdata.uniform("texture", 0);
52 shdata.uniform("curve", 1);
54 curve.storage(LUMINANCE, 256);
55 curve.set_min_filter(LINEAR);
56 curve.set_wrap(CLAMP_TO_EDGE);
63 void ColorCurve::set_peak(float p)
66 throw invalid_argument("ColorCurve::set_peak");
67 shdata.uniform("peak", p);
70 void ColorCurve::set_brightness(float b)
73 throw invalid_argument("ColorCurve::set_brightness");
74 shdata.uniform("brightness", b);
77 void ColorCurve::set_gamma(float g)
80 throw invalid_argument("ColorCurve::set_gamma");
82 unsigned char curve_data[256];
83 for(unsigned i=0; i<256; ++i)
84 curve_data[i] = pow(i/255.0f, 1/g)*255+0.5f;
85 curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
88 void ColorCurve::set_srgb()
90 unsigned char curve_data[256];
92 for(unsigned i=1; i<256; ++i)
93 curve_data[i] = to_srgb(i/255.0f)*255+0.5f;
94 curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
97 void ColorCurve::set_linear()
99 unsigned char curve_data[256];
100 for(unsigned i=0; i<256; ++i)
102 curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
105 void ColorCurve::render(const Texture2D &color_buf, const Texture2D &)
107 Bind _bind_shader(shprog);
109 Bind _bind_mesh(quad);
110 Bind _bind_tex(color_buf);
111 Bind _bind_curve(curve, 1);