Skip to content

Commit 5eb8b56

Browse files
authored
Merge pull request #18 from DarthAffe/FixesAndImprovememnts
Fixes and improvememnts
2 parents 7c768e0 + d5e17f7 commit 5eb8b56

File tree

7 files changed

+288
-29
lines changed

7 files changed

+288
-29
lines changed

ScreenCapture.NET/Extensions/BlackBarDetection.cs

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ public static IImage RemoveBlackBars(this IImage image, int threshold = 0, bool
2727
return image[left, top, right - left, bottom - top];
2828
}
2929

30-
private static int CalculateTop(IImage image, int threshold)
30+
/// <summary>
31+
/// Calculates the first row starting from the top with at least one non black pixel.
32+
/// </summary>
33+
/// <param name="image">The image to check.</param>
34+
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
35+
/// <returns>The row number of the first row with at least one non-black pixel.</returns>
36+
public static int CalculateTop(IImage image, int threshold)
3137
{
3238
IImage.IImageRows rows = image.Rows;
3339
for (int y = 0; y < rows.Count; y++)
@@ -43,7 +49,13 @@ private static int CalculateTop(IImage image, int threshold)
4349
return 0;
4450
}
4551

46-
private static int CalculateBottom(IImage image, int threshold)
52+
/// <summary>
53+
/// Calculates the last row starting from the top with at least one non black pixel.
54+
/// </summary>
55+
/// <param name="image">The image to check.</param>
56+
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
57+
/// <returns>The row number of the last row with at least one non-black pixel.</returns>
58+
public static int CalculateBottom(IImage image, int threshold)
4759
{
4860
IImage.IImageRows rows = image.Rows;
4961
for (int y = rows.Count - 1; y >= 0; y--)
@@ -59,7 +71,13 @@ private static int CalculateBottom(IImage image, int threshold)
5971
return rows.Count;
6072
}
6173

62-
private static int CalculateLeft(IImage image, int threshold)
74+
/// <summary>
75+
/// Calculates the first column starting from the left with at least one non black pixel.
76+
/// </summary>
77+
/// <param name="image">The image to check.</param>
78+
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
79+
/// <returns>The column number of the first column with at least one non-black pixel.</returns>
80+
public static int CalculateLeft(IImage image, int threshold)
6381
{
6482
IImage.IImageColumns columns = image.Columns;
6583
for (int x = 0; x < columns.Count; x++)
@@ -75,7 +93,13 @@ private static int CalculateLeft(IImage image, int threshold)
7593
return 0;
7694
}
7795

