Click here to Skip to main content
15,886,026 members
Articles / Artificial Intelligence

AForge.NET open source framework

Rate me:
Please Sign up or sign in to vote.
4.97/5 (150 votes)
16 May 2007GPL311 min read 828.2K   48.3K   346  
The article describes an open source C# framework for researchers in the areas of Computer Vision and Artificial Intelligence - image processing, neural networks, genetic algorithms, etc.
// AForge Image Processing Library
//
// Copyright � Andrew Kirillov, 2005-2006
// andrew.kirillov@gmail.com
//
// Original idea from CxImage
// http://www.codeproject.com/bitmap/cximage.asp
//
namespace AForge.Imaging.Filters
{
	using System;
	using System.Drawing;
	using System.Drawing.Imaging;

	/// <summary>
	/// Dithering using Stevenson and Arce error diffusion
	/// </summary>
	public class StevensonArceDithering : ErrorDiffusionDithering
	{
		private static int[] coef1 = new int[] { 12, 26, 30, 16 };
//		private static int[] coef2 = new int[] { 5, 12, 12, 26, 12, 12, 5 };
		private static int[] coef2 = new int[] { 12, 26, 12 };
		private static int[] coef3 = new int[] { 5, 12, 12, 5 };

		/// <summary>
		/// Do error diffusion
		/// </summary>
		/// 
		/// <param name="error">Current error value</param>
		/// <param name="ptr">Pointer to current processing pixel</param>
		/// 
		/// <remarks>All parameters of the image and current processing pixel's coordinates
		/// are initialized by base class.</remarks>
		/// 
		protected override unsafe void Diffuse( int error, byte * ptr )
		{
			int ed;	// error diffusion

			// calculate error diffusion
			if ( x < widthM1 - 1 )
			{
				// right + 1 pixel
				ed = ptr[2] + ( error * 32 ) / 200;
				ed = ( ed < 0 ) ? 0 : ( ( ed > 255 ) ? 255 : ed );
				ptr[2] = (byte) ed;
			}

			if ( y < heightM1 )
			{
				// bottom pixels
				ptr += stride;
				for ( int i = -3, j = 0; i <= 3; i += 2, j++ )
				{
					if ( ( x + i >= 0 ) && ( x + i < width ) )
					{
						ed = ptr[i] + ( error * coef1[j] ) / 200;
						ed = ( ed < 0 ) ? 0 : ( ( ed > 255 ) ? 255 : ed );
						ptr[i] = (byte) ed;
					}
				}
			}

			if ( y < heightM1 - 1 )
			{
				// bottom + 1 pixels
				ptr += stride;
				for ( int i = -2, j = 0; i <= 2; i += 2, j++ )
				{
					if ( ( x + i >= 0 ) && ( x + i < width ) )
					{
						ed = ptr[i] + ( error * coef2[j] ) / 200;
						ed = ( ed < 0 ) ? 0 : ( ( ed > 255 ) ? 255 : ed );
						ptr[i] = (byte) ed;
					}
				}
			}

			if ( y < heightM1 - 2 )
			{
				// bottom + 2 pixels
				ptr += stride;
				for ( int i = -3, j = 0; i <= 3; i += 2, j++ )
				{
					if ( ( x + i >= 0 ) && ( x + i < width ) )
					{
						ed = ptr[i] + ( error * coef3[j] ) / 200;
						ed = ( ed < 0 ) ? 0 : ( ( ed > 255 ) ? 255 : ed );
						ptr[i] = (byte) ed;
					}
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer IBM
United Kingdom United Kingdom
Started software development at about 15 years old and it seems like now it lasts most part of my life. Fortunately did not spend too much time with Z80 and BK0010 and switched to 8086 and further. Similar with programming languages – luckily managed to get away from BASIC and Pascal to things like Assembler, C, C++ and then C#. Apart from daily programming for food, do it also for hobby, where mostly enjoy areas like Computer Vision, Robotics and AI. This led to some open source stuff like AForge.NET, Computer Vision Sandbox, cam2web, ANNT, etc.

Comments and Discussions