protected override unsafe void ProcessFilter( UnmanagedImage source, UnmanagedImage destination, Rectangle rect )
{
// processing start and stop X,Y positions
int startX = rect.Left;
int startY = rect.Top;
int stopX = startX + rect.Width;
int stopY = startY + rect.Height;
int srcStride = source.Stride;
int dstStride = destination.Stride;
int srcOffset = srcStride - rect.Width;
int start;
// do the job
byte* src0 = (byte*) source.ImageData.ToPointer( );
byte* dst0 = (byte*) destination.ImageData.ToPointer( );
byte* src = src0;
byte* dst = dst0;
// horizontal pass
// allign pointers to the first pixel to process
src += ( startY * srcStride + startX );
dst += ( startY * dstStride );
// for each line
for ( int y = startY; y < stopY; y++ )
{
// make destination image filled with background color
AForge.SystemTools.SetUnmanagedMemory( dst + startX, bg, stopX - startX );
start = -1;
// for each pixel
for ( int x = startX; x < stopX; x++, src++ )
{
// looking for foreground pixel
if ( start == -1 )
{
if ( *src == fg )
start = x;
continue;
}
// looking for non foreground pixel
if ( *src != fg )
{
dst[start + ( ( x - start ) >> 1 )] = (byte) fg;
start = -1;
}
}
if ( start != -1 )
{
dst[start + ( ( stopX - start ) >> 1 )] = (byte) fg;
}
src += srcOffset;
dst += dstStride;
}
// vertical pass
// allign pointer to the first line to process
src0 += ( startY * srcStride );
// for each column
for ( int x = startX; x < stopX; x++ )
{
src = src0 + x;
dst = dst0 + x;
start = -1;
// for each row
for ( int y = startY; y < stopY; y++, src += srcStride )
{
// looking for foreground pixel
if ( start == -1 )
{
if ( *src == fg )
start = y;
continue;
}
// looking for non foreground pixel
if ( *src != fg )
{
dst[dstStride * ( start + ( ( y - start ) >> 1 ) )] = (byte) fg;
start = -1;
}
}
if ( start != -1 )
{
dst[dstStride * ( start + ( ( stopY - start ) >> 1 ) )] = (byte) fg;
}
}
}
}