Browse Source

FrameQuantizer<T> -> Quantizer<T>

pull/1574/head
Anton Firszov 6 years ago
parent
commit
e1ea534653
  1. 6
      src/ImageSharp/Advanced/AotCompilerTools.cs
  2. 2
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  3. 8
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  4. 2
      src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs
  5. 2
      src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs
  6. 2
      src/ImageSharp/Processing/Processors/Dithering/IDither.cs
  7. 4
      src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs
  8. 13
      src/ImageSharp/Processing/Processors/Quantization/FrameQuantizerUtilities.cs
  9. 12
      src/ImageSharp/Processing/Processors/Quantization/IFrameQuantizer{TPixel}.cs
  10. 8
      src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs
  11. 8
      src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs
  12. 12
      src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs
  13. 8
      src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs
  14. 9
      src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs
  15. 2
      src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs
  16. 8
      src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs
  17. 15
      src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs
  18. 6
      tests/ImageSharp.Tests/Processing/Processors/Quantization/OctreeQuantizerTests.cs
  19. 6
      tests/ImageSharp.Tests/Processing/Processors/Quantization/PaletteQuantizerTests.cs
  20. 6
      tests/ImageSharp.Tests/Processing/Processors/Quantization/WuQuantizerTests.cs
  21. 12
      tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs
  22. 10
      tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs

6
src/ImageSharp/Advanced/AotCompilerTools.cs

@ -112,7 +112,7 @@ namespace SixLabors.ImageSharp.Advanced
private static void AotCompileOctreeQuantizer<TPixel>()
where TPixel : unmanaged, IPixel<TPixel>
{
using (var test = new OctreeFrameQuantizer<TPixel>(Configuration.Default, new OctreeQuantizer().Options))
using (var test = new OctreeQuantizer<TPixel>(Configuration.Default, new OctreeQuantizer().Options))
{
var frame = new ImageFrame<TPixel>(Configuration.Default, 1, 1);
test.QuantizeFrame(frame, frame.Bounds());
@ -126,7 +126,7 @@ namespace SixLabors.ImageSharp.Advanced
private static void AotCompileWuQuantizer<TPixel>()
where TPixel : unmanaged, IPixel<TPixel>
{
using (var test = new WuFrameQuantizer<TPixel>(Configuration.Default, new WuQuantizer().Options))
using (var test = new WuQuantizer<TPixel>(Configuration.Default, new WuQuantizer().Options))
{
var frame = new ImageFrame<TPixel>(Configuration.Default, 1, 1);
test.QuantizeFrame(frame, frame.Bounds());
@ -140,7 +140,7 @@ namespace SixLabors.ImageSharp.Advanced
private static void AotCompilePaletteQuantizer<TPixel>()
where TPixel : unmanaged, IPixel<TPixel>
{
using (var test = (PaletteFrameQuantizer<TPixel>)new PaletteQuantizer(Array.Empty<Color>()).CreateFrameQuantizer<TPixel>(Configuration.Default))
using (var test = (PaletteQuantizer<TPixel>)new PaletteQuantizer(Array.Empty<Color>()).CreatePixelSpecificQuantizer<TPixel>(Configuration.Default))
{
var frame = new ImageFrame<TPixel>(Configuration.Default, 1, 1);
test.QuantizeFrame(frame, frame.Bounds());

2
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -336,7 +336,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
private void Write8BitColor<TPixel>(Stream stream, ImageFrame<TPixel> image, Span<byte> colorPalette)
where TPixel : unmanaged, IPixel<TPixel>
{
using IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(this.configuration);
using IQuantizer<TPixel> frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer<TPixel>(this.configuration);
using IndexedImageFrame<TPixel> quantized = frameQuantizer.QuantizeFrame(image, image.Bounds());
ReadOnlySpan<TPixel> quantizedColors = quantized.Palette.Span;

8
src/ImageSharp/Formats/Gif/GifEncoderCore.cs

@ -81,7 +81,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
// Quantize the image returning a palette.
IndexedImageFrame<TPixel> quantized;
using (IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(this.configuration))
using (IQuantizer<TPixel> frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer<TPixel>(this.configuration))
{
quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds());
}
@ -154,7 +154,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
pixelMap = new EuclideanPixelMap<TPixel>(this.configuration, quantized.Palette);
}
using var paletteFrameQuantizer = new PaletteFrameQuantizer<TPixel>(this.configuration, this.quantizer.Options, pixelMap);
using var paletteFrameQuantizer = new PaletteQuantizer<TPixel>(this.configuration, this.quantizer.Options, pixelMap);
using IndexedImageFrame<TPixel> paletteQuantized = paletteFrameQuantizer.QuantizeFrame(frame, frame.Bounds());
this.WriteImageData(paletteQuantized, stream);
}
@ -184,12 +184,12 @@ namespace SixLabors.ImageSharp.Formats.Gif
MaxColors = frameMetadata.ColorTableLength
};
using IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(this.configuration, options);
using IQuantizer<TPixel> frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer<TPixel>(this.configuration, options);
quantized = frameQuantizer.QuantizeFrame(frame, frame.Bounds());
}
else
{
using IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(this.configuration);
using IQuantizer<TPixel> frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer<TPixel>(this.configuration);
quantized = frameQuantizer.QuantizeFrame(frame, frame.Bounds());
}
}

2
src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs

@ -77,7 +77,7 @@ namespace SixLabors.ImageSharp.Formats.Png
}
// Create quantized frame returning the palette and set the bit depth.
using (IFrameQuantizer<TPixel> frameQuantizer = options.Quantizer.CreateFrameQuantizer<TPixel>(image.GetConfiguration()))
using (IQuantizer<TPixel> frameQuantizer = options.Quantizer.CreatePixelSpecificQuantizer<TPixel>(image.GetConfiguration()))
{
ImageFrame<TPixel> frame = image.Frames.RootFrame;
return frameQuantizer.QuantizeFrame(frame, frame.Bounds());

2
src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs

@ -93,7 +93,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
ImageFrame<TPixel> source,
IndexedImageFrame<TPixel> destination,
Rectangle bounds)
where TFrameQuantizer : struct, IFrameQuantizer<TPixel>
where TFrameQuantizer : struct, IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
int offsetY = bounds.Top;

