Browse Source

Drop Dither property from interface, rename diffuser.

af/merge-core
James Jackson-South 8 years ago
parent
commit
a7f2bd9792
  1. 6
      src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs
  2. 4
      src/ImageSharp/Processing/Quantization/FrameQuantizers/IFrameQuantizer{TPixel}.cs
  3. 2
      src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs
  4. 2
      src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs
  5. 2
      src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs
  6. 9
      src/ImageSharp/Processing/Quantization/IQuantizer.cs
  7. 35
      src/ImageSharp/Processing/Quantization/OctreeQuantizer.cs
  8. 21
      src/ImageSharp/Processing/Quantization/PaletteQuantizer.cs
  9. 35
      src/ImageSharp/Processing/Quantization/WuQuantizer.cs
  10. 10
      tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs

6
src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs

@ -39,8 +39,8 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers
{ {
Guard.NotNull(quantizer, nameof(quantizer)); Guard.NotNull(quantizer, nameof(quantizer));
this.Dither = quantizer.Dither; this.Diffuser = quantizer.Diffuser;
this.DitherType = quantizer.DitherType; this.Dither = this.Diffuser != null;
this.singlePass = singlePass; this.singlePass = singlePass;
} }
@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers
public bool Dither { get; } public bool Dither { get; }
/// <inheritdoc /> /// <inheritdoc />
public IErrorDiffuser DitherType { get; } public IErrorDiffuser Diffuser { get; }
/// <inheritdoc/> /// <inheritdoc/>
public virtual QuantizedFrame<TPixel> QuantizeFrame(ImageFrame<TPixel> image) public virtual QuantizedFrame<TPixel> QuantizeFrame(ImageFrame<TPixel> image)

4
src/ImageSharp/Processing/Quantization/FrameQuantizers/IFrameQuantizer{TPixel}.cs

@ -19,9 +19,9 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers
bool Dither { get; } bool Dither { get; }
/// <summary> /// <summary>
/// Gets the dithering algorithm to apply to the output image. /// Gets the error diffusion algorithm to apply to the output image.
/// </summary> /// </summary>
IErrorDiffuser DitherType { get; } IErrorDiffuser Diffuser { get; }
/// <summary> /// <summary>
/// Quantize an image frame and return the resulting output pixels. /// Quantize an image frame and return the resulting output pixels.

2
src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs

