Gamma

This algorithm performs gamma correction of specified image in RGB color space.

   



									Public Shared Sub ApplyGamma(ByRef bmp As Bitmap, redComponent As Double, greenComponent As Double, blueComponent As Double)
	If redComponent < 0.2 OrElse redComponent > 5 Then Return
	If greenComponent < 0.2 OrElse greenComponent > 5 Then Return
	If blueComponent < 0.2 OrElse blueComponent > 5 Then Return

	Dim bmpData As BitmapData = bmp.LockBits(New Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb)
	Dim ptr As IntPtr = bmpData.Scan0
	Dim stopAddress As Integer = CInt(ptr) + bmpData.Stride * bmpData.Height

	While CInt(ptr) <> stopAddress
		Marshal.WriteByte(ptr, CByte(Math.Min(255, CInt(Math.Truncate((255.0 * Math.Pow(Marshal.ReadByte(ptr) / 255.0, 1.0 / blueComponent)) + 0.5)))))
		Marshal.WriteByte(ptr + 1, CByte(Math.Min(255, CInt(Math.Truncate((255.0 * Math.Pow(Marshal.ReadByte(ptr + 1) / 255.0, 1.0 / greenComponent)) + 0.5)))))
		Marshal.WriteByte(ptr + 2, CByte(Math.Min(255, CInt(Math.Truncate((255.0 * Math.Pow(Marshal.ReadByte(ptr + 2) / 255.0, 1.0 / redComponent)) + 0.5)))))

		ptr += 3
	End While

	bmp.UnlockBits(bmpData)
End Sub
								


Example

									DIm b As Bitmap = CType(Image.FromFile("rose.jpg"), Bitmap)
ApplyGamma(b, 2, 1.6, 0.8)