I am working on image editing using OPENGL
in Android and I have applied filter to an image using photoshop curve now I want to reproduce the same in Android using glsl
. Is there any formula to calculate single fragment color using photoshops curve output value?
EDIT
The math behind the photoshop curve has already been answered in this question
How to recreate the math behind photoshop curves but I am not very clear about how to reproduce the same in glsl fragment shader
.
Screen Shot of my photoshop curve
You're after a function
fragColour = curves(inColour, constants...)
. If you have just the one curve for red, green and blue you apply the same curve to each individually. This answer has a link (below) to code which plots points along the function. The key line is:Which you'd return from
curves
. The variablex
in the loop is yourinColour
. All you need now is the constants which come from thepoints
and the second derivativesd
arrays. These you'll have to pass in as uniforms. The function first has to figure out which point each colourx
is between (findingcur
,next
,sd[i]
andsd[i+1]
), then evaluate and returny
.EDIT:
If you just want to apply some curve you've created in photoshop then the problem is much simpler. The easiest way is to create a simple function that gives a similar shape. I use these as a starting point. A gamma correction curve is also quite common.
This is overkill, but if you do need a more exact result, you could create an image with a linear ramp (e.g. 255 pixels from black to white), apply your filter to it in photoshop and the result becomes a lookup table. Passing in all 255 values to a shader is expensive so if it's a smooth curve you could try some curve fitting tools (for example).
Once you have a function, simply apply it to your colour in GLSL. Applying a gamma curve for example is done like this:
EDIT2:
The curve you have looks very similar to this:
Or even simpler:
Just in case the link dies, I'll copy the code here (not that I've tested it):
And the second derivative: