Exemple #1
0
        private void btn_apply_gamma_Click(object sender, EventArgs e)
        {
            PixelOperations gamma = new PixelOperations();
            b = (Bitmap)ControlTab.SelectedTab.BackgroundImage;
            final_image = gamma.Gamma(p.Buffering(b), (double)numericUpDown_gamma.Value);

            b = p.convert_Buffer_to_Bitmap(final_image, final_image.GetLength(0), final_image.GetLength(1));

            ControlTab.SelectedTab.BackgroundImage = b;
        }
	public BlockLayer(LayerBlendMode b) {
		colorMap = new Color[BlockEditor.textureSize, BlockEditor.textureSize];
		blendMode = b;

		//Temp
		for (int x = 0; x < colorMap.GetLength(0); x++) {
			for (int y = 0; y < colorMap.GetLength(1); y++) {
				//colorMap[x, y] = new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f));
				colorMap[x, y] = new Color(0f, 0f, 0f, 0f);
			}
		}
	}
		public PintaMatriz()
		{
			coloresUsados = new LlistaOrdenada<int, Pen>();
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			matriz = new Color[6400, 3600];
			for (int y = 0; y < matriz.GetLength(DimensionMatriz.Y); y++)
				for (int x = 0; x < matriz.GetLength(DimensionMatriz.X); x++)
					matriz[x, y] = Color.PaleVioletRed;
			Matriz = matriz;
			this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
		}
Exemple #4
0
        public static System.Drawing.Bitmap CreateImage(Color[,] colormap)
        {
            System.Drawing.Bitmap img = new System.Drawing.Bitmap(colormap.GetLength(0), colormap.GetLength(1));

            for (int x = 0; x < img.Height; ++x)
            {
                for (int y = 0; y < img.Width; ++y)
                {
                    img.SetPixel(x, y, colormap[x, y]);
                }
            }
            return(img);
        }
Exemple #5
0
        private void BytesToColors(byte[] bytes, Color[,] colors)
        {
            if (colors.Length * 4 != bytes.Length)
            {
                throw new ArgumentException("Non-matching array sizes!");
            }

            int  byteCount = 0;
            byte r, g, b;

            for (int y = 0; y < colors.GetLength(1); y++)
            {
                for (int x = 0; x < colors.GetLength(0); x++)
                {
                    b = bytes[byteCount++];
                    g = bytes[byteCount++];
                    r = bytes[byteCount++];
                    byteCount++;
                    colors[x, y] = Color.FromArgb(0, r, g, b);
                }
            }
        }
        //private static int minY = 0;
        //private static int maxY = 460;

        //private static int minX = 200;
        //private static int maxX = 540;

        //static int xdist = (maxX - minX) / 3;
        //static int ydist = (maxY - minY) / 3;

        private static void PrintColorMap(Bitmap image, Color[,] compMap, int compCounter)
        {
            int dist             = 100;
            int distX            = dist / 5;
            int diffBetweenBoxes = 1;

            for (int i = 0; i < compMap.GetLength(0); i++)
            {
                for (int j = 0; j < compMap.GetLength(1); j++)
                {
                    int xStart = j * (dist + diffBetweenBoxes) + compCounter * (distX);
                    int yStart = i * dist;
                    image.SetPixel(xStart, yStart, compMap[i, j]);

                    for (int k2 = xStart; k2 < xStart + distX; k2++)
                    {
                        for (int k1 = yStart; k1 < yStart + dist - 1; k1++)
                        {
                            image.SetPixel(k2, k1, compMap[i, j]);
                        }
                    }
                }
            }

            Color bColor = Color.DarkRed;

            for (int i = 0; i < image.Height; i++)
            {
                image.SetPixel(distX * 5, i, bColor);
                image.SetPixel(2 * (distX * 5) + diffBetweenBoxes, i, bColor);
            }

            for (int i = 0; i < image.Width; i++)
            {
                image.SetPixel(i, distX * 5 - diffBetweenBoxes, bColor);
                image.SetPixel(i, 2 * (distX * 5) - diffBetweenBoxes, bColor);
            }
        }
        public Color[,] Apply(Color[,] input)
        {
            int width  = input.GetLength(0);
            int height = input.GetLength(1);

            Color[,] result = new Color[width, height];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    if (x == 0 || y == 0 || x == width - 1 || y == height - 1) //  If the input pixel lies at the border, its value may remain the same.
                    {
                        var p = input[x, y];
                        result[x, y] = Color.FromArgb(p.A, p.R, p.G, p.B);
                    }
                    else
                    {
                        var p = input[x, y];

                        // the 4 pixels left, right, above and below
                        var left  = input[x - 1, y];
                        var right = input[x, y + 1];
                        var above = input[x, y + 1];
                        var below = input[x, y - 1];

                        // find average
                        var avgR = (left.R + right.R + above.R + below.R) / 4;
                        var avgG = (left.G + right.G + above.G + below.G) / 4;
                        var avgB = (left.B + right.B + above.B + below.B) / 4;

                        result[x, y] = Color.FromArgb(p.A, avgR, avgG, avgB);
                    }
                }
            }

            return(result);
        }
