GetPixel() public method

Get a pixel from the image
public GetPixel ( uint x, uint y ) : System.Color
x uint X coordinate of pixel in the image
y uint Y coordinate of pixel in the image
return System.Color
Example #1
0
        /// <summary>
        /// Check if a pixel is collidable at x, y.
        /// </summary>
        /// <param name="x">The X position of the pixel to check.</param>
        /// <param name="y">The Y position of the pixel to check.</param>
        /// <returns>True if the pixel collides.</returns>
        public bool PixelAt(int x, int y)
        {
            if (x < 0 || y < 0 || x > Width || y > Height)
            {
                return(false);
            }

            if (collideImage.GetPixel((uint)x, (uint)y).A > Threshold)
            {
                return(true);
            }
            return(false);
        }
Example #2
0
    /// <summary>
    /// Processes a black and white image to determine which areas on a map can or can't be clicked
    /// </summary>
    /// <param name="path">Path to click map file</param>
    /// <param name="pathGrid">Grid object to set</param>
    /// <returns></returns>
    public static void LoadAndProcessClickMap(string path, PathNode[,] pathNodes, Grid pathGrid, int tileSize)
    {
        var map      = new SFML.Graphics.Image(path);
        var clickMap = new bool[map.Size.X, map.Size.Y];

        float totalPixelsInTile = tileSize * tileSize;
        int   totalTrue         = 0;
        int   xMax = 0;
        int   yMax = 0;

        for (int x = 0; x < pathNodes.GetLength(0); x++)
        {
            for (int y = 0; y < pathNodes.GetLength(1); y++)
            {
                totalTrue = 0;
                xMax      = tileSize * x + tileSize;

                for (int xMap = tileSize * x; xMap < xMax; xMap++)
                {
                    yMax = tileSize * y + tileSize;
                    for (int yMap = tileSize * y; yMap < yMax; yMap++)
                    {
                        if (xMap < map.Size.X && yMap < map.Size.Y && map.GetPixel((uint)xMap, (uint)yMap).B > 40)
                        {
                            totalTrue++;
                        }
                    }
                }
                pathGrid.SetTile(x, y, pathNodes[x, y].Enabled = (totalTrue / totalPixelsInTile) >= .5f);
            }
        }
    }
Example #3
0
        public uint[] CreateBitmask(Texture texture, Image image)
        {
            uint[] mask = new uint[texture.Size.X * texture.Size.Y];

            for (uint y = 0; y < texture.Size.Y; y++)
                for (uint x = 0; x < texture.Size.X; x++)
                    mask[x + y * texture.Size.X] = image.GetPixel(x, y).A;

            Bitmasks.Add(texture, mask);

            return mask;
        }
Example #4
0
    /// <summary>
    /// Processes a greyscale image to determe a scaling factor for each location on the map
    /// </summary>
    /// <param name="path">Indigo path to content</param>
    /// <param name="minScaleValue">Scaling value that represents black in the image</param>
    /// <param name="maxScaleVale">Scaling value that represents white in the image</param>
    /// <returns></returns>
    public static float[,] LoadAndProcessPerspectiveMap(string path, float minScaleValue, float maxScaleVale)
    {
        var   map            = new SFML.Graphics.Image(path);
        var   perspectiveMap = new float[map.Size.X, map.Size.Y];
        float maxValue       = maxScaleVale - minScaleValue;

        for (uint i = 0; i < map.Size.X; i++)
        {
            for (uint j = 0; j < map.Size.Y; j++)
            {
                perspectiveMap[i, j] = (map.GetPixel(i, j).B / 255f) * maxValue + minScaleValue;
            }
        }
        return(perspectiveMap);
    }
