Skip to content

Commit 57462e9

Browse files
committed
Readded black-bar removal
1 parent bcb92eb commit 57462e9

File tree

3 files changed

+189
-141
lines changed

3 files changed

+189
-141
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
namespace ScreenCapture.NET;
2+
3+
/// <summary>
4+
/// Helper-class for black-bar removal.
5+
/// </summary>
6+
public static class BlackBarDetection
7+
{
8+
#region IImage
9+
10+
/// <summary>
11+
/// Create an image with black bars removed
12+
/// </summary>
13+
/// <param name="image">The image the bars are removed from.</param>
14+
/// <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>
15+
/// <param name="removeTop">A bool indicating if black bars should be removed at the top of the image.</param>
16+
/// <param name="removeBottom">A bool indicating if black bars should be removed at the bottom of the image.</param>
17+
/// <param name="removeLeft">A bool indicating if black bars should be removed on the left side of the image.</param>
18+
/// <param name="removeRight">A bool indicating if black bars should be removed on the right side of the image.</param>
19+
/// <returns>The image with black bars removed.</returns>
20+
public static IImage RemoveBlackBars(this IImage image, int threshold = 0, bool removeTop = true, bool removeBottom = true, bool removeLeft = true, bool removeRight = true)
21+
{
22+
int top = removeTop ? CalculateTop(image, threshold) : 0;
23+
int bottom = removeBottom ? CalculateBottom(image, threshold) : image.Height;
24+
int left = removeLeft ? CalculateLeft(image, threshold) : 0;
25+
int right = removeRight ? CalculateRight(image, threshold) : image.Width;
26+
27+
return image[left, top, right - left, bottom - top];
28+
}
29+
30+
private static int CalculateTop(IImage image, int threshold)
31+
{
32+
IImage.IImageRows rows = image.Rows;
33+
for (int y = 0; y < rows.Count; y++)
34+
{
35+
IImage.IImageRow row = rows[y];
36+
foreach (IColor color in row)
37+
{
38+
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
39+
return y;
40+
}
41+
}
42+
43+
return 0;
44+
}
45+
46+
private static int CalculateBottom(IImage image, int threshold)
47+
{
48+
IImage.IImageRows rows = image.Rows;
49+
for (int y = rows.Count - 1; y >= 0; y--)
50+
{
51+
IImage.IImageRow row = rows[y];
52+
foreach (IColor color in row)
53+
{
54+
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
55+
return y;
56+
}
57+
}
58+
59+
return rows.Count;
60+
}
61+
62+
private static int CalculateLeft(IImage image, int threshold)
63+
{
64+
IImage.IImageColumns columns = image.Columns;
65+
for (int x = 0; x < columns.Count; x++)
66+
{
67+
IImage.IImageColumn column = columns[x];
68+
foreach (IColor color in column)
69+
{
70+
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
71+
return x;
72+
}
73+
}
74+
75+
return 0;
76+
}
77+
78+
private static int CalculateRight(IImage image, int threshold)
79+
{
80+
IImage.IImageColumns columns = image.Columns;
81+
for (int x = columns.Count - 1; x >= 0; x--)
82+
{
83+
IImage.IImageColumn column = columns[x];
84+
foreach (IColor color in column)
85+
{
86+
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
87+
return x;
88+
}
89+
}
90+
91+
return columns.Count;
92+
}
93+
94+
#endregion
95+
96+
#region RefImage
97+
98+
/// <summary>
99+
/// Create an image with black bars removed
100+
/// </summary>
101+
/// <param name="image">The image the bars are removed from.</param>
102+
/// <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>
103+
/// <param name="removeTop">A bool indicating if black bars should be removed at the top of the image.</param>
104+
/// <param name="removeBottom">A bool indicating if black bars should be removed at the bottom of the image.</param>
105+
/// <param name="removeLeft">A bool indicating if black bars should be removed on the left side of the image.</param>
106+
/// <param name="removeRight">A bool indicating if black bars should be removed on the right side of the image.</param>
107+
/// <returns>The image with black bars removed.</returns>
108+
public static RefImage<TColor> RemoveBlackBars<TColor>(this RefImage<TColor> image, int threshold = 0, bool removeTop = true, bool removeBottom = true, bool removeLeft = true, bool removeRight = true)
109+
where TColor : struct, IColor
110+
{
111+
int top = removeTop ? CalculateTop(image, threshold) : 0;
112+
int bottom = removeBottom ? CalculateBottom(image, threshold) : image.Height;
113+
int left = removeLeft ? CalculateLeft(image, threshold) : 0;
114+
int right = removeRight ? CalculateRight(image, threshold) : image.Width;
115+
116+
return image[left, top, right - left, bottom - top];
117+
}
118+
119+
private static int CalculateTop<TColor>(this RefImage<TColor> image, int threshold)
120+
where TColor : struct, IColor
121+
{
122+
RefImage<TColor>.ImageRows rows = image.Rows;
123+
for (int y = 0; y < rows.Count; y++)
124+
{
125+
ReadOnlyRefEnumerable<TColor> row = rows[y];
126+
foreach (TColor color in row)
127+
{
128+
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
129+
return y;
130+
}
131+
}
132+
133+
return 0;
134+
}
135+
136+
private static int CalculateBottom<TColor>(this RefImage<TColor> image, int threshold)
137+
where TColor : struct, IColor
138+
{
139+
RefImage<TColor>.ImageRows rows = image.Rows;
140+
for (int y = rows.Count - 1; y >= 0; y--)
141+
{
142+
ReadOnlyRefEnumerable<TColor> row = rows[y];
143+
foreach (TColor color in row)
144+
{
145+
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
146+
return y;
147+
}
148+
}
149+
150+
return rows.Count;
151+
}
152+
153+
private static int CalculateLeft<TColor>(this RefImage<TColor> image, int threshold)
154+
where TColor : struct, IColor
155+
{
156+
RefImage<TColor>.ImageColumns columns = image.Columns;
157+
for (int x = 0; x < columns.Count; x++)
158+
{
159+
ReadOnlyRefEnumerable<TColor> column = columns[x];
160+
foreach (TColor color in column)
161+
{
162+
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
163+
return x;
164+
}
165+
}
166+
167+
return 0;
168+
}
169+
170+
private static int CalculateRight<TColor>(this RefImage<TColor> image, int threshold)
171+
where TColor : struct, IColor
172+
{
173+
RefImage<TColor>.ImageColumns columns = image.Columns;
174+
for (int x = columns.Count - 1; x >= 0; x--)
175+
{
176+
ReadOnlyRefEnumerable<TColor> column = columns[x];
177+
foreach (TColor color in column)
178+
{
179+
if ((color.R > threshold) || (color.G > threshold) || (color.B > threshold))
180+
return x;
181+
}
182+
}
183+
184+
return columns.Count;
185+
}
186+
187+
#endregion
188+
}

ScreenCapture.NET/Model/BlackBarDetection.cs

Lines changed: 0 additions & 141 deletions
This file was deleted.

ScreenCapture.NET/ScreenCapture.NET.csproj.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=communitytoolkit_002Ehighperformance/@EntryIndexedValue">True</s:Boolean>
33
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=directx/@EntryIndexedValue">True</s:Boolean>
44
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=events/@EntryIndexedValue">True</s:Boolean>
5+
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=extensions/@EntryIndexedValue">True</s:Boolean>
56
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=generic/@EntryIndexedValue">True</s:Boolean>
67
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=helper/@EntryIndexedValue">True</s:Boolean>
78
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=model/@EntryIndexedValue">True</s:Boolean>

0 commit comments

Comments
 (0)