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

ProcessFilter() protected method

Process the filter on the specified image.
protected ProcessFilter ( UnmanagedImage image, UnmanagedImage overlay ) : void
image UnmanagedImage Source image data.
overlay UnmanagedImage Overlay image data.
return void
        protected override unsafe void ProcessFilter(UnmanagedImage image, UnmanagedImage overlay)
        {
            PixelFormat pixelFormat = image.PixelFormat;
            int width = image.Width;
            int height = image.Height;

            if ((pixelFormat == PixelFormat.Format8bppIndexed) ||
                (pixelFormat == PixelFormat.Format24bppRgb) ||
                (pixelFormat == PixelFormat.Format32bppRgb) ||
                (pixelFormat == PixelFormat.Format32bppArgb))
            {

                // initialize other variables
                var pixelSize = (pixelFormat == PixelFormat.Format8bppIndexed) ? 1 :
                    (pixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
                var lineSize = width * pixelSize;
                var srcOffset = image.Stride - lineSize;
                var ovrOffset = overlay.Stride - lineSize;
                // new pixel value

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

                // for each line
                for (var y = 0; y < height; y++)
                {
                    // for each pixel
                    for (var x = 0; x < lineSize; x++, ptr++, ovr++)
                        *ptr = (byte)((*ptr * *ovr) / 255);
                    ptr += srcOffset;
                    ovr += ovrOffset;
                }
            }
            else
            {
                // initialize other variables
                int pixelSize = (pixelFormat == PixelFormat.Format16bppGrayScale) ? 1 :
                    (pixelFormat == PixelFormat.Format48bppRgb) ? 3 : 4;
                int lineSize = width * pixelSize;
                int srcStride = image.Stride;
                int ovrStride = overlay.Stride;
                // new pixel value

                // do the job
                var basePtr = (byte*)image.ImageData.ToPointer();
                var baseOvr = (byte*)overlay.ImageData.ToPointer();

                // for each line
                for (var y = 0; y < height; y++)
                {
                    var ptr = (ushort*)(basePtr + y * srcStride);
                    var ovr = (ushort*)(baseOvr + y * ovrStride);

                    // for each pixel
                    for (var x = 0; x < lineSize; x++, ptr++, ovr++)
                        *ptr = (ushort)((*ptr * *ovr) / 65535);
                }
            }
        }
    }