From 6d853c2ae449a297a8b22e169e8d9daef2333cad Mon Sep 17 00:00:00 2001 From: Stanislav Skobelkin Date: Sun, 12 Sep 2021 02:28:30 +0300 Subject: [PATCH] Add an allocation-free alternative to Image.Pixels --- src/SFML.Graphics/Image.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/SFML.Graphics/Image.cs b/src/SFML.Graphics/Image.cs index e167a730..f856ceef 100644 --- a/src/SFML.Graphics/Image.cs +++ b/src/SFML.Graphics/Image.cs @@ -297,6 +297,24 @@ public byte[] Pixels } } + //////////////////////////////////////////////////////////// + /// + /// Copy the array of pixels (RGBA 8 bits integers + /// components) to the provided buffer. + /// Buffer size must be at least Width x Height x 4 + /// + /// Buffer to copy to + //////////////////////////////////////////////////////////// + public void CopyPixels(byte[] buffer) + { + Vector2u size = Size; + if (buffer.Length < size.X * size.Y * 4) + { + throw new ArgumentException("Buffer must have a size of at least Width * Height * 4 bytes", nameof(buffer)); + } + Marshal.Copy(sfImage_getPixelsPtr(CPointer), buffer, 0, (int)(size.X * size.Y * 4)); + } + //////////////////////////////////////////////////////////// /// /// Size of the image, in pixels