protected unsafe override void ProcessFilter( UnmanagedImage image, Rectangle rect )
{
int pixelSize = 3;
if((image.PixelFormat == PixelFormat.Format8bppIndexed) || (image.PixelFormat == PixelFormat.Format16bppGrayScale))
{
pixelSize = 1;
}
else if (image.PixelFormat == PixelFormat.Format32bppArgb)
{
pixelSize = 4;
}
int startY = rect.Top;
int stopY = startY + rect.Height;
int startX = rect.Left * pixelSize;
int stopX = startX + rect.Width * pixelSize;
byte* basePtr = (byte*) image.ImageData.ToPointer( );
if (
( image.PixelFormat == PixelFormat.Format8bppIndexed ) ||
( image.PixelFormat == PixelFormat.Format24bppRgb ) )
{
int offset = image.Stride - ( stopX - startX );
// allign pointer to the first pixel to process
byte* ptr = basePtr + ( startY * image.Stride + rect.Left * pixelSize );
// invert
for ( int y = startY; y < stopY; y++ )
{
for ( int x = startX; x < stopX; x++, ptr++ )
{
// ivert each pixel
*ptr = (byte) ( 255 - *ptr );
}
ptr += offset;
}
}
else if (image.PixelFormat == PixelFormat.Format32bppArgb)
{
int offset = image.Stride - (stopX - startX);
// allign pointer to the first pixel to process
byte* ptr = basePtr + (startY * image.Stride + rect.Left * pixelSize);
// invert
for (int y = startY; y < stopY; y++)
{
for (int x = startX; x < stopX; x++, ptr++)
{
if((x+1) % 4 != 0)
*ptr = (byte)(255 - *ptr);
}
ptr += offset;
}
}
else
{
int stride = image.Stride;
// allign pointer to the first pixel to process
basePtr += (startY * image.Stride + rect.Left * pixelSize * 2);
// invert
for (int y = startY; y < stopY; y++)
{
ushort* ptr = (ushort*)(basePtr);
for (int x = startX; x < stopX; x++, ptr++)
{
// ivert each pixel
*ptr = (ushort)(65535 - *ptr);
}
basePtr += stride;
}
}
}