diff --git a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj
index 661f33e08..42ef080e5 100644
--- a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj
+++ b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj
@@ -37,10 +37,9 @@
-
-
-
+
+
All
diff --git a/src/ImageSharp.Drawing/Primitives/ShapeRegion.cs b/src/ImageSharp.Drawing/Primitives/ShapeRegion.cs
index fddd283e0..812744b89 100644
--- a/src/ImageSharp.Drawing/Primitives/ShapeRegion.cs
+++ b/src/ImageSharp.Drawing/Primitives/ShapeRegion.cs
@@ -3,6 +3,8 @@
using System;
using System.Buffers;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
using SixLabors.Primitives;
using SixLabors.Shapes;
diff --git a/src/ImageSharp.Drawing/Processing/BrushApplicator.cs b/src/ImageSharp.Drawing/Processing/BrushApplicator.cs
index 64f37eeab..41b47a822 100644
--- a/src/ImageSharp.Drawing/Processing/BrushApplicator.cs
+++ b/src/ImageSharp.Drawing/Processing/BrushApplicator.cs
@@ -5,6 +5,7 @@ using System;
using System.Buffers;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp.Drawing/Processing/ImageBrush{TPixel}.cs b/src/ImageSharp.Drawing/Processing/ImageBrush{TPixel}.cs
index 5ebad0f32..c3f81868b 100644
--- a/src/ImageSharp.Drawing/Processing/ImageBrush{TPixel}.cs
+++ b/src/ImageSharp.Drawing/Processing/ImageBrush{TPixel}.cs
@@ -4,6 +4,7 @@
using System;
using System.Buffers;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
diff --git a/src/ImageSharp.Drawing/Processing/PatternBrush{TPixel}.cs b/src/ImageSharp.Drawing/Processing/PatternBrush{TPixel}.cs
index ab48a185b..2ce9a7ce5 100644
--- a/src/ImageSharp.Drawing/Processing/PatternBrush{TPixel}.cs
+++ b/src/ImageSharp.Drawing/Processing/PatternBrush{TPixel}.cs
@@ -5,6 +5,7 @@ using System;
using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.Memory;
diff --git a/src/ImageSharp.Drawing/Processing/Processors/Drawing/DrawImageProcessor.cs b/src/ImageSharp.Drawing/Processing/Processors/Drawing/DrawImageProcessor.cs
index 4a59dfe3e..faf3fe711 100644
--- a/src/ImageSharp.Drawing/Processing/Processors/Drawing/DrawImageProcessor.cs
+++ b/src/ImageSharp.Drawing/Processing/Processors/Drawing/DrawImageProcessor.cs
@@ -5,6 +5,7 @@ using System;
using System.Buffers;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
@@ -139,10 +140,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Drawing
{
amount.GetSpan().Fill(this.Opacity);
- Parallel.For(
+ ParallelFor.WithConfiguration(
minY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span background = source.GetPixelRowSpan(y).Slice(minX, width);
diff --git a/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs b/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs
index e40ba5316..c9d6777ce 100644
--- a/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs
+++ b/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillProcessor.cs
@@ -5,6 +5,7 @@ using System;
using System.Buffers;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
@@ -54,10 +55,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Drawing
// If there's no reason for blending, then avoid it.
if (this.IsSolidBrushWithoutBlending(out SolidBrush solidBrush))
{
- Parallel.For(
+ ParallelFor.WithConfiguration(
minY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
source.GetPixelRowSpan(y).Slice(minX, width).Fill(solidBrush.Color);
@@ -84,10 +85,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Drawing
{
amount.GetSpan().Fill(1f);
- Parallel.For(
+ ParallelFor.WithConfiguration(
minY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
int offsetY = y - startY;
diff --git a/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillRegionProcessor.cs b/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillRegionProcessor.cs
index b9db3f067..514249a2d 100644
--- a/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillRegionProcessor.cs
+++ b/src/ImageSharp.Drawing/Processing/Processors/Drawing/FillRegionProcessor.cs
@@ -4,6 +4,7 @@
using System;
using System.Buffers;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.ImageSharp.Utils;
diff --git a/src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor.cs b/src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor.cs
index 048c4440d..6da635c98 100644
--- a/src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor.cs
+++ b/src/ImageSharp.Drawing/Processing/Processors/Text/DrawTextProcessor.cs
@@ -6,6 +6,7 @@ using System.Buffers;
using System.Collections.Generic;
using SixLabors.Fonts;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Utils;
using SixLabors.Memory;
diff --git a/src/ImageSharp.Drawing/Processing/RecolorBrush{TPixel}.cs b/src/ImageSharp.Drawing/Processing/RecolorBrush{TPixel}.cs
index e1b11637d..87e1dc146 100644
--- a/src/ImageSharp.Drawing/Processing/RecolorBrush{TPixel}.cs
+++ b/src/ImageSharp.Drawing/Processing/RecolorBrush{TPixel}.cs
@@ -5,6 +5,7 @@ using System;
using System.Buffers;
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
diff --git a/src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs b/src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs
index 3904f3d9b..6b69c33f0 100644
--- a/src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs
+++ b/src/ImageSharp.Drawing/Processing/SolidBrush{TPixel}.cs
@@ -5,6 +5,7 @@ using System;
using System.Buffers;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
diff --git a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs
index 1c73b5ed1..328d57596 100644
--- a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs
+++ b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs
@@ -3,6 +3,8 @@
using System;
using System.Runtime.InteropServices;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp/Advanced/IPixelSource.cs b/src/ImageSharp/Advanced/IPixelSource.cs
index 27b3170e6..19616d742 100644
--- a/src/ImageSharp/Advanced/IPixelSource.cs
+++ b/src/ImageSharp/Advanced/IPixelSource.cs
@@ -1,6 +1,7 @@
// 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;
diff --git a/src/ImageSharp/Common/Extensions/ConfigurationExtensions.cs b/src/ImageSharp/Common/Extensions/ConfigurationExtensions.cs
new file mode 100644
index 000000000..6bb5adc06
--- /dev/null
+++ b/src/ImageSharp/Common/Extensions/ConfigurationExtensions.cs
@@ -0,0 +1,22 @@
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+
+using System.Threading.Tasks;
+
+namespace SixLabors.ImageSharp
+{
+ ///
+ /// Contains extension methods for
+ ///
+ internal static class ConfigurationExtensions
+ {
+ ///
+ /// Creates a object based on ,
+ /// having set to
+ ///
+ public static ParallelOptions GetParallelOptions(this Configuration configuration)
+ {
+ return new ParallelOptions() { MaxDegreeOfParallelism = configuration.MaxDegreeOfParallelism };
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ImageSharp/Common/Extensions/StreamExtensions.cs b/src/ImageSharp/Common/Extensions/StreamExtensions.cs
index d11ba8ca5..a200ffebc 100644
--- a/src/ImageSharp/Common/Extensions/StreamExtensions.cs
+++ b/src/ImageSharp/Common/Extensions/StreamExtensions.cs
@@ -4,6 +4,9 @@
using System;
using System.IO;
+using SixLabors.ImageSharp.Memory;
+using SixLabors.Memory;
+
namespace SixLabors.ImageSharp
{
///
@@ -69,5 +72,15 @@ namespace SixLabors.ImageSharp
}
}
}
+
+ public static void Read(this Stream stream, IManagedByteBuffer buffer)
+ {
+ stream.Read(buffer.Array, 0, buffer.Length());
+ }
+
+ public static void Write(this Stream stream, IManagedByteBuffer buffer)
+ {
+ stream.Write(buffer.Array, 0, buffer.Length());
+ }
}
}
diff --git a/src/ImageSharp/Common/Helpers/ParallelFor.cs b/src/ImageSharp/Common/Helpers/ParallelFor.cs
index 02c6deda3..4c14bb6e3 100644
--- a/src/ImageSharp/Common/Helpers/ParallelFor.cs
+++ b/src/ImageSharp/Common/Helpers/ParallelFor.cs
@@ -11,11 +11,11 @@ namespace SixLabors.ImageSharp
internal static class ParallelFor
{
///
- /// Helper method to execute Parallel.For using the settings in
+ /// Helper method to execute Parallel.For using the settings in
///
public static void WithConfiguration(int fromInclusive, int toExclusive, Configuration configuration, Action body)
{
- Parallel.For(fromInclusive, toExclusive, configuration.ParallelOptions, body);
+ Parallel.For(fromInclusive, toExclusive, configuration.GetParallelOptions(), body);
}
///
@@ -37,7 +37,7 @@ namespace SixLabors.ImageSharp
where T : struct
{
MemoryAllocator memoryAllocator = configuration.MemoryAllocator;
- ParallelOptions parallelOptions = configuration.ParallelOptions;
+ ParallelOptions parallelOptions = configuration.GetParallelOptions();
IMemoryOwner InitBuffer()
{
diff --git a/src/ImageSharp/Configuration.cs b/src/ImageSharp/Configuration.cs
index e84674355..1b009bfed 100644
--- a/src/ImageSharp/Configuration.cs
+++ b/src/ImageSharp/Configuration.cs
@@ -27,6 +27,8 @@ namespace SixLabors.ImageSharp
///
private static readonly Lazy Lazy = new Lazy(CreateDefaultInstance);
+ private int maxDegreeOfParallelism = Environment.ProcessorCount;
+
///
/// Initializes a new instance of the class.
///
@@ -55,9 +57,22 @@ namespace SixLabors.ImageSharp
public static Configuration Default { get; } = Lazy.Value;
///
- /// Gets the global parallel options for processing tasks in parallel.
+ /// Gets or sets the maximum number of concurrent tasks enabled in ImageSharp algorithms
+ /// configured with this instance.
///
- public ParallelOptions ParallelOptions { get; private set; } = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };
+ public int MaxDegreeOfParallelism
+ {
+ get => this.maxDegreeOfParallelism;
+ set
+ {
+ if (value <= 0)
+ {
+ throw new ArgumentOutOfRangeException(nameof(this.MaxDegreeOfParallelism));
+ }
+
+ this.maxDegreeOfParallelism = value;
+ }
+ }
///
/// Gets the currently registered s.
@@ -114,7 +129,7 @@ namespace SixLabors.ImageSharp
{
return new Configuration
{
- ParallelOptions = this.ParallelOptions,
+ MaxDegreeOfParallelism = this.MaxDegreeOfParallelism,
ImageFormatsManager = this.ImageFormatsManager,
MemoryAllocator = this.MemoryAllocator,
ImageOperationsProvider = this.ImageOperationsProvider,
diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
index 612850e5f..128ae0854 100644
--- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
+++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
@@ -6,6 +6,7 @@ using System.Buffers.Binary;
using System.IO;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Common.Helpers;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
index 80fc6330a..b49b8a895 100644
--- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
+++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
@@ -4,6 +4,7 @@
using System;
using System.IO;
using SixLabors.ImageSharp.Common.Helpers;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs
index 591f787ea..3832a30c6 100644
--- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs
+++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs
@@ -7,6 +7,7 @@ using System.Runtime.CompilerServices;
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;
diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
index ea507c781..1fb706ae1 100644
--- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
+++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs
@@ -7,6 +7,8 @@ using System.IO;
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.Processors.Quantization;
diff --git a/src/ImageSharp/Formats/Gif/LzwDecoder.cs b/src/ImageSharp/Formats/Gif/LzwDecoder.cs
index 7a2aef180..3c7b6a4af 100644
--- a/src/ImageSharp/Formats/Gif/LzwDecoder.cs
+++ b/src/ImageSharp/Formats/Gif/LzwDecoder.cs
@@ -6,6 +6,8 @@ using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Gif
diff --git a/src/ImageSharp/Formats/Gif/LzwEncoder.cs b/src/ImageSharp/Formats/Gif/LzwEncoder.cs
index 002457db3..e390dfd54 100644
--- a/src/ImageSharp/Formats/Gif/LzwEncoder.cs
+++ b/src/ImageSharp/Formats/Gif/LzwEncoder.cs
@@ -6,6 +6,8 @@ using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Gif
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.CopyTo.cs b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.CopyTo.cs
index 4db6d7431..bebc13f6d 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.CopyTo.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Block8x8F.CopyTo.cs
@@ -4,6 +4,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
// ReSharper disable InconsistentNaming
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs
index 2937b23a7..40b8d391a 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ColorConverters/JpegColorConverter.cs
@@ -7,6 +7,7 @@ using System.Linq;
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/FastACTables.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/FastACTables.cs
index 95693c09b..26bcde8e5 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/FastACTables.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/FastACTables.cs
@@ -3,6 +3,8 @@
using System;
using System.Runtime.CompilerServices;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanTable.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanTable.cs
index f159bda07..0138164ed 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanTable.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/HuffmanTable.cs
@@ -5,6 +5,8 @@ using System;
using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs
index 253b20c39..c03398033 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/IJpegComponent.cs
@@ -1,6 +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;
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs
index 87f675491..900dd3bc8 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockPostProcessor.cs
@@ -3,6 +3,7 @@
using System.Runtime.InteropServices;
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
using SixLabors.Primitives;
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs
index 73a69a069..65a584c4f 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponent.cs
@@ -5,6 +5,7 @@ using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
using SixLabors.Primitives;
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs
index 2b442fcdc..890f40259 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegComponentPostProcessor.cs
@@ -3,6 +3,7 @@
using System;
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
using SixLabors.Primitives;
diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs
index 1b513c612..94382553c 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegImagePostProcessor.cs
@@ -6,6 +6,7 @@ using System.Buffers;
using System.Linq;
using System.Numerics;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
diff --git a/src/ImageSharp/Formats/Jpeg/Components/GenericBlock8x8.cs b/src/ImageSharp/Formats/Jpeg/Components/GenericBlock8x8.cs
index 825a7f5f0..c15024259 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/GenericBlock8x8.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/GenericBlock8x8.cs
@@ -5,6 +5,7 @@ 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;
diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
index 4f71d15b0..1eb4dad89 100644
--- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
+++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
@@ -12,6 +12,7 @@ using SixLabors.ImageSharp.Common.Helpers;
using SixLabors.ImageSharp.Formats.Jpeg.Components;
using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
using SixLabors.ImageSharp.IO;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.MetaData.Profiles.Exif;
using SixLabors.ImageSharp.MetaData.Profiles.Icc;
diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
index 779a41999..e1ec3c1d6 100644
--- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs
@@ -13,6 +13,7 @@ using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Common.Helpers;
using SixLabors.ImageSharp.Formats.Png.Filters;
using SixLabors.ImageSharp.Formats.Png.Zlib;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.MetaData.Profiles.Exif;
using SixLabors.ImageSharp.PixelFormats;
diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
index b420834fb..ffe29aeca 100644
--- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs
+++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs
@@ -9,6 +9,7 @@ using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Common.Helpers;
using SixLabors.ImageSharp.Formats.Png.Filters;
using SixLabors.ImageSharp.Formats.Png.Zlib;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
diff --git a/src/ImageSharp/Image.Decode.cs b/src/ImageSharp/Image.Decode.cs
index 9087db414..3b014e7bd 100644
--- a/src/ImageSharp/Image.Decode.cs
+++ b/src/ImageSharp/Image.Decode.cs
@@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using SixLabors.ImageSharp.Formats;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp/Image.WrapMemory.cs b/src/ImageSharp/Image.WrapMemory.cs
index 77432c3ad..e8d9ab754 100644
--- a/src/ImageSharp/Image.WrapMemory.cs
+++ b/src/ImageSharp/Image.WrapMemory.cs
@@ -4,6 +4,7 @@
using System;
using System.Buffers;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp/ImageExtensions.Internal.cs b/src/ImageSharp/ImageExtensions.Internal.cs
index 6bbc6ec52..dfdbbd89b 100644
--- a/src/ImageSharp/ImageExtensions.Internal.cs
+++ b/src/ImageSharp/ImageExtensions.Internal.cs
@@ -1,6 +1,7 @@
// 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;
diff --git a/src/ImageSharp/ImageFrameCollection.cs b/src/ImageSharp/ImageFrameCollection.cs
index 154ef5014..929dd7e36 100644
--- a/src/ImageSharp/ImageFrameCollection.cs
+++ b/src/ImageSharp/ImageFrameCollection.cs
@@ -5,6 +5,7 @@ using System;
using System.Collections;
using System.Collections.Generic;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp/ImageFrame{TPixel}.cs b/src/ImageSharp/ImageFrame{TPixel}.cs
index 6c04d5aea..a3971fe9c 100644
--- a/src/ImageSharp/ImageFrame{TPixel}.cs
+++ b/src/ImageSharp/ImageFrame{TPixel}.cs
@@ -7,6 +7,7 @@ using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
@@ -89,7 +90,7 @@ namespace SixLabors.ImageSharp
this.MemoryAllocator = configuration.MemoryAllocator;
this.PixelBuffer = this.MemoryAllocator.Allocate2D(width, height);
this.MetaData = metaData;
- this.Clear(configuration.ParallelOptions, backgroundColor);
+ this.Clear(configuration.GetParallelOptions(), backgroundColor);
}
///
diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj
index eca134101..a7ca0a014 100644
--- a/src/ImageSharp/ImageSharp.csproj
+++ b/src/ImageSharp/ImageSharp.csproj
@@ -35,21 +35,13 @@
-
+
+
All
-
-
-
-
-
-
-
-
-
diff --git a/src/ImageSharp/Image{TPixel}.cs b/src/ImageSharp/Image{TPixel}.cs
index 5a5928d6b..8bc5a40bd 100644
--- a/src/ImageSharp/Image{TPixel}.cs
+++ b/src/ImageSharp/Image{TPixel}.cs
@@ -8,6 +8,7 @@ using System.Linq;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp/Memory/AllocationOptions.cs b/src/ImageSharp/Memory/AllocationOptions.cs
deleted file mode 100644
index 5eda00505..000000000
--- a/src/ImageSharp/Memory/AllocationOptions.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-namespace SixLabors.Memory
-{
- ///
- /// Options for allocating buffers.
- ///
- public enum AllocationOptions
- {
- ///
- /// Indicates that the buffer should just be allocated.
- ///
- None,
-
- ///
- /// Indicates that the allocated buffer should be cleaned following allocation.
- ///
- Clean
- }
-}
diff --git a/src/ImageSharp/Memory/ArrayPoolMemoryAllocator.Buffer{T}.cs b/src/ImageSharp/Memory/ArrayPoolMemoryAllocator.Buffer{T}.cs
deleted file mode 100644
index adc8843a3..000000000
--- a/src/ImageSharp/Memory/ArrayPoolMemoryAllocator.Buffer{T}.cs
+++ /dev/null
@@ -1,82 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-using System;
-using System.Buffers;
-using System.Runtime.InteropServices;
-
-namespace SixLabors.Memory
-{
- ///
- /// Contains and
- ///
- public partial class ArrayPoolMemoryAllocator
- {
- ///
- /// The buffer implementation of .
- ///
- private class Buffer : ManagedBufferBase
- where T : struct
- {
- ///
- /// The length of the buffer
- ///
- private readonly int length;
-
- ///
- /// A weak reference to the source pool.
- ///
- ///
- /// By using a weak reference here, we are making sure that array pools and their retained arrays are always GC-ed
- /// after a call to , regardless of having buffer instances still being in use.
- ///
- private WeakReference> sourcePoolReference;
-
- public Buffer(byte[] data, int length, ArrayPool sourcePool)
- {
- this.Data = data;
- this.length = length;
- this.sourcePoolReference = new WeakReference>(sourcePool);
- }
-
- ///
- /// Gets the buffer as a byte array.
- ///
- protected byte[] Data { get; private set; }
-
- ///
- protected override void Dispose(bool disposing)
- {
- if (!disposing || this.Data == null || this.sourcePoolReference == null)
- {
- return;
- }
-
- if (this.sourcePoolReference.TryGetTarget(out ArrayPool pool))
- {
- pool.Return(this.Data);
- }
-
- this.sourcePoolReference = null;
- this.Data = null;
- }
-
- public override Span GetSpan() => MemoryMarshal.Cast(this.Data.AsSpan()).Slice(0, this.length);
-
- protected override object GetPinnableObject() => this.Data;
- }
-
- ///
- /// The implementation of .
- ///
- private class ManagedByteBuffer : Buffer, IManagedByteBuffer
- {
- public ManagedByteBuffer(byte[] data, int length, ArrayPool sourcePool)
- : base(data, length, sourcePool)
- {
- }
-
- public byte[] Array => this.Data;
- }
- }
-}
\ No newline at end of file
diff --git a/src/ImageSharp/Memory/ArrayPoolMemoryAllocator.CommonFactoryMethods.cs b/src/ImageSharp/Memory/ArrayPoolMemoryAllocator.CommonFactoryMethods.cs
deleted file mode 100644
index 78d6e27b2..000000000
--- a/src/ImageSharp/Memory/ArrayPoolMemoryAllocator.CommonFactoryMethods.cs
+++ /dev/null
@@ -1,71 +0,0 @@
-using SixLabors.ImageSharp.PixelFormats;
-
-namespace SixLabors.Memory
-{
- ///
- /// Contains common factory methods and configuration constants.
- ///
- public partial class ArrayPoolMemoryAllocator
- {
- ///
- /// The default value for: maximum size of pooled arrays in bytes.
- /// Currently set to 24MB, which is equivalent to 8 megapixels of raw data.
- ///
- internal const int DefaultMaxPooledBufferSizeInBytes = 24 * 1024 * 1024;
-
- ///
- /// The value for: The threshold to pool arrays in which has less buckets for memory safety.
- ///
- private const int DefaultBufferSelectorThresholdInBytes = 8 * 1024 * 1024;
-
- ///
- /// The default bucket count for .
- ///
- private const int DefaultLargePoolBucketCount = 6;
-
- ///
- /// The default bucket count for .
- ///
- private const int DefaultNormalPoolBucketCount = 16;
-
- ///
- /// This is the default. Should be good for most use cases.
- ///
- /// The memory manager
- public static ArrayPoolMemoryAllocator CreateDefault()
- {
- return new ArrayPoolMemoryAllocator(
- DefaultMaxPooledBufferSizeInBytes,
- DefaultBufferSelectorThresholdInBytes,
- DefaultLargePoolBucketCount,
- DefaultNormalPoolBucketCount);
- }
-
- ///
- /// For environments with limited memory capabilities. Only small images are pooled, which can result in reduced througput.
- ///
- /// The memory manager
- public static ArrayPoolMemoryAllocator CreateWithModeratePooling()
- {
- return new ArrayPoolMemoryAllocator(1024 * 1024, 32 * 1024, 16, 24);
- }
-
- ///
- /// Only pool small buffers like image rows.
- ///
- /// The memory manager
- public static ArrayPoolMemoryAllocator CreateWithMinimalPooling()
- {
- return new ArrayPoolMemoryAllocator(64 * 1024, 32 * 1024, 8, 24);
- }
-
- ///
- /// RAM is not an issue for me, gimme maximum througput!
- ///
- /// The memory manager
- public static ArrayPoolMemoryAllocator CreateWithAggressivePooling()
- {
- return new ArrayPoolMemoryAllocator(128 * 1024 * 1024, 32 * 1024 * 1024, 16, 32);
- }
- }
-}
\ No newline at end of file
diff --git a/src/ImageSharp/Memory/ArrayPoolMemoryAllocator.cs b/src/ImageSharp/Memory/ArrayPoolMemoryAllocator.cs
deleted file mode 100644
index 32c1c6d1d..000000000
--- a/src/ImageSharp/Memory/ArrayPoolMemoryAllocator.cs
+++ /dev/null
@@ -1,140 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-using System.Buffers;
-using System.Runtime.CompilerServices;
-
-namespace SixLabors.Memory
-{
- ///
- /// Implements by allocating memory from .
- ///
- public sealed partial class ArrayPoolMemoryAllocator : MemoryAllocator
- {
- ///
- /// The for small-to-medium buffers which is not kept clean.
- ///
- private ArrayPool normalArrayPool;
-
- ///
- /// The for huge buffers, which is not kept clean.
- ///
- private ArrayPool largeArrayPool;
-
- private readonly int maxArraysPerBucketNormalPool;
-
- private readonly int maxArraysPerBucketLargePool;
-
- ///
- /// Initializes a new instance of the class.
- ///
- public ArrayPoolMemoryAllocator()
- : this(DefaultMaxPooledBufferSizeInBytes, DefaultBufferSelectorThresholdInBytes)
- {
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The maximum size of pooled arrays. Arrays over the thershold are gonna be always allocated.
- public ArrayPoolMemoryAllocator(int maxPoolSizeInBytes)
- : this(maxPoolSizeInBytes, GetLargeBufferThresholdInBytes(maxPoolSizeInBytes))
- {
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The maximum size of pooled arrays. Arrays over the thershold are gonna be always allocated.
- /// Arrays over this threshold will be pooled in which has less buckets for memory safety.
- public ArrayPoolMemoryAllocator(int maxPoolSizeInBytes, int poolSelectorThresholdInBytes)
- : this(maxPoolSizeInBytes, poolSelectorThresholdInBytes, DefaultLargePoolBucketCount, DefaultNormalPoolBucketCount)
- {
- }
-
- ///
- /// Initializes a new instance of the class.
- ///
- /// The maximum size of pooled arrays. Arrays over the thershold are gonna be always allocated.
- /// The threshold to pool arrays in which has less buckets for memory safety.
- /// Max arrays per bucket for the large array pool
- /// Max arrays per bucket for the normal array pool
- public ArrayPoolMemoryAllocator(int maxPoolSizeInBytes, int poolSelectorThresholdInBytes, int maxArraysPerBucketLargePool, int maxArraysPerBucketNormalPool)
- {
- ImageSharp.Guard.MustBeGreaterThan(maxPoolSizeInBytes, 0, nameof(maxPoolSizeInBytes));
- Guard.MustBeLessThanOrEqualTo(poolSelectorThresholdInBytes, maxPoolSizeInBytes, nameof(poolSelectorThresholdInBytes));
-
- this.MaxPoolSizeInBytes = maxPoolSizeInBytes;
- this.PoolSelectorThresholdInBytes = poolSelectorThresholdInBytes;
- this.maxArraysPerBucketLargePool = maxArraysPerBucketLargePool;
- this.maxArraysPerBucketNormalPool = maxArraysPerBucketNormalPool;
-
- this.InitArrayPools();
- }
-
- ///
- /// Gets the maximum size of pooled arrays in bytes.
- ///
- public int MaxPoolSizeInBytes { get; }
-
- ///
- /// Gets the threshold to pool arrays in which has less buckets for memory safety.
- ///
- public int PoolSelectorThresholdInBytes { get; }
-
- ///
- public override void ReleaseRetainedResources()
- {
- this.InitArrayPools();
- }
-
- ///
- internal override IMemoryOwner Allocate(int length, AllocationOptions options = AllocationOptions.None)
- {
- int itemSizeBytes = Unsafe.SizeOf();
- int bufferSizeInBytes = length * itemSizeBytes;
-
- ArrayPool pool = this.GetArrayPool(bufferSizeInBytes);
- byte[] byteArray = pool.Rent(bufferSizeInBytes);
-
- var buffer = new Buffer(byteArray, length, pool);
- if (options == AllocationOptions.Clean)
- {
- buffer.Clear();
- }
-
- return buffer;
- }
-
- ///
- internal override IManagedByteBuffer AllocateManagedByteBuffer(int length, AllocationOptions options)
- {
- ArrayPool pool = this.GetArrayPool(length);
- byte[] byteArray = pool.Rent(length);
-
- var buffer = new ManagedByteBuffer(byteArray, length, pool);
- if (options == AllocationOptions.Clean)
- {
- buffer.Clear();
- }
-
- return buffer;
- }
-
- private static int GetLargeBufferThresholdInBytes(int maxPoolSizeInBytes)
- {
- return maxPoolSizeInBytes / 4;
- }
-
- private ArrayPool GetArrayPool(int bufferSizeInBytes)
- {
- return bufferSizeInBytes <= this.PoolSelectorThresholdInBytes ? this.normalArrayPool : this.largeArrayPool;
- }
-
- private void InitArrayPools()
- {
- this.largeArrayPool = ArrayPool.Create(this.MaxPoolSizeInBytes, this.maxArraysPerBucketLargePool);
- this.normalArrayPool = ArrayPool.Create(this.PoolSelectorThresholdInBytes, this.maxArraysPerBucketNormalPool);
- }
- }
-}
\ No newline at end of file
diff --git a/src/ImageSharp/Memory/BasicArrayBuffer.cs b/src/ImageSharp/Memory/BasicArrayBuffer.cs
deleted file mode 100644
index f40df7604..000000000
--- a/src/ImageSharp/Memory/BasicArrayBuffer.cs
+++ /dev/null
@@ -1,59 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-using System;
-using System.Runtime.CompilerServices;
-
-namespace SixLabors.Memory
-{
- ///
- /// Wraps an array as an instance.
- ///
- internal class BasicArrayBuffer : ManagedBufferBase
- where T : struct
- {
- public BasicArrayBuffer(T[] array, int length)
- {
- ImageSharp.DebugGuard.MustBeLessThanOrEqualTo(length, array.Length, nameof(length));
- this.Array = array;
- this.Length = length;
- }
-
- public BasicArrayBuffer(T[] array)
- : this(array, array.Length)
- {
- }
-
- public T[] Array { get; }
-
- public int Length { get; }
-
- ///
- /// Returns a reference to specified element of the buffer.
- ///
- /// 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));
-
- Span span = this.GetSpan();
- return ref span[index];
- }
- }
-
- protected override void Dispose(bool disposing)
- {
- }
-
- public override Span GetSpan() => this.Array.AsSpan(0, this.Length);
-
- protected override object GetPinnableObject()
- {
- return this.Array;
- }
- }
-}
\ No newline at end of file
diff --git a/src/ImageSharp/Memory/BasicByteBuffer.cs b/src/ImageSharp/Memory/BasicByteBuffer.cs
deleted file mode 100644
index 9f995e347..000000000
--- a/src/ImageSharp/Memory/BasicByteBuffer.cs
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-namespace SixLabors.Memory
-{
- internal sealed class BasicByteBuffer : BasicArrayBuffer, IManagedByteBuffer
- {
- internal BasicByteBuffer(byte[] array)
- : base(array)
- {
- }
- }
-}
\ No newline at end of file
diff --git a/src/ImageSharp/Memory/Buffer2DExtensions.cs b/src/ImageSharp/Memory/Buffer2DExtensions.cs
index be4f0ef15..107457ae7 100644
--- a/src/ImageSharp/Memory/Buffer2DExtensions.cs
+++ b/src/ImageSharp/Memory/Buffer2DExtensions.cs
@@ -3,9 +3,10 @@
using System;
using System.Runtime.CompilerServices;
+
using SixLabors.Primitives;
-namespace SixLabors.Memory
+namespace SixLabors.ImageSharp.Memory
{
///
/// Defines extension methods for .
diff --git a/src/ImageSharp/Memory/Buffer2D{T}.cs b/src/ImageSharp/Memory/Buffer2D{T}.cs
index 844ca1ad1..41a560cdb 100644
--- a/src/ImageSharp/Memory/Buffer2D{T}.cs
+++ b/src/ImageSharp/Memory/Buffer2D{T}.cs
@@ -2,11 +2,11 @@
// Licensed under the Apache License, Version 2.0.
using System;
-using System.Buffers;
using System.Runtime.CompilerServices;
+
using SixLabors.Primitives;
-namespace SixLabors.Memory
+namespace SixLabors.ImageSharp.Memory
{
///
/// Represents a buffer of value type objects
@@ -62,7 +62,7 @@ namespace SixLabors.Memory
get
{
ImageSharp.DebugGuard.MustBeLessThan(x, this.Width, nameof(x));
- DebugGuard.MustBeLessThan(y, this.Height, nameof(y));
+ ImageSharp.DebugGuard.MustBeLessThan(y, this.Height, nameof(y));
Span span = this.Span;
return ref span[(this.Width * y) + x];
}
diff --git a/src/ImageSharp/Memory/BufferArea{T}.cs b/src/ImageSharp/Memory/BufferArea{T}.cs
index 6a2146fd2..f71a28139 100644
--- a/src/ImageSharp/Memory/BufferArea{T}.cs
+++ b/src/ImageSharp/Memory/BufferArea{T}.cs
@@ -1,8 +1,11 @@
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
+
using SixLabors.Primitives;
-namespace SixLabors.Memory
+namespace SixLabors.ImageSharp.Memory
{
///
/// Represents a rectangular area inside a 2D memory buffer ().
@@ -120,7 +123,7 @@ namespace SixLabors.Memory
public BufferArea GetSubArea(Rectangle rectangle)
{
ImageSharp.DebugGuard.MustBeLessThanOrEqualTo(rectangle.Width, this.Rectangle.Width, nameof(rectangle));
- DebugGuard.MustBeLessThanOrEqualTo(rectangle.Height, this.Rectangle.Height, nameof(rectangle));
+ ImageSharp.DebugGuard.MustBeLessThanOrEqualTo(rectangle.Height, this.Rectangle.Height, nameof(rectangle));
int x = this.Rectangle.X + rectangle.X;
int y = this.Rectangle.Y + rectangle.Y;
diff --git a/src/ImageSharp/Memory/IManagedByteBuffer.cs b/src/ImageSharp/Memory/IManagedByteBuffer.cs
deleted file mode 100644
index 91c61424b..000000000
--- a/src/ImageSharp/Memory/IManagedByteBuffer.cs
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-using System.Buffers;
-
-namespace SixLabors.Memory
-{
- ///
- /// Represents a byte buffer backed by a managed array. Useful for interop with classic .NET API-s.
- ///
- internal interface IManagedByteBuffer : IMemoryOwner
- {
- ///
- /// Gets the managed array backing this buffer instance.
- ///
- byte[] Array { get; }
- }
-}
\ No newline at end of file
diff --git a/src/ImageSharp/Memory/ManagedBufferBase.cs b/src/ImageSharp/Memory/ManagedBufferBase.cs
deleted file mode 100644
index 8aaf199ff..000000000
--- a/src/ImageSharp/Memory/ManagedBufferBase.cs
+++ /dev/null
@@ -1,43 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-using System.Buffers;
-using System.Runtime.InteropServices;
-
-namespace SixLabors.Memory
-{
- ///
- /// Provides a base class for implementations by implementing pinning logic for adaption.
- ///
- internal abstract class ManagedBufferBase : MemoryManager
- where T : struct
- {
- private GCHandle pinHandle;
-
- public bool IsMemoryOwner => true;
-
- ///
- /// Gets the object that should be pinned.
- ///
- protected abstract object GetPinnableObject();
-
- public override unsafe MemoryHandle Pin(int elementIndex = 0)
- {
- if (!this.pinHandle.IsAllocated)
- {
- this.pinHandle = GCHandle.Alloc(this.GetPinnableObject(), GCHandleType.Pinned);
- }
-
- void* ptr = (void*)this.pinHandle.AddrOfPinnedObject();
- return new MemoryHandle(ptr, this.pinHandle);
- }
-
- public override void Unpin()
- {
- if (this.pinHandle.IsAllocated)
- {
- this.pinHandle.Free();
- }
- }
- }
-}
\ No newline at end of file
diff --git a/src/ImageSharp/Memory/MemoryAllocator.cs b/src/ImageSharp/Memory/MemoryAllocator.cs
deleted file mode 100644
index 57b721e48..000000000
--- a/src/ImageSharp/Memory/MemoryAllocator.cs
+++ /dev/null
@@ -1,39 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-using System.Buffers;
-
-namespace SixLabors.Memory
-{
- ///
- /// Memory managers are used to allocate memory for image processing operations.
- ///
- public abstract class MemoryAllocator
- {
- ///
- /// Allocates an , holding a of length .
- ///
- /// Type of the data stored in the buffer
- /// Size of the buffer to allocate
- /// The allocation options.
- /// A buffer of values of type .
- internal abstract IMemoryOwner Allocate(int length, AllocationOptions options = AllocationOptions.None)
- where T : struct;
-
- ///
- /// Allocates an .
- ///
- /// The requested buffer length
- /// The allocation options.
- /// The
- internal abstract IManagedByteBuffer AllocateManagedByteBuffer(int length, AllocationOptions options = AllocationOptions.None);
-
- ///
- /// Releases all retained resources not being in use.
- /// Eg: by resetting array pools and letting GC to free the arrays.
- ///
- public virtual void ReleaseRetainedResources()
- {
- }
- }
-}
diff --git a/src/ImageSharp/Memory/MemoryAllocatorExtensions.cs b/src/ImageSharp/Memory/MemoryAllocatorExtensions.cs
index d8c1f51f4..b596351b5 100644
--- a/src/ImageSharp/Memory/MemoryAllocatorExtensions.cs
+++ b/src/ImageSharp/Memory/MemoryAllocatorExtensions.cs
@@ -1,8 +1,12 @@
-using System.Buffers;
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+using System.Buffers;
+
+using SixLabors.Memory;
using SixLabors.Primitives;
-namespace SixLabors.Memory
+namespace SixLabors.ImageSharp.Memory
{
///
/// Extension methods for .
diff --git a/src/ImageSharp/Memory/BufferExtensions.cs b/src/ImageSharp/Memory/MemoryOwnerExtensions.cs
similarity index 83%
rename from src/ImageSharp/Memory/BufferExtensions.cs
rename to src/ImageSharp/Memory/MemoryOwnerExtensions.cs
index 800e0d975..9b68f52c4 100644
--- a/src/ImageSharp/Memory/BufferExtensions.cs
+++ b/src/ImageSharp/Memory/MemoryOwnerExtensions.cs
@@ -3,13 +3,15 @@
using System;
using System.Buffers;
-using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
-namespace SixLabors.Memory
+namespace SixLabors.ImageSharp.Memory
{
- internal static class BufferExtensions
+ ///
+ /// Extension methods for
+ ///
+ internal static class MemoryOwnerExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Span GetSpan(this IMemoryOwner buffer)
@@ -57,15 +59,5 @@ namespace SixLabors.Memory
public static ref T GetReference(this IMemoryOwner buffer)
where T : struct =>
ref MemoryMarshal.GetReference(buffer.GetSpan());
-
- public static void Read(this Stream stream, IManagedByteBuffer buffer)
- {
- stream.Read(buffer.Array, 0, buffer.Length());
- }
-
- public static void Write(this Stream stream, IManagedByteBuffer buffer)
- {
- stream.Write(buffer.Array, 0, buffer.Length());
- }
}
}
\ No newline at end of file
diff --git a/src/ImageSharp/Memory/MemorySource.cs b/src/ImageSharp/Memory/MemorySource.cs
index c0a74b5f8..f0b0ab028 100644
--- a/src/ImageSharp/Memory/MemorySource.cs
+++ b/src/ImageSharp/Memory/MemorySource.cs
@@ -4,7 +4,9 @@
using System;
using System.Buffers;
-namespace SixLabors.Memory
+using SixLabors.Memory;
+
+namespace SixLabors.ImageSharp.Memory
{
///
/// Holds a that is either OWNED or CONSUMED.
diff --git a/src/ImageSharp/Memory/SimpleGcMemoryAllocator.cs b/src/ImageSharp/Memory/SimpleGcMemoryAllocator.cs
deleted file mode 100644
index 612b53820..000000000
--- a/src/ImageSharp/Memory/SimpleGcMemoryAllocator.cs
+++ /dev/null
@@ -1,21 +0,0 @@
-using System.Buffers;
-
-namespace SixLabors.Memory
-{
- ///
- /// Implements by newing up arrays by the GC on every allocation requests.
- ///
- public sealed class SimpleGcMemoryAllocator : MemoryAllocator
- {
- ///
- internal override IMemoryOwner Allocate(int length, AllocationOptions options = AllocationOptions.None)
- {
- return new BasicArrayBuffer(new T[length]);
- }
-
- internal override IManagedByteBuffer AllocateManagedByteBuffer(int length, AllocationOptions options)
- {
- return new BasicByteBuffer(new byte[length]);
- }
- }
-}
\ No newline at end of file
diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs
index b454349d7..9b1e29db8 100644
--- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs
+++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs
@@ -5,6 +5,8 @@
using System;
using System.Numerics;
using System.Buffers;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.PixelFormats.PixelBlenders
diff --git a/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs b/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs
index 57d4e00ae..c4f4266d9 100644
--- a/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs
@@ -70,25 +70,27 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
bool isAlphaOnly = typeof(TPixel) == typeof(Alpha8);
- Parallel.For(
+ ParallelFor.WithConfiguration(
startY,
endY,
- configuration.ParallelOptions,
+ configuration,
y =>
- {
- Span row = source.GetPixelRowSpan(y);
- Rgba32 rgba = default;
-
- for (int x = startX; x < endX; x++)
{
- ref TPixel color = ref row[x];
- color.ToRgba32(ref rgba);
+ Span row = source.GetPixelRowSpan(y);
+ Rgba32 rgba = default;
+
+ for (int x = startX; x < endX; x++)
+ {
+ ref TPixel color = ref row[x];
+ color.ToRgba32(ref rgba);
- // Convert to grayscale using ITU-R Recommendation BT.709 if required
- float luminance = isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
- color = luminance >= threshold ? upper : lower;
- }
- });
+ // Convert to grayscale using ITU-R Recommendation BT.709 if required
+ float luminance = isAlphaOnly
+ ? rgba.A
+ : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B);
+ color = luminance >= threshold ? upper : lower;
+ }
+ });
}
}
}
\ No newline at end of file
diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs
index 57f71a9ce..b5a272543 100644
--- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs
@@ -5,6 +5,7 @@ using System;
using System.Numerics;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.Memory;
@@ -61,10 +62,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
{
source.CopyTo(targetPixels);
- Parallel.For(
+ ParallelFor.WithConfiguration(
startY,
endY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span sourceRow = source.GetPixelRowSpan(y);
diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs
index 6d7147cf7..07b2ed064 100644
--- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs
@@ -4,6 +4,8 @@
using System;
using System.Numerics;
using System.Threading.Tasks;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.ImageSharp.Processing.Processors;
@@ -43,12 +45,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
///
protected override void OnFrameApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration)
{
- ParallelOptions parallelOptions = configuration.ParallelOptions;
-
using (Buffer2D firstPassPixels = configuration.MemoryAllocator.Allocate2D(source.Size()))
{
- this.ApplyConvolution(firstPassPixels, source.PixelBuffer, source.Bounds(), this.KernelX, parallelOptions);
- this.ApplyConvolution(source.PixelBuffer, firstPassPixels, sourceRectangle, this.KernelY, parallelOptions);
+ this.ApplyConvolution(firstPassPixels, source.PixelBuffer, source.Bounds(), this.KernelX, configuration);
+ this.ApplyConvolution(source.PixelBuffer, firstPassPixels, sourceRectangle, this.KernelY, configuration);
}
}
@@ -62,13 +62,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
/// The structure that specifies the portion of the image object to draw.
///
/// The kernel operator.
- /// The parallel options
+ /// The
private void ApplyConvolution(
Buffer2D targetPixels,
Buffer2D sourcePixels,
Rectangle sourceRectangle,
DenseMatrix kernel,
- ParallelOptions parallelOptions)
+ Configuration configuration)
{
int kernelHeight = kernel.Rows;
int kernelWidth = kernel.Columns;
@@ -82,10 +82,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
int maxY = endY - 1;
int maxX = endX - 1;
- Parallel.For(
+ ParallelFor.WithConfiguration(
startY,
endY,
- parallelOptions,
+ configuration,
y =>
{
Span targetRow = targetPixels.GetRowSpan(y);
diff --git a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs
index 84a166545..31e638a0a 100644
--- a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs
@@ -5,6 +5,7 @@ using System;
using System.Numerics;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.ImageSharp.Processing.Processors;
@@ -51,10 +52,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
{
source.CopyTo(targetPixels);
- Parallel.For(
+ ParallelFor.WithConfiguration(
startY,
endY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span sourceRow = source.GetPixelRowSpan(y);
diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorCompassProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorCompassProcessor.cs
index 22297b8f2..316de422f 100644
--- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorCompassProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetectorCompassProcessor.cs
@@ -6,6 +6,8 @@ using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.ImageSharp.Processing.Processors.Filters;
@@ -133,10 +135,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution
Buffer2D passPixels = pass.PixelBuffer;
Buffer2D targetPixels = source.PixelBuffer;
- Parallel.For(
+ ParallelFor.WithConfiguration(
minY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
int offsetY = y - shiftY;
diff --git a/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor.cs b/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor.cs
index b9329f4df..59898e9fc 100644
--- a/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Effects/OilPaintingProcessor.cs
@@ -5,6 +5,7 @@ using System;
using System.Numerics;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
@@ -69,10 +70,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
{
source.CopyTo(targetPixels);
- Parallel.For(
+ ParallelFor.WithConfiguration(
startY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span sourceRow = source.GetPixelRowSpan(y);
diff --git a/src/ImageSharp/Processing/Processors/Effects/PixelateProcessor.cs b/src/ImageSharp/Processing/Processors/Effects/PixelateProcessor.cs
index 56085e76c..50f76efed 100644
--- a/src/ImageSharp/Processing/Processors/Effects/PixelateProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Effects/PixelateProcessor.cs
@@ -73,7 +73,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Effects
Parallel.ForEach(
range,
- configuration.ParallelOptions,
+ configuration.GetParallelOptions(),
y =>
{
int offsetY = y - startY;
diff --git a/src/ImageSharp/Processing/Processors/Filters/FilterProcessor.cs b/src/ImageSharp/Processing/Processors/Filters/FilterProcessor.cs
index e8a1fc9cb..6244d8bf7 100644
--- a/src/ImageSharp/Processing/Processors/Filters/FilterProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Filters/FilterProcessor.cs
@@ -41,10 +41,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters
int endX = interest.Right;
Matrix4x4 matrix = this.Matrix;
- Parallel.For(
+ ParallelFor.WithConfiguration(
startY,
endY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span row = source.GetPixelRowSpan(y);
diff --git a/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs b/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs
index 7b6209c30..e90b35225 100644
--- a/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Normalization/HistogramEqualizationProcessor.cs
@@ -6,6 +6,7 @@ using System.Buffers;
using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
diff --git a/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor.cs
index d2e89fcd0..c7fa2ff19 100644
--- a/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Overlays/BackgroundColorProcessor.cs
@@ -5,6 +5,7 @@ using System;
using System.Buffers;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
@@ -81,10 +82,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
}
PixelBlender blender = PixelOperations.Instance.GetPixelBlender(this.GraphicsOptions.BlenderMode);
- Parallel.For(
+ ParallelFor.WithConfiguration(
minY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span destination = source.GetPixelRowSpan(y - startY).Slice(minX - startX, width);
diff --git a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs
index 17d131413..604249331 100644
--- a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs
@@ -6,6 +6,7 @@ using System.Buffers;
using System.Numerics;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.Memory;
@@ -122,27 +123,25 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
rowColorsSpan[i] = glowColor;
}
- Parallel.For(
+ ParallelFor.WithTemporaryBuffer(
minY,
maxY,
- configuration.ParallelOptions,
- y =>
+ configuration,
+ width,
+ (y, amounts) =>
{
- using (IMemoryOwner amounts = source.MemoryAllocator.Allocate(width))
+ Span amountsSpan = amounts.GetSpan();
+ int offsetY = y - startY;
+ int offsetX = minX - startX;
+ for (int i = 0; i < width; i++)
{
- Span amountsSpan = amounts.GetSpan();
- int offsetY = y - startY;
- int offsetX = minX - startX;
- for (int i = 0; i < width; i++)
- {
- float distance = Vector2.Distance(center, new Vector2(i + offsetX, offsetY));
- amountsSpan[i] = (this.GraphicsOptions.BlendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1);
- }
-
- Span destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width);
-
- this.blender.Blend(source.MemoryAllocator, destination, destination, rowColors.GetSpan(), amountsSpan);
+ float distance = Vector2.Distance(center, new Vector2(i + offsetX, offsetY));
+ amountsSpan[i] = (this.GraphicsOptions.BlendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1);
}
+
+ Span destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width);
+
+ this.blender.Blend(source.MemoryAllocator, destination, destination, rowColors.GetSpan(), amountsSpan);
});
}
}
diff --git a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs
index a306459d1..b8bd6c5a5 100644
--- a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs
@@ -6,6 +6,7 @@ using System.Buffers;
using System.Numerics;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.Memory;
@@ -124,27 +125,33 @@ namespace SixLabors.ImageSharp.Processing.Processors.Overlays
rowColorsSpan[i] = vignetteColor;
}
- Parallel.For(
+ ParallelFor.WithTemporaryBuffer(
minY,
maxY,
- configuration.ParallelOptions,
- y =>
+ configuration,
+ width,
+ (y, amounts) =>
{
- using (IMemoryOwner amounts = source.MemoryAllocator.Allocate(width))
+ Span amountsSpan = amounts.GetSpan();
+ int offsetY = y - startY;
+ int offsetX = minX - startX;
+ for (int i = 0; i < width; i++)
{
- Span amountsSpan = amounts.GetSpan();
- int offsetY = y - startY;
- int offsetX = minX - startX;
- for (int i = 0; i < width; i++)
- {
- float distance = Vector2.Distance(centre, new Vector2(i + offsetX, offsetY));
- amountsSpan[i] = (this.GraphicsOptions.BlendPercentage * (.9F * (distance / maxDistance))).Clamp(0, 1);
- }
-
- Span destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width);
-
- this.blender.Blend(source.MemoryAllocator, destination, destination, rowColors.GetSpan(), amountsSpan);
+ float distance = Vector2.Distance(centre, new Vector2(i + offsetX, offsetY));
+ amountsSpan[i] =
+ (this.GraphicsOptions.BlendPercentage * (.9F * (distance / maxDistance))).Clamp(
+ 0,
+ 1);
}
+
+ Span destination = source.GetPixelRowSpan(offsetY).Slice(offsetX, width);
+
+ this.blender.Blend(
+ source.MemoryAllocator,
+ destination,
+ destination,
+ rowColors.GetSpan(),
+ amountsSpan);
});
}
}
diff --git a/src/ImageSharp/Processing/Processors/Quantization/QuantizedFrame{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/QuantizedFrame{TPixel}.cs
index d0d79093c..2e3bb2c41 100644
--- a/src/ImageSharp/Processing/Processors/Quantization/QuantizedFrame{TPixel}.cs
+++ b/src/ImageSharp/Processing/Processors/Quantization/QuantizedFrame{TPixel}.cs
@@ -4,6 +4,7 @@
using System;
using System.Buffers;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs
index 80eefa9b3..619107f97 100644
--- a/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs
+++ b/src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs
@@ -8,6 +8,7 @@ using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
diff --git a/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs
index d9f35c892..3993ab1a8 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/AffineTransformProcessor.cs
@@ -9,6 +9,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
@@ -77,10 +78,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
if (this.Sampler is NearestNeighborResampler)
{
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
height,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span destRow = destination.GetPixelRowSpan(y);
@@ -115,10 +116,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
using (Buffer2D yBuffer = memoryAllocator.Allocate2D(yLength, height))
using (Buffer2D xBuffer = memoryAllocator.Allocate2D(xLength, height))
{
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
height,
- configuration.ParallelOptions,
+ configuration,
y =>
{
ref TPixel destRowRef = ref MemoryMarshal.GetReference(destination.GetPixelRowSpan(y));
diff --git a/src/ImageSharp/Processing/Processors/Transforms/CropProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/CropProcessor.cs
index 5d714eef5..0c5212375 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/CropProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/CropProcessor.cs
@@ -58,10 +58,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int minX = Math.Max(this.CropRectangle.X, sourceRectangle.X);
int maxX = Math.Min(this.CropRectangle.Right, sourceRectangle.Right);
- Parallel.For(
+ ParallelFor.WithConfiguration(
minY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span sourceRow = source.GetPixelRowSpan(y).Slice(minX);
diff --git a/src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs
index 4ab4971b8..cea6df391 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/FlipProcessor.cs
@@ -4,6 +4,7 @@
using System;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
@@ -58,10 +59,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
using (Buffer2D targetPixels = configuration.MemoryAllocator.Allocate2D(source.Size()))
{
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
halfHeight,
- configuration.ParallelOptions,
+ configuration,
y =>
{
int newY = height - y - 1;
@@ -91,10 +92,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
using (Buffer2D targetPixels = configuration.MemoryAllocator.Allocate2D(source.Size()))
{
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
height,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span sourceRow = source.GetPixelRowSpan(y);
diff --git a/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs
index 716133fb7..042ce2ff6 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/ProjectiveTransformProcessor.cs
@@ -9,6 +9,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
@@ -74,10 +75,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
if (this.Sampler is NearestNeighborResampler)
{
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
height,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span destRow = destination.GetPixelRowSpan(y);
@@ -120,10 +121,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
using (Buffer2D yBuffer = memoryAllocator.Allocate2D(yLength, height))
using (Buffer2D xBuffer = memoryAllocator.Allocate2D(xLength, height))
{
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
height,
- configuration.ParallelOptions,
+ configuration,
y =>
{
ref TPixel destRowRef = ref MemoryMarshal.GetReference(destination.GetPixelRowSpan(y));
diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs
index 9c09b6a22..fd3c34d6c 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs
@@ -10,6 +10,7 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
@@ -270,10 +271,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
float widthFactor = sourceRectangle.Width / (float)this.ResizeRectangle.Width;
float heightFactor = sourceRectangle.Height / (float)this.ResizeRectangle.Height;
- Parallel.For(
+ ParallelFor.WithConfiguration(
minY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
// Y coordinates of source points
@@ -331,10 +332,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
});
// Now process the rows.
- Parallel.For(
+ ParallelFor.WithConfiguration(
minY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
{
// Ensure offsets are normalized for cropping and padding.
diff --git a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs
index d57e9cbd9..b18d882c2 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/RotateProcessor.cs
@@ -147,10 +147,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int height = source.Height;
Rectangle destinationBounds = destination.Bounds();
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
height,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span sourceRow = source.GetPixelRowSpan(y);
@@ -179,10 +179,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int width = source.Width;
int height = source.Height;
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
height,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span sourceRow = source.GetPixelRowSpan(y);
@@ -207,10 +207,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms
int height = source.Height;
Rectangle destinationBounds = destination.Bounds();
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
height,
- configuration.ParallelOptions,
+ configuration,
y =>
{
Span sourceRow = source.GetPixelRowSpan(y);
diff --git a/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs b/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs
index 3983ea091..68133a548 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/WeightsBuffer.cs
@@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0.
using System;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.Processing.Processors.Transforms
diff --git a/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs b/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs
index 6a2b6fbd1..01cf97e59 100644
--- a/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs
+++ b/src/ImageSharp/Processing/Processors/Transforms/WeightsWindow.cs
@@ -6,6 +6,8 @@ using System.Buffers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
namespace SixLabors.ImageSharp.Processing.Processors.Transforms
diff --git a/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs b/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs
index 8bf87fb62..d55c231a7 100644
--- a/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs
+++ b/tests/ImageSharp.Benchmarks/Codecs/CopyPixels.cs
@@ -1,19 +1,17 @@
-//
-// Copyright (c) James Jackson-South and contributors.
+// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
-//
+using System;
+using System.Threading.Tasks;
+
+using BenchmarkDotNet.Attributes;
+
+using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Benchmarks.Codecs
{
- using System;
- using System.Threading.Tasks;
-
- using BenchmarkDotNet.Attributes;
- using SixLabors.ImageSharp.Advanced;
- using SixLabors.Memory;
-
public class CopyPixels : BenchmarkBase
{
[Benchmark(Baseline = true, Description = "PixelAccessor Copy by indexer")]
@@ -24,10 +22,10 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
{
Buffer2D sourcePixels = source.GetRootFramePixelBuffer();
Buffer2D targetPixels = target.GetRootFramePixelBuffer();
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
source.Height,
- Configuration.Default.ParallelOptions,
+ Configuration.Default,
y =>
{
for (int x = 0; x < source.Width; x++)
@@ -48,10 +46,10 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
{
Buffer2D sourcePixels = source.GetRootFramePixelBuffer();
Buffer2D targetPixels = target.GetRootFramePixelBuffer();
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
source.Height,
- Configuration.Default.ParallelOptions,
+ Configuration.Default,
y =>
{
Span sourceRow = sourcePixels.GetRowSpan(y);
@@ -73,17 +71,17 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
using (var source = new Image(1024, 768))
using (var target = new Image(1024, 768))
{
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
source.Height,
- Configuration.Default.ParallelOptions,
+ Configuration.Default,
y =>
- {
- for (int x = 0; x < source.Width; x++)
{
- target[x, y] = source[x, y];
- }
- });
+ for (int x = 0; x < source.Width; x++)
+ {
+ target[x, y] = source[x, y];
+ }
+ });
return target[0, 0];
}
@@ -95,23 +93,23 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs
using (var source = new Image(1024, 768))
using (var target = new Image(1024, 768))
{
- Parallel.For(
+ ParallelFor.WithConfiguration(
0,
source.Height,
- Configuration.Default.ParallelOptions,
+ Configuration.Default,
y =>
- {
- Span sourceRow = source.Frames.RootFrame.GetPixelRowSpan(y);
- Span targetRow = target.Frames.RootFrame.GetPixelRowSpan(y);
-
- for (int x = 0; x < source.Width; x++)
{
- targetRow[x] = sourceRow[x];
- }
- });
+ Span sourceRow = source.Frames.RootFrame.GetPixelRowSpan(y);
+ Span targetRow = target.Frames.RootFrame.GetPixelRowSpan(y);
+
+ for (int x = 0; x < source.Width; x++)
+ {
+ targetRow[x] = sourceRow[x];
+ }
+ });
return target[0, 0];
}
}
}
-}
+}
\ No newline at end of file
diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/LoadResizeSave.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/LoadResizeSave.cs
index 1d485ee08..77ed828ef 100644
--- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/LoadResizeSave.cs
+++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/LoadResizeSave.cs
@@ -37,7 +37,7 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg
[GlobalSetup]
public void Setup()
{
- this.configuration.ParallelOptions.MaxDegreeOfParallelism =
+ this.configuration.MaxDegreeOfParallelism =
this.EnableParallelExecution ? Environment.ProcessorCount : 1;
if (this.sourceBytes == null)
diff --git a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/YCbCrColorConversion.cs b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/YCbCrColorConversion.cs
index 5a8a62373..05edd2791 100644
--- a/tests/ImageSharp.Benchmarks/Codecs/Jpeg/YCbCrColorConversion.cs
+++ b/tests/ImageSharp.Benchmarks/Codecs/Jpeg/YCbCrColorConversion.cs
@@ -1,14 +1,16 @@
-using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters;
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
-namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg
-{
- using System;
- using System.Numerics;
+using System;
+using System.Numerics;
- using BenchmarkDotNet.Attributes;
+using BenchmarkDotNet.Attributes;
- using SixLabors.Memory;
+using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters;
+using SixLabors.ImageSharp.Memory;
+namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg
+{
[Config(typeof(Config.ShortClr))]
public class YCbCrColorConversion
{
@@ -81,6 +83,5 @@ namespace SixLabors.ImageSharp.Benchmarks.Codecs.Jpeg
return buffers;
}
-
}
}
\ No newline at end of file
diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4.cs
index 6f4195d6f..a5fa59ba0 100644
--- a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4.cs
+++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4.cs
@@ -1,3 +1,6 @@
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+
// ReSharper disable InconsistentNaming
using System.Buffers;
@@ -7,7 +10,7 @@ using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
-using SixLabors.Memory;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk
diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs
index 33ad9203c..7e7dfb365 100644
--- a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs
+++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs
@@ -1,3 +1,6 @@
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+
// ReSharper disable InconsistentNaming
using System.Buffers;
@@ -5,7 +8,7 @@ using System;
using BenchmarkDotNet.Attributes;
-using SixLabors.Memory;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk
diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs
index 75ca1206e..50fac2513 100644
--- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs
+++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs
@@ -1,3 +1,6 @@
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+
// ReSharper disable InconsistentNaming
using System.Buffers;
@@ -6,7 +9,7 @@ using System.Numerics;
using BenchmarkDotNet.Attributes;
-using SixLabors.Memory;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk
diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs
index be1ff72d5..4e9c6d10a 100644
--- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs
+++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs
@@ -1,3 +1,6 @@
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+
// ReSharper disable InconsistentNaming
using System.Buffers;
@@ -5,7 +8,7 @@ using System;
using BenchmarkDotNet.Attributes;
-using SixLabors.Memory;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Benchmarks.ColorSpaces.Bulk
diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs
index 799be60cc..8166c8f46 100644
--- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs
+++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs
@@ -1,8 +1,11 @@
-using System;
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+
+using System;
using System.Buffers;
using BenchmarkDotNet.Attributes;
-using SixLabors.Memory;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
// ReSharper disable InconsistentNaming
diff --git a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj
index 3f67f175f..35a3a67e9 100644
--- a/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj
+++ b/tests/ImageSharp.Benchmarks/ImageSharp.Benchmarks.csproj
@@ -18,7 +18,7 @@
-
+
diff --git a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs
index 3133e0b36..d9cf65d4b 100644
--- a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs
+++ b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs
@@ -1,32 +1,35 @@
-//
-// Copyright (c) James Jackson-South and contributors.
+// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
-//
+using System;
using System.Buffers;
+using System.Numerics;
+
+using BenchmarkDotNet.Attributes;
+
+using SixLabors.ImageSharp.Memory;
+using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.ImageSharp.PixelFormats.PixelBlenders;
namespace SixLabors.ImageSharp.Benchmarks
{
- using System;
-
- using BenchmarkDotNet.Attributes;
- using SixLabors.ImageSharp.PixelFormats;
using CoreSize = SixLabors.Primitives.Size;
- using System.Numerics;
-
- using SixLabors.Memory;
- using SixLabors.ImageSharp.PixelFormats.PixelBlenders;
public class PorterDuffBulkVsPixel : BenchmarkBase
{
- private void BulkVectorConvert(Span destination, Span background, Span source, Span 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));
Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length));
Guard.MustBeGreaterThanOrEqualTo(amount.Length, destination.Length, nameof(amount.Length));
- using (IMemoryOwner buffer = Configuration.Default.MemoryAllocator.Allocate(destination.Length * 3))
+ using (IMemoryOwner buffer =
+ Configuration.Default.MemoryAllocator.Allocate(destination.Length * 3))
{
Span destinationSpan = buffer.Slice(0, destination.Length);
Span backgroundSpan = buffer.Slice(destination.Length, destination.Length);
@@ -43,8 +46,13 @@ namespace SixLabors.ImageSharp.Benchmarks
PixelOperations.Instance.PackFromVector4(destinationSpan, destination, destination.Length);
}
}
- private void BulkPixelConvert(Span destination, Span background, Span source, Span amount)
- where TPixel : struct, IPixel
+
+ private void BulkPixelConvert(
+ Span destination,
+ Span background,
+ Span source,
+ Span amount)
+ where TPixel : struct, IPixel
{
Guard.MustBeGreaterThanOrEqualTo(destination.Length, background.Length, nameof(destination));
Guard.MustBeGreaterThanOrEqualTo(source.Length, background.Length, nameof(destination));
@@ -97,4 +105,4 @@ namespace SixLabors.ImageSharp.Benchmarks
}
}
}
-}
+}
\ No newline at end of file
diff --git a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs
index f7f54f4eb..fe1d4221d 100644
--- a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs
+++ b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs
@@ -1,29 +1,27 @@
-//
-// Copyright (c) James Jackson-South and contributors.
+// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
-//
+using System;
using System.Buffers;
+using System.Numerics;
+using System.Threading.Tasks;
-namespace SixLabors.ImageSharp.Benchmarks
-{
+using BenchmarkDotNet.Attributes;
- using BenchmarkDotNet.Attributes;
- using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.ImageSharp.Memory;
+using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.ImageSharp.Processing.Processors;
+using SixLabors.ImageSharp.Processing.Processors.Overlays;
+using SixLabors.Primitives;
+namespace SixLabors.ImageSharp.Benchmarks
+{
using CoreSize = SixLabors.Primitives.Size;
- using System.Numerics;
- using System;
- using System.Threading.Tasks;
-
- using SixLabors.Memory;
- using SixLabors.Primitives;
- using SixLabors.ImageSharp.Processing.Processors.Overlays;
- using SixLabors.ImageSharp.Processing.Processors;
public class Glow : BenchmarkBase
{
private GlowProcessor bulk;
+
private GlowProcessorParallel parallel;
[GlobalSetup]
@@ -31,8 +29,8 @@ namespace SixLabors.ImageSharp.Benchmarks
{
this.bulk = new GlowProcessor(NamedColors.Beige, 800 * .5f, GraphicsOptions.Default);
this.parallel = new GlowProcessorParallel(NamedColors.Beige) { Radius = 800 * .5f, };
-
}
+
[Benchmark(Description = "ImageSharp Glow - Bulk")]
public CoreSize GlowBulk()
{
@@ -76,7 +74,10 @@ namespace SixLabors.ImageSharp.Benchmarks
public float Radius { get; set; }
///
- protected override void OnFrameApply(ImageFrame source, Rectangle sourceRectangle, Configuration configuration)
+ protected override void OnFrameApply(
+ ImageFrame source,
+ Rectangle sourceRectangle,
+ Configuration configuration)
{
int startY = sourceRectangle.Y;
int endY = sourceRectangle.Bottom;
@@ -84,7 +85,9 @@ namespace SixLabors.ImageSharp.Benchmarks
int endX = sourceRectangle.Right;
TPixel glowColor = this.GlowColor;
Vector2 centre = Rectangle.Center(sourceRectangle);
- float maxDistance = this.Radius > 0 ? Math.Min(this.Radius, sourceRectangle.Width * .5F) : sourceRectangle.Width * .5F;
+ float maxDistance = this.Radius > 0
+ ? Math.Min(this.Radius, sourceRectangle.Width * .5F)
+ : sourceRectangle.Width * .5F;
// Align start/end positions.
int minX = Math.Max(0, startX);
@@ -109,26 +112,31 @@ namespace SixLabors.ImageSharp.Benchmarks
Buffer2D sourcePixels = source.PixelBuffer;
rowColors.GetSpan().Fill(glowColor);
- Parallel.For(
+ ParallelFor.WithConfiguration(
minY,
maxY,
- configuration.ParallelOptions,
+ configuration,
y =>
- {
- int offsetY = y - startY;
-
- for (int x = minX; x < maxX; x++)
{
- int offsetX = x - startX;
- float distance = Vector2.Distance(centre, new Vector2(offsetX, offsetY));
- Vector4 sourceColor = sourcePixels[offsetX, offsetY].ToVector4();
- TPixel packed = default(TPixel);
- packed.PackFromVector4(PremultipliedLerp(sourceColor, glowColor.ToVector4(), 1 - (.95F * (distance / maxDistance))));
- sourcePixels[offsetX, offsetY] = packed;
- }
- });
+ int offsetY = y - startY;
+
+ for (int x = minX; x < maxX; x++)
+ {
+ int offsetX = x - startX;
+ float distance = Vector2.Distance(centre, new Vector2(offsetX, offsetY));
+ Vector4 sourceColor = sourcePixels[offsetX, offsetY].ToVector4();
+ TPixel packed = default(TPixel);
+ packed.PackFromVector4(
+ PremultipliedLerp(
+ sourceColor,
+ glowColor.ToVector4(),
+ 1 - (.95F * (distance / maxDistance))));
+ sourcePixels[offsetX, offsetY] = packed;
+ }
+ });
}
}
+
public static Vector4 PremultipliedLerp(Vector4 backdrop, Vector4 source, float amount)
{
amount = amount.Clamp(0, 1);
@@ -162,4 +170,4 @@ namespace SixLabors.ImageSharp.Benchmarks
}
}
}
-}
+}
\ No newline at end of file
diff --git a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs
index d4506fc6a..86dc13e91 100644
--- a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs
+++ b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs
@@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Benchmarks
[GlobalSetup]
public void Setup()
{
- this.configuration.ParallelOptions.MaxDegreeOfParallelism =
+ this.configuration.MaxDegreeOfParallelism =
this.EnableParallelExecution ? Environment.ProcessorCount : 1;
}
diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs
index d870b7bf7..1a7183df8 100644
--- a/tests/ImageSharp.Tests/ConfigurationTests.cs
+++ b/tests/ImageSharp.Tests/ConfigurationTests.cs
@@ -10,6 +10,7 @@ using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.PixelFormats;
using Moq;
using Xunit;
+// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests
{
@@ -45,15 +46,6 @@ namespace SixLabors.ImageSharp.Tests
Assert.True(Configuration.Default != null);
}
- ///
- /// Test that the default configuration parallel options is not null.
- ///
- [Fact]
- public void TestDefaultConfigurationParallelOptionsIsNotNull()
- {
- Assert.True(Configuration.Default.ParallelOptions != null);
- }
-
///
/// Test that the default configuration read origin options is set to begin.
///
@@ -70,9 +62,22 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void TestDefaultConfigurationMaxDegreeOfParallelism()
{
- Assert.True(Configuration.Default.ParallelOptions.MaxDegreeOfParallelism == Environment.ProcessorCount);
+ Assert.True(Configuration.Default.MaxDegreeOfParallelism == Environment.ProcessorCount);
+
+ var cfg = new Configuration();
+ Assert.True(cfg.MaxDegreeOfParallelism == Environment.ProcessorCount);
}
+ [Theory]
+ [InlineData(0)]
+ [InlineData(-42)]
+ public void Set_MaxDegreeOfParallelism_ToNonPositiveValue_Throws(int value)
+ {
+ var cfg = new Configuration();
+ Assert.Throws(() => cfg.MaxDegreeOfParallelism = value);
+ }
+
+
[Fact]
public void ConstructorCallConfigureOnFormatProvider()
{
diff --git a/tests/ImageSharp.Tests/Drawing/BeziersTests.cs b/tests/ImageSharp.Tests/Drawing/BeziersTests.cs
index 443b49c7c..69b2098dc 100644
--- a/tests/ImageSharp.Tests/Drawing/BeziersTests.cs
+++ b/tests/ImageSharp.Tests/Drawing/BeziersTests.cs
@@ -3,9 +3,10 @@
using System.Numerics;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
-using SixLabors.Memory;
+
using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
diff --git a/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs b/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs
index 96af63fd5..0d791fbd2 100644
--- a/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs
+++ b/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs
@@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0.
using System.Numerics;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.Shapes;
@@ -9,8 +11,6 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
- using SixLabors.Memory;
-
public class DrawPathTests : FileTestBase
{
[Fact]
diff --git a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs
index f13f808b6..93715c586 100644
--- a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs
+++ b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs
@@ -2,10 +2,12 @@
// Licensed under the Apache License, Version 2.0.
using System;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives;
using SixLabors.ImageSharp.Processing;
-using SixLabors.Memory;
+
using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
diff --git a/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs
index d3b39709a..d827975c7 100644
--- a/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs
+++ b/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs
@@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0.
using System.Numerics;
+
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.Shapes;
@@ -10,8 +12,6 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
- using SixLabors.Memory;
-
public class LineComplexPolygonTests : FileTestBase
{
[Fact]
diff --git a/tests/ImageSharp.Tests/Drawing/LineTests.cs b/tests/ImageSharp.Tests/Drawing/LineTests.cs
index 747c75cde..43dec547e 100644
--- a/tests/ImageSharp.Tests/Drawing/LineTests.cs
+++ b/tests/ImageSharp.Tests/Drawing/LineTests.cs
@@ -3,6 +3,7 @@
using System.Numerics;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
@@ -10,8 +11,6 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
- using SixLabors.Memory;
-
public class LineTests : FileTestBase
{
[Fact]
diff --git a/tests/ImageSharp.Tests/Drawing/PolygonTests.cs b/tests/ImageSharp.Tests/Drawing/PolygonTests.cs
index f9a41baba..6ea9c647f 100644
--- a/tests/ImageSharp.Tests/Drawing/PolygonTests.cs
+++ b/tests/ImageSharp.Tests/Drawing/PolygonTests.cs
@@ -3,6 +3,7 @@
using System.Numerics;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
using SixLabors.ImageSharp.Processing;
@@ -10,8 +11,6 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
- using SixLabors.Memory;
-
public class PolygonTests : FileTestBase
{
[Fact]
diff --git a/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs
index c8d3fe1bc..2c9628e84 100644
--- a/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs
+++ b/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs
@@ -3,6 +3,7 @@
using System.Numerics;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.Shapes;
@@ -11,8 +12,6 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
- using SixLabors.Memory;
-
public class SolidComplexPolygonTests : FileTestBase
{
[Fact]
diff --git a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs
index e42b4b481..d7fb0a3d3 100644
--- a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs
+++ b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs
@@ -4,6 +4,7 @@
using System;
using System.Numerics;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.Shapes;
@@ -12,8 +13,6 @@ using Xunit;
namespace SixLabors.ImageSharp.Tests.Drawing
{
- using SixLabors.Memory;
-
public class SolidPolygonTests : FileTestBase
{
[Fact]
@@ -184,7 +183,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
string path = TestEnvironment.CreateOutputDirectory("Drawing", "FilledPolygons");
var config = Configuration.CreateDefaultInstance();
- config.ParallelOptions.MaxDegreeOfParallelism = 1;
+ config.MaxDegreeOfParallelism = 1;
using (var image = new Image(config, 100, 100))
{
image.Mutate(x => x
@@ -200,7 +199,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
string path = TestEnvironment.CreateOutputDirectory("Drawing", "FilledPolygons");
var config = Configuration.CreateDefaultInstance();
- config.ParallelOptions.MaxDegreeOfParallelism = 1;
+ config.MaxDegreeOfParallelism = 1;
using (var image = new Image(config, 100, 100))
{
image.Mutate(x => x
@@ -217,7 +216,7 @@ namespace SixLabors.ImageSharp.Tests.Drawing
string path = TestEnvironment.CreateOutputDirectory("Drawing", "FilledPolygons");
var config = Configuration.CreateDefaultInstance();
- config.ParallelOptions.MaxDegreeOfParallelism = 1;
+ config.MaxDegreeOfParallelism = 1;
using (var image = new Image(config, 200, 200))
{
image.Mutate(x => x
diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs
index 88be54dd0..c720fdd4a 100644
--- a/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs
+++ b/tests/ImageSharp.Tests/Formats/Jpg/Block8x8FTests.CopyToBufferArea.cs
@@ -5,7 +5,7 @@
//#define BENCHMARKING
using SixLabors.ImageSharp.Formats.Jpeg.Components;
-using SixLabors.Memory;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Tests.Formats.Jpg.Utils;
using SixLabors.Primitives;
diff --git a/tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs b/tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs
index 956ade502..dedb094bc 100644
--- a/tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs
+++ b/tests/ImageSharp.Tests/Formats/Jpg/GenericBlock8x8Tests.cs
@@ -4,14 +4,13 @@
using System;
using SixLabors.ImageSharp.Formats.Jpeg.Components;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using Xunit;
namespace SixLabors.ImageSharp.Tests.Formats.Jpg
{
- using SixLabors.Memory;
-
public class GenericBlock8x8Tests
{
public static Image CreateTestImage()
diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs
index 8048dd424..a80312766 100644
--- a/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs
+++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegColorConverterTests.cs
@@ -8,7 +8,7 @@ using SixLabors.ImageSharp.ColorSpaces;
using SixLabors.ImageSharp.ColorSpaces.Conversion;
using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder.ColorConverters;
-using SixLabors.Memory;
+using SixLabors.ImageSharp.Memory;
using Xunit;
using Xunit.Abstractions;
@@ -290,7 +290,8 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
}
// no need to dispose when buffer is not array owner
- var source = new MemorySource(new BasicArrayBuffer(values), true);
+ var memory = new Memory(values);
+ var source = new MemorySource(memory);
buffers[i] = new Buffer2D(source, values.Length, 1);
}
return new JpegColorConverter.ComponentValues(buffers, 0);
diff --git a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs
index a10deb983..57d92fa15 100644
--- a/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs
+++ b/tests/ImageSharp.Tests/Formats/Jpg/Utils/LibJpegTools.ComponentData.cs
@@ -1,11 +1,14 @@
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+
using System;
using System.Linq;
using System.Numerics;
using SixLabors.ImageSharp.Formats.Jpeg.Components;
using SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder;
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
-using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Tests.Formats.Jpg.Utils
diff --git a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs
index 815684d84..69572425c 100644
--- a/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs
+++ b/tests/ImageSharp.Tests/Image/ImageTests.WrapMemory.cs
@@ -8,6 +8,7 @@ using System.Drawing.Imaging;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
+using SixLabors.ImageSharp.Common.Helpers;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Shapes;
@@ -102,6 +103,11 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void WrapSystemDrawingBitmap_WhenObserved()
{
+ if (ShouldSkipBitmapTest)
+ {
+ return;
+ }
+
using (var bmp = new Bitmap(51, 23))
{
using (var memoryManager = new BitmapMemoryManager(bmp))
@@ -130,6 +136,11 @@ namespace SixLabors.ImageSharp.Tests
[Fact]
public void WrapSystemDrawingBitmap_WhenOwned()
{
+ if (ShouldSkipBitmapTest)
+ {
+ return;
+ }
+
using (var bmp = new Bitmap(51, 23))
{
var memoryManager = new BitmapMemoryManager(bmp);
@@ -151,6 +162,9 @@ namespace SixLabors.ImageSharp.Tests
bmp.Save(fn, ImageFormat.Bmp);
}
}
+
+ private static bool ShouldSkipBitmapTest =>
+ !TestEnvironment.Is64BitProcess || TestHelpers.ImageSharpBuiltAgainst != "netcoreapp2.1";
}
}
}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs b/tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs
deleted file mode 100644
index 89bb9d95f..000000000
--- a/tests/ImageSharp.Tests/Memory/ArrayPoolMemoryManagerTests.cs
+++ /dev/null
@@ -1,239 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-// ReSharper disable InconsistentNaming
-
-using System.Buffers;
-
-using SixLabors.ImageSharp.PixelFormats;
-
-namespace SixLabors.ImageSharp.Tests.Memory
-{
- using System;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
-
- using SixLabors.Memory;
-
- using Xunit;
-
- public class ArrayPoolMemoryManagerTests
- {
- private const int MaxPooledBufferSizeInBytes = 2048;
-
- private const int PoolSelectorThresholdInBytes = MaxPooledBufferSizeInBytes / 2;
-
- private MemoryAllocator MemoryAllocator { get; set; } = new ArrayPoolMemoryAllocator(MaxPooledBufferSizeInBytes, PoolSelectorThresholdInBytes);
-
- ///
- /// Rent a buffer -> return it -> re-rent -> verify if it's span points to the previous location
- ///
- private bool CheckIsRentingPooledBuffer(int length)
- where T : struct
- {
- IMemoryOwner buffer = this.MemoryAllocator.Allocate(length);
- ref T ptrToPrevPosition0 = ref buffer.GetReference();
- buffer.Dispose();
-
- buffer = this.MemoryAllocator.Allocate(length);
- bool sameBuffers = Unsafe.AreSame(ref ptrToPrevPosition0, ref buffer.GetReference());
- buffer.Dispose();
-
- return sameBuffers;
- }
-
- public class BufferTests : BufferTestSuite
- {
- public BufferTests()
- : base(new ArrayPoolMemoryAllocator(MaxPooledBufferSizeInBytes, PoolSelectorThresholdInBytes))
- {
- }
- }
-
- public class Constructor
- {
- [Fact]
- public void WhenBothParametersPassedByUser()
- {
- var mgr = new ArrayPoolMemoryAllocator(1111, 666);
- Assert.Equal(1111, mgr.MaxPoolSizeInBytes);
- Assert.Equal(666, mgr.PoolSelectorThresholdInBytes);
- }
-
- [Fact]
- public void WhenPassedOnly_MaxPooledBufferSizeInBytes_SmallerThresholdValueIsAutoCalculated()
- {
- var mgr = new ArrayPoolMemoryAllocator(5000);
- Assert.Equal(5000, mgr.MaxPoolSizeInBytes);
- Assert.True(mgr.PoolSelectorThresholdInBytes < mgr.MaxPoolSizeInBytes);
- }
-
- [Fact]
- public void When_PoolSelectorThresholdInBytes_IsGreaterThan_MaxPooledBufferSizeInBytes_ExceptionIsThrown()
- {
- Assert.ThrowsAny(() => { new ArrayPoolMemoryAllocator(100, 200); });
- }
- }
-
- [StructLayout(LayoutKind.Explicit, Size = MaxPooledBufferSizeInBytes / 5)]
- struct LargeStruct
- {
- }
-
- [Theory]
- [InlineData(32)]
- [InlineData(512)]
- [InlineData(MaxPooledBufferSizeInBytes - 1)]
- public void SmallBuffersArePooled_OfByte(int size)
- {
- Assert.True(this.CheckIsRentingPooledBuffer(size));
- }
-
-
- [Theory]
- [InlineData(128 * 1024 * 1024)]
- [InlineData(MaxPooledBufferSizeInBytes + 1)]
- public void LargeBuffersAreNotPooled_OfByte(int size)
- {
- if (!TestEnvironment.Is64BitProcess)
- {
- // can lead to OutOfMemoryException
- return;
- }
-
- Assert.False(this.CheckIsRentingPooledBuffer(size));
- }
-
- [Fact]
- public unsafe void SmallBuffersArePooled_OfBigValueType()
- {
- int count = MaxPooledBufferSizeInBytes / sizeof(LargeStruct) - 1;
-
- Assert.True(this.CheckIsRentingPooledBuffer(count));
- }
-
- [Fact]
- public unsafe void LaregeBuffersAreNotPooled_OfBigValueType()
- {
- if (!TestEnvironment.Is64BitProcess)
- {
- // can lead to OutOfMemoryException
- return;
- }
-
- int count = MaxPooledBufferSizeInBytes / sizeof(LargeStruct) + 1;
-
- Assert.False(this.CheckIsRentingPooledBuffer(count));
- }
-
- [Theory]
- [InlineData(AllocationOptions.None)]
- [InlineData(AllocationOptions.Clean)]
- public void CleaningRequests_AreControlledByAllocationParameter_Clean(AllocationOptions options)
- {
- using (IMemoryOwner firstAlloc = this.MemoryAllocator.Allocate(42))
- {
- firstAlloc.GetSpan().Fill(666);
- }
-
- using (IMemoryOwner secondAlloc = this.MemoryAllocator.Allocate(42, options))
- {
- int expected = options == AllocationOptions.Clean ? 0 : 666;
- Assert.Equal(expected, secondAlloc.GetSpan()[0]);
- }
- }
-
- [Theory]
- [InlineData(false)]
- [InlineData(true)]
- public void ReleaseRetainedResources_ReplacesInnerArrayPool(bool keepBufferAlive)
- {
- IMemoryOwner buffer = this.MemoryAllocator.Allocate(32);
- ref int ptrToPrev0 = ref MemoryMarshal.GetReference(buffer.GetSpan());
-
- if (!keepBufferAlive)
- {
- buffer.Dispose();
- }
-
- this.MemoryAllocator.ReleaseRetainedResources();
-
- buffer = this.MemoryAllocator.Allocate(32);
-
- Assert.False(Unsafe.AreSame(ref ptrToPrev0, ref buffer.GetReference()));
- }
-
- [Fact]
- public void ReleaseRetainedResources_DisposingPreviouslyAllocatedBuffer_IsAllowed()
- {
- IMemoryOwner buffer = this.MemoryAllocator.Allocate(32);
- this.MemoryAllocator.ReleaseRetainedResources();
- buffer.Dispose();
- }
-
- [Fact]
- public void AllocationOverLargeArrayThreshold_UsesDifferentPool()
- {
- if (!TestEnvironment.Is64BitProcess)
- {
- // can lead to OutOfMemoryException
- return;
- }
-
- int arrayLengthThreshold = PoolSelectorThresholdInBytes / sizeof(int);
-
- IMemoryOwner small = this.MemoryAllocator.Allocate(arrayLengthThreshold - 1);
- ref int ptr2Small = ref small.GetReference();
- small.Dispose();
-
- IMemoryOwner large = this.MemoryAllocator.Allocate(arrayLengthThreshold + 1);
-
- Assert.False(Unsafe.AreSame(ref ptr2Small, ref large.GetReference()));
- }
-
- [Fact]
- public void CreateWithAggressivePooling()
- {
- if (!TestEnvironment.Is64BitProcess)
- {
- // can lead to OutOfMemoryException
- return;
- }
-
- this.MemoryAllocator = ArrayPoolMemoryAllocator.CreateWithAggressivePooling();
-
- Assert.True(this.CheckIsRentingPooledBuffer(4096 * 4096));
- }
-
- [Fact]
- public void CreateDefault()
- {
- if (!TestEnvironment.Is64BitProcess)
- {
- // can lead to OutOfMemoryException
- return;
- }
-
- this.MemoryAllocator = ArrayPoolMemoryAllocator.CreateDefault();
-
- Assert.False(this.CheckIsRentingPooledBuffer(2 * 4096 * 4096));
- Assert.True(this.CheckIsRentingPooledBuffer(2048 * 2048));
- }
-
- [Fact]
- public void CreateWithModeratePooling()
- {
- if (!TestEnvironment.Is64BitProcess)
- {
- // can lead to OutOfMemoryException
- return;
- }
-
- this.MemoryAllocator = ArrayPoolMemoryAllocator.CreateWithModeratePooling();
-
- Assert.False(this.CheckIsRentingPooledBuffer(2048 * 2048));
- Assert.True(this.CheckIsRentingPooledBuffer(1024 * 16));
- }
- }
-}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
index 5753d92b3..19ec725f2 100644
--- a/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
+++ b/tests/ImageSharp.Tests/Memory/Buffer2DTests.cs
@@ -6,7 +6,7 @@ using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
-using SixLabors.ImageSharp.PixelFormats;
+using SixLabors.ImageSharp.Memory;
using SixLabors.Memory;
using SixLabors.Primitives;
diff --git a/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs b/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs
index bbf350514..dc735e41b 100644
--- a/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs
+++ b/tests/ImageSharp.Tests/Memory/BufferAreaTests.cs
@@ -1,13 +1,13 @@
+// Copyright (c) Six Labors and contributors.
+// Licensed under the Apache License, Version 2.0.
+using System;
+using SixLabors.ImageSharp.Memory;
+using SixLabors.Primitives;
+using Xunit;
+
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests.Memory
{
- using System;
-
- using SixLabors.Memory;
- using SixLabors.Primitives;
-
- using Xunit;
-
public class BufferAreaTests
{
[Fact]
@@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
{
using (var buffer = Configuration.Default.MemoryAllocator.Allocate2D(10, 20))
{
- var rectangle = new Rectangle(3,2, 5, 6);
+ var rectangle = new Rectangle(3, 2, 5, 6);
var area = new BufferArea(buffer, rectangle);
Assert.Equal(buffer, area.DestinationBuffer);
@@ -33,9 +33,10 @@ namespace SixLabors.ImageSharp.Tests.Memory
buffer[x, y] = y * 100 + x;
}
}
+
return buffer;
}
-
+
[Theory]
[InlineData(2, 3, 2, 2)]
[InlineData(5, 4, 3, 2)]
@@ -44,7 +45,7 @@ namespace SixLabors.ImageSharp.Tests.Memory
using (Buffer2D buffer = CreateTestBuffer(20, 30))
{
Rectangle r = new Rectangle(rx, ry, 5, 6);
-
+
BufferArea area = buffer.GetArea(r);
int value = area[x, y];
diff --git a/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs b/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs
deleted file mode 100644
index e57c13164..000000000
--- a/tests/ImageSharp.Tests/Memory/BufferTestSuite.cs
+++ /dev/null
@@ -1,320 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-using System;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-using SixLabors.Memory;
-using Xunit;
-// ReSharper disable InconsistentNaming
-
-namespace SixLabors.ImageSharp.Tests.Memory
-{
- using System.Buffers;
-
- ///
- /// Inherit this class to test an implementation (provided by ).
- ///
- public abstract class BufferTestSuite
- {
- protected BufferTestSuite(MemoryAllocator memoryAllocator)
- {
- this.MemoryAllocator = memoryAllocator;
- }
-
- protected MemoryAllocator MemoryAllocator { get; }
-
- public struct CustomStruct : IEquatable
- {
- public long A;
-
- public byte B;
-
- public float C;
-
- public CustomStruct(long a, byte b, float c)
- {
- this.A = a;
- this.B = b;
- this.C = c;
- }
-
- public bool Equals(CustomStruct other)
- {
- return this.A == other.A && this.B == other.B && this.C.Equals(other.C);
- }
-
- public override bool Equals(object obj)
- {
- return obj is CustomStruct other && this.Equals(other);
- }
-
- public override int GetHashCode()
- {
- unchecked
- {
- int hashCode = this.A.GetHashCode();
- hashCode = (hashCode * 397) ^ this.B.GetHashCode();
- hashCode = (hashCode * 397) ^ this.C.GetHashCode();
- return hashCode;
- }
- }
- }
-
- public static readonly TheoryData LenthValues = new TheoryData { 0, 1, 7, 1023, 1024 };
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void HasCorrectLength_byte(int desiredLength)
- {
- this.TestHasCorrectLength(desiredLength);
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void HasCorrectLength_float(int desiredLength)
- {
- this.TestHasCorrectLength(desiredLength);
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void HasCorrectLength_CustomStruct(int desiredLength)
- {
- this.TestHasCorrectLength(desiredLength);
- }
-
- private void TestHasCorrectLength(int desiredLength)
- where T : struct
- {
- using (IMemoryOwner buffer = this.MemoryAllocator.Allocate(desiredLength))
- {
- Assert.Equal(desiredLength, buffer.GetSpan().Length);
- }
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void CanAllocateCleanBuffer_byte(int desiredLength)
- {
- this.TestCanAllocateCleanBuffer(desiredLength, false);
- this.TestCanAllocateCleanBuffer(desiredLength, true);
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void CanAllocateCleanBuffer_double(int desiredLength)
- {
- this.TestCanAllocateCleanBuffer(desiredLength);
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void CanAllocateCleanBuffer_CustomStruct(int desiredLength)
- {
- this.TestCanAllocateCleanBuffer(desiredLength);
- }
-
- private IMemoryOwner Allocate(int desiredLength, AllocationOptions options, bool managedByteBuffer)
- where T : struct
- {
- if (managedByteBuffer)
- {
- if (!(this.MemoryAllocator.AllocateManagedByteBuffer(desiredLength, options) is IMemoryOwner buffer))
- {
- throw new InvalidOperationException("typeof(T) != typeof(byte)");
- }
-
- return buffer;
- }
-
- return this.MemoryAllocator.Allocate(desiredLength, options);
- }
-
- private void TestCanAllocateCleanBuffer(int desiredLength, bool testManagedByteBuffer = false)
- where T : struct, IEquatable
- {
- ReadOnlySpan expected = new T[desiredLength];
-
- for (int i = 0; i < 10; i++)
- {
- using (IMemoryOwner buffer = this.Allocate(desiredLength, AllocationOptions.Clean, testManagedByteBuffer))
- {
- Assert.True(buffer.GetSpan().SequenceEqual(expected));
- }
- }
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void SpanPropertyIsAlwaysTheSame_int(int desiredLength)
- {
- this.TestSpanPropertyIsAlwaysTheSame(desiredLength);
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void SpanPropertyIsAlwaysTheSame_byte(int desiredLength)
- {
- this.TestSpanPropertyIsAlwaysTheSame(desiredLength, false);
- this.TestSpanPropertyIsAlwaysTheSame(desiredLength, true);
- }
-
- private void TestSpanPropertyIsAlwaysTheSame(int desiredLength, bool testManagedByteBuffer = false)
- where T : struct
- {
- using (IMemoryOwner buffer = this.Allocate(desiredLength, AllocationOptions.None, testManagedByteBuffer))
- {
- ref T a = ref MemoryMarshal.GetReference(buffer.GetSpan());
- ref T b = ref MemoryMarshal.GetReference(buffer.GetSpan());
- ref T c = ref MemoryMarshal.GetReference(buffer.GetSpan());
-
- Assert.True(Unsafe.AreSame(ref a, ref b));
- Assert.True(Unsafe.AreSame(ref b, ref c));
- }
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void WriteAndReadElements_float(int desiredLength)
- {
- this.TestWriteAndReadElements(desiredLength, x => x * 1.2f);
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void WriteAndReadElements_byte(int desiredLength)
- {
- this.TestWriteAndReadElements(desiredLength, x => (byte)(x+1), false);
- this.TestWriteAndReadElements(desiredLength, x => (byte)(x + 1), true);
- }
-
- private void TestWriteAndReadElements(int desiredLength, Func getExpectedValue, bool testManagedByteBuffer = false)
- where T : struct
- {
- using (IMemoryOwner buffer = this.Allocate(desiredLength, AllocationOptions.None, testManagedByteBuffer))
- {
- T[] expectedVals = new T[buffer.Length()];
-
- for (int i = 0; i < buffer.Length(); i++)
- {
- Span span = buffer.GetSpan();
- expectedVals[i] = getExpectedValue(i);
- span[i] = expectedVals[i];
- }
-
- for (int i = 0; i < buffer.Length(); i++)
- {
- Span span = buffer.GetSpan();
- Assert.Equal(expectedVals[i], span[i]);
- }
- }
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void IndexingSpan_WhenOutOfRange_Throws_byte(int desiredLength)
- {
- this.TestIndexOutOfRangeShouldThrow(desiredLength, false);
- this.TestIndexOutOfRangeShouldThrow(desiredLength, true);
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void IndexingSpan_WhenOutOfRange_Throws_long(int desiredLength)
- {
- this.TestIndexOutOfRangeShouldThrow(desiredLength);
- }
-
- [Theory]
- [MemberData(nameof(LenthValues))]
- public void IndexingSpan_WhenOutOfRange_Throws_CustomStruct(int desiredLength)
- {
- this.TestIndexOutOfRangeShouldThrow(desiredLength);
- }
-
- private T TestIndexOutOfRangeShouldThrow(int desiredLength, bool testManagedByteBuffer = false)
- where T : struct, IEquatable
- {
- var dummy = default(T);
-
- using (IMemoryOwner buffer = this.Allocate(desiredLength, AllocationOptions.None, testManagedByteBuffer))
- {
- Assert.ThrowsAny(
- () =>
- {
- Span span = buffer.GetSpan();
- dummy = span[desiredLength];
- });
-
- Assert.ThrowsAny(
- () =>
- {
- Span span = buffer.GetSpan();
- dummy = span[desiredLength + 1];
- });
-
- Assert.ThrowsAny(
- () =>
- {
- Span span = buffer.GetSpan();
- dummy = span[desiredLength + 42];
- });
- }
-
- return dummy;
- }
-
- [Theory]
- [InlineData(1)]
- [InlineData(7)]
- [InlineData(1024)]
- [InlineData(6666)]
- public void ManagedByteBuffer_ArrayIsCorrect(int desiredLength)
- {
- using (IManagedByteBuffer buffer = this.MemoryAllocator.AllocateManagedByteBuffer(desiredLength))
- {
- ref byte array0 = ref buffer.Array[0];
- ref byte span0 = ref buffer.GetReference();
-
- Assert.True(Unsafe.AreSame(ref span0, ref array0));
- Assert.True(buffer.Array.Length >= buffer.GetSpan().Length);
- }
- }
-
- [Fact]
- public void GetMemory_ReturnsValidMemory()
- {
- using (IMemoryOwner buffer = this.MemoryAllocator.Allocate(42))
- {
- Span span0 = buffer.GetSpan();
- span0[10].A = 30;
- Memory memory = buffer.Memory;
-
- Assert.Equal(42, memory.Length);
- Span span1 = memory.Span;
-
- Assert.Equal(42, span1.Length);
- Assert.Equal(30, span1[10].A);
- }
- }
-
- [Fact]
- public unsafe void GetMemory_ResultIsPinnable()
- {
- using (IMemoryOwner buffer = this.MemoryAllocator.Allocate(42))
- {
- Span span0 = buffer.GetSpan();
- span0[10] = 30;
-
- Memory memory = buffer.Memory;
-
- using (MemoryHandle h = memory.Pin())
- {
- int* ptr = (int*) h.Pointer;
- Assert.Equal(30, ptr[10]);
- }
- }
- }
- }
-}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Memory/MemorySourceTests.cs b/tests/ImageSharp.Tests/Memory/MemorySourceTests.cs
index 9cdfb5635..21217d73f 100644
--- a/tests/ImageSharp.Tests/Memory/MemorySourceTests.cs
+++ b/tests/ImageSharp.Tests/Memory/MemorySourceTests.cs
@@ -3,13 +3,12 @@
using System;
using System.Buffers;
-
+using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
-
using Xunit;
-// ReSharper disable InconsistentNaming
+// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests.Memory
{
public class MemorySourceTests
diff --git a/tests/ImageSharp.Tests/Memory/SimpleGcMemoryManagerTests.cs b/tests/ImageSharp.Tests/Memory/SimpleGcMemoryManagerTests.cs
deleted file mode 100644
index d04336690..000000000
--- a/tests/ImageSharp.Tests/Memory/SimpleGcMemoryManagerTests.cs
+++ /dev/null
@@ -1,15 +0,0 @@
-namespace SixLabors.ImageSharp.Tests.Memory
-{
- using SixLabors.Memory;
-
- public class SimpleGcMemoryManagerTests
- {
- public class BufferTests : BufferTestSuite
- {
- public BufferTests()
- : base(new SimpleGcMemoryAllocator())
- {
- }
- }
- }
-}
\ No newline at end of file
diff --git a/tests/ImageSharp.Tests/Memory/SpanUtilityTests.cs b/tests/ImageSharp.Tests/Memory/SpanUtilityTests.cs
deleted file mode 100644
index 396fb4ca9..000000000
--- a/tests/ImageSharp.Tests/Memory/SpanUtilityTests.cs
+++ /dev/null
@@ -1,263 +0,0 @@
-// Copyright (c) Six Labors and contributors.
-// Licensed under the Apache License, Version 2.0.
-
-// ReSharper disable InconsistentNaming
-// ReSharper disable AccessToStaticMemberViaDerivedType
-namespace SixLabors.ImageSharp.Tests.Memory
-{
- using System;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- using Xunit;
-
- public unsafe class SpanUtilityTests
- {
- // ReSharper disable once ClassNeverInstantiated.Local
- private class Assert : Xunit.Assert
- {
- public static void SameRefs(ref T1 a, ref T2 b)
- {
- ref T1 bb = ref Unsafe.As(ref b);
-
- Assert.True(Unsafe.AreSame(ref a, ref bb), "References are not same!");
- }
- }
-
- public class SpanHelper_Copy
- {
- private static void AssertNotDefault