internal void rotateImage90CW()
        {
            preSave();
            Bitmap temp = new Bitmap(height, width);
            Bitmap bmp  = bitmap;

            unsafe
            {
                LockBitmap lockBitmap = new LockBitmap(bmp);
                lockBitmap.LockBits();

                LockBitmap lockBitmap2 = new LockBitmap(temp);
                lockBitmap2.LockBits();

                for (int y = 0; y < lockBitmap.Height; y++)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        Color pixel = lockBitmap.GetPixel(x, y);
                        lockBitmap2.SetPixel(y, x, pixel);
                    }
                }
                lockBitmap2.UnlockBits();
                lockBitmap.UnlockBits();
                pictureBox.Image = temp;
            }
            updateChange(temp);
            flipHorizontal();
        }
        public void flipVertical()
        {
            preSave();
            Bitmap temp = new Bitmap(width, height);
            Bitmap bmp  = bitmap;

            unsafe
            {
                LockBitmap lockBitmap = new LockBitmap(bmp);
                lockBitmap.LockBits();

                LockBitmap lockBitmap2 = new LockBitmap(temp);
                lockBitmap2.LockBits();

                for (int y = 0; y < lockBitmap.Height; y++)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        Color pixel = lockBitmap.GetPixel(x, height - y - 1);

                        lockBitmap2.SetPixel(x, y, pixel);
                    }
                }
                lockBitmap2.UnlockBits();
                lockBitmap.UnlockBits();
                pictureBox.Image = temp;
            }
            updateChange(temp);
        }
        public void invert()
        {
            preSave();
            Bitmap temp = new Bitmap(width, height);
            Bitmap bmp  = bitmap;

            unsafe
            {
                LockBitmap lockBitmap = new LockBitmap(bmp);
                lockBitmap.LockBits();

                LockBitmap lockBitmap2 = new LockBitmap(temp);
                lockBitmap2.LockBits();

                for (int y = 0; y < lockBitmap.Height; y++)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        Color pixel    = lockBitmap.GetPixel(x, y);
                        Color newPixel = Color.FromArgb(255 - pixel.R, 255 - pixel.G, 255 - pixel.B);
                        lockBitmap2.SetPixel(x, y, newPixel);
                    }
                }
                lockBitmap2.UnlockBits();
                lockBitmap.UnlockBits();
                pictureBox.Image = temp;
            }
            updateChange(temp);
        }
        public void convertToGray()
        {
            preSave();
            Bitmap temp = new Bitmap(width, height);
            Bitmap bmp  = bitmap;

            unsafe
            {
                LockBitmap lockBitmap = new LockBitmap(bmp);
                lockBitmap.LockBits();

                LockBitmap lockBitmap2 = new LockBitmap(temp);
                lockBitmap2.LockBits();

                for (int y = 0; y < lockBitmap.Height; y++)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        Color pixel     = lockBitmap.GetPixel(x, y);
                        int   grayscale = (int)(pixel.R * 0.3) + (int)(pixel.G * 0.59) + (int)(pixel.B * 0.11);
                        lockBitmap2.SetPixel(x, y, Color.FromArgb((int)(grayscale), (int)(grayscale), (int)(grayscale)));
                    }
                }
                lockBitmap2.UnlockBits();
                lockBitmap.UnlockBits();
                pictureBox.Image = temp;
            }
            grayscaleList.Push(true);
            updateChange(temp);
        }
        internal void updateBrightness(int value)
        {
            Bitmap backup = new Bitmap(width, height);
            Bitmap bmp    = bitmap;

            currentBrightness = value;
            unsafe
            {
                LockBitmap lockBitmap = new LockBitmap(bmp);
                lockBitmap.LockBits();

                LockBitmap lockBitmap2 = new LockBitmap(backup);
                lockBitmap2.LockBits();

                for (int y = 0; y < lockBitmap.Height; y++)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        Color c    = lockBitmap.GetPixel(x, y);
                        int   avgR = c.R + value;
                        int   avgB = c.B + value;
                        int   avgG = c.G + value;



                        if (avgB < 0)
                        {
                            avgB = 1;
                        }
                        if (avgR < 0)
                        {
                            avgR = 1;
                        }
                        if (avgG < 0)
                        {
                            avgG = 1;
                        }
                        if (avgB > 255)
                        {
                            avgB = 255;
                        }
                        if (avgR > 255)
                        {
                            avgR = 255;
                        }
                        if (avgG > 255)
                        {
                            avgG = 255;
                        }

                        lockBitmap2.SetPixel(x, y, Color.FromArgb((int)(avgR), (int)(avgG), (int)(avgB)));
                    }
                }
                lockBitmap2.UnlockBits();
                lockBitmap.UnlockBits();
                pictureBox.Image = backup;
                updateHistographh();
            }
        }
        internal Bitmap Erosion(Bitmap bitmap, int kernelWidth, int kernelHeight)
        {
            Bitmap backup = new Bitmap(width, height);
            Bitmap bmp    = bitmap;

            unsafe
            {
                LockBitmap lockBitmap = new LockBitmap(bmp);
                lockBitmap.LockBits();

                LockBitmap lockBitmap2 = new LockBitmap(backup);
                lockBitmap2.LockBits();

                for (int y = 0; y < lockBitmap.Height; y++)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        Color c = lockBitmap.GetPixel(x, y);

                        int maxR = 257;
                        int maxG = 257;
                        int maxB = 257;

                        for (int i = -1; i < kernelWidth - 1; i++)
                        {
                            for (int j = -1; j < kernelHeight - 1; j++)
                            {
                                if (x + i >= 0 && x + i < width && y + j >= 0 && y + j < height)
                                {
                                    Color c1 = lockBitmap.GetPixel(x + i, y + j);

                                    if (c1.R < maxR)
                                    {
                                        maxR = c1.R;
                                    }
                                    if (c1.G < maxG)
                                    {
                                        maxG = c1.G;
                                    }
                                    if (c1.B < maxB)
                                    {
                                        maxB = c1.B;
                                    }
                                }
                            }
                        }


                        lockBitmap2.SetPixel(x, y, Color.FromArgb((int)(maxR), (int)(maxG), (int)(maxB)));
                    }
                }
                lockBitmap2.UnlockBits();
                lockBitmap.UnlockBits();
            }
            return(backup);
        }
        internal void updateComponent(Boolean status, Boolean status2, Boolean status3)
        {
            redOn   = status;
            greenOn = status2;
            blueOn  = status3;

            Bitmap     temp   = new Bitmap(width, height);
            LockBitmap output = new LockBitmap(temp);
            LockBitmap input  = new LockBitmap(bitmap);

            unsafe
            {
                output.LockBits();
                input.LockBits();
                for (int y = 0; y < output.Height; y++)
                {
                    for (int x = 0; x < output.Width; x++)
                    {
                        Color c = input.GetPixel(x, y);
                        int   r = 0, g = 0, b = 0;
                        if (status == true)
                        {
                            r = c.R;
                        }
                        if (status2 == true)
                        {
                            g = c.G;
                        }
                        if (status3 == true)
                        {
                            b = c.B;
                        }
                        Color newpix = Color.FromArgb(r, g, b);
                        output.SetPixel(x, y, newpix);
                    }
                }
                input.UnlockBits();
                output.UnlockBits();
            }

            pictureBox.Image = temp;
            updateHistographh();
        }
        public static Bitmap getBComponent(Bitmap img)
        {
            Bitmap temp = new Bitmap(img.Width, img.Height);
            LockBitmap input = new LockBitmap(img);
            LockBitmap output = new LockBitmap(temp);

            input.LockBits();
            output.LockBits();
            for (int y = 0; y < input.Height; y++)
            {
                for (int x = 0; x < input.Width; x++)
                {
                    Color c = input.GetPixel(x, y);
                    Color newpix = Color.FromArgb(0, 0, c.B);
                    output.SetPixel(x, y, newpix);
                }
            }
            input.UnlockBits();
            output.UnlockBits();
            return temp;
        }
        public Bitmap colorHisto(Bitmap bitmap)
        {
            Bitmap rImage = utility.getRComponent(bitmap);
            Bitmap gImage = utility.getGComponent(bitmap);
            Bitmap bImage = utility.getBComponent(bitmap);

            Bitmap balancedR = HistEq(rImage);
            Bitmap balancedG = HistEq(gImage);
            Bitmap balancedB = HistEq(bImage);

            Bitmap res1 = new Bitmap(width, height);

            LockBitmap result  = new LockBitmap(res1);
            LockBitmap resultR = new LockBitmap(balancedR);
            LockBitmap resultG = new LockBitmap(balancedG);
            LockBitmap resultB = new LockBitmap(balancedB);

            result.LockBits();
            resultR.LockBits();
            resultG.LockBits();
            resultB.LockBits();
            for (int y = 0; y < result.Height; y++)
            {
                for (int x = 0; x < result.Width; x++)
                {
                    Color c = Color.FromArgb(resultR.GetPixel(x, y).B, resultG.GetPixel(x, y).B, resultB.GetPixel(x, y).B);
                    result.SetPixel(x, y, c);
                }
            }
            result.UnlockBits();
            resultR.UnlockBits();
            resultG.UnlockBits();
            resultB.UnlockBits();


            return(res1);
        }
        internal void updateContrast(int value)
        {
            Bitmap backup = new Bitmap(width, height);
            Bitmap bmp    = bitmap;

            currentContrast = value;
            double contrast = (100.0 + value) / value;

            unsafe
            {
                LockBitmap lockBitmap = new LockBitmap(bmp);
                lockBitmap.LockBits();

                LockBitmap lockBitmap2 = new LockBitmap(backup);
                lockBitmap2.LockBits();

                for (int y = 0; y < lockBitmap.Height; y++)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        Color c = lockBitmap.GetPixel(x, y);

                        double R = c.R / 255.0;
                        R -= 0.5;
                        R *= contrast;
                        R += 0.5;
                        R *= 255.0;
                        if (R < 0)
                        {
                            R = 0;
                        }
                        if (R > 255)
                        {
                            R = 255;
                        }


                        double G = c.G / 255.0;
                        G -= 0.5;
                        G *= contrast;
                        G += 0.5;
                        G *= 255.0;
                        if (G < 0)
                        {
                            G = 0;
                        }
                        if (G > 255)
                        {
                            G = 255;
                        }


                        double B = c.B / 255.0;
                        B -= 0.5;
                        B *= contrast;
                        B += 0.5;
                        B *= 255.0;
                        if (B < 0)
                        {
                            B = 0;
                        }
                        if (B > 255)
                        {
                            B = 255;
                        }

                        lockBitmap2.SetPixel(x, y, Color.FromArgb((int)(R), (int)(G), (int)(B)));
                    }
                }
                lockBitmap2.UnlockBits();
                lockBitmap.UnlockBits();
                pictureBox.Image = backup;
            }
            updateHistographh();
        }
        Bitmap applyKernal(int[,] kernel, float div)
        {
            preSave();
            Bitmap bmp = bitmap;

            unsafe
            {
                int        kernelWidth  = kernel.GetLength(0);
                int        kernelHeight = kernel.Length / kernelWidth;
                LockBitmap lockBitmap   = new LockBitmap(bmp);
                lockBitmap.LockBits();
                for (int y = 0; y < lockBitmap.Height; y++)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        int avgR = 0;
                        int avgB = 0;
                        int avgG = 0;

                        for (int i = -1; i < kernelWidth - 1; i++)
                        {
                            for (int j = -1; j < kernelHeight - 1; j++)
                            {
                                if (x + i >= 0 && x + i < width && y + j >= 0 && y + j < height)
                                {
                                    Color c = lockBitmap.GetPixel(x + i, y + j);
                                    avgR += kernel[i + 1, j + 1] * c.R;
                                    avgG += kernel[i + 1, j + 1] * c.G;
                                    avgB += kernel[i + 1, j + 1] * c.B;
                                }
                            }
                        }

                        if (avgB < 0)
                        {
                            avgB = 0;
                        }
                        if (avgR < 0)
                        {
                            avgR = 0;
                        }
                        if (avgG < 0)
                        {
                            avgG = 0;
                        }
                        double R = (avgR * 1.0) / div;
                        double G = (avgG * 1.0) / div;
                        double B = (avgB * 1.0) / div;
                        if (R > 255)
                        {
                            R = 255;
                        }
                        if (G > 255)
                        {
                            G = 255;
                        }
                        if (B > 255)
                        {
                            B = 255;
                        }
                        lockBitmap.SetPixel(x, y, Color.FromArgb((int)(R), (int)(G), (int)(B)));
                    }
                }
                lockBitmap.UnlockBits();
            }
            return(bmp);
        }
        internal Bitmap operation(Bitmap a, Bitmap b, int operation)
        {
            Bitmap backup = new Bitmap(width, height);
            Bitmap bmp    = a;
            Bitmap bmp2   = b;

            unsafe
            {
                LockBitmap lockBitmap = new LockBitmap(bmp);
                lockBitmap.LockBits();

                LockBitmap lockBitmap2 = new LockBitmap(backup);
                lockBitmap2.LockBits();

                LockBitmap lockBitmap3 = new LockBitmap(bmp2);
                lockBitmap3.LockBits();


                for (int y = 0; y < lockBitmap.Height; y++)
                {
                    for (int x = 0; x < lockBitmap.Width; x++)
                    {
                        Color c1 = lockBitmap.GetPixel(x, y);
                        Color c2 = lockBitmap3.GetPixel(x, y);

                        int R = 0, G = 0, B = 0;

                        switch (operation)
                        {
                        case '+':
                            R = c1.R + c2.R;
                            G = c1.G + c2.G;
                            B = c1.B + c2.B;
                            break;

                        case '-':
                            R = c1.R - c2.R;
                            G = c1.G - c2.G;
                            B = c1.B - c2.B;
                            break;

                        case '^':
                            R = c1.R ^ c2.R;
                            G = c1.G ^ c2.G;
                            B = c1.B ^ c2.B;
                            break;

                        case '&':
                            R = c1.R & c2.R;
                            G = c1.G & c2.G;
                            B = c1.B & c2.B;
                            break;

                        case '|':
                            R = c1.R | c2.R;
                            G = c1.G | c2.G;
                            B = c1.B | c2.B;
                            break;
                        }

                        if (R < 0)
                        {
                            R = 0;
                        }
                        if (G < 0)
                        {
                            G = 0;
                        }
                        if (B < 0)
                        {
                            B = 0;
                        }
                        if (R > 255)
                        {
                            R = 255;
                        }
                        if (G > 255)
                        {
                            G = 255;
                        }
                        if (B > 255)
                        {
                            B = 255;
                        }

                        lockBitmap2.SetPixel(x, y, Color.FromArgb(R, G, B));
                    }
                }
                lockBitmap2.UnlockBits();
                lockBitmap.UnlockBits();
                lockBitmap3.UnlockBits();
            }
            return(backup);
        }