Finished image-implementation and added some tests

This commit is contained in:
Darth Affe 2024-07-14 18:00:24 +02:00
parent afdf36e0e5
commit 2d30dc98ec
18 changed files with 1405 additions and 999 deletions

View File

@ -0,0 +1,162 @@
namespace HPPH.Test.Image;
[TestClass]
public class ImageTest
{
#region Constants
private const int TEST_WIDTH = 1920;
private const int TEST_HEIGHT = 1080;
#endregion
#region Methods
[TestMethod]
public void TestImageCreation()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
Assert.AreEqual(TEST_WIDTH, image.Width);
Assert.AreEqual(TEST_HEIGHT, image.Height);
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), image[x, y]);
}
[TestMethod]
public void TestImageInnerFull()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
image = image[0, 0, image.Width, image.Height];
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), image[x, y]);
}
[TestMethod]
public void TestImageEnumerator()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
int counter = 0;
foreach (IColor color in image)
{
int x = counter % image.Width;
int y = counter / image.Width;
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), color);
counter++;
}
}
[TestMethod]
public void TestImageInnerPartial()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
image = image[163, 280, 720, 13];
Assert.AreEqual(720, image.Width);
Assert.AreEqual(13, image.Height);
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(163 + x, 280 + y), image[x, y]);
}
[TestMethod]
public void TestImageInnerInnerPartial()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
image = image[163, 280, 720, 13];
image = image[15, 2, 47, 8];
Assert.AreEqual(47, image.Width);
Assert.AreEqual(8, image.Height);
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(178 + x, 282 + y), image[x, y]);
}
[TestMethod]
public void TestImageRowIndexer()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
Assert.AreEqual(image.Height, image.Rows.Count);
for (int y = 0; y < image.Height; y++)
{
IImageRow row = image.Rows[y];
Assert.AreEqual(image.Width, row.Length);
for (int x = 0; x < row.Length; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
}
}
[TestMethod]
public void TestImageRowEnumerator()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
int y = 0;
foreach (IImageRow row in image.Rows)
{
for (int x = 0; x < row.Length; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
y++;
}
}
[TestMethod]
public void TestImageColumnIndexer()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
Assert.AreEqual(image.Width, image.Columns.Count);
for (int x = 0; x < image.Width; x++)
{
IImageColumn column = image.Columns[x];
Assert.AreEqual(image.Height, column.Length);
for (int y = 0; y < column.Length; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
}
}
[TestMethod]
public void TestImageColumnEnumerator()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
int x = 0;
foreach (IImageColumn column in image.Columns)
{
for (int y = 0; y < column.Length; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
x++;
}
}
[TestMethod]
public void TestAsRefImage()
{
IImage image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT);
image = image[163, 280, 720, 13];
image = image[15, 2, 47, 8];
RefImage<ColorARGB> refImage = image.AsRefImage<ColorARGB>();
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(image[x, y], refImage[x, y]);
}
#endregion
}

View File

@ -0,0 +1,152 @@
using System.Diagnostics;
namespace HPPH.Test.Image;
[TestClass]
public class RefImageTest
{
#region Constants
private const int TEST_WIDTH = 1920;
private const int TEST_HEIGHT = 1080;
#endregion
#region Methods
[TestMethod]
public void TestImageCreation()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
Assert.AreEqual(TEST_WIDTH, image.Width);
Assert.AreEqual(TEST_HEIGHT, image.Height);
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), image[x, y]);
}
[TestMethod]
public void TestImageInnerFull()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
image = image[0, 0, image.Width, image.Height];
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), image[x, y]);
}
[TestMethod]
public void TestImageEnumerator()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
int counter = 0;
foreach (ColorARGB color in image)
{
int x = counter % image.Width;
int y = counter / image.Width;
if(y == 1) Debugger.Break();
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), color);
counter++;
}
}
[TestMethod]
public void TestImageInnerPartial()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
image = image[163, 280, 720, 13];
Assert.AreEqual(720, image.Width);
Assert.AreEqual(13, image.Height);
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(163 + x, 280 + y), image[x, y]);
}
[TestMethod]
public void TestImageInnerInnerPartial()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
image = image[163, 280, 720, 13];
image = image[15, 2, 47, 8];
Assert.AreEqual(47, image.Width);
Assert.AreEqual(8, image.Height);
for (int y = 0; y < image.Height; y++)
for (int x = 0; x < image.Width; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(178 + x, 282 + y), image[x, y]);
}
[TestMethod]
public void TestImageRowIndexer()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
Assert.AreEqual(image.Height, image.Rows.Count);
for (int y = 0; y < image.Height; y++)
{
ImageRow<ColorARGB> row = image.Rows[y];
Assert.AreEqual(image.Width, row.Length);
for (int x = 0; x < row.Length; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
}
}
[TestMethod]
public void TestImageRowEnumerator()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
int y = 0;
foreach (ImageRow<ColorARGB> row in image.Rows)
{
for (int x = 0; x < row.Length; x++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), row[x]);
y++;
}
}
[TestMethod]
public void TestImageColumnIndexer()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
Assert.AreEqual(image.Width, image.Columns.Count);
for (int x = 0; x < image.Width; x++)
{
ImageColumn<ColorARGB> column = image.Columns[x];
Assert.AreEqual(image.Height, column.Length);
for (int y = 0; y < column.Length; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
}
}
[TestMethod]
public void TestImageColumnEnumerator()
{
RefImage<ColorARGB> image = TestDataHelper.CreateTestImage<ColorARGB>(TEST_WIDTH, TEST_HEIGHT).AsRefImage();
int x = 0;
foreach (ImageColumn<ColorARGB> column in image.Columns)
{
for (int y = 0; y < column.Length; y++)
Assert.AreEqual(TestDataHelper.GetColorFromLocation<ColorARGB>(x, y), column[y]);
x++;
}
}
#endregion
}

View File

