I started coding an image processing program from various image processing algorithms, mainly from Rene Schulte, and when benchmarking, I noticed that of all the effects that I could find from different sources, the code for applying "Softlight" was used. The effect was the slowest . I don’t know how to optimize the equation, but I feel that the filter is based on a formula that possibly repeats variables for no reason.
Can this be reduced to something shorter or faster?
int csoftLight(float b, float t)
{
b /= 255;
t /= 255;
return (int)((t < 0.5) ? 255 * ((1 - 2 * t) * b * b + 2 * t * b) : 255 * ((1 - (2 * t - 1)) * b + (2 * t - 1) * (Math.Pow(b, 0.5))));
}
[Edit - Results using the equation of Mohammed Hossein found about soft light in PS]
int other = ((byte)((B < 128) ? (2 * ((A >> 1) + 64)) * ((float)B / 255) : (255 - (2 * (255 - ((A >> 1) + 64)) * (float)(255 - B) / 255))));
int res = (int)((t < 0.5) ? 255 * ((1 - 2 * t) * b * b + 2 * t * b) : 255 * ((1 - (2 * t - 1)) * b + (2 * t - 1) * (Math.Pow(b, 0.5))));
[change]
Here's the fastest algorithm based on the answer of Mohammed Hossein:
int csoftLight(byte A, byte B)
{
return (int)((A < 128) ? (2 * ((B >> 1) + 64)) * ((float)A / 255) : (255 - (2 * (255 - ((B >> 1) + 64)) * (float)(255 - A) / 255)));
}
source
share