Exemple #8
0
        public static FrameworkElement GetSwatches(Action <Color> colorSink)
        {
            swatches = new Color[10, 14]
            {
                { Black, DarkSlateGray, DarkGreen, Green, Olive, DarkOliveGreen, SeaGreen, DarkSeaGreen, LimeGreen, YellowGreen, MediumAquamarine, LightSeaGreen, DarkCyan, Teal },
                { DimGray, SlateGray, LightSlateGray, ForestGreen, DarkKhaki, OliveDrab, MediumSeaGreen, Chartreuse, LightGreen, MediumSpringGreen, Aquamarine, Turquoise, MediumTurquoise, CadetBlue },
                { Gray, DarkGray, Gainsboro, LightGoldenrodYellow, Beige, GreenYellow, LawnGreen, Lime, SpringGreen, Honeydew, PaleTurquoise, LightBlue, DarkTurquoise, SteelBlue },
                { Silver, LightGray, WhiteSmoke, Snow, White, LemonChiffon, PaleGreen, Ivory, FloralWhite, MintCream, LightCyan, Cyan, DeepSkyBlue, CornflowerBlue },
                { Khaki, BlanchedAlmond, Bisque, Cornsilk, Transparent, OldLace, White, Azure, GhostWhite, AliceBlue, PowderBlue, LightSkyBlue, DodgerBlue, MediumBlue },
                { PaleGoldenrod, Wheat, Moccasin, LightYellow, PapayaWhip, PeachPuff, AntiqueWhite, SeaShell, LavenderBlush, Lavender, LightSteelBlue, SkyBlue, RoyalBlue, Blue },
                { Yellow, BurlyWood, NavajoWhite, Orange, Coral, DarkSalmon, LightSalmon, LightPink, MistyRose, Linen, Thistle, MediumSlateBlue, SlateBlue, DarkSlateBlue },
                { Gold, Tan, SandyBrown, DarkOrange, Tomato, Salmon, LightCoral, RosyBrown, Violet, Pink, Plum, BlueViolet, MediumPurple, DarkBlue },
                { Goldenrod, Peru, Chocolate, OrangeRed, Firebrick, Crimson, IndianRed, PaleVioletRed, Orchid, MediumOrchid, DarkViolet, DarkOrchid, Indigo, Navy },
                { DarkGoldenrod, SaddleBrown, Sienna, Red, DarkRed, Maroon, Brown, HotPink, Magenta, DeepPink, MediumVioletRed, DarkMagenta, Purple, MidnightBlue },
            };

            var grid = new UniformGrid()
            {
                Rows    = swatches.GetLength(0),
                Columns = swatches.GetLength(1),
            };

            grid.Width  = SwatchSize * grid.Columns;
            grid.Height = SwatchSize * grid.Rows;

            for (int row = 0; row < grid.Rows; row++)
            {
                for (int column = 0; column < grid.Columns; column++)
                {
                    var swatch = GetColorButton(swatches[row, column], colorSink);
                    Grid.SetColumn(swatch, column);
                    Grid.SetRow(swatch, row);
                    grid.Children.Add(swatch);
                }
            }

            return(grid);
        }
Exemple #9
0
 static void CopyRect(Color[,] src, int x, int y, Color[,] dest, int xd, int yd, int w, int h)
 {
     // top-left : [x,y]
     for (int i = 0; i < w; i++)
     {
         for (int j = 0; j < h; j++)
         {
             // 書き込み先が範囲外なら何もしない
             if (i + xd < dest.GetLength(0) && j + yd < dest.GetLength(1))
             {
                 // 書き込むデータがなければ黒
                 if (i + x < src.GetLength(0) && j + y < src.GetLength(1))
                 {
                     dest[i + xd, j + yd] = src[i + x, j + y];
                 }
                 else
                 {
                     dest[i + xd, j + yd] = Color.Black;
                 }
             }
         }
     }
 }
Exemple #10
0
        private bool ProcessImage(Color[,] image, CIELAB[,] imageLAB)
        {
            double             thresh = 5;
            UnionFind <CIELAB> uf     = new UnionFind <CIELAB>((a, b) => (a.SqDist(b) <= thresh));

            int[,] assignments = uf.ConnectedComponents(imageLAB);//Util.Map<Color, CIELAB>(image, Util.RGBtoLAB));
            int numC = -1;

            for (int i = 0; i < image.GetLength(0); i++)
            {
                for (int j = 0; j < image.GetLength(1); j++)
                {
                    numC = Math.Max(numC, assignments[i, j] + 1);
                }
            }
            if (numC >= 2)
            {
                RemoveBackground(image, imageLAB);
            }

            //if it is a black and white image (with num connected components >= 2), it's not a valid color image
            return(!(isBlackWhite(image, imageLAB) && numC >= 2));
        }
        private static Vector[] FromColors(Color[,] image)
        {
            int width  = image.GetLength(1);
            int height = image.GetLength(0);

            Vector[] result = new Vector[width * height];

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Vector v            = new Vector(3);
                    Color  currentColor = image[i, j];

                    v.Values[0]           = currentColor.R;
                    v.Values[1]           = currentColor.G;
                    v.Values[2]           = currentColor.B;
                    result[i * width + j] = v;
                }
            }

            return(result);
        }
Exemple #12
0
        /// <summary>
        /// 将图像生成的像素数组重新组建为新图像
        /// </summary>
        /// <param name="图像">曾用于生成像素数组的源图像</param>
        /// <param name="规范化像素数组">规范化像素数组</param>
        /// <param name="宽度">像素数组对应的图像区域宽度</param>
        /// <param name="高度">像素数组对应的图像区域高度</param>
        /// <param name="像素格式">使用的像素格式</param>
        /// <returns>新图像</returns>
        public static BitmapSource 由像素数组重建图像(this BitmapSource 图像, PixelFormat 像素格式, Color[,] 规范化像素数组)
        {
            byte[] data  = new byte[图像.Format.BitsPerPixel / 8 * 规范化像素数组.GetLength(0) * 规范化像素数组.GetLength(1)];
            int    index = 0;

            for (int i = 0; i < 规范化像素数组.GetLength(0); i++)
            {
                for (int j = 0; j < 规范化像素数组.GetLength(1); j++)
                {
                    if (图像.Format.BitsPerPixel == 32 || 图像.Format.BitsPerPixel == 24)
                    {
                        data[index++] = 规范化像素数组[i, j].R;
                        data[index++] = 规范化像素数组[i, j].G;
                        data[index++] = 规范化像素数组[i, j].B;
                        if (图像.Format.BitsPerPixel == 32)
                        {
                            data[index++] = 规范化像素数组[i, j].A;
                        }
                    }
                }
            }
            return(由像素数组重建图像(图像, data, 规范化像素数组.GetLength(1), 规范化像素数组.GetLength(0)));
        }
