Example #1
0
    /// <summary>
    /// Erode an image using a k by k box kernel
    /// </summary>
    /// <param name="i">input image</param>
    /// <param name="k">kernel thickness</param>
    /// <returns>modified image</returns>
    public Color[,] Erosion(Color[,] i, int k)
    {
        //set source image to grayscale
        i = PointProcessing.Instance.Treshold(i, 0.5f);

        Color[,] result = new Color[i.GetLength(0), i.GetLength(1)];

        for (int w = 0 + k; w < i.GetLength(0) - k; w++)
        {
            for (int h = 0 + k; h < i.GetLength(1) - k; h++)
            {
                float sum = 0f;
                for (int j = -k / 2; j <= +k / 2; j++)
                {
                    for (int l = -k / 2; l <= +k / 2; l++)
                    {
                        sum += i[w + j, h + l].r;
                    }
                }

                float res = sum < k*k ? 0f : 1f;
                result[w, h].r = res;
                result[w, h].g = res;
                result[w, h].b = res;
            }
        }
        return result;
    }
Example #2
0
        /// <summary>
        /// Encodes the specified image data to png.
        /// </summary>
        /// <param name="pixels">
        /// The pixel data (bottom line first).
        /// </param>
        /// <param name="dpi">
        /// The image resolution in dots per inch.
        /// </param>
        /// <returns>
        /// The png image data.
        /// </returns>
        public static byte[] Encode(Color[,] pixels, int dpi = 96)
        {
            int height = pixels.GetLength(0);
            int width = pixels.GetLength(1);
            var bytes = new byte[(width * height * 4) + height];

            int k = 0;
            for (int i = height - 1; i >= 0; i--)
            {
                bytes[k++] = 0; // Filter
                for (int j = 0; j < width; j++)
                {
                    bytes[k++] = pixels[i, j].R;
                    bytes[k++] = pixels[i, j].G;
                    bytes[k++] = pixels[i, j].B;
                    bytes[k++] = pixels[i, j].A;
                }
            }

            var w = new MemoryWriter();
            w.Write((byte)0x89);
            w.Write("PNG\r\n\x1a\n".ToCharArray());
            WriteChunk(w, "IHDR", CreateHeaderData(width, height));
            WriteChunk(w, "pHYs", CreatePhysicalDimensionsData(dpi, dpi));
            WriteChunk(w, "IDAT", CreateUncompressedBlocks(bytes));
            WriteChunk(w, "IEND", new byte[0]);
            return w.ToArray();
        }
Example #3
0
 static void DisplayArray(Color[,] array)
 {
     //Visit each item
     for (int row = 0; row < array.GetLength(0); row++)
         for (int col = 0; col < array.GetLength(1); col++)
             canvas.SetBBScaledPixel(col, row, array[row, col]);
 }
Example #4
0
        public Color[,] RenderEffect(Color[,] buffer, Color[] palette, int eventToRender)
        {
            var bufferHeight = buffer.GetLength(Utils.IndexRowsOrHeight);
            var bufferWidth = buffer.GetLength(Utils.IndexColsOrWidth);
            var random = new Random(chkBoxStrobe.Checked ? DateTime.Now.Millisecond : 2271965);
            var steps = tbSteps.Value;
            var stepsHalf = steps / 2.0f;
            var lights = Convert.ToInt32((bufferHeight * bufferWidth) * (tbLightCount.Value / 100.0));
            var step = Math.Max(1, (bufferHeight * bufferWidth) / lights);

            for (var y = 0; y < bufferHeight; y++) {
                for (var x = 0; x < bufferWidth; x++) {
                    if ((y * bufferHeight + x + 1) % step != 1 && step != 1) {
                        continue;
                    }

                    var hsv = palette[random.Next() % palette.Length].ToHSV();

                    var randomStep = (eventToRender + random.Next()) % steps;

                    hsv.Value = chkBoxStrobe.Checked ? ((randomStep == (int)(stepsHalf + 0.5f)) ? 1.0f : 0.0f) :
                        Math.Max(0.0f, ((randomStep <= stepsHalf ? randomStep : steps - randomStep) / stepsHalf));

                    buffer[y, x] = hsv.ToColor();
                }
            }
            return buffer;
        }
