Browse Source

Merge pull request #3073 from SixLabors/js/antialis-threshold-

Replace AntialiasSubpixelDepth with AntialiasThreshold
pull/3075/head
James Jackson-South 4 months ago
committed by GitHub
parent
commit
67bac23cff
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 46
      src/ImageSharp/GraphicsOptions.cs
  2. 32
      tests/ImageSharp.Tests/GraphicsOptionsTests.cs
  3. 12
      tests/ImageSharp.Tests/TestUtilities/GraphicsOptionsComparer.cs

46
src/ImageSharp/GraphicsOptions.cs

@ -6,11 +6,12 @@ using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp; namespace SixLabors.ImageSharp;
/// <summary> /// <summary>
/// Options for influencing the drawing functions. /// Provides configuration for controlling how graphics operations are rendered,
/// including antialiasing, pixel blending, alpha composition, and coverage thresholding.
/// </summary> /// </summary>
public class GraphicsOptions : IDeepCloneable<GraphicsOptions> public class GraphicsOptions : IDeepCloneable<GraphicsOptions>
{ {
private int antialiasSubpixelDepth = 16; private float antialiasThreshold = .5F;
private float blendPercentage = 1F; private float blendPercentage = 1F;
/// <summary> /// <summary>
@ -24,61 +25,62 @@ public class GraphicsOptions : IDeepCloneable<GraphicsOptions>
{ {
this.AlphaCompositionMode = source.AlphaCompositionMode; this.AlphaCompositionMode = source.AlphaCompositionMode;
this.Antialias = source.Antialias; this.Antialias = source.Antialias;
this.AntialiasSubpixelDepth = source.AntialiasSubpixelDepth; this.AntialiasThreshold = source.AntialiasThreshold;
this.BlendPercentage = source.BlendPercentage; this.BlendPercentage = source.BlendPercentage;
this.ColorBlendingMode = source.ColorBlendingMode; this.ColorBlendingMode = source.ColorBlendingMode;
} }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether antialiasing should be applied. /// Gets or sets a value indicating whether antialiasing should be applied.
/// Defaults to true. /// When <see langword="true"/>, edges are rendered with smooth sub-pixel coverage.
/// When <see langword="false"/>, coverage is snapped to binary (fully opaque or fully transparent)
/// using <see cref="AntialiasThreshold"/> as the cutoff.
/// Defaults to <see langword="true"/>.
/// </summary> /// </summary>
public bool Antialias { get; set; } = true; public bool Antialias { get; set; } = true;
/// <summary> /// <summary>
/// Gets or sets a value indicating the number of subpixels to use while rendering with antialiasing enabled. /// Gets or sets the coverage threshold used when <see cref="Antialias"/> is <see langword="false"/>.
/// Defaults to 16. /// Pixels with antialiased coverage above this value are rendered as fully opaque;
/// pixels below are discarded. Valid range is 0 to 1. Lower values preserve more
/// thin features at small sizes. Defaults to <c>0.5F</c>.
/// </summary> /// </summary>
public int AntialiasSubpixelDepth public float AntialiasThreshold
{ {
get get => this.antialiasThreshold;
{
return this.antialiasSubpixelDepth;
}
set set
{ {
Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.AntialiasSubpixelDepth)); Guard.MustBeBetweenOrEqualTo(value, 0F, 1F, nameof(this.AntialiasThreshold));
this.antialiasSubpixelDepth = value; this.antialiasThreshold = value;
} }
} }
/// <summary> /// <summary>
/// Gets or sets a value between indicating the blending percentage to apply to the drawing operation. /// Gets or sets the blending percentage applied to the drawing operation.
/// Range 0..1; Defaults to 1. /// A value of <c>1.0</c> applies the operation at full strength; <c>0.0</c> makes it invisible.
/// Valid range is 0 to 1. Defaults to <c>1.0F</c>.
/// </summary> /// </summary>
public float BlendPercentage public float BlendPercentage
{ {
get get => this.blendPercentage;
{
return this.blendPercentage;
}
set set
{ {
Guard.MustBeBetweenOrEqualTo(value, 0, 1F, nameof(this.BlendPercentage)); Guard.MustBeBetweenOrEqualTo(value, 0F, 1F, nameof(this.BlendPercentage));
this.blendPercentage = value; this.blendPercentage = value;
} }
} }
/// <summary> /// <summary>
/// Gets or sets a value indicating the color blending mode to apply to the drawing operation. /// Gets or sets the color blending mode used to combine source and destination pixel colors.
/// Defaults to <see cref="PixelColorBlendingMode.Normal"/>. /// Defaults to <see cref="PixelColorBlendingMode.Normal"/>.
/// </summary> /// </summary>
public PixelColorBlendingMode ColorBlendingMode { get; set; } = PixelColorBlendingMode.Normal; public PixelColorBlendingMode ColorBlendingMode { get; set; } = PixelColorBlendingMode.Normal;
/// <summary> /// <summary>
/// Gets or sets a value indicating the alpha composition mode to apply to the drawing operation /// Gets or sets the alpha composition mode that determines how source and destination alpha
/// channels are combined using Porter-Duff operators.
/// Defaults to <see cref="PixelAlphaCompositionMode.SrcOver"/>. /// Defaults to <see cref="PixelAlphaCompositionMode.SrcOver"/>.
/// </summary> /// </summary>
public PixelAlphaCompositionMode AlphaCompositionMode { get; set; } = PixelAlphaCompositionMode.SrcOver; public PixelAlphaCompositionMode AlphaCompositionMode { get; set; } = PixelAlphaCompositionMode.SrcOver;

32
tests/ImageSharp.Tests/GraphicsOptionsTests.cs

