Merge pull request #6 from DarthAffe/EqualityImprovements

Improved equality comparison for images and added methods to get raw …
This commit is contained in:
DarthAffe 2024-08-19 21:00:05 +02:00 committed by GitHub
commit 723649053c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 21 deletions

View File

@ -249,49 +249,41 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
/// <inheritdoc /> /// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
//TODO DarthAffe 20.07.2024: All of those equals can be optimized
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IImage? other) public bool Equals(IImage? other)
{ {
if (other == null) return false; if (other == null) return false;
if (other.ColorFormat != ColorFormat) return false; if (other.ColorFormat != ColorFormat) return false;
if (other.Width != Width) return false;
if (other.Height != Height) return false;
for (int y = 0; y < Height; y++) return Equals(other.AsRefImage<T>());
for (int x = 0; x < Width; x++)
if (!this[x, y].Equals(other[x, y]))
return false;
return true;
} }
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(IImage<T>? other) public bool Equals(IImage<T>? other)
{ {
if (other == null) return false; if (other == null) return false;
if (other.Width != Width) return false;
if (other.Height != Height) return false;
for (int y = 0; y < Height; y++) return Equals(other.AsRefImage());
for (int x = 0; x < Width; x++)
if (!this[x, y].Equals(other[x, y]))
return false;
return true;
} }
/// <inheritdoc /> /// <inheritdoc />
public bool Equals(Image<T>? other) public bool Equals(Image<T>? other)
{ {
if (other == null) return false; if (other == null) return false;
return Equals(other.AsRefImage());
}
public bool Equals(RefImage<T> other)
{
if (other.Width != Width) return false; if (other.Width != Width) return false;
if (other.Height != Height) return false; if (other.Height != Height) return false;
RefImage<T> thisRef = AsRefImage();
for (int y = 0; y < Height; y++) for (int y = 0; y < Height; y++)
for (int x = 0; x < Width; x++) if (!thisRef.Rows[y].AsByteSpan().SequenceEqual(other.Rows[y].AsByteSpan()))
if (!this[x, y].Equals(other[x, y])) return false;
return false;
return true; return true;
} }

View File

@ -47,6 +47,9 @@ public readonly ref struct ImageRow<T>
#region Methods #region Methods
public ReadOnlySpan<T> AsSpan() => MemoryMarshal.Cast<byte, T>(AsByteSpan());
public ReadOnlySpan<byte> AsByteSpan() => _buffer.Slice(_start, SizeInBytes);
public void CopyTo(Span<T> destination) => CopyTo(MemoryMarshal.AsBytes(destination)); public void CopyTo(Span<T> destination) => CopyTo(MemoryMarshal.AsBytes(destination));
public void CopyTo(Span<byte> destination) public void CopyTo(Span<byte> destination)
@ -54,7 +57,7 @@ public readonly ref struct ImageRow<T>
if (destination == null) throw new ArgumentNullException(nameof(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 (destination.Length < SizeInBytes) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
_buffer.Slice(_start, SizeInBytes).CopyTo(destination); AsByteSpan().CopyTo(destination);
} }
public T[] ToArray() public T[] ToArray()