Example #5
0
        public Color[,] RenderEffect(Color[,] buffer, Color[] palette, int eventToRender)
        {
            _palette = palette;
            _bufferHeight = buffer.GetLength(0);
            _bufferWidth = buffer.GetLength(1);

            if (eventToRender == 0 || _tempBuf == null) {
                InitializeSnowflakes();
            }

            // move snowflakes
            for (var x = 0; x < _bufferWidth; x++) {
                var newX = (x + eventToRender / 20) % _bufferWidth; // CW
                var newX2 = (x - eventToRender / 20) % _bufferWidth; // CCW
                if (newX2 < 0) {
                    newX2 += _bufferWidth;
                }
                for (var y = 0; y < _bufferHeight; y++) {
                    var newY = (y + eventToRender / 10) % _bufferHeight;
                    var newY2 = (newY + _bufferHeight / 2) % _bufferHeight;
                    var color1 = GetTempPixel(newX, newY);
                    if (color1 == Color.Transparent) {
                        color1 = GetTempPixel(newX2, newY2);
                    }
                    buffer[y,x] = color1;
                }
            }
            return buffer;
        }
Example #6
0
	public override Texture2D CreateSprite (Texture2D texture)
	{
		Color[] pixels = texture.GetPixels ();
		Color[,] pixels2D = new Color[texture.width, texture.height];
		for (int i = 0; i < pixels.Length; i++) {
			
			int x = i % texture.width;
			int y = i / texture.height;

			pixels2D [x, y] = pixels [i];
		}

		int amountOfEyes = (int)(sightAngle / 45);
		for (int i = 0; i < amountOfEyes; i++) {
			
			int radius = (texture.width - eyeSize) / 2;
			float xPos = Mathf.Sin (Mathf.Deg2Rad * (i * 45));
			float yPos = Mathf.Cos (Mathf.Deg2Rad * (i * 45));
			Vector2 pos = new Vector2 (xPos, yPos) * radius + new Vector2 (texture.width - eyeSize, texture.height - eyeSize) / 2;

			ApplyKernel (ref pixels2D, texture.width, texture.height, pos);
		}

		for (int x = 0; x < pixels2D.GetLength (0); x++) {
			for (int y = 0; y < pixels2D.GetLength (1); y++) {
				
				pixels [x + y * texture.width] = pixels2D [x, y];
			}
		}

		texture.SetPixels (pixels);
		texture.Apply ();

		return texture;
	}
 public static bool TexturesCollide(Color[,] tex1, Matrix mat1, Color[,] tex2, Matrix mat2)
 {
     int width1 = tex1.GetLength(0);
     int height1 = tex1.GetLength(1);
     int width2 = tex2.GetLength(0);
     int height2 = tex2.GetLength(1);
     Matrix mat1to2 = mat1 * Matrix.Invert(mat2);
     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)) continue;
             if ((y2 < 0) || (y2 >= height2)) continue;
             if (tex1[x1, y1].A <= 0)
             {
                 var tmp = 1 + 1;
                 var other = tmp + 1;
                 continue;
             }
             if (tex2[x2, y2].A <= 0) continue;
     //                    Vector2 screenPos = Vector2.Transform(pos1, mat1);
     //                    return screenPos;
             return true;
         }
     }
     //            return new Vector2(-1, -1);
     return false;
 }
Example #8
0
        public override Color[,] apply(Color[,] imageToProcess, MainViewModel reportProgressTo)
        {
            imageToProcess = base.apply(imageToProcess, reportProgressTo);

            for (int i = 0; i < imageToProcess.GetLength(0); i++)
            {
                int firstPixelY = -1;
                for (int j = 0; j < imageToProcess.GetLength(1); j++)
                {
                    if (imageToProcess[i, j].ToArgb() == Color.Black.ToArgb())
                        if (firstPixelY <= 0 || firstPixelY + 1 == j)
                            firstPixelY = j;
                        else
                        {
                            for (int k = firstPixelY + 1; k < j; k++)
                                imageToProcess[i, k] = Color.Black;

                            firstPixelY = -1;
                        }

                    reportProgressTo.Progress++;
                }
            }

            return imageToProcess;
        }
