AForge.Imaging.Filters.EuclideanColorFiltering.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.PixelFormat == PixelFormat.Format24bppRgb ) ? 3 : 4;

            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;
            int radius2 = radius * radius;

            int dr, dg, db;
            // sphere's center
            int cR = center.Red;
            int cG = center.Green;
            int cB = center.Blue;
            // fill color
            byte fR = fill.Red;
            byte fG = fill.Green;
            byte fB = fill.Blue;

            // 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 )
                {
                    dr = cR - ptr[RGB.R];
                    dg = cG - ptr[RGB.G];
                    db = cB - ptr[RGB.B];

                    // calculate the distance
                    if ( dr * dr + dg * dg + db * db <= radius2 )
                    {
                        // inside sphere
                        if ( !fillOutside )
                        {
                            ptr[RGB.R] = fR;
                            ptr[RGB.G] = fG;
                            ptr[RGB.B] = fB;
                        }
                    }
                    else
                    {
                        // outside sphere
                        if ( fillOutside )
                        {
                            ptr[RGB.R] = fR;
                            ptr[RGB.G] = fG;
                            ptr[RGB.B] = fB;
                        }
                    }
                }
                ptr += offset;
            }
        }
    }