Exemple #13
0
        public static string ToReadableString(this Color[,] grid)
        {
            int rowCount = grid.GetLength(0);
            int colCount = grid.GetLength(1);
            var sb       = new StringBuilder();

            for (int i = 0; i < rowCount; i++)
            {
                for (int j = 0; j < colCount; j++)
                {
                    if (!_cache.ContainsKey(grid[i, j]))
                    {
                        var charToInsert = validChars[_cacheCounter++].ToString();
                        _cache[grid[i, j]] = charToInsert;
                    }
                    sb.Append(_cache[grid[i, j]]);
                    sb.Append(",");
                }
                sb.Length -= 1;
                sb.AppendLine();
            }
            return(sb.ToString());
        }
Exemple #14
0
        public Color[,] Apply(Color[,] input)
        {
            int width  = input.GetLength(0);
            int height = input.GetLength(1);

            Color[,] result = new Color[width, height];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int grayP = 0;
                    var p     = input[x, y];
                    grayP = (p.R + p.G + p.B) / 3;

                    //i used average. there are median,max value of rgb (for dark gray) and min value of rgb(for light gray).

                    result[x, y] = Color.FromArgb(p.A, grayP, grayP, grayP);
                }
            }

            return(result);
        }
        public static Color[,] FromRGB2YUVAndBack(Color[,] im)
        {
            var bit = Colorm2bitmap(im);

            for (int x = 0; x < im.GetLength(0); x++)
            {
                for (int y = 0; y < im.GetLength(1); y++)
                {
                    var px = bit.GetPixel(x, y);

                    var rgb = new RGB(px.R, px.G, px.B);

                    var yuv = rgb.ToYUV();

                    // convert back to rgb since we cant display yuv
                    rgb = yuv.ToRGB();

                    bit.SetPixel(x, y, Color.FromArgb(px.A, rgb.r, rgb.g, rgb.b));
                }
            }

            return(Bitmap2colorm(bit));
        }
Exemple #16
0
        public static void CreateImage(string filename, Color[,] colormap)
        {
            System.Drawing.Bitmap img = new System.Drawing.Bitmap(colormap.GetLength(0), colormap.GetLength(1));

            for (int x = 0; x < img.Height; ++x)
            {
                for (int y = 0; y < img.Width; ++y)
                {
                    img.SetPixel(x, y, colormap[x, y]);
                }
            }

            img.Save(filename, System.Drawing.Imaging.ImageFormat.Jpeg);
        }
Exemple #17
0
        private Color[,] ApplyOr(Color[,] firstImage, Color[,] secondImage)
        {
            if (firstImage.GetLength(0) != secondImage.GetLength(0) ||
                firstImage.GetLength(1) != secondImage.GetLength(1))
            {
                MessageBox.Show("Error images not the same size");
                return(firstImage);
            }

            Color[,] newImage = new Color[firstImage.GetLength(0), firstImage.GetLength(1)];

            for (int u = 0; u < firstImage.GetLength(0); u++)
            {
                for (int v = 0; v < firstImage.GetLength(1); v++)
                {
                    int newValue = firstImage[u, v].G == 255 || secondImage[u, v].G == 255 ? 255 : 0;
                    newImage[u, v] = Color.FromArgb(newValue, newValue, newValue);
                }
            }


            return(newImage);
        }
Exemple #18
0
        private static Bitmap getFlatBitmapFromPixels(Color[,] pixels)
        {
            Bitmap bmp = new Bitmap(pixels.GetLength(1), pixels.GetLength(0), PixelFormat.Format24bppRgb);

            Color bg = Util.ColorFromVec4(FractalManager.Fractal.BackgroundColor);

            Color pix;
            int   r, g, b, a;

            for (int y = 0; y < bmp.Height; y++)
            {
                for (int x = 0; x < bmp.Width; x++)
                {
                    pix = pixels[y, x];
                    a   = (int)pix.A;
                    r   = (int)bg.R * (255 - a) / 255 + (int)pix.R * a / 255;
                    g   = (int)bg.G * (255 - a) / 255 + (int)pix.G * a / 255;
                    b   = (int)bg.B * (255 - a) / 255 + (int)pix.B * a / 255;
                    bmp.SetPixel(x, y, Color.FromArgb(r, g, b));
                }
            }
            return(bmp);
        }
        public bool ToImage(Color[,] colorGrid)
        {
            int adjusted_col;
            int height = colorGrid.GetLength(0);
            int width  = colorGrid.GetLength(1);

            for (int col = 0; col < width; col++)
            {
                for (int row = 0; row < height; row++)
                {
                    if (row % 2 == 0)
                    {
                        adjusted_col = Math.Abs(col - width + 1);
                    }
                    else
                    {
                        adjusted_col = col;
                    }
                    Image.SetPixel(adjusted_col, row, colorGrid[row, col]);
                }
            }
            return(true);
        }
        //Greyscale filter (2.2) and optimizing with Parallel.For (2.4)
        public Color[,] Apply(Color[,] input)
        {
            int width  = input.GetLength(0);
            int height = input.GetLength(1);

            Color[,] result = new Color[width, height];

            Parallel.For(0, width, x =>
            {
                Parallel.For(0, height, y =>
                {
                    var p        = input[x, y];
                    int a        = p.A;
                    int r        = p.R;
                    int g        = p.G;
                    int b        = p.B;
                    int avg      = (r + g + b) / 3;
                    result[x, y] = Color.FromArgb(a, avg, avg, avg);
                });
            });

            return(result);
        }