Example #9
0
        public Color[,] RenderEffect(Color[,] buffer, Color[] palette, int eventToRender)
        {
            const int speedFactor = 200;
            var bufferHeight = buffer.GetLength(Utils.IndexRowsOrHeight);
            var bufferWidth = buffer.GetLength(Utils.IndexColsOrWidth);

            Color color;
            var hsv2 = new HSV();
            var colorcnt = palette.Length;
            var cycleLen = colorcnt * speedFactor;
            var count = tbCount.Value;
            if (eventToRender > (colorcnt - 1) * speedFactor * count && count < 10) {
                color = palette.GetMultiColorBlend(count % 2, false);
            } else {
                color = palette.GetMultiColorBlend(eventToRender % cycleLen / (double)cycleLen, true);
            }
            var hsv = color.ToHSV();
            var halfHeight = (bufferHeight - 1) / 2.0;
            var halfWidth = (bufferWidth - 1) / 2.0;
            for (var col = 0; col < bufferWidth; col++) {
                for (var row = 0; row < bufferHeight; row++) {
                    hsv2.SetToHSV(hsv);
                    if (chkBoxHFade.Checked) hsv2.Value *= (float)(1.0 - Math.Abs(halfWidth - col) / halfWidth);
                    if (chkBoxVFade.Checked) hsv2.Value *= (float)(1.0 - Math.Abs(halfHeight - row) / halfHeight);
                    buffer[row, col] = hsv2.ToColor();
                }
            }
            return buffer;
        }
