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

ProcessFilter() protected method

Process the filter on the specified image.
protected ProcessFilter ( UnmanagedImage image ) : void
image Accord.Imaging.UnmanagedImage Source image data.
return void
        protected unsafe override void ProcessFilter(UnmanagedImage image)
        {
            int width = image.Width;
            int height = image.Height;

            int pixelSize = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int stride = image.Stride;
            int offset = stride - image.Width * pixelSize;

            byte* src = (byte*)image.ImageData.ToPointer();

            // Get maximum color image values
            int maxR = 0, maxG = 0, maxB = 0;
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    if (src[RGB.R] > maxR) maxR = src[RGB.R];
                    if (src[RGB.G] > maxG) maxG = src[RGB.G];
                    if (src[RGB.B] > maxB) maxB = src[RGB.B];
                }
            }

            double kr = maxR > 0 ? (255.0 / maxR) : 0;
            double kg = maxG > 0 ? (255.0 / maxG) : 0;
            double kb = maxB > 0 ? (255.0 / maxB) : 0;


            src = (byte*)image.ImageData.ToPointer();
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++, src += pixelSize)
                {
                    double r = kr * src[RGB.R];
                    double g = kg * src[RGB.G];
                    double b = kb * src[RGB.B];

                    src[RGB.R] = (byte)(r > 255 ? 255 : r);
                    src[RGB.G] = (byte)(g > 255 ? 255 : g);
                    src[RGB.B] = (byte)(b > 255 ? 255 : b);
                }

                src += offset;
            }
        }
    }