mirror of
https://github.com/DarthAffe/ScreenCapture.NET.git
synced 2025-12-12 21:38:42 +00:00
Implemented generic image
This commit is contained in:
parent
52d37b996e
commit
946dafe649
@ -23,20 +23,12 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
|||||||
public int Id { get; }
|
public int Id { get; }
|
||||||
|
|
||||||
public Display Display { get; }
|
public Display Display { get; }
|
||||||
|
|
||||||
#if NET7_0_OR_GREATER
|
public ColorFormat ColorFormat
|
||||||
public ColorFormat ColorFormat
|
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get => TColor.ColorFormat;
|
get => TColor.ColorFormat;
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
public ColorFormat ColorFormat
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get => IColor.GetColorFormat<TColor>();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the x-location of the region on the screen.
|
/// Gets the x-location of the region on the screen.
|
||||||
@ -93,12 +85,18 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
|||||||
get => MemoryMarshal.Cast<byte, TColor>(RawBuffer);
|
get => MemoryMarshal.Cast<byte, TColor>(RawBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Image<TColor> Image
|
public RefImage<TColor> Image
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get => new(Pixels, 0, 0, Width, Height, Width);
|
get => new(Pixels, 0, 0, Width, Height, Width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IImage ICaptureZone.Image
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => new Image<TColor>(InternalBuffer, 0, 0, Width, Height, Width);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets if the <see cref="CaptureZone{T}"/> should be automatically updated on every captured frame.
|
/// Gets or sets if the <see cref="CaptureZone{T}"/> should be automatically updated on every captured frame.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -154,6 +152,14 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
|||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
|
public RefImage<T> GetRefImage<T>()
|
||||||
|
where T : struct, IColor
|
||||||
|
{
|
||||||
|
if (typeof(T) != typeof(TColor)) throw new ArgumentException("The requested Color-Format does not match the data.", nameof(T));
|
||||||
|
|
||||||
|
return new RefImage<T>(MemoryMarshal.Cast<byte, T>(RawBuffer), 0, 0, Width, Height, Width);
|
||||||
|
}
|
||||||
|
|
||||||
public IDisposable Lock()
|
public IDisposable Lock()
|
||||||
{
|
{
|
||||||
Monitor.Enter(_lock);
|
Monitor.Enter(_lock);
|
||||||
@ -216,6 +222,7 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
|||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public UnlockDisposable(object @lock) => this._lock = @lock;
|
public UnlockDisposable(object @lock) => this._lock = @lock;
|
||||||
|
~UnlockDisposable() => Dispose();
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -227,6 +234,8 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
|||||||
|
|
||||||
Monitor.Exit(_lock);
|
Monitor.Exit(_lock);
|
||||||
_disposed = true;
|
_disposed = true;
|
||||||
|
|
||||||
|
GC.SuppressFinalize(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
namespace ScreenCapture.NET;
|
using System;
|
||||||
|
|
||||||
|
namespace ScreenCapture.NET;
|
||||||
|
|
||||||
public interface IColor
|
public interface IColor
|
||||||
{
|
{
|
||||||
@ -7,16 +9,5 @@ public interface IColor
|
|||||||
byte B { get; }
|
byte B { get; }
|
||||||
byte A { get; }
|
byte A { get; }
|
||||||
|
|
||||||
#if NET7_0_OR_GREATER
|
public static virtual ColorFormat ColorFormat => throw new NotSupportedException();
|
||||||
public static abstract ColorFormat ColorFormat { get; }
|
|
||||||
#else
|
|
||||||
public static ColorFormat GetColorFormat<TColor>()
|
|
||||||
where TColor : IColor
|
|
||||||
{
|
|
||||||
System.Type colorType = typeof(TColor);
|
|
||||||
if (colorType == typeof(ColorBGRA)) return ColorFormat.BGRA;
|
|
||||||
|
|
||||||
throw new System.ArgumentException($"Not ColorFormat registered for '{typeof(TColor).Name}'");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
@ -8,6 +8,7 @@ public interface ICaptureZone
|
|||||||
/// Gets the unique id of this <see cref="ICaptureZone"/>.
|
/// Gets the unique id of this <see cref="ICaptureZone"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
int Id { get; }
|
int Id { get; }
|
||||||
|
|
||||||
Display Display { get; }
|
Display Display { get; }
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the x-location of the region on the screen.
|
/// Gets the x-location of the region on the screen.
|
||||||
@ -40,6 +41,8 @@ public interface ICaptureZone
|
|||||||
|
|
||||||
ReadOnlySpan<byte> RawBuffer { get; }
|
ReadOnlySpan<byte> RawBuffer { get; }
|
||||||
|
|
||||||
|
IImage Image { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets if the <see cref="ICaptureZone"/> should be automatically updated on every captured frame.
|
/// Gets or sets if the <see cref="ICaptureZone"/> should be automatically updated on every captured frame.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -58,4 +61,7 @@ public interface ICaptureZone
|
|||||||
/// Only necessary if <see cref="AutoUpdate"/> is set to <c>false</c>.
|
/// Only necessary if <see cref="AutoUpdate"/> is set to <c>false</c>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void RequestUpdate();
|
void RequestUpdate();
|
||||||
|
|
||||||
|
RefImage<TColor> GetRefImage<TColor>()
|
||||||
|
where TColor : struct, IColor;
|
||||||
}
|
}
|
||||||
|
|||||||
35
ScreenCapture.NET/Model/IImage.cs
Normal file
35
ScreenCapture.NET/Model/IImage.cs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace ScreenCapture.NET;
|
||||||
|
|
||||||
|
public interface IImage : IEnumerable<IColor>
|
||||||
|
{
|
||||||
|
int Width { get; }
|
||||||
|
int Height { get; }
|
||||||
|
|
||||||
|
IColor this[int x, int y] { get; }
|
||||||
|
IImage this[int x, int y, int width, int height] { get; }
|
||||||
|
|
||||||
|
IImageRows Rows { get; }
|
||||||
|
IImageColumns Columns { get; }
|
||||||
|
|
||||||
|
public interface IImageRows : IEnumerable<IImageRow>
|
||||||
|
{
|
||||||
|
IImageRow this[int column] { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IImageColumns : IEnumerable<IImageColumn>
|
||||||
|
{
|
||||||
|
IImageColumn this[int column] { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IImageRow : IEnumerable<IColor>
|
||||||
|
{
|
||||||
|
IColor this[int x] { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface IImageColumn : IEnumerable<IColor>
|
||||||
|
{
|
||||||
|
IColor this[int y] { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,15 +1,17 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace ScreenCapture.NET;
|
namespace ScreenCapture.NET;
|
||||||
|
|
||||||
public readonly ref struct Image<TColor>
|
public sealed class Image<TColor> : IImage
|
||||||
where TColor : struct, IColor
|
where TColor : struct, IColor
|
||||||
{
|
{
|
||||||
#region Properties & Fields
|
#region Properties & Fields
|
||||||
|
|
||||||
private readonly ReadOnlySpan<TColor> _pixels;
|
private readonly byte[] _buffer;
|
||||||
|
|
||||||
private readonly int _x;
|
private readonly int _x;
|
||||||
private readonly int _y;
|
private readonly int _y;
|
||||||
@ -22,46 +24,47 @@ public readonly ref struct Image<TColor>
|
|||||||
|
|
||||||
#region Indexer
|
#region Indexer
|
||||||
|
|
||||||
public TColor this[int x, int y]
|
public IColor this[int x, int y]
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
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 _pixels[((_y + y) * _stride) + (_x + x)];
|
return MemoryMarshal.Cast<byte, TColor>(_buffer)[((_y + y) * _stride) + (_x + x)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Image<TColor> this[int x, int y, int width, int height]
|
public IImage this[int x, int y, int width, int height]
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
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 Image<TColor>(_pixels, _x + x, _y + y, width, height, _stride);
|
return new Image<TColor>(_buffer, _x + x, _y + y, width, height, _stride);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ImageRows Rows
|
public IImage.IImageRows Rows
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get => new(_pixels, _x, _y, Width, Height, _stride);
|
get => new ImageRows(_buffer, _x, _y, Width, Height, _stride);
|
||||||
}
|
}
|
||||||
public ImageColumns Columns
|
|
||||||
|
public IImage.IImageColumns Columns
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
get => new(_pixels, _x, _y, Width, Height, _stride);
|
get => new ImageColumns(_buffer, _x, _y, Width, Height, _stride);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
internal Image(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
|
internal Image(byte[] buffer, int x, int y, int width, int height, int stride)
|
||||||
{
|
{
|
||||||
this._pixels = pixels;
|
this._buffer = buffer;
|
||||||
this._x = x;
|
this._x = x;
|
||||||
this._y = y;
|
this._y = y;
|
||||||
this.Width = width;
|
this.Width = width;
|
||||||
@ -73,78 +76,24 @@ public readonly ref struct Image<TColor>
|
|||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
public void CopyTo(in Span<TColor> dest)
|
public IEnumerator<IColor> GetEnumerator()
|
||||||
{
|
{
|
||||||
if (dest == null) throw new ArgumentNullException(nameof(dest));
|
for (int y = 0; y < Height; y++)
|
||||||
if (dest.Length < (Width * Height)) throw new ArgumentException("The destination is too small to fit this image.", nameof(dest));
|
for (int x = 0; x < Width; x++)
|
||||||
|
yield return this[x, y];
|
||||||
ImageRows rows = Rows;
|
|
||||||
Span<TColor> target = dest;
|
|
||||||
foreach (ReadOnlyRefEnumerable<TColor> row in rows)
|
|
||||||
{
|
|
||||||
row.CopyTo(target);
|
|
||||||
target = target[Width..];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public TColor[] ToArray()
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
{
|
|
||||||
TColor[] array = new TColor[Width * Height];
|
|
||||||
CopyTo(array);
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public ImageEnumerator GetEnumerator() => new(_pixels);
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public ref struct ImageEnumerator
|
#region Indexer-Classes
|
||||||
|
|
||||||
|
private sealed class ImageRows : IImage.IImageRows
|
||||||
{
|
{
|
||||||
#region Properties & Fields
|
#region Properties & Fields
|
||||||
|
|
||||||
private readonly ReadOnlySpan<TColor> _pixels;
|
private readonly byte[] _buffer;
|
||||||
private int _position;
|
|
||||||
|
|
||||||
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
|
||||||
public TColor Current
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get => _pixels[_position];
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Constructors
|
|
||||||
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
internal ImageEnumerator(ReadOnlySpan<TColor> pixels)
|
|
||||||
{
|
|
||||||
this._pixels = pixels;
|
|
||||||
|
|
||||||
_position = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Methods
|
|
||||||
|
|
||||||
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public bool MoveNext() => ++_position < _pixels.Length;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
|
|
||||||
#region Indexer-Structs
|
|
||||||
|
|
||||||
public readonly ref struct ImageRows
|
|
||||||
{
|
|
||||||
#region Properties & Fields
|
|
||||||
|
|
||||||
private readonly ReadOnlySpan<TColor> _pixels;
|
|
||||||
private readonly int _x;
|
private readonly int _x;
|
||||||
private readonly int _y;
|
private readonly int _y;
|
||||||
private readonly int _width;
|
private readonly int _width;
|
||||||
@ -155,17 +104,13 @@ public readonly ref struct Image<TColor>
|
|||||||
|
|
||||||
#region Indexer
|
#region Indexer
|
||||||
|
|
||||||
public readonly ReadOnlyRefEnumerable<TColor> this[int row]
|
public IImage.IImageRow this[int row]
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if ((row < 0) || (row > _height)) throw new IndexOutOfRangeException();
|
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
|
||||||
|
|
||||||
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
|
return new ImageRow(_buffer, (((row + _y) * _stride) + _x), _width);
|
||||||
ref TColor rr = ref Unsafe.Add(ref r0, (nint)(uint)(((row + _y) * _stride) + _x));
|
|
||||||
|
|
||||||
return new ReadOnlyRefEnumerable<TColor>(rr, _width, 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,9 +118,9 @@ public readonly ref struct Image<TColor>
|
|||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public ImageRows(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
|
internal ImageRows(byte[] buffer, int x, int y, int width, int height, int stride)
|
||||||
{
|
{
|
||||||
this._pixels = pixels;
|
this._buffer = buffer;
|
||||||
this._x = x;
|
this._x = x;
|
||||||
this._y = y;
|
this._y = y;
|
||||||
this._width = width;
|
this._width = width;
|
||||||
@ -187,56 +132,71 @@ public readonly ref struct Image<TColor>
|
|||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
public IEnumerator<IImage.IImageRow> GetEnumerator()
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
{
|
||||||
public ImageRowsEnumerator GetEnumerator() => new(this);
|
for (int y = 0; y < _height; y++)
|
||||||
|
yield return this[y];
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public ref struct ImageRowsEnumerator
|
|
||||||
{
|
|
||||||
#region Properties & Fields
|
|
||||||
|
|
||||||
private readonly ImageRows _rows;
|
|
||||||
private int _position;
|
|
||||||
|
|
||||||
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
|
||||||
public ReadOnlyRefEnumerable<TColor> Current
|
|
||||||
{
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get => _rows[_position];
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Constructors
|
|
||||||
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
internal ImageRowsEnumerator(ImageRows rows)
|
|
||||||
{
|
|
||||||
this._rows = rows;
|
|
||||||
|
|
||||||
_position = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Methods
|
|
||||||
|
|
||||||
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public bool MoveNext() => ++_position < _rows._height;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public readonly ref struct ImageColumns
|
private sealed class ImageRow : IImage.IImageRow
|
||||||
{
|
{
|
||||||
#region Properties & Fields
|
#region Properties & Fields
|
||||||
|
|
||||||
private readonly ReadOnlySpan<TColor> _pixels;
|
private readonly byte[] _buffer;
|
||||||
|
private readonly int _start;
|
||||||
|
private readonly int _length;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Indexer
|
||||||
|
|
||||||
|
public IColor this[int x]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException();
|
||||||
|
|
||||||
|
ReadOnlySpan<TColor> row = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
|
||||||
|
return row[x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
internal ImageRow(byte[] buffer, int start, int length)
|
||||||
|
{
|
||||||
|
this._buffer = buffer;
|
||||||
|
this._start = start;
|
||||||
|
this._length = length;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
public IEnumerator<IColor> GetEnumerator()
|
||||||
|
{
|
||||||
|
for (int x = 0; x < _length; x++)
|
||||||
|
yield return this[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class ImageColumns : IImage.IImageColumns
|
||||||
|
{
|
||||||
|
#region Properties & Fields
|
||||||
|
|
||||||
|
private readonly byte[] _buffer;
|
||||||
private readonly int _x;
|
private readonly int _x;
|
||||||
private readonly int _y;
|
private readonly int _y;
|
||||||
private readonly int _width;
|
private readonly int _width;
|
||||||
@ -247,17 +207,13 @@ public readonly ref struct Image<TColor>
|
|||||||
|
|
||||||
#region Indexer
|
#region Indexer
|
||||||
|
|
||||||
public ReadOnlyRefEnumerable<TColor> this[int column]
|
public IImage.IImageColumn this[int column]
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
|
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
|
||||||
|
|
||||||
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
|
return new ImageColumn(_buffer, (_y * _stride) + _x + column, _height, _stride);
|
||||||
ref TColor rc = ref Unsafe.Add(ref r0, (nint)(uint)((_y * _stride) + (column + _x)));
|
|
||||||
|
|
||||||
return new ReadOnlyRefEnumerable<TColor>(rc, _height, _stride);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -265,9 +221,9 @@ public readonly ref struct Image<TColor>
|
|||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
|
|
||||||
public ImageColumns(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
|
internal ImageColumns(byte[] buffer, int x, int y, int width, int height, int stride)
|
||||||
{
|
{
|
||||||
this._pixels = pixels;
|
this._buffer = buffer;
|
||||||
this._x = x;
|
this._x = x;
|
||||||
this._y = y;
|
this._y = y;
|
||||||
this._width = width;
|
this._width = width;
|
||||||
@ -279,47 +235,66 @@ public readonly ref struct Image<TColor>
|
|||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
public IEnumerator<IImage.IImageColumn> GetEnumerator()
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
{
|
||||||
public ImageColumnsEnumerator GetEnumerator() => new(this);
|
for (int y = 0; y < _height; y++)
|
||||||
|
yield return this[y];
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
private sealed class ImageColumn : IImage.IImageColumn
|
||||||
|
{
|
||||||
|
#region Properties & Fields
|
||||||
|
|
||||||
|
private readonly byte[] _buffer;
|
||||||
|
private readonly int _start;
|
||||||
|
private readonly int _length;
|
||||||
|
private readonly int _step;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
public ref struct ImageColumnsEnumerator
|
#region Indexer
|
||||||
|
|
||||||
|
public IColor this[int y]
|
||||||
{
|
{
|
||||||
#region Properties & Fields
|
get
|
||||||
|
|
||||||
private readonly ImageColumns _columns;
|
|
||||||
private int _position;
|
|
||||||
|
|
||||||
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
|
||||||
public ReadOnlyRefEnumerable<TColor> Current
|
|
||||||
{
|
{
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
|
||||||
get => _columns[_position];
|
|
||||||
|
ReadOnlySpan<TColor> row = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
|
||||||
|
return row[y * _step];
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Constructors
|
|
||||||
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
internal ImageColumnsEnumerator(ImageColumns columns)
|
|
||||||
{
|
|
||||||
this._columns = columns;
|
|
||||||
this._position = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Methods
|
|
||||||
|
|
||||||
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
|
||||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
||||||
public bool MoveNext() => ++_position < _columns._width;
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
internal ImageColumn(byte[] buffer, int start, int length, int step)
|
||||||
|
{
|
||||||
|
this._buffer = buffer;
|
||||||
|
this._start = start;
|
||||||
|
this._length = length;
|
||||||
|
this._step = step;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
public IEnumerator<IColor> GetEnumerator()
|
||||||
|
{
|
||||||
|
for (int y = 0; y < _length; y++)
|
||||||
|
yield return this[y];
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
326
ScreenCapture.NET/Model/RefImage.cs
Normal file
326
ScreenCapture.NET/Model/RefImage.cs
Normal file
@ -0,0 +1,326 @@
|
|||||||
|
using System;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
|
namespace ScreenCapture.NET;
|
||||||
|
|
||||||
|
public readonly ref struct RefImage<TColor>
|
||||||
|
where TColor : struct, IColor
|
||||||
|
{
|
||||||
|
#region Properties & Fields
|
||||||
|
|
||||||
|
private readonly ReadOnlySpan<TColor> _pixels;
|
||||||
|
|
||||||
|
private readonly int _x;
|
||||||
|
private readonly int _y;
|
||||||
|
private readonly int _stride;
|
||||||
|
|
||||||
|
public int Width { get; }
|
||||||
|
public int Height { get; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Indexer
|
||||||
|
|
||||||
|
public TColor this[int x, int y]
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
|
||||||
|
|
||||||
|
return _pixels[((_y + y) * _stride) + (_x + x)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public RefImage<TColor> this[int x, int y, int width, int height]
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get
|
||||||
|
{
|
||||||
|
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, _stride);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ImageRows Rows
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => new(_pixels, _x, _y, Width, Height, _stride);
|
||||||
|
}
|
||||||
|
public ImageColumns Columns
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => new(_pixels, _x, _y, Width, Height, _stride);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
internal RefImage(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
|
||||||
|
{
|
||||||
|
this._pixels = pixels;
|
||||||
|
this._x = x;
|
||||||
|
this._y = y;
|
||||||
|
this.Width = width;
|
||||||
|
this.Height = height;
|
||||||
|
this._stride = stride;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
public void CopyTo(in Span<TColor> dest)
|
||||||
|
{
|
||||||
|
if (dest == null) throw new ArgumentNullException(nameof(dest));
|
||||||
|
if (dest.Length < (Width * Height)) throw new ArgumentException("The destination is too small to fit this image.", nameof(dest));
|
||||||
|
|
||||||
|
ImageRows rows = Rows;
|
||||||
|
Span<TColor> target = dest;
|
||||||
|
foreach (ReadOnlyRefEnumerable<TColor> row in rows)
|
||||||
|
{
|
||||||
|
row.CopyTo(target);
|
||||||
|
target = target[Width..];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public TColor[] ToArray()
|
||||||
|
{
|
||||||
|
TColor[] array = new TColor[Width * Height];
|
||||||
|
CopyTo(array);
|
||||||
|
return array;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ImageEnumerator GetEnumerator() => new(_pixels);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public ref struct ImageEnumerator
|
||||||
|
{
|
||||||
|
#region Properties & Fields
|
||||||
|
|
||||||
|
private readonly ReadOnlySpan<TColor> _pixels;
|
||||||
|
private int _position;
|
||||||
|
|
||||||
|
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
||||||
|
public TColor Current
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => _pixels[_position];
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
internal ImageEnumerator(ReadOnlySpan<TColor> pixels)
|
||||||
|
{
|
||||||
|
this._pixels = pixels;
|
||||||
|
|
||||||
|
_position = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool MoveNext() => ++_position < _pixels.Length;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Indexer-Structs
|
||||||
|
|
||||||
|
public readonly ref struct ImageRows
|
||||||
|
{
|
||||||
|
#region Properties & Fields
|
||||||
|
|
||||||
|
private readonly ReadOnlySpan<TColor> _pixels;
|
||||||
|
private readonly int _x;
|
||||||
|
private readonly int _y;
|
||||||
|
private readonly int _width;
|
||||||
|
private readonly int _height;
|
||||||
|
private readonly int _stride;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Indexer
|
||||||
|
|
||||||
|
public readonly ReadOnlyRefEnumerable<TColor> this[int row]
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get
|
||||||
|
{
|
||||||
|
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));
|
||||||
|
|
||||||
|
return new ReadOnlyRefEnumerable<TColor>(rr, _width, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
public ImageRows(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
|
||||||
|
{
|
||||||
|
this._pixels = pixels;
|
||||||
|
this._x = x;
|
||||||
|
this._y = y;
|
||||||
|
this._width = width;
|
||||||
|
this._height = height;
|
||||||
|
this._stride = stride;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ImageRowsEnumerator GetEnumerator() => new(this);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public ref struct ImageRowsEnumerator
|
||||||
|
{
|
||||||
|
#region Properties & Fields
|
||||||
|
|
||||||
|
private readonly ImageRows _rows;
|
||||||
|
private int _position;
|
||||||
|
|
||||||
|
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
||||||
|
public ReadOnlyRefEnumerable<TColor> Current
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => _rows[_position];
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
internal ImageRowsEnumerator(ImageRows rows)
|
||||||
|
{
|
||||||
|
this._rows = rows;
|
||||||
|
|
||||||
|
_position = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool MoveNext() => ++_position < _rows._height;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public readonly ref struct ImageColumns
|
||||||
|
{
|
||||||
|
#region Properties & Fields
|
||||||
|
|
||||||
|
private readonly ReadOnlySpan<TColor> _pixels;
|
||||||
|
private readonly int _x;
|
||||||
|
private readonly int _y;
|
||||||
|
private readonly int _width;
|
||||||
|
private readonly int _height;
|
||||||
|
private readonly int _stride;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Indexer
|
||||||
|
|
||||||
|
public ReadOnlyRefEnumerable<TColor> this[int column]
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if ((column < 0) || (column > _width)) throw new IndexOutOfRangeException();
|
||||||
|
|
||||||
|
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
|
||||||
|
ref TColor rc = ref Unsafe.Add(ref r0, (nint)(uint)((_y * _stride) + (column + _x)));
|
||||||
|
|
||||||
|
return new ReadOnlyRefEnumerable<TColor>(rc, _height, _stride);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
public ImageColumns(ReadOnlySpan<TColor> pixels, int x, int y, int width, int height, int stride)
|
||||||
|
{
|
||||||
|
this._pixels = pixels;
|
||||||
|
this._x = x;
|
||||||
|
this._y = y;
|
||||||
|
this._width = width;
|
||||||
|
this._height = height;
|
||||||
|
this._stride = stride;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public ImageColumnsEnumerator GetEnumerator() => new(this);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public ref struct ImageColumnsEnumerator
|
||||||
|
{
|
||||||
|
#region Properties & Fields
|
||||||
|
|
||||||
|
private readonly ImageColumns _columns;
|
||||||
|
private int _position;
|
||||||
|
|
||||||
|
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
||||||
|
public ReadOnlyRefEnumerable<TColor> Current
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
get => _columns[_position];
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
internal ImageColumnsEnumerator(ImageColumns columns)
|
||||||
|
{
|
||||||
|
this._columns = columns;
|
||||||
|
this._position = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Methods
|
||||||
|
|
||||||
|
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
||||||
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||||
|
public bool MoveNext() => ++_position < _columns._width;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user