Example #10
0
        public Color[,] RenderEffect(Color[,] buffer, Color[] palette, int eventToRender)
        {
            var colorCount = palette.Length;
            var spiralCount = colorCount * tbPaletteRepeat.Value;
            var bufferHeight = buffer.GetLength(Utils.IndexRowsOrHeight);
            var bufferWidth = buffer.GetLength(Utils.IndexColsOrWidth);
            var deltaStrands = bufferWidth / spiralCount;
            var spiralThickness = (deltaStrands * tbThickness.Value / 100) + 1;
            long spiralState = eventToRender * tbDirection.Value;

            for (var spiral = 0; spiral < spiralCount; spiral++) {
                var strandBase = spiral * deltaStrands;
                var color = palette[spiral % colorCount];
                for (var thickness = 0; thickness < spiralThickness; thickness++) {
                    var strand = (strandBase + thickness) % bufferWidth;
                    for (var row = 0; row < bufferHeight; row++) {
                        var column = (strand + ((int)spiralState / 10) + (row * tbRotations.Value / bufferHeight)) % bufferWidth;
                        if (column < 0) {
                            column += bufferWidth;
                        }
                        if (chkBoxBlend.Checked) {
                            color = palette.GetMultiColorBlend((bufferHeight - row - 1) / (double)bufferHeight, false);
                        }
                        if (chkBox3D.Checked) {
                            var hsv = color.ToHSV();
                            hsv.Value = (float)((double)(tbRotations.Value < 0 ? thickness + 1 : spiralThickness - thickness) / spiralThickness);
                            color = hsv.ToColor();
                        }
                        buffer[row, column] = color;
                    }
                }
            }
            return buffer;
        }
        public Bitmap Create(Color[,] data)
        {
            int width = data.GetLength(0);
            int height = data.GetLength(1);

            Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
            BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, bitmap.PixelFormat);

            int stride = bmData.Stride;

            byte[] array = new byte[stride * height];

            int index = 0;

            for (int y = height - 1; y >= 0; y--)
            {
                for (int x = 0; x < width; x++)
                {
                    array[index++] = data[x, y].B;
                    array[index++] = data[x, y].G;
                    array[index++] = data[x, y].R;
                }
            }

            IntPtr scan0 = bmData.Scan0;
            Marshal.Copy(array, 0, scan0, stride * bitmap.Height);
            bitmap.UnlockBits(bmData);

            return bitmap;
        }
        public static Boolean TexturesCollide(Color[,] texture1, Matrix matrix1, Color[,] texture2, Matrix matrix2)
        {
            Matrix mat1to2 = matrix1 * Matrix.Invert(matrix2);
            Int32 width1 = texture1.GetLength(0);
            Int32 height1 = texture1.GetLength(1);
            Int32 width2 = texture2.GetLength(0);
            Int32 height2 = texture2.GetLength(1);

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

                    Int32 x2 = (Int32)pos2.X;
                    Int32 y2 = (Int32)pos2.Y;
                    if ((x2 >= 0) && (x2 < width2)) {
                        if ((y2 >= 0) && (y2 < height2)) {
                            if (texture1[x1, y1].A > 0) {
                                if (texture2[x2, y2].A > 0) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }

            return false;
        }
Example #13
0
        public Color[,] RenderEffect(Color[,] buffer, Color[] palette, int eventToRender)
        {
            var bufferHeight = buffer.GetLength(Utils.IndexRowsOrHeight);
            var bufferWidth = buffer.GetLength(Utils.IndexColsOrWidth);
            var colorcnt = palette.Length;

            var halfWidth = Math.Max(1, bufferWidth / 2);
            var halfHeight = Math.Max(1, bufferHeight / 2);
            var outterR = (float) (halfWidth * (tbOuterR.Value / 100.0));
            var innerR = (float) (halfWidth * (tbInnerR.Value / 100.0));
            if (innerR > outterR) innerR = outterR;
            var distance = (float) (halfWidth * (tbDistance.Value / 100.0));

            var mod1440 = eventToRender % 1440;
            var originalDistance = distance;
            for (var i = 1; i <= 360; i++) {
                if (chkBoxAnimate.Checked) distance = (int) (originalDistance + eventToRender / 2.0) % 100;
                var t = (float) ((i + mod1440) * Math.PI / 180);
                var x = Convert.ToInt32((outterR - innerR) * Math.Cos(t) + distance * Math.Cos(((outterR - innerR) / innerR) * t) + halfWidth);
                var y = Convert.ToInt32((outterR - innerR) * Math.Sin(t) + distance * Math.Sin(((outterR - innerR) / innerR) * t) + halfHeight);
                var x2 = Math.Pow((x - halfWidth), 2);
                var y2 = Math.Pow((y - halfHeight), 2);
                var hyp = (Math.Sqrt(x2 + y2) / bufferWidth) * 100.0;

                if (x >= 0 && x < bufferWidth && y >= 0 && y < bufferHeight) {
                    buffer[y, x] = palette[(int)(hyp / (colorcnt > 0 ? bufferWidth / colorcnt : 1)) % colorcnt];
                }
            }
            return buffer;
        }
Example #14
0
        public Color interpolation(Color[,] original_Buffer, PointF p)
        {
            int x1 = (int)Math.Floor(p.X),
                y1 = (int)Math.Floor(p.Y),
                x2 = x1 + 1,
                y2 = y1 + 1;

            x1 = OutBoundery(x1, original_Buffer.GetLength(0));
            x2 = OutBoundery(x2, original_Buffer.GetLength(0));
            y1 = OutBoundery(y1, original_Buffer.GetLength(1));
            y2 = OutBoundery(y2, original_Buffer.GetLength(1));

            Color p1 = original_Buffer[x1, y1];
            Color p2 = original_Buffer[x2, y1];
            Color p3 = original_Buffer[x1, y2];
            Color p4 = original_Buffer[x2, y2];

            double Xfraction = p.X - x1;
            double Yfraction = p.Y - y1;

            //interpolate in X_Direction
            Data dataX1, dataX2, dataFinal;
            dataX1 = interpolate(p1, p2, Xfraction);
            dataX2 = interpolate(p3, p4, Xfraction);
            //interpolate in Y_Direction
            Color newX1 = Color.FromArgb((int)dataX1.R_Val, (int)dataX1.G_Val, (int)dataX1.B_Val);
            Color newX2 = Color.FromArgb((int)dataX2.R_Val, (int)dataX2.G_Val, (int)dataX2.B_Val);
            dataFinal = interpolate(newX1, newX2, Yfraction);

            return Color.FromArgb((int)(dataFinal.R_Val), (int)(dataFinal.G_Val), (int)(dataFinal.B_Val));
        }
 public static int[,] ConvertToGreyscale(Color[,] colors, decimal redWeight, decimal greenWeight, decimal blueWeight)
 {
     int[,] grey = new int[colors.GetLength(0), colors.GetLength(1)];
     for (int x = 0; x < colors.GetLength(0); x++)
         for (int y = 0; y < colors.GetLength(1); y++)
             grey[x, y] = (int)(redWeight * colors[x, y].R + greenWeight * colors[x, y].G + blueWeight * colors[x, y].B);
     return grey;
 }
Example #16
0
        public Color[,] RenderEffect(Color[,] buffer, Color[] palette, int eventToRender)
        {
            var h = 0.0;
            var bufferHeight = buffer.GetLength(Utils.IndexRowsOrHeight);
            var bufferWidth = buffer.GetLength(Utils.IndexColsOrWidth);
            var hsv = new HSV();
            var maxframe = bufferHeight*2;
            var frame = (int) ((bufferHeight*(double) eventToRender/200.0)%maxframe);
            var offset = eventToRender/100.0;
            for (var col = 0; col < bufferWidth; col++) {
                for (var row = 0; row < bufferHeight; row++) {
                    double x1;
                    double y1;
                    double f;
                    switch (tbStyle.Value) {
                        case 1:
                            var n =
                                Math.Abs((col*col - row*row)*
                                         Math.Sin(offset + ((col + row)*Pi2/(bufferHeight + bufferWidth))));
                            var d = col*col + row*row + 1;
                            h = n/d;
                            break;
                        case 2:
                            f = (frame < maxframe/2) ? frame + 1 : maxframe - frame;
                            x1 = (col - bufferWidth/2.0)/f;
                            y1 = (row - bufferHeight/2.0)/f;
                            h = Math.Sqrt(x1*x1 + y1*y1);
                            break;
                        case 3:
                            f = (frame < maxframe/2) ? frame + 1 : maxframe - frame;
                            f = f*0.1 + bufferHeight/60.0;
                            x1 = (col - bufferWidth/2.0)/f;
                            y1 = (row - bufferHeight/2.0)/f;
                            h = Math.Sin(x1)*Math.Cos(y1);
                            break;

                    }
                    hsv.Saturation = 1.0f;
                    hsv.Value = 1.0f;
                    var chunks = tbChunks.Value;
                    var skip = tbSkip.Value;
                    if (chunks > 1 && ((int) (h*chunks))%skip == 0) {
                        continue;
                    }
                    Color color;
                    if (cbColors.SelectedIndex == 0) {
                        hsv.Hue = (float) h;
                        color = hsv.ToColor();
                    }
                    else {
                        color = palette.GetMultiColorBlend(h, false);
                    }

                    buffer[row, col] = color;
                }
            }
            return buffer;
        }
Example #17
0
        public static Color[,] applyFilter(Color[,] screen)
        {
            // search the screen for the sample
            int screen_rows = screen.GetLength(0);
            int screen_colums = screen.GetLength(1);

            int k = 0;
            int l = 0;

            Color[,] subImage = new Color[screen_rows, screen_colums];

            // Vi itererer over vores sample
            while (k < (screen_rows))
            {
                while (l < (screen_colums))
                {
                    // COPY!
                    /*int filter = int.Parse("" + screen[i, j].R + screen[i, j].G + screen[i, j].B);
                    if (filter <= 000)
                    {*/
                    Color c = screen[k, l];
                    if ((int)c.R > 200 || (int)c.G > 200 || (int)c.B > 200)
                    {
                    }
                    else
                    {
                        int grayScale = (int)((screen[k, l].R * .3) + (screen[k, l].G * .59) + (screen[k, l].B * .11));
                        Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);
                        subImage[k, l] = newColor;
                        //subImage[k, l] = screen[i, j];
                    }
                    /*}*/

                    if (k == screen_rows - 1 && l == screen_colums - 1)
                    {
                        k = screen_rows + 1; // Grimt grimt hack, men vi sparer en goto
                        break;
                    }
                    else if (l == screen_colums - 1)
                    {
                        // Hvis vi er for "enden" så reset, og hop til næste row
                        k++;
                        l = 0;

                        // Row jump på screenen også
                    }
                    else
                    {
                        // Hop en enkelt column både på screen og på sample
                        l++;
                    }
                }
            }

            return subImage;
        }
        public static Bitmap CreateImage(Color[,] values)
        {
            Bitmap result = new Bitmap(values.GetLength(0), values.GetLength(1));

            for (int x = 0; x < result.Width; x++)
                for (int y = 0; y < result.Height; y++)
                    result.SetPixel(x, y, values[x, y]);

            return result;
        }
Example #19
0
        public override Color[,] Apply(Color[,] image, int minThreshold = 1)
        {
            int width = image.GetLength(0);
            int height = image.GetLength(1);

            int kernelX = this.Size;
            int kernelY = this.Size;
            int kernelOffsetX = (kernelX - 1) / 2;
            int kernelOffsetY = (kernelY - 1) / 2;

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

            // loop over image coordinates
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int localMaximum = 0;
                    if (image[x, y].R < minThreshold) continue;

                    // loop over neighbourhood pixels
                    for (int kx = -kernelOffsetX; kx <= kernelOffsetX; kx++)
                    {
                        int offsetX = x + kx;
                        for (int ky = -kernelOffsetY; ky <= kernelOffsetY; ky++)
                        {
                            int offsetY = y + ky;
                            // if not out of bounds
                            if (!(
                                offsetX < 0
                                || offsetX >= width
                                || offsetY < 0
                                || offsetY >= height
                                ))
                            {
                                //find maximum value
                                int val = image[offsetX, offsetY].R;
                                if (val > localMaximum) localMaximum = val;
                            }
                        }
                    }
                    //if the pixel is the highest in the neighbourhood, keep it on.
                    if (image[x, y].R == localMaximum)
                    {
                        result[x, y] = Color.FromArgb(localMaximum, localMaximum, localMaximum);
                    }
                    // otherwise turn the pixel off
                    else
                    {
                        result[x, y] = Color.Black;
                    }
                }
            }
            return result;
        }
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="symbol">The unicode character.</param>
		/// <param name="pixels">The pixels array. Must not be larger than 8x8 pixels.</param>
		/// <param name="transparencyColor">The color that represents transparency.</param>
		public MultiColorCharacter(char symbol, Color[,] pixels, Color? transparencyColor = null)
			: base(symbol, pixels.GetLength(0))
		{
			if (pixels.GetLength(1) > 8)
			{
				throw new ArgumentException("The pixels array must not be taller than 8 pixels!");
			}

			Pixels = pixels;
			TransparencyColor = transparencyColor;
		}
        public Texture2D Array2DToTexture(Color[,] array2D)
        {
            Color[] colors1D = new Color[array2D.GetLength(0) * array2D.GetLength(1)];
            for (int x = 0; x < array2D.GetLength(0); x++)
                for (int y = 0; y < array2D.GetLength(1); y++)
                    colors1D[x + y * array2D.GetLength(0)] = array2D[x, y];

            Texture2D texture = new Texture2D(graphics.GraphicsDevice, array2D.GetLength(0), array2D.GetLength(1));
            texture.SetData<Color>(colors1D);
            return texture;
        }
	public Color[,] Render() {
		Color[,] c = new Color[BlockEditor.textureSize, BlockEditor.textureSize];

		for (int x = 0; x < c.GetLength(0); x++) {
			for (int y = 0; y < c.GetLength(1); y++) {
				c[x, y] = new Color(heightMap[x, y], heightMap[x, y], heightMap[x, y]);
			}
		}

		return c;
	}
