Skip to content

Commit 946dafe

Browse files
committed
Implemented generic image
1 parent 52d37b9 commit 946dafe

File tree

7 files changed

+517
-175
lines changed

7 files changed

+517
-175
lines changed

ScreenCapture.NET/Model/CaptureZone.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,12 @@ public sealed class CaptureZone<TColor> : ICaptureZone
2323
public int Id { get; }
2424

2525
public Display Display { get; }
26-
27-
#if NET7_0_OR_GREATER
28-
public ColorFormat ColorFormat
29-
{
30-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
31-
get => TColor.ColorFormat;
32-
}
33-
#else
26+
3427
public ColorFormat ColorFormat
3528
{
3629
[MethodImpl(MethodImplOptions.AggressiveInlining)]
37-
get => IColor.GetColorFormat<TColor>();
30+
get => TColor.ColorFormat;
3831
}
39-
#endif
4032

4133
/// <summary>
4234
/// Gets the x-location of the region on the screen.
@@ -93,12 +85,18 @@ public ReadOnlySpan<TColor> Pixels
9385
get => MemoryMarshal.Cast<byte, TColor>(RawBuffer);
9486
}
9587

96-
public Image<TColor> Image
88+
public RefImage<TColor> Image
9789
{
9890
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9991
get => new(Pixels, 0, 0, Width, Height, Width);
10092
}
10193

94+
IImage ICaptureZone.Image
95+
{
96+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
97+
get => new Image<TColor>(InternalBuffer, 0, 0, Width, Height, Width);
98+
}
99+
102100
/// <summary>
103101
/// Gets or sets if the <see cref="CaptureZone{T}"/> should be automatically updated on every captured frame.
104102
/// </summary>
@@ -154,6 +152,14 @@ internal CaptureZone(int id, Display display, int x, int y, int width, int heigh
154152

155153
#region Methods
156154

155+
public RefImage<T> GetRefImage<T>()
156+
where T : struct, IColor
157+
{
158+
if (typeof(T) != typeof(TColor)) throw new ArgumentException("The requested Color-Format does not match the data.", nameof(T));
159+
160+
return new RefImage<T>(MemoryMarshal.Cast<byte, T>(RawBuffer), 0, 0, Width, Height, Width);
161+
}
162+
157163
public IDisposable Lock()
158164
{
159165
Monitor.Enter(_lock);
@@ -216,6 +222,7 @@ private class UnlockDisposable : IDisposable
216222
#region Constructors
217223

218224
public UnlockDisposable(object @lock) => this._lock = @lock;
225+
~UnlockDisposable() => Dispose();
219226

220227
#endregion
221228

@@ -227,6 +234,8 @@ public void Dispose()
227234

228235
Monitor.Exit(_lock);
229236
_disposed = true;
237+
238+
GC.SuppressFinalize(this);
230239
}
231240

232241
#endregion
File renamed without changes.
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace ScreenCapture.NET;
1+
using System;
2+
3+
namespace ScreenCapture.NET;
24

35
public interface IColor
46
{
@@ -7,16 +9,5 @@ public interface IColor
79
byte B { get; }
810
byte A { get; }
911

10-
#if NET7_0_OR_GREATER
11-
public static abstract ColorFormat ColorFormat { get; }
12-
#else
13-
public static ColorFormat GetColorFormat<TColor>()
14-
where TColor : IColor
15-
{
16-
System.Type colorType = typeof(TColor);
17-
if (colorType == typeof(ColorBGRA)) return ColorFormat.BGRA;
18-
19-
throw new System.ArgumentException($"Not ColorFormat registered for '{typeof(TColor).Name}'");
20-
}
21-
#endif
12+
public static virtual ColorFormat ColorFormat => throw new NotSupportedException();
2213
}

ScreenCapture.NET/Model/ICaptureZone.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public interface ICaptureZone
88
/// Gets the unique id of this <see cref="ICaptureZone"/>.
99
/// </summary>
1010
int Id { get; }
11+
1112
Display Display { get; }
1213
/// <summary>
1314
/// Gets the x-location of the region on the screen.
@@ -40,6 +41,8 @@ public interface ICaptureZone
4041

4142
ReadOnlySpan<byte> RawBuffer { get; }
4243

44+
IImage Image { get; }
45+
4346
/// <summary>
4447
/// Gets or sets if the <see cref="ICaptureZone"/> should be automatically updated on every captured frame.
4548
/// </summary>
@@ -58,4 +61,7 @@ public interface ICaptureZone
5861
/// Only necessary if <see cref="AutoUpdate"/> is set to <c>false</c>.
5962
/// </summary>
6063
void RequestUpdate();
64+
65+
RefImage<TColor> GetRefImage<TColor>()
66+
where TColor : struct, IColor;
6167
}

ScreenCapture.NET/Model/IImage.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
3+
namespace ScreenCapture.NET;
4+
5+
public interface IImage : IEnumerable<IColor>
6+
{
7+
int Width { get; }
8+
int Height { get; }
9+
10+
IColor this[int x, int y] { get; }
11+
IImage this[int x, int y, int width, int height] { get; }
12+
13+
IImageRows Rows { get; }
14+
IImageColumns Columns { get; }
15+
16+
public interface IImageRows : IEnumerable<IImageRow>
17+
{
18+
IImageRow this[int column] { get; }
19+
}
20+
21+
public interface IImageColumns : IEnumerable<IImageColumn>
22+
{
23+
IImageColumn this[int column] { get; }
24+
}
25+
26+
public interface IImageRow : IEnumerable<IColor>
27+
{
28+
IColor this[int x] { get; }
29+
}
30+
31+
public interface IImageColumn : IEnumerable<IColor>
32+
{
33+
IColor this[int y] { get; }
34+
}
35+
}

0 commit comments

Comments
 (0)