|
// AForge Image Processing Library
//
// Copyright � Andrew Kirillov, 2005-2006
// andrew.kirillov@gmail.com
//
namespace AForge.Imaging.Filters
{
using System;
using System.Drawing;
using System.Drawing.Imaging;
/// <summary>
/// Move towards filter
/// </summary>
///
/// <remarks>Move source image towards overlay image by the specified value</remarks>
///
public class MoveTowards : FilterAnyToAny
{
private int stepSize = 1;
private Bitmap overlayImage;
/// <summary>
/// Step size. Default value is 1.
/// </summary>
public int StepSize
{
get { return stepSize; }
set { stepSize = Math.Max( 1, Math.Min( 255, value ) ); }
}
/// <summary>
/// Overlay image
/// </summary>
public Bitmap OverlayImage
{
get { return overlayImage; }
set { overlayImage = value; }
}
/// <summary>
/// Initializes a new instance of the <see cref="MoveTowards"/> class
/// </summary>
public MoveTowards( ) { }
/// <summary>
/// Initializes a new instance of the <see cref="MoveTowards"/> class
/// </summary>
///
/// <param name="overlayImage">Overlay image</param>
///
public MoveTowards( Bitmap overlayImage )
{
this.overlayImage = overlayImage;
}
/// <summary>
/// Initializes a new instance of the <see cref="MoveTowards"/> class
/// </summary>
///
/// <param name="overlayImage">Overlay image</param>
/// <param name="stepSize">Step size</param>
///
public MoveTowards( Bitmap overlayImage, int stepSize )
{
this.overlayImage = overlayImage;
StepSize = stepSize;
}
/// <summary>
/// Process the filter on the specified image
/// </summary>
///
/// <param name="imageData">image data</param>
///
protected override unsafe void ProcessFilter( BitmapData imageData )
{
// source image and overlay must have same pixel format
if ( imageData.PixelFormat != overlayImage.PixelFormat )
throw new ArgumentException( "Source and overlay images must have same pixel format " );
// get image dimension
int width = imageData.Width;
int height = imageData.Height;
// check overlay image size
if ( ( width != overlayImage.Width ) || ( height != overlayImage.Height ) )
throw new ArgumentException( "Overlay image size should be equal to source image size" );
// lock overlay image
BitmapData ovrData = overlayImage.LockBits(
new Rectangle( 0, 0, width, height ),
ImageLockMode.ReadOnly, imageData.PixelFormat );
// initialize other variables
int pixelSize = ( imageData.PixelFormat == PixelFormat.Format8bppIndexed ) ? 1 : 3;
int lineSize = width * pixelSize;
int offset = imageData.Stride - lineSize;
// pixel value
int v;
// do the job
byte * ptr = (byte *) imageData.Scan0.ToPointer( );
byte * ovr = (byte *) ovrData.Scan0.ToPointer( );
// for each line
for ( int y = 0; y < height; y++ )
{
// for each pixel
for ( int x = 0; x < lineSize; x++, ptr++, ovr++ )
{
v = (int) *ovr - *ptr;
if ( v > 0 )
{
*ptr += (byte)( ( stepSize < v ) ? stepSize : v );
}
else if ( v < 0 )
{
v = -v;
*ptr -= (byte)( ( stepSize < v ) ? stepSize : v);
}
}
ptr += offset;
ovr += offset;
}
// unlock overlay image
overlayImage.UnlockBits( ovrData );
}
}
}
|
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.
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.