Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
namespace SixLabors.ImageSharp.Processing
{
/// <summary>
/// The color component to be compared to threshold.
/// Selects the value to be compared to threshold.
/// </summary>
public enum BinaryThresholdColorComponent : int
public enum BinaryThresholdMode
{
/// <summary>
/// Luminance color component according to ITU-R Recommendation BT.709.
/// Compare the color luminance (according to ITU-R Recommendation BT.709).
/// </summary>
Luminance = 0,

/// <summary>
/// HSL saturation color component.
/// Compare the HSL saturation of the color.
/// </summary>
Saturation = 1,

/// <summary>
/// Maximum of YCbCr chroma value, i.e. Cb and Cr distance from achromatic value.
/// Compare the maximum of YCbCr chroma value, i.e. Cb and Cr distance from achromatic value.
/// </summary>
MaxChroma = 2,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ public static class BinaryThresholdExtensions
/// <param name="threshold">The threshold to apply binarization of the image. Must be between 0 and 1.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext BinaryThreshold(this IImageProcessingContext source, float threshold)
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, BinaryThresholdColorComponent.Luminance));
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, BinaryThresholdMode.Luminance));

/// <summary>
/// Applies binarization to the image splitting the pixels at the given threshold.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="threshold">The threshold to apply binarization of the image. Must be between 0 and 1.</param>
/// <param name="colorComponent">The color component to be compared to threshold.</param>
/// <param name="mode">Selects the value to be compared to threshold.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext BinaryThreshold(
this IImageProcessingContext source,
float threshold,
BinaryThresholdColorComponent colorComponent)
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, colorComponent));
BinaryThresholdMode mode)
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, mode));

/// <summary>
/// Applies binarization to the image splitting the pixels at the given threshold with
Expand All @@ -48,24 +48,24 @@ public static IImageProcessingContext BinaryThreshold(
this IImageProcessingContext source,
float threshold,
Rectangle rectangle)
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, BinaryThresholdColorComponent.Luminance), rectangle);
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, BinaryThresholdMode.Luminance), rectangle);

/// <summary>
/// Applies binarization to the image splitting the pixels at the given threshold.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="threshold">The threshold to apply binarization of the image. Must be between 0 and 1.</param>
/// <param name="colorComponent">The color component to be compared to threshold.</param>
/// <param name="mode">Selects the value to be compared to threshold.</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext BinaryThreshold(
this IImageProcessingContext source,
float threshold,
BinaryThresholdColorComponent colorComponent,
BinaryThresholdMode mode,
Rectangle rectangle)
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, colorComponent), rectangle);
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, mode), rectangle);

/// <summary>
/// Applies binarization to the image splitting the pixels at the given threshold with
Expand All @@ -81,7 +81,7 @@ public static IImageProcessingContext BinaryThreshold(
float threshold,
Color upperColor,
Color lowerColor)
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, upperColor, lowerColor, BinaryThresholdColorComponent.Luminance));
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, upperColor, lowerColor, BinaryThresholdMode.Luminance));

/// <summary>
/// Applies binarization to the image splitting the pixels at the given threshold.
Expand All @@ -90,15 +90,15 @@ public static IImageProcessingContext BinaryThreshold(
/// <param name="threshold">The threshold to apply binarization of the image. Must be between 0 and 1.</param>
/// <param name="upperColor">The color to use for pixels that are above the threshold.</param>
/// <param name="lowerColor">The color to use for pixels that are below the threshold</param>
/// <param name="colorComponent">The color component to be compared to threshold.</param>
/// <param name="mode">Selects the value to be compared to threshold.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext BinaryThreshold(
this IImageProcessingContext source,
float threshold,
Color upperColor,
Color lowerColor,
BinaryThresholdColorComponent colorComponent)
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, upperColor, lowerColor, colorComponent));
BinaryThresholdMode mode)
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, upperColor, lowerColor, mode));

/// <summary>
/// Applies binarization to the image splitting the pixels at the given threshold with
Expand All @@ -118,7 +118,7 @@ public static IImageProcessingContext BinaryThreshold(
Color upperColor,
Color lowerColor,
Rectangle rectangle)
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, upperColor, lowerColor, BinaryThresholdColorComponent.Luminance), rectangle);
=> source.ApplyProcessor(new BinaryThresholdProcessor(threshold, upperColor, lowerColor, BinaryThresholdMode.Luminance), rectangle);