Example #23
0
        public Color[,] ApplyMedian(Color[,] image)
        {
            int width = image.GetLength(0);
            int height = image.GetLength(1);
            Color[,] result = new Color[width, height];

            int kernelX = this.matrix.GetLength(0);
            int kernelY = this.matrix.GetLength(1);
            int kernelOffsetX = (kernelX - 1) / 2;
            int kernelOffsetY = (kernelY - 1) / 2;

            int offsetX, offsetY;
            int neighbourhoodSize = kernelX*kernelY;
            int[] neighbourhoodValues = new int[neighbourhoodSize];

            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    for (int kx = -kernelOffsetX; kx <= kernelOffsetX; kx++)
                    {
                        offsetX = x + kx;
                        for (int ky = -kernelOffsetY; ky <= kernelOffsetY; ky++)
                        {
                            offsetY = y + ky;

                            int row = ky + kernelOffsetY;
                            int column = kx + kernelOffsetX;
                            if (
                                offsetX < 0
                                || offsetX >= width
                                || offsetY < 0
                                || offsetY >= height
                                )
                            {

                                neighbourhoodValues[kernelX * row + column] = this.padding;
                            }
                            else
                            {
                                neighbourhoodValues[kernelX * row + column] = image[offsetX, offsetY].R;
                            }
                        }
                    }
                    Array.Sort(neighbourhoodValues);
                    int median = neighbourhoodValues[(neighbourhoodSize + 1)/2];
                    result[x, y] = Color.FromArgb(
                        median, median, median
                    );
                }
            }

            return result;
        }
 private static Bitmap CreateBitmapFromColors(Color[,] image)
 {
   Bitmap bitmap = new Bitmap(image.GetLength(0), image.GetLength(1));
   for (int i = 0; i < image.GetLength(0); i++)
   {
     for (int j = 0; j < image.GetLength(1); j++)
     {
       bitmap.SetPixel(i, j, image[i, j]);
     }
   }
   return bitmap;
 }
