diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs index 785a1f20c8..24d898fee9 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs @@ -12,7 +12,7 @@ namespace ImageSharp.Processing.Processors /// /// Points to a collection of of weights allocated in . /// - protected unsafe struct WeightsWindow + internal unsafe struct WeightsWindow { /// /// The local left index position @@ -22,6 +22,8 @@ namespace ImageSharp.Processing.Processors /// /// The span of weights pointing to . /// + // TODO: In the case of switching to official System.Memory and System.Buffers.Primitives this should be System.Buffers.Buffer (formerly Memory), because Span is stack-only! + // see: https://github.com/dotnet/corefxlab/blob/873d35ebed7264e2f9adb556f3b61bebc12395d6/docs/specs/memory.md public BufferSpan Span; /// @@ -129,7 +131,7 @@ namespace ImageSharp.Processing.Processors /// /// Holds the values in an optimized contigous memory region. /// - protected class WeightsBuffer : IDisposable + internal class WeightsBuffer : IDisposable { private PinnedImageBuffer dataBuffer; @@ -140,8 +142,7 @@ namespace ImageSharp.Processing.Processors /// The size of the destination window public WeightsBuffer(int sourceSize, int destinationSize) { - this.dataBuffer = new PinnedImageBuffer(sourceSize, destinationSize); - this.dataBuffer.Clear(); + this.dataBuffer = PinnedImageBuffer.CreateClean(sourceSize, destinationSize); this.Weights = new WeightsWindow[destinationSize]; } diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs index d1a709384c..255124a754 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.cs @@ -68,38 +68,14 @@ namespace ImageSharp.Processing.Processors /// protected WeightsBuffer VerticalWeights { get; set; } - /// - protected override void BeforeApply(ImageBase source, Rectangle sourceRectangle) - { - if (!(this.Sampler is NearestNeighborResampler)) - { - this.HorizontalWeights = this.PrecomputeWeights( - this.ResizeRectangle.Width, - sourceRectangle.Width); - - this.VerticalWeights = this.PrecomputeWeights( - this.ResizeRectangle.Height, - sourceRectangle.Height); - } - } - - /// - protected override void AfterApply(ImageBase source, Rectangle sourceRectangle) - { - base.AfterApply(source, sourceRectangle); - this.HorizontalWeights?.Dispose(); - this.HorizontalWeights = null; - this.VerticalWeights?.Dispose(); - this.VerticalWeights = null; - } - /// /// Computes the weights to apply at each pixel when resizing. /// /// The destination size /// The source size /// The - protected unsafe WeightsBuffer PrecomputeWeights(int destinationSize, int sourceSize) + // TODO: Made internal to simplify experimenting with weights data. Make it protected again when finished figuring out how to optimize all the stuff! + internal unsafe WeightsBuffer PrecomputeWeights(int destinationSize, int sourceSize) { float ratio = (float)sourceSize / destinationSize; float scale = ratio; @@ -156,5 +132,30 @@ namespace ImageSharp.Processing.Processors return result; } + + /// + protected override void BeforeApply(ImageBase source, Rectangle sourceRectangle) + { + if (!(this.Sampler is NearestNeighborResampler)) + { + this.HorizontalWeights = this.PrecomputeWeights( + this.ResizeRectangle.Width, + sourceRectangle.Width); + + this.VerticalWeights = this.PrecomputeWeights( + this.ResizeRectangle.Height, + sourceRectangle.Height); + } + } + + /// + protected override void AfterApply(ImageBase source, Rectangle sourceRectangle) + { + base.AfterApply(source, sourceRectangle); + this.HorizontalWeights?.Dispose(); + this.HorizontalWeights = null; + this.VerticalWeights?.Dispose(); + this.VerticalWeights = null; + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs index 06cd228a7e..da09aa85e7 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs @@ -1,5 +1,11 @@ namespace ImageSharp.Tests { + using System.IO; + using System.Text; + + using ImageSharp.Processing; + using ImageSharp.Processing.Processors; + using Xunit; using Xunit.Abstractions; @@ -26,5 +32,30 @@ namespace ImageSharp.Tests } }); } + + // [Fact] + public void PrintWeightsData() + { + ResizeProcessor proc = new ResizeProcessor(new BicubicResampler(), 200, 200); + + ResamplingWeightedProcessor.WeightsBuffer weights = proc.PrecomputeWeights(200, 500); + + StringBuilder bld = new StringBuilder(); + + foreach (ResamplingWeightedProcessor.WeightsWindow window in weights.Weights) + { + for (int i = 0; i < window.Length; i++) + { + float value = window.Span[i]; + bld.Append(value); + bld.Append("| "); + } + bld.AppendLine(); + } + + File.WriteAllText("BicubicWeights.MD", bld.ToString()); + + //this.Output.WriteLine(bld.ToString()); + } } } \ No newline at end of file