LitDev.Engines.FIP.InverseImage C# (CSharp) Method

InverseImage() public method

Converts ARGB image to greyscale using luminance method and inverses it with respect to given threshold
public InverseImage ( Bitmap OriginalImage, int threshold ) : Bitmap
OriginalImage System.Drawing.Bitmap Original ARGB image
threshold int Inverse threshold
return System.Drawing.Bitmap
        public Bitmap InverseImage(Bitmap OriginalImage, int threshold)
        {
            FastPixel fpOriginal = new FastPixel(OriginalImage);
            Bitmap OutputImage = new System.Drawing.Bitmap(fpOriginal.Width, fpOriginal.Height);
            FastPixel fpOutput = new FastPixel(OutputImage);

            if (threshold < 1 || threshold > 254)
            {
                throw new Exception("Threshold value must be in range from 1 to 254");
            }

            for (int x = 0; x < fpOriginal.Width; x++)
            {
                for (int y = 0; y < fpOriginal.Height; y++)
                {
                    Color pixel = fpOriginal.GetPixel(x, y);
                    int gs1 = (int)((pixel.R * 0.3) + (pixel.G * 0.59) + (pixel.B * 0.11));

                    int distance = Math.Abs(threshold - gs1);

                    int gs = 0;

                    if (gs1 >= threshold) gs = threshold - distance;
                    if (gs1 < threshold) gs = threshold + distance;

                    if (gs > 255) gs = 255;
                    if (gs < 0) gs = 0;

                    Color newColor = Color.FromArgb(255, gs, gs, gs);
                    fpOutput.SetPixel(x, y, newColor);
                }
            }

            fpOriginal.Unlock(false);
            fpOutput.Unlock(true);
            return OutputImage;
        }