CopyToImage() 공개 메소드

Copy a texture's pixels to an image
public CopyToImage ( ) : Image
리턴 Image
예제 #1
0
        /// <summary>
        /// Draw the surface directly to the game window.  This will refresh the view,
        /// and Display the surface, as well as clear it if AutoClear is true.
        /// </summary>
        /// <param name="game">The Game to render to.</param>
        public void DrawToWindow(Game game)
        {
            RefreshView();

            Display();

            Drawable drawable = RenderShaders();

            game.Window.Draw(drawable, _states);

            if (_saveNextFrame)
            {
                _saveNextFrame = false;

                var texture = new SFML.Graphics.Texture(game.Window.Size.X, game.Window.Size.Y);
                texture.Update(game.Window);
                var capture = texture.CopyToImage();
                capture.SaveToFile(_saveNameFramePath);
            }

            if (AutoClear)
            {
                Clear(FillColor);
            }
        }
예제 #2
0
        public static void SliceAndDice(Vector2f startPoint, Vector2f endPoint, Texture victim, out Texture sliceA,
                                        out Texture sliceB, float repeatsX, float repeatsY)
        {
            //startPoint = new Vector2f(ConvertUnits.ToDisplayUnits(startPoint.X),
            //                          ConvertUnits.ToDisplayUnits(startPoint.Y));
            //endPoint =   new Vector2f(ConvertUnits.ToDisplayUnits(endPoint.X),
            //                          ConvertUnits.ToDisplayUnits(endPoint.Y));

            var input = victim.CopyToImage();

            var outputA = new Image((uint)(input.Size.X * repeatsX), (uint)(input.Size.Y * repeatsY), Color.Transparent);
            var outputB = new Image((uint)(input.Size.X * repeatsX), (uint)(input.Size.Y * repeatsY), Color.Transparent);

            for (uint y = 0; y < input.Size.Y * repeatsY; y++)
            {
                for (uint x = 0; x < input.Size.X * repeatsX; x++)
                {
                    var pos = new Vector2f(x, y);
                    var start = new Vector2(startPoint.X, startPoint.Y);
                    var end = new Vector2(endPoint.X, endPoint.Y);
                    var line = end - start;
                    line.Normalize();
                    if (WhichSideOfLine(startPoint, endPoint, pos))
                        outputA.SetPixel(x, y, input.GetPixel(x % input.Size.X, y % input.Size.Y));
                    else
                        outputB.SetPixel(x, y, input.GetPixel(x % input.Size.X, y % input.Size.Y));
                }
            }

            sliceA = new Texture(outputA);
            sliceB = new Texture(outputB);
        }
예제 #3
0
        static private void CreateShape(SFML.Graphics.Texture texture, World world)
        {
            // Make collision Geo from bitmap
            // Get pixel data in array​
            byte[] bytes = texture.CopyToImage().Pixels;
            uint[] data  = new uint[texture.Size.X * texture.Size.Y];
            for (int i = 0; i < bytes.Length; i += 4)
            {
                data[i / 4] = BitConverter.ToUInt32(bytes, i);
            }

            Byte myByte = 1;

            List <Vertices> _list = PolygonTools.CreatePolygon(data, (int)texture.Size.X, 0.05f, myByte, true, true);
            Vertices        verts = new Vertices();
            Vector2         scale = ConvertUnits.ToSimUnits(new Vector2(1, 1));

            foreach (Vertices v in _list)
            {
                v.Scale(scale);
                //    v.Translate(ConvertUnits.ToSimUnits(new Vector2(-16, -16)));
                Body body = new Body(world);
                body.SleepingAllowed = false;
                body.UserData        = "wall";
                List <Fixture> fixtures = FixtureFactory.AttachCompoundPolygon(
                    FarseerPhysics.Common.Decomposition.Triangulate.ConvexPartition(SimplifyTools.DouglasPeuckerSimplify(v, 0.05f), TriangulationAlgorithm.Bayazit, false, 0.05f),
                    1, body);
            }
        }
예제 #4
0
        /// <summary>
        /// Creates a pixel collider.
        /// </summary>
        /// <param name="source">The source image to create the collider from.</param>
        /// <param name="tags">The tags to register the collider with.</param>
        public PixelCollider(string source, params int[] tags)
        {
            texture      = Textures.Load(source);
            collideImage = texture.CopyToImage();

            Width  = texture.Size.X;
            Height = texture.Size.Y;

            AddTag(tags);
        }