@ -121,7 +121,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers
if (this.Dither) if (this.Dither)
{ {
// Apply the dithering matrix. We have to reapply the value now as the original has changed. // 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; output[(y * source.Width) + x] = pixelValue;

2
src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs

@ -77,7 +77,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers
if (this.Dither) if (this.Dither)
{ {
// Apply the dithering matrix. We have to reapply the value now as the original has changed. // 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; output[(y * source.Width) + x] = pixelValue;

2
src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs

@ -290,7 +290,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers
if (this.Dither) if (this.Dither)
{ {
// Apply the dithering matrix. We have to reapply the value now as the original has changed. // 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; output[(y * source.Width) + x] = pixelValue;

9
src/ImageSharp/Processing/Quantization/IQuantizer.cs

@ -13,14 +13,9 @@ namespace SixLabors.ImageSharp.Processing.Quantization
public interface IQuantizer public interface IQuantizer
{ {
/// <summary> /// <summary>
/// Gets a value indicating whether to apply dithering to the output image. /// Gets the error diffusion algorithm to apply to the output image.
/// </summary> /// </summary>
bool Dither { get; } IErrorDiffuser Diffuser { get; }
/// <summary>
/// Gets the dithering algorithm to apply to the output image.
/// </summary>
IErrorDiffuser DitherType { get; }
/// <summary> /// <summary>
/// Creates the generic frame quantizer /// Creates the generic frame quantizer

35
src/ImageSharp/Processing/Quantization/OctreeQuantizer.cs

@ -18,7 +18,16 @@ namespace SixLabors.ImageSharp.Processing.Quantization
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class. /// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// </summary> /// </summary>
public OctreeQuantizer() public OctreeQuantizer()
: this(255) : this(true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// </summary>
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param>
public OctreeQuantizer(int maxColors)
: this(GetDiffuser(true), maxColors)
{ {
} }
@ -27,40 +36,34 @@ namespace SixLabors.ImageSharp.Processing.Quantization
/// </summary> /// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param> /// <param name="dither">Whether to apply dithering to the output image</param>
public OctreeQuantizer(bool dither) public OctreeQuantizer(bool dither)
: this(dither, DiffuseMode.FloydSteinberg, 255) : this(GetDiffuser(dither), 255)
{ {
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class. /// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// </summary> /// </summary>
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param> /// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</param>
public OctreeQuantizer(int maxColors) public OctreeQuantizer(IErrorDiffuser diffuser)
: this(true, DiffuseMode.FloydSteinberg, maxColors) : this(diffuser, 255)
{ {
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class. /// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// </summary> /// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param> /// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</param>
/// <param name="ditherType">The dithering algorithm to apply to the output image</param>
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param> /// <param name="maxColors">The maximum number of colors to hold in the color palette</param>
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)); Guard.MustBeBetweenOrEqualTo(maxColors, 1, 255, nameof(maxColors));
this.Dither = dither; this.Diffuser = diffuser;
this.DitherType = ditherType;
this.MaxColors = maxColors; this.MaxColors = maxColors;
} }
/// <inheritdoc /> /// <inheritdoc />
public bool Dither { get; } public IErrorDiffuser Diffuser { get; }
/// <inheritdoc />
public IErrorDiffuser DitherType { get; }
/// <summary> /// <summary>
/// Gets the maximum number of colors to hold in the color palette. /// Gets the maximum number of colors to hold in the color palette.
@ -71,5 +74,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>() public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>()
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
=> new OctreeFrameQuantizer<TPixel>(this); => new OctreeFrameQuantizer<TPixel>(this);
private static IErrorDiffuser GetDiffuser(bool dither) => dither ? DiffuseMode.FloydSteinberg : null;
} }
} }

21
src/ImageSharp/Processing/Quantization/PaletteQuantizer.cs

@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization
/// Initializes a new instance of the <see cref="PaletteQuantizer"/> class. /// Initializes a new instance of the <see cref="PaletteQuantizer"/> class.
/// </summary> /// </summary>
public PaletteQuantizer() public PaletteQuantizer()
: this(true, DiffuseMode.FloydSteinberg) : this(true)
{ {
} }
@ -28,28 +28,21 @@ namespace SixLabors.ImageSharp.Processing.Quantization
/// </summary> /// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param> /// <param name="dither">Whether to apply dithering to the output image</param>
public PaletteQuantizer(bool dither) public PaletteQuantizer(bool dither)
: this(dither, DiffuseMode.FloydSteinberg) : this(GetDiffuser(dither))
{ {
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="PaletteQuantizer"/> class. /// Initializes a new instance of the <see cref="PaletteQuantizer"/> class.
/// </summary> /// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param> /// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</param>
/// <param name="ditherType">The dithering algorithm to apply to the output image</param> public PaletteQuantizer(IErrorDiffuser diffuser)
public PaletteQuantizer(bool dither, IErrorDiffuser ditherType)
{ {
Guard.NotNull(ditherType, nameof(ditherType)); this.Diffuser = diffuser;
this.Dither = dither;
this.DitherType = ditherType;
} }
/// <inheritdoc /> /// <inheritdoc />
public bool Dither { get; } public IErrorDiffuser Diffuser { get; }
/// <inheritdoc />
public IErrorDiffuser DitherType { get; }
/// <summary> /// <summary>
/// Gets the palette to use to quantize the image. /// Gets the palette to use to quantize the image.
@ -64,5 +57,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>() public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>()
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
=> new PaletteFrameQuantizer<TPixel>(this); => new PaletteFrameQuantizer<TPixel>(this);
private static IErrorDiffuser GetDiffuser(bool dither) => dither ? DiffuseMode.FloydSteinberg : null;
} }
} }

35
src/ImageSharp/Processing/Quantization/WuQuantizer.cs

@ -18,7 +18,16 @@ namespace SixLabors.ImageSharp.Processing.Quantization
/// Initializes a new instance of the <see cref="WuQuantizer"/> class. /// Initializes a new instance of the <see cref="WuQuantizer"/> class.
/// </summary> /// </summary>
public WuQuantizer() public WuQuantizer()
: this(255) : this(true)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WuQuantizer"/> class.
/// </summary>
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param>
public WuQuantizer(int maxColors)
: this(GetDiffuser(true), maxColors)
{ {
} }
@ -27,40 +36,34 @@ namespace SixLabors.ImageSharp.Processing.Quantization
/// </summary> /// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param> /// <param name="dither">Whether to apply dithering to the output image</param>
public WuQuantizer(bool dither) public WuQuantizer(bool dither)
: this(dither, DiffuseMode.FloydSteinberg, 255) : this(GetDiffuser(dither), 255)
{ {
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WuQuantizer"/> class. /// Initializes a new instance of the <see cref="WuQuantizer"/> class.
/// </summary> /// </summary>
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param> /// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</param>
public WuQuantizer(int maxColors) public WuQuantizer(IErrorDiffuser diffuser)
: this(true, DiffuseMode.FloydSteinberg, maxColors) : this(diffuser, 255)
{ {
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WuQuantizer"/> class. /// Initializes a new instance of the <see cref="WuQuantizer"/> class.
/// </summary> /// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param> /// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</param>
/// <param name="ditherType">The dithering algorithm to apply to the output image</param>
/// <param name="maxColors">The maximum number of colors to hold in the color palette</param> /// <param name="maxColors">The maximum number of colors to hold in the color palette</param>
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)); Guard.MustBeBetweenOrEqualTo(maxColors, 1, 255, nameof(maxColors));
this.Dither = dither; this.Diffuser = diffuser;
this.DitherType = ditherType;
this.MaxColors = maxColors; this.MaxColors = maxColors;
} }
/// <inheritdoc /> /// <inheritdoc />
public bool Dither { get; } public IErrorDiffuser Diffuser { get; }
/// <inheritdoc />
public IErrorDiffuser DitherType { get; }
/// <summary> /// <summary>
/// Gets the maximum number of colors to hold in the color palette. /// Gets the maximum number of colors to hold in the color palette.
@ -71,5 +74,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>() public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>()
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
=> new WuFrameQuantizer<TPixel>(this); => new WuFrameQuantizer<TPixel>(this);
private static IErrorDiffuser GetDiffuser(bool dither) => dither ? DiffuseMode.FloydSteinberg : null;
} }
} }

10
tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs

@ -14,9 +14,13 @@
var octree = new OctreeQuantizer(); var octree = new OctreeQuantizer();
var wu = new WuQuantizer(); var wu = new WuQuantizer();
Assert.True(palette.Dither); Assert.NotNull(palette.Diffuser);
Assert.True(octree.Dither); Assert.NotNull(octree.Diffuser);
Assert.True(wu.Dither); Assert.NotNull(wu.Diffuser);
Assert.True(palette.CreateFrameQuantizer<Rgba32>().Dither);
Assert.True(octree.CreateFrameQuantizer<Rgba32>().Dither);
Assert.True(wu.CreateFrameQuantizer<Rgba32>().Dither);
} }
[Theory] [Theory]

Loading…
Cancel
Save