Exemple #21
0
        public void Draw(ref Color[,] image)
        {
            _step += Step;

            for (int i = 0; i < image.GetLength(0); i++)
            {
                for (int j = 0; j < image.GetLength(1); j++)
                {
                    double calc  = i * Math.Sin(_step / Horizontal) + j * Math.Sin(_step / Vertical);
                    double sat   = (Math.Sin(calc * Period + _step) + 1) / 2;
                    var    color = new Color(sat, sat, sat);

                    if (Invert)
                    {
                        image[i, j] = color.Invert();
                    }
                    else
                    {
                        image[i, j] = color;
                    }
                }
            }
        }
    public void RenderColourMap(Color[,] map)
    {
        int width  = map.GetLength(0);
        int height = map.GetLength(1);

        Texture2D texture = new Texture2D(width, height);

        Color[] colourMap = new Color[width * height];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                colourMap[y * width + x] = map[x, y];
            }
        }

        texture.filterMode = FilterMode.Point;
        texture.SetPixels(colourMap);
        texture.Apply();

        Renderer.sharedMaterial.mainTexture = texture;
        Renderer.transform.localScale       = new Vector3(width, 1, height) * 0.1f;
    }
Exemple #23
0
        // グレースケール
        public static Color[,] GrayScale(Color[,] beforepixel)
        {
            int width  = beforepixel.GetLength(0);
            int height = beforepixel.GetLength(1);

            Color[,] repixel = new Color[width, height];
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    Color ori = beforepixel[i, j];

                    int brightness = (int)(ori.GetBrightness() * 255);
                    repixel[i, j] = Color.FromArgb(
                        ori.A,
                        brightness,
                        brightness,
                        brightness);
                }
            }

            return(repixel);
        }
Exemple #24
0
        ///check if the next position is valid
        public bool NextPosValid(Shape currentShape, int direction)
        {
            Vector2 currentpos = currentShape.position;

            //change the next position based on the parameter
            switch (direction)
            {
            case 0: currentShape.position.Y++; break;

            case 1: currentShape.position.Y--; break;

            case 2: currentShape.position.X++; break;

            case 3: currentShape.position.X--; break;

            default: break;
            }


            //loop through all the blocks and see if it overlaps the current grid
            for (int x = 0; x < 4; x++)
            {
                for (int y = 0; y < 4; y++)
                {
                    //x: x-value shape
                    //y: y-value shape
                    if (currentShape.arr[x, y] > 0)
                    {
                        if (currentShape.position.Y + y >= height || currentShape.position.X + x < 0)
                        {
                            currentShape.position = currentpos;
                            return(false);
                        }

                        if (placedTiles.GetLength(0) > (int)currentShape.position.X + x && placedTiles.GetLength(1) > (int)currentShape.position.Y + y)
                        {
                            if (placedTiles[(int)currentShape.position.X + x, (int)currentShape.position.Y + y] != Color.White)
                            {
                                currentShape.position = currentpos;
                                return(false);
                            }
                        }
                    }
                }
            }

            //reset the position
            currentShape.position = currentpos;
            return(true);
        }
Exemple #25
0
        public void Draw(ref Color[,] image)
        {
            _step += Step;

            for (int i = 0; i < image.GetLength(0); i++)
            {
                for (int j = 0; j < image.GetLength(1); j++)
                {
                    double sat = GetSaturation(i, j);

                    var color = new Color(sat, sat, sat);

                    if (Invert)
                    {
                        image[i, j] = color.Invert();
                    }
                    else
                    {
                        image[i, j] = color;
                    }
                }
            }
        }
Exemple #26
0
        /// <summary>
        /// Filters a portion of the rgbImage into a binary image
        /// </summary>
        /// <param name="rgbImage">unfiltered image</param>
        /// <param name="filter">color range to use for filtering</param>
        /// <param name="filterPixels">filtered binary image</param>
        /// <param name="xMin">inclusive</param>
        /// <param name="xMax">exclusive</param>
        private static void ColorFilterPiece(Color[,] rgbImage, ColorFilter filter, ref bool[,] filterPixels, int xMin, int xMax)
        {
            Color pixelColor;
            int   height = rgbImage.GetLength(1);

            for (int x = xMin; x < xMax; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    pixelColor         = rgbImage[x, y];
                    filterPixels[x, y] = filter.ColorInRange(pixelColor);
                }
            }
        }