2
src/ImageSharp/Processing/Processors/Dithering/IDither.cs

@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
ImageFrame<TPixel> source,
IndexedImageFrame<TPixel> destination,
Rectangle bounds)
where TFrameQuantizer : struct, IFrameQuantizer<TPixel>
where TFrameQuantizer : struct, IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>;
/// <summary>

4
src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs

@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
ImageFrame<TPixel> source,
IndexedImageFrame<TPixel> destination,
Rectangle bounds)
where TFrameQuantizer : struct, IFrameQuantizer<TPixel>
where TFrameQuantizer : struct, IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
var ditherOperation = new QuantizeDitherRowOperation<TFrameQuantizer, TPixel>(
@ -195,7 +195,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering
=> HashCode.Combine(this.thresholdMatrix, this.modulusX, this.modulusY);
private readonly struct QuantizeDitherRowOperation<TFrameQuantizer, TPixel> : IRowOperation
where TFrameQuantizer : struct, IFrameQuantizer<TPixel>
where TFrameQuantizer : struct, IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
private readonly TFrameQuantizer quantizer;

13
src/ImageSharp/Processing/Processors/Quantization/FrameQuantizerUtilities.cs

@ -11,7 +11,7 @@ using SixLabors.ImageSharp.Processing.Processors.Dithering;
namespace SixLabors.ImageSharp.Processing.Processors.Quantization
{
/// <summary>
/// Contains utility methods for <see cref="IFrameQuantizer{TPixel}"/> instances.
/// Contains utility methods for <see cref="IQuantizer{TPixel}"/> instances.
/// </summary>
public static class FrameQuantizerUtilities
{
@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="palette">The frame quantizer palette.</param>
/// <exception cref="InvalidOperationException">
/// The palette has not been built via <see cref="IFrameQuantizer{TPixel}.BuildPalette(ImageFrame{TPixel}, Rectangle)"/>
/// The palette has not been built via <see cref="IQuantizer{TPixel}.CollectPaletteColors"/>
/// </exception>
public static void CheckPaletteState<TPixel>(in ReadOnlyMemory<TPixel> palette)
where TPixel : unmanaged, IPixel<TPixel>
@ -48,14 +48,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
ref TFrameQuantizer quantizer,
ImageFrame<TPixel> source,
Rectangle bounds)
where TFrameQuantizer : struct, IFrameQuantizer<TPixel>
where TFrameQuantizer : struct, IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
Guard.NotNull(source, nameof(source));
var interest = Rectangle.Intersect(source.Bounds(), bounds);
BufferRegion<TPixel> region = source.PixelBuffer.GetRegion(interest);
// Collect the palette. Required before the second pass runs.
quantizer.BuildPalette(source, interest);
quantizer.CollectPaletteColors(region);
var destination = new IndexedImageFrame<TPixel>(
quantizer.Configuration,
@ -83,7 +84,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
ImageFrame<TPixel> source,
IndexedImageFrame<TPixel> destination,
Rectangle bounds)
where TFrameQuantizer : struct, IFrameQuantizer<TPixel>
where TFrameQuantizer : struct, IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
IDither dither = quantizer.Options.Dither;
@ -108,7 +109,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
}
private readonly struct RowIntervalOperation<TFrameQuantizer, TPixel> : IRowIntervalOperation
where TFrameQuantizer : struct, IFrameQuantizer<TPixel>
where TFrameQuantizer : struct, IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
private readonly TFrameQuantizer quantizer;

12
src/ImageSharp/Processing/Processors/Quantization/IFrameQuantizer{TPixel}.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Processing.Processors.Quantization
@ -10,7 +11,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// Provides methods to allow the execution of the quantization process on an image frame.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
public interface IFrameQuantizer<TPixel> : IDisposable
public interface IQuantizer<TPixel> : IDisposable
where TPixel : unmanaged, IPixel<TPixel>
{
/// <summary>
@ -27,16 +28,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// Gets the quantized color palette.
/// </summary>
/// <exception cref="InvalidOperationException">
/// The palette has not been built via <see cref="BuildPalette(ImageFrame{TPixel}, Rectangle)"/>.
/// The palette has not been built via <see cref="CollectPaletteColors"/>.
/// </exception>
ReadOnlyMemory<TPixel> Palette { get; }
/// <summary>
/// Builds the quantized palette from the given image frame and bounds.
/// Adds colors to the quantized palette from the given pixel source.
/// </summary>
/// <param name="source">The source image frame.</param>
/// <param name="bounds">The region of interest bounds.</param>
void BuildPalette(ImageFrame<TPixel> source, Rectangle bounds);
/// <param name="pixelRegion">The <see cref="BufferRegion{T}"/> of source pixels to register.</param>
void CollectPaletteColors(BufferRegion<TPixel> pixelRegion);
/// <summary>
/// Quantizes an image frame and return the resulting output pixels.

8
src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs

@ -20,8 +20,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </summary>
/// <param name="configuration">The <see cref="Configuration"/> to configure internal operations.</param>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>The <see cref="IFrameQuantizer{TPixel}"/>.</returns>
IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>(Configuration configuration)
/// <returns>The <see cref="IQuantizer{TPixel}"/>.</returns>
IQuantizer<TPixel> CreatePixelSpecificQuantizer<TPixel>(Configuration configuration)
where TPixel : unmanaged, IPixel<TPixel>;
/// <summary>
@ -30,8 +30,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="configuration">The <see cref="Configuration"/> to configure internal operations.</param>
/// <param name="options">The options to create the quantizer with.</param>
/// <returns>The <see cref="IFrameQuantizer{TPixel}"/>.</returns>
IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>(Configuration configuration, QuantizerOptions options)
/// <returns>The <see cref="IQuantizer{TPixel}"/>.</returns>
IQuantizer<TPixel> CreatePixelSpecificQuantizer<TPixel>(Configuration configuration, QuantizerOptions options)
where TPixel : unmanaged, IPixel<TPixel>;
}
}