@ -0,0 +1,24 @@
namespace HPPH.Test;
internal static class TestDataHelper
{
public static T GetColorFromLocation<T>(int x, int y)
where T : struct, IColor
{
byte[] xBytes = BitConverter.GetBytes((short)x);
byte[] yBytes = BitConverter.GetBytes((short)y);
return (T)T.Create(xBytes[0], xBytes[1], yBytes[0], yBytes[1]);
}
public static Image<T> CreateTestImage<T>(int width, int height)
where T : struct, IColor
{
T[] buffer = new T[width * height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
buffer[(y * width) + x] = GetColorFromLocation<T>(x, y);
return Image<T>.Create(buffer, width, height);
}
}

View File

@ -1,265 +0,0 @@
// DarthAffe 05.09.2023: Based on https://github.com/CommunityToolkit/dotnet/blob/b0d6c4f9c0cfb5d860400abb00b0ca1b3e94dfa4/src/CommunityToolkit.HighPerformance/Enumerables/ReadOnlyRefEnumerable%7BT%7D.cs
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace HPPH;
/// <summary>
/// A <see langword="ref"/> <see langword="struct"/> that iterates readonly items from arbitrary memory locations.
/// </summary>
/// <typeparam name="T">The type of items to enumerate.</typeparam>
public readonly ref struct ReadOnlyRefEnumerable<T>
{
#region Properties & Fields
/// <summary>
/// The <see cref="ReadOnlySpan{T}"/> instance pointing to the first item in the target memory area.
/// </summary>
/// <remarks>The <see cref="ReadOnlySpan{T}.Length"/> field maps to the total available length.</remarks>
private readonly ReadOnlySpan<T> _span;
/// <summary>
/// The distance between items in the sequence to enumerate.
/// </summary>
/// <remarks>The distance refers to <typeparamref name="T"/> items, not byte offset.</remarks>
private readonly int _step;
/// <summary>
/// Gets the total available length for the sequence.
/// </summary>
public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _span.Length;
}
/// <summary>
/// Gets the element at the specified zero-based index.
/// </summary>
/// <param name="index">The zero-based index of the element.</param>
/// <returns>A reference to the element at the specified index.</returns>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when <paramref name="index"/> is invalid.
/// </exception>
public ref readonly T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((uint)index >= (uint)Length) throw new IndexOutOfRangeException();
ref T r0 = ref MemoryMarshal.GetReference(_span);
nint offset = (nint)(uint)index * (nint)(uint)_step;
ref T ri = ref Unsafe.Add(ref r0, offset);
return ref ri;
}
}
/// <summary>
/// Gets the element at the specified zero-based index.
/// </summary>
/// <param name="index">The zero-based index of the element.</param>
/// <returns>A reference to the element at the specified index.</returns>
/// <exception cref="IndexOutOfRangeException">
/// Thrown when <paramref name="index"/> is invalid.
/// </exception>
public ref readonly T this[Index index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref this[index.GetOffset(Length)];
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ReadOnlyRefEnumerable{T}"/> struct.
/// </summary>
/// <param name="reference">A reference to the first item of the sequence.</param>
/// <param name="length">The number of items in the sequence.</param>
/// <param name="step">The distance between items in the sequence to enumerate.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ReadOnlyRefEnumerable(in T reference, int length, int step)
{
this._step = step;
_span = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in reference), length);
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator() => new(_span, _step);
public T[] ToArray()
{
int length = _span.Length;
// Empty array if no data is mapped
if (length == 0)
return [];
T[] array = new T[length];
CopyTo(array);
return array;
}
/// <summary>
/// Copies the contents of this <see cref="ReadOnlyRefEnumerable{T}"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="ReadOnlyRefEnumerable{T}"/> instance.
/// </exception>
public void CopyTo(Span<T> destination)
{
if (_step == 1)
{
_span.CopyTo(destination);
return;
}
ref T sourceRef = ref MemoryMarshal.GetReference(_span);
int length = _span.Length;
if ((uint)destination.Length < (uint)length)
throw new ArgumentException("The target span is too short to copy all the current items to.");
ref T destinationRef = ref MemoryMarshal.GetReference(destination);
CopyTo(ref sourceRef, ref destinationRef, (nint)(uint)length, (nint)(uint)_step);
}
/// <summary>
/// Attempts to copy the current <see cref="ReadOnlyRefEnumerable{T}"/> instance to a destination <see cref="Span{T}"/>.
/// </summary>
/// <param name="destination">The target <see cref="Span{T}"/> of the copy operation.</param>
/// <returns>Whether or not the operation was successful.</returns>
public bool TryCopyTo(Span<T> destination)
{
if (destination.Length >= _span.Length)
{
CopyTo(destination);
return true;
}
return false;
}
private static void CopyTo(ref T sourceRef, ref T destinationRef, nint length, nint sourceStep)
{
nint sourceOffset = 0;
nint destinationOffset = 0;
while (length >= 8)
{
Unsafe.Add(ref destinationRef, destinationOffset + 0) = Unsafe.Add(ref sourceRef, sourceOffset);
Unsafe.Add(ref destinationRef, destinationOffset + 1) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
Unsafe.Add(ref destinationRef, destinationOffset + 2) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
Unsafe.Add(ref destinationRef, destinationOffset + 3) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
Unsafe.Add(ref destinationRef, destinationOffset + 4) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
Unsafe.Add(ref destinationRef, destinationOffset + 5) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
Unsafe.Add(ref destinationRef, destinationOffset + 6) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
Unsafe.Add(ref destinationRef, destinationOffset + 7) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
length -= 8;
sourceOffset += sourceStep;
destinationOffset += 8;
}
if (length >= 4)
{
Unsafe.Add(ref destinationRef, destinationOffset + 0) = Unsafe.Add(ref sourceRef, sourceOffset);
Unsafe.Add(ref destinationRef, destinationOffset + 1) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
Unsafe.Add(ref destinationRef, destinationOffset + 2) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
Unsafe.Add(ref destinationRef, destinationOffset + 3) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
length -= 4;
sourceOffset += sourceStep;
destinationOffset += 4;
}
while (length > 0)
{
Unsafe.Add(ref destinationRef, destinationOffset) = Unsafe.Add(ref sourceRef, sourceOffset);
length -= 1;
sourceOffset += sourceStep;
destinationOffset += 1;
}
}
#endregion
/// <summary>
/// A custom enumerator type to traverse items within a <see cref="ReadOnlyRefEnumerable{T}"/> instance.
/// </summary>
public ref struct Enumerator
{
#region Properties & Fields
/// <inheritdoc cref="ReadOnlyRefEnumerable{T}._span"/>
private readonly ReadOnlySpan<T> _span;
/// <inheritdoc cref="ReadOnlyRefEnumerable{T}._step"/>
private readonly int _step;
/// <summary>
/// The current position in the sequence.
/// </summary>
private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly ref readonly T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
ref T r0 = ref MemoryMarshal.GetReference(_span);
nint offset = (nint)(uint)_position * (nint)(uint)_step;
ref T ri = ref Unsafe.Add(ref r0, offset);
return ref ri;
}
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Enumerator"/> struct.
/// </summary>
/// <param name="span">The <see cref="ReadOnlySpan{T}"/> instance with the info on the items to traverse.</param>
/// <param name="step">The distance between items in the sequence to enumerate.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal Enumerator(ReadOnlySpan<T> span, int step)
{
this._span = span;
this._step = step;
_position = -1;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => ++_position < _span.Length;
#endregion
}
}