/// <summary>
/// Applies binarization to the image splitting the pixels at the given threshold.
Expand All @@ -127,7 +127,7 @@ public static IImageProcessingContext BinaryThreshold(
/// <param name="threshold">The threshold to apply binarization of the image. Must be between 0 and 1.</param>
/// <param name="upperColor">The color to use for pixels that are above the threshold.</param>
/// <param name="lowerColor">The color to use for pixels that are below the threshold</param>
/// <param name="colorComponent">The color component to be compared to threshold.</param>
/// <param name="mode">Selects the value to be compared to threshold.</param>
/// <param name="rectangle">
/// The <see cref="Rectangle"/> structure that specifies the portion of the image object to alter.
/// </param>
Expand All @@ -137,8 +137,8 @@ public static IImageProcessingContext BinaryThreshold(
float threshold,
Color upperColor,
Color lowerColor,
BinaryThresholdColorComponent colorComponent,
BinaryThresholdMode mode,
Rectangle rectangle) =>
source.ApplyProcessor(new BinaryThresholdProcessor(threshold, upperColor, lowerColor, colorComponent), rectangle);
source.ApplyProcessor(new BinaryThresholdProcessor(threshold, upperColor, lowerColor, mode), rectangle);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public class BinaryThresholdProcessor : IImageProcessor
/// Initializes a new instance of the <see cref="BinaryThresholdProcessor"/> class.
/// </summary>
/// <param name="threshold">The threshold to split the image. Must be between 0 and 1.</param>
/// <param name="colorComponent">The color component to be compared to threshold.</param>
public BinaryThresholdProcessor(float threshold, BinaryThresholdColorComponent colorComponent)
: this(threshold, Color.White, Color.Black, colorComponent)
/// <param name="mode">The color component to be compared to threshold.</param>
public BinaryThresholdProcessor(float threshold, BinaryThresholdMode mode)
: this(threshold, Color.White, Color.Black, mode)
{
}

Expand All @@ -26,7 +26,7 @@ public BinaryThresholdProcessor(float threshold, BinaryThresholdColorComponent c
/// </summary>
/// <param name="threshold">The threshold to split the image. Must be between 0 and 1.</param>
public BinaryThresholdProcessor(float threshold)
: this(threshold, Color.White, Color.Black, BinaryThresholdColorComponent.Luminance)
: this(threshold, Color.White, Color.Black, BinaryThresholdMode.Luminance)
{
}

Expand All @@ -36,14 +36,14 @@ public BinaryThresholdProcessor(float threshold)
/// <param name="threshold">The threshold to split the image. Must be between 0 and 1.</param>
/// <param name="upperColor">The color to use for pixels that are above the threshold.</param>
/// <param name="lowerColor">The color to use for pixels that are below the threshold.</param>
/// <param name="colorComponent">The color component to be compared to threshold.</param>
public BinaryThresholdProcessor(float threshold, Color upperColor, Color lowerColor, BinaryThresholdColorComponent colorComponent)
/// <param name="mode">The color component to be compared to threshold.</param>
public BinaryThresholdProcessor(float threshold, Color upperColor, Color lowerColor, BinaryThresholdMode mode)
{
Guard.MustBeBetweenOrEqualTo(threshold, 0, 1, nameof(threshold));
this.Threshold = threshold;
this.UpperColor = upperColor;
this.LowerColor = lowerColor;
this.ColorComponent = colorComponent;
this.Mode = mode;
}

/// <summary>
Expand All @@ -54,7 +54,7 @@ public BinaryThresholdProcessor(float threshold, Color upperColor, Color lowerCo
/// <param name="upperColor">The color to use for pixels that are above the threshold.</param>
/// <param name="lowerColor">The color to use for pixels that are below the threshold.</param>
public BinaryThresholdProcessor(float threshold, Color upperColor, Color lowerColor)
: this(threshold, upperColor, lowerColor, BinaryThresholdColorComponent.Luminance)
: this(threshold, upperColor, lowerColor, BinaryThresholdMode.Luminance)
{
}

Expand All @@ -73,10 +73,10 @@ public BinaryThresholdProcessor(float threshold, Color upperColor, Color lowerCo
/// </summary>
public Color LowerColor { get; }

/// <summary>
/// Gets a value indicating whether to use saturation value instead of luminance.
/// <summary>
/// Gets the <see cref="BinaryThresholdMode"/> defining the value to be compared to threshold.
/// </summary>
public BinaryThresholdColorComponent ColorComponent { get; }
public BinaryThresholdMode Mode { get; }

/// <inheritdoc />
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>(Configuration configuration, Image<TPixel> source, Rectangle sourceRectangle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
upper,
lower,
threshold,
this.definition.ColorComponent,
this.definition.Mode,
configuration);

ParallelRowIterator.IterateRows<RowOperation, Rgb24>(
Expand All @@ -63,7 +63,7 @@ protected override void OnFrameApply(ImageFrame<TPixel> source)
private readonly TPixel upper;
private readonly TPixel lower;
private readonly byte threshold;
private readonly BinaryThresholdColorComponent colorComponent;
private readonly BinaryThresholdMode mode;
private readonly int startX;
private readonly Configuration configuration;

Expand All @@ -74,15 +74,15 @@ public RowOperation(
TPixel upper,
TPixel lower,
byte threshold,
BinaryThresholdColorComponent colorComponent,
BinaryThresholdMode mode,
Configuration configuration)
{
this.startX = startX;
this.source = source;
this.upper = upper;
this.lower = lower;
this.threshold = threshold;
this.colorComponent = colorComponent;
this.mode = mode;
this.configuration = configuration;
}

Expand All @@ -96,9 +96,9 @@ public void Invoke(int y, Span<Rgb24> span)
Span<TPixel> rowSpan = this.source.GetPixelRowSpan(y).Slice(this.startX, span.Length);
PixelOperations<TPixel>.Instance.ToRgb24(this.configuration, rowSpan, span);

switch (this.colorComponent)
switch (this.mode)
{
case BinaryThresholdColorComponent.Luminance:
case BinaryThresholdMode.Luminance:
{
byte threshold = this.threshold;
for (int x = 0; x < rowSpan.Length; x++)
Expand All @@ -112,7 +112,7 @@ public void Invoke(int y, Span<Rgb24> span)
break;
}

case BinaryThresholdColorComponent.Saturation:
case BinaryThresholdMode.Saturation:
{
float threshold = this.threshold / 255F;
for (int x = 0; x < rowSpan.Length; x++)
Expand All @@ -125,7 +125,7 @@ public void Invoke(int y, Span<Rgb24> span)
break;
}

case BinaryThresholdColorComponent.MaxChroma:
case BinaryThresholdMode.MaxChroma:
{
float threshold = this.threshold / 2F;
for (int x = 0; x < rowSpan.Length; x++)
Expand Down
Loading