diff --git a/src/ImageSharp.Drawing/Primitives/Region.cs b/src/ImageSharp.Drawing/Primitives/Region.cs
index c85e373fb3..27f039f122 100644
--- a/src/ImageSharp.Drawing/Primitives/Region.cs
+++ b/src/ImageSharp.Drawing/Primitives/Region.cs
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
+using System;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Primitives
@@ -19,7 +20,7 @@ public abstract class Region
/// Gets the bounding box that entirely surrounds this region.
///
///
- /// This should always contains all possible points returned from .
+ /// This should always contains all possible points returned from .
///
public abstract Rectangle Bounds { get; }
@@ -28,8 +29,8 @@ public abstract class Region
///
/// The position along the y axis to find intersections.
/// The buffer.
- /// The point in the buffer to start setting offset.
+ /// A instance in the context of the caller.
/// The number of intersections found.
- public abstract int Scan(float y, float[] buffer, int offset);
+ public abstract int Scan(float y, Span buffer, Configuration configuration);
}
}
\ No newline at end of file
diff --git a/src/ImageSharp.Drawing/Primitives/ShapeRegion.cs b/src/ImageSharp.Drawing/Primitives/ShapeRegion.cs
index cfd1945d08..9f5611dc04 100644
--- a/src/ImageSharp.Drawing/Primitives/ShapeRegion.cs
+++ b/src/ImageSharp.Drawing/Primitives/ShapeRegion.cs
@@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
+using SixLabors.Memory;
using SixLabors.Primitives;
using SixLabors.Shapes;
@@ -39,21 +40,23 @@ public ShapeRegion(IPath shape)
public override Rectangle Bounds { get; }
///
- public override int Scan(float y, float[] buffer, int offset)
+ public override int Scan(float y, Span buffer, Configuration configuration)
{
var start = new PointF(this.Bounds.Left - 1, y);
var end = new PointF(this.Bounds.Right + 1, y);
- // TODO: This is a temporary workaround because of the lack of Span API-s on IPath. We should use MemoryManager.Allocate() here!
- var innerBuffer = new PointF[buffer.Length];
- int count = this.Shape.FindIntersections(start, end, innerBuffer, 0);
-
- for (int i = 0; i < count; i++)
+ using (IBuffer tempBuffer = configuration.MemoryAllocator.Allocate(buffer.Length))
{
- buffer[i + offset] = innerBuffer[i].X;
- }
+ Span innerBuffer = tempBuffer.GetSpan();
+ int count = this.Shape.FindIntersections(start, end, innerBuffer);
- return count;
+ for (int i = 0; i < count; i++)
+ {
+ buffer[i] = innerBuffer[i].X;
+ }
+
+ return count;
+ }
}
}
}
\ No newline at end of file
diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/BrushApplicator.cs b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/BrushApplicator.cs
index c546663353..7672681dac 100644
--- a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/BrushApplicator.cs
+++ b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/BrushApplicator.cs
@@ -3,8 +3,8 @@
using System;
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
{
@@ -65,13 +65,13 @@ internal BrushApplicator(ImageFrame target, GraphicsOptions options)
/// scanlineBuffer will be > scanlineWidth but provide and offset in case we want to share a larger buffer across runs.
internal virtual void Apply(Span scanline, int x, int y)
{
- MemoryManager memoryManager = this.Target.MemoryManager;
+ MemoryAllocator memoryAllocator = this.Target.MemoryAllocator;
- using (IBuffer amountBuffer = memoryManager.Allocate(scanline.Length))
- using (IBuffer overlay = memoryManager.Allocate(scanline.Length))
+ using (IBuffer amountBuffer = memoryAllocator.Allocate(scanline.Length))
+ using (IBuffer overlay = memoryAllocator.Allocate(scanline.Length))
{
- Span amountSpan = amountBuffer.Span;
- Span overlaySpan = overlay.Span;
+ Span amountSpan = amountBuffer.GetSpan();
+ Span overlaySpan = overlay.GetSpan();
for (int i = 0; i < scanline.Length; i++)
{
@@ -88,7 +88,7 @@ internal virtual void Apply(Span scanline, int x, int y)
}
Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
- this.Blender.Blend(memoryManager, destinationRow, destinationRow, overlaySpan, amountSpan);
+ this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, overlaySpan, amountSpan);
}
}
}
diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/ImageBrush{TPixel}.cs b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/ImageBrush{TPixel}.cs
index c2e3a16eff..7798488566 100644
--- a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/ImageBrush{TPixel}.cs
+++ b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/ImageBrush{TPixel}.cs
@@ -3,8 +3,8 @@
using System;
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
@@ -118,11 +118,11 @@ public override void Dispose()
internal override void Apply(Span scanline, int x, int y)
{
// Create a span for colors
- using (IBuffer amountBuffer = this.Target.MemoryManager.Allocate(scanline.Length))
- using (IBuffer overlay = this.Target.MemoryManager.Allocate(scanline.Length))
+ using (IBuffer amountBuffer = this.Target.MemoryAllocator.Allocate(scanline.Length))
+ using (IBuffer overlay = this.Target.MemoryAllocator.Allocate(scanline.Length))
{
- Span amountSpan = amountBuffer.Span;
- Span overlaySpan = overlay.Span;
+ Span amountSpan = amountBuffer.GetSpan();
+ Span overlaySpan = overlay.GetSpan();
int sourceY = (y - this.offsetY) % this.yLength;
int offsetX = x - this.offsetX;
@@ -138,7 +138,7 @@ internal override void Apply(Span scanline, int x, int y)
}
Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
- this.Blender.Blend(this.source.MemoryManager, destinationRow, destinationRow, overlaySpan, amountSpan);
+ this.Blender.Blend(this.source.MemoryAllocator, destinationRow, destinationRow, overlaySpan, amountSpan);
}
}
}
diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/PatternBrush{TPixel}.cs b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/PatternBrush{TPixel}.cs
index 765f2a1326..21f2066fb4 100644
--- a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/PatternBrush{TPixel}.cs
+++ b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/PatternBrush{TPixel}.cs
@@ -4,9 +4,9 @@
using System;
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
@@ -151,13 +151,13 @@ public override void Dispose()
internal override void Apply(Span scanline, int x, int y)
{
int patternY = y % this.pattern.Rows;
- MemoryManager memoryManager = this.Target.MemoryManager;
+ MemoryAllocator memoryAllocator = this.Target.MemoryAllocator;
- using (IBuffer amountBuffer = memoryManager.Allocate(scanline.Length))
- using (IBuffer overlay = memoryManager.Allocate(scanline.Length))
+ using (IBuffer amountBuffer = memoryAllocator.Allocate(scanline.Length))
+ using (IBuffer overlay = memoryAllocator.Allocate(scanline.Length))
{
- Span amountSpan = amountBuffer.Span;
- Span overlaySpan = overlay.Span;
+ Span amountSpan = amountBuffer.GetSpan();
+ Span overlaySpan = overlay.GetSpan();
for (int i = 0; i < scanline.Length; i++)
{
@@ -168,7 +168,7 @@ internal override void Apply(Span scanline, int x, int y)
}
Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
- this.Blender.Blend(memoryManager, destinationRow, destinationRow, overlaySpan, amountSpan);
+ this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, overlaySpan, amountSpan);
}
}
}
diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/RecolorBrush{TPixel}.cs b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/RecolorBrush{TPixel}.cs
index 324c54e186..a7da2cc5b8 100644
--- a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/RecolorBrush{TPixel}.cs
+++ b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/RecolorBrush{TPixel}.cs
@@ -4,8 +4,8 @@
using System;
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
@@ -136,13 +136,13 @@ public override void Dispose()
///
internal override void Apply(Span scanline, int x, int y)
{
- MemoryManager memoryManager = this.Target.MemoryManager;
+ MemoryAllocator memoryAllocator = this.Target.MemoryAllocator;
- using (IBuffer amountBuffer = memoryManager.Allocate(scanline.Length))
- using (IBuffer overlay = memoryManager.Allocate(scanline.Length))
+ using (IBuffer amountBuffer = memoryAllocator.Allocate(scanline.Length))
+ using (IBuffer overlay = memoryAllocator.Allocate(scanline.Length))
{
- Span amountSpan = amountBuffer.Span;
- Span overlaySpan = overlay.Span;
+ Span amountSpan = amountBuffer.GetSpan();
+ Span overlaySpan = overlay.GetSpan();
for (int i = 0; i < scanline.Length; i++)
{
@@ -156,7 +156,7 @@ internal override void Apply(Span scanline, int x, int y)
}
Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
- this.Blender.Blend(memoryManager, destinationRow, destinationRow, overlaySpan, amountSpan);
+ this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, overlaySpan, amountSpan);
}
}
}
diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/SolidBrush{TPixel}.cs b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/SolidBrush{TPixel}.cs
index f2054ee0d7..791c307bfc 100644
--- a/src/ImageSharp.Drawing/Processing/Drawing/Brushes/SolidBrush{TPixel}.cs
+++ b/src/ImageSharp.Drawing/Processing/Drawing/Brushes/SolidBrush{TPixel}.cs
@@ -3,8 +3,8 @@
using System;
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Drawing.Brushes
@@ -58,8 +58,8 @@ private class SolidBrushApplicator : BrushApplicator
public SolidBrushApplicator(ImageFrame source, TPixel color, GraphicsOptions options)
: base(source, options)
{
- this.Colors = source.MemoryManager.Allocate(source.Width);
- this.Colors.Span.Fill(color);
+ this.Colors = source.MemoryAllocator.Allocate(source.Width);
+ this.Colors.GetSpan().Fill(color);
}
///
@@ -75,7 +75,7 @@ public SolidBrushApplicator(ImageFrame source, TPixel color, GraphicsOpt
///
/// The color
///
- internal override TPixel this[int x, int y] => this.Colors.Span[x];
+ internal override TPixel this[int x, int y] => this.Colors.GetSpan()[x];
///
public override void Dispose()
@@ -88,24 +88,24 @@ internal override void Apply(Span scanline, int x, int y)
{
Span destinationRow = this.Target.GetPixelRowSpan(y).Slice(x, scanline.Length);
- MemoryManager memoryManager = this.Target.MemoryManager;
+ MemoryAllocator memoryAllocator = this.Target.MemoryAllocator;
if (this.Options.BlendPercentage == 1f)
{
- this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, scanline);
+ this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, this.Colors.GetSpan(), scanline);
}
else
{
- using (IBuffer amountBuffer = memoryManager.Allocate(scanline.Length))
+ using (IBuffer amountBuffer = memoryAllocator.Allocate(scanline.Length))
{
- Span amountSpan = amountBuffer.Span;
+ Span amountSpan = amountBuffer.GetSpan();
for (int i = 0; i < scanline.Length; i++)
{
amountSpan[i] = scanline[i] * this.Options.BlendPercentage;
}
- this.Blender.Blend(memoryManager, destinationRow, destinationRow, this.Colors.Span, amountSpan);
+ this.Blender.Blend(memoryAllocator, destinationRow, destinationRow, this.Colors.GetSpan(), amountSpan);
}
}
}
diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Processors/DrawImageProcessor.cs b/src/ImageSharp.Drawing/Processing/Drawing/Processors/DrawImageProcessor.cs
index 7b6e36d9d1..506df3886c 100644
--- a/src/ImageSharp.Drawing/Processing/Drawing/Processors/DrawImageProcessor.cs
+++ b/src/ImageSharp.Drawing/Processing/Drawing/Processors/DrawImageProcessor.cs
@@ -4,9 +4,9 @@
using System;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Drawing.Processors
@@ -133,11 +133,11 @@ protected override void OnFrameApply(ImageFrame source, Rectangle source
int width = maxX - minX;
- MemoryManager memoryManager = this.Image.GetConfiguration().MemoryManager;
+ MemoryAllocator memoryAllocator = this.Image.GetConfiguration().MemoryAllocator;
- using (IBuffer amount = memoryManager.Allocate(width))
+ using (IBuffer amount = memoryAllocator.Allocate(width))
{
- amount.Span.Fill(this.Opacity);
+ amount.GetSpan().Fill(this.Opacity);
Parallel.For(
minY,
@@ -147,7 +147,7 @@ protected override void OnFrameApply(ImageFrame source, Rectangle source
{
Span background = source.GetPixelRowSpan(y).Slice(minX, width);
Span foreground = targetImage.GetPixelRowSpan(y - locationY).Slice(targetX, width);
- blender.Blend(memoryManager, background, background, foreground, amount.Span);
+ blender.Blend(memoryAllocator, background, background, foreground, amount.GetSpan());
});
}
}
diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillProcessor.cs b/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillProcessor.cs
index 645ff03537..4214041a79 100644
--- a/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillProcessor.cs
+++ b/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillProcessor.cs
@@ -4,10 +4,10 @@
using System;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Drawing.Brushes;
using SixLabors.ImageSharp.Processing.Processors;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Drawing.Processors
@@ -77,13 +77,13 @@ protected override void OnFrameApply(ImageFrame source, Rectangle source
startY = 0;
}
- using (IBuffer amount = source.MemoryManager.Allocate(width))
+ using (IBuffer amount = source.MemoryAllocator.Allocate(width))
using (BrushApplicator applicator = this.brush.CreateApplicator(
source,
sourceRectangle,
this.options))
{
- amount.Span.Fill(1f);
+ amount.GetSpan().Fill(1f);
Parallel.For(
minY,
@@ -94,7 +94,7 @@ protected override void OnFrameApply(ImageFrame source, Rectangle source
int offsetY = y - startY;
int offsetX = minX - startX;
- applicator.Apply(amount.Span, offsetX, offsetY);
+ applicator.Apply(amount.GetSpan(), offsetX, offsetY);
});
}
}
diff --git a/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs b/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs
index 95ac3fe298..c81f4028bf 100644
--- a/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs
+++ b/src/ImageSharp.Drawing/Processing/Drawing/Processors/FillRegionProcessor.cs
@@ -3,11 +3,11 @@
using System;
using System.Runtime.CompilerServices;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.ImageSharp.Processing.Drawing.Brushes;
using SixLabors.ImageSharp.Processing.Processors;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Drawing.Processors
@@ -96,36 +96,35 @@ protected override void OnFrameApply(ImageFrame source, Rectangle source
using (BrushApplicator applicator = this.Brush.CreateApplicator(source, rect, this.Options))
{
int scanlineWidth = maxX - minX;
- using (BasicArrayBuffer buffer = source.MemoryManager.AllocateFake(maxIntersections))
- using (BasicArrayBuffer scanline = source.MemoryManager.AllocateFake(scanlineWidth))
+ using (IBuffer bBuffer = source.MemoryAllocator.Allocate(maxIntersections))
+ using (IBuffer bScanline = source.MemoryAllocator.Allocate(scanlineWidth))
{
bool scanlineDirty = true;
float subpixelFraction = 1f / subpixelCount;
float subpixelFractionPoint = subpixelFraction / subpixelCount;
+
+ Span buffer = bBuffer.GetSpan();
+ Span scanline = bScanline.GetSpan();
+
for (int y = minY; y < maxY; y++)
{
if (scanlineDirty)
{
- // clear the buffer
- for (int x = 0; x < scanlineWidth; x++)
- {
- scanline[x] = 0;
- }
-
+ scanline.Clear();
scanlineDirty = false;
}
float yPlusOne = y + 1;
for (float subPixel = (float)y; subPixel < yPlusOne; subPixel += subpixelFraction)
{
- int pointsFound = region.Scan(subPixel + offset, buffer.Array, 0);
+ int pointsFound = region.Scan(subPixel + offset, buffer, configuration);
if (pointsFound == 0)
{
// nothing on this line skip
continue;
}
- QuickSort(new Span(buffer.Array, 0, pointsFound));
+ QuickSort(buffer.Slice(0, pointsFound));
for (int point = 0; point < pointsFound; point += 2)
{
@@ -181,7 +180,7 @@ protected override void OnFrameApply(ImageFrame source, Rectangle source
}
}
- applicator.Apply(scanline.Span, minX, y);
+ applicator.Apply(scanline, minX, y);
}
}
}
@@ -189,31 +188,45 @@ protected override void OnFrameApply(ImageFrame source, Rectangle source
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static void Swap(Span data, int left, int right)
+ private static void Swap(ref float left, ref float right)
{
- float tmp = data[left];
- data[left] = data[right];
- data[right] = tmp;
+ float tmp = left;
+ left = right;
+ right = tmp;
}
private static void QuickSort(Span data)
{
- QuickSort(data, 0, data.Length - 1);
+ if (data.Length < 2)
+ {
+ return;
+ }
+ else if (data.Length == 2)
+ {
+ if (data[0] > data[1])
+ {
+ Swap(ref data[0], ref data[1]);
+ }
+
+ return;
+ }
+
+ QuickSort(ref data[0], 0, data.Length - 1);
}
- private static void QuickSort(Span data, int lo, int hi)
+ private static void QuickSort(ref float data0, int lo, int hi)
{
if (lo < hi)
{
- int p = Partition(data, lo, hi);
- QuickSort(data, lo, p);
- QuickSort(data, p + 1, hi);
+ int p = Partition(ref data0, lo, hi);
+ QuickSort(ref data0, lo, p);
+ QuickSort(ref data0, p + 1, hi);
}
}
- private static int Partition(Span data, int lo, int hi)
+ private static int Partition(ref float data0, int lo, int hi)
{
- float pivot = data[lo];
+ float pivot = Unsafe.Add(ref data0, lo);
int i = lo - 1;
int j = hi + 1;
while (true)
@@ -222,20 +235,20 @@ private static int Partition(Span data, int lo, int hi)
{
i = i + 1;
}
- while (data[i] < pivot && i < hi);
+ while (Unsafe.Add(ref data0, i) < pivot && i < hi);
do
{
j = j - 1;
}
- while (data[j] > pivot && j > lo);
+ while (Unsafe.Add(ref data0, j) > pivot && j > lo);
if (i >= j)
{
return j;
}
- Swap(data, i, j);
+ Swap(ref Unsafe.Add(ref data0, i), ref Unsafe.Add(ref data0, j));
}
}
}
diff --git a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs
index cbd8db748f..18b1d994b3 100644
--- a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs
+++ b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs
@@ -3,8 +3,8 @@
using System;
using System.Runtime.InteropServices;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp.Advanced
{
@@ -23,6 +23,52 @@ public static Configuration GetConfiguration(this Image source)
where TPixel : struct, IPixel
=> GetConfiguration((IConfigurable)source);
+ ///
+ /// Gets the representation of the pixels as a of contiguous memory in the source image's pixel format
+ /// stored in row major order.
+ ///
+ /// The type of the pixel.
+ /// The source.
+ /// The
+ public static Span GetPixelSpan(this ImageFrame source)
+ where TPixel : struct, IPixel
+ => source.GetPixelMemory().Span;
+
+ ///
+ /// Gets the representation of the pixels as a of contiguous memory in the source image's pixel format
+ /// stored in row major order.
+ ///
+ /// The type of the pixel.
+ /// The source.
+ /// The
+ public static Span GetPixelSpan(this Image source)
+ where TPixel : struct, IPixel
+ => source.Frames.RootFrame.GetPixelSpan();
+
+ ///
+ /// Gets the representation of the pixels as a of contiguous memory
+ /// at row beginning from the the first pixel on that row.
+ ///
+ /// The type of the pixel.
+ /// The source.
+ /// The row.
+ /// The
+ public static Span GetPixelRowSpan(this ImageFrame source, int rowIndex)
+ where TPixel : struct, IPixel
+ => source.PixelBuffer.GetRowSpan(rowIndex);
+
+ ///
+ /// Gets the representation of the pixels as of of contiguous memory
+ /// at row beginning from the the first pixel on that row.
+ ///
+ /// The type of the pixel.
+ /// The source.
+ /// The row.
+ /// The
+ public static Span GetPixelRowSpan(this Image source, int rowIndex)
+ where TPixel : struct, IPixel
+ => source.Frames.RootFrame.GetPixelRowSpan(rowIndex);
+
///
/// Returns a reference to the 0th element of the Pixel buffer,
/// allowing direct manipulation of pixel data through unsafe operations.
@@ -31,9 +77,10 @@ public static Configuration GetConfiguration(this Image source)
/// The Pixel format.
/// The source image frame
/// A pinnable reference the first root of the pixel buffer.
+ [Obsolete("This method will be removed in our next release! Please use MemoryMarshal.GetReference(source.GetPixelSpan())!")]
public static ref TPixel DangerousGetPinnableReferenceToPixelBuffer(this ImageFrame source)
where TPixel : struct, IPixel
- => ref DangerousGetPinnableReferenceToPixelBuffer((IPixelSource)source);
+ => ref DangerousGetPinnableReferenceToPixelBuffer((IPixelSource)source);
///
/// Returns a reference to the 0th element of the Pixel buffer,
@@ -43,59 +90,68 @@ public static ref TPixel DangerousGetPinnableReferenceToPixelBuffer(this
/// The Pixel format.
/// The source image
/// A pinnable reference the first root of the pixel buffer.
+ [Obsolete("This method will be removed in our next release! Please use MemoryMarshal.GetReference(source.GetPixelSpan())!")]
public static ref TPixel DangerousGetPinnableReferenceToPixelBuffer(this Image source)
where TPixel : struct, IPixel
- => ref source.Frames.RootFrame.DangerousGetPinnableReferenceToPixelBuffer();
+ => ref source.Frames.RootFrame.DangerousGetPinnableReferenceToPixelBuffer();
///
- /// Gets the representation of the pixels as an area of contiguous memory in the given pixel format.
+ /// Gets the representation of the pixels as a of contiguous memory in the source image's pixel format
+ /// stored in row major order.
///
- /// The type of the pixel.
- /// The source.
- /// The
- internal static Span GetPixelSpan(this ImageFrame source)
+ /// The Pixel format.
+ /// The source
+ /// The
+ internal static Memory GetPixelMemory(this ImageFrame source)
where TPixel : struct, IPixel
- => GetSpan(source);
+ {
+ return source.PixelBuffer.Buffer.Memory;
+ }
///
- /// Gets the representation of the pixels as an area of contiguous memory at row 'y' beginning from the the first pixel on that row.
+ /// Gets the representation of the pixels as a of contiguous memory in the source image's pixel format
+ /// stored in row major order.
///
- /// The type of the pixel.
- /// The source.
- /// The row.
- /// The
- internal static Span GetPixelRowSpan(this ImageFrame source, int row)
+ /// The Pixel format.
+ /// The source
+ /// The
+ internal static Memory GetPixelMemory(this Image source)
where TPixel : struct, IPixel
- => GetSpan(source, row);
+ {
+ return source.Frames.RootFrame.GetPixelMemory();
+ }
///
- /// Gets the representation of the pixels as an area of contiguous memory in the given pixel format.
+ /// Gets the representation of the pixels as a of contiguous memory
+ /// at row beginning from the the first pixel on that row.
///
/// The type of the pixel.
/// The source.
+ /// The row.
/// The
- internal static Span GetPixelSpan(this Image source)
+ internal static Memory GetPixelRowMemory(this ImageFrame source, int rowIndex)
where TPixel : struct, IPixel
- => source.Frames.RootFrame.GetPixelSpan();
+ => source.PixelBuffer.GetRowMemory(rowIndex);
///
- /// Gets the representation of the pixels as an area of contiguous memory at row 'y' beginning from the the first pixel on that row.
+ /// Gets the representation of the pixels as of of contiguous memory
+ /// at row beginning from the the first pixel on that row.
///
/// The type of the pixel.
/// The source.
- /// The row.
+ /// The row.
/// The
- internal static Span GetPixelRowSpan(this Image source, int row)
+ internal static Memory GetPixelRowMemory(this Image source, int rowIndex)
where TPixel : struct, IPixel
- => source.Frames.RootFrame.GetPixelRowSpan(row);
+ => source.Frames.RootFrame.GetPixelRowMemory(rowIndex);
///
- /// Gets the assigned to 'source'.
+ /// Gets the assigned to 'source'.
///
/// The source image
/// Returns the configuration.
- internal static MemoryManager GetMemoryManager(this IConfigurable source)
- => GetConfiguration(source).MemoryManager;
+ internal static MemoryAllocator GetMemoryAllocator(this IConfigurable source)
+ => GetConfiguration(source).MemoryAllocator;
///
/// Gets the span to the backing buffer.
@@ -105,7 +161,7 @@ internal static MemoryManager GetMemoryManager(this IConfigurable source)
/// The span retuned from Pixel source
private static Span GetSpan(IPixelSource source)
where TPixel : struct, IPixel
- => source.PixelBuffer.Span;
+ => source.PixelBuffer.GetSpan();
///
/// Gets the span to the backing buffer at the given row.
@@ -131,7 +187,7 @@ private static Span GetSpan(IPixelSource source, int row
///
private static Span GetSpan(Buffer2D source, int row)
where TPixel : struct, IPixel
- => source.Span.Slice(row * source.Width, source.Width);
+ => source.GetSpan().Slice(row * source.Width, source.Width);
///
/// Gets the configuration.
@@ -149,6 +205,6 @@ private static Configuration GetConfiguration(IConfigurable source)
/// A reference to the element.
private static ref TPixel DangerousGetPinnableReferenceToPixelBuffer(IPixelSource source)
where TPixel : struct, IPixel
- => ref MemoryMarshal.GetReference(source.PixelBuffer.Span);
+ => ref MemoryMarshal.GetReference(source.PixelBuffer.GetSpan());
}
}
diff --git a/src/ImageSharp/Advanced/IPixelSource.cs b/src/ImageSharp/Advanced/IPixelSource.cs
index a321e877ba..27b3170e63 100644
--- a/src/ImageSharp/Advanced/IPixelSource.cs
+++ b/src/ImageSharp/Advanced/IPixelSource.cs
@@ -1,8 +1,8 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp.Advanced
{
diff --git a/src/ImageSharp/Common/Helpers/ParallelFor.cs b/src/ImageSharp/Common/Helpers/ParallelFor.cs
index da91259051..7061475a7c 100644
--- a/src/ImageSharp/Common/Helpers/ParallelFor.cs
+++ b/src/ImageSharp/Common/Helpers/ParallelFor.cs
@@ -1,6 +1,6 @@
using System;
using System.Threading.Tasks;
-using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp
{
@@ -24,7 +24,7 @@ public static void WithConfiguration(int fromInclusive, int toExclusive, Configu
/// The value type of the buffer
/// The start index, inclusive.
/// The end index, exclusive.
- /// The used for getting the and
+ /// The used for getting the and
/// The length of the requested parallel buffer
/// The delegate that is invoked once per iteration.
public static void WithTemporaryBuffer(
@@ -35,12 +35,12 @@ public static void WithTemporaryBuffer(
Action> body)
where T : struct
{
- MemoryManager memoryManager = configuration.MemoryManager;
+ MemoryAllocator memoryAllocator = configuration.MemoryAllocator;
ParallelOptions parallelOptions = configuration.ParallelOptions;
IBuffer InitBuffer()
{
- return memoryManager.Allocate(bufferLength);
+ return memoryAllocator.Allocate(bufferLength);
}
void CleanUpBuffer(IBuffer buffer)
diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs
index eb08bc579c..e84674355e 100644
--- a/src/ImageSharp/Configuration.cs
+++ b/src/ImageSharp/Configuration.cs
@@ -12,8 +12,8 @@
#if !NETSTANDARD1_1
using SixLabors.ImageSharp.IO;
#endif
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Processing;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp
{
@@ -75,9 +75,9 @@ public Configuration(params IConfigurationModule[] configurationModules)
public ImageFormatManager ImageFormatsManager { get; set; } = new ImageFormatManager();
///
- /// Gets or sets the that is currently in use.
+ /// Gets or sets the that is currently in use.
///
- public MemoryManager MemoryManager { get; set; } = ArrayPoolMemoryManager.CreateDefault();
+ public MemoryAllocator MemoryAllocator { get; set; } = ArrayPoolMemoryAllocator.CreateDefault();
///
/// Gets the maximum header size of all the formats.
@@ -116,7 +116,7 @@ public Configuration ShallowCopy()
{
ParallelOptions = this.ParallelOptions,
ImageFormatsManager = this.ImageFormatsManager,
- MemoryManager = this.MemoryManager,
+ MemoryAllocator = this.MemoryAllocator,
ImageOperationsProvider = this.ImageOperationsProvider,
ReadOrigin = this.ReadOrigin,
diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
index 26bd97b810..3c6e05a8b7 100644
--- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
+++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
@@ -4,9 +4,9 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Bmp
{
@@ -71,7 +71,7 @@ internal sealed class BmpDecoderCore
private readonly Configuration configuration;
- private readonly MemoryManager memoryManager;
+ private readonly MemoryAllocator memoryAllocator;
///
/// Initializes a new instance of the class.
@@ -81,7 +81,7 @@ internal sealed class BmpDecoderCore
public BmpDecoderCore(Configuration configuration, IBmpDecoderOptions options)
{
this.configuration = configuration;
- this.memoryManager = configuration.MemoryManager;
+ this.memoryAllocator = configuration.MemoryAllocator;
}
///
@@ -213,9 +213,9 @@ private void ReadRle8(PixelAccessor pixels, byte[] colors, int w
var color = default(TPixel);
var rgba = new Rgba32(0, 0, 0, 255);
- using (Buffer2D buffer = this.memoryManager.AllocateClean2D(width, height))
+ using (Buffer2D buffer = this.memoryAllocator.AllocateClean2D(width, height))
{
- this.UncompressRle8(width, buffer.Span);
+ this.UncompressRle8(width, buffer.GetSpan());
for (int y = 0; y < height; y++)
{
@@ -337,12 +337,12 @@ private void ReadRgbPalette(PixelAccessor pixels, byte[] colors,
padding = 4 - padding;
}
- using (IManagedByteBuffer row = this.memoryManager.AllocateCleanManagedByteBuffer(arrayWidth + padding))
+ using (IManagedByteBuffer row = this.memoryAllocator.AllocateCleanManagedByteBuffer(arrayWidth + padding))
{
TPixel color = default;
var rgba = new Rgba32(0, 0, 0, 255);
- Span rowSpan = row.Span;
+ Span rowSpan = row.GetSpan();
for (int y = 0; y < height; y++)
{
@@ -389,7 +389,7 @@ private void ReadRgb16(PixelAccessor pixels, int width, int heig
var color = default(TPixel);
var rgba = new Rgba32(0, 0, 0, 255);
- using (IManagedByteBuffer buffer = this.memoryManager.AllocateManagedByteBuffer(stride))
+ using (IManagedByteBuffer buffer = this.memoryAllocator.AllocateManagedByteBuffer(stride))
{
for (int y = 0; y < height; y++)
{
@@ -427,14 +427,14 @@ private void ReadRgb24(PixelAccessor pixels, int width, int heig
{
int padding = CalculatePadding(width, 3);
- using (IManagedByteBuffer row = this.memoryManager.AllocatePaddedPixelRowBuffer(width, 3, padding))
+ using (IManagedByteBuffer row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 3, padding))
{
for (int y = 0; y < height; y++)
{
this.stream.Read(row);
int newY = Invert(y, height, inverted);
Span pixelSpan = pixels.GetRowSpan(newY);
- PixelOperations.Instance.PackFromBgr24Bytes(row.Span, pixelSpan, width);
+ PixelOperations.Instance.PackFromBgr24Bytes(row.GetSpan(), pixelSpan, width);
}
}
}
@@ -452,14 +452,14 @@ private void ReadRgb32(PixelAccessor pixels, int width, int heig
{
int padding = CalculatePadding(width, 4);
- using (IManagedByteBuffer row = this.memoryManager.AllocatePaddedPixelRowBuffer(width, 4, padding))
+ using (IManagedByteBuffer row = this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, 4, padding))
{
for (int y = 0; y < height; y++)
{
this.stream.Read(row);
int newY = Invert(y, height, inverted);
Span pixelSpan = pixels.GetRowSpan(newY);
- PixelOperations.Instance.PackFromBgra32Bytes(row.Span, pixelSpan, width);
+ PixelOperations.Instance.PackFromBgra32Bytes(row.GetSpan(), pixelSpan, width);
}
}
}
diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoder.cs b/src/ImageSharp/Formats/Bmp/BmpEncoder.cs
index 9edd0fcd4e..23b01ae9e8 100644
--- a/src/ImageSharp/Formats/Bmp/BmpEncoder.cs
+++ b/src/ImageSharp/Formats/Bmp/BmpEncoder.cs
@@ -22,7 +22,7 @@ public sealed class BmpEncoder : IImageEncoder, IBmpEncoderOptions
public void Encode(Image image, Stream stream)
where TPixel : struct, IPixel
{
- var encoder = new BmpEncoderCore(this, image.GetMemoryManager());
+ var encoder = new BmpEncoderCore(this, image.GetMemoryAllocator());
encoder.Encode(image, stream);
}
}
diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
index 2b0c907338..d8bf90c7c0 100644
--- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
+++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
@@ -3,8 +3,8 @@
using System;
using System.IO;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Bmp
{
@@ -20,16 +20,16 @@ internal sealed class BmpEncoderCore
private readonly BmpBitsPerPixel bitsPerPixel;
- private readonly MemoryManager memoryManager;
+ private readonly MemoryAllocator memoryAllocator;
///
/// Initializes a new instance of the class.
///
/// The encoder options
- /// The memory manager
- public BmpEncoderCore(IBmpEncoderOptions options, MemoryManager memoryManager)
+ /// The memory manager
+ public BmpEncoderCore(IBmpEncoderOptions options, MemoryAllocator memoryAllocator)
{
- this.memoryManager = memoryManager;
+ this.memoryAllocator = memoryAllocator;
this.bitsPerPixel = options.BitsPerPixel;
}
@@ -109,7 +109,7 @@ private void WriteImage(Stream stream, ImageFrame image)
private IManagedByteBuffer AllocateRow(int width, int bytesPerPixel)
{
- return this.memoryManager.AllocatePaddedPixelRowBuffer(width, bytesPerPixel, this.padding);
+ return this.memoryAllocator.AllocatePaddedPixelRowBuffer(width, bytesPerPixel, this.padding);
}
///
@@ -126,7 +126,7 @@ private void Write32Bit(Stream stream, PixelAccessor pixels)
for (int y = pixels.Height - 1; y >= 0; y--)
{
Span pixelSpan = pixels.GetRowSpan(y);
- PixelOperations.Instance.ToBgra32Bytes(pixelSpan, row.Span, pixelSpan.Length);
+ PixelOperations.Instance.ToBgra32Bytes(pixelSpan, row.GetSpan(), pixelSpan.Length);
stream.Write(row.Array, 0, row.Length());
}
}
@@ -146,7 +146,7 @@ private void Write24Bit(Stream stream, PixelAccessor pixels)
for (int y = pixels.Height - 1; y >= 0; y--)
{
Span pixelSpan = pixels.GetRowSpan(y);
- PixelOperations.Instance.ToBgr24Bytes(pixelSpan, row.Span, pixelSpan.Length);
+ PixelOperations.Instance.ToBgr24Bytes(pixelSpan, row.GetSpan(), pixelSpan.Length);
stream.Write(row.Array, 0, row.Length());
}
}
diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs
index 4fbd4baf51..fc73f55a1e 100644
--- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs
+++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs
@@ -7,9 +7,9 @@
using System.Runtime.InteropServices;
using System.Text;
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Formats.Gif
@@ -87,7 +87,7 @@ public GifDecoderCore(Configuration configuration, IGifDecoderOptions options)
///
public FrameDecodingMode DecodingMode { get; }
- private MemoryManager MemoryManager => this.configuration.MemoryManager;
+ private MemoryAllocator MemoryAllocator => this.configuration.MemoryAllocator;
///
/// Decodes the stream to the image.
@@ -293,7 +293,7 @@ private void ReadComments()
continue;
}
- using (IManagedByteBuffer commentsBuffer = this.MemoryManager.AllocateManagedByteBuffer(length))
+ using (IManagedByteBuffer commentsBuffer = this.MemoryAllocator.AllocateManagedByteBuffer(length))
{
this.stream.Read(commentsBuffer.Array, 0, length);
string comments = this.TextEncoding.GetString(commentsBuffer.Array, 0, length);
@@ -321,15 +321,15 @@ private void ReadFrame(ref Image image, ref ImageFrame p
if (imageDescriptor.LocalColorTableFlag)
{
int length = imageDescriptor.LocalColorTableSize * 3;
- localColorTable = this.configuration.MemoryManager.AllocateManagedByteBuffer(length, true);
+ localColorTable = this.configuration.MemoryAllocator.AllocateManagedByteBuffer(length, true);
this.stream.Read(localColorTable.Array, 0, length);
}
- indices = this.configuration.MemoryManager.AllocateManagedByteBuffer(imageDescriptor.Width * imageDescriptor.Height, true);
+ indices = this.configuration.MemoryAllocator.AllocateManagedByteBuffer(imageDescriptor.Width * imageDescriptor.Height, true);
- this.ReadFrameIndices(imageDescriptor, indices.Span);
- ReadOnlySpan colorTable = MemoryMarshal.Cast((localColorTable ?? this.globalColorTable).Span);
- this.ReadFrameColors(ref image, ref previousFrame, indices.Span, colorTable, imageDescriptor);
+ this.ReadFrameIndices(imageDescriptor, indices.GetSpan());
+ ReadOnlySpan colorTable = MemoryMarshal.Cast((localColorTable ?? this.globalColorTable).GetSpan());
+ this.ReadFrameColors(ref image, ref previousFrame, indices.GetSpan(), colorTable, imageDescriptor);
// Skip any remaining blocks
this.Skip(0);
@@ -350,7 +350,7 @@ private void ReadFrame(ref Image image, ref ImageFrame p
private void ReadFrameIndices(in GifImageDescriptor imageDescriptor, Span indices)
{
int dataSize = this.stream.ReadByte();
- using (var lzwDecoder = new LzwDecoder(this.configuration.MemoryManager, this.stream))
+ using (var lzwDecoder = new LzwDecoder(this.configuration.MemoryAllocator, this.stream))
{
lzwDecoder.DecodePixels(imageDescriptor.Width, imageDescriptor.Height, dataSize, indices);
}
@@ -528,7 +528,7 @@ private void ReadLogicalScreenDescriptorAndGlobalColorTable(Stream stream)
{
int globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3;
- this.globalColorTable = this.MemoryManager.AllocateManagedByteBuffer(globalColorTableLength, true);
+ this.globalColorTable = this.MemoryAllocator.AllocateManagedByteBuffer(globalColorTableLength, true);
// Read the global color table data from the stream
stream.Read(this.globalColorTable.Array, 0, globalColorTableLength);
diff --git a/src/ImageSharp/Formats/Gif/GifEncoder.cs b/src/ImageSharp/Formats/Gif/GifEncoder.cs
index fb072bcb7a..a07928b04f 100644
--- a/src/ImageSharp/Formats/Gif/GifEncoder.cs
+++ b/src/ImageSharp/Formats/Gif/GifEncoder.cs
@@ -34,7 +34,7 @@ public sealed class GifEncoder : IImageEncoder, IGifEncoderOptions
public void Encode(Image image, Stream stream)
where TPixel : struct, IPixel
{
- var encoder = new GifEncoderCore(image.GetConfiguration().MemoryManager, this);
+ var encoder = new GifEncoderCore(image.GetConfiguration().MemoryAllocator, this);
encoder.Encode(image, stream);
}
}
diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
index 747867c805..f84b13f5fc 100644
--- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
+++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
@@ -7,10 +7,10 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Quantization;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Gif
{
@@ -19,7 +19,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
///
internal sealed class GifEncoderCore
{
- private readonly MemoryManager memoryManager;
+ private readonly MemoryAllocator memoryAllocator;
///
/// A reusable buffer used to reduce allocations.
@@ -49,11 +49,11 @@ internal sealed class GifEncoderCore
///
/// Initializes a new instance of the class.
///
- /// The to use for buffer allocations.
+ /// The to use for buffer allocations.
/// The options for the encoder.
- public GifEncoderCore(MemoryManager memoryManager, IGifEncoderOptions options)
+ public GifEncoderCore(MemoryAllocator memoryAllocator, IGifEncoderOptions options)
{
- this.memoryManager = memoryManager;
+ this.memoryAllocator = memoryAllocator;
this.textEncoding = options.TextEncoding ?? GifConstants.DefaultEncoding;
this.quantizer = options.Quantizer;
this.ignoreMetadata = options.IgnoreMetadata;
@@ -317,10 +317,10 @@ private void WriteColorTable(QuantizedFrame image, Stream stream
int colorTableLength = (int)Math.Pow(2, this.bitDepth) * 3; // The maximium number of colors for the bit depth
Rgb24 rgb = default;
- using (IManagedByteBuffer colorTable = this.memoryManager.AllocateManagedByteBuffer(colorTableLength))
+ using (IManagedByteBuffer colorTable = this.memoryAllocator.AllocateManagedByteBuffer(colorTableLength))
{
ref TPixel paletteRef = ref MemoryMarshal.GetReference(image.Palette.AsSpan());
- ref Rgb24 rgb24Ref = ref Unsafe.As(ref MemoryMarshal.GetReference(colorTable.Span));
+ ref Rgb24 rgb24Ref = ref Unsafe.As(ref MemoryMarshal.GetReference(colorTable.GetSpan()));
for (int i = 0; i < pixelCount; i++)
{
ref TPixel entry = ref Unsafe.Add(ref paletteRef, i);
@@ -342,7 +342,7 @@ private void WriteColorTable(QuantizedFrame image, Stream stream
private void WriteImageData(QuantizedFrame image, Stream stream)
where TPixel : struct, IPixel
{
- using (var encoder = new LzwEncoder(this.memoryManager, image.Pixels, (byte)this.bitDepth))
+ using (var encoder = new LzwEncoder(this.memoryAllocator, image.Pixels, (byte)this.bitDepth))
{
encoder.Encode(stream);
}
diff --git a/src/ImageSharp/Formats/Gif/LzwDecoder.cs b/src/ImageSharp/Formats/Gif/LzwDecoder.cs
index 9f9e070e20..977870936a 100644
--- a/src/ImageSharp/Formats/Gif/LzwDecoder.cs
+++ b/src/ImageSharp/Formats/Gif/LzwDecoder.cs
@@ -5,7 +5,7 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
-using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Gif
{
@@ -48,18 +48,18 @@ internal sealed class LzwDecoder : IDisposable
/// Initializes a new instance of the class
/// and sets the stream, where the compressed data should be read from.
///
- /// The to use for buffer allocations.
+ /// The to use for buffer allocations.
/// The stream to read from.
/// is null.
- public LzwDecoder(MemoryManager memoryManager, Stream stream)
+ public LzwDecoder(MemoryAllocator memoryAllocator, Stream stream)
{
Guard.NotNull(stream, nameof(stream));
this.stream = stream;
- this.prefix = memoryManager.Allocate(MaxStackSize, true);
- this.suffix = memoryManager.Allocate(MaxStackSize, true);
- this.pixelStack = memoryManager.Allocate(MaxStackSize + 1, true);
+ this.prefix = memoryAllocator.Allocate(MaxStackSize, true);
+ this.suffix = memoryAllocator.Allocate(MaxStackSize, true);
+ this.pixelStack = memoryAllocator.Allocate(MaxStackSize + 1, true);
}
///
@@ -102,9 +102,9 @@ public void DecodePixels(int width, int height, int dataSize, Span pixels)
int data = 0;
int first = 0;
- ref int prefixRef = ref MemoryMarshal.GetReference(this.prefix.Span);
- ref int suffixRef = ref MemoryMarshal.GetReference(this.suffix.Span);
- ref int pixelStackRef = ref MemoryMarshal.GetReference(this.pixelStack.Span);
+ ref int prefixRef = ref MemoryMarshal.GetReference(this.prefix.GetSpan());
+ ref int suffixRef = ref MemoryMarshal.GetReference(this.suffix.GetSpan());
+ ref int pixelStackRef = ref MemoryMarshal.GetReference(this.pixelStack.GetSpan());
ref byte pixelsRef = ref MemoryMarshal.GetReference(pixels);
for (code = 0; code < clearCode; code++)
diff --git a/src/ImageSharp/Formats/Gif/LzwEncoder.cs b/src/ImageSharp/Formats/Gif/LzwEncoder.cs
index 1dc7e99e83..de9de5e153 100644
--- a/src/ImageSharp/Formats/Gif/LzwEncoder.cs
+++ b/src/ImageSharp/Formats/Gif/LzwEncoder.cs
@@ -5,7 +5,7 @@
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
-using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Gif
{
@@ -168,16 +168,16 @@ internal sealed class LzwEncoder : IDisposable
///
/// Initializes a new instance of the class.
///
- /// The to use for buffer allocations.
+ /// The to use for buffer allocations.
/// The array of indexed pixels.
/// The color depth in bits.
- public LzwEncoder(MemoryManager memoryManager, byte[] indexedPixels, int colorDepth)
+ public LzwEncoder(MemoryAllocator memoryAllocator, byte[] indexedPixels, int colorDepth)
{
this.pixelArray = indexedPixels;
this.initialCodeSize = Math.Max(2, colorDepth);
- this.hashTable = memoryManager.Allocate(HashSize, true);
- this.codeTable = memoryManager.Allocate(HashSize, true);
+ this.hashTable = memoryAllocator.Allocate(HashSize, true);
+ this.codeTable = memoryAllocator.Allocate(HashSize, true);
}
///
@@ -246,7 +246,7 @@ private void ClearBlock(Stream stream)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void ResetCodeTable()
{
- this.hashTable.Span.Fill(-1);
+ this.hashTable.GetSpan().Fill(-1);
}
///
@@ -293,8 +293,8 @@ private void Compress(int intialBits, Stream stream)
this.Output(this.clearCode, stream);
- ref int hashTableRef = ref MemoryMarshal.GetReference(this.hashTable.Span);
- ref int codeTableRef = ref MemoryMarshal.GetReference(this.codeTable.Span);
+ ref int hashTableRef = ref MemoryMarshal.GetReference(this.hashTable.GetSpan());
+ ref int codeTableRef = ref MemoryMarshal.GetReference(this.codeTable.GetSpan());
while (this.position < this.pixelArray.Length)
{
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.CopyTo.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.CopyTo.cs
index 43cc3e9dba..4db6d74317 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.CopyTo.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.CopyTo.cs
@@ -4,7 +4,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
-using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Formats.Jpeg.Components
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs
index 080bf83338..11b5c60d15 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs
@@ -7,7 +7,7 @@
using System.Numerics;
using SixLabors.ImageSharp.Common.Tuples;
-using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters
{
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs
index efa746819d..253b20c39c 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs
@@ -1,7 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
-using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs
index b586d520a6..2baefff9b6 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs
@@ -3,7 +3,7 @@
using System.Runtime.InteropServices;
-using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs
index fe18f8438c..2b442fcdc9 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs
@@ -3,7 +3,7 @@
using System;
-using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
@@ -26,11 +26,11 @@ internal class JpegComponentPostProcessor : IDisposable
///
/// Initializes a new instance of the class.
///
- public JpegComponentPostProcessor(MemoryManager memoryManager, JpegImagePostProcessor imagePostProcessor, IJpegComponent component)
+ public JpegComponentPostProcessor(MemoryAllocator memoryAllocator, JpegImagePostProcessor imagePostProcessor, IJpegComponent component)
{
this.Component = component;
this.ImagePostProcessor = imagePostProcessor;
- this.ColorBuffer = memoryManager.Allocate2D(
+ this.ColorBuffer = memoryAllocator.Allocate2D(
imagePostProcessor.PostProcessorBufferSize.Width,
imagePostProcessor.PostProcessorBufferSize.Height);
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs
index 38340b2380..99408cf576 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs
@@ -4,12 +4,10 @@
using System;
using System.Linq;
using System.Numerics;
-
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
using SixLabors.Primitives;
-
using JpegColorConverter = SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters.JpegColorConverter;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
@@ -49,17 +47,17 @@ internal class JpegImagePostProcessor : IDisposable
///
/// Initializes a new instance of the class.
///
- /// The to use for buffer allocations.
+ /// The to use for buffer allocations.
/// The representing the uncompressed spectral Jpeg data
- public JpegImagePostProcessor(MemoryManager memoryManager, IRawJpegData rawJpeg)
+ public JpegImagePostProcessor(MemoryAllocator memoryAllocator, IRawJpegData rawJpeg)
{
this.RawJpeg = rawJpeg;
IJpegComponent c0 = rawJpeg.Components.First();
this.NumberOfPostProcessorSteps = c0.SizeInBlocks.Height / BlockRowsPerStep;
this.PostProcessorBufferSize = new Size(c0.SizeInBlocks.Width * 8, PixelRowsPerStep);
- this.ComponentProcessors = rawJpeg.Components.Select(c => new JpegComponentPostProcessor(memoryManager, this, c)).ToArray();
- this.rgbaBuffer = memoryManager.Allocate(rawJpeg.ImageSizeInPixels.Width);
+ this.ComponentProcessors = rawJpeg.Components.Select(c => new JpegComponentPostProcessor(memoryAllocator, this, c)).ToArray();
+ this.rgbaBuffer = memoryAllocator.Allocate(rawJpeg.ImageSizeInPixels.Width);
this.colorConverter = JpegColorConverter.GetConverter(rawJpeg.ColorSpace);
}
@@ -155,11 +153,11 @@ private void ConvertColorsInto(ImageFrame destination)
int y = yy - this.PixelRowCounter;
var values = new JpegColorConverter.ComponentValues(buffers, y);
- this.colorConverter.ConvertToRgba(values, this.rgbaBuffer.Span);
+ this.colorConverter.ConvertToRgba(values, this.rgbaBuffer.GetSpan());
Span destRow = destination.GetPixelRowSpan(yy);
- PixelOperations.Instance.PackFromVector4(this.rgbaBuffer.Span, destRow, destination.Width);
+ PixelOperations.Instance.PackFromVector4(this.rgbaBuffer.GetSpan(), destRow, destination.Width);
}
}
}
diff --git a/src/ImageSharp/Formats/Jpeg/Components/GenericBlock8x8.cs b/src/ImageSharp/Formats/Jpeg/Components/GenericBlock8x8.cs
index 9aceb78b2a..825a7f5f0e 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/GenericBlock8x8.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/GenericBlock8x8.cs
@@ -4,10 +4,9 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
-
using SixLabors.ImageSharp.Advanced;
-using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.Memory;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Formats.Jpeg.Components
diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/GolangComponent.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/GolangComponent.cs
index bb3bd01aa3..75cea5551f 100644
--- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/GolangComponent.cs
+++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/GolangComponent.cs
@@ -6,7 +6,7 @@
using SixLabors.ImageSharp.Formats.Jpeg.Components;
using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
-using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
@@ -56,9 +56,9 @@ public GolangComponent(byte identifier, int index)
///
/// Initializes