diff --git a/src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegComponentPostProcessor.cs index 87c1431e02..7c91e85e74 100644 --- a/src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegComponentPostProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegComponentPostProcessor.cs @@ -26,7 +26,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Common.Decoder { this.Component = component; this.ImagePostProcessor = imagePostProcessor; - this.ColorBuffer = new Buffer2D(imagePostProcessor.PostProcessorBufferSize); + this.ColorBuffer = MemoryManager.Current.Allocate2D( + imagePostProcessor.PostProcessorBufferSize.Width, + imagePostProcessor.PostProcessorBufferSize.Height); this.BlockRowsPerStep = JpegImagePostProcessor.BlockRowsPerStep / this.Component.SubSamplingDivisors.Height; this.blockAreaSize = this.Component.SubSamplingDivisors * 8; diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsQuantizationTables.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsQuantizationTables.cs index 1000ce82c5..a585c5080f 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsQuantizationTables.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsQuantizationTables.cs @@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components get; set; } - = new Buffer2D(64, 4); + = MemoryManager.Current.Allocate2D(64, 4); /// public void Dispose() diff --git a/src/ImageSharp/Image/ImageFrame{TPixel}.cs b/src/ImageSharp/Image/ImageFrame{TPixel}.cs index 45ed5f053a..a0e4976cb6 100644 --- a/src/ImageSharp/Image/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/Image/ImageFrame{TPixel}.cs @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp /// The source. internal ImageFrame(ImageFrame source) { - this.pixelBuffer = new Buffer2D(source.pixelBuffer.Width, source.pixelBuffer.Height); + this.pixelBuffer = MemoryManager.Current.Allocate2D(source.pixelBuffer.Width, source.pixelBuffer.Height); source.pixelBuffer.Span.CopyTo(this.pixelBuffer.Span); this.MetaData = source.MetaData.Clone(); } diff --git a/src/ImageSharp/Memory/Buffer2D{T}.cs b/src/ImageSharp/Memory/Buffer2D{T}.cs index a7de343f9c..73444d4978 100644 --- a/src/ImageSharp/Memory/Buffer2D{T}.cs +++ b/src/ImageSharp/Memory/Buffer2D{T}.cs @@ -15,36 +15,6 @@ namespace SixLabors.ImageSharp.Memory internal class Buffer2D : IBuffer2D, IDisposable where T : struct { - public Buffer2D(Size size) - : this(size.Width, size.Height) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The number of elements in a row - /// The number of rows - public Buffer2D(int width, int height) - : this(MemoryManager.Current.Allocate(width * height), width, height) - { - this.Width = width; - this.Height = height; - } - - /// - /// Initializes a new instance of the class. - /// - /// The array to pin - /// The number of elements in a row - /// The number of rows - public Buffer2D(T[] array, int width, int height) - { - this.Buffer = new Buffer(array, width * height); - this.Width = width; - this.Height = height; - } - /// /// Initializes a new instance of the class. /// diff --git a/src/ImageSharp/Memory/MemoryManager.cs b/src/ImageSharp/Memory/MemoryManager.cs index 54b727b607..38e68e22c0 100644 --- a/src/ImageSharp/Memory/MemoryManager.cs +++ b/src/ImageSharp/Memory/MemoryManager.cs @@ -35,5 +35,13 @@ namespace SixLabors.ImageSharp.Memory /// The buffer to release internal abstract void Release(Buffer buffer) where T : struct; + + internal Buffer2D Allocate2D(int width, int height, bool clear = false) + where T : struct + { + var buffer = this.Allocate(width * height, clear); + + return new Buffer2D(buffer, width, height); + } } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index 6dcffbbc04..dbc05876dd 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -121,7 +121,7 @@ namespace SixLabors.ImageSharp.Processing.Processors // First process the columns. Since we are not using multiple threads startY and endY // are the upper and lower bounds of the source rectangle. // TODO: Using a transposed variant of 'firstPassPixels' could eliminate the need for the WeightsWindow.ComputeWeightedColumnSum() method, and improve speed! - using (var firstPassPixels = new Buffer2D(width, source.Height)) + using (var firstPassPixels = MemoryManager.Current.Allocate2D(width, source.Height)) { firstPassPixels.Buffer.Clear(); diff --git a/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs b/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs index b0560b1631..58f835a076 100644 --- a/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs +++ b/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs @@ -149,7 +149,7 @@ public void Setup() { this.width = 2048; - this.buffer = new Buffer2D(2048, 2048); + this.buffer = MemoryManager.Current.Allocate2D(2048, 2048); this.pointer = (Vector4*)this.buffer.Buffer.Pin(); this.array = this.buffer.Buffer.Array; this.pinnable = Unsafe.As>(this.array); diff --git a/tests/ImageSharp.Benchmarks/Image/Jpeg/YCbCrColorConversion.cs b/tests/ImageSharp.Benchmarks/Image/Jpeg/YCbCrColorConversion.cs index 93420aacf8..f513e0d38b 100644 --- a/tests/ImageSharp.Benchmarks/Image/Jpeg/YCbCrColorConversion.cs +++ b/tests/ImageSharp.Benchmarks/Image/Jpeg/YCbCrColorConversion.cs @@ -76,7 +76,7 @@ } // no need to dispose when buffer is not array owner - buffers[i] = new Buffer2D(values, values.Length, 1); + buffers[i] = MemoryManager.Current.Allocate2D(values.Length, 1); } return buffers; diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs index 191cfec731..1dc4896eeb 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs @@ -48,7 +48,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg { Block8x8F block = CreateRandomFloatBlock(0, 100); - using (var buffer = new Buffer2D(20, 20)) + using (var buffer = MemoryManager.Current.Allocate2D(20, 20)) { BufferArea area = buffer.GetArea(5, 10, 8, 8); block.CopyTo(area); @@ -78,7 +78,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg var start = new Point(50, 50); - using (var buffer = new Buffer2D(100, 100)) + using (var buffer = MemoryManager.Current.Allocate2D(100, 100)) { BufferArea area = buffer.GetArea(start.X, start.Y, 8 * horizontalFactor, 8 * verticalFactor); block.CopyTo(area, horizontalFactor, verticalFactor); diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs index 7e0dc915ce..95a62f7fe3 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs @@ -273,7 +273,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg } // no need to dispose when buffer is not array owner - buffers[i] = new Buffer2D(values, values.Length, 1); + buffers[i] = new Buffer2D(new Buffer(values, values.Length), values.Length, 1); } return new JpegColorConverter.ComponentValues(buffers, 0); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs index 40b41b9cba..ad9ad81436 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs @@ -23,7 +23,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils this.HeightInBlocks = heightInBlocks; this.WidthInBlocks = widthInBlocks; this.Index = index; - this.SpectralBlocks = new Buffer2D(this.WidthInBlocks, this.HeightInBlocks); + this.SpectralBlocks = MemoryManager.Current.Allocate2D(this.WidthInBlocks, this.HeightInBlocks); } public Size Size => new Size(this.WidthInBlocks, this.HeightInBlocks); diff --git a/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs index 7ab0ed9491..2e99616a89 100644 --- a/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs +++ b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs @@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Tests.Memory [InlineData(1025, 17)] public void Construct(int width, int height) { - using (Buffer2D buffer = new Buffer2D(width, height)) + using (Buffer2D buffer = MemoryManager.Current.Allocate2D(width, height)) { Assert.Equal(width, buffer.Width); Assert.Equal(height, buffer.Height); @@ -45,7 +45,7 @@ namespace SixLabors.ImageSharp.Tests.Memory public void Construct_FromExternalArray(int width, int height) { TestStructs.Foo[] array = new TestStructs.Foo[width * height + 10]; - using (Buffer2D buffer = new Buffer2D(array, width, height)) + using (Buffer2D buffer = new Buffer2D(new Buffer(array, array.Length), width, height)) { Assert.Equal(width, buffer.Width); Assert.Equal(height, buffer.Height); @@ -76,7 +76,7 @@ namespace SixLabors.ImageSharp.Tests.Memory [InlineData(17, 42, 41)] public void GetRowSpanY(int width, int height, int y) { - using (Buffer2D buffer = new Buffer2D(width, height)) + using (Buffer2D buffer = MemoryManager.Current.Allocate2D(width, height)) { Span span = buffer.GetRowSpan(y); @@ -92,7 +92,7 @@ namespace SixLabors.ImageSharp.Tests.Memory [InlineData(17, 42, 0, 41)] public void GetRowSpanXY(int width, int height, int x, int y) { - using (Buffer2D buffer = new Buffer2D(width, height)) + using (Buffer2D buffer = MemoryManager.Current.Allocate2D(width, height)) { Span span = buffer.GetRowSpan(x, y); @@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Tests.Memory [InlineData(99, 88, 98, 87)] public void Indexer(int width, int height, int x, int y) { - using (Buffer2D buffer = new Buffer2D(width, height)) + using (Buffer2D buffer = MemoryManager.Current.Allocate2D(width, height)) { TestStructs.Foo[] array = buffer.Buffer.Array; diff --git a/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs b/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs index 026b694981..74e9098b90 100644 --- a/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs +++ b/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs @@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Tests.Memory [Fact] public void Construct() { - using (var buffer = new Buffer2D(10, 20)) + using (var buffer = MemoryManager.Current.Allocate2D(10, 20)) { var rectangle = new Rectangle(3,2, 5, 6); var area = new BufferArea(buffer, rectangle); @@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.Tests.Memory private static Buffer2D CreateTestBuffer(int w, int h) { - var buffer = new Buffer2D(w, h); + var buffer = MemoryManager.Current.Allocate2D(w, h); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++)