protected override unsafe void ProcessFilter( UnmanagedImage image, UnmanagedImage overlay )
{
PixelFormat pixelFormat = image.PixelFormat;
// get image dimension
int width = image.Width;
int height = image.Height;
// pixel value
int v;
if (
( pixelFormat == PixelFormat.Format8bppIndexed ) ||
( pixelFormat == PixelFormat.Format24bppRgb ) ||
( pixelFormat == PixelFormat.Format32bppRgb ) ||
( pixelFormat == PixelFormat.Format32bppArgb ) )
{
// initialize other variables
int pixelSize = ( pixelFormat == PixelFormat.Format8bppIndexed ) ? 1 :
( pixelFormat == PixelFormat.Format24bppRgb ) ? 3 : 4;
int lineSize = width * pixelSize;
int srcOffset = image.Stride - lineSize;
int ovrOffset = overlay.Stride - lineSize;
// do the job
byte * ptr = (byte*) image.ImageData.ToPointer( );
byte * ovr = (byte*) overlay.ImageData.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 += srcOffset;
ovr += ovrOffset;
}
}
else
{
// initialize other variables
int pixelSize = ( pixelFormat == PixelFormat.Format16bppGrayScale ) ? 1 :
( pixelFormat == PixelFormat.Format48bppRgb ) ? 3 : 4;
int lineSize = width * pixelSize;
int srcStride = image.Stride;
int ovrStride = overlay.Stride;
// do the job
byte* basePtr = (byte*) image.ImageData.ToPointer( );
byte* baseOvr = (byte*) overlay.ImageData.ToPointer( );
// for each line
for ( int y = 0; y < height; y++ )
{
ushort * ptr = (ushort*) ( basePtr + y * srcStride );
ushort * ovr = (ushort*) ( baseOvr + y * ovrStride );
// for each pixel
for ( int x = 0; x < lineSize; x++, ptr++, ovr++ )
{
v = (int) *ovr - *ptr;
if ( v > 0 )
{
*ptr += (ushort) ( ( stepSize < v ) ? stepSize : v );
}
else if ( v < 0 )
{
v = -v;
*ptr -= (ushort) ( ( stepSize < v ) ? stepSize : v );
}
}
}
}
}
}