Exemple #27
0
        static String Decode(Color[,] image, int distance)
        {
            int counter = 0;

            byte[] byteMessage = new byte[distance * 8];
            for (int i = 0; i < image.GetLength(0); i++)
            {
                for (int j = 0; j < image.GetLength(1); j++)
                {
                    for (int rgb = 0; rgb < 3; rgb++)
                    {
                        if (counter < distance * 8)
                        {
                            byte mask = 1;
                            switch (rgb)
                            {
                            case 0:
                                byteMessage[counter / 8] |= (image[i, j].R % 2 == 1) ? mask <<= (7 - (counter % 8)) : byteMessage[counter / 8];
                                break;

                            case 1:
                                byteMessage[counter / 8] |= (image[i, j].G % 2 == 1) ? mask <<= (7 - (counter % 8)) : byteMessage[counter / 8];
                                break;

                            case 2:
                                byteMessage[counter / 8] |= (image[i, j].B % 2 == 1) ? mask <<= (7 - (counter % 8)) : byteMessage[counter / 8];
                                break;
                            }
                            counter++;
                        }
                    }
                }
            }
            string result = Encoding.ASCII.GetString(byteMessage);

            return(result);
        }
        //Создание целого узора
        private Color[,] MakePattern(Color[,] tmp)
        {
            Color[,] CALLIKA = tmp;
            for (int i = 0; i < CALLIKA.GetLength(0); ++i)
            {
                for (int j = 0; j < CALLIKA.GetLength(1); ++j)
                {
                    CALLIKA[i, j] = Color.Pink;
                }
            }

            CALLIKA        = PlusColumn(0, CALLIKA);
            CALLIKA        = PlusColumn(4, CALLIKA);
            CALLIKA        = PlusColumn(6, CALLIKA);
            CALLIKA        = PlusColumn(8, CALLIKA);
            CALLIKA        = PlusColumn(12, CALLIKA);
            CALLIKA        = PlusColumn(16, CALLIKA);
            CALLIKA        = PlusColumn(18, CALLIKA);
            CALLIKA        = PlusColumn(23, CALLIKA);
            CALLIKA        = PlusColumn(25, CALLIKA);
            CALLIKA        = PlusLine(0, 0, CALLIKA);
            CALLIKA        = PlusLine(0, 4, CALLIKA);
            CALLIKA        = PlusLine(0, 0, CALLIKA);
            CALLIKA        = PlusLine(4, 0, CALLIKA);
            CALLIKA        = PlusLine(4, 2, CALLIKA);
            CALLIKA        = PlusLine(8, 4, CALLIKA);
            CALLIKA        = PlusLine(12, 4, CALLIKA);
            CALLIKA        = PlusLine(23, 0, CALLIKA);
            CALLIKA        = PlusLine(23, 2, CALLIKA);
            CALLIKA[19, 2] = Color.Black;
            CALLIKA[20, 1] = Color.Black;
            CALLIKA[20, 3] = Color.Black;
            CALLIKA[21, 0] = Color.Black;
            CALLIKA[21, 4] = Color.Black;

            return(CALLIKA);
        }
    public List <RecognizedItem> Recognize(Color[,] image)
    {
        var gridColors = ColorBlockNames.Keys;

        Vector4[,] averageColors = new Vector4[GridNumber.x, GridNumber.y];
        int width  = image.GetLength(0); // NOTE: not sure if the order of dimensions is right
        int height = image.GetLength(1);

        //calculating average colors
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                //Debug.Log(i.ToString() + " " + j.ToString());
                Vector2Int blockIndexes = GetBlockIndexes(new Vector2Int(i, j), image);
                if (blockIndexes.x >= 0 && blockIndexes.y >= 0)
                {
                    averageColors[blockIndexes.x, blockIndexes.y] += (Vector4)image[i, j];
                }
            }
        }
        //matching average colors with the closest from the palette
        var blocks = new List <RecognizedItem>();

        for (int i = 0; i < averageColors.GetLength(0); i++)
        {
            for (int j = 0; j < averageColors.GetLength(1); j++)
            {
                averageColors[i, j]  /= GetNPixelsPerBlock(image);
                averageColors[i, j].w = 0;
                blocks.Add(new RecognizedItem(new Vector2(i, j),
                                              ColorBlockNames[gridColors.OrderBy(
                                                                  color => Vector4.Distance(averageColors[i, j], color)).ToArray()[0]]));
            }
        }
        return(blocks);
    }
Exemple #30
0
        private Vector2 TexturesCollide(Color[,] tex1, Matrix mat1, Color[,] tex2, Matrix mat2)
        {
            Matrix mat1to2 = mat1 * Matrix.Invert(mat2);
            int    width1  = tex1.GetLength(0);
            int    height1 = tex1.GetLength(1);
            int    width2  = tex2.GetLength(0);
            int    height2 = tex2.GetLength(1);

            for (int x1 = 0; x1 < width1; x1++)
            {
                for (int y1 = 0; y1 < height1; y1++)
                {
                    Vector2 pos1 = new Vector2(x1, y1);
                    Vector2 pos2 = Vector2.Transform(pos1, mat1to2);


                    int x2 = (int)pos2.X;
                    int y2 = (int)pos2.Y;
                    if ((x2 >= 0) && (x2 < width2))
                    {
                        if ((y2 >= 0) && (y2 < height2))
                        {
                            if (tex1[x1, y1].A > 0)
                            {
                                if (tex2[x2, y2].A > 0)
                                {
                                    Vector2 screenPos = Vector2.Transform(pos1, mat1);
                                    return(screenPos);
                                }
                            }
                        }
                    }
                }
            }

            return(new Vector2(-1, -1));
        }
Exemple #31
0
        public static Color GetColorFromPxArray(Color[,] pixelArrayOLD, int width)
        {
            //if (CountGCFPXA > width)
            if (_countGcfpxa > pixelArrayOLD.GetLength(1) - 1)
            {
                _countGcfpxa   = 0;
                _countGcfpxaH += 1;
            }
            //Debug.Print($"test = {CountGCFPXA}-h{CountGCFPXA_H}");
            var goodColor = pixelArrayOLD[_countGcfpxaH, _countGcfpxa];

            //Color.PaleGreen;//pixelArrayOLD[CountGCFPXA_H,CountGCFPXA];
            _countGcfpxa += 1;
            return(goodColor);
        }
Exemple #32
0
        private unsafe static Bitmap uCreateBitmap(Color[,] pixels)
        {
            Bitmap     bmp  = new Bitmap(pixels.GetLength(0), pixels.GetLength(1));
            BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

            for (int y = 0; y < data.Height; y++)
            {
                // vypocte ukazatel na zacatek y-teho radku
                int *retPos = (int *)((int)data.Scan0 + (y * data.Stride));

                int x = 0;
                while (x < data.Width)
                {
                    // vyplni pixel nahodnou barvou
                    *retPos = pixels[x, y].ToArgb();

                    // posun na dalsi pixel
                    retPos++; x++;
                }
            }
            bmp.UnlockBits(data);

            return(bmp);
        }
        public static void RenderBitmap(this Color[,] bitmap, ISprite sprite)
        {
            int viewHeight = bitmap.GetLength(0);
            int viewWidth  = bitmap.GetLength(1);

            for (int y = 0; y < sprite.Size.Height; y++)
            {
                for (int x = 0; x < sprite.Size.Width; x++)
                {
                    int col = sprite.Position.Left + x;
                    int row = sprite.Position.Top + y;

                    if (col >= 0 &&
                        col < viewWidth &&
                        row >= 0 &&
                        row < viewHeight &&
                        sprite[y, x] != Color.None &&
                        sprite[y, x] != Color.Transparent)
                    {
                        bitmap[row, col] = sprite[y, x];
                    }
                }
            }
        }
