From d6d126d88d2e5b0ef0e9dc10fbc3afaa14f10524 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 18 Aug 2024 12:44:42 +0200 Subject: [PATCH] Improved equality comparison for images and added methods to get raw data access on image-rows --- HPPH/Images/Image.cs | 32 ++++++++++++-------------------- HPPH/Images/ImageRow.cs | 5 ++++- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/HPPH/Images/Image.cs b/HPPH/Images/Image.cs index 1bbea09..8820d42 100644 --- a/HPPH/Images/Image.cs +++ b/HPPH/Images/Image.cs @@ -249,49 +249,41 @@ public sealed class Image : IImage, IEquatable> /// IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - //TODO DarthAffe 20.07.2024: All of those equals can be optimized /// 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()); } /// public bool Equals(IImage? 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()); } /// public bool Equals(Image? other) { if (other == null) return false; + + return Equals(other.AsRefImage()); + } + + public bool Equals(RefImage other) + { if (other.Width != Width) return false; if (other.Height != Height) return false; + RefImage thisRef = AsRefImage(); + for (int y = 0; y < Height; y++) - for (int x = 0; x < Width; x++) - if (!this[x, y].Equals(other[x, y])) - return false; + if (!thisRef.Rows[y].AsByteSpan().SequenceEqual(other.Rows[y].AsByteSpan())) + return false; return true; } diff --git a/HPPH/Images/ImageRow.cs b/HPPH/Images/ImageRow.cs index 40235bb..2bc42da 100644 --- a/HPPH/Images/ImageRow.cs +++ b/HPPH/Images/ImageRow.cs @@ -47,6 +47,9 @@ public readonly ref struct ImageRow #region Methods + public ReadOnlySpan AsSpan() => MemoryMarshal.Cast(AsByteSpan()); + public ReadOnlySpan AsByteSpan() => _buffer.Slice(_start, SizeInBytes); + public void CopyTo(Span destination) => CopyTo(MemoryMarshal.AsBytes(destination)); public void CopyTo(Span destination) @@ -54,7 +57,7 @@ public readonly ref struct ImageRow 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()