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

ProcessFilter() protected method

Process the filter on the specified image.
protected ProcessFilter ( UnmanagedImage sourceData, UnmanagedImage destinationData, Rectangle rect ) : void
sourceData UnmanagedImage Source image data.
destinationData UnmanagedImage Destination image data.
rect System.Drawing.Rectangle Image rectangle for processing by the filter.
return void
        protected override unsafe void ProcessFilter(UnmanagedImage sourceData, UnmanagedImage destinationData, Rectangle rect)
        {
            // processing start and stop X,Y positions
            int startX = rect.Left + 1;
            int startY = rect.Top + 1;
            int stopX = startX + rect.Width - 2;
            int stopY = startY + rect.Height - 2;

            int dstStride = destinationData.Stride;
            int srcStride = sourceData.Stride;

            int dstOffset = dstStride - rect.Width + 2;
            int srcOffset = srcStride - rect.Width + 2;

            int d, max;

            // data pointers
            byte* src = (byte*)sourceData.ImageData.ToPointer();
            byte* dst = (byte*)destinationData.ImageData.ToPointer();

            // allign pointers
            src += srcStride * startY + startX;
            dst += dstStride * startY + startX;

            // for each line
            for (int y = startY; y < stopY; y++)
            {
                // for each pixel
                for (int x = startX; x < stopX; x++, src++, dst++)
                {
                    // left diagonal
                    max = (int)src[-srcStride - 1] - src[srcStride + 1];
                    if (max < 0)
                        max = -max;

                    // right diagonal
                    d = (int)src[-srcStride + 1] - src[srcStride - 1];
                    if (d < 0)
                        d = -d;
                    if (d > max)
                        max = d;
                    // vertical
                    d = (int)src[-srcStride] - src[srcStride];
                    if (d < 0)
                        d = -d;
                    if (d > max)
                        max = d;
                    // horizontal
                    d = (int)src[-1] - src[1];
                    if (d < 0)
                        d = -d;
                    if (d > max)
                        max = d;

                    *dst = (byte)max;
                }
                src += srcOffset;
                dst += dstOffset;
            }

            // draw black rectangle to remove those pixels, which were not processed
            // (this needs to be done for those cases, when filter is applied "in place" -
            // source image is modified instead of creating new copy)
            Drawing.Rectangle(destinationData, rect, Color.Black);
        }
    }