@ -13,7 +13,7 @@ public class GraphicsOptionsTests
private readonly GraphicsOptions cloneGraphicsOptions = new GraphicsOptions().DeepClone(); private readonly GraphicsOptions cloneGraphicsOptions = new GraphicsOptions().DeepClone();
[Fact] [Fact]
public void CloneGraphicsOptionsIsNotNull() => Assert.True(this.cloneGraphicsOptions != null); public void CloneGraphicsOptionsIsNotNull() => Assert.NotNull(this.cloneGraphicsOptions);
[Fact] [Fact]
public void DefaultGraphicsOptionsAntialias() public void DefaultGraphicsOptionsAntialias()
@ -23,35 +23,35 @@ public class GraphicsOptionsTests
} }
[Fact] [Fact]
public void DefaultGraphicsOptionsAntialiasSuppixelDepth() public void DefaultGraphicsOptionsAntialiasThreshold()
{ {
const int Expected = 16; const float expected = .5F;
Assert.Equal(Expected, this.newGraphicsOptions.AntialiasSubpixelDepth); Assert.Equal(expected, this.newGraphicsOptions.AntialiasThreshold);
Assert.Equal(Expected, this.cloneGraphicsOptions.AntialiasSubpixelDepth); Assert.Equal(expected, this.cloneGraphicsOptions.AntialiasThreshold);
} }
[Fact] [Fact]
public void DefaultGraphicsOptionsBlendPercentage() public void DefaultGraphicsOptionsBlendPercentage()
{ {
const float Expected = 1F; const float expected = 1F;
Assert.Equal(Expected, this.newGraphicsOptions.BlendPercentage); Assert.Equal(expected, this.newGraphicsOptions.BlendPercentage);
Assert.Equal(Expected, this.cloneGraphicsOptions.BlendPercentage); Assert.Equal(expected, this.cloneGraphicsOptions.BlendPercentage);
} }
[Fact] [Fact]
public void DefaultGraphicsOptionsColorBlendingMode() public void DefaultGraphicsOptionsColorBlendingMode()
{ {
const PixelColorBlendingMode Expected = PixelColorBlendingMode.Normal; const PixelColorBlendingMode expected = PixelColorBlendingMode.Normal;
Assert.Equal(Expected, this.newGraphicsOptions.ColorBlendingMode); Assert.Equal(expected, this.newGraphicsOptions.ColorBlendingMode);
Assert.Equal(Expected, this.cloneGraphicsOptions.ColorBlendingMode); Assert.Equal(expected, this.cloneGraphicsOptions.ColorBlendingMode);
} }
[Fact] [Fact]
public void DefaultGraphicsOptionsAlphaCompositionMode() public void DefaultGraphicsOptionsAlphaCompositionMode()
{ {
const PixelAlphaCompositionMode Expected = PixelAlphaCompositionMode.SrcOver; const PixelAlphaCompositionMode expected = PixelAlphaCompositionMode.SrcOver;
Assert.Equal(Expected, this.newGraphicsOptions.AlphaCompositionMode); Assert.Equal(expected, this.newGraphicsOptions.AlphaCompositionMode);
Assert.Equal(Expected, this.cloneGraphicsOptions.AlphaCompositionMode); Assert.Equal(expected, this.cloneGraphicsOptions.AlphaCompositionMode);
} }
[Fact] [Fact]
@ -61,7 +61,7 @@ public class GraphicsOptionsTests
{ {
AlphaCompositionMode = PixelAlphaCompositionMode.DestAtop, AlphaCompositionMode = PixelAlphaCompositionMode.DestAtop,
Antialias = false, Antialias = false,
AntialiasSubpixelDepth = 23, AntialiasThreshold = .33F,
BlendPercentage = .25F, BlendPercentage = .25F,
ColorBlendingMode = PixelColorBlendingMode.HardLight, ColorBlendingMode = PixelColorBlendingMode.HardLight,
}; };
@ -79,7 +79,7 @@ public class GraphicsOptionsTests
actual.AlphaCompositionMode = PixelAlphaCompositionMode.DestAtop; actual.AlphaCompositionMode = PixelAlphaCompositionMode.DestAtop;
actual.Antialias = false; actual.Antialias = false;
actual.AntialiasSubpixelDepth = 23; actual.AntialiasThreshold = .67F;
actual.BlendPercentage = .25F; actual.BlendPercentage = .25F;
actual.ColorBlendingMode = PixelColorBlendingMode.HardLight; actual.ColorBlendingMode = PixelColorBlendingMode.HardLight;

12
tests/ImageSharp.Tests/TestUtilities/GraphicsOptionsComparer.cs

@ -6,13 +6,11 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities;
public class GraphicsOptionsComparer : IEqualityComparer<GraphicsOptions> public class GraphicsOptionsComparer : IEqualityComparer<GraphicsOptions>
{ {
public bool Equals(GraphicsOptions x, GraphicsOptions y) public bool Equals(GraphicsOptions x, GraphicsOptions y)
{ => x.AlphaCompositionMode == y.AlphaCompositionMode
return x.AlphaCompositionMode == y.AlphaCompositionMode && x.Antialias == y.Antialias
&& x.Antialias == y.Antialias && x.AntialiasThreshold == y.AntialiasThreshold
&& x.AntialiasSubpixelDepth == y.AntialiasSubpixelDepth && x.BlendPercentage == y.BlendPercentage
&& x.BlendPercentage == y.BlendPercentage && x.ColorBlendingMode == y.ColorBlendingMode;
&& x.ColorBlendingMode == y.ColorBlendingMode;
}
public int GetHashCode(GraphicsOptions obj) => obj.GetHashCode(); public int GetHashCode(GraphicsOptions obj) => obj.GetHashCode();
} }

Loading…
Cancel
Save