78-
private static int CalculateRight(IImage image, int threshold)
96+
/// <summary>
97+
/// Calculates the last column starting from the top with at least one non black pixel.
98+
/// </summary>
99+
/// <param name="image">The image to check.</param>
100+
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
101+
/// <returns>The column number of the last column with at least one non-black pixel.</returns>
102+
public static int CalculateRight(IImage image, int threshold)
79103
{
80104
IImage.IImageColumns columns = image.Columns;
81105
for (int x = columns.Count - 1; x >= 0; x--)
@@ -109,14 +133,20 @@ public static RefImage<TColor> RemoveBlackBars<TColor>(this RefImage<TColor> ima
109133
where TColor : struct, IColor
110134
{
111135
int top = removeTop ? CalculateTop(image, threshold) : 0;
112-
int bottom = removeBottom ? CalculateBottom(image, threshold) : image.Height;
136+
int bottom = removeBottom ? CalculateBottom(image, threshold) : image.Height - 1;
113137
int left = removeLeft ? CalculateLeft(image, threshold) : 0;
114-
int right = removeRight ? CalculateRight(image, threshold) : image.Width;
138+
int right = removeRight ? CalculateRight(image, threshold) : image.Width - 1;
115139

116-
return image[left, top, right - left, bottom - top];
140+
return image[left, top, (right - left) + 1, (bottom - top) + 1];
117141
}
118142

119-
private static int CalculateTop<TColor>(this RefImage<TColor> image, int threshold)
143+
/// <summary>
144+
/// Calculates the first row starting from the top with at least one non black pixel.
145+
/// </summary>
146+
/// <param name="image">The image to check.</param>
147+
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
148+
/// <returns>The row number of the first row with at least one non-black pixel.</returns>
149+
public static int CalculateTop<TColor>(this RefImage<TColor> image, int threshold)
120150
where TColor : struct, IColor
121151
{
122152
RefImage<TColor>.ImageRows rows = image.Rows;
@@ -133,7 +163,13 @@ private static int CalculateTop<TColor>(this RefImage<TColor> image, int thresho
133163
return 0;
134164
}
135165

136-
private static int CalculateBottom<TColor>(this RefImage<TColor> image, int threshold)
166+
/// <summary>
167+
/// Calculates the last row starting from the top with at least one non black pixel.
168+
/// </summary>
169+
/// <param name="image">The image to check.</param>
170+
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
171+
/// <returns>The row number of the last row with at least one non-black pixel.</returns>
172+
public static int CalculateBottom<TColor>(this RefImage<TColor> image, int threshold)
137173
where TColor : struct, IColor
138174
{
139175
RefImage<TColor>.ImageRows rows = image.Rows;
@@ -150,7 +186,13 @@ private static int CalculateBottom<TColor>(this RefImage<TColor> image, int thre
150186
return rows.Count;
151187
}
152188

153-
private static int CalculateLeft<TColor>(this RefImage<TColor> image, int threshold)
189+
/// <summary>
190+
/// Calculates the first column starting from the left with at least one non black pixel.
191+
/// </summary>
192+
/// <param name="image">The image to check.</param>
193+
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
194+
/// <returns>The column number of the first column with at least one non-black pixel.</returns>
195+
public static int CalculateLeft<TColor>(this RefImage<TColor> image, int threshold)
154196
where TColor : struct, IColor
155197
{
156198
RefImage<TColor>.ImageColumns columns = image.Columns;
@@ -167,7 +209,13 @@ private static int CalculateLeft<TColor>(this RefImage<TColor> image, int thresh
167209
return 0;
168210
}
169211

170-
private static int CalculateRight<TColor>(this RefImage<TColor> image, int threshold)
212+
/// <summary>
213+
/// Calculates the last column starting from the top with at least one non black pixel.
214+
/// </summary>
215+
/// <param name="image">The image to check.</param>
216+
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
217+
/// <returns>The column number of the last column with at least one non-black pixel.</returns>
218+
public static int CalculateRight<TColor>(this RefImage<TColor> image, int threshold)
171219
where TColor : struct, IColor
172220
{
173221
RefImage<TColor>.ImageColumns columns = image.Columns;

ScreenCapture.NET/Generic/AbstractScreenCapture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public void Dispose()
240240
_isDisposed = true;
241241
}
242242

243-
/// <inheritdoc cref="Dispose" />
243+
/// <inheritdoc cref="IDisposable.Dispose" />
244244
protected virtual void Dispose(bool disposing) { }
245245

246246
#endregion

ScreenCapture.NET/Model/IImage.cs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

34
namespace ScreenCapture.NET;
45

@@ -7,6 +8,11 @@ namespace ScreenCapture.NET;
78
/// </summary>
89
public interface IImage : IEnumerable<IColor>
910
{
11+
/// <summary>
12+
/// Gets the color format used in this image.
13+
/// </summary>
14+
ColorFormat ColorFormat { get; }
15+
1016
/// <summary>
1117
/// Gets the width of this image.
1218
/// </summary>
@@ -17,6 +23,11 @@ public interface IImage : IEnumerable<IColor>
1723
/// </summary>
1824
int Height { get; }
1925

26+
/// <summary>
27+
/// Gets the size in bytes of this image.
28+
/// </summary>
29+
int SizeInBytes { get; }
30+
2031
/// <summary>
2132
/// Gets the color at the specified location.
2233
/// </summary>
@@ -45,6 +56,28 @@ public interface IImage : IEnumerable<IColor>
4556
/// </summary>
4657
IImageColumns Columns { get; }
4758

59+
/// <summary>
60+
/// Gets an <see cref="RefImage{TColor}"/> representing this <see cref="IImage"/>.
61+
/// </summary>
62+
/// <typeparam name="TColor">The color-type of the iamge.</typeparam>
63+
/// <returns>The <inheritdoc cref="RefImage{TColor}"/>.</returns>
64+
RefImage<TColor> AsRefImage<TColor>() where TColor : struct, IColor;
65+
66+
/// <summary>
67+
/// Copies the contents of this <see cref="IImage"/> into a destination <see cref="Span{T}"/> instance.
68+
/// </summary>
69+
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
70+
/// <exception cref="ArgumentException">
71+
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImage"/> instance.
72+
/// </exception>
73+
void CopyTo(in Span<byte> destination);
74+
75+
/// <summary>
76+
/// Allocates a new array and copies this <see cref="IImage"/> into it.
77+
/// </summary>
78+
/// <returns>The new array containing the data of this <see cref="IImage"/>.</returns>
79+
byte[] ToArray();
80+
4881
/// <summary>
4982
/// Represents a list of rows of an image.
5083
/// </summary>
@@ -91,12 +124,32 @@ public interface IImageRow : IEnumerable<IColor>
91124
/// </summary>
92125
int Length { get; }
93126

127+
/// <summary>
128+
/// Gets the size in bytes of this row.
129+
/// </summary>
130+
int SizeInBytes { get; }
131+
94132
/// <summary>
95133
/// Gets the <see cref="IColor"/> at the specified location.
96134
/// </summary>
97135
/// <param name="x">The location to get the color from.</param>
98136
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
99137
IColor this[int x] { get; }
138+
139+
/// <summary>
140+
/// Copies the contents of this <see cref="IImageRow"/> into a destination <see cref="Span{T}"/> instance.
141+
/// </summary>
142+
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
143+
/// <exception cref="ArgumentException">
144+
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageRow"/> instance.
145+
/// </exception>
146+
void CopyTo(in Span<byte> destination);
147+
148+
/// <summary>
149+
/// Allocates a new array and copies this <see cref="IImageRow"/> into it.
150+
/// </summary>
151+
/// <returns>The new array containing the data of this <see cref="IImageRow"/>.</returns>
152+
byte[] ToArray();
100153
}
101154

102155
/// <summary>
@@ -109,11 +162,31 @@ public interface IImageColumn : IEnumerable<IColor>
109162
/// </summary>
110163
int Length { get; }
111164

165+
/// <summary>
166+
/// Gets the size in bytes of this column.
167+
/// </summary>
168+
int SizeInBytes { get; }
169+
112170
/// <summary>
113171
/// Gets the <see cref="IColor"/> at the specified location.
114172
/// </summary>
115173
/// <param name="y">The location to get the color from.</param>
116174
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
117175
IColor this[int y] { get; }
176+
177+
/// <summary>
178+
/// Copies the contents of this <see cref="IImageColumn"/> into a destination <see cref="Span{T}"/> instance.
179+
/// </summary>
180+
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
181+
/// <exception cref="ArgumentException">
182+
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageColumn"/> instance.
183+
/// </exception>
184+
void CopyTo(in Span<byte> destination);
185+
186+
/// <summary>
187+
/// Allocates a new array and copies this <see cref="IImageColumn"/> into it.
188+
/// </summary>
189+
/// <returns>The new array containing the data of this <see cref="IImageColumn"/>.</returns>
190+
byte[] ToArray();
118191
}
119192
}

0 commit comments

Comments
 (0)