AForge.Imaging.Filters.SimplePosterization.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 )
        {
            // get pixel size
            int pixelSize = Image.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;

            // calculate posterization offset
            int posterizationOffset = ( fillingType == PosterizationFillingType.Min ) ?
                0 : ( ( fillingType == PosterizationFillingType.Max ) ?
                posterizationInterval - 1 : posterizationInterval / 2 );

            // calculate mapping array
            byte[] map = new byte[256];

            for ( int i = 0; i < 256; i++ )
            {
                map[i] = (byte) Math.Min( 255, ( i / posterizationInterval ) * posterizationInterval + posterizationOffset );
            }

            // do the job
            byte* ptr = (byte*) image.ImageData.ToPointer( );

            // allign pointer to the first pixel to process
            ptr += ( startY * image.Stride + startX * pixelSize );

            // check image format
            if ( image.PixelFormat == PixelFormat.Format8bppIndexed )
            {
                // for each line
                for ( int y = startY; y < stopY; y++ )
                {
                    // for each pixel in line
                    for ( int x = startX; x < stopX; x++, ptr++ )
                    {
                        *ptr = map[*ptr];
                    }
                    ptr += offset;
                }
            }
            else
            {
                // for each line
                for ( int y = startY; y < stopY; y++ )
                {
                    // for each pixel in line
                    for ( int x = startX; x < stopX; x++, ptr += pixelSize )
                    {
                        ptr[RGB.R] = map[ptr[RGB.R]];
                        ptr[RGB.G] = map[ptr[RGB.G]];
                        ptr[RGB.B] = map[ptr[RGB.B]];
                    }
                    ptr += offset;
                }
            }
        }
    }