Exemple #34
0
        private void mnuProcessScale200_Click(object sender, EventArgs e)
        {
            if (transformedPic != null)
            {
                Color[,] TempPic = new Color[transformedPic.GetLength(0), transformedPic.GetLength(1)];
                //Copy transformed pic back into temp pic
                for (int i = 0; i < transformedPic.GetLength(0); i++)
                {
                    for (int j = 0; j < transformedPic.GetLength(1); j++)
                    {
                        TempPic[i, j] = transformedPic[i, j];
                    }
                }

                //Copy transformed pic over to temporary pic (double the rows and double the columns)
                //by averaging every other pixel (even lines avg right and left, odd lines avg top and bottom)
                double Red, Green, Blue;
                //Resize transformed pic
                transformedPic = new Color[TempPic.GetLength(0) * 2, TempPic.GetLength(1) * 2];

                //Loop all present pixels first
                for (int i = 0; i < transformedPic.GetLength(0); i += 2)
                {
                    for (int j = 0; j < transformedPic.GetLength(1); j += 2)
                    {
                        transformedPic[i, j] = TempPic[i / 2, j / 2];
                    }
                }

                //Now loop empty pixels in even rows (some pixels filled)
                for (int i = 0; i < transformedPic.GetLength(0); i +=2)
                {
                    for (int j = 1; j < transformedPic.GetLength(1); j += 2)
                    {
                        //Check bottom edge of image (cannot average)
                        if (i + 1 > transformedPic.GetLength(0) - 1)
                        {
                            transformedPic[i, j] = transformedPic[i - 1, j];
                        }
                        //Check right edge of image (cannot average)
                        else if (j + 1 > transformedPic.GetLength(1) - 1)
                        {
                            transformedPic[i, j] = transformedPic[i, j - 1];
                        }
                        //For all other cases, average pixel colours to the left and right
                        else
                        {
                            Red = (transformedPic[i, j - 1].R + transformedPic[i, j + 1].R) / 2 + 0.5;
                            Blue = (transformedPic[i, j - 1].B + transformedPic[i, j + 1].B) / 2 + 0.5;
                            Green = (transformedPic[i, j - 1].G + transformedPic[i, j + 1].G) / 2 + 0.5;

                            //Recombine colour based on new values
                            transformedPic[i, j] = Color.FromArgb((int)Red, (int)Green, (int)Blue);
                        }
                    }
                }

                //Now loop empty pixels in odd rows (whole row empty)
                for (int i = 1; i < transformedPic.GetLength(0); i += 2)
                {
                    for (int j = 0; j < transformedPic.GetLength(1); j++)
                    {
                        //Check bottom edge of image (cannot average)
                        if (i + 1 > transformedPic.GetLength(0) - 1)
                        {
                            transformedPic[i, j] = transformedPic[i - 1, j];
                        }
                        //Check right edge of image (cannot average)
                        else if (j + 1 > transformedPic.GetLength(1) - 1)
                        {
                            transformedPic[i, j] = transformedPic[i, j - 1];
                        }
                        //For all other cases, average pixel colours to the top and bottom
                        else
                        {
                            Red = (transformedPic[i - 1, j].R + transformedPic[i + 1, j].R) / 2 + 0.5;
                            Blue = (transformedPic[i - 1, j].B + transformedPic[i + 1, j].B) / 2 + 0.5;
                            Green = (transformedPic[i - 1, j].G + transformedPic[i + 1, j].G) / 2 + 0.5;

                            //Recombine colour based on new values
                            transformedPic[i, j] = Color.FromArgb((int)Red, (int)Green, (int)Blue);
                        }
                    }
                }

                //Redraw
                this.Refresh();
            }
        }
        private void mnuProcessMirrorV_Click(object sender, EventArgs e)
        {
            if (transformedPic != null) //Runs only if a picture loaded
            {
                int height = transformedPic.GetLength(0); //Gets the height of the original picture
                int width = transformedPic.GetLength(1); //Gets the width of the original picture;
                Color[,] storage = new Color[2 * height, width]; //Creates a new array that is double in height than image on screen

                for (int i = 0; i < storage.GetLength(0); i++) //Loops through each element in the array
                {
                    for (int j = 0; j < storage.GetLength(1); j++)
                    {
                        if (i < height) //Copies image to storage
                            storage[i, j] = transformedPic[i, j];
                        else //Copies flipped image to storage
                            storage[i, j] = storage[height - (i-height), j];
                    }
                }

                transformedPic = new Color[storage.GetLength(0), storage.GetLength(1)]; //Reinitializes transformedPic

                for (int i = 0; i < transformedPic.GetLength(0); i++) //Loops through each element in the array
                {
                    for (int j = 0; j < transformedPic.GetLength(1); j++)
                    {
                        transformedPic[i, j] = storage[i, j]; //Copies array
                    }
                }
            }
            this.Refresh(); //Re-Draws transformedPic on image
        }
