Added ConvertTo-method to IImage

This commit is contained in:
Darth Affe 2024-07-14 21:02:02 +02:00
parent 0731472c24
commit 19c9143fa1
2 changed files with 10 additions and 1 deletions

View File

@ -58,7 +58,7 @@ public sealed class Image<T> : IImage<T>
get
{
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
return new Image<T>(_buffer, _x + x, _y + y, width, height, _stride);
}
}
@ -123,6 +123,13 @@ public sealed class Image<T> : IImage<T>
return new Image<T>(data, 0, 0, width, height, stride);
}
public void ConvertTo<TColor>()
where TColor : struct, IColor
{
for (int i = 0; i < Height; i++)
MemoryMarshal.Cast<byte, T>(_buffer.AsSpan().Slice(((i + _y) * _stride) + _x, Width)).ConvertInPlace<T, TColor>();
}
public void CopyTo(Span<T> destination) => CopyTo(MemoryMarshal.AsBytes(destination));
/// <inheritdoc />

View File

@ -60,6 +60,8 @@ public interface IImage : IEnumerable<IColor>
/// <returns>The <inheritdoc cref="RefImage{TColor}"/>.</returns>
RefImage<TColor> AsRefImage<TColor>() where TColor : struct, IColor;
void ConvertTo<T>() where T : struct, IColor;
/// <summary>
/// Copies the contents of this <see cref="IImage"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>