BiTonal

This algorithm converts specified image to binary image that has only two possible color values.

   



									Public Shared Sub ApplyBiTonal(ByRef bmp As Bitmap, thresholdValue As Short, upperColor As System.Drawing.Color, lowerColor As System.Drawing.Color)
	Dim MaxVal As Integer = 768

	If thresholdValue < 0 Then
		Return
	ElseIf thresholdValue > MaxVal Then
		Return
	End If

	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
	Dim TotalRGB As Integer

	While CInt(ptr) <> stopAddress
		TotalRGB = CInt(Marshal.ReadByte(ptr)) + CInt(Marshal.ReadByte(ptr + 1)) + CInt(Marshal.ReadByte(ptr + 2))

		If TotalRGB <= thresholdValue Then
			Marshal.WriteByte(ptr + 2, lowerColor.R)
			Marshal.WriteByte(ptr + 1, lowerColor.G)
			Marshal.WriteByte(ptr, lowerColor.B)
		Else
			Marshal.WriteByte(ptr + 2, upperColor.R)
			Marshal.WriteByte(ptr + 1, upperColor.G)
			Marshal.WriteByte(ptr, upperColor.B)
		End If

		ptr += 3
	End While

	bmp.UnlockBits(bmpData)
End Sub
								


Example

									DIm b As Bitmap = CType(Image.FromFile("rose.jpg"), Bitmap)
ApplyBiTonal(b, (Byte.MaxValue * 3) / 2, System.Drawing.Color.Red, System.Drawing.Color.White)