Exemple #36
0
        private void UtworzSpektrogram()
        {
            OdswiezDane();
            koloryLinCalosc = new Color[cwt.WynikCWT.GetLength(0), cwt.WynikCWT.GetLength(1)];
            koloryLogCalosc = new Color[cwt.WynikCWT.GetLength(0), cwt.WynikCWT.GetLength(1)];

            double dcLin = (paletaKolorow.Paleta.Length - 1) / (double)cwt.MaxCWTDlaInt16;
            double dcLog = (paletaKolorow.Paleta.Length - 1) / -Math.Log10(minLogarytmowane);

            double liczbaLogarytmowana = 0.0;
            for (int i = 0; i < koloryLinCalosc.GetLength(0); i++)
                for (int j = 0; j < koloryLinCalosc.GetLength(1); j++)
                {
                    koloryLinCalosc[i, j] = paletaKolorow.Paleta[(int)(cwt.WynikCWT[i, j] * dcLin)];

                    //P_B = log10(P / P_0)
                    //wartosc w belach (P_B) jest rowna tej wartosc w ujeciu liniowym podzielonej przez P_0 i zlogarytmowanej.
                    //P_0 to wartość odniesienia 0 B (0 dB). Tutaj to wartosc maks CWT
                    //skalowanie zalezne od ustawionego minimum, tutaj poczatkowo -6 B (-60 dB); wartosc mozna zmienic
                    //gdy liczba logarytmowana jest mniejsza od minimumLogarytmowanego przyjmuje sie, ze wartosc transformaty
                    //w tym punkcie wynosi 0; w poczatkowym przykladzie wiec rozciagamy skale od 0 do -60 dB a wyniki ponizej traktujemy
                    //jako 0
                    liczbaLogarytmowana = cwt.WynikCWT[i, j] / (double)cwt.MaxCWTDlaInt16;
                    if (liczbaLogarytmowana > minLogarytmowane)
                        koloryLogCalosc[i, j] = paletaKolorow.Paleta[(int)(Math.Log10(liczbaLogarytmowana) * dcLog) + paletaKolorow.Paleta.Length - 1];
                    else
                        koloryLogCalosc[i, j] = paletaKolorow.Paleta[0];
                }
        }
 public void Reset()
 {
     // Shutdown the timer if it's running
     this.Stop();
     // Dump anything in the queue
     m_images.Clear();
     // Reset the display buffers
     m_dispbuffer = new Color[8, 8];
     m_width = m_dispbuffer.GetLength(0);
     m_newdisplay = false;
     m_pendingsync = null;
     m_offset = 0;
 }
Exemple #38
0
        private void mnuProcessRotateLeft_Click(object sender, EventArgs e)
        {
            if (transformedPic != null)
            {
                //Switch length and width in temporary pic
                Color[,] TempPic = new Color[transformedPic.GetLength(1), transformedPic.GetLength(0)];
                int Length = transformedPic.GetLength(1) - 1;

                //Loop all rows and columns
                for (int i = 0; i < TempPic.GetLength(0); i++)
                {
                    for (int j = 0; j < TempPic.GetLength(1); j++)
                    {
                        TempPic[i, j] = transformedPic[j, Length - i];
                    }
                }

                //Resize transformed pic
                transformedPic = new Color[TempPic.GetLength(0), TempPic.GetLength(1)];

                //Copy temporary pic back into transformed pic
                for (int i = 0; i < transformedPic.GetLength(0); i++)
                {
                    for (int j = 0; j < transformedPic.GetLength(1); j++)
                    {
                        transformedPic[i, j] = TempPic[i, j];
                    }
                }

                //Redraw
                this.Refresh();
            }
        }
        int m_width; // This (and m_dispbuffer) must be locked for access when the timer is running! (Lock it with m_dispbuffer)

        #endregion Fields

        #region Constructors

        public MatrixDisplayEngine(string portname, int speed, int period)
        {
            // Initialize our variables
            m_targetport = new SerialPort(portname, speed);
            if (period <=0) {
                throw new System.ArgumentException("Period must be a positive integer.");
            }
            m_period = period;
            m_linebuffer = new byte[8 * 3]; // 24 bytes (just enough for one line.)
            m_dispbuffer = new Color[8, 8]; // One blank screen
            m_width = m_dispbuffer.GetLength(0);
            m_images = new Queue<MatrixQueueItem>();
            m_pendingsync = null;
            m_newdisplay = false;
            m_nextdisplay = new Color[8, 8];
        }
Exemple #40
0
        private void mnuProcessScale50_Click(object sender, EventArgs e)
        {
            if (transformedPic != null)
            {
                //Check for minimum 1 by 1 size
                if (transformedPic.GetLength(0) / 2 >= 1 && transformedPic.GetLength(1) / 2 >= 1)
                {
                    Color[,] TempPic = new Color[transformedPic.GetLength(0), transformedPic.GetLength(1)];

                    //Copy tranformed pic into temp pic
                    for (int i = 0; i < transformedPic.GetLength(0); i++)
                    {
                        for (int j = 0; j < transformedPic.GetLength(1); j++)
                        {
                            TempPic[i, j] = transformedPic[i, j];
                        }
                    }

                    //Resize transformed pic
                    transformedPic = new Color[TempPic.GetLength(0) / 2, TempPic.GetLength(1) / 2];

                    //Copy temp pic over to transformed pic (half the rows and half the columns)
                    //by taking every other pixel
                    for (int i = 0; i < transformedPic.GetLength(0); i++)
                    {
                        for (int j = 0; j < transformedPic.GetLength(1); j++)
                        {
                            transformedPic[i, j] = TempPic[i * 2, j * 2];
                        }
                    }

                    //Redraw
                    this.Refresh();
                }
            }
        }
        public void ChangeDisplay(Color[,] newdata, bool blocking=true)
        {
            if (newdata.GetLength(0) < 8)
            {
                throw new ArgumentException("Display Buffer must be at least 8 pixels wide.");
            }

            if (newdata.GetLength(1) != 8)
            {
                throw new ArgumentException("Display Buffer must be exactly 8 pixels high.");
            }

            // If we're currently running...
            if (m_refreshtimer != null) {
                AutoResetEvent lockhandle=null;
                // If the user has asked for blocking
                if (blocking)
                {
                    // Create an event to handle it.
                    lockhandle = new AutoResetEvent(false);
                }

                // Add the image to the queue
                lock (m_images)
                {
                    m_images.Enqueue(new MatrixQueueItem(newdata,lockhandle));
                }

                // This is not thread-safe. Make sure you don't call stop while you're waiting for this or it will hang forever!
                if (blocking)
                {
                    // and wait for the screen to finish displaying this data.
                    lockhandle.WaitOne();
                    lockhandle.Dispose();
                }
            }
            else {
                // Else, if we're not running, set this data as the initial screen and reset the offset to the left.
                m_dispbuffer = newdata;
                m_width = m_dispbuffer.GetLength(0);
                m_offset = 0;
            }
        }