Example #5
0
        public static Bitmap BitmapFromTexture(Texture tex)
        {
            if (tex == null)
            {
                throw new ArgumentNullException("tex");
            }

            SFML.Graphics.Image im = tex.CopyToImage();
            Bitmap bitmap          = new Bitmap((int)im.Size.X, (int)im.Size.Y);

            for (uint x = 0; x < im.Size.X; x++)
            {
                for (uint y = 0; y < im.Size.Y; y++)
                {
                    SFML.Graphics.Color c = im.GetPixel(x, y);
                    bitmap.SetPixel((int)x, (int)y, System.Drawing.Color.FromArgb(c.A, c.R, c.G, c.B));
                }
            }

            im.Dispose();
            return(bitmap);
        }
        private bool IsUnderCursor(TileObject to, Vector2f cursorProjection)
        {
            if (!_layersVisibility[(int)to.Layer])
            {
                return(false);
            }
            if (to.Icon == null)
            {
                return(false);
            }
            for (int i = 0; i < to.Icon.SpritesCount; i++)
            {
                Texture texture = GetTexture(to.Icon.GetResourceId(i));
                if (texture == null)
                {
                    return(false);
                }
                Image    image      = texture.CopyToImage();
                Vector2f sfmlCursor = cursorProjection;
                Vector2f sfmlObject = WorldToSfml(to.Position, to.Offset);
                float    imageX     = sfmlCursor.X - sfmlObject.X;
                float    imageY     = sfmlCursor.Y - sfmlObject.Y;
                if (imageX < 0 || imageY < 0 || imageX >= PixelsPerUnit || imageY >= PixelsPerUnit)
                {
                    continue;
                }

                uint imageXInt = (uint)Math.Round(imageX);
                uint imageYInt = (uint)Math.Round(imageY);

                if (image.GetPixel(imageXInt, imageYInt).A > 0)
                {
                    return(true);
                }
            }

            return(false);
        }
Example #7
0
        public static void Initialize()
        {
            var style = Styles.Titlebar | Styles.Close;
            size = new Vector2f(Configuration.Width, Configuration.Height);

            Window = new RenderWindow(new VideoMode(Configuration.Width, Configuration.Height), "Open Empires", style);
            Window.SetFramerateLimit(Configuration.Framerate);
            Window.SetVerticalSyncEnabled(Configuration.VSync);

            Window.Closed += (sender, args) => Window.Close();
            Window.Resized += (sender, args) => Resize(new Vector2f(args.Width, args.Height));
            /*Window.MouseButtonPressed += (sender, args) => DispatchEvent(new MouseButtonInputArgs(args.Button, true, args.X, args.Y));
            Window.MouseButtonReleased += (sender, args) => DispatchEvent(new MouseButtonInputArgs(args.Button, false, args.X, args.Y));
            Window.MouseWheelMoved += (sender, args) => DispatchEvent(new MouseWheelInputArgs(args.Delta, args.X, args.Y));
            Window.MouseMoved += (sender, args) => DispatchEvent(new MouseMoveInputArgs(args.X, args.Y));*/

            Window.MouseMoved += new EventHandler<MouseMoveEventArgs>(Window_MouseMoved);
            Window.MouseWheelMoved += (sender, args) =>
            {
                /* Mouse wheel zoom implementation where the view will zoom
                 * towards the area the cursor is pointing to
                 */
                var mousePos = Mouse.GetPosition(Window);
                float relX = (float) mousePos.X / Window.Size.X - 0.5f;
                float relY = (float) mousePos.Y / Window.Size.Y - 0.5f;

                GameView.Move(new Vector2f {
                    X = relX * GameView.Size.X,
                    Y = relY * GameView.Size.Y
                });

                GameView.Zoom(1 - (args.Delta * 0.1f));

                GameView.Move(new Vector2f {
                    X = -relX * GameView.Size.X,
                    Y = -relY * GameView.Size.Y
                });
            };

            Window.KeyPressed += (sender, args) =>
            {
                KeyStates[(int)args.Code] = true;
            };

            Window.KeyReleased += (sender, args) =>
            {
                KeyStates[(int)args.Code] = false;
            };

            Blendomatic.ReadBlendomatic(new System.IO.FileStream("blendomatic.dat", System.IO.FileMode.Open));
            map = new Map();

            GameView = new View(DefaultView);
            GameView.Center = new Vector2f((map.Width/2)*96, 0);

            var blend = Blendomatic.BlendingModes[2];
            var tile = blend.Tiles[12];

            var img = new Image(97, 49);
            for (var y = 0; y < img.Size.Y; y++)
                for (var x = 0; x < img.Size.X; x++)
                    img.SetPixel((uint)x, (uint)y, new Color(255, 0, 0, 0));

            var i = 0;
            for (var y = 0; y < img.Size.X; y++)
            {
                var bytesPerRow = y < 24 ? 1 + (4 * y) : 97 - (4 * (y - 24));

                var startX = 48 - (bytesPerRow / 2);
                for (var x = 0; x < bytesPerRow; x++)
                    img.SetPixel((uint)(startX + x), (uint)y, new Color(0, 0, 0, tile[i++]));
            }

            var slpFile = new SLPFile();
            slpFile.LoadFile("textures/ter15002.slp");

            var spritewidth = slpFile.GetFrame(0).m_Width;
            var spriteheight = slpFile.GetFrame(0).m_Height;

            var imgg = new Image((uint)spritewidth, (uint)(spriteheight ), slpFile.GetFrame(0).GetRGBAArray());

            for (var y = 0; y < imgg.Size.Y; y++)
                for (var x = 0; x < imgg.Size.X; x++)
                {
                    var col = imgg.GetPixel((uint)x, (uint)y);
                    col.A = (byte)(Math.Min(255, (128-img.GetPixel((uint)x, (uint)y).A)*2));

                    if (img.GetPixel((uint)x, (uint)y).R == 255)
                        col.A = 0;

                    imgg.SetPixel((uint)x, (uint)y, col);
                }

            selected = new Sprite(new Texture(imgg));

            /* blend sand to forest now */

            var blend2 = Blendomatic.BlendingModes[2];
            var tile2 = blend2.Tiles[12];

            var img2 = new Image(97, 49);
            for (var y = 0; y < img2.Size.Y; y++)
                for (var x = 0; x < img2.Size.X; x++)
                    img2.SetPixel((uint)x, (uint)y, new Color(255, 0, 0, 0));

            var i2 = 0;
            for (var y = 0; y < img.Size.X; y++)
            {
                var bytesPerRow = y < 24 ? 1 + (4 * y) : 97 - (4 * (y - 24));

                var startX = 48 - (bytesPerRow / 2);
                for (var x = 0; x < bytesPerRow; x++)
                    img2.SetPixel((uint)(startX + x), (uint)y, new Color(0, 0, 0, tile[i2++]));
            }

            var slpFile2 = new SLPFile();
            slpFile2.LoadFile("textures/ter15017.slp");

            var spritewidth2 = slpFile2.GetFrame(0).m_Width;
            var spriteheight2 = slpFile2.GetFrame(0).m_Height;

            var imgg2 = new Image((uint)spritewidth2, (uint)(spriteheight2), slpFile2.GetFrame(0).GetRGBAArray());

            for (var y = 0; y < imgg2.Size.Y; y++)
                for (var x = 0; x < imgg2.Size.X; x++)
                {
                    var col = imgg2.GetPixel((uint)x, (uint)y);
                    col.A = (byte)(Math.Min(255, (128 - img.GetPixel((uint)x, (uint)y).A) * 2));

                    if (img2.GetPixel((uint)x, (uint)y).R == 255)
                        col.A = 0;

                    imgg2.SetPixel((uint)x, (uint)y, col);
                }

            selected2 = new Sprite(new Texture(imgg2));

            EntManager = new EntityManager();
        }
