From 9984ffa34883bb61ec4a29eeb1fb81169dfe1997 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 12 May 2017 00:17:05 +1000 Subject: [PATCH] Replace BufferSpan with Span - Add System.Memory - Update System.Runtime.CompilerServices.Unsafe - Move memory classes/structs to ImageSharp.Memory - Replace BufferSpan with Span and adjust methods accordingly --- .../Brushes/ImageBrush{TPixel}.cs | 9 +- .../Brushes/PatternBrush{TPixel}.cs | 6 +- .../Brushes/Processors/BrushApplicator.cs | 6 +- .../Brushes/RecolorBrush{TPixel}.cs | 7 +- .../Brushes/SolidBrush{TPixel}.cs | 7 +- .../Processors/DrawImageProcessor.cs | 6 +- .../Processors/DrawPathProcessor.cs | 4 +- .../Processors/FillProcessor.cs | 2 + .../Processors/FillRegionProcessor.cs | 2 + src/ImageSharp/Common/Helpers/DebugGuard.cs | 4 +- src/ImageSharp/Common/Memory/BufferSpan{T}.cs | 205 ------------------ .../Dithering/ErrorDiffusion/Atkinson.cs | 2 + .../Dithering/ErrorDiffusion/Burks.cs | 2 + .../Dithering/ErrorDiffusion/ErrorDiffuser.cs | 1 + .../ErrorDiffusion/FloydSteinberg.cs | 2 + .../ErrorDiffusion/JarvisJudiceNinke.cs | 2 + .../Dithering/ErrorDiffusion/Sierra2.cs | 2 + .../Dithering/ErrorDiffusion/Sierra3.cs | 2 + .../Dithering/ErrorDiffusion/SierraLite.cs | 2 + .../Dithering/ErrorDiffusion/Stucki.cs | 2 + src/ImageSharp/Dithering/Ordered/Bayer.cs | 2 + src/ImageSharp/Dithering/Ordered/Ordered.cs | 2 + .../Dithering/Ordered/OrderedDither4x4.cs | 1 + .../Components/Decoder/JpegBlockProcessor.cs | 2 + .../Jpeg/Components/Decoder/JpegPixelArea.cs | 2 + .../Components/Decoder/JpegScanDecoder.cs | 2 + .../Jpeg/Components/Decoder/YCbCrImage.cs | 2 + .../Formats/Jpeg/JpegDecoderCore.cs | 1 + .../Formats/Png/Filters/AverageFilter.cs | 5 +- .../Formats/Png/Filters/NoneFilter.cs | 6 +- .../Formats/Png/Filters/PaethFilter.cs | 5 +- .../Formats/Png/Filters/SubFilter.cs | 5 +- .../Formats/Png/Filters/UpFilter.cs | 5 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 9 +- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 14 +- src/ImageSharp/Image/ImageBase{TPixel}.cs | 2 + src/ImageSharp/Image/PixelAccessor{TPixel}.cs | 54 +++-- src/ImageSharp/Image/PixelArea{TPixel}.cs | 8 +- src/ImageSharp/ImageSharp.csproj | 3 +- src/ImageSharp/{Common => }/Memory/Buffer.cs | 31 ++- .../{Common => }/Memory/Buffer2D.cs | 3 +- .../{Common => }/Memory/Buffer2DExtensions.cs | 14 +- .../{Common => }/Memory/Fast2DArray{T}.cs | 6 +- .../{Common => }/Memory/IBuffer2D.cs | 8 +- .../{Common => }/Memory/PixelDataPool{T}.cs | 3 +- .../BufferSpan.cs => Memory/SpanHelper.cs} | 24 +- .../DefaultAddPixelBlender{TPixel}.cs | 10 +- .../DefaultDarkenPixelBlender{TPixel}.cs | 10 +- .../DefaultHardLightPixelBlender{TPixel}.cs | 10 +- .../DefaultLightenPixelBlender{TPixel}.cs | 10 +- .../DefaultMultiplyPixelBlender{TPixel}.cs | 10 +- .../DefaultNormalPixelBlender{TPixel}.cs | 10 +- .../DefaultOverlayPixelBlender{TPixel}.cs | 10 +- .../DefaultScreenPixelBlender{TPixel}.cs | 10 +- .../DefaultSubstractPixelBlender{TPixel}.cs | 10 +- .../PixelFormats/PixelBlender{TPixel}.cs | 4 +- .../PixelFormats/PixelOperations{TPixel}.cs | 75 ++++--- .../PixelFormats/Rgba32.PixelOperations.cs | 35 +-- .../RgbaVector.PixelOperations.cs | 7 +- .../Convolution/BoxBlurProcessor.cs | 1 + .../Convolution/Convolution2DProcessor.cs | 1 + .../Convolution/Convolution2PassProcessor.cs | 1 + .../Convolution/ConvolutionProcessor.cs | 1 + .../EdgeDetection/EdgeDetector2DProcessor.cs | 1 + .../EdgeDetectorCompassProcessor.cs | 1 + .../EdgeDetection/EdgeDetectorProcessor.cs | 1 + .../EdgeDetection/KayyaliProcessor.cs | 1 + .../EdgeDetection/KirschProcessor.cs | 1 + .../EdgeDetection/Laplacian3X3Processor.cs | 1 + .../EdgeDetection/Laplacian5X5Processor.cs | 1 + .../LaplacianOfGaussianProcessor.cs | 1 + .../EdgeDetection/PrewittProcessor.cs | 1 + .../EdgeDetection/RobertsCrossProcessor.cs | 1 + .../EdgeDetection/RobinsonProcessor.cs | 1 + .../EdgeDetection/ScharrProcessor.cs | 1 + .../EdgeDetection/SobelProcessor.cs | 1 + .../Convolution/GaussianBlurProcessor.cs | 1 + .../Convolution/GaussianSharpenProcessor.cs | 1 + .../Effects/BackgroundColorProcessor.cs | 3 +- .../Processors/Overlays/GlowProcessor.cs | 3 +- .../Processors/Overlays/VignetteProcessor.cs | 3 +- .../ResamplingWeightedProcessor.Weights.cs | 16 +- .../Processors/Transforms/ResizeProcessor.cs | 3 +- .../Bulk/PackFromVector4ReferenceVsPointer.cs | 2 +- .../Color/Bulk/PackFromXyzw.cs | 1 + .../Color/Bulk/ToVector4.cs | 1 + .../ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs | 5 +- .../Color/Bulk/ToXyzw.cs | 8 +- .../ImageSharp.Benchmarks/General/Array2D.cs | 2 + .../General/ClearBuffer.cs | 3 +- .../General/IterateArray.cs | 2 + .../General/PixelIndexing.cs | 2 + .../General/Vectorization/VectorFetching.cs | 6 +- .../PixelBlenders/PorterDuffBulkVsPixel.cs | 17 +- tests/ImageSharp.Benchmarks/Samplers/Glow.cs | 2 + .../ImageSharp.Benchmarks/Samplers/Resize.cs | 54 ++--- .../Colors/PixelOperationsTests.cs | 17 +- .../ImageSharp.Tests/Common/Buffer2DTests.cs | 12 +- .../Common/BufferSpanTests.cs | 115 +++++----- tests/ImageSharp.Tests/Common/BufferTests.cs | 30 ++- .../Common/Fast2DArrayTests.cs | 2 + .../Common/PixelDataPoolTests.cs | 1 + .../Drawing/FillPatternTests.cs | 1 + tests/ImageSharp.Tests/ImageComparer.cs | 1 + .../PorterDuffFunctionsTests_TPixel.cs | 22 +- .../TestUtilities/TestPixel.cs | 4 +- 106 files changed, 500 insertions(+), 562 deletions(-) delete mode 100644 src/ImageSharp/Common/Memory/BufferSpan{T}.cs rename src/ImageSharp/{Common => }/Memory/Buffer.cs (88%) rename src/ImageSharp/{Common => }/Memory/Buffer2D.cs (98%) rename src/ImageSharp/{Common => }/Memory/Buffer2DExtensions.cs (71%) rename src/ImageSharp/{Common => }/Memory/Fast2DArray{T}.cs (96%) rename src/ImageSharp/{Common => }/Memory/IBuffer2D.cs (84%) rename src/ImageSharp/{Common => }/Memory/PixelDataPool{T}.cs (98%) rename src/ImageSharp/{Common/Memory/BufferSpan.cs => Memory/SpanHelper.cs} (82%) diff --git a/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs index 5038ea01b..6f851e5c3 100644 --- a/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs @@ -5,7 +5,10 @@ namespace ImageSharp.Drawing.Brushes { + using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; @@ -114,7 +117,7 @@ namespace ImageSharp.Drawing.Brushes } /// - internal override void Apply(BufferSpan scanline, int x, int y) + internal override void Apply(Span scanline, int x, int y) { // create a span for colors using (Buffer amountBuffer = new Buffer(scanline.Length)) @@ -122,7 +125,7 @@ namespace ImageSharp.Drawing.Brushes { int sourceY = (y - this.offsetY) % this.yLength; int offsetX = x - this.offsetX; - BufferSpan sourceRow = this.source.GetRowSpan(sourceY); + Span sourceRow = this.source.GetRowSpan(sourceY); for (int i = 0; i < scanline.Length; i++) { @@ -133,7 +136,7 @@ namespace ImageSharp.Drawing.Brushes overlay[i] = pixel; } - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } diff --git a/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs index dc8a4bc90..90990e54a 100644 --- a/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Drawing.Brushes { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; @@ -147,7 +149,7 @@ namespace ImageSharp.Drawing.Brushes } /// - internal override void Apply(BufferSpan scanline, int x, int y) + internal override void Apply(Span scanline, int x, int y) { int patternY = y % this.pattern.Height; using (Buffer amountBuffer = new Buffer(scanline.Length)) @@ -161,7 +163,7 @@ namespace ImageSharp.Drawing.Brushes overlay[i] = this.pattern[patternY, patternX]; } - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } diff --git a/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs b/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs index d7c70220c..29629324a 100644 --- a/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs +++ b/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Drawing.Processors { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -64,7 +66,7 @@ namespace ImageSharp.Drawing.Processors /// The x position in the target pixel space that the start of the scanline data corresponds to. /// The y position in the target pixel space that whole scanline corresponds to. /// scanlineBuffer will be > scanlineWidth but provide and offset in case we want to share a larger buffer across runs. - internal virtual void Apply(BufferSpan scanline, int x, int y) + internal virtual void Apply(Span scanline, int x, int y) { using (Buffer amountBuffer = new Buffer(scanline.Length)) using (Buffer overlay = new Buffer(scanline.Length)) @@ -79,7 +81,7 @@ namespace ImageSharp.Drawing.Processors overlay[i] = this[x + i, y]; } - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } diff --git a/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs index a96202b7b..64b91e384 100644 --- a/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs @@ -5,7 +5,10 @@ namespace ImageSharp.Drawing.Brushes { + using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; @@ -139,7 +142,7 @@ namespace ImageSharp.Drawing.Brushes } /// - internal override void Apply(BufferSpan scanline, int x, int y) + internal override void Apply(Span scanline, int x, int y) { using (Buffer amountBuffer = new Buffer(scanline.Length)) using (Buffer overlay = new Buffer(scanline.Length)) @@ -155,7 +158,7 @@ namespace ImageSharp.Drawing.Brushes overlay[i] = this[offsetX, y]; } - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } diff --git a/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs index 71b802136..28f7b0e45 100644 --- a/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs @@ -5,7 +5,10 @@ namespace ImageSharp.Drawing.Brushes { + using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; @@ -87,9 +90,9 @@ namespace ImageSharp.Drawing.Brushes } /// - internal override void Apply(BufferSpan scanline, int x, int y) + internal override void Apply(Span scanline, int x, int y) { - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); using (Buffer amountBuffer = new Buffer(scanline.Length)) { diff --git a/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs b/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs index c49631de8..ed45417fc 100644 --- a/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Drawing.Processors using System; using System.Numerics; using System.Threading.Tasks; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using ImageSharp.Processing; @@ -97,8 +99,8 @@ namespace ImageSharp.Drawing.Processors this.ParallelOptions, y => { - BufferSpan background = sourcePixels.GetRowSpan(y).Slice(minX, width); - BufferSpan foreground = toBlendPixels.GetRowSpan(y - this.Location.Y).Slice(0, width); + Span background = sourcePixels.GetRowSpan(y).Slice(minX, width); + Span foreground = toBlendPixels.GetRowSpan(y - this.Location.Y).Slice(0, width); this.blender.Blend(background, background, foreground, amount); }); } diff --git a/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs b/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs index 3fd829549..d1332c435 100644 --- a/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Drawing.Processors using System; using System.Numerics; using System.Threading.Tasks; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using ImageSharp.Processing; using Pens; @@ -110,7 +112,7 @@ namespace ImageSharp.Drawing.Processors colors[i] = color.Color; } - BufferSpan destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width); + Span destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width); blender.Blend(destination, destination, colors, amount); } }); diff --git a/src/ImageSharp.Drawing/Processors/FillProcessor.cs b/src/ImageSharp.Drawing/Processors/FillProcessor.cs index 25eccd245..fa6f48156 100644 --- a/src/ImageSharp.Drawing/Processors/FillProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/FillProcessor.cs @@ -10,6 +10,8 @@ namespace ImageSharp.Drawing.Processors using System.Threading.Tasks; using Drawing; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using ImageSharp.Processing; diff --git a/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs b/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs index 323214fb8..a57be3a5a 100644 --- a/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Drawing.Processors using System; using System.Buffers; using Drawing; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using ImageSharp.Processing; diff --git a/src/ImageSharp/Common/Helpers/DebugGuard.cs b/src/ImageSharp/Common/Helpers/DebugGuard.cs index f7c83452f..096f96f04 100644 --- a/src/ImageSharp/Common/Helpers/DebugGuard.cs +++ b/src/ImageSharp/Common/Helpers/DebugGuard.cs @@ -170,7 +170,7 @@ namespace ImageSharp /// /// is true /// - public static void MustBeSameSized(BufferSpan target, BufferSpan other, string parameterName) + public static void MustBeSameSized(Span target, Span other, string parameterName) where T : struct { if (target.Length != other.Length) @@ -189,7 +189,7 @@ namespace ImageSharp /// /// is true /// - public static void MustBeSizedAtLeast(BufferSpan target, BufferSpan minSpan, string parameterName) + public static void MustBeSizedAtLeast(Span target, Span minSpan, string parameterName) where T : struct { if (target.Length < minSpan.Length) diff --git a/src/ImageSharp/Common/Memory/BufferSpan{T}.cs b/src/ImageSharp/Common/Memory/BufferSpan{T}.cs deleted file mode 100644 index 1b0bef314..000000000 --- a/src/ImageSharp/Common/Memory/BufferSpan{T}.cs +++ /dev/null @@ -1,205 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - using System.Diagnostics; - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - - /// - /// Represents a contiguous region of a pinned managed array. - /// The array is usually owned by a instance. - /// - /// - /// is very similar to corefx System.Span<T>, and we try to maintain a compatible API. - /// There are several differences though: - /// - It's not possible to use it with stack objects or pointers to unmanaged memory, only with managed arrays. - /// - It's possible to retrieve a reference to the array () so we can pass it to API-s like - /// - It's possible to retrieve the pinned pointer. This enables optimized (unchecked) unsafe operations. - /// - There is no bounds checking for performance reasons, only in debug mode. This makes an unsafe type! - /// - /// The type of elements of the array - internal unsafe struct BufferSpan - where T : struct - { - /// - /// Initializes a new instance of the struct from a pinned array and an start. - /// - /// The pinned array - /// The index at which to begin the span. - /// The length - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan(T[] array, int start, int length) - { - GuardArray(array); - - DebugGuard.MustBeLessThanOrEqualTo(start, array.Length, nameof(start)); - DebugGuard.MustBeLessThanOrEqualTo(length, array.Length - start, nameof(length)); - - this.Array = array; - this.Length = length; - this.Start = start; - } - - /// - /// Initializes a new instance of the struct from a pinned array and an start. - /// - /// The pinned array - /// The index at which to begin the span. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan(T[] array, int start) - { - GuardArray(array); - DebugGuard.MustBeLessThanOrEqualTo(start, array.Length, nameof(start)); - - this.Array = array; - this.Length = array.Length - start; - this.Start = start; - } - - /// - /// Initializes a new instance of the struct from a pinned array. - /// - /// The pinned array - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan(T[] array) - { - GuardArray(array); - - this.Array = array; - this.Start = 0; - this.Length = array.Length; - } - - /// - /// Gets the backing array. - /// - public T[] Array { get; private set; } - - /// - /// Gets the length of the - /// - public int Length { get; private set; } - - /// - /// Gets the start inside - /// - public int Start { get; private set; } - - /// - /// Gets the start inside in bytes. - /// - public int ByteOffset => this.Start * Unsafe.SizeOf(); - - /// - /// Returns a reference to specified element of the span. - /// - /// The index - /// The reference to the specified element - public ref T this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - DebugGuard.MustBeLessThan(index, this.Length, nameof(index)); - ref T startRef = ref this.DangerousGetPinnableReference(); - return ref Unsafe.Add(ref startRef, index); - } - } - - /// - /// Converts generic to a of bytes - /// setting it's and to correct values. - /// - /// The span of bytes - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan AsBytes() - { - BufferSpan result = default(BufferSpan); - result.Array = Unsafe.As(this.Array); - result.Start = this.Start * Unsafe.SizeOf(); - result.Length = this.Length * Unsafe.SizeOf(); - return result; - } - - /// - /// Returns a reference to the 0th element of the Span. If the Span is empty, returns a reference to the location where the 0th element - /// would have been stored. Such a reference can be used for pinning but must never be dereferenced. - /// - /// The reference to the 0th element - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref T DangerousGetPinnableReference() - { - ref T origin = ref this.Array[0]; - return ref Unsafe.Add(ref origin, this.Start); - } - - /// - /// Forms a slice out of the given BufferSpan, beginning at 'start'. - /// - /// TThe index at which to begin this slice. - /// The offseted (sliced) BufferSpan - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan Slice(int start) - { - DebugGuard.MustBeLessThan(start, this.Length, nameof(start)); - - BufferSpan result = default(BufferSpan); - result.Array = this.Array; - result.Start = this.Start + start; - result.Length = this.Length - start; - return result; - } - - /// - /// Forms a slice out of the given BufferSpan, beginning at 'start'. - /// - /// The index at which to begin this slice. - /// The desired length for the slice (exclusive). - /// The sliced BufferSpan - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan Slice(int start, int length) - { - DebugGuard.MustBeLessThanOrEqualTo(start, this.Length, nameof(start)); - DebugGuard.MustBeLessThanOrEqualTo(length, this.Length - start, nameof(length)); - - BufferSpan result = default(BufferSpan); - result.Array = this.Array; - result.Start = this.Start + start; - result.Length = length; - return result; - } - - /// - /// Clears `count` elements from the beginning of the span. - /// - /// The number of elements to clear - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Clear(int count) - { - DebugGuard.MustBeLessThanOrEqualTo(count, this.Length, nameof(count)); - - // TODO: Use Unsafe.InitBlock(ref T) for small arrays, when it get's official - System.Array.Clear(this.Array, this.Start, count); - } - - /// - /// Clears the the span - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Clear() - { - this.Clear(this.Length); - } - - [Conditional("DEBUG")] - private static void GuardArray(T[] array) - { - DebugGuard.NotNull(array, nameof(array)); - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs index b94b87255..629944ea1 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Atkinson image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs index 894b6e236..02d41c369 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Burks image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs index 5d0ecde6b..7a5fabdb3 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Dithering using System.Numerics; using System.Runtime.CompilerServices; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs b/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs index f7a93667f..6165da634 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Floyd–Steinberg image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs b/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs index 60fef8121..6daeab32f 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the JarvisJudiceNinke image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs index 4325438e0..0c0944d0e 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Sierra2 image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs index 25ea70d0a..2e2220846 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Sierra3 image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs b/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs index c7b1d214f..fe4c933a9 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the SierraLite image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs index 93258c350..b04c16481 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Stucki image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/Ordered/Bayer.cs b/src/ImageSharp/Dithering/Ordered/Bayer.cs index 3792c3c02..ded17d1e1 100644 --- a/src/ImageSharp/Dithering/Ordered/Bayer.cs +++ b/src/ImageSharp/Dithering/Ordered/Bayer.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering.Ordered { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the 4x4 Bayer dithering matrix. /// diff --git a/src/ImageSharp/Dithering/Ordered/Ordered.cs b/src/ImageSharp/Dithering/Ordered/Ordered.cs index ae75b87f2..1fd39eb8b 100644 --- a/src/ImageSharp/Dithering/Ordered/Ordered.cs +++ b/src/ImageSharp/Dithering/Ordered/Ordered.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering.Ordered { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the 4x4 ordered dithering matrix. /// diff --git a/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs b/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs index 917f57318..48d6c3f6a 100644 --- a/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs +++ b/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Dithering.Ordered { + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs index 0ce233739..71472c00f 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Formats.Jpg using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using ImageSharp.Memory; + /// /// Encapsulates the implementation of processing "raw" -s into Jpeg image channels. /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs index 920457a0c..342ce299c 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs @@ -6,6 +6,8 @@ namespace ImageSharp.Formats.Jpg { using System.Runtime.CompilerServices; + using ImageSharp.Memory; + /// /// Represents an area of a Jpeg subimage (channel) /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs index 2a9b0c6b2..7d2e6d441 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs @@ -9,6 +9,8 @@ namespace ImageSharp.Formats.Jpg using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using ImageSharp.Memory; + /// /// Encapsulates the impementation of Jpeg SOS Huffman decoding. See JpegScanDecoder.md! /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs index 86192318b..89e327f0d 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Formats.Jpg using System; using System.Buffers; + using ImageSharp.Memory; + /// /// Represents an image made up of three color components (luminance, blue chroma, red chroma) /// diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 92607883d..9d9ecacfe 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -11,6 +11,7 @@ namespace ImageSharp.Formats using System.Threading.Tasks; using ImageSharp.Formats.Jpg; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index d3b32d977..db2189b3c 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats { + using System; using System.Runtime.CompilerServices; /// @@ -21,7 +22,7 @@ namespace ImageSharp.Formats /// The previous scanline. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerPixel) + public static void Decode(Span scanline, Span previousScanline, int bytesPerPixel) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); @@ -55,7 +56,7 @@ namespace ImageSharp.Formats /// The filtered scanline result. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerPixel) + public static void Encode(Span scanline, Span previousScanline, Span result, int bytesPerPixel) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); diff --git a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs index 87d794902..ee77d1a5e 100644 --- a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Formats using System; using System.Runtime.CompilerServices; + using ImageSharp.Memory; + /// /// The None filter, the scanline is transmitted unmodified; it is only necessary to /// insert a filter type byte before the data. @@ -21,12 +23,12 @@ namespace ImageSharp.Formats /// The scanline to encode /// The filtered scanline result. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan result) + public static void Encode(Span scanline, Span result) { // Insert a byte before the data. result[0] = 0; result = result.Slice(1); - BufferSpan.Copy(scanline, result); + SpanHelper.Copy(scanline, result); } } } diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index 4226596f8..ec12eca05 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats { + using System; using System.Runtime.CompilerServices; /// @@ -22,7 +23,7 @@ namespace ImageSharp.Formats /// The previous scanline. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerPixel) + public static void Decode(Span scanline, Span previousScanline, int bytesPerPixel) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); @@ -57,7 +58,7 @@ namespace ImageSharp.Formats /// The filtered scanline result. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerPixel) + public static void Encode(Span scanline, Span previousScanline, Span result, int bytesPerPixel) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index d40f261ed..f81122ad2 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats { + using System; using System.Runtime.CompilerServices; /// @@ -20,7 +21,7 @@ namespace ImageSharp.Formats /// The scanline to decode /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, int bytesPerPixel) + public static void Decode(Span scanline, int bytesPerPixel) { ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); @@ -48,7 +49,7 @@ namespace ImageSharp.Formats /// The filtered scanline result. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan result, int bytesPerPixel) + public static void Encode(Span scanline, Span result, int bytesPerPixel) { DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index fa3ef57da..b89a3c5fc 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats { + using System; using System.Runtime.CompilerServices; /// @@ -20,7 +21,7 @@ namespace ImageSharp.Formats /// The scanline to decode /// The previous scanline. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, BufferSpan previousScanline) + public static void Decode(Span scanline, Span previousScanline) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); @@ -43,7 +44,7 @@ namespace ImageSharp.Formats /// The previous scanline. /// The filtered scanline result. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result) + public static void Encode(Span scanline, Span previousScanline, Span result) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 13b4c1167..f8be0f6cc 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -12,6 +12,7 @@ namespace ImageSharp.Formats using System.Linq; using System.Runtime.CompilerServices; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using static ComparableExtensions; @@ -503,8 +504,8 @@ namespace ImageSharp.Formats this.currentRowBytesRead = 0; - BufferSpan scanSpan = this.scanline.Slice(0, bytesPerInterlaceScanline); - BufferSpan prevSpan = this.previousScanline.Span.Slice(0, bytesPerInterlaceScanline); + Span scanSpan = this.scanline.Slice(0, bytesPerInterlaceScanline); + Span prevSpan = this.previousScanline.Span.Slice(0, bytesPerInterlaceScanline); var filterType = (FilterType)scanSpan[0]; switch (filterType) @@ -565,8 +566,8 @@ namespace ImageSharp.Formats where TPixel : struct, IPixel { var color = default(TPixel); - BufferSpan pixelBuffer = pixels.GetRowSpan(this.currentRow); - var scanlineBuffer = new BufferSpan(defilteredScanline, 1); + Span pixelBuffer = pixels.GetRowSpan(this.currentRow); + var scanlineBuffer = new Span(defilteredScanline, 1); switch (this.PngColorType) { diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 44f1513ee..49c18db34 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -10,6 +10,8 @@ namespace ImageSharp.Formats using System.IO; using System.Linq; using System.Runtime.CompilerServices; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Quantizers; @@ -340,15 +342,15 @@ namespace ImageSharp.Formats private void CollecTPixelBytes(PixelAccessor pixels, int row) where TPixel : struct, IPixel { - BufferSpan rowSpan = pixels.GetRowSpan(row); + Span rowSpan = pixels.GetRowSpan(row); if (this.bytesPerPixel == 4) { - PixelOperations.Instance.ToXyzwBytes(rowSpan, this.rawScanline, this.width); + PixelOperations.Instance.ToXyzwBytes(rowSpan, this.rawScanline, 0, this.width); } else { - PixelOperations.Instance.ToXyzBytes(rowSpan, this.rawScanline, this.width); + PixelOperations.Instance.ToXyzBytes(rowSpan, this.rawScanline, 0, this.width); } } @@ -387,8 +389,8 @@ namespace ImageSharp.Formats /// The private Buffer GetOptimalFilteredScanline() { - BufferSpan scanSpan = this.rawScanline.Span; - BufferSpan prevSpan = this.previousScanline.Span; + Span scanSpan = this.rawScanline.Span; + Span prevSpan = this.previousScanline.Span; // Palette images don't compress well with adaptive filtering. if (this.pngColorType == PngColorType.Palette || this.bitDepth < 8) @@ -442,7 +444,7 @@ namespace ImageSharp.Formats /// The last variation sum /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - private int CalculateTotalVariation(BufferSpan scanline, int lastSum) + private int CalculateTotalVariation(Span scanline, int lastSum) { ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); int sum = 0; diff --git a/src/ImageSharp/Image/ImageBase{TPixel}.cs b/src/ImageSharp/Image/ImageBase{TPixel}.cs index d5023c4ba..4fd9d26cb 100644 --- a/src/ImageSharp/Image/ImageBase{TPixel}.cs +++ b/src/ImageSharp/Image/ImageBase{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp { using System; using System.Diagnostics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processing; diff --git a/src/ImageSharp/Image/PixelAccessor{TPixel}.cs b/src/ImageSharp/Image/PixelAccessor{TPixel}.cs index 1e46a672e..f4c7c7642 100644 --- a/src/ImageSharp/Image/PixelAccessor{TPixel}.cs +++ b/src/ImageSharp/Image/PixelAccessor{TPixel}.cs @@ -9,6 +9,8 @@ namespace ImageSharp using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading.Tasks; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -114,7 +116,7 @@ namespace ImageSharp public ParallelOptions ParallelOptions { get; } /// - BufferSpan IBuffer2D.Span => this.pixelBuffer; + Span IBuffer2D.Span => this.pixelBuffer; private static PixelOperations Operations => PixelOperations.Instance; @@ -250,7 +252,7 @@ namespace ImageSharp /// The target pixel buffer accessor. internal void CopyTo(PixelAccessor target) { - BufferSpan.Copy(this.pixelBuffer.Span, target.pixelBuffer.Span); + SpanHelper.Copy(this.pixelBuffer.Span, target.pixelBuffer.Span); } /// @@ -266,8 +268,8 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = area.GetRowSpan(y); - BufferSpan destination = this.GetRowSpan(targetX, targetY + y); + Span source = area.GetRowSpan(y); + Span destination = this.GetRowSpan(targetX, targetY + y); Operations.PackFromZyxBytes(source, destination, width); } @@ -286,8 +288,8 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = area.GetRowSpan(y); - BufferSpan destination = this.GetRowSpan(targetX, targetY + y); + Span source = area.GetRowSpan(y); + Span destination = this.GetRowSpan(targetX, targetY + y); Operations.PackFromZyxwBytes(source, destination, width); } @@ -306,8 +308,8 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = area.GetRowSpan(y); - BufferSpan destination = this.GetRowSpan(targetX, targetY + y); + Span source = area.GetRowSpan(y); + Span destination = this.GetRowSpan(targetX, targetY + y); Operations.PackFromXyzBytes(source, destination, width); } @@ -326,8 +328,8 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = area.GetRowSpan(y); - BufferSpan destination = this.GetRowSpan(targetX, targetY + y); + Span source = area.GetRowSpan(y); + Span destination = this.GetRowSpan(targetX, targetY + y); Operations.PackFromXyzwBytes(source, destination, width); } } @@ -345,9 +347,11 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = this.GetRowSpan(sourceX, sourceY + y); - BufferSpan destination = area.GetRowSpan(y); - Operations.ToZyxBytes(source, destination, width); + Span source = this.GetRowSpan(sourceX, sourceY + y); + using (Buffer destination = new Buffer(area.Bytes)) + { + Operations.ToZyxBytes(source, destination, y * area.RowStride, width); + } } } @@ -364,9 +368,11 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = this.GetRowSpan(sourceX, sourceY + y); - BufferSpan destination = area.GetRowSpan(y); - Operations.ToZyxwBytes(source, destination, width); + Span source = this.GetRowSpan(sourceX, sourceY + y); + using (Buffer destination = new Buffer(area.Bytes)) + { + Operations.ToZyxwBytes(source, destination, y * area.RowStride, width); + } } } @@ -383,9 +389,11 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = this.GetRowSpan(sourceX, sourceY + y); - BufferSpan destination = area.GetRowSpan(y); - Operations.ToXyzBytes(source, destination, width); + Span source = this.GetRowSpan(sourceX, sourceY + y); + using (Buffer destination = new Buffer(area.Bytes)) + { + Operations.ToXyzBytes(source, destination, y * area.RowStride, width); + } } } @@ -402,9 +410,11 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = this.GetRowSpan(sourceX, sourceY + y); - BufferSpan destination = area.GetRowSpan(y); - Operations.ToXyzwBytes(source, destination, width); + Span source = this.GetRowSpan(sourceX, sourceY + y); + using (Buffer destination = new Buffer(area.Bytes)) + { + Operations.ToXyzwBytes(source, destination, y * area.RowStride, width); + } } } diff --git a/src/ImageSharp/Image/PixelArea{TPixel}.cs b/src/ImageSharp/Image/PixelArea{TPixel}.cs index 3dd187768..4ddfcadb7 100644 --- a/src/ImageSharp/Image/PixelArea{TPixel}.cs +++ b/src/ImageSharp/Image/PixelArea{TPixel}.cs @@ -8,6 +8,8 @@ namespace ImageSharp using System; using System.Diagnostics; using System.IO; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -191,11 +193,11 @@ namespace ImageSharp } /// - /// Gets a to the row y. + /// Gets a to the row y. /// /// The y coordinate - /// The - internal BufferSpan GetRowSpan(int y) + /// The + internal Span GetRowSpan(int y) { return this.byteBuffer.Slice(y * this.RowStride); } diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 0269e770f..6194be1bf 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -38,8 +38,9 @@ All + - + diff --git a/src/ImageSharp/Common/Memory/Buffer.cs b/src/ImageSharp/Memory/Buffer.cs similarity index 88% rename from src/ImageSharp/Common/Memory/Buffer.cs rename to src/ImageSharp/Memory/Buffer.cs index c26b8ea18..a894ea53a 100644 --- a/src/ImageSharp/Common/Memory/Buffer.cs +++ b/src/ImageSharp/Memory/Buffer.cs @@ -3,10 +3,9 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { using System; - using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -97,9 +96,9 @@ namespace ImageSharp public T[] Array { get; private set; } /// - /// Gets a to the backing buffer. + /// Gets a to the backing buffer. /// - public BufferSpan Span => this; + public Span Span => this; /// /// Returns a reference to specified element of the buffer. @@ -117,13 +116,13 @@ namespace ImageSharp } /// - /// Converts to an . + /// Converts to an . /// /// The to convert. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator BufferSpan(Buffer buffer) + public static implicit operator Span(Buffer buffer) { - return new BufferSpan(buffer.Array, 0, buffer.Length); + return new Span(buffer.Array, 0, buffer.Length); } /// @@ -140,26 +139,26 @@ namespace ImageSharp } /// - /// Gets a to an offseted position inside the buffer. + /// Gets a to an offseted position inside the buffer. /// /// The start - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan Slice(int start) + public Span Slice(int start) { - return new BufferSpan(this.Array, start, this.Length - start); + return new Span(this.Array, start, this.Length - start); } /// - /// Gets a to an offseted position inside the buffer. + /// Gets a to an offsetted position inside the buffer. /// /// The start /// The length of the slice - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan Slice(int start, int length) + public Span Slice(int start, int length) { - return new BufferSpan(this.Array, start, length); + return new Span(this.Array, start, length); } /// @@ -210,7 +209,7 @@ namespace ImageSharp } /// - /// Clears the buffer, filling elements between 0 and -1 with default(T) + /// Clears the contents of this buffer. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Clear() diff --git a/src/ImageSharp/Common/Memory/Buffer2D.cs b/src/ImageSharp/Memory/Buffer2D.cs similarity index 98% rename from src/ImageSharp/Common/Memory/Buffer2D.cs rename to src/ImageSharp/Memory/Buffer2D.cs index c4eb50700..e5ccfbd19 100644 --- a/src/ImageSharp/Common/Memory/Buffer2D.cs +++ b/src/ImageSharp/Memory/Buffer2D.cs @@ -3,9 +3,8 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { - using System; using System.Runtime.CompilerServices; /// diff --git a/src/ImageSharp/Common/Memory/Buffer2DExtensions.cs b/src/ImageSharp/Memory/Buffer2DExtensions.cs similarity index 71% rename from src/ImageSharp/Common/Memory/Buffer2DExtensions.cs rename to src/ImageSharp/Memory/Buffer2DExtensions.cs index 4c3cc4d40..51e558281 100644 --- a/src/ImageSharp/Common/Memory/Buffer2DExtensions.cs +++ b/src/ImageSharp/Memory/Buffer2DExtensions.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { using System; using System.Runtime.CompilerServices; @@ -14,29 +14,29 @@ namespace ImageSharp internal static class Buffer2DExtensions { /// - /// Gets a to the row 'y' beginning from the pixel at 'x'. + /// Gets a to the row 'y' beginning from the pixel at 'x'. /// /// The buffer /// The x coordinate (position in the row) /// The y (row) coordinate /// The element type - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static BufferSpan GetRowSpan(this IBuffer2D buffer, int x, int y) + 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); } /// - /// Gets a to the row 'y' beginning from the pixel at 'x'. + /// Gets a to the row 'y' beginning from the pixel at 'x'. /// /// The buffer /// The y (row) coordinate /// The element type - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static BufferSpan GetRowSpan(this IBuffer2D buffer, int y) + public static Span GetRowSpan(this IBuffer2D buffer, int y) where T : struct { return buffer.Span.Slice(y * buffer.Width, buffer.Width); diff --git a/src/ImageSharp/Common/Memory/Fast2DArray{T}.cs b/src/ImageSharp/Memory/Fast2DArray{T}.cs similarity index 96% rename from src/ImageSharp/Common/Memory/Fast2DArray{T}.cs rename to src/ImageSharp/Memory/Fast2DArray{T}.cs index 401c83ce6..260c829e2 100644 --- a/src/ImageSharp/Common/Memory/Fast2DArray{T}.cs +++ b/src/ImageSharp/Memory/Fast2DArray{T}.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { using System; using System.Diagnostics; @@ -94,11 +94,11 @@ namespace ImageSharp } /// - /// Performs an implicit conversion from a 2D array to a . + /// Performs an implicit conversion from a 2D array to a . /// /// The source array. /// - /// The represenation on the source data. + /// The represenation on the source data. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator Fast2DArray(T[,] data) diff --git a/src/ImageSharp/Common/Memory/IBuffer2D.cs b/src/ImageSharp/Memory/IBuffer2D.cs similarity index 84% rename from src/ImageSharp/Common/Memory/IBuffer2D.cs rename to src/ImageSharp/Memory/IBuffer2D.cs index 1ba08e49f..300c29a1b 100644 --- a/src/ImageSharp/Common/Memory/IBuffer2D.cs +++ b/src/ImageSharp/Memory/IBuffer2D.cs @@ -3,8 +3,10 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { + using System; + /// /// An interface that represents a pinned buffer of value type objects /// interpreted as a 2D region of x elements. @@ -24,8 +26,8 @@ namespace ImageSharp int Height { get; } /// - /// Gets a to the backing buffer. + /// Gets a to the backing buffer. /// - BufferSpan Span { get; } + Span Span { get; } } } \ No newline at end of file diff --git a/src/ImageSharp/Common/Memory/PixelDataPool{T}.cs b/src/ImageSharp/Memory/PixelDataPool{T}.cs similarity index 98% rename from src/ImageSharp/Common/Memory/PixelDataPool{T}.cs rename to src/ImageSharp/Memory/PixelDataPool{T}.cs index 0ec367d24..a8b5501cc 100644 --- a/src/ImageSharp/Common/Memory/PixelDataPool{T}.cs +++ b/src/ImageSharp/Memory/PixelDataPool{T}.cs @@ -3,9 +3,8 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { - using System; using System.Buffers; using ImageSharp.PixelFormats; diff --git a/src/ImageSharp/Common/Memory/BufferSpan.cs b/src/ImageSharp/Memory/SpanHelper.cs similarity index 82% rename from src/ImageSharp/Common/Memory/BufferSpan.cs rename to src/ImageSharp/Memory/SpanHelper.cs index f8a5453a8..57b771591 100644 --- a/src/ImageSharp/Common/Memory/BufferSpan.cs +++ b/src/ImageSharp/Memory/SpanHelper.cs @@ -1,19 +1,18 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { using System; using System.Numerics; using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; /// - /// Utility methods for + /// Utility methods for /// - internal static class BufferSpan + internal static class SpanHelper { /// /// Fetches a from the beginning of the span. @@ -22,7 +21,7 @@ namespace ImageSharp /// The span to fetch the vector from /// A reference to the beginning of the span [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref Vector FetchVector(this BufferSpan span) + public static ref Vector FetchVector(this Span span) where T : struct { return ref Unsafe.As>(ref span.DangerousGetPinnableReference()); @@ -32,11 +31,11 @@ namespace ImageSharp /// Copy 'count' number of elements of the same type from 'source' to 'dest' /// /// The element type. - /// The to copy elements from. - /// The destination . + /// The to copy elements from. + /// The destination . /// The number of elements to copy [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy(BufferSpan source, BufferSpan destination, int count) + public static unsafe void Copy(Span source, Span destination, int count) where T : struct { DebugGuard.MustBeLessThanOrEqualTo(count, source.Length, nameof(count)); @@ -48,6 +47,7 @@ namespace ImageSharp int byteCount = Unsafe.SizeOf() * count; // TODO: Use unfixed Unsafe.CopyBlock(ref T, ref T, int) for small blocks, when it gets available! + // This is now available. Check with Anton re intent. Do we replace both ifdefs? fixed (byte* pSrc = &srcRef) fixed (byte* pDest = &destRef) { @@ -64,10 +64,10 @@ namespace ImageSharp /// Copy all elements of 'source' into 'destination'. /// /// The element type. - /// The to copy elements from. - /// The destination . + /// The to copy elements from. + /// The destination . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Copy(BufferSpan source, BufferSpan destination) + public static void Copy(Span source, Span destination) where T : struct { Copy(source, destination, source.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultAddPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultAddPixelBlender{TPixel}.cs index ab3aee041..261a98674 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultAddPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultAddPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultDarkenPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultDarkenPixelBlender{TPixel}.cs index e0ff80b66..bca99e2f0 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultDarkenPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultDarkenPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultHardLightPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultHardLightPixelBlender{TPixel}.cs index cec0dc0db..646423cff 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultHardLightPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultHardLightPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultLightenPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultLightenPixelBlender{TPixel}.cs index 32cd20650..55ad81e7a 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultLightenPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultLightenPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultMultiplyPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultMultiplyPixelBlender{TPixel}.cs index 7e0137018..e21efaed0 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultMultiplyPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultMultiplyPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultNormalPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultNormalPixelBlender{TPixel}.cs index 47bb084ca..9d63d11e0 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultNormalPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultNormalPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultOverlayPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultOverlayPixelBlender{TPixel}.cs index fcb56e3dc..8172909ec 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultOverlayPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultOverlayPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultScreenPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultScreenPixelBlender{TPixel}.cs index df0de293c..8405c3946 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultScreenPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultScreenPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultSubstractPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultSubstractPixelBlender{TPixel}.cs index 415ac04b2..ab44cb760 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultSubstractPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultSubstractPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs index 23340a60a..1a1d1cd05 100644 --- a/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs @@ -5,6 +5,8 @@ namespace ImageSharp.PixelFormats { + using System; + /// /// Abstract base class for calling pixel composition functions /// @@ -34,6 +36,6 @@ namespace ImageSharp.PixelFormats /// A value between 0 and 1 indicating the weight of the second source vector. /// At amount = 0, "from" is returned, at amount = 1, "to" is returned. /// - public abstract void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount); + public abstract void Blend(Span destination, Span background, Span source, Span amount); } } diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs index 207040521..fc1817a89 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs @@ -5,9 +5,12 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; using System.Runtime.CompilerServices; + using ImageSharp.Memory; + /// /// A stateless class implementing Strategy Pattern for batched pixel-data conversion operations /// for pixel buffers of type . @@ -24,10 +27,10 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of /// - /// The to the source vectors. - /// The to the destination colors. + /// The to the source vectors. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromVector4(BufferSpan sourceVectors, BufferSpan destColors, int count) + internal virtual void PackFromVector4(Span sourceVectors, Span destColors, int count) { ref Vector4 sourceRef = ref sourceVectors.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -43,10 +46,10 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination vectors. + /// The to the source colors. + /// The to the destination vectors. /// The number of pixels to convert. - internal virtual void ToVector4(BufferSpan sourceColors, BufferSpan destVectors, int count) + internal virtual void ToVector4(Span sourceColors, Span destVectors, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); ref Vector4 destRef = ref destVectors.DangerousGetPinnableReference(); @@ -62,10 +65,10 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of that converts data in . /// - /// The to the source bytes. - /// The to the destination colors. + /// The to the source bytes. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromXyzBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal virtual void PackFromXyzBytes(Span sourceBytes, Span destColors, int count) { ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -85,10 +88,11 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination bytes. + /// The to the source colors. + /// The to the destination bytes. + /// The starting index of the . /// The number of pixels to convert. - internal virtual void ToXyzBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal virtual void ToXyzBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); byte[] dest = destBytes.Array; @@ -96,17 +100,17 @@ namespace ImageSharp.PixelFormats for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToXyzBytes(dest, destBytes.Start + (i * 3)); + sp.ToXyzBytes(dest, startIndex + (i * 3)); } } /// /// Bulk version of that converts data in . /// - /// The to the source bytes. - /// The to the destination colors. + /// The to the source bytes. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromXyzwBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal virtual void PackFromXyzwBytes(Span sourceBytes, Span destColors, int count) { ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -126,10 +130,11 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination bytes. + /// The to the source colors. + /// The to the destination bytes. + /// The starting index of the . /// The number of pixels to convert. - internal virtual void ToXyzwBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal virtual void ToXyzwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); byte[] dest = destBytes.Array; @@ -137,17 +142,17 @@ namespace ImageSharp.PixelFormats for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToXyzwBytes(dest, destBytes.Start + (i * 4)); + sp.ToXyzwBytes(dest, startIndex + (i * 4)); } } /// /// Bulk version of that converts data in . /// - /// The to the source bytes. - /// The to the destination colors. + /// The to the source bytes. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromZyxBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal virtual void PackFromZyxBytes(Span sourceBytes, Span destColors, int count) { ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -167,10 +172,11 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination bytes. + /// The to the source colors. + /// The to the destination bytes. + /// The starting index of the . /// The number of pixels to convert. - internal virtual void ToZyxBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal virtual void ToZyxBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); byte[] dest = destBytes.Array; @@ -178,17 +184,17 @@ namespace ImageSharp.PixelFormats for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToZyxBytes(dest, destBytes.Start + (i * 3)); + sp.ToZyxBytes(dest, startIndex + (i * 3)); } } /// /// Bulk version of that converts data in . /// - /// The to the source bytes. - /// The to the destination colors. + /// The to the source bytes. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromZyxwBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal virtual void PackFromZyxwBytes(Span sourceBytes, Span destColors, int count) { ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -208,10 +214,11 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination bytes. + /// The to the source colors. + /// The to the destination bytes. + /// The starting index of the . /// The number of pixels to convert. - internal virtual void ToZyxwBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal virtual void ToZyxwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); byte[] dest = destBytes.Array; @@ -219,7 +226,7 @@ namespace ImageSharp.PixelFormats for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToZyxwBytes(dest, destBytes.Start + (i * 4)); + sp.ToZyxwBytes(dest, startIndex + (i * 4)); } } } diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs index fd94a3592..c9dca89a3 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs @@ -10,6 +10,7 @@ namespace ImageSharp using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -26,8 +27,8 @@ namespace ImageSharp /// SIMD optimized bulk implementation of /// that works only with `count` divisible by . /// - /// The to the source colors. - /// The to the dstination vectors. + /// The to the source colors. + /// The to the dstination vectors. /// The number of pixels to convert. /// /// Implementation adapted from: @@ -39,7 +40,7 @@ namespace ImageSharp /// https://github.com/dotnet/corefx/issues/15957 /// /// - internal static void ToVector4SimdAligned(BufferSpan sourceColors, BufferSpan destVectors, int count) + internal static void ToVector4SimdAligned(Span sourceColors, Span destVectors, int count) { if (!Vector.IsHardwareAccelerated) { @@ -90,7 +91,7 @@ namespace ImageSharp } /// - internal override void ToVector4(BufferSpan sourceColors, BufferSpan destVectors, int count) + internal override void ToVector4(Span sourceColors, Span destVectors, int count) { if (count < 256 || !Vector.IsHardwareAccelerated) { @@ -117,7 +118,7 @@ namespace ImageSharp } /// - internal override void PackFromXyzBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal override void PackFromXyzBytes(Span sourceBytes, Span destColors, int count) { ref RGB24 sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference(); @@ -133,10 +134,10 @@ namespace ImageSharp } /// - internal override void ToXyzBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal override void ToXyzBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); - ref RGB24 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); + ref RGB24 destRef = ref Unsafe.As(ref new Span(destBytes.Array, startIndex).DangerousGetPinnableReference()); for (int i = 0; i < count; i++) { @@ -148,19 +149,19 @@ namespace ImageSharp } /// - internal override unsafe void PackFromXyzwBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal override unsafe void PackFromXyzwBytes(Span sourceBytes, Span destColors, int count) { - BufferSpan.Copy(sourceBytes, destColors.AsBytes(), count * sizeof(Rgba32)); + SpanHelper.Copy(sourceBytes, destColors.AsBytes(), count * sizeof(Rgba32)); } /// - internal override unsafe void ToXyzwBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal override unsafe void ToXyzwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { - BufferSpan.Copy(sourceColors.AsBytes(), destBytes, count * sizeof(Rgba32)); + SpanHelper.Copy(sourceColors.AsBytes(), new Span(destBytes.Array, startIndex), count * sizeof(Rgba32)); } /// - internal override void PackFromZyxBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal override void PackFromZyxBytes(Span sourceBytes, Span destColors, int count) { ref RGB24 sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference(); @@ -176,10 +177,10 @@ namespace ImageSharp } /// - internal override void ToZyxBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal override void ToZyxBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); - ref RGB24 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); + ref RGB24 destRef = ref Unsafe.As(ref new Span(destBytes.Array, startIndex).DangerousGetPinnableReference()); for (int i = 0; i < count; i++) { @@ -191,7 +192,7 @@ namespace ImageSharp } /// - internal override void PackFromZyxwBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal override void PackFromZyxwBytes(Span sourceBytes, Span destColors, int count) { ref RGBA32 sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference(); @@ -206,10 +207,10 @@ namespace ImageSharp } /// - internal override void ToZyxwBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal override void ToZyxwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); - ref RGBA32 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); + ref RGBA32 destRef = ref Unsafe.As(ref new Span(destBytes.Array, startIndex).DangerousGetPinnableReference()); for (int i = 0; i < count; i++) { diff --git a/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs b/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs index da0900c11..5c7ee17ca 100644 --- a/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs @@ -5,8 +5,11 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; + using ImageSharp.Memory; + /// /// Provides optimized overrides for bulk operations. /// @@ -18,9 +21,9 @@ namespace ImageSharp.PixelFormats internal class PixelOperations : PixelOperations { /// - internal override unsafe void ToVector4(BufferSpan sourceColors, BufferSpan destVectors, int count) + internal override unsafe void ToVector4(Span sourceColors, Span destVectors, int count) { - BufferSpan.Copy(sourceColors.AsBytes(), destVectors.AsBytes(), count * sizeof(Vector4)); + SpanHelper.Copy(sourceColors.AsBytes(), destVectors.AsBytes(), count * sizeof(Vector4)); } } } diff --git a/src/ImageSharp/Processing/Processors/Convolution/BoxBlurProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/BoxBlurProcessor.cs index 652442388..b97e07079 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/BoxBlurProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/BoxBlurProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs index e6a42cc0c..29086b53f 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs index 965a725a1..e39104793 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs index 475f14ccf..17d5e3243 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetector2DProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetector2DProcessor.cs index 457854a31..6f1057e00 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetector2DProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetector2DProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorCompassProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorCompassProcessor.cs index a4d1d5409..a03d12677 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorCompassProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorCompassProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorProcessor.cs index e7670dd1d..415b574b8 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs index d72816a76..b1361b514 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KirschProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KirschProcessor.cs index d882bdb16..af4700bb9 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KirschProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KirschProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian3X3Processor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian3X3Processor.cs index 39f64fb5a..5f58d56f8 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian3X3Processor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian3X3Processor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian5X5Processor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian5X5Processor.cs index c65cb5bd7..2e365374c 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian5X5Processor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian5X5Processor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/LaplacianOfGaussianProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/LaplacianOfGaussianProcessor.cs index 57ab4dce6..de2594653 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/LaplacianOfGaussianProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/LaplacianOfGaussianProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/PrewittProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/PrewittProcessor.cs index d1515dee8..1b2d5f50e 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/PrewittProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/PrewittProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs index bab9ff622..d1b9614b6 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobinsonProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobinsonProcessor.cs index 4afca0f01..bc687f674 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobinsonProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobinsonProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/ScharrProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/ScharrProcessor.cs index a583d3c0d..339b9741f 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/ScharrProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/ScharrProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/SobelProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/SobelProcessor.cs index 1c2a6a18f..b9060ecbc 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/SobelProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/SobelProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor.cs index 87de922a1..4cd49e149 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor.cs index 34d099033..5bb29a67e 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs b/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs index 511a810b2..cc95f15fc 100644 --- a/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -82,7 +83,7 @@ namespace ImageSharp.Processing.Processors { int offsetY = y - startY; - BufferSpan destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width); + Span destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width); // this switched color & destination in the 2nd and 3rd places because we are applying the target colour under the current one blender.Blend(destination, colors, destination, amount); diff --git a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs index 5b5d64a9c..9de91c47b 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -96,7 +97,7 @@ namespace ImageSharp.Processing.Processors amounts[i] = (this.options.BlendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1); } - BufferSpan destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); + Span destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); this.blender.Blend(destination, destination, rowColors, amounts); } diff --git a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs index d698b543c..be431b07d 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -104,7 +105,7 @@ namespace ImageSharp.Processing.Processors amounts[i] = (this.options.BlendPercentage * (.9F * (distance / maxDistance))).Clamp(0, 1); } - BufferSpan destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); + Span destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); this.blender.Blend(destination, destination, rowColors, amounts); } diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs index f9c78c12f..d49f37803 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs @@ -4,6 +4,8 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Runtime.CompilerServices; + using ImageSharp.Memory; + /// /// Conains the definition of and . /// @@ -12,7 +14,7 @@ namespace ImageSharp.Processing.Processors /// /// Points to a collection of of weights allocated in . /// - internal unsafe struct WeightsWindow + internal struct WeightsWindow { /// /// The local left index position @@ -22,9 +24,7 @@ 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; + public Span Span; /// /// Initializes a new instance of the struct. @@ -32,7 +32,7 @@ namespace ImageSharp.Processing.Processors /// The local left index /// The span [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal WeightsWindow(int left, BufferSpan span) + internal WeightsWindow(int left, Span span) { this.Left = left; this.Span = span; @@ -55,7 +55,7 @@ namespace ImageSharp.Processing.Processors /// The source row position. /// The weighted sum [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeWeightedRowSum(BufferSpan rowSpan, int sourceX) + public Vector4 ComputeWeightedRowSum(Span rowSpan, int sourceX) { ref float horizontalValues = ref this.Ptr; int left = this.Left; @@ -82,7 +82,7 @@ namespace ImageSharp.Processing.Processors /// The source row position. /// The weighted sum [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeExpandedWeightedRowSum(BufferSpan rowSpan, int sourceX) + public Vector4 ComputeExpandedWeightedRowSum(Span rowSpan, int sourceX) { ref float horizontalValues = ref this.Ptr; int left = this.Left; @@ -169,7 +169,7 @@ namespace ImageSharp.Processing.Processors /// The weights public WeightsWindow GetWeightsWindow(int destIdx, int leftIdx, int rightIdx) { - BufferSpan span = this.dataBuffer.GetRowSpan(destIdx).Slice(leftIdx, rightIdx - leftIdx + 1); + Span span = this.dataBuffer.GetRowSpan(destIdx).Slice(leftIdx, rightIdx - leftIdx + 1); return new WeightsWindow(leftIdx, span); } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index dde79a7e4..61a64f60f 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -121,7 +122,7 @@ namespace ImageSharp.Processing.Processors // TODO: Without Parallel.For() this buffer object could be reused: using (Buffer tempRowBuffer = new Buffer(sourcePixels.Width)) { - BufferSpan sourceRow = sourcePixels.GetRowSpan(y); + Span sourceRow = sourcePixels.GetRowSpan(y); PixelOperations.Instance.ToVector4( sourceRow, diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4ReferenceVsPointer.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4ReferenceVsPointer.cs index 65c4a235d..30d93e3d9 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4ReferenceVsPointer.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4ReferenceVsPointer.cs @@ -6,7 +6,7 @@ using BenchmarkDotNet.Attributes; using ImageSharp; - using ImageSharp.PixelFormats; + using ImageSharp.Memory; /// /// Compares two implementation candidates for general BulkPixelOperations.ToVector4(): diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs index d363769d0..501ae7949 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs @@ -3,6 +3,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk { using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; using ImageSharp.PixelFormats; public abstract class PackFromXyzw diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs index cd1797558..65a2988b9 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; using ImageSharp.PixelFormats; public abstract class ToVector4 diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs index 663f85fb7..af76434a5 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs @@ -3,6 +3,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk { using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; using ImageSharp.PixelFormats; public abstract class ToXyz @@ -45,13 +46,13 @@ namespace ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().ToXyzBytes(this.source, this.destination, this.Count); + new PixelOperations().ToXyzBytes(this.source, this.destination, 0, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.ToXyzBytes(this.source, this.destination, this.Count); + PixelOperations.Instance.ToXyzBytes(this.source, this.destination, 0, this.Count); } } diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs index 7ac621113..01e7ef371 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; namespace ImageSharp.Benchmarks.Color.Bulk { using BenchmarkDotNet.Attributes; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; public abstract class ToXyzw @@ -49,16 +51,16 @@ namespace ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().ToXyzwBytes(this.source, this.destination, this.Count); + new PixelOperations().ToXyzwBytes(this.source, this.destination, 0, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.ToXyzwBytes(this.source, this.destination, this.Count); + PixelOperations.Instance.ToXyzwBytes(this.source, this.destination, 0, this.Count); } } - + public class ToXyzw_Rgba32 : ToXyzw { } diff --git a/tests/ImageSharp.Benchmarks/General/Array2D.cs b/tests/ImageSharp.Benchmarks/General/Array2D.cs index fce92e9be..9d44fc93d 100644 --- a/tests/ImageSharp.Benchmarks/General/Array2D.cs +++ b/tests/ImageSharp.Benchmarks/General/Array2D.cs @@ -9,6 +9,8 @@ namespace ImageSharp.Benchmarks.General using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; + public class Array2D { private float[] flatArray; diff --git a/tests/ImageSharp.Benchmarks/General/ClearBuffer.cs b/tests/ImageSharp.Benchmarks/General/ClearBuffer.cs index 97deb72c5..fc1b46a91 100644 --- a/tests/ImageSharp.Benchmarks/General/ClearBuffer.cs +++ b/tests/ImageSharp.Benchmarks/General/ClearBuffer.cs @@ -3,11 +3,10 @@ namespace ImageSharp.Benchmarks.General { using System; using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using ImageSharp.Memory; public unsafe class ClearBuffer { diff --git a/tests/ImageSharp.Benchmarks/General/IterateArray.cs b/tests/ImageSharp.Benchmarks/General/IterateArray.cs index eeab6506a..b9e2f7acd 100644 --- a/tests/ImageSharp.Benchmarks/General/IterateArray.cs +++ b/tests/ImageSharp.Benchmarks/General/IterateArray.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Benchmarks.General using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; + public class IterateArray { // Usual pinned stuff diff --git a/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs b/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs index d9237b801..18c99a744 100644 --- a/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs +++ b/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs @@ -5,6 +5,8 @@ using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; + // Pixel indexing benchmarks compare different methods for getting/setting all pixel values in a subsegment of a single pixel row. public abstract unsafe class PixelIndexing { diff --git a/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs index e6d1a2fd0..018f11334 100644 --- a/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs +++ b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs @@ -1,9 +1,11 @@ namespace ImageSharp.Benchmarks.General.Vectorization { + using System; using System.Numerics; using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; /// /// This benchmark compares different methods for fetching memory data into @@ -88,11 +90,11 @@ namespace ImageSharp.Benchmarks.General.Vectorization } [Benchmark] - public void FetchWithBufferSpanUtility() + public void FetchWithSpanUtility() { Vector v = new Vector(this.testValue); - BufferSpan span = new BufferSpan(this.data); + Span span = new Span(this.data); ref Vector start = ref span.FetchVector(); diff --git a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs index 474788b35..1da69f1a8 100644 --- a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs +++ b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs @@ -5,16 +5,19 @@ namespace ImageSharp.Benchmarks { + using System; using BenchmarkDotNet.Attributes; using ImageSharp.PixelFormats; using CoreSize = ImageSharp.Size; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats.PixelBlenders; public class PorterDuffBulkVsPixel : BenchmarkBase { - private void BulkVectorConvert(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + private void BulkVectorConvert(Span destination, Span background, Span source, Span amount) where TPixel : struct, IPixel { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); @@ -23,9 +26,9 @@ namespace ImageSharp.Benchmarks using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); @@ -38,7 +41,7 @@ namespace ImageSharp.Benchmarks PixelOperations.Instance.PackFromVector4(destinationSpan, destination, destination.Length); } } - private void BulkPixelConvert(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + private void BulkPixelConvert(Span destination, Span background, Span source, Span amount) where TPixel : struct, IPixel { Guard.MustBeGreaterThanOrEqualTo(destination.Length, background.Length, nameof(destination)); @@ -66,7 +69,7 @@ namespace ImageSharp.Benchmarks { for (int y = 0; y < image.Height; y++) { - BufferSpan span = pixels.GetRowSpan(y); + Span span = pixels.GetRowSpan(y); BulkVectorConvert(span, span, span, amounts); } } @@ -89,7 +92,7 @@ namespace ImageSharp.Benchmarks { for (int y = 0; y < image.Height; y++) { - BufferSpan span = pixels.GetRowSpan(y); + Span span = pixels.GetRowSpan(y); BulkPixelConvert(span, span, span, amounts); } } diff --git a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs index 76a0bc23b..7608d3065 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs @@ -15,6 +15,8 @@ namespace ImageSharp.Benchmarks using System; using System.Threading.Tasks; + using ImageSharp.Memory; + public class Glow : BenchmarkBase { private GlowProcessor bulk; diff --git a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs index 60c0d31d2..d96be70f7 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs @@ -46,34 +46,34 @@ namespace ImageSharp.Benchmarks } } - [Benchmark(Description = "ImageSharp Vector Resize")] - public CoreSize ResizeCoreVector() - { - using (Image image = new Image(2000, 2000)) - { - image.Resize(400, 400); - return new CoreSize(image.Width, image.Height); - } - } + //[Benchmark(Description = "ImageSharp Vector Resize")] + //public CoreSize ResizeCoreVector() + //{ + // using (Image image = new Image(2000, 2000)) + // { + // image.Resize(400, 400); + // return new CoreSize(image.Width, image.Height); + // } + //} - [Benchmark(Description = "ImageSharp Compand Resize")] - public CoreSize ResizeCoreCompand() - { - using (Image image = new Image(2000, 2000)) - { - image.Resize(400, 400, true); - return new CoreSize(image.Width, image.Height); - } - } + //[Benchmark(Description = "ImageSharp Compand Resize")] + //public CoreSize ResizeCoreCompand() + //{ + // using (Image image = new Image(2000, 2000)) + // { + // image.Resize(400, 400, true); + // return new CoreSize(image.Width, image.Height); + // } + //} - [Benchmark(Description = "ImageSharp Vector Compand Resize")] - public CoreSize ResizeCoreVectorCompand() - { - using (Image image = new Image(2000, 2000)) - { - image.Resize(400, 400, true); - return new CoreSize(image.Width, image.Height); - } - } + //[Benchmark(Description = "ImageSharp Vector Compand Resize")] + //public CoreSize ResizeCoreVectorCompand() + //{ + // using (Image image = new Image(2000, 2000)) + // { + // image.Resize(400, 400, true); + // return new CoreSize(image.Width, image.Height); + // } + //} } } diff --git a/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs b/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs index 4deeb40e8..e6d23dfc5 100644 --- a/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Tests.Colors using System; using System.Numerics; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; @@ -180,7 +181,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToXyzBytes(s, d, count) + (s, d) => Operations.ToXyzBytes(s, d, 0, count) ); } @@ -221,7 +222,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToXyzwBytes(s, d, count) + (s, d) => Operations.ToXyzwBytes(s, d, 0, count) ); } @@ -262,7 +263,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToZyxBytes(s, d, count) + (s, d) => Operations.ToZyxBytes(s, d, 0, count) ); } @@ -303,7 +304,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToZyxwBytes(s, d, count) + (s, d) => Operations.ToZyxwBytes(s, d, 0, count) ); } @@ -316,8 +317,8 @@ namespace ImageSharp.Tests.Colors public Buffer ActualDestBuffer { get; } public Buffer ExpectedDestBuffer { get; } - public BufferSpan Source => this.SourceBuffer; - public BufferSpan ActualDest => this.ActualDestBuffer; + public Span Source => this.SourceBuffer; + public Span ActualDest => this.ActualDestBuffer; public TestBuffers(TSource[] source, TDest[] expectedDest) { @@ -366,13 +367,13 @@ namespace ImageSharp.Tests.Colors internal static void TestOperation( TSource[] source, TDest[] expected, - Action, BufferSpan> action) + Action, Buffer> action) where TSource : struct where TDest : struct { using (TestBuffers buffers = new TestBuffers(source, expected)) { - action(buffers.Source, buffers.ActualDest); + action(buffers.Source, buffers.ActualDestBuffer); buffers.Verify(); } } diff --git a/tests/ImageSharp.Tests/Common/Buffer2DTests.cs b/tests/ImageSharp.Tests/Common/Buffer2DTests.cs index ac92ab87b..5f44a132d 100644 --- a/tests/ImageSharp.Tests/Common/Buffer2DTests.cs +++ b/tests/ImageSharp.Tests/Common/Buffer2DTests.cs @@ -4,6 +4,8 @@ namespace ImageSharp.Tests.Common using System; using System.Runtime.CompilerServices; + using ImageSharp.Memory; + using Xunit; using static TestStructs; @@ -13,7 +15,7 @@ namespace ImageSharp.Tests.Common // ReSharper disable once ClassNeverInstantiated.Local private class Assert : Xunit.Assert { - public static void SpanPointsTo(BufferSpan span, Buffer buffer, int bufferOffset = 0) + public static void SpanPointsTo(Span span, Buffer buffer, int bufferOffset = 0) where T : struct { ref T actual = ref span.DangerousGetPinnableReference(); @@ -75,9 +77,9 @@ namespace ImageSharp.Tests.Common { using (Buffer2D buffer = new Buffer2D(width, height)) { - BufferSpan span = buffer.GetRowSpan(y); + Span span = buffer.GetRowSpan(y); - Assert.Equal(width * y, span.Start); + // Assert.Equal(width * y, span.Start); Assert.Equal(width, span.Length); Assert.SpanPointsTo(span, buffer, width * y); } @@ -91,9 +93,9 @@ namespace ImageSharp.Tests.Common { using (Buffer2D buffer = new Buffer2D(width, height)) { - BufferSpan span = buffer.GetRowSpan(x, y); + Span span = buffer.GetRowSpan(x, y); - Assert.Equal(width * y + x, span.Start); + // Assert.Equal(width * y + x, span.Start); Assert.Equal(width - x, span.Length); Assert.SpanPointsTo(span, buffer, width * y + x); } diff --git a/tests/ImageSharp.Tests/Common/BufferSpanTests.cs b/tests/ImageSharp.Tests/Common/BufferSpanTests.cs index e5fcbf9ff..af33a981b 100644 --- a/tests/ImageSharp.Tests/Common/BufferSpanTests.cs +++ b/tests/ImageSharp.Tests/Common/BufferSpanTests.cs @@ -7,13 +7,14 @@ namespace ImageSharp.Tests.Common using System.Numerics; using System.Runtime.CompilerServices; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; using static TestStructs; - public unsafe class BufferSpanTests + public unsafe class SpanTests { // ReSharper disable once ClassNeverInstantiated.Local private class Assert : Xunit.Assert @@ -31,7 +32,7 @@ namespace ImageSharp.Tests.Common { float[] stuff = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; - BufferSpan span = new BufferSpan(stuff); + Span span = new Span(stuff); ref Vector v = ref span.FetchVector(); @@ -48,10 +49,10 @@ namespace ImageSharp.Tests.Common using (Buffer colorBuf = new Buffer(fooz)) { - BufferSpan orig = colorBuf.Slice(1); - BufferSpan asBytes = orig.AsBytes(); + Span orig = colorBuf.Slice(1); + Span asBytes = orig.AsBytes(); - Assert.Equal(asBytes.Start, sizeof(Foo)); + // Assert.Equal(asBytes.Start, sizeof(Foo)); Assert.Equal(orig.Length * Unsafe.SizeOf(), asBytes.Length); Assert.SameRefs(ref orig.DangerousGetPinnableReference(), ref asBytes.DangerousGetPinnableReference()); } @@ -65,10 +66,10 @@ namespace ImageSharp.Tests.Common Foo[] array = Foo.CreateArray(3); // Act: - BufferSpan span = new BufferSpan(array); + Span span = new Span(array); // Assert: - Assert.Equal(array, span.Array); + Assert.Equal(array, span.ToArray()); Assert.Equal(3, span.Length); Assert.SameRefs(ref array[0], ref span.DangerousGetPinnableReference()); } @@ -80,11 +81,9 @@ namespace ImageSharp.Tests.Common int start = 2; // Act: - BufferSpan span = new BufferSpan(array, start); + Span span = new Span(array, start); // Assert: - Assert.Equal(array, span.Array); - Assert.Equal(start, span.Start); Assert.SameRefs(ref array[start], ref span.DangerousGetPinnableReference()); Assert.Equal(array.Length - start, span.Length); } @@ -96,11 +95,9 @@ namespace ImageSharp.Tests.Common int start = 2; int length = 3; // Act: - BufferSpan span = new BufferSpan(array, start, length); + Span span = new Span(array, start, length); // Assert: - Assert.Equal(array, span.Array); - Assert.Equal(start, span.Start); Assert.SameRefs(ref array[start], ref span.DangerousGetPinnableReference()); Assert.Equal(length, span.Length); } @@ -116,14 +113,12 @@ namespace ImageSharp.Tests.Common int start1 = 2; int totalOffset = start0 + start1; - BufferSpan span = new BufferSpan(array, start0); + Span span = new Span(array, start0); // Act: span = span.Slice(start1); // Assert: - Assert.Equal(array, span.Array); - Assert.Equal(totalOffset, span.Start); Assert.SameRefs(ref array[totalOffset], ref span.DangerousGetPinnableReference()); Assert.Equal(array.Length - totalOffset, span.Length); } @@ -137,37 +132,35 @@ namespace ImageSharp.Tests.Common int totalOffset = start0 + start1; int sliceLength = 3; - BufferSpan span = new BufferSpan(array, start0); + Span span = new Span(array, start0); // Act: span = span.Slice(start1, sliceLength); // Assert: - Assert.Equal(array, span.Array); - Assert.Equal(totalOffset, span.Start); Assert.SameRefs(ref array[totalOffset], ref span.DangerousGetPinnableReference()); Assert.Equal(sliceLength, span.Length); } } - [Theory] - [InlineData(4)] - [InlineData(1500)] - public void Clear(int count) - { - Foo[] array = Foo.CreateArray(count + 42); + //[Theory] + //[InlineData(4)] + //[InlineData(1500)] + //public void Clear(int count) + //{ + // Foo[] array = Foo.CreateArray(count + 42); - int offset = 2; - BufferSpan ap = new BufferSpan(array, offset); + // int offset = 2; + // Span ap = new Span(array, offset); - // Act: - ap.Clear(count); + // // Act: + // ap.Clear(count); - Assert.NotEqual(default(Foo), array[offset - 1]); - Assert.Equal(default(Foo), array[offset]); - Assert.Equal(default(Foo), array[offset + count - 1]); - Assert.NotEqual(default(Foo), array[offset + count]); - } + // Assert.NotEqual(default(Foo), array[offset - 1]); + // Assert.Equal(default(Foo), array[offset]); + // Assert.Equal(default(Foo), array[offset + count - 1]); + // Assert.NotEqual(default(Foo), array[offset + count]); + //} public class Indexer { @@ -187,7 +180,7 @@ namespace ImageSharp.Tests.Common public void Read(int length, int start, int index) { Foo[] a = Foo.CreateArray(length); - BufferSpan span = new BufferSpan(a, start); + Span span = new Span(a, start); Foo element = span[index]; @@ -199,7 +192,7 @@ namespace ImageSharp.Tests.Common public void Write(int length, int start, int index) { Foo[] a = Foo.CreateArray(length); - BufferSpan span = new BufferSpan(a, start); + Span span = new Span(a, start); span[index] = new Foo(666, 666); @@ -214,9 +207,9 @@ namespace ImageSharp.Tests.Common public void AsBytes_Read(int length, int start, int index, int byteOffset) { Foo[] a = Foo.CreateArray(length); - BufferSpan span = new BufferSpan(a, start); + Span span = new Span(a, start); - BufferSpan bytes = span.AsBytes(); + Span bytes = span.AsBytes(); byte actual = bytes[index * Unsafe.SizeOf() + byteOffset]; @@ -234,7 +227,7 @@ namespace ImageSharp.Tests.Common public void DangerousGetPinnableReference(int start, int length) { Foo[] a = Foo.CreateArray(length); - BufferSpan span = new BufferSpan(a, start); + Span span = new Span(a, start); ref Foo r = ref span.DangerousGetPinnableReference(); Assert.True(Unsafe.AreSame(ref a[start], ref r)); @@ -276,10 +269,10 @@ namespace ImageSharp.Tests.Common Foo[] source = Foo.CreateArray(count + 2); Foo[] dest = new Foo[count + 5]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, 1); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, 1); - BufferSpan.Copy(apSource, apDest, count - 1); + SpanHelper.Copy(apSource, apDest, count - 1); AssertNotDefault(source, 1); AssertNotDefault(dest, 1); @@ -299,10 +292,10 @@ namespace ImageSharp.Tests.Common AlignedFoo[] source = AlignedFoo.CreateArray(count + 2); AlignedFoo[] dest = new AlignedFoo[count + 5]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, 1); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, 1); - BufferSpan.Copy(apSource, apDest, count - 1); + SpanHelper.Copy(apSource, apDest, count - 1); AssertNotDefault(source, 1); AssertNotDefault(dest, 1); @@ -322,10 +315,10 @@ namespace ImageSharp.Tests.Common int[] source = CreateTestInts(count + 2); int[] dest = new int[count + 5]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, 1); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, 1); - BufferSpan.Copy(apSource, apDest, count - 1); + SpanHelper.Copy(apSource, apDest, count - 1); AssertNotDefault(source, 1); AssertNotDefault(dest, 1); @@ -346,10 +339,10 @@ namespace ImageSharp.Tests.Common Foo[] source = Foo.CreateArray(count + 2); byte[] dest = new byte[destCount + sizeof(Foo) * 2]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, sizeof(Foo)); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, sizeof(Foo)); - BufferSpan.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(Foo)); + SpanHelper.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(Foo)); AssertNotDefault(source, 1); @@ -369,10 +362,10 @@ namespace ImageSharp.Tests.Common AlignedFoo[] source = AlignedFoo.CreateArray(count + 2); byte[] dest = new byte[destCount + sizeof(AlignedFoo) * 2]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, sizeof(AlignedFoo)); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, sizeof(AlignedFoo)); - BufferSpan.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(AlignedFoo)); + SpanHelper.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(AlignedFoo)); AssertNotDefault(source, 1); @@ -392,10 +385,10 @@ namespace ImageSharp.Tests.Common int[] source = CreateTestInts(count + 2); byte[] dest = new byte[destCount + sizeof(int) + 1]; - BufferSpan apSource = new BufferSpan(source); - BufferSpan apDest = new BufferSpan(dest); + Span apSource = new Span(source); + Span apDest = new Span(dest); - BufferSpan.Copy(apSource.AsBytes(), apDest, count * sizeof(int)); + SpanHelper.Copy(apSource.AsBytes(), apDest, count * sizeof(int)); AssertNotDefault(source, 1); @@ -413,10 +406,10 @@ namespace ImageSharp.Tests.Common byte[] source = CreateTestBytes(srcCount); Foo[] dest = new Foo[count + 2]; - BufferSpan apSource = new BufferSpan(source); - BufferSpan apDest = new BufferSpan(dest); + Span apSource = new Span(source); + Span apDest = new Span(dest); - BufferSpan.Copy(apSource, apDest.AsBytes(), count * sizeof(Foo)); + SpanHelper.Copy(apSource, apDest.AsBytes(), count * sizeof(Foo)); AssertNotDefault(source, sizeof(Foo) + 1); AssertNotDefault(dest, 1); @@ -435,7 +428,7 @@ namespace ImageSharp.Tests.Common using (Buffer colorBuf = new Buffer(colors)) using (Buffer byteBuf = new Buffer(colors.Length * 4)) { - BufferSpan.Copy(colorBuf.Span.AsBytes(), byteBuf, colorBuf.Length * sizeof(Rgba32)); + SpanHelper.Copy(colorBuf.Span.AsBytes(), byteBuf, colorBuf.Length * sizeof(Rgba32)); byte[] a = byteBuf.Array; diff --git a/tests/ImageSharp.Tests/Common/BufferTests.cs b/tests/ImageSharp.Tests/Common/BufferTests.cs index 23205d012..010bf40b3 100644 --- a/tests/ImageSharp.Tests/Common/BufferTests.cs +++ b/tests/ImageSharp.Tests/Common/BufferTests.cs @@ -6,6 +6,8 @@ namespace ImageSharp.Tests.Common using System.Runtime.InteropServices; using System.Threading.Tasks; + using ImageSharp.Memory; + using Xunit; using static TestStructs; @@ -15,7 +17,7 @@ namespace ImageSharp.Tests.Common // ReSharper disable once ClassNeverInstantiated.Local private class Assert : Xunit.Assert { - public static void SpanPointsTo(BufferSpan span, Buffer buffer, int bufferOffset = 0) + public static void SpanPointsTo(Span span, Buffer buffer, int bufferOffset = 0) where T : struct { ref T actual = ref span.DangerousGetPinnableReference(); @@ -58,10 +60,8 @@ namespace ImageSharp.Tests.Common } } - [Theory] - [InlineData(42)] - [InlineData(1111)] - public void Clear(int count) + [Fact] + public void Clear() { Foo[] a = { new Foo() { A = 1, B = 2 }, new Foo() { A = 3, B = 4 } }; using (Buffer buffer = new Buffer(a)) @@ -144,10 +144,10 @@ namespace ImageSharp.Tests.Common { using (Buffer buffer = new Buffer(bufferLength)) { - BufferSpan span = buffer; + Span span = buffer; - Assert.Equal(buffer.Array, span.Array); - Assert.Equal(0, span.Start); + //Assert.Equal(buffer.Array, span.ToArray()); + //Assert.Equal(0, span.Start); Assert.SpanPointsTo(span, buffer); Assert.Equal(span.Length, bufferLength); } @@ -158,10 +158,10 @@ namespace ImageSharp.Tests.Common { using (Buffer buffer = new Buffer(42)) { - BufferSpan span = buffer.Span; + Span span = buffer.Span; - Assert.Equal(buffer.Array, span.Array); - Assert.Equal(0, span.Start); + // Assert.Equal(buffer.Array, span.ToArray()); + // Assert.Equal(0, span.Start); Assert.SpanPointsTo(span, buffer); Assert.Equal(span.Length, 42); } @@ -177,10 +177,8 @@ namespace ImageSharp.Tests.Common { using (Buffer buffer = new Buffer(bufferLength)) { - BufferSpan span = buffer.Slice(start); + Span span = buffer.Slice(start); - Assert.Equal(buffer.Array, span.Array); - Assert.Equal(start, span.Start); Assert.SpanPointsTo(span, buffer, start); Assert.Equal(span.Length, bufferLength - start); } @@ -193,10 +191,8 @@ namespace ImageSharp.Tests.Common { using (Buffer buffer = new Buffer(bufferLength)) { - BufferSpan span = buffer.Slice(start, spanLength); + Span span = buffer.Slice(start, spanLength); - Assert.Equal(buffer.Array, span.Array); - Assert.Equal(start, span.Start); Assert.SpanPointsTo(span, buffer, start); Assert.Equal(span.Length, spanLength); } diff --git a/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs b/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs index 7db7a4820..efdcaa848 100644 --- a/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs +++ b/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests.Common { using System; + using ImageSharp.Memory; + using Xunit; public class Fast2DArrayTests diff --git a/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs b/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs index e673b28f1..1291160b2 100644 --- a/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs +++ b/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Tests { using System.Linq; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; diff --git a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs index 254c54ba1..7bacebe42 100644 --- a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs @@ -10,6 +10,7 @@ namespace ImageSharp.Tests.Drawing using ImageSharp.Drawing; using ImageSharp.Drawing.Brushes; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; diff --git a/tests/ImageSharp.Tests/ImageComparer.cs b/tests/ImageSharp.Tests/ImageComparer.cs index 9b8a51fde..d339dc83d 100644 --- a/tests/ImageSharp.Tests/ImageComparer.cs +++ b/tests/ImageSharp.Tests/ImageComparer.cs @@ -2,6 +2,7 @@ { using System; using ImageSharp; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; diff --git a/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs b/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs index 5fa1fccbc..8932f1ffe 100644 --- a/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs +++ b/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs @@ -16,10 +16,10 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public class PorterDuffFunctionsTests_TPixel { - private static BufferSpan AsSpan(T value) + private static Span AsSpan(T value) where T : struct { - return new BufferSpan(new[] { value }); + return new Span(new[] { value }); } public static TheoryData NormalBlendFunctionData = new TheoryData() { @@ -50,7 +50,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void NormalBlendFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultNormalPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -89,7 +89,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void MultiplyFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultMultiplyPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -128,7 +128,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void AddFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultAddPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -167,7 +167,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void SubstractFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultSubstractPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -206,7 +206,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void ScreenFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultScreenPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -245,7 +245,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void DarkenFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultDarkenPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -284,7 +284,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void LightenFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultLightenPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -323,7 +323,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void OverlayFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultOverlayPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -362,7 +362,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void HardLightFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultHardLightPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs b/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs index 7e3d318c0..852bc587d 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs @@ -38,9 +38,9 @@ namespace ImageSharp.Tests.TestUtilities return pix; } - internal BufferSpan AsSpan() + internal Span AsSpan() { - return new BufferSpan(new[] { AsPixel() }); + return new Span(new[] { AsPixel() }); } public void Deserialize(IXunitSerializationInfo info)