View File

@ -9,6 +9,8 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=generatorattributes/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=generic/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=images/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=images_005Cimplementations/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=images_005Cinterfaces/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=interfaces/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=minmax/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=minmax_005Cgeneric/@EntryIndexedValue">True</s:Boolean>

View File

@ -1,191 +0,0 @@
namespace HPPH;
/// <summary>
/// Represents a image.
/// </summary>
public interface IImage : IEnumerable<IColor>
{
/// <summary>
/// Gets the color format used in this image.
/// </summary>
IColorFormat ColorFormat { get; }
/// <summary>
/// Gets the width of this image.
/// </summary>
int Width { get; }
/// <summary>
/// Gets the height of this image.
/// </summary>
int Height { get; }
/// <summary>
/// Gets the size in bytes of this image.
/// </summary>
int SizeInBytes { get; }
/// <summary>
/// Gets the color at the specified location.
/// </summary>
/// <param name="x">The X-location to read.</param>
/// <param name="y">The Y-location to read.</param>
/// <returns>The color at the specified location.</returns>
IColor this[int x, int y] { get; }
/// <summary>
/// Gets an image representing the specified location.
/// </summary>
/// <param name="x">The X-location of the image.</param>
/// <param name="y">The Y-location of the image.</param>
/// <param name="width">The width of the sub-image.</param>
/// <param name="height"></param>
/// <returns></returns>
IImage this[int x, int y, int width, int height] { get; }
/// <summary>
/// Gets a list of all rows of this image.
/// </summary>
IImageRows Rows { get; }
/// <summary>
/// Gets a list of all columns of this image.
/// </summary>
IImageColumns Columns { get; }
/// <summary>
/// Gets an <see cref="RefImage{TColor}"/> representing this <see cref="IImage"/>.
/// </summary>
/// <typeparam name="TColor">The color-type of the iamge.</typeparam>
/// <returns>The <inheritdoc cref="RefImage{TColor}"/>.</returns>
RefImage<TColor> AsRefImage<TColor>() where TColor : struct, IColor;
/// <summary>
/// Copies the contents of this <see cref="IImage"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImage"/> instance.
/// </exception>
void CopyTo(Span<byte> destination);
/// <summary>
/// Allocates a new array and copies this <see cref="IImage"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="IImage"/>.</returns>
byte[] ToRawArray();
IColor[] ToArray();
/// <summary>
/// Represents a list of rows of an image.
/// </summary>
public interface IImageRows : IEnumerable<IImageRow>
{
/// <summary>
/// Gets the amount of rows in this list.
/// </summary>
int Count { get; }
/// <summary>
/// Gets a specific <see cref="IImageRow"/>.
/// </summary>
/// <param name="column">The ´row to get.</param>
/// <returns>The requested <see cref="IImageRow"/>.</returns>
IImageRow this[int column] { get; }
}
/// <summary>
/// Represents a list of columns of an image.
/// </summary>
public interface IImageColumns : IEnumerable<IImageColumn>
{
/// <summary>
/// Gets the amount of columns in this list.
/// </summary>
int Count { get; }
/// <summary>
/// Gets a specific <see cref="IImageColumn"/>.
/// </summary>
/// <param name="column">The column to get.</param>
/// <returns>The requested <see cref="IImageColumn"/>.</returns>
IImageColumn this[int column] { get; }
}
/// <summary>
/// Represents a single row of an image.
/// </summary>
public interface IImageRow : IEnumerable<IColor>
{
/// <summary>
/// Gets the length of the row.
/// </summary>
int Length { get; }
/// <summary>
/// Gets the size in bytes of this row.
/// </summary>
int SizeInBytes { get; }
/// <summary>
/// Gets the <see cref="IColor"/> at the specified location.
/// </summary>
/// <param name="x">The location to get the color from.</param>
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
IColor this[int x] { get; }
/// <summary>
/// Copies the contents of this <see cref="IImageRow"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageRow"/> instance.
/// </exception>
void CopyTo(Span<byte> destination);
/// <summary>
/// Allocates a new array and copies this <see cref="IImageRow"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="IImageRow"/>.</returns>
byte[] ToArray();
}
/// <summary>
/// Represents a single column of an image.
/// </summary>
public interface IImageColumn : IEnumerable<IColor>
{
/// <summary>
/// Gets the length of the column.
/// </summary>
int Length { get; }
/// <summary>
/// Gets the size in bytes of this column.
/// </summary>
int SizeInBytes { get; }
/// <summary>
/// Gets the <see cref="IColor"/> at the specified location.
/// </summary>
/// <param name="y">The location to get the color from.</param>
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
IColor this[int y] { get; }
/// <summary>
/// Copies the contents of this <see cref="IImageColumn"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageColumn"/> instance.
/// </exception>
void CopyTo(Span<byte> destination);
/// <summary>
/// Allocates a new array and copies this <see cref="IImageColumn"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="IImageColumn"/>.</returns>
byte[] ToArray();
}
}

View File

