-1
\$\begingroup\$

For my android application, I want to apply brightness and contrast shader on same image.

At present I am using gpuimage plugin. In that I found two separate program for brightness and contrast as per the following.

Contrast shader:

    varying highp vec2 textureCoordinate;

    uniform sampler2D inputImageTexture;
    uniform lowp float contrast;

    void main()
    {
         lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);

         gl_FragColor = vec4(((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5)), textureColor.w);
    }

Brightness shader:

    varying highp vec2 textureCoordinate;

    uniform sampler2D inputImageTexture;
    uniform lowp float brightness;

    void main()
    {
        lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);

        gl_FragColor = vec4((textureColor.rgb + vec3(brightness)), textureColor.w);
    }

Now applying both of the effects I write following code

    varying highp vec2 textureCoordinate;
    uniform sampler2D inputImageTexture;
    varying highp vec2 textureCoordinate2;
    uniform sampler2D inputImageTexture2;
    uniform lowp float contrast;
    uniform lowp float brightness;

    void main()
    {
        lowp vec4 textureColorForContrast = texture2D(inputImageTexture, textureCoordinate);

        lowp vec4 contastVec4 = vec4(((textureColorForContrast.rgb - vec3(0.5)) * contrast + vec3(0.5)), textureColorForContrast.w);
        lowp vec4 textureColorForBrightness = texture2D(inputImageTexture2, textureCoordinate2);

        lowp vec4 brightnessVec4 = vec4((textureColorForBrightness.rgb + vec3(brightness)), textureColorForBrightness.w);
        gl_FragColor = contastVec4 + brightnessVec4;
    }

Doesn't able to get desire result. I can't able to figure out what I have to do next? So please friends help me in this. What program I have to write?

\$\endgroup\$
1
  • \$\begingroup\$ This question is a intra-network cross-post. Please don't cross-post between StackExchange sites. \$\endgroup\$ Commented Jun 16, 2014 at 16:40

1 Answer 1

2
\$\begingroup\$

Try:

varying highp vec2 textureCoordinate;

uniform sampler2D inputImageTexture;
uniform lowp float brightness;
uniform lowp float contrast;

void main()
{
    lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
    textureColor.rgb = ((textureColor.rgb - vec3(0.5)) * contrast + vec3(0.5));
    gl_FragColor = vec4((textureColor.rgb + vec3(brightness)), textureColor.w);
}

EDIT: The reason why your shader was not working, is because you were doubling the input too. If you think of it, the first shaders apply ONE effect to ONE image. When merging both, you'd expect TWO effects on ONE image. But what you tried to do was applying TWO effects on TWO images. You just had to get rid of the second texture and UV parameters, then apply both effects on the single texture remaining.

\$\endgroup\$
3
  • \$\begingroup\$ Downvoter, care to comment? \$\endgroup\$ Commented Jun 12, 2014 at 16:33
  • \$\begingroup\$ Sorry at present I can't give you reply because my eclipse not working properly because adt update and it frustrating me. \$\endgroup\$ Commented Jun 12, 2014 at 16:56
  • \$\begingroup\$ your answer is correct and worked as expected. thanks for help \$\endgroup\$ Commented Jun 18, 2014 at 15:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.