8
src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs

@ -36,13 +36,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
public QuantizerOptions Options { get; }
/// <inheritdoc />
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>(Configuration configuration)
public IQuantizer<TPixel> CreatePixelSpecificQuantizer<TPixel>(Configuration configuration)
where TPixel : unmanaged, IPixel<TPixel>
=> this.CreateFrameQuantizer<TPixel>(configuration, this.Options);
=> this.CreatePixelSpecificQuantizer<TPixel>(configuration, this.Options);
/// <inheritdoc />
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>(Configuration configuration, QuantizerOptions options)
public IQuantizer<TPixel> CreatePixelSpecificQuantizer<TPixel>(Configuration configuration, QuantizerOptions options)
where TPixel : unmanaged, IPixel<TPixel>
=> new OctreeFrameQuantizer<TPixel>(configuration, options);
=> new OctreeQuantizer<TPixel>(configuration, options);
}
}

12
src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs → src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs

@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// <see href="http://msdn.microsoft.com/en-us/library/aa479306.aspx"/>
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
public struct OctreeFrameQuantizer<TPixel> : IFrameQuantizer<TPixel>
public struct OctreeQuantizer<TPixel> : IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
private readonly int maxColors;
@ -29,12 +29,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="OctreeFrameQuantizer{TPixel}"/> struct.
/// Initializes a new instance of the <see cref="OctreeQuantizer{TPixel}"/> struct.
/// </summary>
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param>
/// <param name="options">The quantizer options defining quantization rules.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public OctreeFrameQuantizer(Configuration configuration, QuantizerOptions options)
public OctreeQuantizer(Configuration configuration, QuantizerOptions options)
{
Guard.NotNull(configuration, nameof(configuration));
Guard.NotNull(options, nameof(options));
@ -69,15 +69,17 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void BuildPalette(ImageFrame<TPixel> source, Rectangle bounds)
public void CollectPaletteColors(BufferRegion<TPixel> pixelRegion)
{
Rectangle bounds = pixelRegion.Rectangle;
Buffer2D<TPixel> source = pixelRegion.Buffer;
using IMemoryOwner<Rgba32> buffer = this.Configuration.MemoryAllocator.Allocate<Rgba32>(bounds.Width);
Span<Rgba32> bufferSpan = buffer.GetSpan();
// Loop through each row
for (int y = bounds.Top; y < bounds.Bottom; y++)
{
Span<TPixel> row = source.GetPixelRowSpan(y).Slice(bounds.Left, bounds.Width);
Span<TPixel> row = source.GetRowSpan(y).Slice(bounds.Left, bounds.Width);
PixelOperations<TPixel>.Instance.ToRgba32(this.Configuration, row, bufferSpan);
for (int x = 0; x < bufferSpan.Length; x++)

8
src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs

@ -41,12 +41,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
public QuantizerOptions Options { get; }
/// <inheritdoc />
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>(Configuration configuration)
public IQuantizer<TPixel> CreatePixelSpecificQuantizer<TPixel>(Configuration configuration)
where TPixel : unmanaged, IPixel<TPixel>
=> this.CreateFrameQuantizer<TPixel>(configuration, this.Options);
=> this.CreatePixelSpecificQuantizer<TPixel>(configuration, this.Options);
/// <inheritdoc />
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>(Configuration configuration, QuantizerOptions options)
public IQuantizer<TPixel> CreatePixelSpecificQuantizer<TPixel>(Configuration configuration, QuantizerOptions options)
where TPixel : unmanaged, IPixel<TPixel>
{
Guard.NotNull(options, nameof(options));
@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
Color.ToPixel(configuration, this.colorPalette.Span, palette.AsSpan());
var pixelMap = new EuclideanPixelMap<TPixel>(configuration, palette);
return new PaletteFrameQuantizer<TPixel>(configuration, options, pixelMap);
return new PaletteQuantizer<TPixel>(configuration, options, pixelMap);
}
}
}

9
src/ImageSharp/Processing/Processors/Quantization/PaletteFrameQuantizer{TPixel}.cs → src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs

@ -3,6 +3,7 @@
using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Processing.Processors.Quantization
@ -12,19 +13,19 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// <see href="http://msdn.microsoft.com/en-us/library/aa479306.aspx"/>
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal struct PaletteFrameQuantizer<TPixel> : IFrameQuantizer<TPixel>
internal struct PaletteQuantizer<TPixel> : IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
private readonly EuclideanPixelMap<TPixel> pixelMap;
/// <summary>
/// Initializes a new instance of the <see cref="PaletteFrameQuantizer{TPixel}"/> struct.
/// Initializes a new instance of the <see cref="PaletteQuantizer{TPixel}"/> struct.
/// </summary>
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param>
/// <param name="options">The quantizer options defining quantization rules.</param>
/// <param name="pixelMap">The pixel map for looking up color matches from a predefined palette.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public PaletteFrameQuantizer(
public PaletteQuantizer(
Configuration configuration,
QuantizerOptions options,
EuclideanPixelMap<TPixel> pixelMap)
@ -53,7 +54,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// <inheritdoc/>
[MethodImpl(InliningOptions.ShortMethod)]
public void BuildPalette(ImageFrame<TPixel> source, Rectangle bounds)
public void CollectPaletteColors(BufferRegion<TPixel> pixelRegion)
{
}

2
src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs

@ -38,7 +38,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
var interest = Rectangle.Intersect(source.Bounds(), this.SourceRectangle);
Configuration configuration = this.Configuration;
using IFrameQuantizer<TPixel> frameQuantizer = this.quantizer.CreateFrameQuantizer<TPixel>(configuration);
using IQuantizer<TPixel> frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer<TPixel>(configuration);
using IndexedImageFrame<TPixel> quantized = frameQuantizer.QuantizeFrame(source, interest);
var operation = new RowIntervalOperation(this.SourceRectangle, source, quantized);

8
src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs

@ -35,13 +35,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
public QuantizerOptions Options { get; }
/// <inheritdoc />
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>(Configuration configuration)
public IQuantizer<TPixel> CreatePixelSpecificQuantizer<TPixel>(Configuration configuration)
where TPixel : unmanaged, IPixel<TPixel>
=> this.CreateFrameQuantizer<TPixel>(configuration, this.Options);
=> this.CreatePixelSpecificQuantizer<TPixel>(configuration, this.Options);
/// <inheritdoc />
public IFrameQuantizer<TPixel> CreateFrameQuantizer<TPixel>(Configuration configuration, QuantizerOptions options)
public IQuantizer<TPixel> CreatePixelSpecificQuantizer<TPixel>(Configuration configuration, QuantizerOptions options)
where TPixel : unmanaged, IPixel<TPixel>
=> new WuFrameQuantizer<TPixel>(configuration, options);
=> new WuQuantizer<TPixel>(configuration, options);
}
}

