Accord.Imaging.Filters.Texturer.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.GetPixelFormatSize(image.PixelFormat) / 8;

            // processing width and height
            int width = rect.Width;
            int height = rect.Height;

            // processing region's dimension
            int widthToProcess = width;
            int heightToProcess = height;

            // if generator was specified, then generate a texture
            // otherwise use provided texture
            if (textureGenerator != null)
            {
                texture = textureGenerator.Generate(width, height);
            }
            else
            {
                widthToProcess = Math.Min(width, texture.GetLength(1));
                heightToProcess = Math.Min(height, texture.GetLength(0));
            }

            int offset = image.Stride - widthToProcess * pixelSize;

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

            // allign pointer to the first pixel to process
            ptr += (rect.Top * image.Stride + rect.Left * pixelSize);

            // texture
            for (int y = 0; y < heightToProcess; y++)
            {
                for (int x = 0; x < widthToProcess; x++)
                {
                    double t = texture[y, x];
                    // process each pixel
                    for (int i = 0; i < pixelSize; i++, ptr++)
                    {
                        *ptr = (byte)Math.Min(255.0f, (preserveLevel * (*ptr)) + (filterLevel * (*ptr)) * t);
                    }
                }
                ptr += offset;
            }
        }
    }