@ -6,8 +6,8 @@ namespace HPPH;
/// <inheritdoc />
[SkipLocalsInit]
public sealed class Image<TColor> : IImage
where TColor : struct, IColor
public sealed class Image<T> : IImage<T>
where T : struct, IColor
{
#region Properties & Fields
@ -21,7 +21,7 @@ public sealed class Image<TColor> : IImage
public IColorFormat ColorFormat
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => TColor.ColorFormat;
get => T.ColorFormat;
}
/// <inheritdoc />
@ -37,42 +37,59 @@ public sealed class Image<TColor> : IImage
#region Indexer
IColor IImage.this[int x, int y] => this[x, y];
/// <inheritdoc />
public IColor this[int x, int y]
public ref readonly T this[int x, int y]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
return MemoryMarshal.Cast<byte, TColor>(_buffer.AsSpan()[((_y + y) * _stride)..])[_x + x];
return ref Unsafe.Add(ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), (nint)(uint)((_y + y) * _stride))), (nint)(uint)(_x + x));
}
}
IImage IImage.this[int x, int y, int width, int height]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
}
}
/// <inheritdoc />
public IImage this[int x, int y, int width, int height]
public RefImage<T> this[int x, int y, int width, int height]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
return new Image<TColor>(_buffer, _x + x, _y + y, width, height, _stride);
return new RefImage<T>(_buffer, _x + x, _y + y, width, height, _stride);
}
}
/// <inheritdoc />
public IImage.IImageRows Rows
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new ImageRows(_buffer, _x, _y, Width, Height, _stride);
}
IImageRows IImage.Rows => new IColorImageRows<T>(_buffer, _x, _y, Width, Height, _stride);
/// <inheritdoc />
public IImage.IImageColumns Columns
public ImageRows<T> Rows
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new ImageColumns(_buffer, _x, _y, Width, Height, _stride);
get => new(_buffer, _x, _y, Width, Height, _stride);
}
IImageColumns IImage.Columns => new IColorImageColumns<T>(_buffer, _x, _y, Width, Height, _stride);
/// <inheritdoc />
public ImageColumns<T> Columns
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_buffer, _x, _y, Width, Height, _stride);
}
#endregion
@ -93,19 +110,21 @@ public sealed class Image<TColor> : IImage
#region Methods
public static Image<TColor> Create(ReadOnlySpan<TColor> buffer, int width, int height)
=> Create(MemoryMarshal.AsBytes(buffer), width, height, width * TColor.ColorFormat.BytesPerPixel);
public static Image<T> Create(ReadOnlySpan<T> buffer, int width, int height)
=> Create(MemoryMarshal.AsBytes(buffer), width, height, width * T.ColorFormat.BytesPerPixel);
public static Image<TColor> Create(ReadOnlySpan<byte> buffer, int width, int height, int stride)
public static Image<T> Create(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.");
byte[] data = new byte[buffer.Length];
buffer.CopyTo(data);
return new Image<TColor>(data, 0, 0, width, height, stride);
return new Image<T>(data, 0, 0, width, height, stride);
}
public void CopyTo(Span<T> destination) => CopyTo(MemoryMarshal.AsBytes(destination));
/// <inheritdoc />
public void CopyTo(Span<byte> destination)
{
@ -113,9 +132,9 @@ public sealed class Image<TColor> : IImage
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
int targetStride = Width * ColorFormat.BytesPerPixel;
IImage.IImageRows rows = Rows;
ImageRows<T> rows = Rows;
Span<byte> target = destination;
foreach (IImage.IImageRow row in rows)
foreach (ImageRow<T> row in rows)
{
row.CopyTo(target);
target = target[targetStride..];
@ -130,324 +149,62 @@ public sealed class Image<TColor> : IImage
return array;
}
//TODO DarthAffe 06.07.2024: This has some potential for optimization
public IColor[] ToArray()
public T[] ToArray()
{
T[] colors = new T[Width * Height];
CopyTo(colors);
return colors;
}
//TODO DarthAffe 11.07.2024: This has some potential for optimization
IColor[] IImage.ToArray()
{
IColor[] colors = new IColor[Width * Height];
int counter = 0;
foreach (IImage.IImageRow row in Rows)
foreach (IColor color in row)
foreach (ImageRow<T> row in Rows)
foreach (T color in row)
colors[counter++] = color;
return colors;
}
/// <inheritdoc />
public RefImage<T> AsRefImage<T>()
where T : struct, IColor
{
if (typeof(T) != typeof(TColor)) throw new ArgumentException("The requested color format does not fit this image.", nameof(T));
public RefImage<T> AsRefImage() => new(_buffer, _x, _y, Width, Height, _stride);
return new RefImage<T>(_buffer, _x, _y, Width, Height, _stride);
public RefImage<TColor> AsRefImage<TColor>()
where TColor : struct, IColor
{
if (typeof(TColor) != typeof(T)) throw new ArgumentException("The requested color format does not fit this image.", nameof(TColor));
return new RefImage<TColor>(_buffer, _x, _y, Width, Height, _stride);
}
/// <inheritdoc />
public IEnumerator<IColor> GetEnumerator()
/// <summary>
/// Returns a reference to the first element of this image inside the full image buffer.
/// </summary>
public ref readonly byte GetPinnableReference()
{
if (_buffer.Length == 0)
return ref Unsafe.NullRef<byte>();
return ref MemoryMarshal.GetReference(new ReadOnlySpan<byte>(_buffer)[((_y * _stride) + (_x * ColorFormat.BytesPerPixel))..]);
}
public IEnumerator<T> GetEnumerator()
{
for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++)
yield return this[x, y];
}
IEnumerator<IColor> IEnumerable<IColor>.GetEnumerator()
{
for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++)
yield return this[x, y];
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
#region Indexer-Classes
/// <inheritdoc />
private sealed class ImageRows : IImage.IImageRows
{
#region Properties & Fields
private readonly byte[] _buffer;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
private readonly int _stride;
/// <inheritdoc />
public int Count => _height;
#endregion
#region Indexer
/// <inheritdoc />
public IImage.IImageRow this[int row]
{
get
{
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
return new ImageRow(_buffer, ((row + _y) * _stride) + (_x * TColor.ColorFormat.BytesPerPixel), _width);
}
}
#endregion
#region Constructors
internal ImageRows(byte[] buffer, int x, int y, int width, int height, int stride)
{
this._buffer = buffer;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._stride = stride;
}
#endregion
#region Methods
/// <inheritdoc />
public IEnumerator<IImage.IImageRow> GetEnumerator()
{
for (int y = 0; y < _height; y++)
yield return this[y];
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}
/// <inheritdoc />
private sealed class ImageRow : IImage.IImageRow
{
#region Properties & Fields
private readonly byte[] _buffer;
private readonly int _start;
private readonly int _length;
/// <inheritdoc />
public int Length => _length;
/// <inheritdoc />
public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel;
#endregion
#region Indexer
/// <inheritdoc />
public IColor this[int x]
{
get
{
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
return MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..][x];
}
}
#endregion
#region Constructors
internal ImageRow(byte[] buffer, int start, int length)
{
this._buffer = buffer;
this._start = start;
this._length = length;
}
#endregion
#region Methods
/// <inheritdoc />
public void CopyTo(Span<byte> destination)
{
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
_buffer.AsSpan().Slice(_start, SizeInBytes).CopyTo(destination);
}
/// <inheritdoc />
public byte[] ToArray()
{
byte[] array = new byte[SizeInBytes];
CopyTo(array);
return array;
}
/// <inheritdoc />
public IEnumerator<IColor> GetEnumerator()
{
for (int x = 0; x < _length; x++)
yield return this[x];
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}
/// <inheritdoc />
private sealed class ImageColumns : IImage.IImageColumns
{
#region Properties & Fields
private readonly byte[] _buffer;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
private readonly int _stride;
/// <inheritdoc />
public int Count => _width;
#endregion
#region Indexer
/// <inheritdoc />
public IImage.IImageColumn this[int column]
{
get
{
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
return new ImageColumn(_buffer, (_y * _stride) + ((_x + column) * TColor.ColorFormat.BytesPerPixel), _height, _stride);
}
}
#endregion
#region Constructors
internal ImageColumns(byte[] buffer, int x, int y, int width, int height, int stride)
{
this._buffer = buffer;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._stride = stride;
}
#endregion
#region Methods
/// <inheritdoc />
public IEnumerator<IImage.IImageColumn> GetEnumerator()
{
for (int y = 0; y < _height; y++)
yield return this[y];
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}
/// <inheritdoc />
private sealed class ImageColumn : IImage.IImageColumn
{
#region Properties & Fields
private readonly byte[] _buffer;
private readonly int _start;
private readonly int _length;
private readonly int _step;
/// <inheritdoc />
public int Length => _length;
/// <inheritdoc />
public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel;
#endregion
#region Indexer
/// <inheritdoc />
public IColor this[int y]
{
get
{
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
return MemoryMarshal.Cast<byte, TColor>(_buffer.AsSpan()[_start..])[y * _step];
}
}
#endregion
#region Constructors
internal ImageColumn(byte[] buffer, int start, int length, int step)
{
this._buffer = buffer;
this._start = start;
this._length = length;
this._step = step;
}
#endregion
#region Methods
/// <inheritdoc />
public void CopyTo(Span<byte> destination)
{
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
if (_step == 1)
_buffer.AsSpan(_start, SizeInBytes).CopyTo(destination);
else
{
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer.AsSpan()[_start..]);
Span<TColor> target = MemoryMarshal.Cast<byte, TColor>(destination);
for (int i = 0; i < Length; i++)
target[i] = data[i * _step];
}
}
/// <inheritdoc />
public byte[] ToArray()
{
byte[] array = new byte[SizeInBytes];
CopyTo(array);
return array;
}
/// <inheritdoc />
public IEnumerator<IColor> GetEnumerator()
{
for (int y = 0; y < _length; y++)
yield return this[y];
}
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}
#endregion
}

207
HPPH/Images/ImageColumn.cs Normal file
View File

@ -0,0 +1,207 @@
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace HPPH;
[SkipLocalsInit]
public readonly ref struct ImageColumn<T>
where T : struct, IColor
{
#region Properties & Fields
private readonly ReadOnlySpan<byte> _buffer;
private readonly int _start;
private readonly int _length;
private readonly int _step;
public int Length => _length;
public int SizeInBytes => Length * T.ColorFormat.BytesPerPixel;
#endregion
#region Indexer
public ref readonly T this[int y]
{
get
{
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
return ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer), (nint)(uint)(_start + (y * _step))));
}
}
#endregion
#region Constructors
internal ImageColumn(ReadOnlySpan<byte> buffer, int start, int length, int step)
{
this._buffer = buffer;
this._start = start;
this._length = length;
this._step = step;
}
#endregion
#region Methods
public void CopyTo(Span<T> destination) => CopyTo(MemoryMarshal.AsBytes(destination));
public void CopyTo(Span<byte> destination)
{
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
if (_step == 1)
_buffer.Slice(_start, SizeInBytes).CopyTo(destination);
else
{
ReadOnlySpan<T> data = MemoryMarshal.Cast<byte, T>(_buffer[_start..]);
Span<T> target = MemoryMarshal.Cast<byte, T>(destination);
for (int i = 0; i < Length; i++)
target[i] = data[i * _step];
}
}
public T[] ToArray()
{
T[] array = new T[Length];
CopyTo(array);
return array;
}
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImageColumnEnumerator GetEnumerator() => new(this);
#endregion
public ref struct ImageColumnEnumerator
{
#region Properties & Fields
private readonly ImageColumn<T> _column;
private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _column[_position];
}
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ImageColumnEnumerator(ImageColumn<T> column)
{
this._column = column;
this._position = -1;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => ++_position < _column.Length;
#endregion
}
}
//HACK DarthAffe 14.07.2024: Not nice, should be removed once ref structs are able to implement interfaces (https://github.com/dotnet/csharplang/blob/main/proposals/ref-struct-interfaces.md)
[SkipLocalsInit]
internal class IColorImageColumn<T> : IImageColumn
where T : struct, IColor
{
#region Properties & Fields
private readonly byte[] _buffer;
private readonly int _start;
private readonly int _length;
private readonly int _step;
public int Length => _length;
public int SizeInBytes => Length * T.ColorFormat.BytesPerPixel;
#endregion
#region Indexer
public IColor this[int y]
{
get
{
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
return MemoryMarshal.Cast<byte, T>(_buffer.AsSpan()[(_start + (y * _step))..])[0];
}
}
#endregion
#region Constructors
internal IColorImageColumn(byte[] buffer, int start, int length, int step)
{
this._buffer = buffer;
this._start = start;
this._length = length;
this._step = step;
}
#endregion
#region Methods
public void CopyTo(Span<IColor> destination)
{
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < _length) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
for (int i = 0; i < _length; i++)
destination[i] = this[i];
}
public void CopyTo(Span<byte> destination)
{
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
if (_step == 1)
_buffer.AsSpan().Slice(_start, SizeInBytes).CopyTo(destination);
else
{
ReadOnlySpan<T> data = MemoryMarshal.Cast<byte, T>(_buffer.AsSpan()[_start..]);
Span<T> target = MemoryMarshal.Cast<byte, T>(destination);
for (int i = 0; i < Length; i++)
target[i] = data[i * _step];
}
}
public IColor[] ToArray()
{
IColor[] array = new IColor[Length];
CopyTo(array);
return array;
}
public IEnumerator<IColor> GetEnumerator()
{
for (int i = 0; i < Length; i++)
yield return this[i];
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}

