Fixed some potential issues with non aligned data in Image and RefImage - requires extensive testing!

This commit is contained in:
Darth Affe 2024-07-11 00:21:56 +02:00
parent 7d2496a818
commit be39739f4d
2 changed files with 76 additions and 42 deletions

View File

@ -45,7 +45,7 @@ public sealed class Image<TColor> : IImage
{ {
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException(); if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
return MemoryMarshal.Cast<byte, TColor>(_buffer)[((_y + y) * _stride) + (_x + x)]; return MemoryMarshal.Cast<byte, TColor>(_buffer.AsSpan()[((_y + y) * _stride)..])[_x + x];
} }
} }
@ -79,7 +79,7 @@ public sealed class Image<TColor> : IImage
#region Constructors #region Constructors
internal Image(byte[] buffer, int x, int y, int width, int height, int stride) private Image(byte[] buffer, int x, int y, int width, int height, int stride)
{ {
this._buffer = buffer; this._buffer = buffer;
this._x = x; this._x = x;
@ -93,6 +93,19 @@ public sealed class Image<TColor> : IImage
#region Methods #region Methods
public static Image<TColor> Create(ReadOnlySpan<TColor> buffer, int width, int height)
=> Create(MemoryMarshal.AsBytes(buffer), width, height, width * TColor.ColorFormat.BytesPerPixel);
public static Image<TColor> Create(ReadOnlySpan<byte> buffer, int width, int height, int stride)
{
if (stride < width) throw new ArgumentException("Stride can't be smaller than width.");
if (buffer.Length < (height * stride)) throw new ArgumentException("Not enough data in the buffer.");
byte[] data = new byte[buffer.Length];
buffer.CopyTo(data);
return new Image<TColor>(data, 0, 0, width, height, stride);
}
/// <inheritdoc /> /// <inheritdoc />
public void CopyTo(Span<byte> destination) public void CopyTo(Span<byte> destination)
{ {
@ -136,7 +149,7 @@ public sealed class Image<TColor> : IImage
{ {
if (typeof(T) != typeof(TColor)) throw new ArgumentException("The requested color format does not fit this image.", nameof(T)); if (typeof(T) != typeof(TColor)) throw new ArgumentException("The requested color format does not fit this image.", nameof(T));
return new RefImage<T>(MemoryMarshal.Cast<byte, T>(_buffer), _x, _y, Width, Height, _stride); return new RefImage<T>(_buffer, _x, _y, Width, Height, _stride);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -180,7 +193,7 @@ public sealed class Image<TColor> : IImage
{ {
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException(); if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
return new ImageRow(_buffer, (((row + _y) * _stride) + _x), _width); return new ImageRow(_buffer, ((row + _y) * _stride) + (_x * TColor.ColorFormat.BytesPerPixel), _width);
} }
} }
@ -188,7 +201,7 @@ public sealed class Image<TColor> : IImage
#region Constructors #region Constructors
internal ImageRows(byte[] buffer, int x, int y, int width, int height, int stride) internal ImageRows(byte[] buffer, int x, int y, int width, int height, int stride, int bpp)
{ {
this._buffer = buffer; this._buffer = buffer;
this._x = x; this._x = x;
@ -241,8 +254,7 @@ public sealed class Image<TColor> : IImage
{ {
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException(); if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
ReadOnlySpan<TColor> row = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..]; return MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..][x];
return row[x];
} }
} }
@ -267,7 +279,7 @@ public sealed class Image<TColor> : IImage
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));
MemoryMarshal.Cast<byte, TColor>(_buffer).Slice(_start, _length).CopyTo(MemoryMarshal.Cast<byte, TColor>(destination)); _buffer.AsSpan().Slice(_start, SizeInBytes).CopyTo(destination);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -317,7 +329,7 @@ public sealed class Image<TColor> : IImage
{ {
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException(); if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
return new ImageColumn(_buffer, (_y * _stride) + _x + column, _height, _stride); return new ImageColumn(_buffer, (_y * _stride) + ((_x + column) * TColor.ColorFormat.BytesPerPixel), _height, _stride);
} }
} }
@ -379,8 +391,7 @@ public sealed class Image<TColor> : IImage
{ {
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException(); if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..]; return MemoryMarshal.Cast<byte, TColor>(_buffer.AsSpan()[_start..])[y * _step];
return data[y * _step];
} }
} }
@ -410,7 +421,7 @@ public sealed class Image<TColor> : IImage
_buffer.AsSpan(_start, SizeInBytes).CopyTo(destination); _buffer.AsSpan(_start, SizeInBytes).CopyTo(destination);
else else
{ {
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..]; ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer.AsSpan()[_start..]);
Span<TColor> target = MemoryMarshal.Cast<byte, TColor>(destination); Span<TColor> target = MemoryMarshal.Cast<byte, TColor>(destination);
for (int i = 0; i < Length; i++) for (int i = 0; i < Length; i++)
target[i] = data[i * _step]; target[i] = data[i * _step];

