Added Wrap-Methods to Image and RefImage for allocation free creation

This commit is contained in:
Darth Affe 2024-07-21 17:29:14 +02:00
parent 536a462540
commit cfdd95f079
2 changed files with 19 additions and 0 deletions

View File

@ -125,6 +125,14 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
return new Image<T>(data, 0, 0, width, height, stride);
}
public static Image<T> Wrap(byte[] buffer, int width, int height, int stride)
{
if (stride < width) throw new ArgumentException("Stride can't be smaller than width.");
if (buffer.Length < (height * stride)) throw new ArgumentException("Not enough data in the buffer.");
return new Image<T>(buffer, 0, 0, width, height, stride);
}
/// <inheritdoc />
public IImage<TColor> ConvertTo<TColor>()
where TColor : struct, IColor

View File

@ -86,6 +86,17 @@ public readonly ref struct RefImage<T>
#region Methods
public static RefImage<T> Wrap(ReadOnlySpan<T> buffer, int width, int height)
=> Wrap(MemoryMarshal.AsBytes(buffer), width, height, width * T.ColorFormat.BytesPerPixel);
public static RefImage<T> Wrap(ReadOnlySpan<byte> buffer, int width, int height, int stride)
{
if (stride < width) throw new ArgumentException("Stride can't be smaller than width.");
if (buffer.Length < (height * stride)) throw new ArgumentException("Not enough data in the buffer.");
return new RefImage<T>(buffer, 0, 0, width, height, stride);
}
/// <summary>
/// Copies the contents of this <see cref="RefImage{T}"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>