163
HPPH/Images/ImageColumns.cs Normal file
View File

@ -0,0 +1,163 @@
using System.Collections;
using System.Runtime.CompilerServices;
namespace HPPH;
[SkipLocalsInit]
public readonly ref struct ImageColumns<T>
where T : struct, IColor
{
#region Properties & Fields
private readonly ReadOnlySpan<byte> _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
private readonly int _stride;
private readonly int _bpp;
public int Count => _width;
#endregion
#region Indexer
public ImageColumn<T> this[int column]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
return new ImageColumn<T>(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride);
}
}
#endregion
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
internal ImageColumns(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{
this._data = data;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._stride = stride;
_bpp = T.ColorFormat.BytesPerPixel;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImageColumnsEnumerator GetEnumerator() => new(this);
#endregion
public ref struct ImageColumnsEnumerator
{
#region Properties & Fields
private readonly ImageColumns<T> _columns;
private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly ImageColumn<T> Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _columns[_position];
}
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ImageColumnsEnumerator(ImageColumns<T> columns)
{
this._columns = columns;
this._position = -1;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => ++_position < _columns._width;
#endregion
}
}
//HACK DarthAffe 14.07.2024: Not nice, should be removed once ref structs are able to implement interfaces (https://github.com/dotnet/csharplang/blob/main/proposals/ref-struct-interfaces.md)
[SkipLocalsInit]
internal class IColorImageColumns<T> : IImageColumns
where T : struct, IColor
{
#region Properties & Fields
private readonly byte[] _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
private readonly int _stride;
private readonly int _bpp;
public int Count => _width;
#endregion
#region Indexer
public IImageColumn this[int column]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
return new IColorImageColumn<T>(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride);
}
}
#endregion
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
internal IColorImageColumns(byte[] data, int x, int y, int width, int height, int stride)
{
this._data = data;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._stride = stride;
_bpp = T.ColorFormat.BytesPerPixel;
}
#endregion
#region Methods
public IEnumerator<IImageColumn> GetEnumerator()
{
for (int i = 0; i < _width; i++)
yield return this[i];
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}

