Accord.Imaging.Filters.Invert.ProcessFilter C# (CSharp) Method

ProcessFilter() protected method

Process the filter on the specified image.
protected ProcessFilter ( UnmanagedImage image, Rectangle rect ) : void
image UnmanagedImage Source image data.
rect System.Drawing.Rectangle Image rectangle for processing by the filter.
return void
        protected override unsafe void ProcessFilter( UnmanagedImage image, Rectangle rect )
        {
            int pixelSize = ( ( image.PixelFormat == PixelFormat.Format8bppIndexed ) ||
                              ( image.PixelFormat == PixelFormat.Format16bppGrayScale ) ) ? 1 : 3;

            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
            {
                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;
                }
            }
        }
    }