Exemplo n.º 1
0
        //
        //         Из PictureBox в  ZArrayDescriptor.array
        //
        public static ZArrayDescriptor getArrayFromImage(PictureBox pictureBox)
        {
            if (pictureBox == null)
            {
                MessageBox.Show("pictureBox == null"); return(null);
            }


            int        w1   = pictureBox.Image.Width;
            int        h1   = pictureBox.Image.Height;
            Bitmap     bmp1 = new Bitmap(pictureBox.Image, w1, h1);
            BitmapData data = ImageProcessor.getBitmapData(bmp1);


            ZArrayDescriptor result = new ZArrayDescriptor();

            result.array  = new double[w1, h1];
            result.width  = w1;
            result.height = h1;

            for (int i = 0; i < w1; i++)
            {
                for (int j = 0; j < h1; j++)
                {
                    Color c = ImageProcessor.getPixel(i, j, data);
                    result.array[i, j] = (c.R + c.G + c.B) / 3;
                    //result.array[i, j] = ImageProcessor.getPixel_blue(j, i, data);
                }
            }

            bmp1.UnlockBits(data);

            return(result);
        }
        public void Stolb(int i, int N2x, BitmapData data2, int Ny, int NyP, ColorModeEnum colorMode)
        {
            int N2 = 0;

            if (Ny > NyP)
            {
                N2 = (Ny - NyP) / 2;
                for (int j = 0; j < N2; j++)
                {
                    array[i, j] = 0.0;
                }
                for (int j = N2; j < Ny - N2; j++)
                {
                    Color c1 = ImageProcessor.getPixel(i - N2x, j - N2, data2);
                    array[i, j] = GetColorValue(c1, colorMode);
                }
                for (int j = Ny - N2; j < Ny; j++)
                {
                    array[i, j] = 0.0;
                }
            }
            else
            {
                for (int j = 0; j < Ny; j++)
                {
                    Color c1 = ImageProcessor.getPixel(i - N2x, j, data2);
                    array[i, j] = c1.R;
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        unsafe private static void drawPixelSquareStep(int x, int y, Color fillColor, List <Point> listOfPoints, BitmapData imageData, Image someImage, Color borderColor)
        {
            Color pixelcolor;

            if (x + 1 < someImage.Size.Width)
            {
                pixelcolor = ImageProcessor.getPixel(x + 1, y, imageData);
                if ((pixelcolor.ToArgb() != borderColor.ToArgb()) && (pixelcolor.ToArgb() != fillColor.ToArgb()))
                {
                    listOfPoints.Add(new Point(x + 1, y));
                    ImageProcessor.setPixel(imageData, x + 1, y, fillColor);
                }
            }

            if (y + 1 < someImage.Size.Height)
            {
                pixelcolor = ImageProcessor.getPixel(x, y + 1, imageData);

                if ((pixelcolor.ToArgb() != borderColor.ToArgb()) && (pixelcolor.ToArgb() != fillColor.ToArgb()))
                {
                    listOfPoints.Add(new Point(x, y + 1));
                    ImageProcessor.setPixel(imageData, x, y + 1, fillColor);
                }
            }

            if (x - 1 >= 0)
            {
                pixelcolor = ImageProcessor.getPixel(x - 1, y, imageData);

                if ((pixelcolor.ToArgb() != borderColor.ToArgb()) && (pixelcolor.ToArgb() != fillColor.ToArgb()))
                {
                    listOfPoints.Add(new Point(x - 1, y));
                    ImageProcessor.setPixel(imageData, x - 1, y, fillColor);
                }
            }

            if (y - 1 >= 0)
            {
                pixelcolor = ImageProcessor.getPixel(x, y - 1, imageData);

                if ((pixelcolor.ToArgb() != borderColor.ToArgb()) && (pixelcolor.ToArgb() != fillColor.ToArgb()))
                {
                    listOfPoints.Add(new Point(x, y - 1));
                    ImageProcessor.setPixel(imageData, x, y - 1, fillColor);
                }
            }
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public static Rectangle determineImageRect(Bitmap someImage)
        {
            BitmapData bitmapData = ImageProcessor.getBitmapData(someImage);

            bool  flag = false;
            Color pixelColor;
            Color blackColor = Color.Black;

            int top    = 0;
            int left   = 0;
            int right  = 0;
            int bottom = 0;

            for (int i = 0; i < someImage.Height; i++)
            {
                for (int j = 0; j < someImage.Width; j++)
                {
                    pixelColor = ImageProcessor.getPixel(j, i, bitmapData);
                    if (pixelColor.ToArgb() != blackColor.ToArgb())
                    {
                        flag = true;
                        break;
                    }
                }

                if (flag == true)
                {
                    top = i;
                    break;
                }
            }

            flag = false;
            for (int i = 0; i < someImage.Width; i++)
            {
                for (int j = 0; j < someImage.Height; j++)
                {
                    pixelColor = ImageProcessor.getPixel(i, j, bitmapData);
                    if (pixelColor.ToArgb() != blackColor.ToArgb())
                    {
                        flag = true;
                        break;
                    }
                }

                if (flag == true)
                {
                    left = i;
                    break;
                }
            }

            flag = false;
            for (int i = someImage.Width - 1; i >= 0; i--)
            {
                for (int j = 0; j < someImage.Height; j++)
                {
                    pixelColor = ImageProcessor.getPixel(i, j, bitmapData);
                    if (pixelColor.ToArgb() != blackColor.ToArgb())
                    {
                        flag = true;
                        break;
                    }
                }

                if (flag == true)
                {
                    right = i;
                    break;
                }
            }

            flag = false;
            for (int i = someImage.Height - 1; i >= 0; i--)
            {
                for (int j = 0; j < someImage.Width; j++)
                {
                    pixelColor = ImageProcessor.getPixel(j, i, bitmapData);
                    if (pixelColor.ToArgb() != blackColor.ToArgb())
                    {
                        flag = true;
                        break;
                    }
                }

                if (flag == true)
                {
                    bottom = i;
                    break;
                }
            }


            someImage.UnlockBits(bitmapData);

            Rectangle result = new Rectangle(left, top, right - left + 1, bottom - top + 1);

            return(result);
        }
Exemplo n.º 5
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public static void drawLines(double N_sin, double f1, int max_x, int max_y, int XY, PictureBox pc1)    // sin b/w
        {
            BitmapData bitmapData = ImageProcessor.getBitmapData((Bitmap)pc1.Image);
            byte       r;
            double     nx = max_x + 1;
            double     pi = Math.PI;
            double     af = pi * 2 * N_sin / nx;

            if (XY == 0)  // Полосы ориентированы перпендикулярно оси X
            {
                for (int i = 0; i < max_x; i++)
                {
                    for (int j = 0; j < max_y; j++)
                    {
                        r = (byte)((Math.Sin(af * i + 1 + pi * f1 / 180) + 1) * 127);
                        if (r > 255)
                        {
                            r = 0;
                        }

                        if (r > 125)
                        {
                            r = 255;
                        }
                        else
                        {
                            r = 0;
                        }

                        ImageProcessor.setPixel(bitmapData, i, j, Color.FromArgb(r, r, r));
                    }
                }
            }

            if (XY == 1) // Полосы ориентированы перпендикулярно оси y
            {
                for (int i = 0; i < max_y; i++)
                {
                    for (int j = 0; j < max_x; j++)
                    {
                        r = (byte)((Math.Sin(af * i + 1 + pi * f1 / 180) + 1) * 127);
                        if (r > 255)
                        {
                            r = 0;
                        }

                        if (r > 125)
                        {
                            r = 255;
                        }
                        else
                        {
                            r = 0;
                        }

                        ImageProcessor.setPixel(bitmapData, j, i, Color.FromArgb(r, r, r));
                    }
                }
            }

            Color currentColor;
            int   black = 0;
            int   white = 0;

            for (int y = 0; y < max_y; y++)
            {
                for (int x = 0; x < max_x; x++)
                {
                    currentColor = ImageProcessor.getPixel(x, y, bitmapData);

                    if (currentColor.ToArgb() == Color.White.ToArgb())
                    {
                        white++;
                    }
                    else
                    {
                        black++;
                    }
                }
            }

            ((Bitmap)pc1.Image).UnlockBits(bitmapData);
        }
Exemplo n.º 6
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        public static void drawDitheredLines(double N_sin, double f1, int max_x, int max_y, int XY, PictureBox pc1)
        {
            BitmapData bitmapData = ImageProcessor.getBitmapData((Bitmap)pc1.Image);
            byte       r;
            double     nx = max_x + 1;
            double     pi = Math.PI;
            double     af = pi * 2 * N_sin / nx;

            if (XY == 0)  // Полосы ориентированы перпендикулярно оси X
            {
                for (int i = 0; i < max_x; i++)
                {
                    for (int j = 0; j < max_y; j++)
                    {
                        r = (byte)((Math.Sin(af * i + 1 + pi * f1 / 180) + 1) * 127);
                        if (r > 255)
                        {
                            r = 0;
                        }
                        ImageProcessor.setPixel(bitmapData, i, j, Color.FromArgb(r, r, r));
                    }
                }
            }
            else if (XY == 1) // Полосы ориентированы перпендикулярно оси y
            {
                for (int i = 0; i < max_y; i++)
                {
                    for (int j = 0; j < max_x; j++)
                    {
                        r = (byte)((Math.Sin(af * i + 1 + pi * f1 / 180) + 1) * 127);
                        if (r > 255)
                        {
                            r = 0;
                        }

                        ImageProcessor.setPixel(bitmapData, j, i, Color.FromArgb(r, r, r));
                    }
                }
            }


            Color  currentColor;
            int    averageColor;
            Color  newColor;
            double quantError;

            for (int y = 0; y < max_y; y++)
            {
                for (int x = 0; x < max_x; x++)
                {
                    currentColor = ImageProcessor.getPixel(x, y, bitmapData);
                    averageColor = currentColor.R;

                    if (averageColor >= 128)
                    {
                        newColor = Color.White;
                    }
                    else
                    {
                        newColor = Color.Black;
                    }

                    quantError = averageColor - newColor.R;

                    ImageProcessor.setPixel(bitmapData, x, y, newColor);

                    if (x < max_x - 1)
                    {
                        currentColor = ImageProcessor.getPixel(x + 1, y, bitmapData);
                        double a = (double)(7.0 / 16.0) * quantError;
                        averageColor = currentColor.R + (int)a;

                        if (averageColor > 255)
                        {
                            averageColor = 255;
                        }

                        if (averageColor < 0)
                        {
                            averageColor = 0;
                        }

                        ImageProcessor.setPixel(bitmapData, x + 1, y, Color.FromArgb(averageColor, averageColor, averageColor));
                    }

                    if ((y < max_y - 1) && (x > 0))
                    {
                        currentColor = ImageProcessor.getPixel(x - 1, y + 1, bitmapData);
                        double a = (3.0 / 16.0) * quantError;
                        averageColor = currentColor.R + (int)a;

                        if (averageColor > 255)
                        {
                            averageColor = 255;
                        }

                        if (averageColor < 0)
                        {
                            averageColor = 0;
                        }

                        ImageProcessor.setPixel(bitmapData, x - 1, y + 1, Color.FromArgb(averageColor, averageColor, averageColor));
                    }

                    if (y < max_y - 1)
                    {
                        currentColor = ImageProcessor.getPixel(x, y + 1, bitmapData);
                        double a = (5.0 / 16.0) * quantError;
                        averageColor = currentColor.R + (int)a;

                        if (averageColor > 255)
                        {
                            averageColor = 255;
                        }

                        if (averageColor < 0)
                        {
                            averageColor = 0;
                        }

                        ImageProcessor.setPixel(bitmapData, x, y + 1, Color.FromArgb(averageColor, averageColor, averageColor));
                    }

                    if ((y < max_y - 1) && (x < max_x - 1))
                    {
                        currentColor = ImageProcessor.getPixel(x + 1, y + 1, bitmapData);
                        double a = (1.0 / 16.0) * quantError;
                        averageColor = currentColor.R + (int)a;

                        if (averageColor > 255)
                        {
                            averageColor = 255;
                        }

                        if (averageColor < 0)
                        {
                            averageColor = 0;
                        }

                        ImageProcessor.setPixel(bitmapData, x + 1, y + 1, Color.FromArgb(averageColor, averageColor, averageColor));
                    }
                }
            }

            int black = 0;
            int white = 0;

            for (int y = 0; y < max_y; y++)
            {
                for (int x = 0; x < max_x; x++)
                {
                    currentColor = ImageProcessor.getPixel(x, y, bitmapData);

                    if ((currentColor.ToArgb() != Color.White.ToArgb()) && (currentColor.ToArgb() != Color.Black.ToArgb()))
                    {
                        if (currentColor.R >= 128)
                        {
                            newColor = Color.White;
                        }
                        else
                        {
                            newColor = Color.Black;
                        }
                        ImageProcessor.setPixel(bitmapData, x, y, newColor);
                    }

                    if (currentColor.ToArgb() == Color.White.ToArgb())
                    {
                        white++;
                    }
                    else
                    {
                        black++;
                    }
                }
            }

            ((Bitmap)pc1.Image).UnlockBits(bitmapData);
        }
Exemplo n.º 7
0
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///          Приведение изображения к диапазону без ZArrayDescriptor (перегруженный метод)
        /// </summary>
        public static void Range_Picture(PictureBox pictureBox01, double min, double max)
        {
            // c1 = ImageProcessor.getPixel(i, j, data1);                       // c1 = bmp1.GetPixel(i, j);
            // ImageProcessor.setPixel(data5, i, j, Color.FromArgb(r, r, r));   // bmp2.SetPixel(j, i, c1);
            // bmp5.UnlockBits(data5);
            if (pictureBox01 == null)
            {
                MessageBox.Show("SumClass pictureBox01 == null"); return;
            }


            int width  = pictureBox01.Image.Width;
            int height = pictureBox01.Image.Height;

            Bitmap     bmp2  = new Bitmap(width, height);
            BitmapData data2 = ImageProcessor.getBitmapData(bmp2);

            Bitmap     bmp1  = new Bitmap(pictureBox01.Image, width, height);
            BitmapData data1 = ImageProcessor.getBitmapData(bmp1);


            if (max == min)
            {
                for (int j = 0; j < width; j++)
                {
                    for (int i = 0; i < height; i++)
                    {
                        int c = 0;
                        if (max < 255 && max > 0.0)
                        {
                            c = Convert.ToInt32(max);
                        }
                        if (max > 255)
                        {
                            c = 255;
                        }
                        if (max < 0)
                        {
                            c = 0;
                        }
                        Color c1 = Color.FromArgb(c, c, c);
                        ImageProcessor.setPixel(data2, j, i, c1);
                    }
                }
            }
            if (max != min)
            {
                double mxmn = 255.0 / (max - min);
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        //double fc = zArrayPicture.array[j, i];
                        Color  c2 = ImageProcessor.getPixel(i, j, data1);
                        double fc = c2.G;
                        if (fc > max)
                        {
                            fc = max;
                        }
                        if (fc < min)
                        {
                            fc = min;
                        }
                        int   c  = Convert.ToInt32((fc - min) * mxmn);
                        Color c1 = Color.FromArgb(c, c, c);
                        ImageProcessor.setPixel(data2, i, j, c1);
                    }
                }
            }
            pictureBox01.Image = bmp2;
            bmp2.UnlockBits(data2);
        }