183
HPPH/Images/ImageRow.cs Normal file
View File

@ -0,0 +1,183 @@
using System.Collections;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace HPPH;
[SkipLocalsInit]
public readonly ref struct ImageRow<T>
where T : struct, IColor
{
#region Properties & Fields
private readonly ReadOnlySpan<byte> _buffer;
private readonly int _start;
private readonly int _length;
public int Length => _length;
public int SizeInBytes => Length * T.ColorFormat.BytesPerPixel;
#endregion
#region Indexer
public ref readonly T this[int x]
{
get
{
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
return ref Unsafe.Add(ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer), (nint)(uint)_start)), (nint)(uint)x);
}
}
#endregion
#region Constructors
internal ImageRow(ReadOnlySpan<byte> buffer, int start, int length)
{
this._buffer = buffer;
this._start = start;
this._length = length;
}
#endregion
#region Methods
public void CopyTo(Span<T> destination) => CopyTo(MemoryMarshal.AsBytes(destination));
public void CopyTo(Span<byte> destination)
{
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
_buffer.Slice(_start, SizeInBytes).CopyTo(destination);
}
public T[] ToArray()
{
T[] array = new T[Length];
CopyTo(array);
return array;
}
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImageRowEnumerator GetEnumerator() => new(this);
#endregion
public ref struct ImageRowEnumerator
{
#region Properties & Fields
private readonly ImageRow<T> _column;
private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _column[_position];
}
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ImageRowEnumerator(ImageRow<T> column)
{
this._column = column;
this._position = -1;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => ++_position < _column.Length;
#endregion
}
}
//HACK DarthAffe 14.07.2024: Not nice, should be removed once ref structs are able to implement interfaces (https://github.com/dotnet/csharplang/blob/main/proposals/ref-struct-interfaces.md)
[SkipLocalsInit]
internal class IColorImageRow<T> : IImageRow
where T : struct, IColor
{
#region Properties & Fields
private readonly byte[] _buffer;
private readonly int _start;
private readonly int _length;
public int Length => _length;
public int SizeInBytes => Length * T.ColorFormat.BytesPerPixel;
#endregion
#region Indexer
public IColor this[int x]
{
get
{
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
return MemoryMarshal.Cast<byte, T>(_buffer.AsSpan()[_start..])[x];
}
}
#endregion
#region Constructors
internal IColorImageRow(byte[] buffer, int start, int length)
{
this._buffer = buffer;
this._start = start;
this._length = length;
}
#endregion
#region Methods
public void CopyTo(Span<IColor> destination)
{
for (int i = 0; i < _length; i++)
destination[i] = this[i];
}
public void CopyTo(Span<byte> destination)
{
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
_buffer.AsSpan().Slice(_start, SizeInBytes).CopyTo(destination);
}
public IColor[] ToArray()
{
IColor[] array = new IColor[Length];
CopyTo(array);
return array;
}
public IEnumerator<IColor> GetEnumerator()
{
for (int i = 0; i < _length; i++)
yield return this[i];
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}

159
HPPH/Images/ImageRows.cs Normal file
View File

@ -0,0 +1,159 @@
using System.Collections;
using System.Runtime.CompilerServices;
namespace HPPH;
[SkipLocalsInit]
public readonly ref struct ImageRows<T>
where T : struct, IColor
{
#region Properties & Fields
private readonly ReadOnlySpan<byte> _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
private readonly int _stride;
public int Count => _height;
#endregion
#region Indexer
public ImageRow<T> this[int row]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
return new ImageRow<T>(_data, ((row + _y) * _stride) + _x, _width);
}
}
#endregion
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
internal ImageRows(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{
this._data = data;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._stride = stride;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImageRowsEnumerator GetEnumerator() => new(this);
#endregion
public ref struct ImageRowsEnumerator
{
#region Properties & Fields
private readonly ImageRows<T> _rows;
private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly ImageRow<T> Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _rows[_position];
}
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ImageRowsEnumerator(ImageRows<T> rows)
{
this._rows = rows;
_position = -1;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => ++_position < _rows._height;
#endregion
}
}
//HACK DarthAffe 14.07.2024: Not nice, should be removed once ref structs are able to implement interfaces (https://github.com/dotnet/csharplang/blob/main/proposals/ref-struct-interfaces.md)
[SkipLocalsInit]
internal class IColorImageRows<T> : IImageRows
where T : struct, IColor
{
#region Properties & Fields
private readonly byte[] _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
private readonly int _stride;
public int Count => _height;
#endregion
#region Indexer
public IImageRow this[int row]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
return new IColorImageRow<T>(_data, ((row + _y) * _stride) + _x, _width);
}
}
#endregion
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
internal IColorImageRows(byte[] data, int x, int y, int width, int height, int stride)
{
this._data = data;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._stride = stride;
}
#endregion
#region Methods
public IEnumerator<IImageRow> GetEnumerator()
{
for (int i = 0; i < _height; i++)
yield return this[i];
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}

