Browse Source

- Allocate Buffer2Ds from memory manager

pull/431/head
Lauri Kotilainen 8 years ago
parent
commit
c5eb2cfb71
  1. 4
      src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegComponentPostProcessor.cs
  2. 2
      src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsQuantizationTables.cs
  3. 2
      src/ImageSharp/Image/ImageFrame{TPixel}.cs
  4. 30
      src/ImageSharp/Memory/Buffer2D{T}.cs
  5. 8
      src/ImageSharp/Memory/MemoryManager.cs
  6. 2
      src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs
  7. 2
      tests/ImageSharp.Benchmarks/General/PixelIndexing.cs
  8. 2
      tests/ImageSharp.Benchmarks/Image/Jpeg/YCbCrColorConversion.cs
  9. 4
      tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs
  10. 2
      tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs
  11. 2
      tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs
  12. 10
      tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
  13. 4
      tests/ImageSharp.Tests/Memory/BufferAreaTests.cs

4
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<float>(imagePostProcessor.PostProcessorBufferSize);
this.ColorBuffer = MemoryManager.Current.Allocate2D<float>(
imagePostProcessor.PostProcessorBufferSize.Width,
imagePostProcessor.PostProcessorBufferSize.Height);
this.BlockRowsPerStep = JpegImagePostProcessor.BlockRowsPerStep / this.Component.SubSamplingDivisors.Height;
this.blockAreaSize = this.Component.SubSamplingDivisors * 8;

2
src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsQuantizationTables.cs

@ -49,7 +49,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components
get; set;
}
= new Buffer2D<short>(64, 4);
= MemoryManager.Current.Allocate2D<short>(64, 4);
/// <inheritdoc/>
public void Dispose()

2
src/ImageSharp/Image/ImageFrame{TPixel}.cs

@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp
/// <param name="source">The source.</param>
internal ImageFrame(ImageFrame<TPixel> source)
{
this.pixelBuffer = new Buffer2D<TPixel>(source.pixelBuffer.Width, source.pixelBuffer.Height);
this.pixelBuffer = MemoryManager.Current.Allocate2D<TPixel>(source.pixelBuffer.Width, source.pixelBuffer.Height);
source.pixelBuffer.Span.CopyTo(this.pixelBuffer.Span);
this.MetaData = source.MetaData.Clone();
}

30
src/ImageSharp/Memory/Buffer2D{T}.cs

@ -15,36 +15,6 @@ namespace SixLabors.ImageSharp.Memory
internal class Buffer2D<T> : IBuffer2D<T>, IDisposable
where T : struct
{
public Buffer2D(Size size)
: this(size.Width, size.Height)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Buffer2D{T}"/> class.
/// </summary>
/// <param name="width">The number of elements in a row</param>
/// <param name="height">The number of rows</param>
public Buffer2D(int width, int height)
: this(MemoryManager.Current.Allocate<T>(width * height), width, height)
{
this.Width = width;
this.Height = height;
}
/// <summary>
/// Initializes a new instance of the <see cref="Buffer2D{T}"/> class.
/// </summary>
/// <param name="array">The array to pin</param>
/// <param name="width">The number of elements in a row</param>
/// <param name="height">The number of rows</param>
public Buffer2D(T[] array, int width, int height)
{
this.Buffer = new Buffer<T>(array, width * height);
this.Width = width;
this.Height = height;
}
/// <summary>
/// Initializes a new instance of the <see cref="Buffer2D{T}"/> class.
/// </summary>

8
src/ImageSharp/Memory/MemoryManager.cs

@ -35,5 +35,13 @@ namespace SixLabors.ImageSharp.Memory
/// <param name="buffer">The buffer to release</param>
internal abstract void Release<T>(Buffer<T> buffer)
where T : struct;
internal Buffer2D<T> Allocate2D<T>(int width, int height, bool clear = false)
where T : struct
{
var buffer = this.Allocate<T>(width * height, clear);
return new Buffer2D<T>(buffer, width, height);
}
}
}

