From 83f0979fe0fe3b508472958e8dca9ecfd5afe6a3 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 16 Feb 2023 12:05:22 +1000 Subject: [PATCH 1/3] Allow setting configuration property --- src/ImageSharp/Formats/DecoderOptions.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/DecoderOptions.cs b/src/ImageSharp/Formats/DecoderOptions.cs index 989fc49fc..a4f46368b 100644 --- a/src/ImageSharp/Formats/DecoderOptions.cs +++ b/src/ImageSharp/Formats/DecoderOptions.cs @@ -17,13 +17,14 @@ public sealed class DecoderOptions /// /// Gets the shared default general decoder options instance. + /// Used internally to reduce allocations for default decoding operations. /// internal static DecoderOptions Default { get; } = LazyOptions.Value; /// /// Gets a custom configuration instance to be used by the image processing pipeline. /// - public Configuration Configuration { get; internal set; } = Configuration.Default; + public Configuration Configuration { get; init; } = Configuration.Default; /// /// Gets the target size to decode the image into. Scaling should use an operation equivalent to . From 670b1a27bbf437ddae49c9d947c5c418b5aa8caf Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 16 Feb 2023 13:38:36 +1000 Subject: [PATCH 2/3] Fix assignment of configuration options in unit tests --- src/ImageSharp/Formats/DecoderOptions.cs | 13 ++++++++++++- .../TestUtilities/ImageProviders/FileProvider.cs | 8 ++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/DecoderOptions.cs b/src/ImageSharp/Formats/DecoderOptions.cs index a4f46368b..f786a9df1 100644 --- a/src/ImageSharp/Formats/DecoderOptions.cs +++ b/src/ImageSharp/Formats/DecoderOptions.cs @@ -15,6 +15,11 @@ public sealed class DecoderOptions private uint maxFrames = int.MaxValue; + // Used by the FileProvider in the unit tests to set the configuration on the fly. +#pragma warning disable SA1401 // Fields should be private + internal Configuration BackingConfiguration = Configuration.Default; +#pragma warning restore SA1401 // Fields should be private + /// /// Gets the shared default general decoder options instance. /// Used internally to reduce allocations for default decoding operations. @@ -24,7 +29,7 @@ public sealed class DecoderOptions /// /// Gets a custom configuration instance to be used by the image processing pipeline. /// - public Configuration Configuration { get; init; } = Configuration.Default; + public Configuration Configuration { get => this.BackingConfiguration; init => this.BackingConfiguration = value; } /// /// Gets the target size to decode the image into. Scaling should use an operation equivalent to . @@ -46,3 +51,9 @@ public sealed class DecoderOptions /// public uint MaxFrames { get => this.maxFrames; init => this.maxFrames = Math.Clamp(value, 1, int.MaxValue); } } + +internal static class DecoderOptionsExtensions +{ + public static void SetConfiguration(this DecoderOptions options, Configuration configuration) + => options.BackingConfiguration = configuration; +} diff --git a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs index 3285da31b..3652d77a1 100644 --- a/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs +++ b/tests/ImageSharp.Tests/TestUtilities/ImageProviders/FileProvider.cs @@ -207,7 +207,7 @@ public abstract partial class TestImageProvider : IXunitSerializable Guard.NotNull(decoder, nameof(decoder)); Guard.NotNull(options, nameof(options)); - options.Configuration = this.Configuration; + options.SetConfiguration(this.Configuration); // Used in small subset of decoder tests, no caching. // TODO: Check Path here. Why combined? @@ -244,7 +244,7 @@ public abstract partial class TestImageProvider : IXunitSerializable Guard.NotNull(decoder, nameof(decoder)); Guard.NotNull(options, nameof(options)); - options.GeneralOptions.Configuration = this.Configuration; + options.GeneralOptions.SetConfiguration(this.Configuration); // Used in small subset of decoder tests, no caching. // TODO: Check Path here. Why combined? @@ -268,7 +268,7 @@ public abstract partial class TestImageProvider : IXunitSerializable private Image DecodeImage(IImageDecoder decoder, DecoderOptions options) { - options.Configuration = this.Configuration; + options.SetConfiguration(this.Configuration); var testFile = TestFile.Create(this.FilePath); using Stream stream = new MemoryStream(testFile.Bytes); @@ -278,7 +278,7 @@ public abstract partial class TestImageProvider : IXunitSerializable private Image DecodeImage(ISpecializedImageDecoder decoder, T options) where T : class, ISpecializedDecoderOptions, new() { - options.GeneralOptions.Configuration = this.Configuration; + options.GeneralOptions.SetConfiguration(this.Configuration); var testFile = TestFile.Create(this.FilePath); using Stream stream = new MemoryStream(testFile.Bytes); From 8a42441c735b029db732dd8a4dfab29c467767a6 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 17 Feb 2023 10:09:38 +1000 Subject: [PATCH 3/3] Update DecoderOptions.cs --- src/ImageSharp/Formats/DecoderOptions.cs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp/Formats/DecoderOptions.cs b/src/ImageSharp/Formats/DecoderOptions.cs index f786a9df1..6243a071d 100644 --- a/src/ImageSharp/Formats/DecoderOptions.cs +++ b/src/ImageSharp/Formats/DecoderOptions.cs @@ -16,9 +16,9 @@ public sealed class DecoderOptions private uint maxFrames = int.MaxValue; // Used by the FileProvider in the unit tests to set the configuration on the fly. -#pragma warning disable SA1401 // Fields should be private - internal Configuration BackingConfiguration = Configuration.Default; -#pragma warning restore SA1401 // Fields should be private +#pragma warning disable IDE0032 // Use auto property + private Configuration configuration = Configuration.Default; +#pragma warning restore IDE0032 // Use auto property /// /// Gets the shared default general decoder options instance. @@ -29,7 +29,11 @@ public sealed class DecoderOptions /// /// Gets a custom configuration instance to be used by the image processing pipeline. /// - public Configuration Configuration { get => this.BackingConfiguration; init => this.BackingConfiguration = value; } +#pragma warning disable IDE0032 // Use auto property +#pragma warning disable RCS1085 // Use auto-implemented property. + public Configuration Configuration { get => this.configuration; init => this.configuration = value; } +#pragma warning restore RCS1085 // Use auto-implemented property. +#pragma warning restore IDE0032 // Use auto property /// /// Gets the target size to decode the image into. Scaling should use an operation equivalent to . @@ -50,10 +54,6 @@ public sealed class DecoderOptions /// Gets the maximum number of image frames to decode, inclusive. /// public uint MaxFrames { get => this.maxFrames; init => this.maxFrames = Math.Clamp(value, 1, int.MaxValue); } -} -internal static class DecoderOptionsExtensions -{ - public static void SetConfiguration(this DecoderOptions options, Configuration configuration) - => options.BackingConfiguration = configuration; + internal void SetConfiguration(Configuration configuration) => this.configuration = configuration; }