View File

@ -0,0 +1,133 @@
namespace HPPH;
/// <summary>
/// Represents an image.
/// </summary>
public interface IImage : IEnumerable<IColor>
{
/// <summary>
/// Gets the color format used in this image.
/// </summary>
IColorFormat ColorFormat { get; }
/// <summary>
/// Gets the width of this image.
/// </summary>
int Width { get; }
/// <summary>
/// Gets the height of this image.
/// </summary>
int Height { get; }
/// <summary>
/// Gets the size in bytes of this image.
/// </summary>
int SizeInBytes { get; }
/// <summary>
/// Gets the color at the specified location.
/// </summary>
/// <param name="x">The X-location to read.</param>
/// <param name="y">The Y-location to read.</param>
/// <returns>The color at the specified location.</returns>
IColor this[int x, int y] { get; }
/// <summary>
/// Gets an image representing the specified location.
/// </summary>
/// <param name="x">The X-location of the image.</param>
/// <param name="y">The Y-location of the image.</param>
/// <param name="width">The width of the sub-image.</param>
/// <param name="height"></param>
/// <returns></returns>
IImage this[int x, int y, int width, int height] { get; }
/// <summary>
/// Gets a list of all rows of this image.
/// </summary>
IImageRows Rows { get; }
/// <summary>
/// Gets a list of all columns of this image.
/// </summary>
IImageColumns Columns { get; }
/// <summary>
/// Gets an <see cref="RefImage{TColor}"/> representing this <see cref="IImage"/>.
/// </summary>
/// <typeparam name="TColor">The color-type of the iamge.</typeparam>
/// <returns>The <inheritdoc cref="RefImage{TColor}"/>.</returns>
RefImage<TColor> AsRefImage<TColor>() where TColor : struct, IColor;
/// <summary>
/// Copies the contents of this <see cref="IImage"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImage"/> instance.
/// </exception>
void CopyTo(Span<byte> destination);
IColor[] ToArray();
/// <summary>
/// Allocates a new array and copies this <see cref="IImage"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="IImage"/>.</returns>
byte[] ToRawArray();
}
/// <summary>
/// Represents an image.
/// </summary>
public interface IImage<T> : IImage
where T : struct, IColor
{
/// <summary>
/// Gets the color at the specified location.
/// </summary>
/// <param name="x">The X-location to read.</param>
/// <param name="y">The Y-location to read.</param>
/// <returns>The color at the specified location.</returns>
new ref readonly T this[int x, int y] { get; }
/// <summary>
/// Gets an image representing the specified location.
/// </summary>
/// <param name="x">The X-location of the image.</param>
/// <param name="y">The Y-location of the image.</param>
/// <param name="width">The width of the sub-image.</param>
/// <param name="height"></param>
/// <returns></returns>
new RefImage<T> this[int x, int y, int width, int height] { get; }
/// <summary>
/// Gets a list of all rows of this image.
/// </summary>
new ImageRows<T> Rows { get; }
/// <summary>
/// Gets a list of all columns of this image.
/// </summary>
new ImageColumns<T> Columns { get; }
/// <summary>
/// Gets an <see cref="RefImage{TColor}"/> representing this <see cref="IImage"/>.
/// </summary>
/// <returns>The <inheritdoc cref="RefImage{TColor}"/>.</returns>
RefImage<T> AsRefImage();
/// <summary>
/// Copies the contents of this <see cref="IImage"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImage"/> instance.
/// </exception>
void CopyTo(Span<T> destination);
new T[] ToArray();
new IEnumerator<T> GetEnumerator();
}

View File

@ -0,0 +1,41 @@
namespace HPPH;
/// <summary>
/// Represents a single column of an image.
/// </summary>
public interface IImageColumn : IEnumerable<IColor>
{
/// <summary>
/// Gets the length of the column.
/// </summary>
int Length { get; }
/// <summary>
/// Gets the size in bytes of this column.
/// </summary>
int SizeInBytes { get; }
/// <summary>
/// Gets the <see cref="IColor"/> at the specified location.
/// </summary>
/// <param name="y">The location to get the color from.</param>
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
IColor this[int y] { get; }
void CopyTo(Span<IColor> destination);
/// <summary>
/// Copies the contents of this <see cref="IImageColumn"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageColumn"/> instance.
/// </exception>
void CopyTo(Span<byte> destination);
/// <summary>
/// Allocates a new array and copies this <see cref="IImageColumn"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="IImageColumn"/>.</returns>
IColor[] ToArray();
}

View File

@ -0,0 +1,19 @@
namespace HPPH;
/// <summary>
/// Represents a list of columns of an image.
/// </summary>
public interface IImageColumns : IEnumerable<IImageColumn>
{
/// <summary>
/// Gets the amount of columns in this list.
/// </summary>
int Count { get; }
/// <summary>
/// Gets a specific <see cref="IImageColumn"/>.
/// </summary>
/// <param name="column">The column to get.</param>
/// <returns>The requested <see cref="IImageColumn"/>.</returns>
IImageColumn this[int column] { get; }
}

View File

@ -0,0 +1,39 @@
namespace HPPH;
/// <summary>
/// Represents a single row of an image.
/// </summary>
public interface IImageRow : IEnumerable<IColor>
{
/// <summary>
/// Gets the length of the row.
/// </summary>
int Length { get; }
/// <summary>
/// Gets the size in bytes of this row.
/// </summary>
int SizeInBytes { get; }
/// <summary>
/// Gets the <see cref="IColor"/> at the specified location.
/// </summary>
/// <param name="x">The location to get the color from.</param>
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
IColor this[int x] { get; }
/// <summary>
/// Copies the contents of this <see cref="IImageRow"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageRow"/> instance.
/// </exception>
void CopyTo(Span<byte> destination);
/// <summary>
/// Allocates a new array and copies this <see cref="IImageRow"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="IImageRow"/>.</returns>
IColor[] ToArray();
}

View File

@ -0,0 +1,19 @@
namespace HPPH;
/// <summary>
/// Represents a list of rows of an image.
/// </summary>
public interface IImageRows : IEnumerable<IImageRow>
{
/// <summary>
/// Gets the amount of rows in this list.
/// </summary>
int Count { get; }
/// <summary>
/// Gets a specific <see cref="IImageRow"/>.
/// </summary>
/// <param name="column">The ´row to get.</param>
/// <returns>The requested <see cref="IImageRow"/>.</returns>
IImageRow this[int column] { get; }
}

View File

