Is this formula repetitive or optimal?

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?

// Basically, b is from Image A, and t from Image B
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]

// Input: 137 and 113

// Byte version:
int other = ((byte)((B < 128) ? (2 * ((A >> 1) + 64)) * ((float)B / 255) : (255 - (2 * (255 - ((A >> 1) + 64)) * (float)(255 - B) / 255))));
// Returns 116    


// float version:
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))));
// Returns 129

[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)));          
}
+5
source share
1

: ?

.

#define ChannelBlend_SoftLight(A,B) ((uint8)((B < 128)?(2*((A>>1)+64))*((float)B/255):(255-(2*(255-((A>>1)+64))*(float)(255-B)/255)))) //not very accurate

- 2 ( , ). , , .

( ​​ this, , , ...

#define ChannelBlend_SoftLight(A,B) (uint8)(((A < 128) ? (2 * ((B >> 1) + 64)) * ((float) A / 255) : (255 - (2 * (255 - ((B >> 1) + 64)) * (float) (255 - A) / 255))));
+4

All Articles