From 9e6a03459aac1c5f5186ae8243175b91b4a02d8c Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 28 Jan 2020 17:44:23 +1100 Subject: [PATCH] Normalize MemoryAllocator references. --- src/ImageSharp/Advanced/AotCompilerTools.cs | 4 +- src/ImageSharp/Configuration.cs | 2 +- src/ImageSharp/Formats/Gif/GifEncoder.cs | 2 +- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 23 +++++------ src/ImageSharp/ImageFrame.cs | 6 --- src/ImageSharp/ImageFrame{TPixel}.cs | 6 +-- .../BackgroundColorProcessor{TPixel}.cs | 5 ++- .../Overlays/GlowProcessor{TPixel}.cs | 3 +- .../Overlays/VignetteProcessor{TPixel}.cs | 3 +- .../Quantization/FrameQuantizer{TPixel}.cs | 24 +++++++---- .../OctreeFrameQuantizer{TPixel}.cs | 10 +++-- .../Quantization/OctreeQuantizer.cs | 8 ++-- .../PaletteFrameQuantizer{TPixel}.cs | 5 ++- .../Quantization/PaletteQuantizer.cs | 4 +- .../Quantization/WuFrameQuantizer{TPixel}.cs | 40 ++++++++++--------- .../Processors/Quantization/WuQuantizer.cs | 9 ++--- .../ImageComparison/ExactImageComparer.cs | 2 +- .../ImageComparison/TolerantImageComparer.cs | 2 +- 18 files changed, 83 insertions(+), 75 deletions(-) diff --git a/src/ImageSharp/Advanced/AotCompilerTools.cs b/src/ImageSharp/Advanced/AotCompilerTools.cs index bb4ddb7d0c..142ea3f3e7 100644 --- a/src/ImageSharp/Advanced/AotCompilerTools.cs +++ b/src/ImageSharp/Advanced/AotCompilerTools.cs @@ -109,7 +109,7 @@ namespace SixLabors.ImageSharp.Advanced private static void AotCompileOctreeQuantizer() where TPixel : struct, IPixel { - using (var test = new OctreeFrameQuantizer(new OctreeQuantizer(false))) + using (var test = new OctreeFrameQuantizer(Configuration.Default, new OctreeQuantizer(false))) { test.AotGetPalette(); } @@ -122,7 +122,7 @@ namespace SixLabors.ImageSharp.Advanced private static void AotCompileWuQuantizer() where TPixel : struct, IPixel { - using (var test = new WuFrameQuantizer(Configuration.Default.MemoryAllocator, new WuQuantizer(false))) + using (var test = new WuFrameQuantizer(Configuration.Default, new WuQuantizer(false))) { test.QuantizeFrame(new ImageFrame(Configuration.Default, 1, 1)); test.AotGetPalette(); diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs index 9f26df300b..619be880a0 100644 --- a/src/ImageSharp/Configuration.cs +++ b/src/ImageSharp/Configuration.cs @@ -16,7 +16,7 @@ using SixLabors.ImageSharp.Processing; namespace SixLabors.ImageSharp { /// - /// Provides configuration code which allows altering default behaviour or extending the library. + /// Provides configuration which allows altering default behaviour or extending the library. /// public sealed class Configuration { diff --git a/src/ImageSharp/Formats/Gif/GifEncoder.cs b/src/ImageSharp/Formats/Gif/GifEncoder.cs index fef311596e..248915cb70 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoder.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoder.cs @@ -28,7 +28,7 @@ namespace SixLabors.ImageSharp.Formats.Gif public void Encode(Image image, Stream stream) where TPixel : struct, IPixel { - var encoder = new GifEncoderCore(image.GetConfiguration().MemoryAllocator, this); + var encoder = new GifEncoderCore(image.GetConfiguration(), this); encoder.Encode(image, stream); } } diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index b4aae0744b..a691e527ee 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -53,11 +53,12 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Initializes a new instance of the class. /// - /// The to use for buffer allocations. + /// The configuration which allows altering default behaviour or extending the library. /// The options for the encoder. - public GifEncoderCore(MemoryAllocator memoryAllocator, IGifEncoderOptions options) + public GifEncoderCore(Configuration configuration, IGifEncoderOptions options) { - this.memoryAllocator = memoryAllocator; + this.configuration = configuration; + this.memoryAllocator = configuration.MemoryAllocator; this.quantizer = options.Quantizer; this.colorTableMode = options.ColorTableMode; } @@ -74,16 +75,14 @@ namespace SixLabors.ImageSharp.Formats.Gif Guard.NotNull(image, nameof(image)); Guard.NotNull(stream, nameof(stream)); - this.configuration = image.GetConfiguration(); - ImageMetadata metadata = image.Metadata; GifMetadata gifMetadata = metadata.GetGifMetadata(); - this.colorTableMode = this.colorTableMode ?? gifMetadata.ColorTableMode; + this.colorTableMode ??= gifMetadata.ColorTableMode; bool useGlobalTable = this.colorTableMode == GifColorTableMode.Global; // Quantize the image returning a palette. IQuantizedFrame quantized; - using (IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(image.GetConfiguration())) + using (IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(this.configuration)) { quantized = frameQuantizer.QuantizeFrame(image.Frames.RootFrame); } @@ -146,7 +145,7 @@ namespace SixLabors.ImageSharp.Formats.Gif else { using (IFrameQuantizer paletteFrameQuantizer = - new PaletteFrameQuantizer(this.quantizer.Diffuser, quantized.Palette)) + new PaletteFrameQuantizer(this.configuration, this.quantizer.Diffuser, quantized.Palette)) { using (IQuantizedFrame paletteQuantized = paletteFrameQuantizer.QuantizeFrame(frame)) { @@ -172,14 +171,14 @@ namespace SixLabors.ImageSharp.Formats.Gif if (previousFrame != null && previousMeta.ColorTableLength != frameMetadata.ColorTableLength && frameMetadata.ColorTableLength > 0) { - using (IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(image.GetConfiguration(), frameMetadata.ColorTableLength)) + using (IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(this.configuration, frameMetadata.ColorTableLength)) { quantized = frameQuantizer.QuantizeFrame(frame); } } else { - using (IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(image.GetConfiguration())) + using (IFrameQuantizer frameQuantizer = this.quantizer.CreateFrameQuantizer(this.configuration)) { quantized = frameQuantizer.QuantizeFrame(frame); } @@ -202,9 +201,7 @@ namespace SixLabors.ImageSharp.Formats.Gif /// /// Returns the index of the most transparent color in the palette. /// - /// - /// The quantized. - /// + /// The quantized frame. /// The pixel format. /// /// The . diff --git a/src/ImageSharp/ImageFrame.cs b/src/ImageSharp/ImageFrame.cs index af7405c759..0d7859615b 100644 --- a/src/ImageSharp/ImageFrame.cs +++ b/src/ImageSharp/ImageFrame.cs @@ -31,17 +31,11 @@ namespace SixLabors.ImageSharp Guard.NotNull(metadata, nameof(metadata)); this.configuration = configuration ?? Configuration.Default; - this.MemoryAllocator = configuration.MemoryAllocator; this.Width = width; this.Height = height; this.Metadata = metadata; } - /// - /// Gets the to use for buffer allocations. - /// - public MemoryAllocator MemoryAllocator { get; } - /// /// Gets the width. /// diff --git a/src/ImageSharp/ImageFrame{TPixel}.cs b/src/ImageSharp/ImageFrame{TPixel}.cs index fef1730b93..e1112c0170 100644 --- a/src/ImageSharp/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/ImageFrame{TPixel}.cs @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp Guard.MustBeGreaterThan(width, 0, nameof(width)); Guard.MustBeGreaterThan(height, 0, nameof(height)); - this.PixelBuffer = this.MemoryAllocator.Allocate2D(width, height, AllocationOptions.Clean); + this.PixelBuffer = this.GetConfiguration().MemoryAllocator.Allocate2D(width, height, AllocationOptions.Clean); } /// @@ -88,7 +88,7 @@ namespace SixLabors.ImageSharp Guard.MustBeGreaterThan(width, 0, nameof(width)); Guard.MustBeGreaterThan(height, 0, nameof(height)); - this.PixelBuffer = this.MemoryAllocator.Allocate2D(width, height); + this.PixelBuffer = this.GetConfiguration().MemoryAllocator.Allocate2D(width, height); this.Clear(backgroundColor); } @@ -132,7 +132,7 @@ namespace SixLabors.ImageSharp Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(source, nameof(source)); - this.PixelBuffer = this.MemoryAllocator.Allocate2D(source.PixelBuffer.Width, source.PixelBuffer.Height); + this.PixelBuffer = this.GetConfiguration().MemoryAllocator.Allocate2D(source.PixelBuffer.Width, source.PixelBuffer.Height); source.PixelBuffer.GetSpan().CopyTo(this.PixelBuffer.GetSpan()); } diff --git a/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs index 53bd0f2311..c4fabead2b 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor{TPixel}.cs @@ -65,9 +65,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); Configuration configuration = this.Configuration; + MemoryAllocator memoryAllocator = configuration.MemoryAllocator; - using (IMemoryOwner colors = source.MemoryAllocator.Allocate(width)) - using (IMemoryOwner amount = source.MemoryAllocator.Allocate(width)) + using (IMemoryOwner colors = memoryAllocator.Allocate(width)) + using (IMemoryOwner amount = memoryAllocator.Allocate(width)) { // Be careful! Do not capture colorSpan & amountSpan in the lambda below! Span colorSpan = colors.GetSpan(); diff --git a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs index 86071cdfdc..363e670d02 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor{TPixel}.cs @@ -77,8 +77,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays float blendPercentage = this.definition.GraphicsOptions.BlendPercentage; Configuration configuration = this.Configuration; + MemoryAllocator memoryAllocator = configuration.MemoryAllocator; - using (IMemoryOwner rowColors = source.MemoryAllocator.Allocate(width)) + using (IMemoryOwner rowColors = memoryAllocator.Allocate(width)) { rowColors.GetSpan().Fill(glowColor); diff --git a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs index 402fb17764..3e037189da 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor{TPixel}.cs @@ -81,8 +81,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays var workingRect = Rectangle.FromLTRB(minX, minY, maxX, maxY); float blendPercentage = this.definition.GraphicsOptions.BlendPercentage; Configuration configuration = this.Configuration; + MemoryAllocator memoryAllocator = configuration.MemoryAllocator; - using (IMemoryOwner rowColors = source.MemoryAllocator.Allocate(width)) + using (IMemoryOwner rowColors = memoryAllocator.Allocate(width)) { rowColors.GetSpan().Fill(vignetteColor); diff --git a/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs index 84e7832404..eb3838d21e 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/FrameQuantizer{TPixel}.cs @@ -7,7 +7,7 @@ using System.Collections.Generic; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing.Processors.Dithering; @@ -40,6 +40,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// Initializes a new instance of the class. /// + /// The configuration which allows altering default behaviour or extending the library. /// The quantizer /// /// If true, the quantization process only needs to loop through the source pixels once @@ -49,10 +50,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// only call the method. /// If two passes are required, the code will also call . /// - protected FrameQuantizer(IQuantizer quantizer, bool singlePass) + protected FrameQuantizer(Configuration configuration, IQuantizer quantizer, bool singlePass) { Guard.NotNull(quantizer, nameof(quantizer)); + this.Configuration = configuration; this.Diffuser = quantizer.Diffuser; this.Dither = this.Diffuser != null; this.singlePass = singlePass; @@ -61,6 +63,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// Initializes a new instance of the class. /// + /// The configuration which allows altering default behaviour or extending the library. /// The diffuser /// /// If true, the quantization process only needs to loop through the source pixels once @@ -70,8 +73,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// only call the method. /// If two passes are required, the code will also call . /// - protected FrameQuantizer(IErrorDiffuser diffuser, bool singlePass) + protected FrameQuantizer(Configuration configuration, IErrorDiffuser diffuser, bool singlePass) { + this.Configuration = configuration; this.Diffuser = diffuser; this.Dither = this.Diffuser != null; this.singlePass = singlePass; @@ -83,6 +87,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// public bool Dither { get; } + /// + /// Gets the configuration which allows altering default behaviour or extending the library. + /// + protected Configuration Configuration { get; } + /// public void Dispose() { @@ -109,15 +118,16 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization // Collect the palette. Required before the second pass runs. ReadOnlyMemory palette = this.GetPalette(); - Configuration configuration = image.GetConfiguration(); - this.paletteVector = configuration.MemoryAllocator.Allocate(palette.Length); + MemoryAllocator memoryAllocator = this.Configuration.MemoryAllocator; + + this.paletteVector = memoryAllocator.Allocate(palette.Length); PixelOperations.Instance.ToVector4( - configuration, + this.Configuration, palette.Span, this.paletteVector.Memory.Span, PixelConversionModifiers.Scale); - var quantizedFrame = new QuantizedFrame(image.MemoryAllocator, width, height, palette); + var quantizedFrame = new QuantizedFrame(memoryAllocator, width, height, palette); Span pixelSpan = quantizedFrame.GetWritablePixelSpan(); if (this.Dither) diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs index 393cb5f602..4b94c14bea 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeFrameQuantizer{TPixel}.cs @@ -37,27 +37,29 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// Initializes a new instance of the class. /// + /// The configuration which allows altering default behaviour or extending the library. /// The octree quantizer /// /// The Octree quantizer is a two pass algorithm. The initial pass sets up the Octree, /// the second pass quantizes a color based on the nodes in the tree /// - public OctreeFrameQuantizer(OctreeQuantizer quantizer) - : this(quantizer, quantizer.MaxColors) + public OctreeFrameQuantizer(Configuration configuration, OctreeQuantizer quantizer) + : this(configuration, quantizer, quantizer.MaxColors) { } /// /// Initializes a new instance of the class. /// + /// The configuration which allows altering default behaviour or extending the library. /// The octree quantizer. /// The maximum number of colors to hold in the color palette. /// /// The Octree quantizer is a two pass algorithm. The initial pass sets up the Octree, /// the second pass quantizes a color based on the nodes in the tree /// - public OctreeFrameQuantizer(OctreeQuantizer quantizer, int maxColors) - : base(quantizer, false) + public OctreeFrameQuantizer(Configuration configuration, OctreeQuantizer quantizer, int maxColors) + : base(configuration, quantizer, false) { this.colors = maxColors; this.octree = new Octree(ImageMaths.GetBitsNeededForColorDepth(this.colors).Clamp(1, 8)); diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs index f5fa8c95d9..aaf2c42cb4 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using SixLabors.ImageSharp.PixelFormats; @@ -83,16 +83,16 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// public IFrameQuantizer CreateFrameQuantizer(Configuration configuration) where TPixel : struct, IPixel - => new OctreeFrameQuantizer(this); + => new OctreeFrameQuantizer(configuration, this); /// public IFrameQuantizer CreateFrameQuantizer(Configuration configuration, int maxColors) where TPixel : struct, IPixel { maxColors = maxColors.Clamp(QuantizerConstants.MinColors, QuantizerConstants.MaxColors); - return new OctreeFrameQuantizer(this, maxColors); + return new OctreeFrameQuantizer(configuration, this, maxColors); } private static IErrorDiffuser GetDiffuser(bool dither) => dither ? KnownDiffusers.FloydSteinberg : null; } -} \ No newline at end of file +} diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteFrameQuantizer{TPixel}.cs index f774f80be2..825eb6bee4 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteFrameQuantizer{TPixel}.cs @@ -26,10 +26,11 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// Initializes a new instance of the class. /// + /// The configuration which allows altering default behaviour or extending the library. /// The palette quantizer. /// An array of all colors in the palette. - public PaletteFrameQuantizer(IErrorDiffuser diffuser, ReadOnlyMemory colors) - : base(diffuser, true) => this.palette = colors; + public PaletteFrameQuantizer(Configuration configuration, IErrorDiffuser diffuser, ReadOnlyMemory colors) + : base(configuration, diffuser, true) => this.palette = colors; /// protected override void SecondPass( diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs index 17734bcdc0..a493e6f88b 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer.cs @@ -61,7 +61,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization { var palette = new TPixel[this.Palette.Length]; Color.ToPixel(configuration, this.Palette.Span, palette.AsSpan()); - return new PaletteFrameQuantizer(this.Diffuser, palette); + return new PaletteFrameQuantizer(configuration, this.Diffuser, palette); } /// @@ -73,7 +73,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization var palette = new TPixel[max]; Color.ToPixel(configuration, this.Palette.Span.Slice(0, max), palette.AsSpan()); - return new PaletteFrameQuantizer(this.Diffuser, palette); + return new PaletteFrameQuantizer(configuration, this.Diffuser, palette); } private static IErrorDiffuser GetDiffuser(bool dither) => dither ? KnownDiffusers.FloydSteinberg : null; diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs index e695dbd179..2de02ebb3a 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs @@ -37,6 +37,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization internal sealed class WuFrameQuantizer : FrameQuantizer where TPixel : struct, IPixel { + private readonly MemoryAllocator memoryAllocator; + // The following two variables determine the amount of bits to preserve when calculating the histogram. // Reducing the value of these numbers the granularity of the color maps produced, making it much faster // and using much less memory but potentially less accurate. Current results are very good though! @@ -121,39 +123,39 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// /// Initializes a new instance of the class. /// - /// The . - /// The wu quantizer + /// The configuration which allows altering default behaviour or extending the library. + /// The Wu quantizer /// /// The Wu quantizer is a two pass algorithm. The initial pass sets up the 3-D color histogram, /// the second pass quantizes a color based on the position in the histogram. /// - public WuFrameQuantizer(MemoryAllocator memoryAllocator, WuQuantizer quantizer) - : this(memoryAllocator, quantizer, quantizer.MaxColors) + public WuFrameQuantizer(Configuration configuration, WuQuantizer quantizer) + : this(configuration, quantizer, quantizer.MaxColors) { } /// /// Initializes a new instance of the class. /// - /// The . - /// The wu quantizer. + /// The configuration which allows altering default behaviour or extending the library. + /// The Wu quantizer. /// The maximum number of colors to hold in the color palette. /// /// The Wu quantizer is a two pass algorithm. The initial pass sets up the 3-D color histogram, /// the second pass quantizes a color based on the position in the histogram. /// - public WuFrameQuantizer(MemoryAllocator memoryAllocator, WuQuantizer quantizer, int maxColors) - : base(quantizer, false) + public WuFrameQuantizer(Configuration configuration, WuQuantizer quantizer, int maxColors) + : base(configuration, quantizer, false) { - Guard.NotNull(memoryAllocator, nameof(memoryAllocator)); + this.memoryAllocator = this.Configuration.MemoryAllocator; - this.vwt = memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); - this.vmr = memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); - this.vmg = memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); - this.vmb = memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); - this.vma = memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); - this.m2 = memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); - this.tag = memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); + this.vwt = this.memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); + this.vmr = this.memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); + this.vmg = this.memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); + this.vmb = this.memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); + this.vma = this.memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); + this.m2 = this.memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); + this.tag = this.memoryAllocator.Allocate(TableLength, AllocationOptions.Clean); this.colors = maxColors; } @@ -229,7 +231,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization protected override void FirstPass(ImageFrame source, int width, int height) { this.Build3DHistogram(source, width, height); - this.Get3DMoments(source.MemoryAllocator); + this.Get3DMoments(this.memoryAllocator); this.BuildCube(); } @@ -465,7 +467,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization // Build up the 3-D color histogram // Loop through each row - using (IMemoryOwner rgbaBuffer = source.MemoryAllocator.Allocate(source.Width)) + using (IMemoryOwner rgbaBuffer = this.memoryAllocator.Allocate(source.Width)) { for (int y = 0; y < height; y++) { @@ -840,7 +842,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization private void BuildCube() { this.colorCube = new Box[this.colors]; - var vv = new double[this.colors]; + Span vv = stackalloc double[this.colors]; ref Box cube = ref this.colorCube[0]; cube.RMin = cube.GMin = cube.BMin = cube.AMin = 0; diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs index b80cedeb39..3f2deaec06 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors and contributors. +// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using SixLabors.ImageSharp.PixelFormats; @@ -68,13 +68,12 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization /// public int MaxColors { get; } - /// /// public IFrameQuantizer CreateFrameQuantizer(Configuration configuration) where TPixel : struct, IPixel { Guard.NotNull(configuration, nameof(configuration)); - return new WuFrameQuantizer(configuration.MemoryAllocator, this); + return new WuFrameQuantizer(configuration, this); } /// @@ -83,9 +82,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization { Guard.NotNull(configuration, nameof(configuration)); maxColors = maxColors.Clamp(QuantizerConstants.MinColors, QuantizerConstants.MaxColors); - return new WuFrameQuantizer(configuration.MemoryAllocator, this, maxColors); + return new WuFrameQuantizer(configuration, this, maxColors); } private static IErrorDiffuser GetDiffuser(bool dither) => dither ? KnownDiffusers.FloydSteinberg : null; } -} \ No newline at end of file +} diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ExactImageComparer.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ExactImageComparer.cs index f5f709ce8d..b8b7ca025a 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ExactImageComparer.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/ExactImageComparer.cs @@ -26,7 +26,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison var bBuffer = new Rgba64[width]; var differences = new List(); - Configuration configuration = expected.Configuration; + Configuration configuration = expected.GetConfiguration(); for (int y = 0; y < actual.Height; y++) { diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs index cbcc6b8450..751c8d46b9 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageComparison/TolerantImageComparer.cs @@ -72,7 +72,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison float totalDifference = 0F; var differences = new List(); - Configuration configuration = expected.Configuration; + Configuration configuration = expected.GetConfiguration(); for (int y = 0; y < actual.Height; y++) {