diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs index 5f0510627c..bf0d80b07c 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs @@ -39,8 +39,8 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers { Guard.NotNull(quantizer, nameof(quantizer)); - this.Dither = quantizer.Dither; - this.DitherType = quantizer.DitherType; + this.Diffuser = quantizer.Diffuser; + this.Dither = this.Diffuser != null; this.singlePass = singlePass; } @@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers public bool Dither { get; } /// - public IErrorDiffuser DitherType { get; } + public IErrorDiffuser Diffuser { get; } /// public virtual QuantizedFrame QuantizeFrame(ImageFrame image) diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/IFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/IFrameQuantizer{TPixel}.cs index 0972a636a3..435302bd3e 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/IFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/IFrameQuantizer{TPixel}.cs @@ -19,9 +19,9 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers bool Dither { get; } /// - /// Gets the dithering algorithm to apply to the output image. + /// Gets the error diffusion algorithm to apply to the output image. /// - IErrorDiffuser DitherType { get; } + IErrorDiffuser Diffuser { get; } /// /// Quantize an image frame and return the resulting output pixels. diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs index 56a6c7240a..431064f220 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs @@ -121,7 +121,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers if (this.Dither) { // Apply the dithering matrix. We have to reapply the value now as the original has changed. - this.DitherType.Dither(source, sourcePixel, transformedPixel, x, y, 0, 0, width, height); + this.Diffuser.Dither(source, sourcePixel, transformedPixel, x, y, 0, 0, width, height); } output[(y * source.Width) + x] = pixelValue; diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs index 141c1afa05..b3a3eee634 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs @@ -77,7 +77,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers if (this.Dither) { // Apply the dithering matrix. We have to reapply the value now as the original has changed. - this.DitherType.Dither(source, sourcePixel, transformedPixel, x, y, 0, 0, width, height); + this.Diffuser.Dither(source, sourcePixel, transformedPixel, x, y, 0, 0, width, height); } output[(y * source.Width) + x] = pixelValue; diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs index 6adb38d2ee..fbc40dc8a1 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs @@ -290,7 +290,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers if (this.Dither) { // Apply the dithering matrix. We have to reapply the value now as the original has changed. - this.DitherType.Dither(source, sourcePixel, transformedPixel, x, y, 0, 0, width, height); + this.Diffuser.Dither(source, sourcePixel, transformedPixel, x, y, 0, 0, width, height); } output[(y * source.Width) + x] = pixelValue; diff --git a/src/ImageSharp/Processing/Quantization/IQuantizer.cs b/src/ImageSharp/Processing/Quantization/IQuantizer.cs index 2eb872a4f0..e00b865ac5 100644 --- a/src/ImageSharp/Processing/Quantization/IQuantizer.cs +++ b/src/ImageSharp/Processing/Quantization/IQuantizer.cs @@ -13,14 +13,9 @@ namespace SixLabors.ImageSharp.Processing.Quantization public interface IQuantizer { /// - /// Gets a value indicating whether to apply dithering to the output image. + /// Gets the error diffusion algorithm to apply to the output image. /// - bool Dither { get; } - - /// - /// Gets the dithering algorithm to apply to the output image. - /// - IErrorDiffuser DitherType { get; } + IErrorDiffuser Diffuser { get; } /// /// Creates the generic frame quantizer diff --git a/src/ImageSharp/Processing/Quantization/OctreeQuantizer.cs b/src/ImageSharp/Processing/Quantization/OctreeQuantizer.cs index acc5943c30..9d27970b14 100644 --- a/src/ImageSharp/Processing/Quantization/OctreeQuantizer.cs +++ b/src/ImageSharp/Processing/Quantization/OctreeQuantizer.cs @@ -18,7 +18,16 @@ namespace SixLabors.ImageSharp.Processing.Quantization /// Initializes a new instance of the class. /// public OctreeQuantizer() - : this(255) + : this(true) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The maximum number of colors to hold in the color palette + public OctreeQuantizer(int maxColors) + : this(GetDiffuser(true), maxColors) { } @@ -27,40 +36,34 @@ namespace SixLabors.ImageSharp.Processing.Quantization /// /// Whether to apply dithering to the output image public OctreeQuantizer(bool dither) - : this(dither, DiffuseMode.FloydSteinberg, 255) + : this(GetDiffuser(dither), 255) { } /// /// Initializes a new instance of the class. /// - /// The maximum number of colors to hold in the color palette - public OctreeQuantizer(int maxColors) - : this(true, DiffuseMode.FloydSteinberg, maxColors) + /// The error diffusion algorithm, if any, to apply to the output image + public OctreeQuantizer(IErrorDiffuser diffuser) + : this(diffuser, 255) { } /// /// Initializes a new instance of the class. /// - /// Whether to apply dithering to the output image - /// The dithering algorithm to apply to the output image + /// The error diffusion algorithm, if any, to apply to the output image /// The maximum number of colors to hold in the color palette - public OctreeQuantizer(bool dither, IErrorDiffuser ditherType, int maxColors) + public OctreeQuantizer(IErrorDiffuser diffuser, int maxColors) { - Guard.NotNull(ditherType, nameof(ditherType)); Guard.MustBeBetweenOrEqualTo(maxColors, 1, 255, nameof(maxColors)); - this.Dither = dither; - this.DitherType = ditherType; + this.Diffuser = diffuser; this.MaxColors = maxColors; } /// - public bool Dither { get; } - - /// - public IErrorDiffuser DitherType { get; } + public IErrorDiffuser Diffuser { get; } /// /// Gets the maximum number of colors to hold in the color palette. @@ -71,5 +74,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization public IFrameQuantizer CreateFrameQuantizer() where TPixel : struct, IPixel => new OctreeFrameQuantizer(this); + + private static IErrorDiffuser GetDiffuser(bool dither) => dither ? DiffuseMode.FloydSteinberg : null; } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Quantization/PaletteQuantizer.cs b/src/ImageSharp/Processing/Quantization/PaletteQuantizer.cs index ccdfae9a1e..f25b6537a7 100644 --- a/src/ImageSharp/Processing/Quantization/PaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Quantization/PaletteQuantizer.cs @@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization /// Initializes a new instance of the class. /// public PaletteQuantizer() - : this(true, DiffuseMode.FloydSteinberg) + : this(true) { } @@ -28,28 +28,21 @@ namespace SixLabors.ImageSharp.Processing.Quantization /// /// Whether to apply dithering to the output image public PaletteQuantizer(bool dither) - : this(dither, DiffuseMode.FloydSteinberg) + : this(GetDiffuser(dither)) { } /// /// Initializes a new instance of the class. /// - /// Whether to apply dithering to the output image - /// The dithering algorithm to apply to the output image - public PaletteQuantizer(bool dither, IErrorDiffuser ditherType) + /// The error diffusion algorithm, if any, to apply to the output image + public PaletteQuantizer(IErrorDiffuser diffuser) { - Guard.NotNull(ditherType, nameof(ditherType)); - - this.Dither = dither; - this.DitherType = ditherType; + this.Diffuser = diffuser; } /// - public bool Dither { get; } - - /// - public IErrorDiffuser DitherType { get; } + public IErrorDiffuser Diffuser { get; } /// /// Gets the palette to use to quantize the image. @@ -64,5 +57,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization public IFrameQuantizer CreateFrameQuantizer() where TPixel : struct, IPixel => new PaletteFrameQuantizer(this); + + private static IErrorDiffuser GetDiffuser(bool dither) => dither ? DiffuseMode.FloydSteinberg : null; } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Quantization/WuQuantizer.cs b/src/ImageSharp/Processing/Quantization/WuQuantizer.cs index 0d306107e9..9cfb554126 100644 --- a/src/ImageSharp/Processing/Quantization/WuQuantizer.cs +++ b/src/ImageSharp/Processing/Quantization/WuQuantizer.cs @@ -18,7 +18,16 @@ namespace SixLabors.ImageSharp.Processing.Quantization /// Initializes a new instance of the class. /// public WuQuantizer() - : this(255) + : this(true) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// The maximum number of colors to hold in the color palette + public WuQuantizer(int maxColors) + : this(GetDiffuser(true), maxColors) { } @@ -27,40 +36,34 @@ namespace SixLabors.ImageSharp.Processing.Quantization /// /// Whether to apply dithering to the output image public WuQuantizer(bool dither) - : this(dither, DiffuseMode.FloydSteinberg, 255) + : this(GetDiffuser(dither), 255) { } /// /// Initializes a new instance of the class. /// - /// The maximum number of colors to hold in the color palette - public WuQuantizer(int maxColors) - : this(true, DiffuseMode.FloydSteinberg, maxColors) + /// The error diffusion algorithm, if any, to apply to the output image + public WuQuantizer(IErrorDiffuser diffuser) + : this(diffuser, 255) { } /// /// Initializes a new instance of the class. /// - /// Whether to apply dithering to the output image - /// The dithering algorithm to apply to the output image + /// The error diffusion algorithm, if any, to apply to the output image /// The maximum number of colors to hold in the color palette - public WuQuantizer(bool dither, IErrorDiffuser ditherType, int maxColors) + public WuQuantizer(IErrorDiffuser diffuser, int maxColors) { - Guard.NotNull(ditherType, nameof(ditherType)); Guard.MustBeBetweenOrEqualTo(maxColors, 1, 255, nameof(maxColors)); - this.Dither = dither; - this.DitherType = ditherType; + this.Diffuser = diffuser; this.MaxColors = maxColors; } /// - public bool Dither { get; } - - /// - public IErrorDiffuser DitherType { get; } + public IErrorDiffuser Diffuser { get; } /// /// Gets the maximum number of colors to hold in the color palette. @@ -71,5 +74,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization public IFrameQuantizer CreateFrameQuantizer() where TPixel : struct, IPixel => new WuFrameQuantizer(this); + + private static IErrorDiffuser GetDiffuser(bool dither) => dither ? DiffuseMode.FloydSteinberg : null; } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs b/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs index 15d2bf51f7..8965904a5a 100644 --- a/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs +++ b/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs @@ -14,9 +14,13 @@ var octree = new OctreeQuantizer(); var wu = new WuQuantizer(); - Assert.True(palette.Dither); - Assert.True(octree.Dither); - Assert.True(wu.Dither); + Assert.NotNull(palette.Diffuser); + Assert.NotNull(octree.Diffuser); + Assert.NotNull(wu.Diffuser); + + Assert.True(palette.CreateFrameQuantizer().Dither); + Assert.True(octree.CreateFrameQuantizer().Dither); + Assert.True(wu.CreateFrameQuantizer().Dither); } [Theory]