Hello,

I'm writing a simple color corrector to teach myself C#, and have come across a couple of performance problems.

First, I am using Bitmap.LockBits to loop through the pixels (this part seems fast enough!). The problem I am having is having to do if statements in the loop to clamp colors if they're out of range (0-255), and also it seems Math.Pow( ) is really, really slow (which forces me to do an additional if inside the loop, even!)

Here's sample code (for blue, so this is repeated for red and green as well) for the math performed per pixel, per component:

if (tmpBlueGamma != 1.0) { tmp = (float)((Math.Pow((double)(srcR[x * pixelSize] * toFloat), tmpBlueGamma)) * tmpBlueGain + tmpBlueOffset) * 255.0f; } else { tmp = ((float)(srcR[x * pixelSize]) * tmpBlueGain + tmpBlueOffset); } if (tmp < 0.0f) tmp = 0.0f; if (tmp > 255.0f) tmp = 255.0f; row[x * pixelSize] = (byte)tmp;

I realize I could do the math in byte instead of float (except for that darn Math.Pow requiring (double)), but that's not where I lose most time. If I take out the Gamma calculation, things speed up considerably; if I also remove clamping, everything is instantaneous. Any help greatly appreciated.