diff --git a/src/ImageSharp/Advanced/AotCompilerTools.cs b/src/ImageSharp/Advanced/AotCompilerTools.cs index 23ae62c7a..d692292b0 100644 --- a/src/ImageSharp/Advanced/AotCompilerTools.cs +++ b/src/ImageSharp/Advanced/AotCompilerTools.cs @@ -112,7 +112,7 @@ namespace SixLabors.ImageSharp.Advanced private static void AotCompileOctreeQuantizer() where TPixel : unmanaged, IPixel { - using (var test = new OctreeFrameQuantizer(Configuration.Default, new OctreeQuantizer().Options)) + using (var test = new OctreeQuantizer(Configuration.Default, new OctreeQuantizer().Options)) { var frame = new ImageFrame(Configuration.Default, 1, 1); test.QuantizeFrame(frame, frame.Bounds()); @@ -126,7 +126,7 @@ namespace SixLabors.ImageSharp.Advanced private static void AotCompileWuQuantizer() where TPixel : unmanaged, IPixel { - using (var test = new WuFrameQuantizer(Configuration.Default, new WuQuantizer().Options)) + using (var test = new WuQuantizer(Configuration.Default, new WuQuantizer().Options)) { var frame = new ImageFrame(Configuration.Default, 1, 1); test.QuantizeFrame(frame, frame.Bounds()); @@ -140,7 +140,7 @@ namespace SixLabors.ImageSharp.Advanced private static void AotCompilePaletteQuantizer() where TPixel : unmanaged, IPixel { - using (var test = (PaletteFrameQuantizer)new PaletteQuantizer(Array.Empty()).CreateFrameQuantizer(Configuration.Default)) + using (var test = (PaletteQuantizer)new PaletteQuantizer(Array.Empty()).CreatePixelSpecificQuantizer(Configuration.Default)) { var frame = new ImageFrame(Configuration.Default, 1, 1); test.QuantizeFrame(frame, frame.Bounds()); diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 7d2799503..c7c53037f 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -336,7 +336,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp private void Write8BitColor(Stream stream, ImageFrame image, Span colorPalette) where TPixel : unmanaged, IPixel { - using IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(this.configuration); + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration); using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(image, image.Bounds()); ReadOnlySpan quantizedColors = quantized.Palette.Span; diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 62410025c..ae5f62443 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -81,7 +81,7 @@ namespace SixLabors.ImageSharp.Formats.Gif // Quantize the image returning a palette. IndexedImageFrame quantized; - using (IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(this.configuration)) + using (IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration)) { quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame, image.Bounds()); } @@ -154,7 +154,7 @@ namespace SixLabors.ImageSharp.Formats.Gif pixelMap = new EuclideanPixelMap(this.configuration, quantized.Palette); } - using var paletteFrameQuantizer = new PaletteFrameQuantizer(this.configuration, this.quantizer.Options, pixelMap); + using var paletteFrameQuantizer = new PaletteQuantizer(this.configuration, this.quantizer.Options, pixelMap); using IndexedImageFrame paletteQuantized = paletteFrameQuantizer.QuantizeFrame(frame, frame.Bounds()); this.WriteImageData(paletteQuantized, stream); } @@ -184,12 +184,12 @@ namespace SixLabors.ImageSharp.Formats.Gif MaxColors = frameMetadata.ColorTableLength }; - using IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(this.configuration, options); + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration, options); quantized = frameQuantizer.QuantizeFrame(frame, frame.Bounds()); } else { - using IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(this.configuration); + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration); quantized = frameQuantizer.QuantizeFrame(frame, frame.Bounds()); } } diff --git a/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs b/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs index 3f490ca6f..8cfd2af35 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs +++ b/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 frameQuantizer = options.Quantizer.CreateFrameQuantizer(image.GetConfiguration())) + using (IQuantizer frameQuantizer = options.Quantizer.CreatePixelSpecificQuantizer(image.GetConfiguration())) { ImageFrame frame = image.Frames.RootFrame; return frameQuantizer.QuantizeFrame(frame, frame.Bounds()); diff --git a/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs b/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs index 7d30bada6..e322afb8d 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/ErrorDither.cs @@ -93,7 +93,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering ImageFrame source, IndexedImageFrame destination, Rectangle bounds) - where TFrameQuantizer : struct, IFrameQuantizer + where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel { int offsetY = bounds.Top; diff --git a/src/ImageSharp/Processing/Processors/Dithering/IDither.cs b/src/ImageSharp/Processing/Processors/Dithering/IDither.cs index 8f9d82537..8b0e1b8a8 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/IDither.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/IDither.cs @@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering ImageFrame source, IndexedImageFrame destination, Rectangle bounds) - where TFrameQuantizer : struct, IFrameQuantizer + where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel; /// diff --git a/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs b/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs index 6862cff00..0f5a7fb55 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs @@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering ImageFrame source, IndexedImageFrame destination, Rectangle bounds) - where TFrameQuantizer : struct, IFrameQuantizer + where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel { var ditherOperation = new QuantizeDitherRowOperation( @@ -195,7 +195,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering => HashCode.Combine(this.thresholdMatrix, this.modulusX, this.modulusY); private readonly struct QuantizeDitherRowOperation : IRowOperation - where TFrameQuantizer : struct, IFrameQuantizer + where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel { private readonly TFrameQuantizer quantizer; diff --git a/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizerUtilities.cs b/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizerUtilities.cs index 4d75042ea..c74ea4959 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizerUtilities.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizerUtilities.cs @@ -11,7 +11,7 @@ using SixLabors.ImageSharp.Processing.Processors.Dithering; namespace SixLabors.ImageSharp.Processing.Processors.Quantization { /// - /// Contains utility methods for instances. + /// Contains utility methods for instances. /// public static class FrameQuantizerUtilities { @@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// The pixel format. /// The frame quantizer palette. /// - /// The palette has not been built via + /// The palette has not been built via /// public static void CheckPaletteState(in ReadOnlyMemory palette) where TPixel : unmanaged, IPixel @@ -48,14 +48,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization ref TFrameQuantizer quantizer, ImageFrame source, Rectangle bounds) - where TFrameQuantizer : struct, IFrameQuantizer + where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel { Guard.NotNull(source, nameof(source)); var interest = Rectangle.Intersect(source.Bounds(), bounds); + BufferRegion 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( quantizer.Configuration, @@ -83,7 +84,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization ImageFrame source, IndexedImageFrame destination, Rectangle bounds) - where TFrameQuantizer : struct, IFrameQuantizer + where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel { IDither dither = quantizer.Options.Dither; @@ -108,7 +109,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization } private readonly struct RowIntervalOperation : IRowIntervalOperation - where TFrameQuantizer : struct, IFrameQuantizer + where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel { private readonly TFrameQuantizer quantizer; diff --git a/src/ImageSharp/Processing/Processors/Quantization/IFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/IFrameQuantizer{TPixel}.cs index cc87715eb..33ba61747 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/IFrameQuantizer{TPixel}.cs +++ b/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. /// /// The pixel format. - public interface IFrameQuantizer : IDisposable + public interface IQuantizer : IDisposable where TPixel : unmanaged, IPixel { /// @@ -27,16 +28,15 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// Gets the quantized color palette. /// /// - /// The palette has not been built via . + /// The palette has not been built via . /// ReadOnlyMemory Palette { get; } /// - /// Builds the quantized palette from the given image frame and bounds. + /// Adds colors to the quantized palette from the given pixel source. /// - /// The source image frame. - /// The region of interest bounds. - void BuildPalette(ImageFrame source, Rectangle bounds); + /// The of source pixels to register. + void CollectPaletteColors(BufferRegion pixelRegion); /// /// Quantizes an image frame and return the resulting output pixels. diff --git a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs index 01e4b5e8a..e59b9a6a7 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/IQuantizer.cs @@ -20,8 +20,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// The to configure internal operations. /// The pixel format. - /// The . - IFrameQuantizer CreateFrameQuantizer(Configuration configuration) + /// The . + IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel; /// @@ -30,8 +30,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// The pixel format. /// The to configure internal operations. /// The options to create the quantizer with. - /// The . - IFrameQuantizer CreateFrameQuantizer(Configuration configuration, QuantizerOptions options) + /// The . + IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel; } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs index 9e04edef0..02e85167d 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs @@ -36,13 +36,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization public QuantizerOptions Options { get; } /// - public IFrameQuantizer CreateFrameQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel - => this.CreateFrameQuantizer(configuration, this.Options); + => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IFrameQuantizer CreateFrameQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel - => new OctreeFrameQuantizer(configuration, options); + => new OctreeQuantizer(configuration, options); } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs similarity index 97% rename from src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs rename to src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs index ce2e406d4..2a9b8d79b 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// /// The pixel format. - public struct OctreeFrameQuantizer : IFrameQuantizer + public struct OctreeQuantizer : IQuantizer where TPixel : unmanaged, IPixel { private readonly int maxColors; @@ -29,12 +29,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization private bool isDisposed; /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the struct. /// /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [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 /// [MethodImpl(InliningOptions.ShortMethod)] - public void BuildPalette(ImageFrame source, Rectangle bounds) + public void CollectPaletteColors(BufferRegion pixelRegion) { + Rectangle bounds = pixelRegion.Rectangle; + Buffer2D source = pixelRegion.Buffer; using IMemoryOwner buffer = this.Configuration.MemoryAllocator.Allocate(bounds.Width); Span bufferSpan = buffer.GetSpan(); // Loop through each row for (int y = bounds.Top; y < bounds.Bottom; y++) { - Span row = source.GetPixelRowSpan(y).Slice(bounds.Left, bounds.Width); + Span row = source.GetRowSpan(y).Slice(bounds.Left, bounds.Width); PixelOperations.Instance.ToRgba32(this.Configuration, row, bufferSpan); for (int x = 0; x < bufferSpan.Length; x++) diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs index c14ea6153..38816a168 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs @@ -41,12 +41,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization public QuantizerOptions Options { get; } /// - public IFrameQuantizer CreateFrameQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel - => this.CreateFrameQuantizer(configuration, this.Options); + => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IFrameQuantizer CreateFrameQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel { 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(configuration, palette); - return new PaletteFrameQuantizer(configuration, options, pixelMap); + return new PaletteQuantizer(configuration, options, pixelMap); } } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs similarity index 91% rename from src/ImageSharp/Processing/Processors/Quantization/PaletteFrameQuantizer{TPixel}.cs rename to src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs index ade73e2d0..e40cf0c8c 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteFrameQuantizer{TPixel}.cs +++ b/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 /// /// /// The pixel format. - internal struct PaletteFrameQuantizer : IFrameQuantizer + internal struct PaletteQuantizer : IQuantizer where TPixel : unmanaged, IPixel { private readonly EuclideanPixelMap pixelMap; /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the struct. /// /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. /// The pixel map for looking up color matches from a predefined palette. [MethodImpl(InliningOptions.ShortMethod)] - public PaletteFrameQuantizer( + public PaletteQuantizer( Configuration configuration, QuantizerOptions options, EuclideanPixelMap pixelMap) @@ -53,7 +54,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// [MethodImpl(InliningOptions.ShortMethod)] - public void BuildPalette(ImageFrame source, Rectangle bounds) + public void CollectPaletteColors(BufferRegion pixelRegion) { } diff --git a/src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs index 386caf1be..dce769692 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs +++ b/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 frameQuantizer = this.quantizer.CreateFrameQuantizer(configuration); + using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(configuration); using IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(source, interest); var operation = new RowIntervalOperation(this.SourceRectangle, source, quantized); diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs index d2e33aa1f..0f1dff181 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs @@ -35,13 +35,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization public QuantizerOptions Options { get; } /// - public IFrameQuantizer CreateFrameQuantizer(Configuration configuration) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration) where TPixel : unmanaged, IPixel - => this.CreateFrameQuantizer(configuration, this.Options); + => this.CreatePixelSpecificQuantizer(configuration, this.Options); /// - public IFrameQuantizer CreateFrameQuantizer(Configuration configuration, QuantizerOptions options) + public IQuantizer CreatePixelSpecificQuantizer(Configuration configuration, QuantizerOptions options) where TPixel : unmanaged, IPixel - => new WuFrameQuantizer(configuration, options); + => new WuQuantizer(configuration, options); } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs similarity index 98% rename from src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs rename to src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs index d15db74e6..a83bf3b6d 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs @@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// /// The pixel format. - internal struct WuFrameQuantizer : IFrameQuantizer + internal struct WuQuantizer : IQuantizer where TPixel : unmanaged, IPixel { private readonly MemoryAllocator memoryAllocator; @@ -77,12 +77,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization private bool isDisposed; /// - /// Initializes a new instance of the struct. + /// Initializes a new instance of the struct. /// /// The configuration which allows altering default behaviour or extending the library. /// The quantizer options defining quantization rules. [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 } /// - public void BuildPalette(ImageFrame source, Rectangle bounds) + public void CollectPaletteColors(BufferRegion pixelRegion) { + Rectangle bounds = pixelRegion.Rectangle; + Buffer2D source = pixelRegion.Buffer; + this.Build3DHistogram(source, bounds); this.Get3DMoments(this.memoryAllocator); this.BuildCube(); @@ -360,7 +363,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// The source data. /// The bounds within the source image to quantize. - private void Build3DHistogram(ImageFrame source, Rectangle bounds) + private void Build3DHistogram(Buffer2D source, Rectangle bounds) { Span momentSpan = this.momentsOwner.GetSpan(); @@ -370,7 +373,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization for (int y = bounds.Top; y < bounds.Bottom; y++) { - Span row = source.GetPixelRowSpan(y).Slice(bounds.Left, bounds.Width); + Span row = source.GetRowSpan(y).Slice(bounds.Left, bounds.Width); PixelOperations.Instance.ToRgba32(this.Configuration, row, bufferSpan); for (int x = 0; x < bufferSpan.Length; x++) diff --git a/tests/ImageSharp.Tests/Processing/Processors/Quantization/OctreeQuantizerTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Quantization/OctreeQuantizerTests.cs index bb7921d68..dd27164f3 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Quantization/OctreeQuantizerTests.cs +++ b/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 frameQuantizer = quantizer.CreateFrameQuantizer(Configuration.Default); + IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(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(Configuration.Default); + frameQuantizer = quantizer.CreatePixelSpecificQuantizer(Configuration.Default); Assert.NotNull(frameQuantizer); Assert.Null(frameQuantizer.Options.Dither); frameQuantizer.Dispose(); quantizer = new OctreeQuantizer(new QuantizerOptions { Dither = KnownDitherings.Atkinson }); - frameQuantizer = quantizer.CreateFrameQuantizer(Configuration.Default); + frameQuantizer = quantizer.CreatePixelSpecificQuantizer(Configuration.Default); Assert.NotNull(frameQuantizer); Assert.Equal(KnownDitherings.Atkinson, frameQuantizer.Options.Dither); frameQuantizer.Dispose(); diff --git a/tests/ImageSharp.Tests/Processing/Processors/Quantization/PaletteQuantizerTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Quantization/PaletteQuantizerTests.cs index 3c1fa11ab..acc9a0e01 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Quantization/PaletteQuantizerTests.cs +++ b/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 frameQuantizer = quantizer.CreateFrameQuantizer(Configuration.Default); + IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(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(Configuration.Default); + frameQuantizer = quantizer.CreatePixelSpecificQuantizer(Configuration.Default); Assert.NotNull(frameQuantizer); Assert.Null(frameQuantizer.Options.Dither); frameQuantizer.Dispose(); quantizer = new PaletteQuantizer(Palette, new QuantizerOptions { Dither = KnownDitherings.Atkinson }); - frameQuantizer = quantizer.CreateFrameQuantizer(Configuration.Default); + frameQuantizer = quantizer.CreatePixelSpecificQuantizer(Configuration.Default); Assert.NotNull(frameQuantizer); Assert.Equal(KnownDitherings.Atkinson, frameQuantizer.Options.Dither); frameQuantizer.Dispose(); diff --git a/tests/ImageSharp.Tests/Processing/Processors/Quantization/WuQuantizerTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Quantization/WuQuantizerTests.cs index eb9d738e9..d78e9254c 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Quantization/WuQuantizerTests.cs +++ b/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 frameQuantizer = quantizer.CreateFrameQuantizer(Configuration.Default); + IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(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(Configuration.Default); + frameQuantizer = quantizer.CreatePixelSpecificQuantizer(Configuration.Default); Assert.NotNull(frameQuantizer); Assert.Null(frameQuantizer.Options.Dither); frameQuantizer.Dispose(); quantizer = new WuQuantizer(new QuantizerOptions { Dither = KnownDitherings.Atkinson }); - frameQuantizer = quantizer.CreateFrameQuantizer(Configuration.Default); + frameQuantizer = quantizer.CreatePixelSpecificQuantizer(Configuration.Default); Assert.NotNull(frameQuantizer); Assert.Equal(KnownDitherings.Atkinson, frameQuantizer.Options.Dither); frameQuantizer.Dispose(); diff --git a/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs b/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs index 7945741b0..86229a7b6 100644 --- a/tests/ImageSharp.Tests/Quantization/QuantizedImageTests.cs +++ b/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 quantizer = werner.CreateFrameQuantizer(this.Configuration)) + using (IQuantizer quantizer = werner.CreatePixelSpecificQuantizer(this.Configuration)) { Assert.NotNull(quantizer.Options.Dither); } - using (IFrameQuantizer quantizer = webSafe.CreateFrameQuantizer(this.Configuration)) + using (IQuantizer quantizer = webSafe.CreatePixelSpecificQuantizer(this.Configuration)) { Assert.NotNull(quantizer.Options.Dither); } - using (IFrameQuantizer quantizer = octree.CreateFrameQuantizer(this.Configuration)) + using (IQuantizer quantizer = octree.CreatePixelSpecificQuantizer(this.Configuration)) { Assert.NotNull(quantizer.Options.Dither); } - using (IFrameQuantizer quantizer = wu.CreateFrameQuantizer(this.Configuration)) + using (IQuantizer quantizer = wu.CreatePixelSpecificQuantizer(this.Configuration)) { Assert.NotNull(quantizer.Options.Dither); } @@ -70,7 +70,7 @@ namespace SixLabors.ImageSharp.Tests foreach (ImageFrame frame in image.Frames) { - using (IFrameQuantizer frameQuantizer = quantizer.CreateFrameQuantizer(this.Configuration)) + using (IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.Configuration)) using (IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(frame, frame.Bounds())) { int index = this.GetTransparentIndex(quantized); @@ -100,7 +100,7 @@ namespace SixLabors.ImageSharp.Tests foreach (ImageFrame frame in image.Frames) { - using (IFrameQuantizer frameQuantizer = quantizer.CreateFrameQuantizer(this.Configuration)) + using (IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(this.Configuration)) using (IndexedImageFrame quantized = frameQuantizer.QuantizeFrame(frame, frame.Bounds())) { int index = this.GetTransparentIndex(quantized); diff --git a/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs b/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs index 37b8cab60..75e88ba79 100644 --- a/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs +++ b/tests/ImageSharp.Tests/Quantization/WuQuantizerTests.cs @@ -20,7 +20,7 @@ namespace SixLabors.ImageSharp.Tests.Quantization using var image = new Image(config, 1, 1, Color.Black); ImageFrame frame = image.Frames.RootFrame; - using IFrameQuantizer frameQuantizer = quantizer.CreateFrameQuantizer(config); + using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config); using IndexedImageFrame 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(config, 1, 1, default(Rgba32)); ImageFrame frame = image.Frames.RootFrame; - using IFrameQuantizer frameQuantizer = quantizer.CreateFrameQuantizer(config); + using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config); using IndexedImageFrame result = frameQuantizer.QuantizeFrame(frame, frame.Bounds()); Assert.Equal(1, result.Palette.Length); @@ -86,7 +86,7 @@ namespace SixLabors.ImageSharp.Tests.Quantization ImageFrame frame = image.Frames.RootFrame; - using IFrameQuantizer frameQuantizer = quantizer.CreateFrameQuantizer(config); + using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config); using IndexedImageFrame 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 frame = image.Frames.RootFrame; - using IFrameQuantizer frameQuantizer = quantizer.CreateFrameQuantizer(config); + using IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config); using IndexedImageFrame 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 frame = image.Frames.RootFrame; - using (IFrameQuantizer frameQuantizer = quantizer.CreateFrameQuantizer(config)) + using (IQuantizer frameQuantizer = quantizer.CreatePixelSpecificQuantizer(config)) using (IndexedImageFrame result = frameQuantizer.QuantizeFrame(frame, frame.Bounds())) { Assert.Equal(4 * 8, result.Palette.Length);