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));
50 shdata.uniform("texture", 0);
51 shdata.uniform("curve", 1);
53 curve.storage(LUMINANCE, 256);
54 curve.set_min_filter(LINEAR);
55 curve.set_wrap(CLAMP_TO_EDGE);
62 void ColorCurve::set_peak(float p)
65 throw invalid_argument("ColorCurve::set_peak");
66 shdata.uniform("peak", p);
69 void ColorCurve::set_brightness(float b)
72 throw invalid_argument("ColorCurve::set_brightness");
73 shdata.uniform("brightness", b);
76 void ColorCurve::set_gamma(float g)
79 throw invalid_argument("ColorCurve::set_gamma");
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);
87 void ColorCurve::set_srgb()
89 unsigned char curve_data[256];
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);
96 void ColorCurve::set_linear()
98 unsigned char curve_data[256];
99 for(unsigned i=0; i<256; ++i)
101 curve.image(0, LUMINANCE, UNSIGNED_BYTE, curve_data);
104 void ColorCurve::render(const Texture2D &color_buf, const Texture2D &)
106 Bind _bind_shader(shprog);
108 Bind _bind_tex(color_buf);
109 Bind _bind_curve(curve, 1);