tags:

views:

525

answers:

2

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!

A: 

That contrast calculation looks wrong to me. If you leave it out, does your brightness work ok?

rikh
not... if you set a low value, it becomes white
A: 

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

Walt Ritscher
Hmmm. I thought this question was from this week, now I see that it was asked a year ago.
Walt Ritscher