Added additional tests and some small fixes and improvements

This commit is contained in:
DarthAffe committed 2024-07-18 23:09:49 +02:00
1 parent e1738845ea
commit c6ac54a936
24 files changed
+1503 -187

No files matched your search

+1 -1
View File
@@ -205,7 +205,7 @@ public sealed class Image<T> : IImage<T>
if (_buffer.Length == 0)
return ref Unsafe.NullRef<byte>();
return ref MemoryMarshal.GetReference(new ReadOnlySpan<byte>(_buffer)[((_y * _stride) + (_x * ColorFormat.BytesPerPixel))..]);
return ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), (_y * _stride) + (_x * ColorFormat.BytesPerPixel));
}
public IEnumerator<T> GetEnumerator()
+5 -5
View File
@@ -60,10 +60,10 @@ public readonly ref struct ImageColumn<T>
_buffer.Slice(_start, SizeInBytes).CopyTo(destination);
else
{
ReadOnlySpan<T> data = MemoryMarshal.Cast<byte, T>(_buffer[_start..]);
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] = data[i * _step];
target[i] = Unsafe.As<byte, T>(ref Unsafe.Add(ref dataRef, i * _step));
}
}
@@ -143,7 +143,7 @@ internal class IColorImageColumn<T> : IImageColumn
{
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
return MemoryMarshal.Cast<byte, T>(_buffer.AsSpan()[(_start + (y * _step))..])[0];
return Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), _start + (y * _step)));
}
}
@@ -181,10 +181,10 @@ internal class IColorImageColumn<T> : IImageColumn
_buffer.AsSpan().Slice(_start, SizeInBytes).CopyTo(destination);
else
{
ReadOnlySpan<T> data = MemoryMarshal.Cast<byte, T>(_buffer.AsSpan()[_start..]);
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] = data[i * _step];
target[i] = Unsafe.As<byte, T>(ref Unsafe.Add(ref dataRef, i * _step));
}
}
+1 -1
View File
@@ -131,7 +131,7 @@ internal class IColorImageRow<T> : IImageRow
{
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
return MemoryMarshal.Cast<byte, T>(_buffer.AsSpan()[_start..])[x];
return Unsafe.Add(ref Unsafe.As<byte, T>(ref Unsafe.Add(ref MemoryMarshal.GetReference(_buffer.AsSpan()), (nint)(uint)_start)), (nint)(uint)x);
}
}
+1 -2
View File
@@ -126,8 +126,7 @@ public readonly ref struct RefImage<T>
if (_data.Length == 0)
return ref Unsafe.NullRef<byte>();
int offset = (_y * RawStride) + (_x * T.ColorFormat.BytesPerPixel);
return ref MemoryMarshal.GetReference(_data[offset..]);
return ref Unsafe.Add(ref MemoryMarshal.GetReference(_data), (_y * RawStride) + (_x * T.ColorFormat.BytesPerPixel));
}
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
+14 -1
View File
@@ -39,7 +39,7 @@ public static partial class PixelHelper
try
{
image.CopyTo(buffer);
return Average<T>(buffer);
return Average(buffer);
}
finally
{
@@ -47,6 +47,19 @@ public static partial class PixelHelper
}
}
public static T Average<T>(this Span<T> colors)
where T : struct, IColor
{
if (colors == null) throw new ArgumentNullException(nameof(colors));
return T.ColorFormat.BytesPerPixel switch
{
3 => Unsafe.BitCast<Generic3ByteData, T>(Average(MemoryMarshal.Cast<T, Generic3ByteData>(colors))),
4 => Unsafe.BitCast<Generic4ByteData, T>(Average(MemoryMarshal.Cast<T, Generic4ByteData>(colors))),
_ => throw new NotSupportedException("Data is not of a supported valid color-type.")
};
}
public static T Average<T>(this ReadOnlySpan<T> colors)
where T : struct, IColor
{
+1 -1
View File
@@ -86,7 +86,7 @@ public static partial class PixelHelper
T[] result = new T[cubes.Length];
for (int i = 0; i < cubes.Length; i++)
result[i] = Average<T>(cubes[i].Slice(colors));
result[i] = Average(cubes[i].Slice(colors));
return result;
}
+5 -1
View File
@@ -41,7 +41,7 @@ public static unsafe partial class PixelHelper
try
{
image.CopyTo(buffer);
return Sum<T>(buffer);
return Sum(buffer);
}
finally
{
@@ -53,6 +53,10 @@ public static unsafe partial class PixelHelper
where T : struct, IColor
=> T.ColorFormat.Sum(MemoryMarshal.AsBytes(colors));
public static ISum Sum<T>(this Span<T> colors)
where T : struct, IColor
=> T.ColorFormat.Sum(MemoryMarshal.AsBytes(colors));
internal static ISum Sum<T, TSum>(ReadOnlySpan<T> colors)
where T : struct, IColor
where TSum : struct, ISum