Do you think the following one is best for adjusting Contrast level? In fact, I would like to get the same/close perfromance for adjusting contrast as found in Photoshop.
public static Bitmap AdjustContrast(Bitmap Image, float Value)
{
Value = (100.0f + Value) / 100.0f;
Value *= Value;
System.Drawing.Bitmap NewBitmap = Image;
for (int x = 0; x < NewBitmap.Width; ++x)
{
for (int y = 0; y < NewBitmap.Height; ++y)
{
Color Pixel = NewBitmap.GetPixel(x, y);
float Red = Pixel.R / 255.0f;
float Green = Pixel.G / 255.0f;
float Blue = Pixel.B / 255.0f;
Red = (((Red - 0.5f) * Value) + 0.5f) * 255.0f;
Green = (((Green - 0.5f) * Value) + 0.5f) * 255.0f;
Blue = (((Blue - 0.5f) * Value) + 0.5f) * 255.0f;
NewBitmap.SetPixel(x, y, Color.FromArgb(Clamp((int)Red, 255, 0), Clamp((int)Green, 255, 0), Clamp((int)Blue, 255, 0)));
}
}
return NewBitmap;
}
public static T Clamp<T>(T Value, T Max, T Min)
where T : System.IComparable<T>
{
if (Value.CompareTo(Max) > 0)
return Max;
if (Value.CompareTo(Min) < 0)
return Min;
return Value;
}
The above code is not mine and I forgot the source of the code.