Browse Source

Drop Dither property from interface, rename diffuser.

pull/487/head
James Jackson-South 8 years ago
parent
commit
a2037cef6e
  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));
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; }
/// <inheritdoc />
public IErrorDiffuser DitherType { get; }
public IErrorDiffuser Diffuser { get; }
/// <inheritdoc/>
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; }
/// <summary>
/// Gets the dithering algorithm to apply to the output image.
/// Gets the error diffusion algorithm to apply to the output image.
/// </summary>
IErrorDiffuser DitherType { get; }
IErrorDiffuser Diffuser { get; }
/// <summary>
/// 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)
{
// 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;

2
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;

2
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;

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

@ -13,14 +13,9 @@ namespace SixLabors.ImageSharp.Processing.Quantization
public interface IQuantizer
{
/// <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>
bool Dither { get; }
/// <summary>
/// Gets the dithering algorithm to apply to the output image.
/// </summary>
IErrorDiffuser DitherType { get; }
IErrorDiffuser Diffuser { get; }
/// <summary>
/// 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.
/// </summary>
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>
/// <param name="dither">Whether to apply dithering to the output image</param>
public OctreeQuantizer(bool dither)
: this(dither, DiffuseMode.FloydSteinberg, 255)
: this(GetDiffuser(dither), 255)
{
}
/// <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(true, DiffuseMode.FloydSteinberg, maxColors)
/// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</param>
public OctreeQuantizer(IErrorDiffuser diffuser)
: this(diffuser, 255)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param>
/// <param name="ditherType">The dithering algorithm to apply to the output image</param>
/// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</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));
this.Dither = dither;
this.DitherType = ditherType;
this.Diffuser = diffuser;
this.MaxColors = maxColors;
}
/// <inheritdoc />
public bool Dither { get; }
/// <inheritdoc />
public IErrorDiffuser DitherType { get; }
public IErrorDiffuser Diffuser { get; }
/// <summary>
/// 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>()
where TPixel : struct, IPixel<TPixel>
=> 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.
/// </summary>
public PaletteQuantizer()
: this(true, DiffuseMode.FloydSteinberg)
: this(true)
{
}
@ -28,28 +28,21 @@ namespace SixLabors.ImageSharp.Processing.Quantization
/// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param>
public PaletteQuantizer(bool dither)
: this(dither, DiffuseMode.FloydSteinberg)
: this(GetDiffuser(dither))
{
}
/// <summary>
/// Initializes a new instance of the <see cref="PaletteQuantizer"/> class.
/// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param>
/// <param name="ditherType">The dithering algorithm to apply to the output image</param>
public PaletteQuantizer(bool dither, IErrorDiffuser ditherType)
/// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</param>
public PaletteQuantizer(IErrorDiffuser diffuser)
{
Guard.NotNull(ditherType, nameof(ditherType));
this.Dither = dither;
this.DitherType = ditherType;
this.Diffuser = diffuser;
}
/// <inheritdoc />
public bool Dither { get; }
/// <inheritdoc />
public IErrorDiffuser DitherType { get; }
public IErrorDiffuser Diffuser { get; }
/// <summary>
/// Gets the palette to use to quantize the image.
@ -64,5 +57,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>()
where TPixel : struct, IPixel<TPixel>
=> 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.
/// </summary>
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>
/// <param name="dither">Whether to apply dithering to the output image</param>
public WuQuantizer(bool dither)
: this(dither, DiffuseMode.FloydSteinberg, 255)
: this(GetDiffuser(dither), 255)
{
}
/// <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(true, DiffuseMode.FloydSteinberg, maxColors)
/// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</param>
public WuQuantizer(IErrorDiffuser diffuser)
: this(diffuser, 255)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="WuQuantizer"/> class.
/// </summary>
/// <param name="dither">Whether to apply dithering to the output image</param>
/// <param name="ditherType">The dithering algorithm to apply to the output image</param>
/// <param name="diffuser">The error diffusion algorithm, if any, to apply to the output image</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));
this.Dither = dither;
this.DitherType = ditherType;
this.Diffuser = diffuser;
this.MaxColors = maxColors;
}
/// <inheritdoc />
public bool Dither { get; }
/// <inheritdoc />
public IErrorDiffuser DitherType { get; }
public IErrorDiffuser Diffuser { get; }
/// <summary>
/// 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>()
where TPixel : struct, IPixel<TPixel>
=> 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 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<Rgba32>().Dither);
Assert.True(octree.CreateFrameQuantizer<Rgba32>().Dither);
Assert.True(wu.CreateFrameQuantizer<Rgba32>().Dither);
}
[Theory]

Loading…
Cancel
Save