예제 #5
0
        /// <summary>
        /// Creates a pixel collider.
        /// </summary>
        /// <param name="source">The source image to create the collider from.</param>
        /// <param name="tags">The tags to register the collider with.</param>
        public PixelCollider(string source, params int[] tags)
        {
            texture = Textures.Load(source);
            collideImage = texture.CopyToImage();

            Width = texture.Size.X;
            Height = texture.Size.Y;

            AddTag(tags);
        }
예제 #6
0
        public override System.Drawing.Color PixelColor(Texture texture, uint x, uint y, System.Drawing.Color defaultColor)
        {
            SFMLTexture tex = texture.RendererData as SFMLTexture;

            if (tex == null)
            {
                return(defaultColor);
            }
            var   img   = tex.CopyToImage();
            Color pixel = img.GetPixel(x, y);

            return(System.Drawing.Color.FromArgb(pixel.A, pixel.R, pixel.G, pixel.B));
        }
예제 #7
0
        uint[] GetBitmask(Texture texture)
        {
            uint[] mask;

            if (!Bitmasks.ContainsKey(texture))
            {
                Image image = texture.CopyToImage();
                mask = CreateBitmask(texture, image);
            }
            else
                return Bitmasks[texture];

            return mask;
        }
예제 #8
0
        public static System.Drawing.Bitmap ConvertTextureToBitmap(SFML.Graphics.Texture texture)
        {
            var image  = texture.CopyToImage();
            var size   = image.Size;
            var bitmap = new System.Drawing.Bitmap((int)size.X, (int)size.Y,
                                                   System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            for (var x = 0; x < size.X; ++x)
            {
                for (var y = 0; y < size.Y; ++y)
                {
                    var p = image.GetPixel((uint)x, (uint)y);
                    bitmap.SetPixel(x, y, System.Drawing.Color.FromArgb(p.A, p.R, p.G, p.B));
                }
            }

            return(bitmap);
        }
예제 #9
0
파일: Program.cs 프로젝트: libjared/jaunt
        private static void LoadContentInitialize()
        {
            window = new RenderWindow(
                new VideoMode(800, 600), "Project Title");

            windowSize = new Vector2f(800, 600);
            window.SetFramerateLimit(60);
            window.Closed += (a, b) => { window.Close(); };

            camera2D = new View(cameraPos, new Vector2f(640, 480));



            tempIdleN = new Sprite(new Texture("Content/tempIdleN.png"));
            tempIdleS = new Sprite(new Texture("Content/tempIdleS.png"));
            tempIdleEW = new Sprite(new Texture("Content/tempIdleEW.png"));
            background = new Sprite(new Texture("Content/background.png"));
            basicLevelDec = new Sprite(new Texture("Content/basicLevel_decorMap.png"));


            basicLevelCol = new Texture("Content/basicLevel_collisionMap.png");
            backgroundTexture = basicLevelCol;
            map = backgroundTexture.CopyToImage();

            font = new Font("Content/Font1.ttf");

            click = new SoundBuffer("Content/click.wav");
            SaD = new SoundBuffer("Content/SaD.wav");
            fart = new SoundBuffer("Content/fart.wav");
            crunch = new SoundBuffer("Content/crunch.wav");

            window.TextEntered += (object sender, TextEventArgs e) =>
            {

                if (Keyboard.IsKeyPressed(Keyboard.Key.Back))
                {
                    if (clientPlayer.textCapture.Length > 0)
                        clientPlayer.textCapture = clientPlayer.textCapture.Substring(0, clientPlayer.textCapture.Length - 1);
                }
                else if (Keyboard.IsKeyPressed(Keyboard.Key.Return))
                {
                }
                else if (Keyboard.IsKeyPressed(Keyboard.Key.LControl))
                {
                }
                else if (Keyboard.IsKeyPressed(Keyboard.Key.Tab))
                {
                }
                else
                {
                    clientPlayer.textCapture += e.Unicode;
                }

            };

            window.Closed += (object sender, EventArgs e) =>
            {
                client.Disconnect("Exit");
                System.Threading.Thread.Sleep(100);

            };

        }
        /// <summary>
        /// Returns a surface with the specified image rendered to it as characters.
        /// </summary>
        /// <param name="image">The image to render.</param>
        /// <returns>The surface.</returns>
        public TextSurface GetSurface(Texture2D image)
        {
            editor.Clear();

#if SFML
            using (var imageData = image.CopyToImage())
            {
                using (var memStream = new global::System.IO.MemoryStream())
                {
                    var binForm = new global::System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    memStream.Write(imageData.Pixels, 0, imageData.Pixels.Length);
                    memStream.Seek(0, global::System.IO.SeekOrigin.Begin);
                    pixels = (Color[])binForm.Deserialize(memStream);
                }
            }
#elif MONOGAME
            image.GetData <Color>(pixels);
#endif

            System.Threading.Tasks.Parallel.For(0, surface.Width * surface.Height, (i) =>
                                                //for (int i = 0; i < surface.Width * surface.Height; i++)
            {
                int allR = 0;
                int allG = 0;
                int allB = 0;

                int min = i * fontPixels;
                int max = min + fontPixels;

                for (int pixel = min; pixel < max; pixel++)
                {
                    Color color = pixels[indexes[pixel]];

                    allR += color.R;
                    allG += color.G;
                    allB += color.B;
                }

                // print our character
                byte sr = (byte)(allR / fontPixels);
                byte sg = (byte)(allG / fontPixels);
                byte sb = (byte)(allB / fontPixels);

                var newColor = new Color(sr, sg, sb);
                float sbri   = newColor.GetBrightness() * 255;


                Point surfacePoint = surface.GetPointFromIndex(i);
                if (UseBlockMode)
                {
                    if (sbri > 204)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 219, newColor); //█
                    }
                    else if (sbri > 152)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 178, newColor); //▓
                    }
                    else if (sbri > 100)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 177, newColor); //▒
                    }
                    else if (sbri > 48)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 176, newColor); //░
                    }
                    else
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 0, Color.Black);
                    }
                }
                else
                {
                    if (sbri > 230)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'#', newColor);
                    }
                    else if (sbri > 207)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'&', newColor);
                    }
                    else if (sbri > 184)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'$', newColor);
                    }
                    else if (sbri > 161)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'X', newColor);
                    }
                    else if (sbri > 138)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'x', newColor);
                    }
                    else if (sbri > 115)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'=', newColor);
                    }
                    else if (sbri > 92)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'+', newColor);
                    }
                    else if (sbri > 69)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)';', newColor);
                    }
                    else if (sbri > 46)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)':', newColor);
                    }
                    else if (sbri > 23)
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, (int)'.', newColor);
                    }
                    else
                    {
                        editor.SetGlyph(surfacePoint.X, surfacePoint.Y, 0, Color.Black);
                    }
                }
            }
                                                );

            return(surface);
        }
