mirror of
https://github.com/DarthAffe/HPPH.git
synced 2026-11-12 03:49:11 +00:00
More tests and small fixes
This commit is contained in:
1 parent
2a26c4404f
commit
9f3fbc7473
12 files changed
+1345
-396
No files matched your search
+45
-1
@@ -6,7 +6,7 @@ namespace HPPH;
|
||||
|
||||
/// <inheritdoc />
|
||||
[SkipLocalsInit]
|
||||
public sealed class Image<T> : IImage<T>
|
||||
public sealed class Image<T> : IImage<T>, IEquatable<Image<T>>
|
||||
where T : struct, IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
@@ -224,5 +224,49 @@ public sealed class Image<T> : IImage<T>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public bool Equals(Image<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;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -56,15 +56,10 @@ public readonly ref struct ImageColumn<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));
|
||||
|
||||
if (_step == 1)
|
||||
_buffer.Slice(_start, SizeInBytes).CopyTo(destination);
|
||||
else
|
||||
{
|
||||
ref byte dataRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer), _start);
|
||||
Span<T> target = MemoryMarshal.Cast<byte, T>(destination);
|
||||
for (int i = 0; i < Length; i++)
|
||||
target[i] = Unsafe.As<byte, T>(ref Unsafe.Add(ref dataRef, i * _step));
|
||||
}
|
||||
ref byte dataRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer), _start);
|
||||
Span<T> target = MemoryMarshal.Cast<byte, T>(destination);
|
||||
for (int i = 0; i < Length; i++)
|
||||
target[i] = Unsafe.As<byte, T>(ref Unsafe.Add(ref dataRef, i * _step));
|
||||
}
|
||||
|
||||
public T[] ToArray()
|
||||
@@ -177,15 +172,10 @@ internal class IColorImageColumn<T> : IImageColumn
|
||||
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
|
||||
{
|
||||
ref byte dataRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), _start);
|
||||
Span<T> target = MemoryMarshal.Cast<byte, T>(destination);
|
||||
for (int i = 0; i < Length; i++)
|
||||
target[i] = Unsafe.As<byte, T>(ref Unsafe.Add(ref dataRef, i * _step));
|
||||
}
|
||||
ref byte dataRef = ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), _start);
|
||||
Span<T> target = MemoryMarshal.Cast<byte, T>(destination);
|
||||
for (int i = 0; i < Length; i++)
|
||||
target[i] = Unsafe.As<byte, T>(ref Unsafe.Add(ref dataRef, i * _step));
|
||||
}
|
||||
|
||||
public IColor[] ToArray()
|
||||
|
||||
@@ -28,7 +28,7 @@ public readonly ref struct ImageColumns<T>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
|
||||
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new ImageColumn<T>(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride);
|
||||
}
|
||||
@@ -124,7 +124,7 @@ internal class IColorImageColumns<T> : IImageColumns
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
|
||||
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new IColorImageColumn<T>(_data, (_y * _stride) + ((column + _x) * _bpp), _height, _stride);
|
||||
}
|
||||
|
||||
@@ -152,6 +152,9 @@ internal class IColorImageRow<T> : IImageRow
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public readonly ref struct ImageRows<T>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
|
||||
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new ImageRow<T>(_data, ((row + _y) * _stride) + _x, _width);
|
||||
}
|
||||
@@ -122,7 +122,7 @@ internal class IColorImageRows<T> : IImageRows
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
|
||||
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new IColorImageRow<T>(_data, ((row + _y) * _stride) + _x, _width);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
/// <summary>
|
||||
/// Represents an image.
|
||||
/// </summary>
|
||||
public interface IImage : IEnumerable<IColor>
|
||||
public interface IImage : IEnumerable<IColor>, IEquatable<IImage>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the color format used in this image.
|
||||
@@ -61,7 +61,7 @@ public interface IImage : IEnumerable<IColor>
|
||||
RefImage<TColor> AsRefImage<TColor>() where TColor : struct, IColor;
|
||||
|
||||
IImage<TColor> ConvertTo<TColor>() where TColor : struct, IColor;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="IImage"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
@@ -83,7 +83,7 @@ public interface IImage : IEnumerable<IColor>
|
||||
/// <summary>
|
||||
/// Represents an image.
|
||||
/// </summary>
|
||||
public interface IImage<T> : IImage
|
||||
public interface IImage<T> : IImage, IEquatable<IImage<T>>
|
||||
where T : struct, IColor
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user