2
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<Vector4>(width, source.Height))
using (var firstPassPixels = MemoryManager.Current.Allocate2D<Vector4>(width, source.Height))
{
firstPassPixels.Buffer.Clear();

2
tests/ImageSharp.Benchmarks/General/PixelIndexing.cs

@ -149,7 +149,7 @@
public void Setup()
{
this.width = 2048;
this.buffer = new Buffer2D<Vector4>(2048, 2048);
this.buffer = MemoryManager.Current.Allocate2D<Vector4>(2048, 2048);
this.pointer = (Vector4*)this.buffer.Buffer.Pin();
this.array = this.buffer.Buffer.Array;
this.pinnable = Unsafe.As<Pinnable<Vector4>>(this.array);

2
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<float>(values, values.Length, 1);
buffers[i] = MemoryManager.Current.Allocate2D<float>(values.Length, 1);
}
return buffers;

4
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<float>(20, 20))
using (var buffer = MemoryManager.Current.Allocate2D<float>(20, 20))
{
BufferArea<float> 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<float>(100, 100))
using (var buffer = MemoryManager.Current.Allocate2D<float>(100, 100))
{
BufferArea<float> area = buffer.GetArea(start.X, start.Y, 8 * horizontalFactor, 8 * verticalFactor);
block.CopyTo(area, horizontalFactor, verticalFactor);

2
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<float>(values, values.Length, 1);
buffers[i] = new Buffer2D<float>(new Buffer<float>(values, values.Length), values.Length, 1);
}
return new JpegColorConverter.ComponentValues(buffers, 0);
}

2
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<Block8x8>(this.WidthInBlocks, this.HeightInBlocks);
this.SpectralBlocks = MemoryManager.Current.Allocate2D<Block8x8>(this.WidthInBlocks, this.HeightInBlocks);
}
public Size Size => new Size(this.WidthInBlocks, this.HeightInBlocks);

10
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<TestStructs.Foo> buffer = new Buffer2D<TestStructs.Foo>(width, height))
using (Buffer2D<TestStructs.Foo> buffer = MemoryManager.Current.Allocate2D<TestStructs.Foo>(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<TestStructs.Foo> buffer = new Buffer2D<TestStructs.Foo>(array, width, height))
using (Buffer2D<TestStructs.Foo> buffer = new Buffer2D<TestStructs.Foo>(new Buffer<TestStructs.Foo>(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<TestStructs.Foo> buffer = new Buffer2D<TestStructs.Foo>(width, height))
using (Buffer2D<TestStructs.Foo> buffer = MemoryManager.Current.Allocate2D<TestStructs.Foo>(width, height))
{
Span<TestStructs.Foo> 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<TestStructs.Foo> buffer = new Buffer2D<TestStructs.Foo>(width, height))
using (Buffer2D<TestStructs.Foo> buffer = MemoryManager.Current.Allocate2D<TestStructs.Foo>(width, height))
{
Span<TestStructs.Foo> 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<TestStructs.Foo> buffer = new Buffer2D<TestStructs.Foo>(width, height))
using (Buffer2D<TestStructs.Foo> buffer = MemoryManager.Current.Allocate2D<TestStructs.Foo>(width, height))
{
TestStructs.Foo[] array = buffer.Buffer.Array;

4
tests/ImageSharp.Tests/Memory/BufferAreaTests.cs

@ -13,7 +13,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
[Fact]
public void Construct()
{
using (var buffer = new Buffer2D<int>(10, 20))
using (var buffer = MemoryManager.Current.Allocate2D<int>(10, 20))
{
var rectangle = new Rectangle(3,2, 5, 6);
var area = new BufferArea<int>(buffer, rectangle);
@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
private static Buffer2D<int> CreateTestBuffer(int w, int h)
{
var buffer = new Buffer2D<int>(w, h);
var buffer = MemoryManager.Current.Allocate2D<int>(w, h);
for (int y = 0; y < h; y++)
{
for (int x = 0; x < w; x++)

Loading…
Cancel
Save