mirror of
https://github.com/DarthAffe/HPPH.git
synced 2025-12-12 13:28:37 +00:00
Improved equality comparison for images and added methods to get raw data access on image-rows
This commit is contained in:
parent
32664a2971
commit
d6d126d88d
@ -249,48 +249,40 @@ public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
//TODO DarthAffe 20.07.2024: All of those equals can be optimized
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IImage? other)
|
||||
{
|
||||
if (other == null) 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++)
|
||||
for (int x = 0; x < Width; x++)
|
||||
if (!this[x, y].Equals(other[x, y]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
return Equals(other.AsRefImage<T>());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Equals(IImage<T>? other)
|
||||
{
|
||||
if (other == null) return false;
|
||||
if (other.Width != Width) return false;
|
||||
if (other.Height != Height) return false;
|
||||
|
||||
for (int y = 0; y < Height; y++)
|
||||
for (int x = 0; x < Width; x++)
|
||||
if (!this[x, y].Equals(other[x, y]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
return Equals(other.AsRefImage());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Equals(Image<T>? other)
|
||||
{
|
||||
if (other == null) return false;
|
||||
|
||||
return Equals(other.AsRefImage());
|
||||
}
|
||||
|
||||
public bool Equals(RefImage<T> other)
|
||||
{
|
||||
if (other.Width != Width) return false;
|
||||
if (other.Height != Height) return false;
|
||||
|
||||
RefImage<T> thisRef = AsRefImage();
|
||||
|
||||
for (int y = 0; y < Height; y++)
|
||||
for (int x = 0; x < Width; x++)
|
||||
if (!this[x, y].Equals(other[x, y]))
|
||||
if (!thisRef.Rows[y].AsByteSpan().SequenceEqual(other.Rows[y].AsByteSpan()))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
|
||||
@ -47,6 +47,9 @@ public readonly ref struct ImageRow<T>
|
||||
|
||||
#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<byte> destination)
|
||||
@ -54,7 +57,7 @@ public readonly ref struct ImageRow<T>
|
||||
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);
|
||||
AsByteSpan().CopyTo(destination);
|
||||
}
|
||||
|
||||
public T[] ToArray()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user