libcamera: shaders: Add support for contrast

Apply contrast after black-level and CCM operations.

Suggested-by: Milan Zamazal <mzamazal@redhat.com>
Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
Tested-by: Robert Mader <robert.mader@collabora.com>
Tested-by: Hans de Goede <johannes.goede@oss.qualcomm.com> # ThinkPad T14s gen 6 (arm64) ov02c10 + X1c gen 12 ov08x40
Tested-by: Kieran Bingham <kieran.bingham@ideasonboard.com> # Lenovo X13s
Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
This commit is contained in:
Bryan O'Donoghue
2026-01-06 17:00:46 +00:00
committed by Kieran Bingham
parent 46aac79a44
commit e4effc1e30
2 changed files with 37 additions and 0 deletions

View File

@@ -68,6 +68,16 @@ uniform sampler2D tex_y;
uniform mat3 ccm;
uniform vec3 blacklevel;
uniform float gamma;
uniform float contrastExp;
float apply_contrast(float value)
{
// Apply simple S-curve
if (value < 0.5)
return 0.5 * pow(value / 0.5, contrastExp);
else
return 1.0 - 0.5 * pow((1.0 - value) / 0.5, contrastExp);
}
void main(void)
{
@@ -261,6 +271,15 @@ void main(void)
rgb.g = (rin * ccm[1][0]) + (gin * ccm[1][1]) + (bin * ccm[1][2]);
rgb.b = (rin * ccm[2][0]) + (gin * ccm[2][1]) + (bin * ccm[2][2]);
/*
* Contrast
* contrastExp 0..2 contrast to 0..infinity; avoid actual infinity at tan(pi/2)
*/
rgb = clamp(rgb, 0.0, 1.0);
rgb.r = apply_contrast(rgb.r);
rgb.g = apply_contrast(rgb.g);
rgb.b = apply_contrast(rgb.b);
/* Apply gamma after colour correction */
rgb = pow(rgb, vec3(gamma));

View File

@@ -27,6 +27,16 @@ varying vec4 xCoord;
uniform mat3 ccm;
uniform vec3 blacklevel;
uniform float gamma;
uniform float contrastExp;
float apply_contrast(float value)
{
// Apply simple S-curve
if (value < 0.5)
return 0.5 * pow(value / 0.5, contrastExp);
else
return 1.0 - 0.5 * pow((1.0 - value) / 0.5, contrastExp);
}
void main(void) {
vec3 rgb;
@@ -156,6 +166,14 @@ void main(void) {
rgb.g = (rin * ccm[1][0]) + (gin * ccm[1][1]) + (bin * ccm[1][2]);
rgb.b = (rin * ccm[2][0]) + (gin * ccm[2][1]) + (bin * ccm[2][2]);
/*
* Contrast
*/
rgb = clamp(rgb, 0.0, 1.0);
rgb.r = apply_contrast(rgb.r);
rgb.g = apply_contrast(rgb.g);
rgb.b = apply_contrast(rgb.b);
/* Apply gamma after colour correction */
rgb = pow(rgb, vec3(gamma));