Help with Pixel Shader effect for brightness and contrast

Hans picture Hans · Jun 3, 2009 · Viewed 25.9k times · Source

What is a simple pixel shader script effect to apply brightness and contrast?

I found this one, but it doesn't seem to be correct:

sampler2D input : register(s0);
float brightness : register(c0);
float contrast : register(c1);

float4 main(float2 uv : TEXCOORD) : COLOR
{
    float4 color = tex2D(input, uv); 
    float4 result = color;
    result = color + brightness;
    result = result * (1.0+contrast)/1.0;

    return result;
}

thanks!

Answer

Walt Ritscher picture Walt Ritscher · Jun 12, 2010

Is this what you are looking for?

float Brightness : register(C0);
float Contrast :   register(C1);

sampler2D Texture1Sampler : register(S0);

float4 main(float2 uv : TEXCOORD) : COLOR
{

   float4 pixelColor = tex2D(Texture1Sampler, uv);
   pixelColor.rgb /= pixelColor.a;

  // Apply contrast.
  pixelColor.rgb = ((pixelColor.rgb - 0.5f) * max(Contrast, 0)) + 0.5f;

  // Apply brightness.
  pixelColor.rgb += Brightness;

  // Return final pixel color.
  pixelColor.rgb *= pixelColor.a;


 return pixelColor;
}

--- Tested with Shazzam Shader Editor http://shazzam-tool.com