protected override unsafe void ProcessFilter( UnmanagedImage image, Rectangle rect )
{
int pixelSize = Bitmap.GetPixelFormatSize( image.PixelFormat ) / 8;
int startX = rect.Left;
int startY = rect.Top;
int stopX = startX + rect.Width;
int stopY = startY + rect.Height;
int offset = image.Stride - rect.Width * pixelSize;
RGB rgb = new RGB( );
HSL hsl = new HSL( );
float desaturationChangeFactor = 1.0f + adjustValue;
// do the job
byte* ptr = (byte*) image.ImageData.ToPointer( );
// allign pointer to the first pixel to process
ptr += ( startY * image.Stride + startX * pixelSize );
// for each row
for ( int y = startY; y < stopY; y++ )
{
// for each pixel
for ( int x = startX; x < stopX; x++, ptr += pixelSize )
{
rgb.Red = ptr[RGB.R];
rgb.Green = ptr[RGB.G];
rgb.Blue = ptr[RGB.B];
// convert to HSL
Accord.Imaging.HSL.FromRGB( rgb, hsl );
if ( adjustValue > 0 )
{
hsl.Saturation += ( 1.0f - hsl.Saturation ) * adjustValue * hsl.Saturation;
}
else if ( adjustValue < 0 )
{
hsl.Saturation *= desaturationChangeFactor;
}
// convert back to RGB
Accord.Imaging.HSL.ToRGB(hsl, rgb);
ptr[RGB.R] = rgb.Red;
ptr[RGB.G] = rgb.Green;
ptr[RGB.B] = rgb.Blue;
}
ptr += offset;
}
}
}