diff --git a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs index 5060f5f37..1f77b93f7 100644 --- a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs +++ b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs @@ -117,7 +117,7 @@ namespace SixLabors.ImageSharp.Advanced /// The span retuned from Pixel source private static Span GetSpan(IPixelSource source) where TPixel : struct, IPixel - => source.PixelBuffer.Span; + => source.PixelBuffer.GetSpan(); /// /// Gets the span to the backing buffer at the given row. @@ -143,7 +143,7 @@ namespace SixLabors.ImageSharp.Advanced /// private static Span GetSpan(Buffer2D source, int row) where TPixel : struct, IPixel - => source.Span.Slice(row * source.Width, source.Width); + => source.GetSpan().Slice(row * source.Width, source.Width); /// /// Gets the configuration. @@ -161,6 +161,6 @@ namespace SixLabors.ImageSharp.Advanced /// A reference to the element. private static ref TPixel DangerousGetPinnableReferenceToPixelBuffer(IPixelSource source) where TPixel : struct, IPixel - => ref MemoryMarshal.GetReference(source.PixelBuffer.Span); + => ref MemoryMarshal.GetReference(source.PixelBuffer.GetSpan()); } } diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index a3df87ff4..43be0004e 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -215,7 +215,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp using (Buffer2D buffer = this.memoryManager.AllocateClean2D(width, height)) { - this.UncompressRle8(width, buffer.Span); + this.UncompressRle8(width, buffer.GetSpan()); for (int y = 0; y < height; y++) { diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs index ccbb5c6c0..70ac760e6 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsFrameComponent.cs @@ -136,7 +136,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components public ref Block8x8 GetBlockReference(int column, int row) { int offset = ((this.WidthInBlocks + 1) * row) + column; - return ref Unsafe.Add(ref MemoryMarshal.GetReference(this.SpectralBlocks.Span), offset); + return ref Unsafe.Add(ref MemoryMarshal.GetReference(this.SpectralBlocks.GetSpan()), offset); } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs index 49bc10539..0a780dfe2 100644 --- a/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/PdfJsPort/Components/PdfJsScanDecoder.cs @@ -2,13 +2,14 @@ // Licensed under the Apache License, Version 2.0. using System; + #if DEBUG using System.Diagnostics; #endif using System.Runtime.CompilerServices; using System.Runtime.InteropServices; - using SixLabors.ImageSharp.Formats.Jpeg.Components; +using SixLabors.ImageSharp.Memory; namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components { @@ -166,7 +167,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (componentsLength == 1) { PdfJsFrameComponent component = components[this.compIndex]; - ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.Span)); + ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.GetSpan())); ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; @@ -188,7 +189,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components for (int i = 0; i < componentsLength; i++) { PdfJsFrameComponent component = components[i]; - ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.Span)); + ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.GetSpan())); ref PdfJsHuffmanTable dcHuffmanTable = ref dcHuffmanTables[component.DCHuffmanTableId]; ref PdfJsHuffmanTable acHuffmanTable = ref acHuffmanTables[component.ACHuffmanTableId]; int h = component.HorizontalSamplingFactor; @@ -224,7 +225,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components if (componentsLength == 1) { PdfJsFrameComponent component = components[this.compIndex]; - ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.Span)); + ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.GetSpan())); ref PdfJsHuffmanTable huffmanTable = ref huffmanTables[isAC ? component.ACHuffmanTableId : component.DCHuffmanTableId]; for (int n = 0; n < this.mcuToRead; n++) @@ -267,7 +268,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort.Components for (int i = 0; i < componentsLength; i++) { PdfJsFrameComponent component = components[i]; - ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.Span)); + ref short blockDataRef = ref MemoryMarshal.GetReference(MemoryMarshal.Cast(component.SpectralBlocks.GetSpan())); ref PdfJsHuffmanTable huffmanTable = ref huffmanTables[isAC ? component.ACHuffmanTableId : component.DCHuffmanTableId]; int h = component.HorizontalSamplingFactor; int v = component.VerticalSamplingFactor; diff --git a/src/ImageSharp/ImageFrame{TPixel}.cs b/src/ImageSharp/ImageFrame{TPixel}.cs index 5886ca6fd..328252344 100644 --- a/src/ImageSharp/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/ImageFrame{TPixel}.cs @@ -104,7 +104,7 @@ namespace SixLabors.ImageSharp this.configuration = configuration; this.MemoryManager = configuration.MemoryManager; this.PixelBuffer = this.MemoryManager.Allocate2D(source.PixelBuffer.Width, source.PixelBuffer.Height); - source.PixelBuffer.Span.CopyTo(this.PixelBuffer.Span); + source.PixelBuffer.GetSpan().CopyTo(this.PixelBuffer.GetSpan()); this.MetaData = source.MetaData.Clone(); } @@ -213,7 +213,7 @@ namespace SixLabors.ImageSharp throw new ArgumentException("ImageFrame.CopyTo(): target must be of the same size!", nameof(target)); } - this.GetPixelSpan().CopyTo(target.Span); + this.GetPixelSpan().CopyTo(target.GetSpan()); } /// diff --git a/src/ImageSharp/Memory/Buffer2DExtensions.cs b/src/ImageSharp/Memory/Buffer2DExtensions.cs index ac5ab09db..6c07a20d3 100644 --- a/src/ImageSharp/Memory/Buffer2DExtensions.cs +++ b/src/ImageSharp/Memory/Buffer2DExtensions.cs @@ -12,6 +12,15 @@ namespace SixLabors.ImageSharp.Memory /// internal static class Buffer2DExtensions { + /// + /// Gets a to the backing buffer of . + /// + internal static Span GetSpan(this IBuffer2D buffer) + where T : struct + { + return buffer.Buffer.GetSpan(); + } + /// /// Gets a to the row 'y' beginning from the pixel at 'x'. /// @@ -24,7 +33,7 @@ namespace SixLabors.ImageSharp.Memory public static Span GetRowSpan(this IBuffer2D buffer, int x, int y) where T : struct { - return buffer.Span.Slice((y * buffer.Width) + x, buffer.Width - x); + return buffer.GetSpan().Slice((y * buffer.Width) + x, buffer.Width - x); } /// @@ -38,7 +47,7 @@ namespace SixLabors.ImageSharp.Memory public static Span GetRowSpan(this IBuffer2D buffer, int y) where T : struct { - return buffer.Span.Slice(y * buffer.Width, buffer.Width); + return buffer.GetSpan().Slice(y * buffer.Width, buffer.Width); } /// diff --git a/src/ImageSharp/Memory/Buffer2D{T}.cs b/src/ImageSharp/Memory/Buffer2D{T}.cs index 0c780426d..7689ecdb2 100644 --- a/src/ImageSharp/Memory/Buffer2D{T}.cs +++ b/src/ImageSharp/Memory/Buffer2D{T}.cs @@ -34,11 +34,6 @@ namespace SixLabors.ImageSharp.Memory /// public int Height { get; private set; } - /// - /// Gets the span to the whole area. - /// - public Span Span => this.Buffer.GetSpan(); - /// /// Gets the backing /// diff --git a/src/ImageSharp/Memory/BufferArea{T}.cs b/src/ImageSharp/Memory/BufferArea{T}.cs index 990b494fc..315e57d1b 100644 --- a/src/ImageSharp/Memory/BufferArea{T}.cs +++ b/src/ImageSharp/Memory/BufferArea{T}.cs @@ -71,7 +71,7 @@ namespace SixLabors.ImageSharp.Memory /// The position inside a row /// The row index /// The reference to the value - public ref T this[int x, int y] => ref this.DestinationBuffer.Span[this.GetIndexOf(x, y)]; + public ref T this[int x, int y] => ref this.DestinationBuffer.GetSpan()[this.GetIndexOf(x, y)]; /// /// Gets a reference to the [0,0] element. @@ -79,7 +79,7 @@ namespace SixLabors.ImageSharp.Memory /// The reference to the [0,0] element [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref T GetReferenceToOrigin() => - ref this.DestinationBuffer.Span[(this.Rectangle.Y * this.DestinationBuffer.Width) + this.Rectangle.X]; + ref this.DestinationBuffer.GetSpan()[(this.Rectangle.Y * this.DestinationBuffer.Width) + this.Rectangle.X]; /// /// Gets a span to row 'y' inside this area. @@ -93,7 +93,7 @@ namespace SixLabors.ImageSharp.Memory int xx = this.Rectangle.X; int width = this.Rectangle.Width; - return this.DestinationBuffer.Span.Slice(yy + xx, width); + return this.DestinationBuffer.GetSpan().Slice(yy + xx, width); } /// @@ -147,7 +147,7 @@ namespace SixLabors.ImageSharp.Memory // Optimization for when the size of the area is the same as the buffer size. if (this.IsFullBufferArea) { - this.DestinationBuffer.Span.Clear(); + this.DestinationBuffer.GetSpan().Clear(); return; } diff --git a/src/ImageSharp/Memory/BufferExtensions.cs b/src/ImageSharp/Memory/BufferExtensions.cs index 0f04f34f2..3e7ebcdc8 100644 --- a/src/ImageSharp/Memory/BufferExtensions.cs +++ b/src/ImageSharp/Memory/BufferExtensions.cs @@ -52,7 +52,7 @@ namespace SixLabors.ImageSharp.Memory buffer.GetSpan().Clear(); } - public static ref T DangerousGetPinnableReference(this IBuffer buffer) + public static ref T GetReference(this IBuffer buffer) where T : struct => ref MemoryMarshal.GetReference(buffer.GetSpan()); diff --git a/src/ImageSharp/Memory/IBuffer2D{T}.cs b/src/ImageSharp/Memory/IBuffer2D{T}.cs index 2f60fd02a..0fc8867a6 100644 --- a/src/ImageSharp/Memory/IBuffer2D{T}.cs +++ b/src/ImageSharp/Memory/IBuffer2D{T}.cs @@ -6,7 +6,7 @@ using System; namespace SixLabors.ImageSharp.Memory { /// - /// An interface that represents a pinned buffer of value type objects + /// An interface that represents a contigous buffer of value type objects /// interpreted as a 2D region of x elements. /// /// The value type. @@ -24,8 +24,8 @@ namespace SixLabors.ImageSharp.Memory int Height { get; } /// - /// Gets a to the backing buffer. + /// Gets the contigous buffer being wrapped. /// - Span Span { get; } + IBuffer Buffer { get; } } } \ No newline at end of file diff --git a/src/ImageSharp/Memory/IBuffer{T}.cs b/src/ImageSharp/Memory/IBuffer{T}.cs index c23a46e6a..838c785bf 100644 --- a/src/ImageSharp/Memory/IBuffer{T}.cs +++ b/src/ImageSharp/Memory/IBuffer{T}.cs @@ -15,14 +15,14 @@ namespace SixLabors.ImageSharp.Memory where T : struct { /// - /// Gets the span to the memory "promised" by this buffer. + /// Gets the ownerd by this buffer. /// - /// The - Span GetSpan(); + Memory Memory { get; } /// - /// Gets the ownerd by this buffer. + /// Gets the span to the memory "promised" by this buffer. /// - Memory Memory { get; } + /// The + Span GetSpan(); } } \ No newline at end of file diff --git a/src/ImageSharp/PixelAccessor{TPixel}.cs b/src/ImageSharp/PixelAccessor{TPixel}.cs index 757983270..bcc758e9e 100644 --- a/src/ImageSharp/PixelAccessor{TPixel}.cs +++ b/src/ImageSharp/PixelAccessor{TPixel}.cs @@ -51,8 +51,7 @@ namespace SixLabors.ImageSharp /// public int Height { get; private set; } - /// - public Span Span => this.PixelBuffer.Span; + public IBuffer Buffer => this.PixelBuffer.Buffer; /// /// Gets or sets the pixel at the specified position. @@ -66,14 +65,14 @@ namespace SixLabors.ImageSharp get { this.CheckCoordinates(x, y); - return this.Span[(y * this.Width) + x]; + return this.GetSpan()[(y * this.Width) + x]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] set { this.CheckCoordinates(x, y); - Span span = this.Span; + Span span = this.GetSpan(); span[(y * this.Width) + x] = value; } } @@ -110,7 +109,7 @@ namespace SixLabors.ImageSharp /// The target pixel buffer accessor. internal void CopyTo(PixelAccessor target) { - this.PixelBuffer.Span.CopyTo(target.PixelBuffer.Span); + this.PixelBuffer.GetSpan().CopyTo(target.PixelBuffer.GetSpan()); } /// diff --git a/tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs b/tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs index c3d59b039..b84a78a55 100644 --- a/tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs +++ b/tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs @@ -31,11 +31,11 @@ namespace SixLabors.ImageSharp.Tests.Memory where T : struct { IBuffer buffer = this.MemoryManager.Allocate(length); - ref T ptrToPrevPosition0 = ref buffer.DangerousGetPinnableReference(); + ref T ptrToPrevPosition0 = ref buffer.GetReference(); buffer.Dispose(); buffer = this.MemoryManager.Allocate(length); - bool sameBuffers = Unsafe.AreSame(ref ptrToPrevPosition0, ref buffer.DangerousGetPinnableReference()); + bool sameBuffers = Unsafe.AreSame(ref ptrToPrevPosition0, ref buffer.GetReference()); buffer.Dispose(); return sameBuffers; @@ -159,7 +159,7 @@ namespace SixLabors.ImageSharp.Tests.Memory buffer = this.MemoryManager.Allocate(32); - Assert.False(Unsafe.AreSame(ref ptrToPrev0, ref buffer.DangerousGetPinnableReference())); + Assert.False(Unsafe.AreSame(ref ptrToPrev0, ref buffer.GetReference())); } [Fact] @@ -182,12 +182,12 @@ namespace SixLabors.ImageSharp.Tests.Memory int arrayLengthThreshold = PoolSelectorThresholdInBytes / sizeof(int); IBuffer small = this.MemoryManager.Allocate(arrayLengthThreshold - 1); - ref int ptr2Small = ref small.DangerousGetPinnableReference(); + ref int ptr2Small = ref small.GetReference(); small.Dispose(); IBuffer large = this.MemoryManager.Allocate(arrayLengthThreshold + 1); - Assert.False(Unsafe.AreSame(ref ptr2Small, ref large.DangerousGetPinnableReference())); + Assert.False(Unsafe.AreSame(ref ptr2Small, ref large.GetReference())); } [Fact] diff --git a/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs index 896bde035..ca3837ad2 100644 --- a/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs +++ b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs @@ -21,7 +21,7 @@ namespace SixLabors.ImageSharp.Tests.Memory where T : struct { ref T actual = ref MemoryMarshal.GetReference(span); - ref T expected = ref Unsafe.Add(ref buffer.DangerousGetPinnableReference(), bufferOffset); + ref T expected = ref Unsafe.Add(ref buffer.GetReference(), bufferOffset); Assert.True(Unsafe.AreSame(ref expected, ref actual), "span does not point to the expected position"); } @@ -71,7 +71,7 @@ namespace SixLabors.ImageSharp.Tests.Memory { using (Buffer2D buffer = this.MemoryManager.Allocate2D(42, 42, true)) { - Span span = buffer.Span; + Span span = buffer.GetSpan(); for (int j = 0; j < span.Length; j++) { Assert.Equal(0, span[j]); diff --git a/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs b/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs index db7367d97..bae3b4b61 100644 --- a/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs +++ b/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs @@ -117,7 +117,7 @@ namespace SixLabors.ImageSharp.Tests.Memory using (Buffer2D buffer = CreateTestBuffer(22, 13)) { buffer.GetArea().Clear(); - Span fullSpan = buffer.Span; + Span fullSpan = buffer.GetSpan(); Assert.True(fullSpan.SequenceEqual(new int[fullSpan.Length])); } } diff --git a/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs b/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs index b6385a424..b19b1b03b 100644 --- a/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs +++ b/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs @@ -276,7 +276,7 @@ namespace SixLabors.ImageSharp.Tests.Memory using (IManagedByteBuffer buffer = this.MemoryManager.AllocateManagedByteBuffer(desiredLength)) { ref byte array0 = ref buffer.Array[0]; - ref byte span0 = ref buffer.DangerousGetPinnableReference(); + ref byte span0 = ref buffer.GetReference(); Assert.True(Unsafe.AreSame(ref span0, ref array0)); Assert.True(buffer.Array.Length >= buffer.GetSpan().Length); diff --git a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs index 650b1a053..ad0d43dcc 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs @@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs using (IBuffer workBuffer = Configuration.Default.MemoryManager.Allocate(w)) { - fixed (Bgra32* destPtr = &workBuffer.DangerousGetPinnableReference()) + fixed (Bgra32* destPtr = &workBuffer.GetReference()) { for (int y = 0; y < h; y++) { @@ -78,7 +78,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs using (IBuffer workBuffer = Configuration.Default.MemoryManager.Allocate(w)) { - fixed (Bgr24* destPtr = &workBuffer.DangerousGetPinnableReference()) + fixed (Bgr24* destPtr = &workBuffer.GetReference()) { for (int y = 0; y < h; y++) { @@ -113,7 +113,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs using (IBuffer workBuffer = image.GetConfiguration().MemoryManager.Allocate(w)) { - fixed (Bgra32* sourcePtr = &workBuffer.DangerousGetPinnableReference()) + fixed (Bgra32* sourcePtr = &workBuffer.GetReference()) { for (int y = 0; y < h; y++) diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index b8c0489c8..3c5d5a7ba 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -33,7 +33,7 @@ namespace SixLabors.ImageSharp.Tests { using (Buffer2D temp = memoryManager.Allocate2D(img.Width, img.Height)) { - Span tempSpan = temp.Span; + Span tempSpan = temp.GetSpan(); foreach (ImageFrame frame in img.Frames) { Span pixelSpan = frame.GetPixelSpan(); @@ -665,7 +665,7 @@ namespace SixLabors.ImageSharp.Tests Span pixels = image.Frames.RootFrame.GetPixelSpan(); - Span bufferSpan = buffer.Span; + Span bufferSpan = buffer.GetSpan(); for (int i = 0; i < bufferSpan.Length; i++) {