mirror of
https://github.com/DarthAffe/ScreenCapture.NET.git
synced 2025-12-12 13:28:35 +00:00
Changed all image-based things to use HPPH
This commit is contained in:
parent
c30865aec6
commit
4a794409c4
@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using HPPH;
|
||||
using SharpGen.Runtime;
|
||||
using Vortice.Direct3D;
|
||||
using Vortice.Direct3D11;
|
||||
@ -135,7 +136,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
if (_context == null) return;
|
||||
|
||||
@ -188,7 +189,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void CopyRotate0(in ReadOnlySpan<byte> source, int sourceStride, in CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
private static void CopyRotate0(ReadOnlySpan<byte> source, int sourceStride, CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
int height = captureZone.Height;
|
||||
int stride = captureZone.Stride;
|
||||
@ -204,7 +205,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void CopyRotate90(in ReadOnlySpan<byte> source, int sourceStride, in CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
private static void CopyRotate90(ReadOnlySpan<byte> source, int sourceStride, CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
int width = captureZone.Width;
|
||||
int height = captureZone.Height;
|
||||
@ -220,7 +221,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void CopyRotate180(in ReadOnlySpan<byte> source, int sourceStride, in CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
private static void CopyRotate180(ReadOnlySpan<byte> source, int sourceStride, CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
int width = captureZone.Width;
|
||||
int height = captureZone.Height;
|
||||
@ -237,7 +238,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void CopyRotate270(in ReadOnlySpan<byte> source, int sourceStride, in CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
private static void CopyRotate270(ReadOnlySpan<byte> source, int sourceStride, CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
int width = captureZone.Width;
|
||||
int height = captureZone.Height;
|
||||
@ -309,7 +310,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
base.ValidateCaptureZoneAndThrow(x, y, width, height, downscaleLevel);
|
||||
}
|
||||
|
||||
private void InitializeCaptureZone(in CaptureZone<ColorBGRA> captureZone)
|
||||
private void InitializeCaptureZone(CaptureZone<ColorBGRA> captureZone)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using ScreenCapture.NET.Downscale;
|
||||
using HPPH;
|
||||
using SharpGen.Runtime;
|
||||
using Vortice.Direct3D9;
|
||||
|
||||
@ -92,7 +92,7 @@ public sealed class DX9ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
if (_buffer == null) return;
|
||||
|
||||
@ -106,55 +106,26 @@ public sealed class DX9ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void CopyZone(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
private void CopyZone(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
ReadOnlySpan<ColorBGRA> source = MemoryMarshal.Cast<byte, ColorBGRA>(_buffer);
|
||||
Span<ColorBGRA> target = MemoryMarshal.Cast<byte, ColorBGRA>(buffer);
|
||||
|
||||
int offsetX = captureZone.X;
|
||||
int offsetY = captureZone.Y;
|
||||
int width = captureZone.Width;
|
||||
int height = captureZone.Height;
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
int sourceOffset = ((y + offsetY) * Display.Width) + offsetX;
|
||||
int targetOffset = y * width;
|
||||
|
||||
source.Slice(sourceOffset, width).CopyTo(target.Slice(targetOffset, width));
|
||||
}
|
||||
RefImage<ColorBGRA>.Wrap(_buffer, Display.Width, Display.Height, _stride)[captureZone.X, captureZone.Y, captureZone.Width, captureZone.Height]
|
||||
.CopyTo(MemoryMarshal.Cast<byte, ColorBGRA>(buffer));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void DownscaleZone(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
private void DownscaleZone(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
ReadOnlySpan<byte> source = _buffer;
|
||||
Span<byte> target = buffer;
|
||||
RefImage<ColorBGRA> source = RefImage<ColorBGRA>.Wrap(_buffer, Display.Width, Display.Height, _stride)[captureZone.X, captureZone.Y, captureZone.UnscaledWidth, captureZone.UnscaledHeight];
|
||||
Span<ColorBGRA> target = MemoryMarshal.Cast<byte, ColorBGRA>(buffer);
|
||||
|
||||
int blockSize = 1 << captureZone.DownscaleLevel;
|
||||
|
||||
int offsetX = captureZone.X;
|
||||
int offsetY = captureZone.Y;
|
||||
int width = captureZone.Width;
|
||||
int height = captureZone.Height;
|
||||
int stride = captureZone.Stride;
|
||||
int bpp = captureZone.ColorFormat.BytesPerPixel;
|
||||
int unscaledWith = captureZone.UnscaledWidth;
|
||||
|
||||
Span<byte> scaleBuffer = stackalloc byte[bpp];
|
||||
for (int y = 0; y < height; y++)
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
AverageByteSampler.Sample(new SamplerInfo<byte>((x + offsetX) * blockSize, (y + offsetY) * blockSize, blockSize, blockSize, unscaledWith, bpp, source), scaleBuffer);
|
||||
|
||||
int targetOffset = (y * stride) + (x * bpp);
|
||||
|
||||
// DarthAffe 07.09.2023: Unroll as optimization since we know it's always 4 bpp - not ideal but it does quite a lot
|
||||
target[targetOffset] = scaleBuffer[0];
|
||||
target[targetOffset + 1] = scaleBuffer[1];
|
||||
target[targetOffset + 2] = scaleBuffer[2];
|
||||
target[targetOffset + 3] = scaleBuffer[3];
|
||||
}
|
||||
target[(y * width) + x] = source[x * blockSize, y * blockSize, blockSize, blockSize].Average();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@ -1,149 +0,0 @@
|
||||
// DarthAffe 07.09.2023: Copied from https://github.com/DarthAffe/RGB.NET/blob/2e0754f474b82ed4d0cae5c6c44378d234f1321b/RGB.NET.Presets/Textures/Sampler/AverageByteSampler.cs
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ScreenCapture.NET.Downscale;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a sampled that averages multiple byte-data entries.
|
||||
/// </summary>
|
||||
internal static class AverageByteSampler
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private static readonly int INT_VECTOR_LENGTH = Vector<uint>.Count;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public static unsafe void Sample(in SamplerInfo<byte> info, in Span<byte> pixelData)
|
||||
{
|
||||
int count = info.Width * info.Height;
|
||||
if (count == 0) return;
|
||||
|
||||
int dataLength = pixelData.Length;
|
||||
Span<uint> sums = stackalloc uint[dataLength];
|
||||
|
||||
int elementsPerVector = Vector<byte>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
if (Vector.IsHardwareAccelerated && (info.Height > 1) && (info.Width >= valuesPerVector) && (dataLength <= Vector<byte>.Count))
|
||||
{
|
||||
int chunks = info.Width / elementsPerVector;
|
||||
|
||||
Vector<uint> sum1 = Vector<uint>.Zero;
|
||||
Vector<uint> sum2 = Vector<uint>.Zero;
|
||||
Vector<uint> sum3 = Vector<uint>.Zero;
|
||||
Vector<uint> sum4 = Vector<uint>.Zero;
|
||||
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
ReadOnlySpan<byte> data = info[y];
|
||||
|
||||
fixed (byte* colorPtr = data)
|
||||
{
|
||||
byte* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
{
|
||||
Vector<byte> bytes = *(Vector<byte>*)current;
|
||||
Vector.Widen(bytes, out Vector<ushort> short1, out Vector<ushort> short2);
|
||||
Vector.Widen(short1, out Vector<uint> int1, out Vector<uint> int2);
|
||||
Vector.Widen(short2, out Vector<uint> int3, out Vector<uint> int4);
|
||||
|
||||
sum1 = Vector.Add(sum1, int1);
|
||||
sum2 = Vector.Add(sum2, int2);
|
||||
sum3 = Vector.Add(sum3, int3);
|
||||
sum4 = Vector.Add(sum4, int4);
|
||||
|
||||
current += valuesPerVector;
|
||||
}
|
||||
}
|
||||
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
|
||||
int value = 0;
|
||||
int sumIndex = 0;
|
||||
for (int j = 0; (j < INT_VECTOR_LENGTH) && (value < valuesPerVector); j++)
|
||||
{
|
||||
sums[sumIndex] += sum1[j];
|
||||
++sumIndex;
|
||||
++value;
|
||||
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
|
||||
for (int j = 0; (j < INT_VECTOR_LENGTH) && (value < valuesPerVector); j++)
|
||||
{
|
||||
sums[sumIndex] += sum2[j];
|
||||
++sumIndex;
|
||||
++value;
|
||||
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
|
||||
for (int j = 0; (j < INT_VECTOR_LENGTH) && (value < valuesPerVector); j++)
|
||||
{
|
||||
sums[sumIndex] += sum3[j];
|
||||
++sumIndex;
|
||||
++value;
|
||||
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
|
||||
for (int j = 0; (j < INT_VECTOR_LENGTH) && (value < valuesPerVector); j++)
|
||||
{
|
||||
sums[sumIndex] += sum4[j];
|
||||
++sumIndex;
|
||||
++value;
|
||||
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
ReadOnlySpan<byte> data = info[y];
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
}
|
||||
}
|
||||
|
||||
float divisor = count * byte.MaxValue;
|
||||
for (int i = 0; i < pixelData.Length; i++)
|
||||
pixelData[i] = (sums[i] / divisor).GetByteValueFromPercentage();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static byte GetByteValueFromPercentage(this float percentage)
|
||||
{
|
||||
if (float.IsNaN(percentage)) return 0;
|
||||
|
||||
percentage = percentage.Clamp(0, 1.0f);
|
||||
return (byte)(percentage >= 1.0f ? 255 : percentage * 256.0f);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static float Clamp(this float value, float min, float max)
|
||||
{
|
||||
// ReSharper disable ConvertIfStatementToReturnStatement - I'm not sure why, but inlining this statement reduces performance by ~10%
|
||||
if (value < min) return min;
|
||||
if (value > max) return max;
|
||||
return value;
|
||||
// ReSharper restore ConvertIfStatementToReturnStatement
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
// DarthAffe 07.09.2023: Copied from https://github.com/DarthAffe/RGB.NET/blob/2e0754f474b82ed4d0cae5c6c44378d234f1321b/RGB.NET.Core/Rendering/Textures/Sampler/SamplerInfo.cs
|
||||
|
||||
using System;
|
||||
|
||||
namespace ScreenCapture.NET.Downscale;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the information used to sample data.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the data to sample.</typeparam>
|
||||
internal readonly ref struct SamplerInfo<T>
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly ReadOnlySpan<T> _data;
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _stride;
|
||||
private readonly int _dataPerPixel;
|
||||
private readonly int _dataWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the width of the region the data comes from.
|
||||
/// </summary>
|
||||
public readonly int Width;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the height of region the data comes from.
|
||||
/// </summary>
|
||||
public readonly int Height;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data for the requested row.
|
||||
/// </summary>
|
||||
/// <param name="row">The row to get the data for.</param>
|
||||
/// <returns>A readonly span containing the data of the row.</returns>
|
||||
public ReadOnlySpan<T> this[int row] => _data.Slice((((_y + row) * _stride) + _x) * _dataPerPixel, _dataWidth);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SamplerInfo{T}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="x">The X-location of the region the data comes from.</param>
|
||||
/// <param name="y">The Y-location of the region the data comes from.</param>
|
||||
/// <param name="width">The width of the region the data comes from.</param>
|
||||
/// <param name="height">The height of region the data comes from.</param>
|
||||
/// <param name="stride">The number of pixels in a row of data.</param>
|
||||
/// <param name="dataPerPixel">The number of {T} representing a single pixel.</param>
|
||||
/// <param name="data">The data to sample.</param>
|
||||
public SamplerInfo(int x, int y, int width, int height, int stride, int dataPerPixel, in ReadOnlySpan<T> data)
|
||||
{
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._data = data;
|
||||
this._stride = stride;
|
||||
this._dataPerPixel = dataPerPixel;
|
||||
this.Width = width;
|
||||
this.Height = height;
|
||||
|
||||
_dataWidth = width * dataPerPixel;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,149 +0,0 @@
|
||||
// DarthAffe 07.09.2023: Copied from https://github.com/DarthAffe/RGB.NET/blob/2e0754f474b82ed4d0cae5c6c44378d234f1321b/RGB.NET.Presets/Textures/Sampler/AverageByteSampler.cs
|
||||
|
||||
using System;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace ScreenCapture.NET.Downscale;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a sampled that averages multiple byte-data entries.
|
||||
/// </summary>
|
||||
internal static class AverageByteSampler
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private static readonly int INT_VECTOR_LENGTH = Vector<uint>.Count;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public static unsafe void Sample(in SamplerInfo<byte> info, in Span<byte> pixelData)
|
||||
{
|
||||
int count = info.Width * info.Height;
|
||||
if (count == 0) return;
|
||||
|
||||
int dataLength = pixelData.Length;
|
||||
Span<uint> sums = stackalloc uint[dataLength];
|
||||
|
||||
int elementsPerVector = Vector<byte>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
if (Vector.IsHardwareAccelerated && (info.Height > 1) && (info.Width >= valuesPerVector) && (dataLength <= Vector<byte>.Count))
|
||||
{
|
||||
int chunks = info.Width / elementsPerVector;
|
||||
|
||||
Vector<uint> sum1 = Vector<uint>.Zero;
|
||||
Vector<uint> sum2 = Vector<uint>.Zero;
|
||||
Vector<uint> sum3 = Vector<uint>.Zero;
|
||||
Vector<uint> sum4 = Vector<uint>.Zero;
|
||||
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
ReadOnlySpan<byte> data = info[y];
|
||||
|
||||
fixed (byte* colorPtr = data)
|
||||
{
|
||||
byte* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
{
|
||||
Vector<byte> bytes = *(Vector<byte>*)current;
|
||||
Vector.Widen(bytes, out Vector<ushort> short1, out Vector<ushort> short2);
|
||||
Vector.Widen(short1, out Vector<uint> int1, out Vector<uint> int2);
|
||||
Vector.Widen(short2, out Vector<uint> int3, out Vector<uint> int4);
|
||||
|
||||
sum1 = Vector.Add(sum1, int1);
|
||||
sum2 = Vector.Add(sum2, int2);
|
||||
sum3 = Vector.Add(sum3, int3);
|
||||
sum4 = Vector.Add(sum4, int4);
|
||||
|
||||
current += valuesPerVector;
|
||||
}
|
||||
}
|
||||
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
|
||||
int value = 0;
|
||||
int sumIndex = 0;
|
||||
for (int j = 0; (j < INT_VECTOR_LENGTH) && (value < valuesPerVector); j++)
|
||||
{
|
||||
sums[sumIndex] += sum1[j];
|
||||
++sumIndex;
|
||||
++value;
|
||||
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
|
||||
for (int j = 0; (j < INT_VECTOR_LENGTH) && (value < valuesPerVector); j++)
|
||||
{
|
||||
sums[sumIndex] += sum2[j];
|
||||
++sumIndex;
|
||||
++value;
|
||||
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
|
||||
for (int j = 0; (j < INT_VECTOR_LENGTH) && (value < valuesPerVector); j++)
|
||||
{
|
||||
sums[sumIndex] += sum3[j];
|
||||
++sumIndex;
|
||||
++value;
|
||||
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
|
||||
for (int j = 0; (j < INT_VECTOR_LENGTH) && (value < valuesPerVector); j++)
|
||||
{
|
||||
sums[sumIndex] += sum4[j];
|
||||
++sumIndex;
|
||||
++value;
|
||||
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
ReadOnlySpan<byte> data = info[y];
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
}
|
||||
}
|
||||
|
||||
float divisor = count * byte.MaxValue;
|
||||
for (int i = 0; i < pixelData.Length; i++)
|
||||
pixelData[i] = (sums[i] / divisor).GetByteValueFromPercentage();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static byte GetByteValueFromPercentage(this float percentage)
|
||||
{
|
||||
if (float.IsNaN(percentage)) return 0;
|
||||
|
||||
percentage = percentage.Clamp(0, 1.0f);
|
||||
return (byte)(percentage >= 1.0f ? 255 : percentage * 256.0f);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static float Clamp(this float value, float min, float max)
|
||||
{
|
||||
// ReSharper disable ConvertIfStatementToReturnStatement - I'm not sure why, but inlining this statement reduces performance by ~10%
|
||||
if (value < min) return min;
|
||||
if (value > max) return max;
|
||||
return value;
|
||||
// ReSharper restore ConvertIfStatementToReturnStatement
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
// DarthAffe 07.09.2023: Copied from https://github.com/DarthAffe/RGB.NET/blob/2e0754f474b82ed4d0cae5c6c44378d234f1321b/RGB.NET.Core/Rendering/Textures/Sampler/SamplerInfo.cs
|
||||
|
||||
using System;
|
||||
|
||||
namespace ScreenCapture.NET.Downscale;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the information used to sample data.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the data to sample.</typeparam>
|
||||
internal readonly ref struct SamplerInfo<T>
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly ReadOnlySpan<T> _data;
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _stride;
|
||||
private readonly int _dataPerPixel;
|
||||
private readonly int _dataWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the width of the region the data comes from.
|
||||
/// </summary>
|
||||
public readonly int Width;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the height of region the data comes from.
|
||||
/// </summary>
|
||||
public readonly int Height;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data for the requested row.
|
||||
/// </summary>
|
||||
/// <param name="row">The row to get the data for.</param>
|
||||
/// <returns>A readonly span containing the data of the row.</returns>
|
||||
public ReadOnlySpan<T> this[int row] => _data.Slice((((_y + row) * _stride) + _x) * _dataPerPixel, _dataWidth);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SamplerInfo{T}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="x">The X-location of the region the data comes from.</param>
|
||||
/// <param name="y">The Y-location of the region the data comes from.</param>
|
||||
/// <param name="width">The width of the region the data comes from.</param>
|
||||
/// <param name="height">The height of region the data comes from.</param>
|
||||
/// <param name="stride">The number of pixels in a row of data.</param>
|
||||
/// <param name="dataPerPixel">The number of {T} representing a single pixel.</param>
|
||||
/// <param name="data">The data to sample.</param>
|
||||
public SamplerInfo(int x, int y, int width, int height, int stride, int dataPerPixel, in ReadOnlySpan<T> data)
|
||||
{
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._data = data;
|
||||
this._stride = stride;
|
||||
this._dataPerPixel = dataPerPixel;
|
||||
this.Width = width;
|
||||
this.Height = height;
|
||||
|
||||
_dataWidth = width * dataPerPixel;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -2,7 +2,6 @@
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
internal static partial class X11
|
||||
{
|
||||
internal const nint DISPLAY_NAME = 0;
|
||||
@ -65,69 +64,4 @@ internal static partial class X11
|
||||
public nint obdata;
|
||||
// ReSharper restore MemberCanBePrivate.Global
|
||||
}
|
||||
}
|
||||
#else
|
||||
internal static class X11
|
||||
{
|
||||
internal const nint DISPLAY_NAME = 0;
|
||||
|
||||
internal const long ALL_PLANES = -1;
|
||||
internal const int ZPIXMAP = 2;
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern nint XOpenDisplay(nint displayName);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern int XScreenCount(nint display);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern nint XScreenOfDisplay(nint display, int screeenNumber);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern int XWidthOfScreen(nint screen);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern int XHeightOfScreen(nint screen);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern nint XRootWindowOfScreen(nint screen);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern nint XGetImage(nint display, nint drawable, int x, int y, uint width, uint height, long planeMask, int format);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern nint XGetSubImage(nint display, nint drawable, int x, int y, uint width, uint height, long planeMask, int format, nint image, int destX, int dextY);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern void XDestroyImage(nint image);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern nint XDisplayString(nint display);
|
||||
|
||||
[DllImport("libX11.so.6")]
|
||||
internal static extern void XCloseDisplay(nint display);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
internal unsafe struct XImage
|
||||
{
|
||||
// ReSharper disable MemberCanBePrivate.Global
|
||||
public int width;
|
||||
public int height;
|
||||
public int xoffset;
|
||||
public int format;
|
||||
public byte* data;
|
||||
public int byte_order;
|
||||
public int bitmap_unit;
|
||||
public int bitmap_bit_order;
|
||||
public int bitmap_pad;
|
||||
public int depth;
|
||||
public int bytes_per_line;
|
||||
public int bits_per_pixel;
|
||||
public uint red_mask;
|
||||
public uint green_mask;
|
||||
public uint blue_mask;
|
||||
public nint obdata;
|
||||
// ReSharper restore MemberCanBePrivate.Global
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using ScreenCapture.NET.Downscale;
|
||||
using HPPH;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
@ -59,7 +59,7 @@ public sealed class X11ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
using IDisposable @lock = captureZone.Lock();
|
||||
{
|
||||
@ -71,55 +71,26 @@ public sealed class X11ScreenCapture : AbstractScreenCapture<ColorBGRA>
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void CopyZone(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
private void CopyZone(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
ReadOnlySpan<ColorBGRA> source = MemoryMarshal.Cast<byte, ColorBGRA>(Data);
|
||||
Span<ColorBGRA> target = MemoryMarshal.Cast<byte, ColorBGRA>(buffer);
|
||||
|
||||
int offsetX = captureZone.X;
|
||||
int offsetY = captureZone.Y;
|
||||
int width = captureZone.Width;
|
||||
int height = captureZone.Height;
|
||||
int sourceStride = _image.bytes_per_line / captureZone.ColorFormat.BytesPerPixel;
|
||||
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
int sourceOffset = ((y + offsetY) * sourceStride) + offsetX;
|
||||
int targetOffset = y * width;
|
||||
source.Slice(sourceOffset, width).CopyTo(target.Slice(targetOffset, width));
|
||||
}
|
||||
RefImage<ColorBGRA>.Wrap(Data, Display.Width, Display.Height, _image.bytes_per_line)[captureZone.X, captureZone.Y, captureZone.Width, captureZone.Height]
|
||||
.CopyTo(MemoryMarshal.Cast<byte, ColorBGRA>(buffer));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void DownscaleZone(CaptureZone<ColorBGRA> captureZone, in Span<byte> buffer)
|
||||
private void DownscaleZone(CaptureZone<ColorBGRA> captureZone, Span<byte> buffer)
|
||||
{
|
||||
ReadOnlySpan<byte> source = Data;
|
||||
Span<byte> target = buffer;
|
||||
RefImage<ColorBGRA> source = RefImage<ColorBGRA>.Wrap(Data, Display.Width, Display.Height, _image.bytes_per_line)[captureZone.X, captureZone.Y, captureZone.UnscaledWidth, captureZone.UnscaledHeight];
|
||||
Span<ColorBGRA> target = MemoryMarshal.Cast<byte, ColorBGRA>(buffer);
|
||||
|
||||
int blockSize = 1 << captureZone.DownscaleLevel;
|
||||
|
||||
int offsetX = captureZone.X;
|
||||
int offsetY = captureZone.Y;
|
||||
int width = captureZone.Width;
|
||||
int height = captureZone.Height;
|
||||
int stride = captureZone.Stride;
|
||||
int bpp = captureZone.ColorFormat.BytesPerPixel;
|
||||
int sourceStride = _image.bytes_per_line / bpp;
|
||||
|
||||
Span<byte> scaleBuffer = stackalloc byte[bpp];
|
||||
for (int y = 0; y < height; y++)
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
AverageByteSampler.Sample(new SamplerInfo<byte>((x + offsetX) * blockSize, (y + offsetY) * blockSize, blockSize, blockSize, sourceStride, bpp, source), scaleBuffer);
|
||||
|
||||
int targetOffset = (y * stride) + (x * bpp);
|
||||
|
||||
// DarthAffe 09.09.2023: Unroll as optimization since we know it's always 4 bpp - not ideal but it does quite a lot
|
||||
target[targetOffset] = scaleBuffer[0];
|
||||
target[targetOffset + 1] = scaleBuffer[1];
|
||||
target[targetOffset + 2] = scaleBuffer[2];
|
||||
target[targetOffset + 3] = scaleBuffer[3];
|
||||
}
|
||||
target[(y * width) + x] = source[x * blockSize, y * blockSize, blockSize, blockSize].Average();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@ -1,266 +0,0 @@
|
||||
// DarthAffe 05.09.2023: Based on https://github.com/CommunityToolkit/dotnet/blob/b0d6c4f9c0cfb5d860400abb00b0ca1b3e94dfa4/src/CommunityToolkit.HighPerformance/Enumerables/ReadOnlyRefEnumerable%7BT%7D.cs
|
||||
|
||||
// Licensed to the .NET Foundation under one or more agreements.
|
||||
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
// See the LICENSE file in the project root for more information.
|
||||
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// A <see langword="ref"/> <see langword="struct"/> that iterates readonly items from arbitrary memory locations.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of items to enumerate.</typeparam>
|
||||
public readonly ref struct ReadOnlyRefEnumerable<T>
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ReadOnlySpan{T}"/> instance pointing to the first item in the target memory area.
|
||||
/// </summary>
|
||||
/// <remarks>The <see cref="ReadOnlySpan{T}.Length"/> field maps to the total available length.</remarks>
|
||||
private readonly ReadOnlySpan<T> _span;
|
||||
|
||||
/// <summary>
|
||||
/// The distance between items in the sequence to enumerate.
|
||||
/// </summary>
|
||||
/// <remarks>The distance refers to <typeparamref name="T"/> items, not byte offset.</remarks>
|
||||
private readonly int _step;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the total available length for the sequence.
|
||||
/// </summary>
|
||||
public int Length
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => _span.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the element at the specified zero-based index.
|
||||
/// </summary>
|
||||
/// <param name="index">The zero-based index of the element.</param>
|
||||
/// <returns>A reference to the element at the specified index.</returns>
|
||||
/// <exception cref="IndexOutOfRangeException">
|
||||
/// Thrown when <paramref name="index"/> is invalid.
|
||||
/// </exception>
|
||||
public ref readonly T this[int index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((uint)index >= (uint)Length) throw new IndexOutOfRangeException();
|
||||
|
||||
ref T r0 = ref MemoryMarshal.GetReference(_span);
|
||||
nint offset = (nint)(uint)index * (nint)(uint)_step;
|
||||
ref T ri = ref Unsafe.Add(ref r0, offset);
|
||||
|
||||
return ref ri;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the element at the specified zero-based index.
|
||||
/// </summary>
|
||||
/// <param name="index">The zero-based index of the element.</param>
|
||||
/// <returns>A reference to the element at the specified index.</returns>
|
||||
/// <exception cref="IndexOutOfRangeException">
|
||||
/// Thrown when <paramref name="index"/> is invalid.
|
||||
/// </exception>
|
||||
public ref readonly T this[Index index]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => ref this[index.GetOffset(Length)];
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ReadOnlyRefEnumerable{T}"/> struct.
|
||||
/// </summary>
|
||||
/// <param name="reference">A reference to the first item of the sequence.</param>
|
||||
/// <param name="length">The number of items in the sequence.</param>
|
||||
/// <param name="step">The distance between items in the sequence to enumerate.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal ReadOnlyRefEnumerable(in T reference, int length, int step)
|
||||
{
|
||||
this._step = step;
|
||||
|
||||
_span = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(reference), length);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerable.GetEnumerator"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public Enumerator GetEnumerator() => new(_span, _step);
|
||||
|
||||
public T[] ToArray()
|
||||
{
|
||||
int length = _span.Length;
|
||||
|
||||
// Empty array if no data is mapped
|
||||
if (length == 0)
|
||||
return Array.Empty<T>();
|
||||
|
||||
T[] array = new T[length];
|
||||
CopyTo(array);
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="ReadOnlyRefEnumerable{T}"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="ReadOnlyRefEnumerable{T}"/> instance.
|
||||
/// </exception>
|
||||
public void CopyTo(Span<T> destination)
|
||||
{
|
||||
if (_step == 1)
|
||||
{
|
||||
_span.CopyTo(destination);
|
||||
return;
|
||||
}
|
||||
|
||||
ref T sourceRef = ref MemoryMarshal.GetReference(_span);
|
||||
int length = _span.Length;
|
||||
if ((uint)destination.Length < (uint)length)
|
||||
throw new ArgumentException("The target span is too short to copy all the current items to.");
|
||||
|
||||
ref T destinationRef = ref MemoryMarshal.GetReference(destination);
|
||||
|
||||
CopyTo(ref sourceRef, ref destinationRef, (nint)(uint)length, (nint)(uint)_step);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to copy the current <see cref="ReadOnlyRefEnumerable{T}"/> instance to a destination <see cref="Span{T}"/>.
|
||||
/// </summary>
|
||||
/// <param name="destination">The target <see cref="Span{T}"/> of the copy operation.</param>
|
||||
/// <returns>Whether or not the operation was successful.</returns>
|
||||
public bool TryCopyTo(Span<T> destination)
|
||||
{
|
||||
if (destination.Length >= _span.Length)
|
||||
{
|
||||
CopyTo(destination);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void CopyTo(ref T sourceRef, ref T destinationRef, nint length, nint sourceStep)
|
||||
{
|
||||
nint sourceOffset = 0;
|
||||
nint destinationOffset = 0;
|
||||
|
||||
while (length >= 8)
|
||||
{
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 0) = Unsafe.Add(ref sourceRef, sourceOffset);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 1) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 2) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 3) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 4) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 5) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 6) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 7) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
|
||||
length -= 8;
|
||||
sourceOffset += sourceStep;
|
||||
destinationOffset += 8;
|
||||
}
|
||||
|
||||
if (length >= 4)
|
||||
{
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 0) = Unsafe.Add(ref sourceRef, sourceOffset);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 1) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 2) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
Unsafe.Add(ref destinationRef, destinationOffset + 3) = Unsafe.Add(ref sourceRef, sourceOffset += sourceStep);
|
||||
|
||||
length -= 4;
|
||||
sourceOffset += sourceStep;
|
||||
destinationOffset += 4;
|
||||
}
|
||||
|
||||
while (length > 0)
|
||||
{
|
||||
Unsafe.Add(ref destinationRef, destinationOffset) = Unsafe.Add(ref sourceRef, sourceOffset);
|
||||
|
||||
length -= 1;
|
||||
sourceOffset += sourceStep;
|
||||
destinationOffset += 1;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// A custom enumerator type to traverse items within a <see cref="ReadOnlyRefEnumerable{T}"/> instance.
|
||||
/// </summary>
|
||||
public ref struct Enumerator
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc cref="ReadOnlyRefEnumerable{T}._span"/>
|
||||
private readonly ReadOnlySpan<T> _span;
|
||||
|
||||
/// <inheritdoc cref="ReadOnlyRefEnumerable{T}._step"/>
|
||||
private readonly int _step;
|
||||
|
||||
/// <summary>
|
||||
/// The current position in the sequence.
|
||||
/// </summary>
|
||||
private int _position;
|
||||
|
||||
/// <inheritdoc cref="System.Collections.Generic.IEnumerator{T}.Current"/>
|
||||
public readonly ref readonly T Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
ref T r0 = ref MemoryMarshal.GetReference(_span);
|
||||
|
||||
nint offset = (nint)(uint)_position * (nint)(uint)_step;
|
||||
ref T ri = ref Unsafe.Add(ref r0, offset);
|
||||
|
||||
return ref ri;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Enumerator"/> struct.
|
||||
/// </summary>
|
||||
/// <param name="span">The <see cref="ReadOnlySpan{T}"/> instance with the info on the items to traverse.</param>
|
||||
/// <param name="step">The distance between items in the sequence to enumerate.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal Enumerator(ReadOnlySpan<T> span, int step)
|
||||
{
|
||||
this._span = span;
|
||||
this._step = step;
|
||||
|
||||
_position = -1;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc cref="System.Collections.IEnumerator.MoveNext"/>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext() => ++_position < _span.Length;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
namespace ScreenCapture.NET;
|
||||
using HPPH;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Helper-class for black-bar removal.
|
||||
@ -35,10 +37,10 @@ public static class BlackBarDetection
|
||||
/// <returns>The row number of the first row with at least one non-black pixel.</returns>
|
||||
public static int CalculateTop(IImage image, int threshold)
|
||||
{
|
||||
IImage.IImageRows rows = image.Rows;
|
||||
IImageRows rows = image.Rows;
|
||||
for (int y = 0; y < rows.Count; y++)
|
||||
{
|
||||
IImage.IImageRow row = rows[y];
|
||||
IImageRow row = rows[y];
|
||||
foreach (IColor color in row)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
@ -57,10 +59,10 @@ public static class BlackBarDetection
|
||||
/// <returns>The row number of the last row with at least one non-black pixel.</returns>
|
||||
public static int CalculateBottom(IImage image, int threshold)
|
||||
{
|
||||
IImage.IImageRows rows = image.Rows;
|
||||
IImageRows rows = image.Rows;
|
||||
for (int y = rows.Count - 1; y >= 0; y--)
|
||||
{
|
||||
IImage.IImageRow row = rows[y];
|
||||
IImageRow row = rows[y];
|
||||
foreach (IColor color in row)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
@ -79,10 +81,10 @@ public static class BlackBarDetection
|
||||
/// <returns>The column number of the first column with at least one non-black pixel.</returns>
|
||||
public static int CalculateLeft(IImage image, int threshold)
|
||||
{
|
||||
IImage.IImageColumns columns = image.Columns;
|
||||
IImageColumns columns = image.Columns;
|
||||
for (int x = 0; x < columns.Count; x++)
|
||||
{
|
||||
IImage.IImageColumn column = columns[x];
|
||||
IImageColumn column = columns[x];
|
||||
foreach (IColor color in column)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
@ -101,10 +103,10 @@ public static class BlackBarDetection
|
||||
/// <returns>The column number of the last column with at least one non-black pixel.</returns>
|
||||
public static int CalculateRight(IImage image, int threshold)
|
||||
{
|
||||
IImage.IImageColumns columns = image.Columns;
|
||||
IImageColumns columns = image.Columns;
|
||||
for (int x = columns.Count - 1; x >= 0; x--)
|
||||
{
|
||||
IImage.IImageColumn column = columns[x];
|
||||
IImageColumn column = columns[x];
|
||||
foreach (IColor color in column)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
@ -117,6 +119,124 @@ public static class BlackBarDetection
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Image
|
||||
|
||||
/// <summary>
|
||||
/// Create an image with black bars removed
|
||||
/// </summary>
|
||||
/// <param name="image">The image the bars are removed from.</param>
|
||||
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
|
||||
/// <param name="removeTop">A bool indicating if black bars should be removed at the top of the image.</param>
|
||||
/// <param name="removeBottom">A bool indicating if black bars should be removed at the bottom of the image.</param>
|
||||
/// <param name="removeLeft">A bool indicating if black bars should be removed on the left side of the image.</param>
|
||||
/// <param name="removeRight">A bool indicating if black bars should be removed on the right side of the image.</param>
|
||||
/// <returns>The image with black bars removed.</returns>
|
||||
public static RefImage<T> RemoveBlackBars<T>(this IImage<T> image, int threshold = 0, bool removeTop = true, bool removeBottom = true, bool removeLeft = true, bool removeRight = true)
|
||||
where T : struct, IColor
|
||||
{
|
||||
int top = removeTop ? CalculateTop(image, threshold) : 0;
|
||||
int bottom = removeBottom ? CalculateBottom(image, threshold) : image.Height;
|
||||
int left = removeLeft ? CalculateLeft(image, threshold) : 0;
|
||||
int right = removeRight ? CalculateRight(image, threshold) : image.Width;
|
||||
|
||||
return image[left, top, right - left, bottom - top];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the first row starting from the top with at least one non black pixel.
|
||||
/// </summary>
|
||||
/// <param name="image">The image to check.</param>
|
||||
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
|
||||
/// <returns>The row number of the first row with at least one non-black pixel.</returns>
|
||||
public static int CalculateTop<T>(IImage<T> image, int threshold)
|
||||
where T : struct, IColor
|
||||
{
|
||||
ImageRows<T> rows = image.Rows;
|
||||
for (int y = 0; y < rows.Count; y++)
|
||||
{
|
||||
ImageRow<T> row = rows[y];
|
||||
foreach (T color in row)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the last row starting from the top with at least one non black pixel.
|
||||
/// </summary>
|
||||
/// <param name="image">The image to check.</param>
|
||||
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
|
||||
/// <returns>The row number of the last row with at least one non-black pixel.</returns>
|
||||
public static int CalculateBottom<T>(IImage<T> image, int threshold)
|
||||
where T : struct, IColor
|
||||
{
|
||||
ImageRows<T> rows = image.Rows;
|
||||
for (int y = rows.Count - 1; y >= 0; y--)
|
||||
{
|
||||
ImageRow<T> row = rows[y];
|
||||
foreach (T color in row)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
return rows.Count;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the first column starting from the left with at least one non black pixel.
|
||||
/// </summary>
|
||||
/// <param name="image">The image to check.</param>
|
||||
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
|
||||
/// <returns>The column number of the first column with at least one non-black pixel.</returns>
|
||||
public static int CalculateLeft<T>(IImage<T> image, int threshold)
|
||||
where T : struct, IColor
|
||||
{
|
||||
ImageColumns<T> columns = image.Columns;
|
||||
for (int x = 0; x < columns.Count; x++)
|
||||
{
|
||||
ImageColumn<T> column = columns[x];
|
||||
foreach (T color in column)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the last column starting from the top with at least one non black pixel.
|
||||
/// </summary>
|
||||
/// <param name="image">The image to check.</param>
|
||||
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
|
||||
/// <returns>The column number of the last column with at least one non-black pixel.</returns>
|
||||
public static int CalculateRight<T>(IImage<T> image, int threshold)
|
||||
where T : struct, IColor
|
||||
{
|
||||
ImageColumns<T> columns = image.Columns;
|
||||
for (int x = columns.Count - 1; x >= 0; x--)
|
||||
{
|
||||
ImageColumn<T> column = columns[x];
|
||||
foreach (T color in column)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
return columns.Count;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region RefImage
|
||||
|
||||
/// <summary>
|
||||
@ -149,10 +269,10 @@ public static class BlackBarDetection
|
||||
public static int CalculateTop<TColor>(this RefImage<TColor> image, int threshold)
|
||||
where TColor : struct, IColor
|
||||
{
|
||||
RefImage<TColor>.ImageRows rows = image.Rows;
|
||||
ImageRows<TColor> rows = image.Rows;
|
||||
for (int y = 0; y < rows.Count; y++)
|
||||
{
|
||||
ReadOnlyRefEnumerable<TColor> row = rows[y];
|
||||
ImageRow<TColor> row = rows[y];
|
||||
foreach (TColor color in row)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
@ -172,10 +292,10 @@ public static class BlackBarDetection
|
||||
public static int CalculateBottom<TColor>(this RefImage<TColor> image, int threshold)
|
||||
where TColor : struct, IColor
|
||||
{
|
||||
RefImage<TColor>.ImageRows rows = image.Rows;
|
||||
ImageRows<TColor> rows = image.Rows;
|
||||
for (int y = rows.Count - 1; y >= 0; y--)
|
||||
{
|
||||
ReadOnlyRefEnumerable<TColor> row = rows[y];
|
||||
ImageRow<TColor> row = rows[y];
|
||||
foreach (TColor color in row)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
@ -195,10 +315,10 @@ public static class BlackBarDetection
|
||||
public static int CalculateLeft<TColor>(this RefImage<TColor> image, int threshold)
|
||||
where TColor : struct, IColor
|
||||
{
|
||||
RefImage<TColor>.ImageColumns columns = image.Columns;
|
||||
ImageColumns<TColor> columns = image.Columns;
|
||||
for (int x = 0; x < columns.Count; x++)
|
||||
{
|
||||
ReadOnlyRefEnumerable<TColor> column = columns[x];
|
||||
ImageColumn<TColor> column = columns[x];
|
||||
foreach (TColor color in column)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
@ -218,10 +338,10 @@ public static class BlackBarDetection
|
||||
public static int CalculateRight<TColor>(this RefImage<TColor> image, int threshold)
|
||||
where TColor : struct, IColor
|
||||
{
|
||||
RefImage<TColor>.ImageColumns columns = image.Columns;
|
||||
ImageColumns<TColor> columns = image.Columns;
|
||||
for (int x = columns.Count - 1; x >= 0; x--)
|
||||
{
|
||||
ReadOnlyRefEnumerable<TColor> column = columns[x];
|
||||
ImageColumn<TColor> column = columns[x];
|
||||
foreach (TColor color in column)
|
||||
{
|
||||
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using HPPH;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
@ -89,7 +90,7 @@ public abstract class AbstractScreenCapture<TColor> : IScreenCapture
|
||||
/// </summary>
|
||||
/// <param name="captureZone">The capture zone to update.</param>
|
||||
/// <param name="buffer">The buffer containing the current pixel-data of the capture zone.</param>
|
||||
protected abstract void PerformCaptureZoneUpdate(CaptureZone<TColor> captureZone, in Span<byte> buffer);
|
||||
protected abstract void PerformCaptureZoneUpdate(CaptureZone<TColor> captureZone, Span<byte> buffer);
|
||||
|
||||
/// <summary>
|
||||
/// Raises the <see cref="Updated"/>-event.
|
||||
|
||||
@ -4,6 +4,7 @@ using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
using HPPH;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
@ -20,21 +21,12 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
||||
/// <inheritdoc />
|
||||
public Display Display { get; }
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public ColorFormat ColorFormat
|
||||
public IColorFormat ColorFormat
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => TColor.ColorFormat;
|
||||
}
|
||||
#else
|
||||
/// <inheritdoc />
|
||||
public ColorFormat ColorFormat
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => default(TColor).Net6ColorFormat;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <inheritdoc />
|
||||
public int X { get; internal set; }
|
||||
@ -83,19 +75,27 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="RefImage{TColor}"/>. Basically the same as <see cref="Image"/> but with better performance.
|
||||
/// Gets a <see cref="IImage{TColor}"/>.
|
||||
/// </summary>
|
||||
public RefImage<TColor> Image
|
||||
public IImage<TColor> Image
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new(Pixels, 0, 0, Width, Height, Width);
|
||||
get
|
||||
{
|
||||
lock (_lock)
|
||||
return Image<TColor>.Create(RawBuffer, Width, Height, Stride);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IImage ICaptureZone.Image
|
||||
IImage ICaptureZone.Image => Image;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a <see cref="RefImage{TColor}"/>.
|
||||
/// </summary>
|
||||
public RefImage<TColor> RefImage
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new Image<TColor>(InternalBuffer, 0, 0, Width, Height, Width);
|
||||
get => RefImage<TColor>.Wrap(RawBuffer, Width, Height, Stride);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -104,7 +104,7 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
||||
/// <inheritdoc />
|
||||
public bool IsUpdateRequested { get; private set; }
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Events
|
||||
|
||||
@ -151,8 +151,7 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
||||
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);
|
||||
return RefImage<T>.Wrap(RawBuffer, Width, Height, Stride);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -201,6 +200,7 @@ public sealed class CaptureZone<TColor> : ICaptureZone
|
||||
|
||||
#region Constructors
|
||||
|
||||
// ReSharper disable once ConvertToPrimaryConstructor
|
||||
public UnlockDisposable(object @lock) => this._lock = @lock;
|
||||
~UnlockDisposable() => Dispose();
|
||||
|
||||
|
||||
@ -1,71 +0,0 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 32 bit ABGR-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorABGR : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.ABGR;
|
||||
|
||||
#if !NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public ColorFormat Net6ColorFormat => ColorFormat;
|
||||
#endif
|
||||
|
||||
private readonly byte _a;
|
||||
private readonly byte _b;
|
||||
private readonly byte _g;
|
||||
private readonly byte _r;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte A => _a;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorABGR"/> class.
|
||||
/// </summary>
|
||||
/// <param name="a">The alpha-component of the color.</param>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
public ColorABGR(byte a, byte b, byte g, byte r)
|
||||
{
|
||||
this._a = a;
|
||||
this._b = b;
|
||||
this._g = g;
|
||||
this._r = r;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,71 +0,0 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 32 bit ARGB-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorARGB : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.ARGB;
|
||||
|
||||
#if !NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public ColorFormat Net6ColorFormat => ColorFormat;
|
||||
#endif
|
||||
|
||||
private readonly byte _a;
|
||||
private readonly byte _r;
|
||||
private readonly byte _g;
|
||||
private readonly byte _b;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte A => _a;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorARGB"/> class.
|
||||
/// </summary>
|
||||
/// <param name="a">The alpha-component of the color.</param>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
public ColorARGB(byte a, byte r, byte g, byte b)
|
||||
{
|
||||
this._a = a;
|
||||
this._r = r;
|
||||
this._g = g;
|
||||
this._b = b;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,68 +0,0 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 24 bit BGR-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorBGR : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.BGR;
|
||||
|
||||
#if !NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public ColorFormat Net6ColorFormat => ColorFormat;
|
||||
#endif
|
||||
|
||||
private readonly byte _b;
|
||||
private readonly byte _g;
|
||||
private readonly byte _r;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte A => byte.MaxValue;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorBGR"/> class.
|
||||
/// </summary>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
public ColorBGR(byte b, byte g, byte r)
|
||||
{
|
||||
this._b = b;
|
||||
this._g = g;
|
||||
this._r = r;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {A}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,71 +0,0 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 32 bit BGRA-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorBGRA : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.BGRA;
|
||||
|
||||
#if !NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public ColorFormat Net6ColorFormat => ColorFormat;
|
||||
#endif
|
||||
|
||||
private readonly byte _b;
|
||||
private readonly byte _g;
|
||||
private readonly byte _r;
|
||||
private readonly byte _a;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte A => _a;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorBGRA"/> class.
|
||||
/// </summary>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
/// <param name="a">The alpha-component of the color.</param>
|
||||
public ColorBGRA(byte b, byte g, byte r, byte a)
|
||||
{
|
||||
this._b = b;
|
||||
this._g = g;
|
||||
this._r = r;
|
||||
this._a = a;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,58 +0,0 @@
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color format.
|
||||
/// </summary>
|
||||
public readonly struct ColorFormat
|
||||
{
|
||||
#region Instances
|
||||
|
||||
public static readonly ColorFormat BGRA = new(1, 4);
|
||||
public static readonly ColorFormat ABGR = new(2, 4);
|
||||
public static readonly ColorFormat RGBA = new(3, 4);
|
||||
public static readonly ColorFormat ARGB = new(4, 4);
|
||||
public static readonly ColorFormat BGR = new(5, 3);
|
||||
public static readonly ColorFormat RGB = new(6, 3);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Id of the color-format.
|
||||
/// </summary>
|
||||
public readonly int Id;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Bytes per pixel for this color-format.
|
||||
/// </summary>
|
||||
public readonly int BytesPerPixel;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
private ColorFormat(int id, int bytesPerPixel)
|
||||
{
|
||||
this.Id = id;
|
||||
this.BytesPerPixel = bytesPerPixel;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public bool Equals(ColorFormat other) => Id == other.Id;
|
||||
public override bool Equals(object? obj) => obj is ColorFormat other && Equals(other);
|
||||
|
||||
public override int GetHashCode() => Id;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Operators
|
||||
|
||||
public static bool operator ==(ColorFormat left, ColorFormat right) => left.Equals(right);
|
||||
public static bool operator !=(ColorFormat left, ColorFormat right) => !(left == right);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,68 +0,0 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 24 bit RGB-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorRGB : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.RGB;
|
||||
|
||||
#if !NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public ColorFormat Net6ColorFormat => ColorFormat;
|
||||
#endif
|
||||
|
||||
private readonly byte _r;
|
||||
private readonly byte _g;
|
||||
private readonly byte _b;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte A => byte.MaxValue;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorRGB"/> class.
|
||||
/// </summary>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
public ColorRGB(byte r, byte g, byte b)
|
||||
{
|
||||
this._r = r;
|
||||
this._g = g;
|
||||
this._b = b;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {A}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,71 +0,0 @@
|
||||
// ReSharper disable ConvertToAutoProperty
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a color in 32 bit RGBA-format.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")]
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
public readonly struct ColorRGBA : IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <inheritdoc />
|
||||
public static ColorFormat ColorFormat => ColorFormat.RGBA;
|
||||
|
||||
#if !NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public ColorFormat Net6ColorFormat => ColorFormat;
|
||||
#endif
|
||||
|
||||
private readonly byte _r;
|
||||
private readonly byte _g;
|
||||
private readonly byte _b;
|
||||
private readonly byte _a;
|
||||
|
||||
// ReSharper disable ConvertToAutoPropertyWhenPossible
|
||||
/// <inheritdoc />
|
||||
public byte R => _r;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte G => _g;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte B => _b;
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte A => _a;
|
||||
// ReSharper restore ConvertToAutoPropertyWhenPossible
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ColorRGBA"/> class.
|
||||
/// </summary>
|
||||
/// <param name="r">The red-component of the color.</param>
|
||||
/// <param name="g">The green-component of the color.</param>
|
||||
/// <param name="b">The blue-component of the color.</param>
|
||||
/// <param name="a">The alpha-component of the color.</param>
|
||||
public ColorRGBA(byte r, byte g, byte b, byte a)
|
||||
{
|
||||
this._r = r;
|
||||
this._g = g;
|
||||
this._b = b;
|
||||
this._a = a;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]";
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,38 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a generic color made of 4 bytes (alpha, red, green and blue)
|
||||
/// </summary>
|
||||
public interface IColor
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the red-component of this color.
|
||||
/// </summary>
|
||||
byte R { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the green-component of this color.
|
||||
/// </summary>
|
||||
byte G { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the blue-component of this color.
|
||||
/// </summary>
|
||||
byte B { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the alpha-component of this color.
|
||||
/// </summary>
|
||||
byte A { get; }
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
/// <summary>
|
||||
/// Gets the color-format of this color.
|
||||
/// </summary>
|
||||
public static virtual ColorFormat ColorFormat => throw new NotSupportedException();
|
||||
#else
|
||||
public ColorFormat Net6ColorFormat { get; }
|
||||
#endif
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using HPPH;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
@ -15,7 +16,7 @@ public interface ICaptureZone
|
||||
/// <summary>
|
||||
/// Gets the color-format used in the buffer of this zone.
|
||||
/// </summary>
|
||||
ColorFormat ColorFormat { get; }
|
||||
IColorFormat ColorFormat { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the x-location of the region on the screen.
|
||||
|
||||
@ -1,192 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a image.
|
||||
/// </summary>
|
||||
public interface IImage : IEnumerable<IColor>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the color format used in this image.
|
||||
/// </summary>
|
||||
ColorFormat ColorFormat { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the width of this image.
|
||||
/// </summary>
|
||||
int Width { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the height of this image.
|
||||
/// </summary>
|
||||
int Height { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of this image.
|
||||
/// </summary>
|
||||
int SizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the color at the specified location.
|
||||
/// </summary>
|
||||
/// <param name="x">The X-location to read.</param>
|
||||
/// <param name="y">The Y-location to read.</param>
|
||||
/// <returns>The color at the specified location.</returns>
|
||||
IColor this[int x, int y] { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an image representing the specified location.
|
||||
/// </summary>
|
||||
/// <param name="x">The X-location of the image.</param>
|
||||
/// <param name="y">The Y-location of the image.</param>
|
||||
/// <param name="width">The width of the sub-image.</param>
|
||||
/// <param name="height"></param>
|
||||
/// <returns></returns>
|
||||
IImage this[int x, int y, int width, int height] { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of all rows of this image.
|
||||
/// </summary>
|
||||
IImageRows Rows { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of all columns of this image.
|
||||
/// </summary>
|
||||
IImageColumns Columns { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets an <see cref="RefImage{TColor}"/> representing this <see cref="IImage"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="TColor">The color-type of the iamge.</typeparam>
|
||||
/// <returns>The <inheritdoc cref="RefImage{TColor}"/>.</returns>
|
||||
RefImage<TColor> AsRefImage<TColor>() where TColor : struct, IColor;
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="IImage"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImage"/> instance.
|
||||
/// </exception>
|
||||
void CopyTo(in Span<byte> destination);
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new array and copies this <see cref="IImage"/> into it.
|
||||
/// </summary>
|
||||
/// <returns>The new array containing the data of this <see cref="IImage"/>.</returns>
|
||||
byte[] ToArray();
|
||||
|
||||
/// <summary>
|
||||
/// Represents a list of rows of an image.
|
||||
/// </summary>
|
||||
public interface IImageRows : IEnumerable<IImageRow>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the amount of rows in this list.
|
||||
/// </summary>
|
||||
int Count { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific <see cref="IImageRow"/>.
|
||||
/// </summary>
|
||||
/// <param name="column">The ´row to get.</param>
|
||||
/// <returns>The requested <see cref="IImageRow"/>.</returns>
|
||||
IImageRow this[int column] { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a list of columns of an image.
|
||||
/// </summary>
|
||||
public interface IImageColumns : IEnumerable<IImageColumn>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the amount of columns in this list.
|
||||
/// </summary>
|
||||
int Count { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a specific <see cref="IImageColumn"/>.
|
||||
/// </summary>
|
||||
/// <param name="column">The column to get.</param>
|
||||
/// <returns>The requested <see cref="IImageColumn"/>.</returns>
|
||||
IImageColumn this[int column] { get; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single row of an image.
|
||||
/// </summary>
|
||||
public interface IImageRow : IEnumerable<IColor>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the length of the row.
|
||||
/// </summary>
|
||||
int Length { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of this row.
|
||||
/// </summary>
|
||||
int SizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IColor"/> at the specified location.
|
||||
/// </summary>
|
||||
/// <param name="x">The location to get the color from.</param>
|
||||
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
|
||||
IColor this[int x] { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="IImageRow"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageRow"/> instance.
|
||||
/// </exception>
|
||||
void CopyTo(in Span<byte> destination);
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new array and copies this <see cref="IImageRow"/> into it.
|
||||
/// </summary>
|
||||
/// <returns>The new array containing the data of this <see cref="IImageRow"/>.</returns>
|
||||
byte[] ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Represents a single column of an image.
|
||||
/// </summary>
|
||||
public interface IImageColumn : IEnumerable<IColor>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the length of the column.
|
||||
/// </summary>
|
||||
int Length { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size in bytes of this column.
|
||||
/// </summary>
|
||||
int SizeInBytes { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the <see cref="IColor"/> at the specified location.
|
||||
/// </summary>
|
||||
/// <param name="y">The location to get the color from.</param>
|
||||
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
|
||||
IColor this[int y] { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="IImageColumn"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageColumn"/> instance.
|
||||
/// </exception>
|
||||
void CopyTo(in Span<byte> destination);
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new array and copies this <see cref="IImageColumn"/> into it.
|
||||
/// </summary>
|
||||
/// <returns>The new array containing the data of this <see cref="IImageColumn"/>.</returns>
|
||||
byte[] ToArray();
|
||||
}
|
||||
}
|
||||
@ -1,449 +0,0 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace ScreenCapture.NET;
|
||||
|
||||
/// <inheritdoc />
|
||||
public sealed class Image<TColor> : IImage
|
||||
where TColor : struct, IColor
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly byte[] _buffer;
|
||||
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _stride;
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public ColorFormat ColorFormat
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => TColor.ColorFormat;
|
||||
}
|
||||
#else
|
||||
/// <inheritdoc />
|
||||
public ColorFormat ColorFormat
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => default(TColor).Net6ColorFormat;
|
||||
}
|
||||
#endif
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Width { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Height { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public int SizeInBytes => Width * Height * ColorFormat.BytesPerPixel;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IColor this[int x, int y]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
|
||||
|
||||
return MemoryMarshal.Cast<byte, TColor>(_buffer)[((_y + y) * _stride) + (_x + x)];
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage 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 Image<TColor>(_buffer, _x + x, _y + y, width, height, _stride);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage.IImageRows Rows
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new ImageRows(_buffer, _x, _y, Width, Height, _stride);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage.IImageColumns Columns
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new ImageColumns(_buffer, _x, _y, Width, Height, _stride);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal Image(byte[] buffer, int x, int y, int width, int height, int stride)
|
||||
{
|
||||
this._buffer = buffer;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this.Width = width;
|
||||
this.Height = height;
|
||||
this._stride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public void CopyTo(in Span<byte> 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));
|
||||
|
||||
int targetStride = Width * ColorFormat.BytesPerPixel;
|
||||
IImage.IImageRows rows = Rows;
|
||||
Span<byte> target = destination;
|
||||
foreach (IImage.IImageRow row in rows)
|
||||
{
|
||||
row.CopyTo(target);
|
||||
target = target[targetStride..];
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[] ToArray()
|
||||
{
|
||||
byte[] array = new byte[SizeInBytes];
|
||||
CopyTo(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public RefImage<T> AsRefImage<T>()
|
||||
where T : struct, IColor
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IColor> GetEnumerator()
|
||||
{
|
||||
for (int y = 0; y < Height; y++)
|
||||
for (int x = 0; x < Width; x++)
|
||||
yield return this[x, y];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer-Classes
|
||||
|
||||
/// <inheritdoc />
|
||||
private sealed class ImageRows : IImage.IImageRows
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly byte[] _buffer;
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
private readonly int _height;
|
||||
private readonly int _stride;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Count => _height;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage.IImageRow this[int row]
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((row < 0) || (row >= _height)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new ImageRow(_buffer, (((row + _y) * _stride) + _x), _width);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal ImageRows(byte[] buffer, int x, int y, int width, int height, int stride)
|
||||
{
|
||||
this._buffer = buffer;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
this._stride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IImage.IImageRow> GetEnumerator()
|
||||
{
|
||||
for (int y = 0; y < _height; y++)
|
||||
yield return this[y];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
private sealed class ImageRow : IImage.IImageRow
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly byte[] _buffer;
|
||||
private readonly int _start;
|
||||
private readonly int _length;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Length => _length;
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel;
|
||||
#else
|
||||
/// <inheritdoc />
|
||||
public int SizeInBytes => Length * default(TColor).Net6ColorFormat.BytesPerPixel;
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
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
|
||||
|
||||
/// <inheritdoc />
|
||||
public void CopyTo(in Span<byte> 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));
|
||||
|
||||
MemoryMarshal.Cast<byte, TColor>(_buffer).Slice(_start, _length).CopyTo(MemoryMarshal.Cast<byte, TColor>(destination));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[] ToArray()
|
||||
{
|
||||
byte[] array = new byte[SizeInBytes];
|
||||
CopyTo(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IColor> GetEnumerator()
|
||||
{
|
||||
for (int x = 0; x < _length; x++)
|
||||
yield return this[x];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
private sealed class ImageColumns : IImage.IImageColumns
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly byte[] _buffer;
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _width;
|
||||
private readonly int _height;
|
||||
private readonly int _stride;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Count => _width;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IImage.IImageColumn this[int column]
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((column < 0) || (column >= _width)) throw new IndexOutOfRangeException();
|
||||
|
||||
return new ImageColumn(_buffer, (_y * _stride) + _x + column, _height, _stride);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
internal ImageColumns(byte[] buffer, int x, int y, int width, int height, int stride)
|
||||
{
|
||||
this._buffer = buffer;
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
this._stride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IImage.IImageColumn> GetEnumerator()
|
||||
{
|
||||
for (int y = 0; y < _height; y++)
|
||||
yield return this[y];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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;
|
||||
|
||||
/// <inheritdoc />
|
||||
public int Length => _length;
|
||||
|
||||
#if NET7_0_OR_GREATER
|
||||
/// <inheritdoc />
|
||||
public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel;
|
||||
#else
|
||||
/// <inheritdoc />
|
||||
public int SizeInBytes => Length * default(TColor).Net6ColorFormat.BytesPerPixel;
|
||||
#endif
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
/// <inheritdoc />
|
||||
public IColor this[int y]
|
||||
{
|
||||
get
|
||||
{
|
||||
if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException();
|
||||
|
||||
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
|
||||
return data[y * _step];
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
|
||||
/// <inheritdoc />
|
||||
public void CopyTo(in Span<byte> 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 (_step == 1)
|
||||
_buffer.AsSpan(_start, SizeInBytes).CopyTo(destination);
|
||||
else
|
||||
{
|
||||
ReadOnlySpan<TColor> data = MemoryMarshal.Cast<byte, TColor>(_buffer)[_start..];
|
||||
Span<TColor> target = MemoryMarshal.Cast<byte, TColor>(destination);
|
||||
for (int i = 0; i < Length; i++)
|
||||
target[i] = data[i * _step];
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[] ToArray()
|
||||
{
|
||||
byte[] array = new byte[SizeInBytes];
|
||||
CopyTo(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerator<IColor> GetEnumerator()
|
||||
{
|
||||
for (int y = 0; y < _length; y++)
|
||||
yield return this[y];
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,367 +0,0 @@
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the width of the image.
|
||||
/// </summary>
|
||||
public int Width { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the height of the image.
|
||||
/// </summary>
|
||||
public int Height { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the stride (entries per row) of the underlying buffer.
|
||||
/// Only useful if you want to work with a pinned buffer.
|
||||
/// </summary>
|
||||
public int RawStride { get; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Indexer
|
||||
|
||||
public ref readonly TColor this[int x, int y]
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
if ((x < 0) || (y < 0) || (x >= Width) || (y >= Height)) throw new IndexOutOfRangeException();
|
||||
|
||||
ref TColor r0 = ref MemoryMarshal.GetReference(_pixels);
|
||||
nint offset = (nint)(uint)((_y + y) * RawStride) + (_x + x);
|
||||
return ref Unsafe.Add(ref r0, offset);
|
||||
}
|
||||
}
|
||||
|
||||
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, RawStride);
|
||||
}
|
||||
}
|
||||
|
||||
public ImageRows Rows
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new(_pixels, _x, _y, Width, Height, RawStride);
|
||||
}
|
||||
public ImageColumns Columns
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get => new(_pixels, _x, _y, Width, Height, RawStride);
|
||||
}
|
||||
|
||||
#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.RawStride = stride;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Copies the contents of this <see cref="RefImage{TColor}"/> into a destination <see cref="Span{T}"/> instance.
|
||||
/// </summary>
|
||||
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
|
||||
/// <exception cref="ArgumentException">
|
||||
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="RefImage{TColor}"/> instance.
|
||||
/// </exception>
|
||||
public void CopyTo(in Span<TColor> destination)
|
||||
{
|
||||
if (destination == null) throw new ArgumentNullException(nameof(destination));
|
||||
if (destination.Length < (Width * Height)) throw new ArgumentException("The destination is too small to fit this image.", nameof(destination));
|
||||
|
||||
ImageRows rows = Rows;
|
||||
Span<TColor> target = destination;
|
||||
foreach (ReadOnlyRefEnumerable<TColor> row in rows)
|
||||
{
|
||||
row.CopyTo(target);
|
||||
target = target[Width..];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a new array and copies this <see cref="RefImage{TColor}"/> into it.
|
||||
/// </summary>
|
||||
/// <returns>The new array containing the data of this <see cref="RefImage{TColor}"/>.</returns>
|
||||
public TColor[] ToArray()
|
||||
{
|
||||
TColor[] array = new TColor[Width * Height];
|
||||
CopyTo(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a reference to the first element of this image inside the full image buffer.
|
||||
/// </summary>
|
||||
public ref readonly TColor GetPinnableReference()
|
||||
{
|
||||
if (_pixels.Length == 0)
|
||||
return ref Unsafe.NullRef<TColor>();
|
||||
|
||||
int offset = (_y * RawStride) + _x;
|
||||
return ref MemoryMarshal.GetReference(_pixels[offset..]);
|
||||
}
|
||||
|
||||
/// <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;
|
||||
|
||||
public int Count => _height;
|
||||
|
||||
#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;
|
||||
|
||||
public int Count => _width;
|
||||
|
||||
#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
|
||||
}
|
||||
@ -60,4 +60,8 @@
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="HPPH" Version="1.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@ -1,179 +0,0 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ScreenCapture.NET.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class ImageTest
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private static IScreenCapture? _screenCapture;
|
||||
private static ICaptureZone? _captureZone;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
[ClassInitialize]
|
||||
public static void ClassInit(TestContext _)
|
||||
{
|
||||
_screenCapture = new TestScreenCapture();
|
||||
_captureZone = _screenCapture.RegisterCaptureZone(0, 0, _screenCapture.Display.Width, _screenCapture.Display.Height);
|
||||
_screenCapture.CaptureScreen();
|
||||
}
|
||||
|
||||
[ClassCleanup]
|
||||
public static void ClassCleanup()
|
||||
{
|
||||
_screenCapture?.Dispose();
|
||||
_screenCapture = null;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageFullScreen()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
|
||||
Assert.AreEqual(_captureZone.Width, image.Width);
|
||||
Assert.AreEqual(_captureZone.Height, image.Height);
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), image[x, y]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageInnerFull()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
image = image[0, 0, image.Width, image.Height];
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), image[x, y]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageEnumerator()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
|
||||
int counter = 0;
|
||||
foreach (IColor color in image)
|
||||
{
|
||||
int x = counter % image.Width;
|
||||
int y = counter / image.Width;
|
||||
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), color);
|
||||
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageInnerPartial()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
image = image[163, 280, 720, 13];
|
||||
|
||||
Assert.AreEqual(720, image.Width);
|
||||
Assert.AreEqual(13, image.Height);
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(163 + x, 280 + y), image[x, y]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageInnerInnerPartial()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
image = image[163, 280, 720, 13];
|
||||
image = image[15, 2, 47, 8];
|
||||
|
||||
Assert.AreEqual(47, image.Width);
|
||||
Assert.AreEqual(8, image.Height);
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(178 + x, 282 + y), image[x, y]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageRowIndexer()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
|
||||
Assert.AreEqual(image.Height, image.Rows.Count);
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
{
|
||||
IImage.IImageRow row = image.Rows[y];
|
||||
Assert.AreEqual(image.Width, row.Length);
|
||||
for (int x = 0; x < row.Length; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), row[x]);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageRowEnumerator()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
|
||||
int y = 0;
|
||||
foreach (IImage.IImageRow row in image.Rows)
|
||||
{
|
||||
for (int x = 0; x < row.Length; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), row[x]);
|
||||
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageColumnIndexer()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
|
||||
Assert.AreEqual(image.Width, image.Columns.Count);
|
||||
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
{
|
||||
IImage.IImageColumn column = image.Columns[x];
|
||||
Assert.AreEqual(image.Height, column.Length);
|
||||
for (int y = 0; y < column.Length; y++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), column[y]);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageColumnEnumerator()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
|
||||
int x = 0;
|
||||
foreach (IImage.IImageColumn column in image.Columns)
|
||||
{
|
||||
for (int y = 0; y < column.Length; y++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), column[y]);
|
||||
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestAsRefImage()
|
||||
{
|
||||
IImage image = _captureZone!.Image;
|
||||
image = image[163, 280, 720, 13];
|
||||
image = image[15, 2, 47, 8];
|
||||
|
||||
RefImage<ColorARGB> refImage = image.AsRefImage<ColorARGB>();
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
Assert.AreEqual(image[x, y], refImage[x, y]);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,165 +0,0 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ScreenCapture.NET.Tests;
|
||||
|
||||
[TestClass]
|
||||
public class RefImageTest
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private static TestScreenCapture? _screenCapture;
|
||||
private static CaptureZone<ColorARGB>? _captureZone;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
[ClassInitialize]
|
||||
public static void ClassInit(TestContext _)
|
||||
{
|
||||
_screenCapture = new TestScreenCapture();
|
||||
_captureZone = _screenCapture.RegisterCaptureZone(0, 0, _screenCapture.Display.Width, _screenCapture.Display.Height);
|
||||
_screenCapture.CaptureScreen();
|
||||
}
|
||||
|
||||
[ClassCleanup]
|
||||
public static void ClassCleanup()
|
||||
{
|
||||
_screenCapture?.Dispose();
|
||||
_screenCapture = null;
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageFullScreen()
|
||||
{
|
||||
RefImage<ColorARGB> image = _captureZone!.Image;
|
||||
|
||||
Assert.AreEqual(_captureZone.Width, image.Width);
|
||||
Assert.AreEqual(_captureZone.Height, image.Height);
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), image[x, y]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageInnerFull()
|
||||
{
|
||||
RefImage<ColorARGB> image = _captureZone!.Image;
|
||||
image = image[0, 0, image.Width, image.Height];
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), image[x, y]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageEnumerator()
|
||||
{
|
||||
RefImage<ColorARGB> image = _captureZone!.Image;
|
||||
|
||||
int counter = 0;
|
||||
foreach (ColorARGB color in image)
|
||||
{
|
||||
int x = counter % image.Width;
|
||||
int y = counter / image.Width;
|
||||
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), color);
|
||||
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageInnerPartial()
|
||||
{
|
||||
RefImage<ColorARGB> image = _captureZone!.Image;
|
||||
image = image[163, 280, 720, 13];
|
||||
|
||||
Assert.AreEqual(720, image.Width);
|
||||
Assert.AreEqual(13, image.Height);
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(163 + x, 280 + y), image[x, y]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageInnerInnerPartial()
|
||||
{
|
||||
RefImage<ColorARGB> image = _captureZone!.Image;
|
||||
image = image[163, 280, 720, 13];
|
||||
image = image[15, 2, 47, 8];
|
||||
|
||||
Assert.AreEqual(47, image.Width);
|
||||
Assert.AreEqual(8, image.Height);
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(178 + x, 282 + y), image[x, y]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageRowIndexer()
|
||||
{
|
||||
RefImage<ColorARGB> image = _captureZone!.Image;
|
||||
|
||||
Assert.AreEqual(image.Height, image.Rows.Count);
|
||||
|
||||
for (int y = 0; y < image.Height; y++)
|
||||
{
|
||||
ReadOnlyRefEnumerable<ColorARGB> row = image.Rows[y];
|
||||
Assert.AreEqual(image.Width, row.Length);
|
||||
for (int x = 0; x < row.Length; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), row[x]);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageRowEnumerator()
|
||||
{
|
||||
RefImage<ColorARGB> image = _captureZone!.Image;
|
||||
|
||||
int y = 0;
|
||||
foreach (ReadOnlyRefEnumerable<ColorARGB> row in image.Rows)
|
||||
{
|
||||
for (int x = 0; x < row.Length; x++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), row[x]);
|
||||
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageColumnIndexer()
|
||||
{
|
||||
RefImage<ColorARGB> image = _captureZone!.Image;
|
||||
|
||||
Assert.AreEqual(image.Width, image.Columns.Count);
|
||||
|
||||
for (int x = 0; x < image.Width; x++)
|
||||
{
|
||||
ReadOnlyRefEnumerable<ColorARGB> column = image.Columns[x];
|
||||
Assert.AreEqual(image.Height, column.Length);
|
||||
for (int y = 0; y < column.Length; y++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), column[y]);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestImageColumnEnumerator()
|
||||
{
|
||||
RefImage<ColorARGB> image = _captureZone!.Image;
|
||||
|
||||
int x = 0;
|
||||
foreach (ReadOnlyRefEnumerable<ColorARGB> column in image.Columns)
|
||||
{
|
||||
for (int y = 0; y < column.Length; y++)
|
||||
Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), column[y]);
|
||||
|
||||
x++;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
using HPPH;
|
||||
|
||||
namespace ScreenCapture.NET.Tests;
|
||||
|
||||
@ -17,7 +18,7 @@ internal class TestScreenCapture : AbstractScreenCapture<ColorARGB>
|
||||
|
||||
protected override bool PerformScreenCapture() => true;
|
||||
|
||||
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorARGB> captureZone, in Span<byte> buffer)
|
||||
protected override void PerformCaptureZoneUpdate(CaptureZone<ColorARGB> captureZone, Span<byte> buffer)
|
||||
{
|
||||
Span<ColorARGB> pixels = MemoryMarshal.Cast<byte, ColorARGB>(buffer);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user