예제 #11
0
        /// <summary>
        /// Copies the pixels from a <see cref="Texture"/> to a <see cref="Bitmap"/>.
        /// </summary>
        /// <param name="texture">The source <see cref="Texture"/>.</param>
        /// <param name="b">The destination <see cref="Bitmap"/>.</param>
        /// <param name="source">The source area to copy.</param>
        static unsafe void CopyToBitmap(Texture texture, Bitmap b, Rectangle source)
        {
            const int bytesPerColor = 4;

            var image = texture.CopyToImage();
            var pixels = image.Pixels;

            // Lock the whole bitmap for write only
            var rect = new Rectangle(0, 0, source.Width, source.Height);
            var data = b.LockBits(rect, ImageLockMode.WriteOnly, b.PixelFormat);

            try
            {
                var srcStride = rect.Width * bytesPerColor;
                var dataStride = data.Stride / bytesPerColor;
                var srcXtimesBPP = source.X * bytesPerColor;
                var srcY = source.Y;

                // Grab the pointer to the pixels array
                fixed (byte* p = pixels)
                {
                    var row = (int*)data.Scan0;

                    // Copy the pixel values byte-by-byte, making sure to copy the RGBA source to the ARGB destination
                    for (var y = 0; y < data.Height; y++)
                    {
                        var srcOff = ((y + srcY) * srcStride) + srcXtimesBPP;

                        for (var x = 0; x < data.Width; x++)
                        {
                            // Masks for getting the 4 bytes in the int
                            const int b0 = 255;
                            const int b1 = b0 << 8;
                            const int b2 = b0 << 16;
                            const int b3 = b0 << 24;

                            // Get the raw value at the source (pixels[]) as an int (instead of grabbing each byte at a time)
                            var raw = *((int*)(p + srcOff));

                            // Convert to the correct format by moving doing the following to the source bytes:
                            //      src 0 -> dst 2 (move left 2 bytes)
                            //      src 1 -> dst 1 (no moving)
                            //      src 2 -> dst 0 (move right 2 bytes)
                            //      src 3 -> dst 3 (no moving)
                            var converted = (raw & (b3 | b1)) | ((raw & b0) << 16) | ((raw & b2) >> 16);

                            // Store the converted result
                            row[x] = converted;

                            // Move the source over 4 bytes
                            srcOff += bytesPerColor;
                        }

                        row += dataStride;
                    }
                }
            }
            finally
            {
                b.UnlockBits(data);
            }
        }