How do you write efficient image editing code in Visual Basic 2008
(Running in .NET 2.0 mode)? I'm writing a simple image editing program, and I have this code for increasing brightness:
Dim R As Double, G As Double, B As Double, Colour As Color
Dim NewImage As Bitmap = Image.Clone()
For x As Integer = 0 To NewImage.Width - 1
For y As Integer = 0 To NewImage.Height - 1
Colour = NewImage.GetPixel(x, y)
R = CDbl(Colour.R) / 255
G = CDbl(Colour.G) / 255
B = CDbl(Colour.B) / 255
R = Math.Sqrt((R ^ (1 / Factor)) * (1 - (1 - R) ^ Factor))
G = Math.Sqrt((G ^ (1 / Factor)) * (1 - (1 - G) ^ Factor))
B = Math.Sqrt((B ^ (1 / Factor)) * (1 - (1 - B) ^ Factor))
NewImage.SetPixel(x, y, Color.FromArgb(CInt(255 * R), CInt(255 * G), CInt(255 * B)))
Next y
Next x
Image = NewImage
ReZoom()
However, this code takes about 10 seconds to run, which is too long. The processing needed for this is about the same as 3 gamma operations, and gamma increases take no time at all on other image editing programs. What should I do?
I've just done some tests, and it takes about 9 seconds if I comment out the 3 lines that actually changes the RGB values, and 11 seconds with this code in, so the majority of the time is just the getting and setting of the pixel values, which is unacceptable.
|