Example #25
0
		public SpriteMap(Color[,] pixels)
		{
			if (((pixels.GetLength(0) % 8) != 0) || ((pixels.GetLength(1) % 8) != 0))
			{
				throw new ArgumentException("Expecting an image with sides of mutiple of 8 pixels");
			}

			Pixels = pixels;

			SpriteCountHorizontal = Pixels.GetLength(0) / 8;
			SpriteCountVertical = Pixels.GetLength(1) / 8;
		}
Example #26
0
        public Color[,] LinearFilter(Color[,] input, double[,] Mask, int originX, int originY, string Postprocessing)
        {
            int width = input.GetLength(0);
            int height = input.GetLength(1);
            int mask_width = Mask.GetLength(0);
            int mask_height = Mask.GetLength(1);

            Color[,] temp = Padding(input, Mask, originX, originY);
            Color[,] result = new Color[width, height];
            Data[,] data = new Data[width, height];
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Data sum;
                    sum.R_Val = sum.G_Val = sum.B_Val = 0;
                    for (int k = 0; k < mask_height; k++)
                    {
                        for (int m = 0; m < mask_width; m++)
                        {
                            sum.R_Val += Mask[m, k] * temp[i + m, j + k].R;
                            sum.G_Val += Mask[m, k] * temp[i + m, j + k].G;
                            sum.B_Val += Mask[m, k] * temp[i + m, j + k].B;
                        }
                    }
                    if (Postprocessing == "Cutoff")
                    {
                        sum.R_Val = pixel.cutOff(sum.R_Val);
                        sum.G_Val = pixel.cutOff(sum.G_Val);
                        sum.B_Val = pixel.cutOff(sum.B_Val);
                        result[i, j] = Color.FromArgb((int)sum.R_Val, (int)sum.G_Val, (int)sum.B_Val);

                    }
                    else if (Postprocessing == "Absolute")
                    {

                        sum.R_Val = Math.Abs(sum.R_Val);
                        sum.G_Val = Math.Abs(sum.G_Val);
                        sum.B_Val = Math.Abs(sum.B_Val);
                        data[i, j] = sum;
                    }
                    else if (Postprocessing == "None")
                    {
                        result[i, j] = Color.FromArgb((int)sum.R_Val, (int)sum.G_Val, (int)sum.B_Val);
                    }
                }
            }
            if (Postprocessing == "Absolute")
            {
                result = pixel.Contrast(0, 255, data);
            }
            return result;
        }
	Lab[,] RgbArrayToLabArray(Color[,] RgbArray)
	{
		Lab[,] rawLabArray= new Lab[RgbArray.GetLength(0), RgbArray.GetLength(1)];
		for (int i = 0; i < RgbArray.GetLength (0); i++) 
		{
			for (int j = 0; j < RgbArray.GetLength (1); j++) 
			{
				rawLabArray[i,j] = ColorUtil.ConvertRGBtoLAB (RgbArray[i,j]);
			}
		}
		return rawLabArray;
	}
