public void drawing(bufferedLockBitmap pixel, Chart chart1)
        {
            int R, G, B;
            for (int i = 0; i < pixel.Height; i++)
            {
                for (int j = 0; j <pixel.Width; j++)
                {
                    R = pixel.Getpixel(j,i).R;
                    Rarray[R]++;
                    G = pixel.Getpixel(j, i).G;
                    Garray[G]++;
                    B = pixel.Getpixel(j, i).B;
                    Barray[B]++;
                }
            }
            chart1.Series["Red"].Points.Clear();
            chart1.Series["Green"].Points.Clear();
            chart1.Series["Blue"].Points.Clear();

            for (int w = 0; w < 256; w++)
            {
                chart1.Series["Red"].Points.AddXY(w, this.Rarray[w]);
                chart1.Series["Green"].Points.AddXY(w, this.Garray[w]);
                chart1.Series["Blue"].Points.AddXY(w, this.Barray[w]);

            }
            chart1.Series["Green"].ChartType = SeriesChartType.SplineArea;
            chart1.Series["Green"].Color = Color.Green;

            chart1.Series["Red"].ChartType = SeriesChartType.SplineArea;
            chart1.Series["Red"].Color = Color.Red;

            chart1.Series["Blue"].ChartType = SeriesChartType.SplineArea;
            chart1.Series["Blue"].Color = Color.Blue;
        }
        public Bitmap Addpictures(bufferedLockBitmap pic1, bufferedLockBitmap pic2, double fraction)
        {
            if (pic1.Width == pic2.Width && pic1.Height == pic2.Height)
            {
                temp = new Bitmap(pic1.Width, pic1.Height);
                bufferedLockBitmap r = new bufferedLockBitmap(temp);
                r.LockBits();
                int R, G, B = 0;
                double f;
                for (int i = 0; i < temp.Height; i++)
                {
                    for (int j = 0; j < temp.Width; j++)
                    {
                        f = ((((double)pic1.Getpixel(j, i).R)) * fraction) + (((double)pic2.Getpixel(j, i).R) * (1 - fraction));
                        R = Convert.ToInt32(f);
                        f = ((((double)pic1.Getpixel(j, i).G)) * fraction) + (((double)pic2.Getpixel(j, i).G) * (1 - fraction));
                        G = Convert.ToInt32(f);
                        f = ((((double)pic1.Getpixel(j, i).B)) * fraction) + (((double)pic2.Getpixel(j, i).B) * (1 - fraction));
                        B = Convert.ToInt32(f);
                        r.SetPixel(j, i, Color.FromArgb(R, G, B));
                    }
                }
                r.UnlockBits();
                return r.source;

            }
            else
                MessageBox.Show("picture 1 size is equal to picture 2 size");
            return temp;
        }
        /**
         * find 4 adjacent pixels
            X1 = floor (OldX)	; X2 = X1 + 1
            Y1 = floor (OldY)	; Y2 = Y1 + 1
            P1 = OrigBuf[X1,Y1] 	; P2 = OrigBuf[X2,Y1]
            P3 = OrigBuf[X1,Y2] 	; P4 = OrigBuf[X2,Y2]
            Calculate X, Y fractions
            Xfraction = OldX – X1
            Yfraction = OldY – Y1
            Interpolate in X-Direction
            Z1 = P1 × (1 – Xfraction) + P2 × Xfraction
            Z2 = P3 × (1 – Xfraction) + P4 × Xfraction
            Interpolate in Y-Direction
            NewPixel = Z1 × (1 – Yfraction) + Z2 × Yfraction
         */
        public Color calculate(bufferedLockBitmap _original_buffer, float _old_x, float _old_y,int _new_width, int _new_height)
        {
            int new_red, new_green, new_blue;
            Color ret;
            int x_1 = (int)Math.Floor(_old_x);
            int x_2 = x_1 + 1;

            int y_1 = (int)Math.Floor(_old_y);
            int y_2 = y_1 + 1;

            Color p_1 = Color.FromArgb(0);
            Color p_2 = Color.FromArgb(0);
            Color p_3 = Color.FromArgb(0);
            Color p_4 = Color.FromArgb(0);
            if (x_2 < _new_width && y_2 < _new_height)
            {
                p_1 = _original_buffer.Getpixel(x_1, y_1);
                p_2 = _original_buffer.Getpixel(x_2, y_2);
                p_3 = _original_buffer.Getpixel(x_1, y_2);
                p_4 = _original_buffer.Getpixel(x_2, y_2);

            }

            float x_fraction = _old_x - x_1;
            float y_fraction = _old_y - y_1;

            // Interpolate in X-Direction
            // Red
            int r_1 = (int)Math.Floor(p_1.R * (1 - x_fraction) + p_2.R * x_fraction);
            int r_2 = (int)Math.Floor(p_3.R * (1 - x_fraction) + p_4.R * x_fraction);

            // Green
            int g_1 = (int)Math.Floor(p_1.G * (1 - x_fraction) + p_2.G * x_fraction);
            int g_2 = (int)Math.Floor(p_3.G * (1 - x_fraction) + p_4.G * x_fraction);

            // Blue
            int b_1 = (int)Math.Floor(p_1.B * (1 - x_fraction) + p_2.B * x_fraction);
            int b_2 = (int)Math.Floor(p_3.B * (1 - x_fraction) + p_4.B * x_fraction);

            // Interpolate in Y-Direction
            new_red = (int)Math.Floor(r_1 * (1 - y_fraction) + r_2 * y_fraction);
            new_green = (int)Math.Floor(g_1 * (1 - y_fraction) + g_2 * y_fraction);
            new_blue = (int)Math.Floor(b_1 * (1 - y_fraction) + b_2 * y_fraction);
            ret = Color.FromArgb(new_red, new_green, new_blue);
            return ret;
        }
        public Pixel_logic__Operations(bufferedLockBitmap input)
        {
            int te = 0;

            for (int i = 0; i < input.Height; i++)
            {
                for (int j = 0; j < input.Width; j++)
                {
                    te = input.Getpixel(j, i).R;
                    if (te > maxR) maxR = te;

                    te = input.Getpixel(j, i).G;
                    if (te > maxG) maxG = te;

                    te = input.Getpixel(j, i).B;
                    if (te > maxb) maxb = te;

                    te = input.Getpixel(j, i).R;
                    if (te < minR) minR = te;

                    te = input.Getpixel(j, i).G;
                    if (te < minG) minG = te;

                    te = input.Getpixel(j, i).B;
                    if (te < minb) minb = te;

                }
            }
            Word = new byte[8];
            Word[0] = 1;
            Word[1] = 2;
            Word[2] = 4;
            Word[3] = 8;
            Word[4] = 16;
            Word[5] = 32;
            Word[6] = 64;
            Word[7] = 128;
        }
        public Bitmap AND(bufferedLockBitmap pic1, bufferedLockBitmap pic2)
        {
            temp = new Bitmap(pic1.Width, pic1.Height);
            bufferedLockBitmap bf = new bufferedLockBitmap(temp);
            bf.LockBits();
            int R, G, B = 0;
            if (pic1.Width == pic2.Width)
            {
                R = 0; G = 0; B = 0;
                for (int i = 0; i < pic1.Height; i++)
                {
                    for (int j = 0; j < pic1.Width; j++)
                    {
                        R = pic1.Getpixel(j, i).R & pic2.Getpixel(j, i).R;
                        G = pic1.Getpixel(j, i).G & pic2.Getpixel(j, i).G;
                        B = pic1.Getpixel(j, i).B & pic2.Getpixel(j, i).B;

                        bf.SetPixel(j, i, Color.FromArgb(Convert.ToInt32(R), Convert.ToInt32(G), Convert.ToInt32(B)));

                    }
                }
            }
            bf.UnlockBits();
            return bf.source;
        }
        public Bitmap Subtraction(bufferedLockBitmap pic1, bufferedLockBitmap pic2)
        {
            temp = new Bitmap(pic1.Width, pic1.Height);
            bufferedLockBitmap bf = new bufferedLockBitmap(temp);
            bf.LockBits();
            int gmaxR = 0, gmaxG = 0, gmaxb = 0, gminR = 1000, gminG = 10000, gminb = 1000;
            int te;

            int R, G, B = 0;
            if (pic1.Width == pic2.Width)
            {
                R = 0; G = 0; B = 0;
                for (int i = 0; i < pic1.Height; i++)
                {
                    for (int j = 0; j < pic1.Width; j++)
                    {
                        R = Math.Abs(pic1.Getpixel(j, i).R - pic2.Getpixel(j, i).R);
                        G = Math.Abs(pic1.Getpixel(j, i).G - pic2.Getpixel(j, i).G);
                        B = Math.Abs(pic1.Getpixel(j, i).B - pic2.Getpixel(j, i).B);
                        te = R;
                        if (te > gmaxR) gmaxR = te;
                        if (te < gminR) gminR = te;

                        te = G;

                        if (te > gmaxb) gmaxb = te;
                        if (te < gminb) gminb = te;
                        te = B;
                        if (te > gmaxG) gmaxG = te;
                        if (te < gminG) gminG = te;

                    }
                }
            }

            for (int i = 0; i < pic1.Height; i++)
            {
                for (int j = 0; j < pic1.Width; j++)
                {

                    R = Math.Abs(pic1.Getpixel(j, i).R - pic2.Getpixel(j, i).R);
                    G = Math.Abs(pic1.Getpixel(j, i).G - pic2.Getpixel(j, i).G);
                    B = Math.Abs(pic1.Getpixel(j, i).B - pic2.Getpixel(j, i).B);
                    R = Convert.ToInt32(((double)(R - minR) / (maxR - minR)) * ((255 - 0)));
                    G = Convert.ToInt32(((double)(G - minG) / (maxG - minG)) * ((255 - 0)));
                    B = Convert.ToInt32(((double)(B - minb) / (maxb - minb)) * ((255 - 0)));
                    if (R > 255) R = 255;
                    else if (R < 0) R = 0;
                    if (G > 255) G = 255;
                    else if (G < 0) G = 0;
                    if (B > 255) B = 255;
                    else if (B < 0) B = 0;
                    bf.SetPixel(j, i, Color.FromArgb(Convert.ToInt32(R), Convert.ToInt32(G), Convert.ToInt32(B)));
                }
            }

            bf.UnlockBits();
            return bf.source;
        }
 public void Quantization(bufferedLockBitmap input, int num)
 {
     int sum = 0;
     double f = 2;
     int math = Convert.ToInt32(Math.Log(Convert.ToDouble(num), f));
     for (int w = 7; w > 7 - math; w--)
     {
         sum = sum + Convert.ToInt32(Math.Pow(f, Convert.ToInt32(w)));
     }
     temp = new Bitmap(input.Width, input.Height);
     bf = new bufferedLockBitmap(temp);
     input.LockBits();
     int R, G, B;
     for (int i = 0; i < input.Height; i++)
     {
         for (int j = 0; j < input.Width; j++)
         {
             R = input.Getpixel(j, i).R & sum;
             G = input.Getpixel(j, i).G & sum;
             B = input.Getpixel(j, i).B & sum;
             input.SetPixel(j, i, Color.FromArgb(R, G, B));
         }
     }
     input.UnlockBits();
 }
        public void notoperation(bufferedLockBitmap input, int dif)
        {
            if (dif == 0)
                for (int i = 0; i < input.Height; i++)
                {
                    for (int j = 0; j < input.Width; j++)
                    {
                        input.SetPixel(j, i, Color.FromArgb(dif - input.Getpixel(j, i).R, dif - input.Getpixel(j, i).G, dif - input.Getpixel(j, i).B));
                    }
                }
            else
            {
                temp = new Bitmap(input.Width, input.Height);
                bufferedLockBitmap bf = new bufferedLockBitmap(temp);
                bf.LockBits();
                int gmaxR = 0, gmaxG = 0, gmaxb = 0, gminR = 1000, gminG = 10000, gminb = 1000;
                int te;

                int R, G, B = 0;
                for (int i = 0; i < input.Height; i++)
                {
                    for (int j = 0; j < input.Width; j++)
                    {
                        R = dif - input.Getpixel(j, i).R;
                        G = dif - input.Getpixel(j, i).G;
                        B = dif - input.Getpixel(j, i).B;
                        te = R;
                        if (te > gmaxR) gmaxR = te;
                        if (te < gminR) gminR = te;

                        te = G;

                        if (te > gmaxb) gmaxb = te;
                        if (te < gminb) gminb = te;
                        te = B;
                        if (te > gmaxG) gmaxG = te;
                        if (te < gminG) gminG = te;

                    }
                }

                for (int i = 0; i < input.Height; i++)
                {
                    for (int j = 0; j < input.Width; j++)
                    {
                        R =Math.Abs( dif - input.Getpixel(j, i).R);
                        G = Math.Abs(dif - input.Getpixel(j, i).G);
                        B = Math.Abs(dif - input.Getpixel(j, i).B);

                        //R = Convert.ToInt32(((double)(R - minR) / (maxR - minR)) * ((255 - 0)));
                        //G = Convert.ToInt32(((double)(G - minG) / (maxG - minG)) * ((255 - 0)));
                        //B = Convert.ToInt32(((double)(B - minb) / (maxb - minb)) * ((255 - 0)));

                        if (R > 255) R = 255;
                        else if (R < 0) R = 0;
                        if (G > 255) G = 255;
                        else if (G < 0) G = 0;
                        if (B > 255) B = 255;
                        else if (B < 0) B = 0;
                        input.SetPixel(j, i, Color.FromArgb(Convert.ToInt32(R), Convert.ToInt32(G), Convert.ToInt32(B)));
                    }

                }
                bf.UnlockBits();
            }
        }
 public bufferedLockBitmap Grayscale(bufferedLockBitmap input)
 {
     input.LockBits();
     int sum = 0;
     for (int i = 0; i < input.Height; i++)
     {
         for (int j = 0; j < input.Width; j++)
         {
             sum = 0;
             sum = input.Getpixel(j, i).R + input.Getpixel(j, i).G + input.Getpixel(j, i).B;
             sum /= 3;
             input.SetPixel(j, i, Color.FromArgb(sum, sum, sum));
         }
     }
     input.UnlockBits();
     return input;
 }
        public bufferedLockBitmap Gamma(bufferedLockBitmap input, double dif)
        {
            if (dif == 0)
                return input;
            if (dif < 0)
                dif = 1 / Math.Abs(dif);
            double te = 0;
            double gmaxR = -10000, gmaxG =-10000, gmaxb = -10000, gminR = 10000, gminG = 10000, gminb = 10000;
            for (int i = 0; i < input.Height; i++)
            {
                for (int j = 0; j < input.Width; j++)
                {
                    te = Math.Pow(input.Getpixel(j, i).R, dif);
                    if (te > gmaxR) gmaxR = te;
                    else if (te < gminR) gminR = te;

                    te = Math.Pow(input.Getpixel(j, i).B, dif);
                    if (te > gmaxb) gmaxb = te;
                    else if (te < gminb) gminb = te;

                    te = Math.Pow(input.Getpixel(j, i).G, dif);
                    if (te > gmaxG) gmaxG = te;
                     if (te < gminG) gminG = te;

                }
            }

            temp = new Bitmap(input.Width, input.Height);
            bf = new bufferedLockBitmap(temp);
            bf.LockBits();
            double R, G, B = 0,x;
            for (int i = 0; i < input.Height; i++)
            {
                for (int j = 0; j < input.Width; j++)
                {
                    R = (Math.Pow(input.Getpixel(j, i).R, dif));
                    x = (Math.Pow(input.Getpixel(j, i).G, dif));
                    B = (Math.Pow(input.Getpixel(j, i).B, dif));

                    R = Convert.ToInt32(((double)(R - gminR) / (gmaxR - gminR)) * (255 - 0));
                    G = Convert.ToInt32(((double)(x - gminG) / (gmaxG - gminG)) * (255 - 0));
                    B = Convert.ToInt32(((double)(B - gminb) / (gmaxb - gminb)) * (255 - 0));

                    //if (R > 255) R = 255;
                    //else if (R < 0) R = 0;
                    //if (G > 255) G = 255;
                    //else if (G < 0) G = 0;
                    //if (B > 255) B = 255;
                    //else if (B < 0) B = 0;
                    bf.SetPixel(j, i, Color.FromArgb(Convert.ToInt32(R), Convert.ToInt32(G), Convert.ToInt32(B)));
                }
            }
            bf.UnlockBits();
            return bf;
        }
 public bufferedLockBitmap Flippingvertical(bufferedLockBitmap pic1)
 {
     Bitmap temp = new Bitmap(pic1.Width, pic1.Height);
     bufferedLockBitmap t = new bufferedLockBitmap(temp);
     t.LockBits();
     //flip images
     Color p;
     int ImageWidth = pic1.Width;
     int ImageHigh = pic1.Height;
     Bitmap mimg = new Bitmap(ImageWidth, ImageHigh);
     for (int i = 0; i < ImageHigh; i++)
     {
         for (int lx = 0; lx < ImageWidth; ++lx)
         {
             p = pic1.Getpixel(lx, ImageHigh - i - 1);
             t.SetPixel(lx, i, p);
         }
     }
     t.UnlockBits();
     return t;
 }
 public bufferedLockBitmap FlippingHorizontal(bufferedLockBitmap imageLockBitmap)
 {
     Bitmap temp = new Bitmap(imageLockBitmap.Width, imageLockBitmap.Height);
     bufferedLockBitmap t = new bufferedLockBitmap(temp);
     t.LockBits();
     //flip images
     int ImageWidth = imageLockBitmap.Width;
     int ImageHigh = imageLockBitmap.Height;
     Bitmap mimg = new Bitmap(ImageWidth, ImageHigh);
     for (int i = 0; i < ImageHigh; i++)
     {
         for (int lx = 0; lx < ImageWidth; ++lx)
         {
             Color p = imageLockBitmap.Getpixel(ImageWidth - lx - 1, i);
             t.SetPixel(lx, i, p);
         }
     }
     t.UnlockBits();
     return t;
 }
        public bufferedLockBitmap contrast(bufferedLockBitmap input, int x, int y)
        {
            temp = new Bitmap(input.Width, input.Height);
            bf = new bufferedLockBitmap(temp);
            bf.LockBits();
            int NewMinR = 0, NewMinG = 0, NewMinB = 0, NewMaxR = 0, NewMaxG = 0, NewMaxB = 0;
            temp = new Bitmap(input.Width, input.Height);
            int R, G, B;
            NewMinR = minR + x;
            NewMinG = minG + x;
            NewMinB = minb + x;

            NewMaxR = maxR + y;
            NewMaxB = maxb + y;
            NewMaxG = maxG + y;

            for (int i = 0; i < input.Height; i++)
            {
                for (int j = 0; j < input.Width; j++)
                {
                    R = input.Getpixel(j, i).R;
                    R = Convert.ToInt32(((double)(R - minR) / (maxR - minR)) * ((NewMaxR - NewMinR)) + NewMinR);
                    G = input.Getpixel(j, i).G;
                    G = Convert.ToInt32(((double)(G - minG) / (maxG - minG)) * ((NewMaxG - NewMinG)) + NewMinG);
                    B = input.Getpixel(j, i).B;
                    B = Convert.ToInt32(((double)(B - minb) / (maxb - minb)) * ((NewMaxB - NewMinB)) + NewMinB);

                    if (R > 255) R = 255;
                    else if (R < 0) R = 0;
                    if (G > 255) G = 255;
                    else if (G < 0) G = 0;
                    if (B > 255) B = 255;
                    else if (B < 0) B = 0;
                    bf.SetPixel(j, i, Color.FromArgb(R, G, B));
                }
            }
            bf.UnlockBits();
            return bf;
        }
 public bufferedLockBitmap Brightness(bufferedLockBitmap input, int dif)
 {
     temp = new Bitmap(input.Width, input.Height);
     bf = new bufferedLockBitmap(temp);
     bf.LockBits();
     int R, G, B = 0;
     for (int i = 0; i < input.Height; i++)
     {
         for (int j = 0; j < input.Width; j++)
         {
             R = input.Getpixel(j, i).R + dif;
             G = input.Getpixel(j, i).G + dif;
             B = input.Getpixel(j, i).B + dif;
             if (R > 255) R = 255;
             else if (R < 0) R = 0;
             if (G > 255) G = 255;
             else if (G < 0) G = 0;
             if (B > 255) B = 255;
             else if (B < 0) B = 0;
             bf.SetPixel(j, i, Color.FromArgb(R, G, B));
         }
     }
     bf.UnlockBits();
     return bf;
 }
 public bufferedLockBitmap Bit_plane(bufferedLockBitmap input, int number, bool R, bool G, bool B)
 {
     temp = new Bitmap(input.source);
     bf = new bufferedLockBitmap(temp);
     bf.LockBits();
     int p;
     int red = 0, blue = 0, green = 0;
     for (int i = 0; i < temp.Height; i++)
     {
         for (int j = 0; j < temp.Width; j++)
         {
             if (R == true)
             {
                 p = bf.Getpixel(j, i).R & Word[number ];
                 if (p != 0)
                     red = 255;
                 else if (p == 0)
                     red = 0;
             }
             if (G == true)
             {
                 p = bf.Getpixel(j, i).G & Word[number ];
                 if (p != 0)
                     green = 255;
                 else if (p == 0)
                     green = 0;
             }
             if (B == true)
             {
                 p = bf.Getpixel(j, i).B & Word[number ];
                 if (p != 0)
                     blue = 255;
                 else if (p == 0)
                     blue = 0;
             }
             bf.SetPixel(j, i, Color.FromArgb(red, green, blue));
         }
     }
     bf.UnlockBits();
     return bf;
 }
        public bufferedLockBitmap LinearFilter(bufferedLockBitmap input, double[,] mask, int w, int h, int x, int y, String word)
        {
            Bitmap temp = new Bitmap(input.Width, input.Height);
            bufferedLockBitmap bt = new bufferedLockBitmap(temp);
            bt.LockBits();
            double sumR = 0, sumG = 0, sumB = 0;
            for (int i = 0; i < input.Height; i++)
            {
                for (int j = 0; j < input.Width; j++)
                {
                    for (int maskh = 0; maskh < h; maskh++)
                    {
                        for (int maskw = 0; maskw < w; maskw++)
                        {
                            if (j + maskw - x < 0)
                                continue;
                            else if (j + maskw - x >= input.Width)
                                continue;
                            else if ((i + maskh - y >= input.Height))
                                continue;
                            else if (i + maskh - y < 0)
                                continue;
                            else
                            {
                                sumR += (double)input.Getpixel(j + maskw - x, i + maskh - y).R * mask[maskh, maskw];
                                sumG += (double)input.Getpixel(j + maskw - x, i + maskh - y).G * mask[maskh, maskw];
                                sumB += (double)input.Getpixel(j + maskw - x, i + maskh - y).B * mask[maskh, maskw];
                            }
                        }
                    }

                    if (word == "cutoff")
                    {
                        if (sumR > 255) sumR = 255;
                        else if (sumR < 0) sumR = 0;
                        if (sumG > 255) sumG = 255;
                        else if (sumG < 0) sumG = 0;
                        if (sumB > 255) sumB = 255;
                        else if (sumB < 0) sumB = 0;
                    }
                    else if (word == "abs")
                    {
                        sumB = Math.Abs(sumB);
                        sumR = Math.Abs(sumR);
                        sumG = Math.Abs(sumG);
                        if (sumR > 255) sumR = 255;
                        else if (sumR < 0) sumR = 0;
                        if (sumG > 255) sumG = 255;
                        else if (sumG < 0) sumG = 0;
                        if (sumB > 255) sumB = 255;
                        else if (sumB < 0) sumB = 0;
                    }
                    bt.SetPixel(j, i, Color.FromArgb(Convert.ToInt32(sumR), Convert.ToInt32(sumG), Convert.ToInt32(sumB)));
                    sumR = 0;
                    sumG = 0;
                    sumB = 0;
                }
            }
            bt.UnlockBits();
            return bt;
        }