} //****************************** end Shading_Cor ***********************

        private void button3_Click(object sender, EventArgs e) // Impulse Noise
        {
            if (!OPEN)
            {
                MessageBox.Show("Please open an image");
                return;
            }
            if (!SHAD)
            {
                MessageBox.Show("Please ckick the button 'Shading'");
                return;
            }
            if (!BIN)
            {
                MessageBox.Show("Please click in the histogram");
                return;
            }
            ResultBmp = new Bitmap(origBmp.Width, origBmp.Height, PixelFormat.Format24bppRgb);

            ImpulseIm.Copy(BinIm);
            int nbyte = ImpulseIm.N_Bits / 8;

            Drawn = true;
            progressBar1.Visible = true;
            ImpulseIm.DeleteBit0(nbyte, this);

            int maxLi, minLi;

            int[] histo = new int[256];
            for (int i = 0; i < 256; i++)
            {
                histo[i] = 0;
            }
            if (nbyte == 3)
            {
                int lum;
                int i1 = origBmp.Width * origBmp.Height / 100 + 1;
                for (int i = 0; i < origBmp.Width * origBmp.Height; i++)
                {
                    lum = MaxC(ImpulseIm.Grid[3 * i + 2], ImpulseIm.Grid[3 * i + 1], ImpulseIm.Grid[3 * i + 0]);
                    histo[lum]++;
                }
            }
            else
            {
                for (int i = 0; i < origBmp.Width * origBmp.Height; i++)
                {
                    histo[ImpulseIm.Grid[i]]++;
                }
            }

            for (maxLi = 255; maxLi > 0; maxLi--)
            {
                if (histo[maxLi] != 0)
                {
                    break;
                }
            }
            for (minLi = 0; minLi < 256; minLi++)
            {
                if (histo[minLi] != 0)
                {
                    break;
                }
            }
            CPnoise PN = new CPnoise(histo, 1000, 4000);

            PN.Sort(ImpulseIm, histo, Number, pictureBox1.Width, pictureBox1.Height, this);

            int maxDark  = (int)numericUpDown4.Value;
            int maxLight = (int)numericUpDown5.Value;

            PN.DarkNoise(ref ImpulseIm, minLi, maxLi, maxDark, this);
            ImpulseIm.DeleteBit0(nbyte, this);

            PN.LightNoise(ref ImpulseIm, minLi, maxLi, maxLight, this);

            for (int i = 0; i < nbyte * origBmp.Width * origBmp.Height; i++)
            {
                if (ImpulseIm.Grid[i] == 252 || ImpulseIm.Grid[i] == 254)
                {
                    ImpulseIm.Grid[i] = 255;
                }
            }

            ImageToBitmapNew(ImpulseIm, ResultBmp);

            pictureBox2.Image = ResultBmp;

            Graphics g     = pictureBox1.CreateGraphics();
            Pen      myPen = new System.Drawing.Pen(System.Drawing.Color.LightGray);

            for (int n = 0; n < Number; n += 2)
            {
                g.DrawLine(myPen, v[n + 1].X, v[n + 0].Y, v[n + 1].X, v[n + 1].Y);
                g.DrawLine(myPen, v[n + 0].X, v[n + 0].Y, v[n + 1].X, v[n + 0].Y);
                g.DrawLine(myPen, v[n + 0].X, v[n + 0].Y, v[n + 0].X, v[n + 1].Y);
                g.DrawLine(myPen, v[n + 0].X, v[n + 1].Y, v[n + 1].X, v[n + 1].Y);
            }
            progressBar1.Visible = false;

            groupBox1.Visible = true;
            label6.Visible    = true;

            label6.Visible  = true;
            label8.Visible  = true;
            label9.Visible  = true;
            label10.Text    = "Noise removed";
            label10.Visible = true;
            button4.Visible = true;

            CHOICE = false;
            pictureBox3.Visible = true;
        } //********************************** end Impulse noise *******************************
        private void button1_Click_1(object sender, EventArgs e) // Open image
        {
            pictureBox1.Visible = false;
            pictureBox2.Visible = false;
            pictureBox3.Visible = true;
            groupBox1.Visible   = false;

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                label12.Visible = false;
                try
                {
                    origBmp       = new Bitmap(openFileDialog1.FileName);
                    OpenImageFile = openFileDialog1.FileName;
                    Number        = 0;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " +
                                    ex.Message);
                }
            }
            else
            {
                return;
            }

            label12.Text    = "Opened image: " + OpenImageFile;
            label12.Visible = true;
            label11.Visible = true;
            label6.Visible  = true;

            button2.Visible        = false;
            button3.Visible        = false;
            button4.Visible        = false;
            numericUpDown1.Visible = false;
            numericUpDown2.Visible = false;
            numericUpDown4.Visible = false;
            numericUpDown5.Visible = false;
            label1.Visible         = false;
            label2.Visible         = false;
            label4.Visible         = false;
            label5.Visible         = false;
            label7.Visible         = false;
            label8.Visible         = false;
            label9.Visible         = false;
            label10.Visible        = false;
            pictureBox2.Visible    = false;
            pictureBox3.Visible    = false;

            groupBox1.Visible = true;

            progressBar1.Maximum = 100;
            progressBar1.Value   = 0;
            progressBar1.Step    = 1;
            progressBar1.Visible = true;

            if (origBmp.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                nbyteBmp = 1;
            }
            else
            if (origBmp.PixelFormat == PixelFormat.Format24bppRgb)
            {
                nbyteBmp = 3;
            }
            else
            {
                MessageBox.Show("Pixel format=" + origBmp.PixelFormat + " not used in this project.");
                return;
            }

            nbyteIm = BitmapToImage(origBmp, ref OrigIm);

            pictureBox1.Visible = true;
            pictureBox1.Image   = origBmp;

            width  = origBmp.Width;
            height = origBmp.Height;

            int N_Bits = nbyteIm * 8;

            SigmaIm = new CImage(width, height, N_Bits);
            SigmaIm.SigmaSimpleUni(OrigIm, 2, 50);
            GrayIm = new CImage(width, height, 8);
            if (nbyteIm == 3)
            {
                GrayIm.ColorToGray(SigmaIm);
            }
            else
            {
                GrayIm.Copy(SigmaIm);
            }
            MeanIm    = new CImage(width, height, 8);
            ShadIm    = new CImage(width, height, N_Bits);
            ImpulseIm = new CImage(width, height, N_Bits);
            BinIm     = new CImage(width, height, 8);

            ScaleX     = (double)pictureBox1.Width / (double)width;
            ScaleY     = (double)pictureBox1.Height / (double)height;
            Scale1     = Math.Min(ScaleX, ScaleY);
            marginX    = (pictureBox1.Width - (int)(Scale1 * width)) / 2;
            marginY    = (pictureBox1.Height - (int)(Scale1 * height)) / 2;
            ShadingBmp = new Bitmap(origBmp.Width, origBmp.Height, PixelFormat.Format24bppRgb);

            pictureBox2.Visible  = false;
            pictureBox3.Visible  = false;
            radioButton1.Checked = false;
            radioButton2.Checked = false;
            radioButton3.Checked = false;
            radioButton4.Checked = false;
            progressBar1.Visible = false;

            KIND = -1;
            OPEN = true;
        } //********************************** end Open image *************************************