15
src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs → src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs

@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </para>
/// </remarks>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal struct WuFrameQuantizer<TPixel> : IFrameQuantizer<TPixel>
internal struct WuQuantizer<TPixel> : IQuantizer<TPixel>
where TPixel : unmanaged, IPixel<TPixel>
{
private readonly MemoryAllocator memoryAllocator;
@ -77,12 +77,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="WuFrameQuantizer{TPixel}"/> struct.
/// Initializes a new instance of the <see cref="WuQuantizer{TPixel}"/> struct.
/// </summary>
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param>
/// <param name="options">The quantizer options defining quantization rules.</param>
[MethodImpl(InliningOptions.ShortMethod)]
public WuFrameQuantizer(Configuration configuration, QuantizerOptions options)
public WuQuantizer(Configuration configuration, QuantizerOptions options)
{
Guard.NotNull(configuration, nameof(configuration));
Guard.NotNull(options, nameof(options));
@ -118,8 +118,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
}
/// <inheritdoc/>
public void BuildPalette(ImageFrame<TPixel> source, Rectangle bounds)
public void CollectPaletteColors(BufferRegion<TPixel> pixelRegion)
{
Rectangle bounds = pixelRegion.Rectangle;
Buffer2D<TPixel> source = pixelRegion.Buffer;
this.Build3DHistogram(source, bounds);
this.Get3DMoments(this.memoryAllocator);
this.BuildCube();
@ -360,7 +363,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </summary>
/// <param name="source">The source data.</param>
/// <param name="bounds">The bounds within the source image to quantize.</param>
private void Build3DHistogram(ImageFrame<TPixel> source, Rectangle bounds)
private void Build3DHistogram(Buffer2D<TPixel> source, Rectangle bounds)
{
Span<Moment> momentSpan = this.momentsOwner.GetSpan();
@ -370,7 +373,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
for (int y = bounds.Top; y < bounds.Bottom; y++)
{
Span<TPixel> row = source.GetPixelRowSpan(y).Slice(bounds.Left, bounds.Width);
Span<TPixel> row = source.GetRowSpan(y).Slice(bounds.Left, bounds.Width);
PixelOperations<TPixel>.Instance.ToRgba32(this.Configuration, row, bufferSpan);
for (int x = 0; x < bufferSpan.Length; x++)

6
tests/ImageSharp.Tests/Processing/Processors/Quantization/OctreeQuantizerTests.cs

@ -39,7 +39,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Quantization
public void OctreeQuantizerCanCreateFrameQuantizer()
{
var quantizer = new OctreeQuantizer();
IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(Configuration.Default);
IQuantizer<Rgba32> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(Configuration.Default);
Assert.NotNull(frameQuantizer);
Assert.NotNull(frameQuantizer.Options);
@ -47,14 +47,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Quantization
frameQuantizer.Dispose();
quantizer = new OctreeQuantizer(new QuantizerOptions { Dither = null });
frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(Configuration.Default);
frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(Configuration.Default);
Assert.NotNull(frameQuantizer);
Assert.Null(frameQuantizer.Options.Dither);
frameQuantizer.Dispose();
quantizer = new OctreeQuantizer(new QuantizerOptions { Dither = KnownDitherings.Atkinson });
frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(Configuration.Default);
frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(Configuration.Default);
Assert.NotNull(frameQuantizer);
Assert.Equal(KnownDitherings.Atkinson, frameQuantizer.Options.Dither);
frameQuantizer.Dispose();

6
tests/ImageSharp.Tests/Processing/Processors/Quantization/PaletteQuantizerTests.cs

@ -41,7 +41,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Quantization
public void PaletteQuantizerCanCreateFrameQuantizer()
{
var quantizer = new PaletteQuantizer(Palette);
IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(Configuration.Default);
IQuantizer<Rgba32> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(Configuration.Default);
Assert.NotNull(frameQuantizer);
Assert.NotNull(frameQuantizer.Options);
@ -49,14 +49,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Quantization
frameQuantizer.Dispose();
quantizer = new PaletteQuantizer(Palette, new QuantizerOptions { Dither = null });
frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(Configuration.Default);
frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(Configuration.Default);
Assert.NotNull(frameQuantizer);
Assert.Null(frameQuantizer.Options.Dither);
frameQuantizer.Dispose();
quantizer = new PaletteQuantizer(Palette, new QuantizerOptions { Dither = KnownDitherings.Atkinson });
frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(Configuration.Default);
frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(Configuration.Default);
Assert.NotNull(frameQuantizer);
Assert.Equal(KnownDitherings.Atkinson, frameQuantizer.Options.Dither);
frameQuantizer.Dispose();

6
tests/ImageSharp.Tests/Processing/Processors/Quantization/WuQuantizerTests.cs

@ -39,7 +39,7 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Quantization
public void WuQuantizerCanCreateFrameQuantizer()
{
var quantizer = new WuQuantizer();
IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(Configuration.Default);
IQuantizer<Rgba32> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(Configuration.Default);
Assert.NotNull(frameQuantizer);
Assert.NotNull(frameQuantizer.Options);
@ -47,14 +47,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Quantization
frameQuantizer.Dispose();
quantizer = new WuQuantizer(new QuantizerOptions { Dither = null });
frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(Configuration.Default);
frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(Configuration.Default);
Assert.NotNull(frameQuantizer);
Assert.Null(frameQuantizer.Options.Dither);
frameQuantizer.Dispose();
quantizer = new WuQuantizer(new QuantizerOptions { Dither = KnownDitherings.Atkinson });
frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(Configuration.Default);
frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(Configuration.Default);
Assert.NotNull(frameQuantizer);
Assert.Equal(KnownDitherings.Atkinson, frameQuantizer.Options.Dither);
frameQuantizer.Dispose();

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

@ -27,22 +27,22 @@ namespace SixLabors.ImageSharp.Tests
Assert.NotNull(octree.Options.Dither);
Assert.NotNull(wu.Options.Dither);
using (IFrameQuantizer<Rgba32> quantizer = werner.CreateFrameQuantizer<Rgba32>(this.Configuration))
using (IQuantizer<Rgba32> quantizer = werner.CreatePixelSpecificQuantizer<Rgba32>(this.Configuration))
{
Assert.NotNull(quantizer.Options.Dither);
}
using (IFrameQuantizer<Rgba32> quantizer = webSafe.CreateFrameQuantizer<Rgba32>(this.Configuration))
using (IQuantizer<Rgba32> quantizer = webSafe.CreatePixelSpecificQuantizer<Rgba32>(this.Configuration))
{
Assert.NotNull(quantizer.Options.Dither);
}
using (IFrameQuantizer<Rgba32> quantizer = octree.CreateFrameQuantizer<Rgba32>(this.Configuration))
using (IQuantizer<Rgba32> quantizer = octree.CreatePixelSpecificQuantizer<Rgba32>(this.Configuration))
{
Assert.NotNull(quantizer.Options.Dither);
}
using (IFrameQuantizer<Rgba32> quantizer = wu.CreateFrameQuantizer<Rgba32>(this.Configuration))
using (IQuantizer<Rgba32> quantizer = wu.CreatePixelSpecificQuantizer<Rgba32>(this.Configuration))
{
Assert.NotNull(quantizer.Options.Dither);
}
@ -70,7 +70,7 @@ namespace SixLabors.ImageSharp.Tests
foreach (ImageFrame<TPixel> frame in image.Frames)
{
using (IFrameQuantizer<TPixel> frameQuantizer = quantizer.CreateFrameQuantizer<TPixel>(this.Configuration))
using (IQuantizer<TPixel> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<TPixel>(this.Configuration))
using (IndexedImageFrame<TPixel> quantized = frameQuantizer.QuantizeFrame(frame, frame.Bounds()))
{
int index = this.GetTransparentIndex(quantized);
@ -100,7 +100,7 @@ namespace SixLabors.ImageSharp.Tests
foreach (ImageFrame<TPixel> frame in image.Frames)
{
using (IFrameQuantizer<TPixel> frameQuantizer = quantizer.CreateFrameQuantizer<TPixel>(this.Configuration))
using (IQuantizer<TPixel> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<TPixel>(this.Configuration))
using (IndexedImageFrame<TPixel> quantized = frameQuantizer.QuantizeFrame(frame, frame.Bounds()))
{
int index = this.GetTransparentIndex(quantized);

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

@ -20,7 +20,7 @@ namespace SixLabors.ImageSharp.Tests.Quantization
using var image = new Image<Rgba32>(config, 1, 1, Color.Black);
ImageFrame<Rgba32> frame = image.Frames.RootFrame;
using IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(config);
using IQuantizer<Rgba32> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(config);
using IndexedImageFrame<Rgba32> result = frameQuantizer.QuantizeFrame(frame, frame.Bounds());
Assert.Equal(1, result.Palette.Length);
@ -40,7 +40,7 @@ namespace SixLabors.ImageSharp.Tests.Quantization
using var image = new Image<Rgba32>(config, 1, 1, default(Rgba32));
ImageFrame<Rgba32> frame = image.Frames.RootFrame;
using IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(config);
using IQuantizer<Rgba32> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(config);
using IndexedImageFrame<Rgba32> result = frameQuantizer.QuantizeFrame(frame, frame.Bounds());
Assert.Equal(1, result.Palette.Length);
@ -86,7 +86,7 @@ namespace SixLabors.ImageSharp.Tests.Quantization
ImageFrame<Rgba32> frame = image.Frames.RootFrame;
using IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(config);
using IQuantizer<Rgba32> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(config);
using IndexedImageFrame<Rgba32> result = frameQuantizer.QuantizeFrame(frame, frame.Bounds());
Assert.Equal(256, result.Palette.Length);
@ -126,7 +126,7 @@ namespace SixLabors.ImageSharp.Tests.Quantization
var quantizer = new WuQuantizer(new QuantizerOptions { Dither = null });
ImageFrame<TPixel> frame = image.Frames.RootFrame;
using IFrameQuantizer<TPixel> frameQuantizer = quantizer.CreateFrameQuantizer<TPixel>(config);
using IQuantizer<TPixel> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<TPixel>(config);
using IndexedImageFrame<TPixel> result = frameQuantizer.QuantizeFrame(frame, frame.Bounds());
Assert.Equal(48, result.Palette.Length);
@ -155,7 +155,7 @@ namespace SixLabors.ImageSharp.Tests.Quantization
var quantizer = new WuQuantizer(new QuantizerOptions { Dither = null });
ImageFrame<Rgba32> frame = image.Frames.RootFrame;
using (IFrameQuantizer<Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer<Rgba32>(config))
using (IQuantizer<Rgba32> frameQuantizer = quantizer.CreatePixelSpecificQuantizer<Rgba32>(config))
using (IndexedImageFrame<Rgba32> result = frameQuantizer.QuantizeFrame(frame, frame.Bounds()))
{
Assert.Equal(4 * 8, result.Palette.Length);

Loading…
Cancel
Save