View File

@ -8,7 +8,7 @@ public readonly ref struct RefImage<TColor>
{ {
#region Properties & Fields #region Properties & Fields
private readonly ReadOnlySpan<TColor> _pixels; private readonly ReadOnlySpan<byte> _data;
private readonly int _x; private readonly int _x;
private readonly int _y; private readonly int _y;
@ -40,8 +40,9 @@ public readonly ref struct RefImage<TColor>
{ {
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException(); if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels); ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_data[((_y + y) * RawStride)..]);
nint offset = (nint)(uint)((_y + y) * RawStride) + (_x + x); ref TColor r0 = ref MemoryMarshal.GetReference(data);
nint offset = (nint)(uint)(_x + x);
return ref Unsafe.Add(ref r0, offset); return ref Unsafe.Add(ref r0, offset);
} }
} }
@ -53,29 +54,29 @@ public readonly ref struct RefImage<TColor>
{ {
if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException(); if ((x < 0) || (y < 0) || (width <= 0) || (height <= 0) || ((x + width) > Width) || ((y + height) > Height)) throw new IndexOutOfRangeException();
return new RefImage<TColor>(_pixels, _x + x, _y + y, width, height, RawStride); return new RefImage<TColor>(_data, _x + x, _y + y, width, height, RawStride);
} }
} }
public ImageRows Rows public ImageRows Rows
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_pixels, _x, _y, Width, Height, RawStride); get => new(_data, _x, _y, Width, Height, RawStride);
} }
public ImageColumns Columns public ImageColumns Columns
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(_pixels, _x, _y, Width, Height, RawStride); get => new(_data, _x, _y, Width, Height, RawStride);
} }
#endregion #endregion
#region Constructors #region Constructors
internal RefImage(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride) internal RefImage(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{ {
this._pixels = pixels; this._data = data;
this._x = x; this._x = x;
this._y = y; this._y = y;
this.Width = width; this.Width = width;
@ -122,44 +123,63 @@ public readonly ref struct RefImage<TColor>
/// <summary> /// <summary>
/// Returns a reference to the first element of this image inside the full image buffer. /// Returns a reference to the first element of this image inside the full image buffer.
/// </summary> /// </summary>
public ref readonly TColor GetPinnableReference() public ref readonly byte GetPinnableReference()
{ {
if (_pixels.Length == 0) if (_data.Length == 0)
return ref Unsafe.NullRef<TColor>(); return ref Unsafe.NullRef<byte>();
int offset = (_y * RawStride) + _x; int offset = (_y * RawStride) + (_x * TColor.ColorFormat.BytesPerPixel);
return ref MemoryMarshal.GetReference(_pixels[offset..]); return ref MemoryMarshal.GetReference(_data[offset..]);
} }
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/> /// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public ImageEnumerator GetEnumerator() => new(_pixels); public ImageEnumerator GetEnumerator() => new(_data, _x, _y, Width, Height, RawStride);
#endregion #endregion
//TODO DarthAffe 10.07.2024: anpassen
public ref struct ImageEnumerator public ref struct ImageEnumerator
{ {
#region Properties & Fields #region Properties & Fields
private readonly ReadOnlySpan<TColor> _pixels; private readonly ReadOnlySpan<byte> _data;
private readonly int _x;
private readonly int _y;
private readonly int _width;
private readonly int _stride;
private readonly int _count;
private int _position; private int _position;
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/> /// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
public readonly TColor Current public readonly TColor Current
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _pixels[_position]; get
{
int row = (_position / _width);
int column = _position - (row * _width);
return MemoryMarshal.Cast<byte, TColor>(_data[(_y * _stride)..])[_x + column];
}
} }
#endregion #endregion
#region Constructors #region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ImageEnumerator(ReadOnlySpan<TColor> pixels) internal ImageEnumerator(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{ {
this._pixels = pixels; this._data = data;
this._x = x;
this._y = y;
this._width = width;
this._stride = stride;
_count = _width * height;
_position = -1; _position = -1;
} }
@ -170,7 +190,7 @@ public readonly ref struct RefImage<TColor>
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/> /// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext() => ++_position < _pixels.Length; public bool MoveNext() => ++_position < _count;
#endregion #endregion
} }
@ -181,7 +201,7 @@ public readonly ref struct RefImage<TColor>
{ {
#region Properties & Fields #region Properties & Fields
private readonly ReadOnlySpan<TColor> _pixels; private readonly ReadOnlySpan<byte> _data;
private readonly int _x; private readonly int _x;
private readonly int _y; private readonly int _y;
private readonly int _width; private readonly int _width;
@ -201,8 +221,10 @@ public readonly ref struct RefImage<TColor>
{ {
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException(); if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
ref TColor rr = ref Unsafe.Add(ref r0, (nint)(uint)(((row + _y) * _stride) + _x)); ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_data[((row + _y) * _stride)..]);
ref TColor r0 = ref MemoryMarshal.GetReference(data);
ref TColor rr = ref Unsafe.Add(ref r0, (nint)(uint)_x);
return new ReadOnlyRefEnumerable<TColor>(rr, _width, 1); return new ReadOnlyRefEnumerable<TColor>(rr, _width, 1);
} }
@ -213,9 +235,9 @@ public readonly ref struct RefImage<TColor>
#region Constructors #region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types // ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
public ImageRows(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride) internal ImageRows(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{ {
this._pixels = pixels; this._data = data;
this._x = x; this._x = x;
this._y = y; this._y = y;
this._width = width; this._width = width;
@ -276,7 +298,7 @@ public readonly ref struct RefImage<TColor>
{ {
#region Properties & Fields #region Properties & Fields
private readonly ReadOnlySpan<TColor> _pixels; private readonly ReadOnlySpan<byte> _data;
private readonly int _x; private readonly int _x;
private readonly int _y; private readonly int _y;
private readonly int _width; private readonly int _width;
@ -296,8 +318,9 @@ public readonly ref struct RefImage<TColor>
{ {
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException(); if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels); ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_data[(_y * _stride)..]);
ref TColor rc = ref Unsafe.Add(ref r0, (nint)(uint)((_y * _stride) + (column + _x))); ref TColor r0 = ref MemoryMarshal.GetReference(data);
ref TColor rc = ref Unsafe.Add(ref r0, (nint)(uint)(column + _x));
return new ReadOnlyRefEnumerable<TColor>(rc, _height, _stride); return new ReadOnlyRefEnumerable<TColor>(rc, _height, _stride);
} }
@ -308,9 +331,9 @@ public readonly ref struct RefImage<TColor>
#region Constructors #region Constructors
// ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types // ReSharper disable once ConvertToPrimaryConstructor - Not possible with ref types
public ImageColumns(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride) internal ImageColumns(ReadOnlySpan<byte> data, int x, int y, int width, int height, int stride)
{ {
this._pixels = pixels; this._data = data;
this._x = x; this._x = x;
this._y = y; this._y = y;
this._width = width; this._width = width;