Accord.Imaging.Filters.ColorFiltering.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;

            // do the job
            byte* ptr = (byte*)image.ImageData.ToPointer();
            byte r, g, b;

            // 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)
                {
                    r = ptr[RGB.R];
                    g = ptr[RGB.G];
                    b = ptr[RGB.B];

                    // check pixel
                    if (
                        (r >= red.Min) && (r <= red.Max) &&
                        (g >= green.Min) && (g <= green.Max) &&
                        (b >= blue.Min) && (b <= blue.Max)
                        )
                    {
                        if (!fillOutsideRange)
                        {
                            ptr[RGB.R] = fillR;
                            ptr[RGB.G] = fillG;
                            ptr[RGB.B] = fillB;
                        }
                    }
                    else
                    {
                        if (fillOutsideRange)
                        {
                            ptr[RGB.R] = fillR;
                            ptr[RGB.G] = fillG;
                            ptr[RGB.B] = fillB;
                        }
                    }
                }
                ptr += offset;
            }
        }
    }