Exemple #42
0
        private void mnuProcessReset_Click(object sender, EventArgs e)
        {
            transformedPic = new Color[original.GetLength(0), original.GetLength(1)];

            for (int i = 0; i < transformedPic.GetLength(0); i++)
            {
                for (int j = 0; j < transformedPic.GetLength(1); j++)
                {
                    transformedPic[i, j] = original[i, j];
                }
            }

            this.Refresh();
        }
        private void mnuProcessRotate_Click(object sender, EventArgs e)
        {
            if (transformedPic != null) //Runs only if a picture loaded
            {
                int height = transformedPic.GetLength(0); //Gets the height of the original picture
                int width = transformedPic.GetLength(1); //Gets the width of the original picture;
                Color[,] storage = new Color[width, height]; //Creates a storage that has switched dimensions

                for (int i = 0; i < storage.GetLength(0); i++) //Loops through each element in the array
                {
                    for (int j = 0; j < storage.GetLength(1); j++)
                    {
                        storage[i, j] = transformedPic[height - (j+1), i]; //Copies rotated pixel to storage
                    }
                }

                transformedPic = new Color[storage.GetLength(0), storage.GetLength(1)]; //Reinitializes transformedPic

                for (int i = 0; i < transformedPic.GetLength(0); i++) //Loops through each element in the array
                {
                    for (int j = 0; j < transformedPic.GetLength(1); j++)
                    {
                        transformedPic[i, j] = storage[i, j]; //Copies array
                    }
                }
            }
            this.Refresh(); //Re-Draws transformedPic on image
        }
Exemple #44
0
        private void mnuProcessMirrorV_Click(object sender, EventArgs e)
        {
            if (transformedPic != null)
            {
                Color[,] TempPic = new Color[transformedPic.GetLength(0), transformedPic.GetLength(1)];

                //Copy transformed pic into temp pic
                for (int i = 0; i < transformedPic.GetLength(0); i++)
                {
                    for (int j = 0; j < transformedPic.GetLength(1); j++)
                    {
                        TempPic[i, j] = transformedPic[i, j];
                    }
                }

                //Resize transformed pic
                transformedPic = new Color[TempPic.GetLength(0) * 2, TempPic.GetLength(1)];
                int Length = transformedPic.GetLength(0) - 1;

                //Copy temp pic over to transformed pic (double rows) and mirror vertical
                for (int i = 0; i < TempPic.GetLength(0); i++)
                {
                    for (int j = 0; j < TempPic.GetLength(1); j++)
                    {
                        transformedPic[i, j] = TempPic[i, j];
                        transformedPic[Length - i, j] = TempPic[i, j];
                    }
                }

                //Redraw
                this.Refresh();
            }
        }
        private void DisplayWorker(object s)
        {
            int lineposition;
            Color curcolor;

            lock (m_targetport)
            {
                if (m_targetport.IsOpen)
                {
                    // For each pixel in the frame (scanning in left to right, top to bottom order)
                    for (int y = 0; y < 8; y++)
                    {
                        // Reset our line pointer
                        lineposition = 0;

                        for (int x = 0 + m_offset; x < 8 + m_offset; x++)
                        {
                            // If we've gone off the end of the buffer but have new data to show
                            if (x >= m_width & m_newdisplay==true)
                            {
                                // get the new data
                                curcolor = m_nextdisplay[x % m_width, y];
                            }
                            else
                            {
                                // Otherwise, just loop the old data
                                curcolor = m_dispbuffer[x % m_width, y];
                            }
                            // Write the color to the buffer
                            m_linebuffer[lineposition++] = (byte) curcolor.R;
                            m_linebuffer[lineposition++] = (byte) curcolor.G;
                            m_linebuffer[lineposition++] = (byte) curcolor.B;
                        }
                        // Write the line to the port
                        m_targetport.Write(m_linebuffer, 0, 8 * 3);
                    }
                    // Write a 0 at the end to mark an end of frame
                    m_linebuffer[0] = 0xFF;
                    // Transmit the commit command to the bus.
                    m_targetport.Write(m_linebuffer, 0, 1);
                }
            }

            // Move to the next column
            m_offset++;

            // If we're at the end?
            if (m_offset >= m_width)
            {
                // Reset the offset
                m_offset = m_offset % m_width;
                if (m_newdisplay)
                {
                    // Rotate the new buffer into the old place
                    m_dispbuffer = m_nextdisplay;
                    m_width = m_dispbuffer.GetLength(0);
                    m_newdisplay = false;

                    // If the current queue item we just shifted in is waiting for a notification
                    if (m_pendingsync != null)
                    {
                        // Send the waiting thread
                        m_pendingsync.Set();
                        m_pendingsync = null;
                    }
                }
            }

            // If we don't have anything currently pending being shifted in...
            if(m_newdisplay == false) {
                lock (m_images)
                {
                    // Check if there's more out there.
                    if (m_images.Count > 0)
                    {
                        // If there is, grab it out of the queue.
                        MatrixQueueItem item;
                        item = m_images.Dequeue();
                        m_nextdisplay = item.DisplayData;
                        m_pendingsync = item.SyncHandle;
                        m_newdisplay = true;
                    }
                }
            }
        }