From b61366ac005a9e7b08731a1a4db16e161bd4bb3d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Aug 2020 15:49:45 +0200 Subject: [PATCH 01/13] Add ByteMemoryManager type --- src/ImageSharp/Memory/ByteMemoryManager{T}.cs | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/ImageSharp/Memory/ByteMemoryManager{T}.cs diff --git a/src/ImageSharp/Memory/ByteMemoryManager{T}.cs b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs new file mode 100644 index 000000000..3a9eb34c2 --- /dev/null +++ b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs @@ -0,0 +1,67 @@ +// Copyright (c) Six Labors. +// Licensed under the Apache License, Version 2.0. +using System; +using System.Buffers; +using System.Runtime.InteropServices; + +namespace SixLabors.ImageSharp.Memory +{ + /// + /// A custom that can wrap of instances + /// and cast them to be for any arbitrary unmanaged value type. + /// + /// The value type to use when casting the wrapped instance. + internal sealed class ByteMemoryManager : MemoryManager + where T : unmanaged + { + /// + /// The wrapped of instance. + /// + private readonly Memory memory; + + /// + /// Initializes a new instance of the class. + /// + /// The of instance to wrap. + public ByteMemoryManager(Memory memory) + { + this.memory = memory; + } + + /// + protected override void Dispose(bool disposing) + { + } + + /// + public override Span GetSpan() + { + if (MemoryMarshal.TryGetArray(this.memory, out ArraySegment arraySegment)) + { + return MemoryMarshal.Cast(arraySegment.AsSpan()); + } + + if (MemoryMarshal.TryGetMemoryManager>(this.memory, out MemoryManager memoryManager)) + { + return MemoryMarshal.Cast(memoryManager.GetSpan()); + } + + // This should never be reached, as Memory can currently only be wrapping + // either a byte[] array or a MemoryManager instance in this case. + ThrowHelper.ThrowArgumentException("The input Memory instance was not valid.", nameof(this.memory)); + + return default; + } + + /// + public override MemoryHandle Pin(int elementIndex = 0) + { + return this.memory.Pin(); + } + + /// + public override void Unpin() + { + } + } +} From cd394109840e0a823b65cd569349279c235bf210 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Aug 2020 15:56:24 +0200 Subject: [PATCH 02/13] Add Image.WrapMemory from Memory --- src/ImageSharp/Image.WrapMemory.cs | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/src/ImageSharp/Image.WrapMemory.cs b/src/ImageSharp/Image.WrapMemory.cs index 2d3c29ed4..34bdeafdc 100644 --- a/src/ImageSharp/Image.WrapMemory.cs +++ b/src/ImageSharp/Image.WrapMemory.cs @@ -150,5 +150,70 @@ namespace SixLabors.ImageSharp int height) where TPixel : unmanaged, IPixel => WrapMemory(Configuration.Default, pixelMemoryOwner, width, height); + + /// + /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, + /// allowing to view/manipulate it as an ImageSharp instance. + /// + /// The pixel type + /// The + /// The byte memory representing the pixel data. + /// The width of the memory image. + /// The height of the memory image. + /// The . + /// The configuration is null. + /// The metadata is null. + /// An instance + public static Image WrapMemory( + Configuration configuration, + Memory byteMemory, + int width, + int height, + ImageMetadata metadata) + where TPixel : unmanaged, IPixel + { + Guard.NotNull(configuration, nameof(configuration)); + Guard.NotNull(metadata, nameof(metadata)); + + var memoryManager = new ByteMemoryManager(byteMemory); + var memorySource = MemoryGroup.Wrap(memoryManager.Memory); + return new Image(configuration, memorySource, width, height, metadata); + } + + /// + /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, + /// allowing to view/manipulate it as an ImageSharp instance. + /// + /// The pixel type + /// The + /// The byte memory representing the pixel data. + /// The width of the memory image. + /// The height of the memory image. + /// The configuration is null. + /// An instance. + public static Image WrapMemory( + Configuration configuration, + Memory byteMemory, + int width, + int height) + where TPixel : unmanaged, IPixel + => WrapMemory(configuration, byteMemory, width, height, new ImageMetadata()); + + /// + /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, + /// allowing to view/manipulate it as an ImageSharp instance. + /// The memory is being observed, the caller remains responsible for managing it's lifecycle. + /// + /// The pixel type. + /// The byte memory representing the pixel data. + /// The width of the memory image. + /// The height of the memory image. + /// An instance. + public static Image WrapMemory( + Memory byteMemory, + int width, + int height) + where TPixel : unmanaged, IPixel + => WrapMemory(Configuration.Default, byteMemory, width, height); } } From 129977e4654553bdd1b0fde12a16cb8e8093c3d3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Aug 2020 16:12:42 +0200 Subject: [PATCH 03/13] Add tests for new Image.WrapMemory APIs --- .../Image/ImageTests.WrapMemory.cs | 107 +++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs index 2b30d9459..c0cd3f56a 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs @@ -6,10 +6,10 @@ using System.Buffers; using System.Drawing; using System.Drawing.Imaging; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Common.Helpers; -using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.PixelFormats; using Xunit; @@ -80,6 +80,52 @@ namespace SixLabors.ImageSharp.Tests } } + public sealed class CastMemoryManager : MemoryManager + where TFrom : unmanaged + where TTo : unmanaged + { + private readonly Memory memory; + + public CastMemoryManager(Memory memory) + { + this.memory = memory; + } + + /// + protected override void Dispose(bool disposing) + { + } + + /// + public override Span GetSpan() + { + if (MemoryMarshal.TryGetArray(this.memory, out ArraySegment arraySegment)) + { + return MemoryMarshal.Cast(arraySegment.AsSpan()); + } + + if (MemoryMarshal.TryGetMemoryManager>(this.memory, out MemoryManager memoryManager)) + { + return MemoryMarshal.Cast(memoryManager.GetSpan()); + } + + ThrowHelper.ThrowArgumentException("The input Memory instance was not valid.", nameof(this.memory)); + + return default; + } + + /// + public override MemoryHandle Pin(int elementIndex = 0) + { + return this.memory.Pin(); + } + + /// + public override void Unpin() + { + } + } + [Fact] public void WrapMemory_CreatedImageIsCorrect() { @@ -173,6 +219,65 @@ namespace SixLabors.ImageSharp.Tests } } + [Fact] + public void WrapMemory_FromBytes_CreatedImageIsCorrect() + { + Configuration cfg = Configuration.Default.Clone(); + var metaData = new ImageMetadata(); + + var array = new byte[25 * Unsafe.SizeOf()]; + var memory = new Memory(array); + + using (var image = Image.WrapMemory(cfg, memory, 5, 5, metaData)) + { + Assert.True(image.TryGetSinglePixelSpan(out Span imageSpan)); + ref Rgba32 pixel0 = ref imageSpan[0]; + Assert.True(Unsafe.AreSame(ref Unsafe.As(ref array[0]), ref pixel0)); + + Assert.Equal(cfg, image.GetConfiguration()); + Assert.Equal(metaData, image.Metadata); + } + } + + [Fact] + public void WrapSystemDrawingBitmap_FromBytes_WhenObserved() + { + if (ShouldSkipBitmapTest) + { + return; + } + + using (var bmp = new Bitmap(51, 23)) + { + using (var memoryManager = new BitmapMemoryManager(bmp)) + { + Memory pixelMemory = memoryManager.Memory; + Memory byteMemory = new CastMemoryManager(pixelMemory).Memory; + Bgra32 bg = Color.Red; + Bgra32 fg = Color.Green; + + using (var image = Image.WrapMemory(byteMemory, bmp.Width, bmp.Height)) + { + Assert.Equal(pixelMemory, image.GetRootFramePixelBuffer().GetSingleMemory()); + Assert.True(image.TryGetSinglePixelSpan(out Span imageSpan)); + imageSpan.Fill(bg); + for (var i = 10; i < 20; i++) + { + image.GetPixelRowSpan(i).Slice(10, 10).Fill(fg); + } + } + + Assert.False(memoryManager.IsDisposed); + } + + string fn = System.IO.Path.Combine( + TestEnvironment.ActualOutputDirectoryFullPath, + $"{nameof(this.WrapSystemDrawingBitmap_WhenObserved)}.bmp"); + + bmp.Save(fn, ImageFormat.Bmp); + } + } + private static bool ShouldSkipBitmapTest => !TestEnvironment.Is64BitProcess || (TestHelpers.ImageSharpBuiltAgainst != "netcoreapp3.1" && TestHelpers.ImageSharpBuiltAgainst != "netcoreapp2.1"); } From 8f4417cf61c92cf400d50f6b4bb05022be7882b6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Aug 2020 16:30:27 +0200 Subject: [PATCH 04/13] Remove unnecessary indirection in ByteMemoryManager --- src/ImageSharp/Memory/ByteMemoryManager{T}.cs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/src/ImageSharp/Memory/ByteMemoryManager{T}.cs b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs index 3a9eb34c2..924230fc8 100644 --- a/src/ImageSharp/Memory/ByteMemoryManager{T}.cs +++ b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs @@ -36,21 +36,7 @@ namespace SixLabors.ImageSharp.Memory /// public override Span GetSpan() { - if (MemoryMarshal.TryGetArray(this.memory, out ArraySegment arraySegment)) - { - return MemoryMarshal.Cast(arraySegment.AsSpan()); - } - - if (MemoryMarshal.TryGetMemoryManager>(this.memory, out MemoryManager memoryManager)) - { - return MemoryMarshal.Cast(memoryManager.GetSpan()); - } - - // This should never be reached, as Memory can currently only be wrapping - // either a byte[] array or a MemoryManager instance in this case. - ThrowHelper.ThrowArgumentException("The input Memory instance was not valid.", nameof(this.memory)); - - return default; + return MemoryMarshal.Cast(this.memory.Span); } /// From b9dc71d37534810e03120dc94c26bb8dccdf2e85 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Aug 2020 16:46:05 +0200 Subject: [PATCH 05/13] Fix bug in Pin() method, minor code tweaks --- src/ImageSharp/Memory/ByteMemoryManager{T}.cs | 2 +- .../Image/ImageTests.WrapMemory.cs | 16 ++-------------- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/src/ImageSharp/Memory/ByteMemoryManager{T}.cs b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs index 924230fc8..70d1b1c30 100644 --- a/src/ImageSharp/Memory/ByteMemoryManager{T}.cs +++ b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.Memory /// public override MemoryHandle Pin(int elementIndex = 0) { - return this.memory.Pin(); + return this.memory.Slice(elementIndex).Pin(); } /// diff --git a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs index c0cd3f56a..06ee069c6 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs @@ -99,25 +99,13 @@ namespace SixLabors.ImageSharp.Tests /// public override Span GetSpan() { - if (MemoryMarshal.TryGetArray(this.memory, out ArraySegment arraySegment)) - { - return MemoryMarshal.Cast(arraySegment.AsSpan()); - } - - if (MemoryMarshal.TryGetMemoryManager>(this.memory, out MemoryManager memoryManager)) - { - return MemoryMarshal.Cast(memoryManager.GetSpan()); - } - - ThrowHelper.ThrowArgumentException("The input Memory instance was not valid.", nameof(this.memory)); - - return default; + return MemoryMarshal.Cast(this.memory.Span); } /// public override MemoryHandle Pin(int elementIndex = 0) { - return this.memory.Pin(); + return this.memory.Slice(elementIndex).Pin(); } /// From 77a6d08a463265897bc1239de6d1413cbc7ead10 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Aug 2020 17:15:16 +0200 Subject: [PATCH 06/13] Fix relative offsetting in Pin() methods --- src/ImageSharp/Memory/ByteMemoryManager{T}.cs | 6 +++++- tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs | 10 +++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/Memory/ByteMemoryManager{T}.cs b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs index 70d1b1c30..223709df6 100644 --- a/src/ImageSharp/Memory/ByteMemoryManager{T}.cs +++ b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. using System; using System.Buffers; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace SixLabors.ImageSharp.Memory @@ -42,7 +43,10 @@ namespace SixLabors.ImageSharp.Memory /// public override MemoryHandle Pin(int elementIndex = 0) { - return this.memory.Slice(elementIndex).Pin(); + // We need to adjust the offset into the wrapped byte segment, + // as the input index refers to the target-cast memory of T. + // We just have to shift this index by the byte size of T. + return this.memory.Slice(elementIndex * Unsafe.SizeOf()).Pin(); } /// diff --git a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs index 06ee069c6..ee8e4b97a 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs @@ -105,7 +105,15 @@ namespace SixLabors.ImageSharp.Tests /// public override MemoryHandle Pin(int elementIndex = 0) { - return this.memory.Slice(elementIndex).Pin(); + int byteOffset = elementIndex * Unsafe.SizeOf(); + int shiftedOffset = Math.DivRem(byteOffset, Unsafe.SizeOf(), out int remainder); + + if (remainder != 0) + { + ThrowHelper.ThrowArgumentException("The input index doesn't result in an aligned item access", nameof(elementIndex)); + } + + return this.memory.Slice(shiftedOffset).Pin(); } /// From 9642d0e14c9f6398abdb0299e5ea16d94bc0e57c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Aug 2020 17:45:38 +0200 Subject: [PATCH 07/13] Fix comparison in wrapping bytes test --- .../ImageSharp.Tests/Image/ImageTests.WrapMemory.cs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs index ee8e4b97a..9e1ebbf85 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs @@ -254,8 +254,16 @@ namespace SixLabors.ImageSharp.Tests using (var image = Image.WrapMemory(byteMemory, bmp.Width, bmp.Height)) { - Assert.Equal(pixelMemory, image.GetRootFramePixelBuffer().GetSingleMemory()); - Assert.True(image.TryGetSinglePixelSpan(out Span imageSpan)); + Span pixelSpan = pixelMemory.Span; + Span imageSpan = image.GetRootFramePixelBuffer().GetSingleMemory().Span; + + // We can't compare the two Memory instances directly as they wrap different memory managers. + // To check that the underlying data matches, we can just manually check their lenth, and the + // fact that a reference to the first pixel in both spans is actually the same memory location. + Assert.Equal(pixelSpan.Length, imageSpan.Length); + Assert.True(Unsafe.AreSame(ref pixelSpan.GetPinnableReference(), ref imageSpan.GetPinnableReference())); + + Assert.True(image.TryGetSinglePixelSpan(out imageSpan)); imageSpan.Fill(bg); for (var i = 10; i < 20; i++) { From 0e5ddabb7cd65392315331648a26fe34ac0d90de Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 13 Aug 2020 19:31:04 +0200 Subject: [PATCH 08/13] Replace Default.Clone with CreateDefaultConfiguration --- tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs index 9e1ebbf85..bb22f59a3 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs @@ -125,7 +125,7 @@ namespace SixLabors.ImageSharp.Tests [Fact] public void WrapMemory_CreatedImageIsCorrect() { - Configuration cfg = Configuration.Default.Clone(); + var cfg = Configuration.CreateDefaultInstance(); var metaData = new ImageMetadata(); var array = new Rgba32[25]; @@ -218,7 +218,7 @@ namespace SixLabors.ImageSharp.Tests [Fact] public void WrapMemory_FromBytes_CreatedImageIsCorrect() { - Configuration cfg = Configuration.Default.Clone(); + var cfg = Configuration.CreateDefaultInstance(); var metaData = new ImageMetadata(); var array = new byte[25 * Unsafe.SizeOf()]; From 575402d3312be739e0e16d76e3d550589774c2e9 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 25 Aug 2020 22:35:10 +0200 Subject: [PATCH 09/13] Add input size validation in Image.WrapMemory --- src/ImageSharp/Image.WrapMemory.cs | 5 ++ .../Image/ImageTests.WrapMemory.cs | 51 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/ImageSharp/Image.WrapMemory.cs b/src/ImageSharp/Image.WrapMemory.cs index 34bdeafdc..081ed22a1 100644 --- a/src/ImageSharp/Image.WrapMemory.cs +++ b/src/ImageSharp/Image.WrapMemory.cs @@ -38,6 +38,7 @@ namespace SixLabors.ImageSharp { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(metadata, nameof(metadata)); + Guard.IsTrue(pixelMemory.Length == width * height, nameof(pixelMemory), "The length of the input memory doesn't match the specified image size"); var memorySource = MemoryGroup.Wrap(pixelMemory); return new Image(configuration, memorySource, width, height, metadata); @@ -105,6 +106,7 @@ namespace SixLabors.ImageSharp { Guard.NotNull(configuration, nameof(configuration)); Guard.NotNull(metadata, nameof(metadata)); + Guard.IsTrue(pixelMemoryOwner.Memory.Length == width * height, nameof(pixelMemoryOwner), "The length of the input memory doesn't match the specified image size"); var memorySource = MemoryGroup.Wrap(pixelMemoryOwner); return new Image(configuration, memorySource, width, height, metadata); @@ -176,6 +178,9 @@ namespace SixLabors.ImageSharp Guard.NotNull(metadata, nameof(metadata)); var memoryManager = new ByteMemoryManager(byteMemory); + + Guard.IsTrue(memoryManager.Memory.Length == width * height, nameof(byteMemory), "The length of the input memory doesn't match the specified image size"); + var memorySource = MemoryGroup.Wrap(memoryManager.Memory); return new Image(configuration, memorySource, width, height, metadata); } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs index bb22f59a3..7dc7dbb30 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs @@ -282,6 +282,57 @@ namespace SixLabors.ImageSharp.Tests } } + [Theory] + [InlineData(0, 5, 5)] + [InlineData(20, 5, 5)] + [InlineData(26, 5, 5)] + [InlineData(2, 1, 1)] + [InlineData(1023, 32, 32)] + public void WrapMemory_MemoryOfT_InvalidSize(int size, int height, int width) + { + var array = new Rgba32[size]; + var memory = new Memory(array); + + Assert.Throws(() => Image.WrapMemory(memory, height, width)); + } + + private class TestMemoryOwner : IMemoryOwner + { + public Memory Memory { get; set; } + + public void Dispose() + { + } + } + + [Theory] + [InlineData(0, 5, 5)] + [InlineData(20, 5, 5)] + [InlineData(26, 5, 5)] + [InlineData(2, 1, 1)] + [InlineData(1023, 32, 32)] + public void WrapMemory_IMemoryOwnerOfT_InvalidSize(int size, int height, int width) + { + var array = new Rgba32[size]; + var memory = new TestMemoryOwner { Memory = array }; + + Assert.Throws(() => Image.WrapMemory(memory, height, width)); + } + + [Theory] + [InlineData(0, 5, 5)] + [InlineData(20, 5, 5)] + [InlineData(26, 5, 5)] + [InlineData(2, 1, 1)] + [InlineData(1023, 32, 32)] + public void WrapMemory_MemoryOfByte_InvalidSize(int size, int height, int width) + { + var array = new byte[size * Unsafe.SizeOf()]; + var memory = new Memory(array); + + Assert.Throws(() => Image.WrapMemory(memory, height, width)); + } + private static bool ShouldSkipBitmapTest => !TestEnvironment.Is64BitProcess || (TestHelpers.ImageSharpBuiltAgainst != "netcoreapp3.1" && TestHelpers.ImageSharpBuiltAgainst != "netcoreapp2.1"); } From 5397ab328fa51c5a0d07924b9bdec9773fd0d2d3 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 25 Aug 2020 22:39:21 +0200 Subject: [PATCH 10/13] Minor optimizations, improve XML docs and annotations --- .../Memory/MemoryOwnerExtensions.cs | 37 +++++++++++++++++-- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Memory/MemoryOwnerExtensions.cs b/src/ImageSharp/Memory/MemoryOwnerExtensions.cs index 98fd40e65..0cff94110 100644 --- a/src/ImageSharp/Memory/MemoryOwnerExtensions.cs +++ b/src/ImageSharp/Memory/MemoryOwnerExtensions.cs @@ -3,6 +3,7 @@ using System; using System.Buffers; +using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -13,12 +14,29 @@ namespace SixLabors.ImageSharp.Memory /// internal static class MemoryOwnerExtensions { + /// + /// Gets a from an instance. + /// + /// The buffer + /// The + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span GetSpan(this IMemoryOwner buffer) - => buffer.Memory.Span; + { + return buffer.Memory.Span; + } + /// + /// Gets the length of an internal buffer. + /// + /// The buffer + /// The length of the buffer + [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Length(this IMemoryOwner buffer) - => buffer.GetSpan().Length; + { + return buffer.Memory.Length; + } /// /// Gets a to an offsetted position inside the buffer. @@ -26,6 +44,7 @@ namespace SixLabors.ImageSharp.Memory /// The buffer /// The start /// The + [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span Slice(this IMemoryOwner buffer, int start) { @@ -39,6 +58,7 @@ namespace SixLabors.ImageSharp.Memory /// The start /// The length of the slice /// The + [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span Slice(this IMemoryOwner buffer, int start, int length) { @@ -55,8 +75,17 @@ namespace SixLabors.ImageSharp.Memory buffer.GetSpan().Clear(); } + /// + /// Gets a reference to the first item in the internal buffer for an instance. + /// + /// The buffer + /// A reference to the first item within the memory wrapped by + [Pure] + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ref T GetReference(this IMemoryOwner buffer) - where T : struct => - ref MemoryMarshal.GetReference(buffer.GetSpan()); + where T : struct + { + return ref MemoryMarshal.GetReference(buffer.GetSpan()); + } } } From 88a93fafe2b4f7db2086561e6195d3998533be9c Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 25 Aug 2020 22:41:43 +0200 Subject: [PATCH 11/13] Remove unnecessary Memory.Span access --- src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs b/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs index b11411e32..448eb3833 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs @@ -262,7 +262,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering this.source = source; this.bounds = bounds; this.scale = processor.DitherScale; - this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(processor.Palette.Span.Length); + this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(processor.Palette.Length); } [MethodImpl(InliningOptions.ShortMethod)] From 16c9dba5bc4ed336d375efd9270f0a4f8bd54413 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 26 Aug 2020 01:25:02 +0200 Subject: [PATCH 12/13] Simplify XML docs for WrapMemory APIs --- src/ImageSharp/Image.WrapMemory.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp/Image.WrapMemory.cs b/src/ImageSharp/Image.WrapMemory.cs index 081ed22a1..d89c44dc5 100644 --- a/src/ImageSharp/Image.WrapMemory.cs +++ b/src/ImageSharp/Image.WrapMemory.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp { /// /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, - /// allowing to view/manipulate it as an ImageSharp instance. + /// allowing to view/manipulate it as an instance. /// /// The pixel type /// The @@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp /// /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, - /// allowing to view/manipulate it as an ImageSharp instance. + /// allowing to view/manipulate it as an instance. /// /// The pixel type /// The @@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp /// /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, - /// allowing to view/manipulate it as an ImageSharp instance. + /// allowing to view/manipulate it as an instance. /// The memory is being observed, the caller remains responsible for managing it's lifecycle. /// /// The pixel type. @@ -82,7 +82,7 @@ namespace SixLabors.ImageSharp /// /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, - /// allowing to view/manipulate it as an ImageSharp instance. + /// allowing to view/manipulate it as an instance. /// The ownership of the is being transferred to the new instance, /// meaning that the caller is not allowed to dispose . /// It will be disposed together with the result image. @@ -114,7 +114,7 @@ namespace SixLabors.ImageSharp /// /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, - /// allowing to view/manipulate it as an ImageSharp instance. + /// allowing to view/manipulate it as an instance. /// The ownership of the is being transferred to the new instance, /// meaning that the caller is not allowed to dispose . /// It will be disposed together with the result image. @@ -136,7 +136,7 @@ namespace SixLabors.ImageSharp /// /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, - /// allowing to view/manipulate it as an ImageSharp instance. + /// allowing to view/manipulate it as an instance. /// The ownership of the is being transferred to the new instance, /// meaning that the caller is not allowed to dispose . /// It will be disposed together with the result image. @@ -155,7 +155,7 @@ namespace SixLabors.ImageSharp /// /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, - /// allowing to view/manipulate it as an ImageSharp instance. + /// allowing to view/manipulate it as an instance. /// /// The pixel type /// The @@ -187,7 +187,7 @@ namespace SixLabors.ImageSharp /// /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, - /// allowing to view/manipulate it as an ImageSharp instance. + /// allowing to view/manipulate it as an instance. /// /// The pixel type /// The @@ -206,7 +206,7 @@ namespace SixLabors.ImageSharp /// /// Wraps an existing contiguous memory area of 'width' x 'height' pixels, - /// allowing to view/manipulate it as an ImageSharp instance. + /// allowing to view/manipulate it as an instance. /// The memory is being observed, the caller remains responsible for managing it's lifecycle. /// /// The pixel type. From d6e3f9fbbc02715c14139075805d9d84a68dc885 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Tue, 1 Sep 2020 13:21:56 +0200 Subject: [PATCH 13/13] Remove [Pure] attributes --- src/ImageSharp/Memory/MemoryOwnerExtensions.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/ImageSharp/Memory/MemoryOwnerExtensions.cs b/src/ImageSharp/Memory/MemoryOwnerExtensions.cs index 0cff94110..c2551ccf2 100644 --- a/src/ImageSharp/Memory/MemoryOwnerExtensions.cs +++ b/src/ImageSharp/Memory/MemoryOwnerExtensions.cs @@ -3,7 +3,6 @@ using System; using System.Buffers; -using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -19,7 +18,6 @@ namespace SixLabors.ImageSharp.Memory /// /// The buffer /// The - [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span GetSpan(this IMemoryOwner buffer) { @@ -31,7 +29,6 @@ namespace SixLabors.ImageSharp.Memory /// /// The buffer /// The length of the buffer - [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int Length(this IMemoryOwner buffer) { @@ -44,7 +41,6 @@ namespace SixLabors.ImageSharp.Memory /// The buffer /// The start /// The - [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span Slice(this IMemoryOwner buffer, int start) { @@ -58,7 +54,6 @@ namespace SixLabors.ImageSharp.Memory /// The start /// The length of the slice /// The - [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span Slice(this IMemoryOwner buffer, int start, int length) { @@ -80,7 +75,6 @@ namespace SixLabors.ImageSharp.Memory /// /// The buffer /// A reference to the first item within the memory wrapped by - [Pure] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ref T GetReference(this IMemoryOwner buffer) where T : struct