From c30865aec6f519e39e55772c6582eead103d8f50 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 21 Jul 2024 15:36:20 +0200 Subject: [PATCH 1/7] Removed support for older .net versions --- ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj | 2 +- ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj | 2 +- ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj | 2 +- ScreenCapture.NET/ScreenCapture.NET.csproj | 2 +- Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj b/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj index 185819b..ed4e237 100644 --- a/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj +++ b/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj @@ -1,6 +1,6 @@  - net8.0;net7.0;net6.0 + net8.0 win-x64 latest enable diff --git a/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj b/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj index 59fc73f..5bd4b8e 100644 --- a/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj +++ b/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj @@ -1,6 +1,6 @@  - net8.0;net7.0;net6.0 + net8.0 win-x64 latest enable diff --git a/ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj b/ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj index 44f8dd7..5dbd69c 100644 --- a/ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj +++ b/ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj @@ -1,6 +1,6 @@  - net8.0;net7.0;net6.0 + net8.0 linux-x64 latest enable diff --git a/ScreenCapture.NET/ScreenCapture.NET.csproj b/ScreenCapture.NET/ScreenCapture.NET.csproj index 05ecc8d..5fa4a96 100644 --- a/ScreenCapture.NET/ScreenCapture.NET.csproj +++ b/ScreenCapture.NET/ScreenCapture.NET.csproj @@ -1,6 +1,6 @@  - net8.0;net7.0;net6.0 + net8.0 latest enable true diff --git a/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj b/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj index bae3f9c..5ac3e11 100644 --- a/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj +++ b/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj @@ -1,7 +1,7 @@ - net8.0;net7.0;net6.0 + net8.0 enable false From 4a794409c43a040bc07690e311e9bf120b9356ae Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 21 Jul 2024 22:29:33 +0200 Subject: [PATCH 2/7] Changed all image-based things to use HPPH --- ScreenCapture.NET.DX11/DX11ScreenCapture.cs | 13 +- ScreenCapture.NET.DX9/DX9ScreenCapture.cs | 47 +- .../Downscale/AverageByteSampler.cs | 149 ------ .../Downscale/SamplerInfo.cs | 67 --- .../Downscale/AverageByteSampler.cs | 149 ------ .../Downscale/SamplerInfo.cs | 67 --- ScreenCapture.NET.X11/X11.cs | 68 +-- ScreenCapture.NET.X11/X11ScreenCapture.cs | 47 +- .../ReadOnlyRefEnumerable.cs | 266 ----------- .../Extensions/BlackBarDetection.cs | 154 +++++- .../Generic/AbstractScreenCapture.cs | 3 +- ScreenCapture.NET/Model/CaptureZone.cs | 38 +- ScreenCapture.NET/Model/Colors/ColorABGR.cs | 71 --- ScreenCapture.NET/Model/Colors/ColorARGB.cs | 71 --- ScreenCapture.NET/Model/Colors/ColorBGR.cs | 68 --- ScreenCapture.NET/Model/Colors/ColorBGRA.cs | 71 --- ScreenCapture.NET/Model/Colors/ColorFormat.cs | 58 --- ScreenCapture.NET/Model/Colors/ColorRGB.cs | 68 --- ScreenCapture.NET/Model/Colors/ColorRGBA.cs | 71 --- ScreenCapture.NET/Model/Colors/IColor.cs | 38 -- ScreenCapture.NET/Model/ICaptureZone.cs | 3 +- ScreenCapture.NET/Model/IImage.cs | 192 -------- ScreenCapture.NET/Model/Image.cs | 449 ------------------ ScreenCapture.NET/Model/RefImage.cs | 367 -------------- ScreenCapture.NET/ScreenCapture.NET.csproj | 4 + Tests/ScreenCapture.NET.Tests/ImageTest.cs | 179 ------- Tests/ScreenCapture.NET.Tests/RefImageTest.cs | 165 ------- .../TestScreenCapture.cs | 3 +- 28 files changed, 192 insertions(+), 2754 deletions(-) delete mode 100644 ScreenCapture.NET.DX9/Downscale/AverageByteSampler.cs delete mode 100644 ScreenCapture.NET.DX9/Downscale/SamplerInfo.cs delete mode 100644 ScreenCapture.NET.X11/Downscale/AverageByteSampler.cs delete mode 100644 ScreenCapture.NET.X11/Downscale/SamplerInfo.cs delete mode 100644 ScreenCapture.NET/CommunityToolkit.HighPerformance/ReadOnlyRefEnumerable.cs delete mode 100644 ScreenCapture.NET/Model/Colors/ColorABGR.cs delete mode 100644 ScreenCapture.NET/Model/Colors/ColorARGB.cs delete mode 100644 ScreenCapture.NET/Model/Colors/ColorBGR.cs delete mode 100644 ScreenCapture.NET/Model/Colors/ColorBGRA.cs delete mode 100644 ScreenCapture.NET/Model/Colors/ColorFormat.cs delete mode 100644 ScreenCapture.NET/Model/Colors/ColorRGB.cs delete mode 100644 ScreenCapture.NET/Model/Colors/ColorRGBA.cs delete mode 100644 ScreenCapture.NET/Model/Colors/IColor.cs delete mode 100644 ScreenCapture.NET/Model/IImage.cs delete mode 100644 ScreenCapture.NET/Model/Image.cs delete mode 100644 ScreenCapture.NET/Model/RefImage.cs delete mode 100644 Tests/ScreenCapture.NET.Tests/ImageTest.cs delete mode 100644 Tests/ScreenCapture.NET.Tests/RefImageTest.cs diff --git a/ScreenCapture.NET.DX11/DX11ScreenCapture.cs b/ScreenCapture.NET.DX11/DX11ScreenCapture.cs index 66d6059..eebd50a 100644 --- a/ScreenCapture.NET.DX11/DX11ScreenCapture.cs +++ b/ScreenCapture.NET.DX11/DX11ScreenCapture.cs @@ -3,6 +3,7 @@ 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 @@ protected override bool PerformScreenCapture() } /// - protected override void PerformCaptureZoneUpdate(CaptureZone captureZone, in Span buffer) + protected override void PerformCaptureZoneUpdate(CaptureZone captureZone, Span buffer) { if (_context == null) return; @@ -188,7 +189,7 @@ protected override void PerformCaptureZoneUpdate(CaptureZone captureZ } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void CopyRotate0(in ReadOnlySpan source, int sourceStride, in CaptureZone captureZone, in Span buffer) + private static void CopyRotate0(ReadOnlySpan source, int sourceStride, CaptureZone captureZone, Span buffer) { int height = captureZone.Height; int stride = captureZone.Stride; @@ -204,7 +205,7 @@ private static void CopyRotate0(in ReadOnlySpan source, int sourceStride, } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void CopyRotate90(in ReadOnlySpan source, int sourceStride, in CaptureZone captureZone, in Span buffer) + private static void CopyRotate90(ReadOnlySpan source, int sourceStride, CaptureZone captureZone, Span buffer) { int width = captureZone.Width; int height = captureZone.Height; @@ -220,7 +221,7 @@ private static void CopyRotate90(in ReadOnlySpan source, int sourceStride, } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void CopyRotate180(in ReadOnlySpan source, int sourceStride, in CaptureZone captureZone, in Span buffer) + private static void CopyRotate180(ReadOnlySpan source, int sourceStride, CaptureZone captureZone, Span buffer) { int width = captureZone.Width; int height = captureZone.Height; @@ -237,7 +238,7 @@ private static void CopyRotate180(in ReadOnlySpan source, int sourceStride } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void CopyRotate270(in ReadOnlySpan source, int sourceStride, in CaptureZone captureZone, in Span buffer) + private static void CopyRotate270(ReadOnlySpan source, int sourceStride, CaptureZone captureZone, Span buffer) { int width = captureZone.Width; int height = captureZone.Height; @@ -309,7 +310,7 @@ protected override void ValidateCaptureZoneAndThrow(int x, int y, int width, int base.ValidateCaptureZoneAndThrow(x, y, width, height, downscaleLevel); } - private void InitializeCaptureZone(in CaptureZone captureZone) + private void InitializeCaptureZone(CaptureZone captureZone) { int x; int y; diff --git a/ScreenCapture.NET.DX9/DX9ScreenCapture.cs b/ScreenCapture.NET.DX9/DX9ScreenCapture.cs index bd8f4d3..94ffcee 100644 --- a/ScreenCapture.NET.DX9/DX9ScreenCapture.cs +++ b/ScreenCapture.NET.DX9/DX9ScreenCapture.cs @@ -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 @@ protected override bool PerformScreenCapture() } /// - protected override void PerformCaptureZoneUpdate(CaptureZone captureZone, in Span buffer) + protected override void PerformCaptureZoneUpdate(CaptureZone captureZone, Span buffer) { if (_buffer == null) return; @@ -106,55 +106,26 @@ protected override void PerformCaptureZoneUpdate(CaptureZone captureZ } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void CopyZone(CaptureZone captureZone, in Span buffer) + private void CopyZone(CaptureZone captureZone, Span buffer) { - ReadOnlySpan source = MemoryMarshal.Cast(_buffer); - Span target = MemoryMarshal.Cast(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.Wrap(_buffer, Display.Width, Display.Height, _stride)[captureZone.X, captureZone.Y, captureZone.Width, captureZone.Height] + .CopyTo(MemoryMarshal.Cast(buffer)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DownscaleZone(CaptureZone captureZone, in Span buffer) + private void DownscaleZone(CaptureZone captureZone, Span buffer) { - ReadOnlySpan source = _buffer; - Span target = buffer; + RefImage source = RefImage.Wrap(_buffer, Display.Width, Display.Height, _stride)[captureZone.X, captureZone.Y, captureZone.UnscaledWidth, captureZone.UnscaledHeight]; + Span target = MemoryMarshal.Cast(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 scaleBuffer = stackalloc byte[bpp]; for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) - { - AverageByteSampler.Sample(new SamplerInfo((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(); } /// diff --git a/ScreenCapture.NET.DX9/Downscale/AverageByteSampler.cs b/ScreenCapture.NET.DX9/Downscale/AverageByteSampler.cs deleted file mode 100644 index e8e7423..0000000 --- a/ScreenCapture.NET.DX9/Downscale/AverageByteSampler.cs +++ /dev/null @@ -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; - -/// -/// Represents a sampled that averages multiple byte-data entries. -/// -internal static class AverageByteSampler -{ - #region Constants - - private static readonly int INT_VECTOR_LENGTH = Vector.Count; - - #endregion - - #region Methods - - public static unsafe void Sample(in SamplerInfo info, in Span pixelData) - { - int count = info.Width * info.Height; - if (count == 0) return; - - int dataLength = pixelData.Length; - Span sums = stackalloc uint[dataLength]; - - int elementsPerVector = Vector.Count / dataLength; - int valuesPerVector = elementsPerVector * dataLength; - if (Vector.IsHardwareAccelerated && (info.Height > 1) && (info.Width >= valuesPerVector) && (dataLength <= Vector.Count)) - { - int chunks = info.Width / elementsPerVector; - - Vector sum1 = Vector.Zero; - Vector sum2 = Vector.Zero; - Vector sum3 = Vector.Zero; - Vector sum4 = Vector.Zero; - - for (int y = 0; y < info.Height; y++) - { - ReadOnlySpan data = info[y]; - - fixed (byte* colorPtr = data) - { - byte* current = colorPtr; - for (int i = 0; i < chunks; i++) - { - Vector bytes = *(Vector*)current; - Vector.Widen(bytes, out Vector short1, out Vector short2); - Vector.Widen(short1, out Vector int1, out Vector int2); - Vector.Widen(short2, out Vector int3, out Vector 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 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 -} \ No newline at end of file diff --git a/ScreenCapture.NET.DX9/Downscale/SamplerInfo.cs b/ScreenCapture.NET.DX9/Downscale/SamplerInfo.cs deleted file mode 100644 index 1862de2..0000000 --- a/ScreenCapture.NET.DX9/Downscale/SamplerInfo.cs +++ /dev/null @@ -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; - -/// -/// Represents the information used to sample data. -/// -/// The type of the data to sample. -internal readonly ref struct SamplerInfo -{ - #region Properties & Fields - - private readonly ReadOnlySpan _data; - private readonly int _x; - private readonly int _y; - private readonly int _stride; - private readonly int _dataPerPixel; - private readonly int _dataWidth; - - /// - /// Gets the width of the region the data comes from. - /// - public readonly int Width; - - /// - /// Gets the height of region the data comes from. - /// - public readonly int Height; - - /// - /// Gets the data for the requested row. - /// - /// The row to get the data for. - /// A readonly span containing the data of the row. - public ReadOnlySpan this[int row] => _data.Slice((((_y + row) * _stride) + _x) * _dataPerPixel, _dataWidth); - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The X-location of the region the data comes from. - /// The Y-location of the region the data comes from. - /// The width of the region the data comes from. - /// The height of region the data comes from. - /// The number of pixels in a row of data. - /// The number of {T} representing a single pixel. - /// The data to sample. - public SamplerInfo(int x, int y, int width, int height, int stride, int dataPerPixel, in ReadOnlySpan 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 -} \ No newline at end of file diff --git a/ScreenCapture.NET.X11/Downscale/AverageByteSampler.cs b/ScreenCapture.NET.X11/Downscale/AverageByteSampler.cs deleted file mode 100644 index e8e7423..0000000 --- a/ScreenCapture.NET.X11/Downscale/AverageByteSampler.cs +++ /dev/null @@ -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; - -/// -/// Represents a sampled that averages multiple byte-data entries. -/// -internal static class AverageByteSampler -{ - #region Constants - - private static readonly int INT_VECTOR_LENGTH = Vector.Count; - - #endregion - - #region Methods - - public static unsafe void Sample(in SamplerInfo info, in Span pixelData) - { - int count = info.Width * info.Height; - if (count == 0) return; - - int dataLength = pixelData.Length; - Span sums = stackalloc uint[dataLength]; - - int elementsPerVector = Vector.Count / dataLength; - int valuesPerVector = elementsPerVector * dataLength; - if (Vector.IsHardwareAccelerated && (info.Height > 1) && (info.Width >= valuesPerVector) && (dataLength <= Vector.Count)) - { - int chunks = info.Width / elementsPerVector; - - Vector sum1 = Vector.Zero; - Vector sum2 = Vector.Zero; - Vector sum3 = Vector.Zero; - Vector sum4 = Vector.Zero; - - for (int y = 0; y < info.Height; y++) - { - ReadOnlySpan data = info[y]; - - fixed (byte* colorPtr = data) - { - byte* current = colorPtr; - for (int i = 0; i < chunks; i++) - { - Vector bytes = *(Vector*)current; - Vector.Widen(bytes, out Vector short1, out Vector short2); - Vector.Widen(short1, out Vector int1, out Vector int2); - Vector.Widen(short2, out Vector int3, out Vector 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 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 -} \ No newline at end of file diff --git a/ScreenCapture.NET.X11/Downscale/SamplerInfo.cs b/ScreenCapture.NET.X11/Downscale/SamplerInfo.cs deleted file mode 100644 index 1862de2..0000000 --- a/ScreenCapture.NET.X11/Downscale/SamplerInfo.cs +++ /dev/null @@ -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; - -/// -/// Represents the information used to sample data. -/// -/// The type of the data to sample. -internal readonly ref struct SamplerInfo -{ - #region Properties & Fields - - private readonly ReadOnlySpan _data; - private readonly int _x; - private readonly int _y; - private readonly int _stride; - private readonly int _dataPerPixel; - private readonly int _dataWidth; - - /// - /// Gets the width of the region the data comes from. - /// - public readonly int Width; - - /// - /// Gets the height of region the data comes from. - /// - public readonly int Height; - - /// - /// Gets the data for the requested row. - /// - /// The row to get the data for. - /// A readonly span containing the data of the row. - public ReadOnlySpan this[int row] => _data.Slice((((_y + row) * _stride) + _x) * _dataPerPixel, _dataWidth); - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The X-location of the region the data comes from. - /// The Y-location of the region the data comes from. - /// The width of the region the data comes from. - /// The height of region the data comes from. - /// The number of pixels in a row of data. - /// The number of {T} representing a single pixel. - /// The data to sample. - public SamplerInfo(int x, int y, int width, int height, int stride, int dataPerPixel, in ReadOnlySpan 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 -} \ No newline at end of file diff --git a/ScreenCapture.NET.X11/X11.cs b/ScreenCapture.NET.X11/X11.cs index 85b8ec3..9439b7a 100644 --- a/ScreenCapture.NET.X11/X11.cs +++ b/ScreenCapture.NET.X11/X11.cs @@ -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 unsafe struct XImage 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 \ No newline at end of file +} \ No newline at end of file diff --git a/ScreenCapture.NET.X11/X11ScreenCapture.cs b/ScreenCapture.NET.X11/X11ScreenCapture.cs index 4ff48c8..c797c75 100644 --- a/ScreenCapture.NET.X11/X11ScreenCapture.cs +++ b/ScreenCapture.NET.X11/X11ScreenCapture.cs @@ -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 @@ protected override bool PerformScreenCapture() } /// - protected override void PerformCaptureZoneUpdate(CaptureZone captureZone, in Span buffer) + protected override void PerformCaptureZoneUpdate(CaptureZone captureZone, Span buffer) { using IDisposable @lock = captureZone.Lock(); { @@ -71,55 +71,26 @@ protected override void PerformCaptureZoneUpdate(CaptureZone captureZ } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void CopyZone(CaptureZone captureZone, in Span buffer) + private void CopyZone(CaptureZone captureZone, Span buffer) { - ReadOnlySpan source = MemoryMarshal.Cast(Data); - Span target = MemoryMarshal.Cast(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.Wrap(Data, Display.Width, Display.Height, _image.bytes_per_line)[captureZone.X, captureZone.Y, captureZone.Width, captureZone.Height] + .CopyTo(MemoryMarshal.Cast(buffer)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DownscaleZone(CaptureZone captureZone, in Span buffer) + private void DownscaleZone(CaptureZone captureZone, Span buffer) { - ReadOnlySpan source = Data; - Span target = buffer; + RefImage source = RefImage.Wrap(Data, Display.Width, Display.Height, _image.bytes_per_line)[captureZone.X, captureZone.Y, captureZone.UnscaledWidth, captureZone.UnscaledHeight]; + Span target = MemoryMarshal.Cast(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 scaleBuffer = stackalloc byte[bpp]; for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) - { - AverageByteSampler.Sample(new SamplerInfo((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(); } /// diff --git a/ScreenCapture.NET/CommunityToolkit.HighPerformance/ReadOnlyRefEnumerable.cs b/ScreenCapture.NET/CommunityToolkit.HighPerformance/ReadOnlyRefEnumerable.cs deleted file mode 100644 index 6dda976..0000000 --- a/ScreenCapture.NET/CommunityToolkit.HighPerformance/ReadOnlyRefEnumerable.cs +++ /dev/null @@ -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; - -/// -/// A that iterates readonly items from arbitrary memory locations. -/// -/// The type of items to enumerate. -public readonly ref struct ReadOnlyRefEnumerable -{ - #region Properties & Fields - - /// - /// The instance pointing to the first item in the target memory area. - /// - /// The field maps to the total available length. - private readonly ReadOnlySpan _span; - - /// - /// The distance between items in the sequence to enumerate. - /// - /// The distance refers to items, not byte offset. - private readonly int _step; - - /// - /// Gets the total available length for the sequence. - /// - public int Length - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => _span.Length; - } - - /// - /// Gets the element at the specified zero-based index. - /// - /// The zero-based index of the element. - /// A reference to the element at the specified index. - /// - /// Thrown when is invalid. - /// - 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; - } - } - - /// - /// Gets the element at the specified zero-based index. - /// - /// The zero-based index of the element. - /// A reference to the element at the specified index. - /// - /// Thrown when is invalid. - /// - public ref readonly T this[Index index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => ref this[index.GetOffset(Length)]; - } - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the struct. - /// - /// A reference to the first item of the sequence. - /// The number of items in the sequence. - /// The distance between items in the sequence to enumerate. - [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 - - /// - [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[] array = new T[length]; - CopyTo(array); - - return array; - } - - /// - /// Copies the contents of this into a destination instance. - /// - /// The destination instance. - /// - /// Thrown when is shorter than the source instance. - /// - public void CopyTo(Span 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); - } - - /// - /// Attempts to copy the current instance to a destination . - /// - /// The target of the copy operation. - /// Whether or not the operation was successful. - public bool TryCopyTo(Span 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 - - /// - /// A custom enumerator type to traverse items within a instance. - /// - public ref struct Enumerator - { - #region Properties & Fields - - /// - private readonly ReadOnlySpan _span; - - /// - private readonly int _step; - - /// - /// The current position in the sequence. - /// - private int _position; - - /// - 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 - - /// - /// Initializes a new instance of the struct. - /// - /// The instance with the info on the items to traverse. - /// The distance between items in the sequence to enumerate. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal Enumerator(ReadOnlySpan span, int step) - { - this._span = span; - this._step = step; - - _position = -1; - } - - #endregion - - #region Methods - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool MoveNext() => ++_position < _span.Length; - - #endregion - } -} diff --git a/ScreenCapture.NET/Extensions/BlackBarDetection.cs b/ScreenCapture.NET/Extensions/BlackBarDetection.cs index a4c218e..99848d8 100644 --- a/ScreenCapture.NET/Extensions/BlackBarDetection.cs +++ b/ScreenCapture.NET/Extensions/BlackBarDetection.cs @@ -1,4 +1,6 @@ -namespace ScreenCapture.NET; +using HPPH; + +namespace ScreenCapture.NET; /// /// Helper-class for black-bar removal. @@ -35,10 +37,10 @@ public static IImage RemoveBlackBars(this IImage image, int threshold = 0, bool /// The row number of the first row with at least one non-black pixel. 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 int CalculateTop(IImage image, int threshold) /// The row number of the last row with at least one non-black pixel. 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 int CalculateBottom(IImage image, int threshold) /// The column number of the first column with at least one non-black pixel. 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 int CalculateLeft(IImage image, int threshold) /// The column number of the last column with at least one non-black pixel. 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 int CalculateRight(IImage image, int threshold) #endregion + + #region Image + + /// + /// Create an image with black bars removed + /// + /// The image the bars are removed from. + /// The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.) + /// A bool indicating if black bars should be removed at the top of the image. + /// A bool indicating if black bars should be removed at the bottom of the image. + /// A bool indicating if black bars should be removed on the left side of the image. + /// A bool indicating if black bars should be removed on the right side of the image. + /// The image with black bars removed. + public static RefImage RemoveBlackBars(this IImage 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]; + } + + /// + /// Calculates the first row starting from the top with at least one non black pixel. + /// + /// The image to check. + /// The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.) + /// The row number of the first row with at least one non-black pixel. + public static int CalculateTop(IImage image, int threshold) + where T : struct, IColor + { + ImageRows rows = image.Rows; + for (int y = 0; y < rows.Count; y++) + { + ImageRow row = rows[y]; + foreach (T color in row) + { + if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold)) + return y; + } + } + + return 0; + } + + /// + /// Calculates the last row starting from the top with at least one non black pixel. + /// + /// The image to check. + /// The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.) + /// The row number of the last row with at least one non-black pixel. + public static int CalculateBottom(IImage image, int threshold) + where T : struct, IColor + { + ImageRows rows = image.Rows; + for (int y = rows.Count - 1; y >= 0; y--) + { + ImageRow row = rows[y]; + foreach (T color in row) + { + if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold)) + return y; + } + } + + return rows.Count; + } + + /// + /// Calculates the first column starting from the left with at least one non black pixel. + /// + /// The image to check. + /// The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.) + /// The column number of the first column with at least one non-black pixel. + public static int CalculateLeft(IImage image, int threshold) + where T : struct, IColor + { + ImageColumns columns = image.Columns; + for (int x = 0; x < columns.Count; x++) + { + ImageColumn column = columns[x]; + foreach (T color in column) + { + if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold)) + return x; + } + } + + return 0; + } + + /// + /// Calculates the last column starting from the top with at least one non black pixel. + /// + /// The image to check. + /// The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.) + /// The column number of the last column with at least one non-black pixel. + public static int CalculateRight(IImage image, int threshold) + where T : struct, IColor + { + ImageColumns columns = image.Columns; + for (int x = columns.Count - 1; x >= 0; x--) + { + ImageColumn 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 /// @@ -149,10 +269,10 @@ public static RefImage RemoveBlackBars(this RefImage ima public static int CalculateTop(this RefImage image, int threshold) where TColor : struct, IColor { - RefImage.ImageRows rows = image.Rows; + ImageRows rows = image.Rows; for (int y = 0; y < rows.Count; y++) { - ReadOnlyRefEnumerable row = rows[y]; + ImageRow row = rows[y]; foreach (TColor color in row) { if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold)) @@ -172,10 +292,10 @@ public static int CalculateTop(this RefImage image, int threshol public static int CalculateBottom(this RefImage image, int threshold) where TColor : struct, IColor { - RefImage.ImageRows rows = image.Rows; + ImageRows rows = image.Rows; for (int y = rows.Count - 1; y >= 0; y--) { - ReadOnlyRefEnumerable row = rows[y]; + ImageRow row = rows[y]; foreach (TColor color in row) { if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold)) @@ -195,10 +315,10 @@ public static int CalculateBottom(this RefImage image, int thres public static int CalculateLeft(this RefImage image, int threshold) where TColor : struct, IColor { - RefImage.ImageColumns columns = image.Columns; + ImageColumns columns = image.Columns; for (int x = 0; x < columns.Count; x++) { - ReadOnlyRefEnumerable column = columns[x]; + ImageColumn column = columns[x]; foreach (TColor color in column) { if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold)) @@ -218,10 +338,10 @@ public static int CalculateLeft(this RefImage image, int thresho public static int CalculateRight(this RefImage image, int threshold) where TColor : struct, IColor { - RefImage.ImageColumns columns = image.Columns; + ImageColumns columns = image.Columns; for (int x = columns.Count - 1; x >= 0; x--) { - ReadOnlyRefEnumerable column = columns[x]; + ImageColumn column = columns[x]; foreach (TColor color in column) { if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold)) diff --git a/ScreenCapture.NET/Generic/AbstractScreenCapture.cs b/ScreenCapture.NET/Generic/AbstractScreenCapture.cs index 9ff4753..4e866c1 100644 --- a/ScreenCapture.NET/Generic/AbstractScreenCapture.cs +++ b/ScreenCapture.NET/Generic/AbstractScreenCapture.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using HPPH; namespace ScreenCapture.NET; @@ -89,7 +90,7 @@ public virtual bool CaptureScreen() /// /// The capture zone to update. /// The buffer containing the current pixel-data of the capture zone. - protected abstract void PerformCaptureZoneUpdate(CaptureZone captureZone, in Span buffer); + protected abstract void PerformCaptureZoneUpdate(CaptureZone captureZone, Span buffer); /// /// Raises the -event. diff --git a/ScreenCapture.NET/Model/CaptureZone.cs b/ScreenCapture.NET/Model/CaptureZone.cs index 7b75431..381feb9 100644 --- a/ScreenCapture.NET/Model/CaptureZone.cs +++ b/ScreenCapture.NET/Model/CaptureZone.cs @@ -4,6 +4,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; +using HPPH; namespace ScreenCapture.NET; @@ -20,21 +21,12 @@ public sealed class CaptureZone : ICaptureZone /// public Display Display { get; } -#if NET7_0_OR_GREATER /// - public ColorFormat ColorFormat + public IColorFormat ColorFormat { [MethodImpl(MethodImplOptions.AggressiveInlining)] get => TColor.ColorFormat; } -#else - /// - public ColorFormat ColorFormat - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => default(TColor).Net6ColorFormat; - } -#endif /// public int X { get; internal set; } @@ -83,19 +75,27 @@ public ReadOnlySpan Pixels } /// - /// Gets a . Basically the same as but with better performance. + /// Gets a . /// - public RefImage Image + public IImage Image { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => new(Pixels, 0, 0, Width, Height, Width); + get + { + lock (_lock) + return Image.Create(RawBuffer, Width, Height, Stride); + } } /// - IImage ICaptureZone.Image + IImage ICaptureZone.Image => Image; + + /// + /// Gets a . + /// + public RefImage RefImage { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => new Image(InternalBuffer, 0, 0, Width, Height, Width); + get => RefImage.Wrap(RawBuffer, Width, Height, Stride); } /// @@ -104,7 +104,7 @@ IImage ICaptureZone.Image /// public bool IsUpdateRequested { get; private set; } -#endregion + #endregion #region Events @@ -151,8 +151,7 @@ public RefImage GetRefImage() 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(MemoryMarshal.Cast(RawBuffer), 0, 0, Width, Height, Width); + return RefImage.Wrap(RawBuffer, Width, Height, Stride); } /// @@ -201,6 +200,7 @@ private class UnlockDisposable : IDisposable #region Constructors + // ReSharper disable once ConvertToPrimaryConstructor public UnlockDisposable(object @lock) => this._lock = @lock; ~UnlockDisposable() => Dispose(); diff --git a/ScreenCapture.NET/Model/Colors/ColorABGR.cs b/ScreenCapture.NET/Model/Colors/ColorABGR.cs deleted file mode 100644 index bf0676c..0000000 --- a/ScreenCapture.NET/Model/Colors/ColorABGR.cs +++ /dev/null @@ -1,71 +0,0 @@ -// ReSharper disable ConvertToAutoProperty - -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace ScreenCapture.NET; - -/// -/// Represents a color in 32 bit ABGR-format. -/// -[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")] -[StructLayout(LayoutKind.Sequential)] -public readonly struct ColorABGR : IColor -{ - #region Properties & Fields - - /// - public static ColorFormat ColorFormat => ColorFormat.ABGR; - -#if !NET7_0_OR_GREATER - /// - public ColorFormat Net6ColorFormat => ColorFormat; -#endif - - private readonly byte _a; - private readonly byte _b; - private readonly byte _g; - private readonly byte _r; - - // ReSharper disable ConvertToAutoPropertyWhenPossible - /// - public byte A => _a; - - /// - public byte B => _b; - - /// - public byte G => _g; - - /// - public byte R => _r; - // ReSharper restore ConvertToAutoPropertyWhenPossible - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The alpha-component of the color. - /// The blue-component of the color. - /// The green-component of the color. - /// The red-component of the color. - 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 - - /// - public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]"; - - #endregion -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/Colors/ColorARGB.cs b/ScreenCapture.NET/Model/Colors/ColorARGB.cs deleted file mode 100644 index 26e6561..0000000 --- a/ScreenCapture.NET/Model/Colors/ColorARGB.cs +++ /dev/null @@ -1,71 +0,0 @@ -// ReSharper disable ConvertToAutoProperty - -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace ScreenCapture.NET; - -/// -/// Represents a color in 32 bit ARGB-format. -/// -[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")] -[StructLayout(LayoutKind.Sequential)] -public readonly struct ColorARGB : IColor -{ - #region Properties & Fields - - /// - public static ColorFormat ColorFormat => ColorFormat.ARGB; - -#if !NET7_0_OR_GREATER - /// - public ColorFormat Net6ColorFormat => ColorFormat; -#endif - - private readonly byte _a; - private readonly byte _r; - private readonly byte _g; - private readonly byte _b; - - // ReSharper disable ConvertToAutoPropertyWhenPossible - /// - public byte A => _a; - - /// - public byte R => _r; - - /// - public byte G => _g; - - /// - public byte B => _b; - // ReSharper restore ConvertToAutoPropertyWhenPossible - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The alpha-component of the color. - /// The red-component of the color. - /// The green-component of the color. - /// The blue-component of the color. - 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 - - /// - public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]"; - - #endregion -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/Colors/ColorBGR.cs b/ScreenCapture.NET/Model/Colors/ColorBGR.cs deleted file mode 100644 index f837f17..0000000 --- a/ScreenCapture.NET/Model/Colors/ColorBGR.cs +++ /dev/null @@ -1,68 +0,0 @@ -// ReSharper disable ConvertToAutoProperty - -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace ScreenCapture.NET; - -/// -/// Represents a color in 24 bit BGR-format. -/// -[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")] -[StructLayout(LayoutKind.Sequential)] -public readonly struct ColorBGR : IColor -{ - #region Properties & Fields - - /// - public static ColorFormat ColorFormat => ColorFormat.BGR; - -#if !NET7_0_OR_GREATER - /// - public ColorFormat Net6ColorFormat => ColorFormat; -#endif - - private readonly byte _b; - private readonly byte _g; - private readonly byte _r; - - // ReSharper disable ConvertToAutoPropertyWhenPossible - /// - public byte A => byte.MaxValue; - - /// - public byte B => _b; - - /// - public byte G => _g; - - /// - public byte R => _r; - // ReSharper restore ConvertToAutoPropertyWhenPossible - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The blue-component of the color. - /// The green-component of the color. - /// The red-component of the color. - public ColorBGR(byte b, byte g, byte r) - { - this._b = b; - this._g = g; - this._r = r; - } - - #endregion - - #region Methods - - /// - public override string ToString() => $"[A: {A}, R: {_r}, G: {_g}, B: {_b}]"; - - #endregion -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/Colors/ColorBGRA.cs b/ScreenCapture.NET/Model/Colors/ColorBGRA.cs deleted file mode 100644 index 0689148..0000000 --- a/ScreenCapture.NET/Model/Colors/ColorBGRA.cs +++ /dev/null @@ -1,71 +0,0 @@ -// ReSharper disable ConvertToAutoProperty - -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace ScreenCapture.NET; - -/// -/// Represents a color in 32 bit BGRA-format. -/// -[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")] -[StructLayout(LayoutKind.Sequential)] -public readonly struct ColorBGRA : IColor -{ - #region Properties & Fields - - /// - public static ColorFormat ColorFormat => ColorFormat.BGRA; - -#if !NET7_0_OR_GREATER - /// - public ColorFormat Net6ColorFormat => ColorFormat; -#endif - - private readonly byte _b; - private readonly byte _g; - private readonly byte _r; - private readonly byte _a; - - // ReSharper disable ConvertToAutoPropertyWhenPossible - /// - public byte B => _b; - - /// - public byte G => _g; - - /// - public byte R => _r; - - /// - public byte A => _a; - // ReSharper restore ConvertToAutoPropertyWhenPossible - -#endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The blue-component of the color. - /// The green-component of the color. - /// The red-component of the color. - /// The alpha-component of the color. - 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 - - /// - public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]"; - - #endregion -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/Colors/ColorFormat.cs b/ScreenCapture.NET/Model/Colors/ColorFormat.cs deleted file mode 100644 index a9f08e6..0000000 --- a/ScreenCapture.NET/Model/Colors/ColorFormat.cs +++ /dev/null @@ -1,58 +0,0 @@ -namespace ScreenCapture.NET; - -/// -/// Represents a color format. -/// -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 - - /// - /// Gets the Id of the color-format. - /// - public readonly int Id; - - /// - /// Gets the Bytes per pixel for this color-format. - /// - 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 -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/Colors/ColorRGB.cs b/ScreenCapture.NET/Model/Colors/ColorRGB.cs deleted file mode 100644 index cbc7e86..0000000 --- a/ScreenCapture.NET/Model/Colors/ColorRGB.cs +++ /dev/null @@ -1,68 +0,0 @@ -// ReSharper disable ConvertToAutoProperty - -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace ScreenCapture.NET; - -/// -/// Represents a color in 24 bit RGB-format. -/// -[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")] -[StructLayout(LayoutKind.Sequential)] -public readonly struct ColorRGB : IColor -{ - #region Properties & Fields - - /// - public static ColorFormat ColorFormat => ColorFormat.RGB; - -#if !NET7_0_OR_GREATER - /// - public ColorFormat Net6ColorFormat => ColorFormat; -#endif - - private readonly byte _r; - private readonly byte _g; - private readonly byte _b; - - // ReSharper disable ConvertToAutoPropertyWhenPossible - /// - public byte A => byte.MaxValue; - - /// - public byte R => _r; - - /// - public byte G => _g; - - /// - public byte B => _b; - // ReSharper restore ConvertToAutoPropertyWhenPossible - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The red-component of the color. - /// The green-component of the color. - /// The blue-component of the color. - public ColorRGB(byte r, byte g, byte b) - { - this._r = r; - this._g = g; - this._b = b; - } - - #endregion - - #region Methods - - /// - public override string ToString() => $"[A: {A}, R: {_r}, G: {_g}, B: {_b}]"; - - #endregion -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/Colors/ColorRGBA.cs b/ScreenCapture.NET/Model/Colors/ColorRGBA.cs deleted file mode 100644 index df3379a..0000000 --- a/ScreenCapture.NET/Model/Colors/ColorRGBA.cs +++ /dev/null @@ -1,71 +0,0 @@ -// ReSharper disable ConvertToAutoProperty - -using System.Diagnostics; -using System.Runtime.InteropServices; - -namespace ScreenCapture.NET; - -/// -/// Represents a color in 32 bit RGBA-format. -/// -[DebuggerDisplay("[A: {A}, R: {R}, G: {G}, B: {B}]")] -[StructLayout(LayoutKind.Sequential)] -public readonly struct ColorRGBA : IColor -{ - #region Properties & Fields - - /// - public static ColorFormat ColorFormat => ColorFormat.RGBA; - -#if !NET7_0_OR_GREATER - /// - public ColorFormat Net6ColorFormat => ColorFormat; -#endif - - private readonly byte _r; - private readonly byte _g; - private readonly byte _b; - private readonly byte _a; - - // ReSharper disable ConvertToAutoPropertyWhenPossible - /// - public byte R => _r; - - /// - public byte G => _g; - - /// - public byte B => _b; - - /// - public byte A => _a; - // ReSharper restore ConvertToAutoPropertyWhenPossible - - #endregion - - #region Constructors - - /// - /// Initializes a new instance of the class. - /// - /// The red-component of the color. - /// The green-component of the color. - /// The blue-component of the color. - /// The alpha-component of the color. - 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 - - /// - public override string ToString() => $"[A: {_a}, R: {_r}, G: {_g}, B: {_b}]"; - - #endregion -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/Colors/IColor.cs b/ScreenCapture.NET/Model/Colors/IColor.cs deleted file mode 100644 index 8a1c3d1..0000000 --- a/ScreenCapture.NET/Model/Colors/IColor.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; - -namespace ScreenCapture.NET; - -/// -/// Represents a generic color made of 4 bytes (alpha, red, green and blue) -/// -public interface IColor -{ - /// - /// Gets the red-component of this color. - /// - byte R { get; } - - /// - /// Gets the green-component of this color. - /// - byte G { get; } - - /// - /// Gets the blue-component of this color. - /// - byte B { get; } - - /// - /// Gets the alpha-component of this color. - /// - byte A { get; } - -#if NET7_0_OR_GREATER - /// - /// Gets the color-format of this color. - /// - public static virtual ColorFormat ColorFormat => throw new NotSupportedException(); -#else - public ColorFormat Net6ColorFormat { get; } -#endif -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/ICaptureZone.cs b/ScreenCapture.NET/Model/ICaptureZone.cs index 915da3a..2b4d78f 100644 --- a/ScreenCapture.NET/Model/ICaptureZone.cs +++ b/ScreenCapture.NET/Model/ICaptureZone.cs @@ -1,4 +1,5 @@ using System; +using HPPH; namespace ScreenCapture.NET; @@ -15,7 +16,7 @@ public interface ICaptureZone /// /// Gets the color-format used in the buffer of this zone. /// - ColorFormat ColorFormat { get; } + IColorFormat ColorFormat { get; } /// /// Gets the x-location of the region on the screen. diff --git a/ScreenCapture.NET/Model/IImage.cs b/ScreenCapture.NET/Model/IImage.cs deleted file mode 100644 index 027b205..0000000 --- a/ScreenCapture.NET/Model/IImage.cs +++ /dev/null @@ -1,192 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace ScreenCapture.NET; - -/// -/// Represents a image. -/// -public interface IImage : IEnumerable -{ - /// - /// Gets the color format used in this image. - /// - ColorFormat ColorFormat { get; } - - /// - /// Gets the width of this image. - /// - int Width { get; } - - /// - /// Gets the height of this image. - /// - int Height { get; } - - /// - /// Gets the size in bytes of this image. - /// - int SizeInBytes { get; } - - /// - /// Gets the color at the specified location. - /// - /// The X-location to read. - /// The Y-location to read. - /// The color at the specified location. - IColor this[int x, int y] { get; } - - /// - /// Gets an image representing the specified location. - /// - /// The X-location of the image. - /// The Y-location of the image. - /// The width of the sub-image. - /// - /// - IImage this[int x, int y, int width, int height] { get; } - - /// - /// Gets a list of all rows of this image. - /// - IImageRows Rows { get; } - - /// - /// Gets a list of all columns of this image. - /// - IImageColumns Columns { get; } - - /// - /// Gets an representing this . - /// - /// The color-type of the iamge. - /// The . - RefImage AsRefImage() where TColor : struct, IColor; - - /// - /// Copies the contents of this into a destination instance. - /// - /// The destination instance. - /// - /// Thrown when is shorter than the source instance. - /// - void CopyTo(in Span destination); - - /// - /// Allocates a new array and copies this into it. - /// - /// The new array containing the data of this . - byte[] ToArray(); - - /// - /// Represents a list of rows of an image. - /// - public interface IImageRows : IEnumerable - { - /// - /// Gets the amount of rows in this list. - /// - int Count { get; } - - /// - /// Gets a specific . - /// - /// The ´row to get. - /// The requested . - IImageRow this[int column] { get; } - } - - /// - /// Represents a list of columns of an image. - /// - public interface IImageColumns : IEnumerable - { - /// - /// Gets the amount of columns in this list. - /// - int Count { get; } - - /// - /// Gets a specific . - /// - /// The column to get. - /// The requested . - IImageColumn this[int column] { get; } - } - - /// - /// Represents a single row of an image. - /// - public interface IImageRow : IEnumerable - { - /// - /// Gets the length of the row. - /// - int Length { get; } - - /// - /// Gets the size in bytes of this row. - /// - int SizeInBytes { get; } - - /// - /// Gets the at the specified location. - /// - /// The location to get the color from. - /// The at the specified location. - IColor this[int x] { get; } - - /// - /// Copies the contents of this into a destination instance. - /// - /// The destination instance. - /// - /// Thrown when is shorter than the source instance. - /// - void CopyTo(in Span destination); - - /// - /// Allocates a new array and copies this into it. - /// - /// The new array containing the data of this . - byte[] ToArray(); - } - - /// - /// Represents a single column of an image. - /// - public interface IImageColumn : IEnumerable - { - /// - /// Gets the length of the column. - /// - int Length { get; } - - /// - /// Gets the size in bytes of this column. - /// - int SizeInBytes { get; } - - /// - /// Gets the at the specified location. - /// - /// The location to get the color from. - /// The at the specified location. - IColor this[int y] { get; } - - /// - /// Copies the contents of this into a destination instance. - /// - /// The destination instance. - /// - /// Thrown when is shorter than the source instance. - /// - void CopyTo(in Span destination); - - /// - /// Allocates a new array and copies this into it. - /// - /// The new array containing the data of this . - byte[] ToArray(); - } -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/Image.cs b/ScreenCapture.NET/Model/Image.cs deleted file mode 100644 index 3197676..0000000 --- a/ScreenCapture.NET/Model/Image.cs +++ /dev/null @@ -1,449 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace ScreenCapture.NET; - -/// -public sealed class Image : 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 - /// - public ColorFormat ColorFormat - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => TColor.ColorFormat; - } -#else - /// - public ColorFormat ColorFormat - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => default(TColor).Net6ColorFormat; - } -#endif - - /// - public int Width { get; } - - /// - public int Height { get; } - - /// - public int SizeInBytes => Width * Height * ColorFormat.BytesPerPixel; - - #endregion - - #region Indexer - - /// - 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(_buffer)[((_y + y) * _stride) + (_x + x)]; - } - } - - /// - 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(_buffer, _x + x, _y + y, width, height, _stride); - } - } - - /// - public IImage.IImageRows Rows - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => new ImageRows(_buffer, _x, _y, Width, Height, _stride); - } - - /// - 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 - - /// - public void CopyTo(in Span 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 target = destination; - foreach (IImage.IImageRow row in rows) - { - row.CopyTo(target); - target = target[targetStride..]; - } - } - - /// - public byte[] ToArray() - { - byte[] array = new byte[SizeInBytes]; - CopyTo(array); - return array; - } - - /// - public RefImage AsRefImage() - 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(MemoryMarshal.Cast(_buffer), _x, _y, Width, Height, _stride); - } - - /// - public IEnumerator GetEnumerator() - { - for (int y = 0; y < Height; y++) - for (int x = 0; x < Width; x++) - yield return this[x, y]; - } - - /// - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - #endregion - - #region Indexer-Classes - - /// - 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; - - /// - public int Count => _height; - - #endregion - - #region Indexer - - /// - 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 - - /// - public IEnumerator GetEnumerator() - { - for (int y = 0; y < _height; y++) - yield return this[y]; - } - - /// - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - #endregion - } - - /// - private sealed class ImageRow : IImage.IImageRow - { - #region Properties & Fields - - private readonly byte[] _buffer; - private readonly int _start; - private readonly int _length; - - /// - public int Length => _length; - -#if NET7_0_OR_GREATER - /// - public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel; -#else - /// - public int SizeInBytes => Length * default(TColor).Net6ColorFormat.BytesPerPixel; -#endif - - #endregion - - #region Indexer - - /// - public IColor this[int x] - { - get - { - if ((x < 0) || (x >= _length)) throw new IndexOutOfRangeException(); - - ReadOnlySpan row = MemoryMarshal.Cast(_buffer)[_start..]; - return row[x]; - } - } - - #endregion - - #region Constructors - - internal ImageRow(byte[] buffer, int start, int length) - { - this._buffer = buffer; - this._start = start; - this._length = length; - } - - #endregion - - #region Methods - - /// - public void CopyTo(in Span 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(_buffer).Slice(_start, _length).CopyTo(MemoryMarshal.Cast(destination)); - } - - /// - public byte[] ToArray() - { - byte[] array = new byte[SizeInBytes]; - CopyTo(array); - return array; - } - - /// - public IEnumerator GetEnumerator() - { - for (int x = 0; x < _length; x++) - yield return this[x]; - } - - /// - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - #endregion - } - - /// - private sealed class ImageColumns : IImage.IImageColumns - { - #region Properties & Fields - - private readonly byte[] _buffer; - private readonly int _x; - private readonly int _y; - private readonly int _width; - private readonly int _height; - private readonly int _stride; - - /// - public int Count => _width; - - #endregion - - #region Indexer - - /// - 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 - - /// - public IEnumerator GetEnumerator() - { - for (int y = 0; y < _height; y++) - yield return this[y]; - } - - /// - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - #endregion - } - - /// - private sealed class ImageColumn : IImage.IImageColumn - { - #region Properties & Fields - - private readonly byte[] _buffer; - private readonly int _start; - private readonly int _length; - private readonly int _step; - - /// - public int Length => _length; - -#if NET7_0_OR_GREATER - /// - public int SizeInBytes => Length * TColor.ColorFormat.BytesPerPixel; -#else - /// - public int SizeInBytes => Length * default(TColor).Net6ColorFormat.BytesPerPixel; -#endif - - #endregion - - #region Indexer - - /// - public IColor this[int y] - { - get - { - if ((y < 0) || (y >= _length)) throw new IndexOutOfRangeException(); - - ReadOnlySpan data = MemoryMarshal.Cast(_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 - - /// - public void CopyTo(in Span 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 data = MemoryMarshal.Cast(_buffer)[_start..]; - Span target = MemoryMarshal.Cast(destination); - for (int i = 0; i < Length; i++) - target[i] = data[i * _step]; - } - } - - /// - public byte[] ToArray() - { - byte[] array = new byte[SizeInBytes]; - CopyTo(array); - return array; - } - - /// - public IEnumerator GetEnumerator() - { - for (int y = 0; y < _length; y++) - yield return this[y]; - } - - /// - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - - #endregion - } - - #endregion -} \ No newline at end of file diff --git a/ScreenCapture.NET/Model/RefImage.cs b/ScreenCapture.NET/Model/RefImage.cs deleted file mode 100644 index 72272c2..0000000 --- a/ScreenCapture.NET/Model/RefImage.cs +++ /dev/null @@ -1,367 +0,0 @@ -using System; -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace ScreenCapture.NET; - -public readonly ref struct RefImage - where TColor : struct, IColor -{ - #region Properties & Fields - - private readonly ReadOnlySpan _pixels; - - private readonly int _x; - private readonly int _y; - - /// - /// Gets the width of the image. - /// - public int Width { get; } - - /// - /// Gets the height of the image. - /// - public int Height { get; } - - /// - /// Gets the stride (entries per row) of the underlying buffer. - /// Only useful if you want to work with a pinned buffer. - /// - 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 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(_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 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 - - /// - /// Copies the contents of this into a destination instance. - /// - /// The destination instance. - /// - /// Thrown when is shorter than the source instance. - /// - public void CopyTo(in Span 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 target = destination; - foreach (ReadOnlyRefEnumerable row in rows) - { - row.CopyTo(target); - target = target[Width..]; - } - } - - /// - /// Allocates a new array and copies this into it. - /// - /// The new array containing the data of this . - public TColor[] ToArray() - { - TColor[] array = new TColor[Width * Height]; - CopyTo(array); - return array; - } - - /// - /// Returns a reference to the first element of this image inside the full image buffer. - /// - public ref readonly TColor GetPinnableReference() - { - if (_pixels.Length == 0) - return ref Unsafe.NullRef(); - - int offset = (_y * RawStride) + _x; - return ref MemoryMarshal.GetReference(_pixels[offset..]); - } - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ImageEnumerator GetEnumerator() => new(_pixels); - - #endregion - - public ref struct ImageEnumerator - { - #region Properties & Fields - - private readonly ReadOnlySpan _pixels; - private int _position; - - /// - public TColor Current - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get => _pixels[_position]; - } - - #endregion - - #region Constructors - - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal ImageEnumerator(ReadOnlySpan pixels) - { - this._pixels = pixels; - - _position = -1; - } - - #endregion - - #region Methods - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool MoveNext() => ++_position < _pixels.Length; - - #endregion - } - - #region Indexer-Structs - - public readonly ref struct ImageRows - { - #region Properties & Fields - - private readonly ReadOnlySpan _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 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(rr, _width, 1); - } - } - - #endregion - - #region Constructors - - public ImageRows(ReadOnlySpan 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 - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ImageRowsEnumerator GetEnumerator() => new(this); - - #endregion - - public ref struct ImageRowsEnumerator - { - #region Properties & Fields - - private readonly ImageRows _rows; - private int _position; - - /// - public ReadOnlyRefEnumerable 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 - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool MoveNext() => ++_position < _rows._height; - - #endregion - } - } - - public readonly ref struct ImageColumns - { - #region Properties & Fields - - private readonly ReadOnlySpan _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 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(rc, _height, _stride); - } - } - - #endregion - - #region Constructors - - public ImageColumns(ReadOnlySpan 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 - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ImageColumnsEnumerator GetEnumerator() => new(this); - - #endregion - - public ref struct ImageColumnsEnumerator - { - #region Properties & Fields - - private readonly ImageColumns _columns; - private int _position; - - /// - public ReadOnlyRefEnumerable 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 - - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool MoveNext() => ++_position < _columns._width; - - #endregion - } - } - - #endregion -} \ No newline at end of file diff --git a/ScreenCapture.NET/ScreenCapture.NET.csproj b/ScreenCapture.NET/ScreenCapture.NET.csproj index 5fa4a96..f33d37d 100644 --- a/ScreenCapture.NET/ScreenCapture.NET.csproj +++ b/ScreenCapture.NET/ScreenCapture.NET.csproj @@ -60,4 +60,8 @@ + + + + diff --git a/Tests/ScreenCapture.NET.Tests/ImageTest.cs b/Tests/ScreenCapture.NET.Tests/ImageTest.cs deleted file mode 100644 index 7834319..0000000 --- a/Tests/ScreenCapture.NET.Tests/ImageTest.cs +++ /dev/null @@ -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 refImage = image.AsRefImage(); - - 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 -} \ No newline at end of file diff --git a/Tests/ScreenCapture.NET.Tests/RefImageTest.cs b/Tests/ScreenCapture.NET.Tests/RefImageTest.cs deleted file mode 100644 index dae38bd..0000000 --- a/Tests/ScreenCapture.NET.Tests/RefImageTest.cs +++ /dev/null @@ -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? _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 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 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 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 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 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 image = _captureZone!.Image; - - Assert.AreEqual(image.Height, image.Rows.Count); - - for (int y = 0; y < image.Height; y++) - { - ReadOnlyRefEnumerable 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 image = _captureZone!.Image; - - int y = 0; - foreach (ReadOnlyRefEnumerable 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 image = _captureZone!.Image; - - Assert.AreEqual(image.Width, image.Columns.Count); - - for (int x = 0; x < image.Width; x++) - { - ReadOnlyRefEnumerable 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 image = _captureZone!.Image; - - int x = 0; - foreach (ReadOnlyRefEnumerable column in image.Columns) - { - for (int y = 0; y < column.Length; y++) - Assert.AreEqual(TestScreenCapture.GetColorFromLocation(x, y), column[y]); - - x++; - } - } - - #endregion -} \ No newline at end of file diff --git a/Tests/ScreenCapture.NET.Tests/TestScreenCapture.cs b/Tests/ScreenCapture.NET.Tests/TestScreenCapture.cs index e1de0e5..66b7b98 100644 --- a/Tests/ScreenCapture.NET.Tests/TestScreenCapture.cs +++ b/Tests/ScreenCapture.NET.Tests/TestScreenCapture.cs @@ -1,5 +1,6 @@ using System; using System.Runtime.InteropServices; +using HPPH; namespace ScreenCapture.NET.Tests; @@ -17,7 +18,7 @@ public TestScreenCapture(Rotation rotation = Rotation.None) protected override bool PerformScreenCapture() => true; - protected override void PerformCaptureZoneUpdate(CaptureZone captureZone, in Span buffer) + protected override void PerformCaptureZoneUpdate(CaptureZone captureZone, Span buffer) { Span pixels = MemoryMarshal.Cast(buffer); From d74f3a6b21032043d565f1c5d61dbe3308f8eb4d Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 21 Jul 2024 22:50:31 +0200 Subject: [PATCH 3/7] Updated all nugets --- ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj | 2 +- ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj | 2 +- .../ScreenCapture.NET.Tests.csproj | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj b/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj index ed4e237..2e00b5a 100644 --- a/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj +++ b/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj @@ -63,7 +63,7 @@ - + diff --git a/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj b/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj index 5bd4b8e..13fd39c 100644 --- a/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj +++ b/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj @@ -62,7 +62,7 @@ - + diff --git a/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj b/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj index 5ac3e11..1327462 100644 --- a/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj +++ b/Tests/ScreenCapture.NET.Tests/ScreenCapture.NET.Tests.csproj @@ -9,10 +9,10 @@ - - - - + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive From 122327532bb30ad497c15b2c2e24a2adb6a89540 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 21 Jul 2024 22:53:45 +0200 Subject: [PATCH 4/7] Change image creation in capturezone to wrap --- ScreenCapture.NET/Model/CaptureZone.cs | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/ScreenCapture.NET/Model/CaptureZone.cs b/ScreenCapture.NET/Model/CaptureZone.cs index 381feb9..eb0a58b 100644 --- a/ScreenCapture.NET/Model/CaptureZone.cs +++ b/ScreenCapture.NET/Model/CaptureZone.cs @@ -77,14 +77,7 @@ public ReadOnlySpan Pixels /// /// Gets a . /// - public IImage Image - { - get - { - lock (_lock) - return Image.Create(RawBuffer, Width, Height, Stride); - } - } + public IImage Image => Image.Wrap(InternalBuffer, Width, Height, Stride); /// IImage ICaptureZone.Image => Image; From fd228af17041cc43e588ba33d897d80b56434b71 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Sun, 21 Jul 2024 22:55:38 +0200 Subject: [PATCH 5/7] Fixed small code issues --- ScreenCapture.NET.DX11/DX11ScreenCapture.cs | 6 +++--- ScreenCapture.NET/Model/CaptureZone.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ScreenCapture.NET.DX11/DX11ScreenCapture.cs b/ScreenCapture.NET.DX11/DX11ScreenCapture.cs index eebd50a..c5df93d 100644 --- a/ScreenCapture.NET.DX11/DX11ScreenCapture.cs +++ b/ScreenCapture.NET.DX11/DX11ScreenCapture.cs @@ -24,12 +24,12 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture #region Constants private static readonly FeatureLevel[] FEATURE_LEVELS = - { + [ FeatureLevel.Level_11_1, FeatureLevel.Level_11_0, FeatureLevel.Level_10_1, FeatureLevel.Level_10_0 - }; + ]; #endregion @@ -54,7 +54,7 @@ public sealed class DX11ScreenCapture : AbstractScreenCapture private ID3D11DeviceContext? _context; private ID3D11Texture2D? _captureTexture; - private readonly Dictionary, ZoneTextures> _textures = new(); + private readonly Dictionary, ZoneTextures> _textures = []; #endregion diff --git a/ScreenCapture.NET/Model/CaptureZone.cs b/ScreenCapture.NET/Model/CaptureZone.cs index eb0a58b..93637f5 100644 --- a/ScreenCapture.NET/Model/CaptureZone.cs +++ b/ScreenCapture.NET/Model/CaptureZone.cs @@ -204,7 +204,7 @@ private class UnlockDisposable : IDisposable /// public void Dispose() { - if (_disposed) throw new ObjectDisposedException("The lock is already released"); + ObjectDisposedException.ThrowIf(_disposed, this); Monitor.Exit(_lock); _disposed = true; From e9eb490607b4803f903b7e3b7dbe51b97f53c892 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 22 Jul 2024 23:12:34 +0200 Subject: [PATCH 6/7] Bumped version to 3.0.0 --- ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj | 7 +++---- ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj | 6 +++--- ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj | 6 +++--- ScreenCapture.NET/ScreenCapture.NET.csproj | 6 +++--- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj b/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj index 2e00b5a..6e0b0b3 100644 --- a/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj +++ b/ScreenCapture.NET.DX11/ScreenCapture.NET.DX11.csproj @@ -27,12 +27,11 @@ True - - Fixed a memory-leak when disposing the ScreenCapture - 2.0.4 - 2.0.4 - 2.0.4 + 3.0.0 + 3.0.0 + 3.0.0 ..\bin\ true diff --git a/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj b/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj index 13fd39c..cdbdd8e 100644 --- a/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj +++ b/ScreenCapture.NET.DX9/ScreenCapture.NET.DX9.csproj @@ -29,9 +29,9 @@ - 2.0.4 - 2.0.4 - 2.0.4 + 3.0.0 + 3.0.0 + 3.0.0 ..\bin\ true diff --git a/ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj b/ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj index 5dbd69c..5cdcb03 100644 --- a/ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj +++ b/ScreenCapture.NET.X11/ScreenCapture.NET.X11.csproj @@ -29,9 +29,9 @@ - 2.0.4 - 2.0.4 - 2.0.4 + 3.0.0 + 3.0.0 + 3.0.0 ..\bin\ true diff --git a/ScreenCapture.NET/ScreenCapture.NET.csproj b/ScreenCapture.NET/ScreenCapture.NET.csproj index f33d37d..fd9a0b3 100644 --- a/ScreenCapture.NET/ScreenCapture.NET.csproj +++ b/ScreenCapture.NET/ScreenCapture.NET.csproj @@ -28,9 +28,9 @@ - 2.0.4 - 2.0.4 - 2.0.4 + 3.0.0 + 3.0.0 + 3.0.0 ..\bin\ true From f314a4128b0c4083fa9536b48f6f0849a630162f Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Mon, 22 Jul 2024 23:21:41 +0200 Subject: [PATCH 7/7] Updated readme --- README.md | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cac0f24..8887b83 100644 --- a/README.md +++ b/README.md @@ -26,14 +26,14 @@ IEnumerable displays = screenCaptureService.GetDisplays(graphicsCards.F // Create a screen-capture for all screens you want to capture IScreenCapture screenCapture = screenCaptureService.GetScreenCapture(displays.First()); -// Register the regions you want to capture om the screen +// Register the regions you want to capture on the screen // Capture the whole screen ICaptureZone fullscreen = screenCapture.RegisterCaptureZone(0, 0, screenCapture.Display.Width, screenCapture.Display.Height); // Capture a 100x100 region at the top left and scale it down to 50x50 ICaptureZone topLeft = screenCapture.RegisterCaptureZone(0, 0, 100, 100, downscaleLevel: 1); // Capture the screen -// This should be done in a loop on a seperate thread as CaptureScreen blocks if the screen is not updated (still image). +// This should be done in a loop on a separate thread as CaptureScreen blocks if the screen is not updated (still image). screenCapture.CaptureScreen(); // Do something with the captured image - e.g. access all pixels (same could be done with topLeft) @@ -57,12 +57,12 @@ using (fullscreen.Lock()) IColor imageColorExample = image[10, 20]; // Get the first row - IImage.IImageRow row = image.Rows[0]; + IImageRow row = image.Rows[0]; // Get the 10th pixel of the row IColor rowColorExample = row[10]; // Get the first column - IImage.IImageColumn column = image.Columns[0]; + IImageColumn column = image.Columns[0]; // Get the 10th pixel of the column IColor columnColorExample = column[10]; @@ -73,7 +73,7 @@ using (fullscreen.Lock()) } ``` -IF you know which Capture-provider you're using it performs a bit better to not use the abstraction but a more low-level approach instead. +If you know which Capture-provider you're using it performs a bit better to not use the abstraction but a more low-level approach instead. This is the same example as above but without using the interfaces: ```csharp DX11ScreenCaptureService screenCaptureService = new DX11ScreenCaptureService(); @@ -88,17 +88,20 @@ screenCapture.CaptureScreen(); using (fullscreen.Lock()) { - RefImage image = fullscreen.Image; + IImage image = fullscreen.Image; + + // You can also get a ref image which has a slight performance benefit in some cases + // RefImage refImage = image.AsRefImage(); foreach (ColorBGRA color in image) Console.WriteLine($"A: {color.A}, R: {color.R}, G: {color.G}, B: {color.B}"); ColorBGRA imageColorExample = image[10, 20]; - ReadOnlyRefEnumerable row = image.Rows[0]; + ImageRow row = image.Rows[0]; ColorBGRA rowColorExample = row[10]; - ReadOnlyRefEnumerable column = image.Columns[0]; + ImageColumn column = image.Columns[0]; ColorBGRA columnColorExample = column[10]; RefImage subImage = image[100, 150, 400, 300];