Example #28
0
 /**
  * Konstruktor
  */
 public BoardEventArgs(Color[,] boardData)
 {
     Color[,] newBoardData = new Color[boardData.GetLength(0), boardData.GetLength(1)];
     for (int i = 0; i < boardData.GetLength(1); i++)
     {
         for (int j = 0; j < boardData.GetLength(0); j++)
         {
             newBoardData[j, i] = boardData[j, i];
         }
     }
     removedLines = new List<int>();
     this.boardData = newBoardData;
 }
Example #29
0
    /// <summary>
    /// Sets the grid texture.
    /// </summary>
    /// <returns><c>true</c>, if grid texture was set, <c>false</c> otherwise.</returns>
    /// <param name="grid">X and Y array of colors</param>
    public bool SetGridTexture(Color[,] grid)
    {
        //first we make simple tests, to make sure our caller gets the correct feedback from this method
        if (grid == null) return false;
        if (grid.GetLength(0) == 0) return false;
        if (grid.GetLength(1) == 0) return false;
        if (texture == null) return false;

        //we have to make sure the grid is compatible with the texture.
        //if they are not compatible, I can just create a new texture with matching sizes.
        if ((grid.GetLength(0) != texture.width) || (grid.GetLength(1) != texture.height)) {
            texture = new Texture2D(grid.GetLength(0),grid.GetLength(1));
            Debug.Log("New texture generated'" + grid.GetLength(0) + "," + grid.GetLength(1));
            //This is required, since a new instance of Texture2D has been created on it.
            guiTexture.texture = texture;
        }

        for (int x = 0; x < texture.width; x++) {
            for (int y = 0; y < texture.height; y++) {
                texture.SetPixel(x, y, grid[x,y]);
            }
        }
        //Screen.SetResolution(texture.width, texture.height, false);
        texture.Apply ();
        return true;
    }