Example #8
0
        static void Main(string[] args)
        {
            var palette = new Image("palette.png");
            palVec = new Vector3f[256];

            for (uint x = 0; x < palVec.Length; x++)
            {
                palVec[x] = (Vector3f)palette.GetPixel(x, 0);
            }

            var image = new Image("test.png");
            var imgVec = new Vector3f[image.Size.X, image.Size.Y];
            var imgRes = new byte[image.Size.X, image.Size.Y];

            for (uint y = 0; y < image.Size.Y; y++)
            {
                for (uint x = 0; x < image.Size.X; x++)
                {
                    imgVec[x, y] = (Vector3f)image.GetPixel(x, y);
                }
            }

            for (uint y = 0; y < image.Size.Y; y++)
            {
                for (uint x = 0; x < image.Size.X; x++)
                {
                    if (x == 0 || y == 0 || x == image.Size.X - 1 || y == image.Size.Y - 1)
                    {
                        imgRes[x, y] = FindClosestColor(imgVec[x, y]);
                        continue;
                    }

                    var oldPix = imgVec[x, y];
                    var newCol = FindClosestColor(oldPix);
                    var newPix = palVec[newCol];
                    var error = oldPix - newPix;

                    imgRes[x, y] = newCol;

                    imgVec[x + 1, y + 0] += error * (7.0f / 16.0f);
                    imgVec[x - 1, y + 1] += error * (3.0f / 16.0f);
                    imgVec[x + 0, y + 1] += error * (5.0f / 16.0f);
                    imgVec[x + 1, y + 1] += error * (1.0f / 16.0f);
                }
            }

            for (uint y = 0; y < image.Size.Y; y++)
            {
                for (uint x = 0; x < image.Size.X; x++)
                {
                    image.SetPixel(x, y, (Color)palVec[imgRes[x, y]]);
                }
            }

            image.SaveToFile("out.png");

            var o = File.OpenWrite("out.pic");
            for (uint y = 0; y < image.Size.Y; y++)
            {
                for (uint x = 0; x < image.Size.X; x++)
                {
                    o.WriteByte(imgRes[x, y]);
                }
            }
            o.Dispose();
        }