Skip to content

Commit

Permalink
Merge pull request #18 from DarthAffe/FixesAndImprovememnts
Browse files Browse the repository at this point in the history
Fixes and improvememnts
  • Loading branch information
DarthAffe authored Sep 12, 2023
2 parents 7c768e0 + d5e17f7 commit 5eb8b56
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 29 deletions.
70 changes: 59 additions & 11 deletions ScreenCapture.NET/Extensions/BlackBarDetection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ public static IImage RemoveBlackBars(this IImage image, int threshold = 0, bool
return image[left, top, right - left, bottom - top];
}

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

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

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

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

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

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

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

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

private static int CalculateRight<TColor>(this RefImage<TColor> image, int threshold)
/// <summary>
/// Calculates the last column starting from the top with at least one non black pixel.
/// </summary>
/// <param name="image">The image to check.</param>
/// <param name="threshold">The threshold of "blackness" used to detect black bars. (e. g. Threshold 5 will consider a pixel of color [5,5,5] as black.)</param>
/// <returns>The column number of the last column with at least one non-black pixel.</returns>
public static int CalculateRight<TColor>(this RefImage<TColor> image, int threshold)
where TColor : struct, IColor
{
RefImage<TColor>.ImageColumns columns = image.Columns;
Expand Down
2 changes: 1 addition & 1 deletion ScreenCapture.NET/Generic/AbstractScreenCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public void Dispose()
_isDisposed = true;
}

/// <inheritdoc cref="Dispose" />
/// <inheritdoc cref="IDisposable.Dispose" />
protected virtual void Dispose(bool disposing) { }

#endregion
Expand Down
75 changes: 74 additions & 1 deletion ScreenCapture.NET/Model/IImage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace ScreenCapture.NET;

Expand All @@ -7,6 +8,11 @@ namespace ScreenCapture.NET;
/// </summary>
public interface IImage : IEnumerable<IColor>
{
/// <summary>
/// Gets the color format used in this image.
/// </summary>
ColorFormat ColorFormat { get; }

/// <summary>
/// Gets the width of this image.
/// </summary>
Expand All @@ -17,6 +23,11 @@ public interface IImage : IEnumerable<IColor>
/// </summary>
int Height { get; }

/// <summary>
/// Gets the size in bytes of this image.
/// </summary>
int SizeInBytes { get; }

/// <summary>
/// Gets the color at the specified location.
/// </summary>
Expand Down Expand Up @@ -45,6 +56,28 @@ public interface IImage : IEnumerable<IColor>
/// </summary>
IImageColumns Columns { get; }

/// <summary>
/// Gets an <see cref="RefImage{TColor}"/> representing this <see cref="IImage"/>.
/// </summary>
/// <typeparam name="TColor">The color-type of the iamge.</typeparam>
/// <returns>The <inheritdoc cref="RefImage{TColor}"/>.</returns>
RefImage<TColor> AsRefImage<TColor>() where TColor : struct, IColor;

/// <summary>
/// Copies the contents of this <see cref="IImage"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImage"/> instance.
/// </exception>
void CopyTo(in Span<byte> destination);

/// <summary>
/// Allocates a new array and copies this <see cref="IImage"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="IImage"/>.</returns>
byte[] ToArray();

/// <summary>
/// Represents a list of rows of an image.
/// </summary>
Expand Down Expand Up @@ -91,12 +124,32 @@ public interface IImageRow : IEnumerable<IColor>
/// </summary>
int Length { get; }

/// <summary>
/// Gets the size in bytes of this row.
/// </summary>
int SizeInBytes { get; }

/// <summary>
/// Gets the <see cref="IColor"/> at the specified location.
/// </summary>
/// <param name="x">The location to get the color from.</param>
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
IColor this[int x] { get; }

/// <summary>
/// Copies the contents of this <see cref="IImageRow"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageRow"/> instance.
/// </exception>
void CopyTo(in Span<byte> destination);

/// <summary>
/// Allocates a new array and copies this <see cref="IImageRow"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="IImageRow"/>.</returns>
byte[] ToArray();
}

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

/// <summary>
/// Gets the size in bytes of this column.
/// </summary>
int SizeInBytes { get; }

/// <summary>
/// Gets the <see cref="IColor"/> at the specified location.
/// </summary>
/// <param name="y">The location to get the color from.</param>
/// <returns>The <see cref="IColor"/> at the specified location.</returns>
IColor this[int y] { get; }

/// <summary>
/// Copies the contents of this <see cref="IImageColumn"/> into a destination <see cref="Span{T}"/> instance.
/// </summary>
/// <param name="destination">The destination <see cref="Span{T}"/> instance.</param>
/// <exception cref="ArgumentException">
/// Thrown when <paramref name="destination"/> is shorter than the source <see cref="IImageColumn"/> instance.
/// </exception>
void CopyTo(in Span<byte> destination);

/// <summary>
/// Allocates a new array and copies this <see cref="IImageColumn"/> into it.
/// </summary>
/// <returns>The new array containing the data of this <see cref="IImageColumn"/>.</returns>
byte[] ToArray();
}
}
Loading

0 comments on commit 5eb8b56

Please sign in to comment.