Example #30
0
    /// <summary>
    /// Draws the grid.
    /// </summary>
    /// <param name="lines">Lines. how many lines you want in the grid</param>
    /// <param name="columns">Columns. how many columns you want in the grid</param>
    /// <param name="gridLocal">Grid local. The grid being changed.</param>
    /// <param name="borderColor">Border color. Im using Unity Color class, basic RGBA style.</param>
    public void DrawGrid(int lines,int columns,Color[,] gridLocal, Color borderColor)
    {
        //I`m going to draw the grid with 4 fors, skipping the grid space to save processing.
        //This method can only be used if the background is clear. otherwize it will keep garbage.
        //Another way of doing it could be to use 2 fors and if statements inside. It really depends on what we want to do.
        //in this drawing method I am cleaning the background each time it changes.
        //grid.gridArray = new Color[grid.GridWidth+1, grid.GridHeight+1];
        for (int x = 0; x < gridLocal.GetLength(0); x++) {
            for (int y = 0; y < gridLocal.GetLength(1); y++) {
                gridLocal[x,y] = grid.backgroundColor;
                //Debug.Log(gridLocal[x,y]);
            }
        }

        //Getting all first pixels positions of all cells
        grid.gridPositions = new Vector2[grid.GridColumns, grid.GridLines];
        float rectWidth = (gridLocal.GetLength(0)/columns);
        float rectHeigh = (gridLocal.GetLength (1) / lines);
        for (int x=0; x < grid.GridColumns; x++) {
            for(int y=0; y < grid.GridLines;y++){
                grid.gridPositions[x,y] = new Vector2(x*rectWidth,y*rectHeigh);
            }
        }

        //then I paint the lines, pixel by pixel.
        for (int x = 0; x < gridLocal.GetLength(0) ; x+=(gridLocal.GetLength(0)/(columns))) {
            for (int y = 0; y < gridLocal.GetLength(1) ; y++) {
                gridLocal[x,y] = borderColor;
                gridLocal[gridLocal.GetLength(0)-1,y] = borderColor;
            }
        }
        for (int x = 0; x < gridLocal.GetLength(0); x++) {
            for (int y = 0; y < gridLocal.GetLength(1); y+=(gridLocal.GetLength(1)/lines)) {
                gridLocal [x, y] = borderColor;
                gridLocal[x,gridLocal.GetLength(1)-1] = borderColor;
            }
        }

        //then I paint the user selected points
        Vector2 linePoint = new Vector2 (8, 15);
        Vector2 endLinePoint = new Vector2 (60, 104);
        for (float t=0; t<1; t+=(0.001f)) {
            linePoint = CalculatePointOfLine(t,pointA,pointB);
            gridLocal[(int)linePoint.x, (int)linePoint.y] = Color.blue;
        }

        //then I paint the line between selected points
        //I am using a Bresenham algorithm to calculate the cells
        gridGeneratedPointsText = ""; //setting up string to be filled with grid positions
        int sizeW = (int)((gridLocal.GetLength(0)/(columns)));
        int sizeH = (int)((gridLocal.GetLength(1)/(lines)));
        for (int i=1; i<= grid.thickness; i++) {
            Vector2 tempThickA = new Vector2(pointA.x + (i*sizeW),pointA.y+(i*sizeH));
            Vector2 tempThickB = new Vector2(pointB.x + (i*sizeW),pointB.y+(i*sizeH));
            gridLocal = lineBresenham (tempThickA, tempThickB, gridLocal);
        }

        //sends information to renderer
        gridRenderer.SetGridTexture(grid.gridArray);
    }