@ -3,8 +3,9 @@ using System.Runtime.InteropServices;
namespace HPPH;
public readonly ref struct RefImage<TColor>
where TColor : struct, IColor
[SkipLocalsInit]
public readonly ref struct RefImage<T>
where T : struct, IColor
{
#region Properties & Fields
@ -33,38 +34,35 @@ public readonly ref struct RefImage<TColor>
#region Indexer
public ref readonly TColor this[int x, int y]
public ref readonly T this[int x, int y]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_data[((_y + y) * RawStride)..]);
ref TColor r0 = ref MemoryMarshal.GetReference(data);
nint offset = (nint)(uint)(_x + x);
return ref Unsafe.Add(ref r0, offset);
return ref Unsafe.Add(ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_data), (nint)(uint)((_y + y) * RawStride))), (nint)(uint)(_x + x));
}
}
public RefImage<TColor> this[int x, int y, int width, int height]
public RefImage<T> this[int x, int y, int width, int height]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
return new RefImage<TColor>(_data, _x + x, _y + y, width, height, RawStride);
return new RefImage<T>(_data, _x + x, _y + y, width, height, RawStride);
}
}
public ImageRows Rows
public ImageRows<T> Rows
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_data, _x, _y, Width, Height, RawStride);
}
public ImageColumns Columns
public ImageColumns<T> Columns
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_data, _x, _y, Width, Height, RawStride);
@ -89,20 +87,20 @@ public readonly ref struct RefImage<TColor>
#region Methods
/// <summary>
/// Copies the contents of this <see cref="RefImage{TColor}"/> into a destination <see cref="Span{T}"/> instance.
/// Copies the contents of this <see cref="RefImage{T}"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="RefImage{TColor}"/> instance.
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="RefImage{T}"/> instance.
/// </exception>
public void CopyTo(Span<TColor> destination)
public void CopyTo(Span<T> destination)
{
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (destination.Length < (Width * Height)) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
ImageRows rows = Rows;
Span<TColor> target = destination;
foreach (ReadOnlyRefEnumerable<TColor> row in rows)
ImageRows<T> rows = Rows;
Span<T> target = destination;
foreach (ImageRow<T> row in rows)
{
row.CopyTo(target);
target = target[Width..];
@ -110,12 +108,12 @@ public readonly ref struct RefImage<TColor>
}
/// <summary>
/// Allocates a new array and copies this <see cref="RefImage{TColor}"/> into it.
/// Allocates a new array and copies this <see cref="RefImage{T}"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="RefImage{TColor}"/>.</returns>
public TColor[] ToArray()
/// <returns>The new array containing the data of this <see cref="RefImage{T}"/>.</returns>
public T[] ToArray()
{
TColor[] array = new TColor[Width * Height];
T[] array = new T[Width * Height];
CopyTo(array);
return array;
}
@ -128,7 +126,7 @@ public readonly ref struct RefImage<TColor>
if (_data.Length == 0)
return ref Unsafe.NullRef<byte>();
int offset = (_y * RawStride) + (_x * TColor.ColorFormat.BytesPerPixel);
int offset = (_y * RawStride) + (_x * T.ColorFormat.BytesPerPixel);
return ref MemoryMarshal.GetReference(_data[offset..]);
}
@ -138,7 +136,6 @@ public readonly ref struct RefImage<TColor>
#endregion
//TODO DarthAffe 10.07.2024: anpassen
public ref struct ImageEnumerator
{
#region Properties & Fields
@ -153,15 +150,15 @@ public readonly ref struct RefImage<TColor>
private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly TColor Current
public ref readonly T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
int row = (_position / _width);
int column = _position - (row * _width);
int y = (_position / _width);
int x = _position - (y * _width);
return MemoryMarshal.Cast<byte, TColor>(_data[(_y * _stride)..])[_x + column];
return ref Unsafe.Add(ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_data), (nint)(uint)((_y + y) * _stride))), (nint)(uint)(_x + x));
}
}
@ -194,199 +191,4 @@ public readonly ref struct RefImage<TColor>
#endregion
}
#region Indexer-Structs
public readonly ref struct ImageRows
{
#region Properties & Fields
private readonly ReadOnlySpan<byte> _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
private readonly int _stride;
public int Count => _height;
#endregion
#region Indexer
public ReadOnlyRefEnumerable<TColor> this[int row]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_data[((row + _y) * _stride)..]);
ref TColor r0 = ref MemoryMarshal.GetReference(data);
ref TColor rr = ref Unsafe.Add(ref r0, (nint)(uint)_x);
return new ReadOnlyRefEnumerable<TColor>(rr, _width, 1);
}
}
#endregion
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
internal ImageRows(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{
this._data = data;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._stride = stride;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImageRowsEnumerator GetEnumerator() => new(this);
#endregion
public ref struct ImageRowsEnumerator
{
#region Properties & Fields
private readonly ImageRows _rows;
private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly ReadOnlyRefEnumerable<TColor> Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _rows[_position];
}
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ImageRowsEnumerator(ImageRows rows)
{
this._rows = rows;
_position = -1;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => ++_position < _rows._height;
#endregion
}
}
public readonly ref struct ImageColumns
{
#region Properties & Fields
private readonly ReadOnlySpan<byte> _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _height;
private readonly int _stride;
public int Count => _width;
#endregion
#region Indexer
public ReadOnlyRefEnumerable<TColor> this[int column]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_data[(_y * _stride)..]);
ref TColor r0 = ref MemoryMarshal.GetReference(data);
ref TColor rc = ref Unsafe.Add(ref r0, (nint)(uint)(column + _x));
return new ReadOnlyRefEnumerable<TColor>(rc, _height, _stride);
}
}
#endregion
#region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
internal ImageColumns(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{
this._data = data;
this._x = x;
this._y = y;
this._width = width;
this._height = height;
this._stride = stride;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImageColumnsEnumerator GetEnumerator() => new(this);
#endregion
public ref struct ImageColumnsEnumerator
{
#region Properties & Fields
private readonly ImageColumns _columns;
private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly ReadOnlyRefEnumerable<TColor> Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _columns[_position];
}
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ImageColumnsEnumerator(ImageColumns columns)
{
this._columns = columns;
this._position = -1;
}
#endregion
#region Methods
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => ++_position < _columns._width;
#endregion
}
}
#endregion
}

View File

@ -36,7 +36,7 @@ public static unsafe partial class PixelHelper
try
{
image.CopyTo(buffer);
return MinMax<T>(buffer);
return MinMax(buffer);
}
finally
{