BiTonal

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

   



									public static void ApplyBiTonal(ref Bitmap bmp, short thresholdValue, System.Drawing.Color upperColor, System.Drawing.Color lowerColor)
{
	int MaxVal = 768;

	if (thresholdValue < 0) return;
	else if (thresholdValue > MaxVal) return;

	BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

	unsafe
	{
		int TotalRGB;

		byte* ptr = (byte*)bmpData.Scan0.ToPointer();
		int stopAddress = (int)ptr + bmpData.Stride * bmpData.Height;

		while ((int)ptr != stopAddress)
		{
			TotalRGB = ptr[0] + ptr[1] + ptr[2];

			if (TotalRGB <= thresholdValue)
			{
				ptr[2] = lowerColor.R;
				ptr[1] = lowerColor.G;
				ptr[0] = lowerColor.B;
			}
			else
			{
				ptr[2] = upperColor.R;
				ptr[1] = upperColor.G;
				ptr[0] = upperColor.B;
			}

			ptr += 3;
		}
	}

	bmp.UnlockBits(bmpData);
}
								


Example

									Bitmap b = (Bitmap)Image.FromFile("rose.jpg");
ApplyBiTonal(ref b, (byte.MaxValue * 3) / 2, System.Drawing.Color.Red, System.Drawing.Color.White);