Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting configuration property #2357

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/ImageSharp/Formats/DecoderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,25 @@ public sealed class DecoderOptions

private uint maxFrames = int.MaxValue;

// Used by the FileProvider in the unit tests to set the configuration on the fly.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I know. This is nasty but so is the file provider.

#pragma warning disable IDE0032 // Use auto property
private Configuration configuration = Configuration.Default;
#pragma warning restore IDE0032 // Use auto property

/// <summary>
/// Gets the shared default general decoder options instance.
/// Used internally to reduce allocations for default decoding operations.
/// </summary>
internal static DecoderOptions Default { get; } = LazyOptions.Value;

/// <summary>
/// Gets a custom configuration instance to be used by the image processing pipeline.
/// </summary>
public Configuration Configuration { get; internal set; } = Configuration.Default;
#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

/// <summary>
/// Gets the target size to decode the image into. Scaling should use an operation equivalent to <see cref="ResizeMode.Max"/>.
Expand All @@ -44,4 +54,6 @@ public sealed class DecoderOptions
/// Gets the maximum number of image frames to decode, inclusive.
/// </summary>
public uint MaxFrames { get => this.maxFrames; init => this.maxFrames = Math.Clamp(value, 1, int.MaxValue); }

internal void SetConfiguration(Configuration configuration) => this.configuration = configuration;
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public override async Task<Image<TPixel>> GetImageAsync(IImageDecoder decoder, D
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?
Expand Down Expand Up @@ -244,7 +244,7 @@ public override async Task<Image<TPixel>> GetImageAsync<T>(ISpecializedImageDeco
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?
Expand All @@ -268,7 +268,7 @@ public override void Serialize(IXunitSerializationInfo info)

private Image<TPixel> 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);
Expand All @@ -278,7 +278,7 @@ private Image<TPixel> DecodeImage(IImageDecoder decoder, DecoderOptions options)
private Image<TPixel> DecodeImage<T>(ISpecializedImageDecoder<T> 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);
Expand Down