Skip to content

Commit dfe3fb5

Browse files
Merge pull request #1936 from SixLabors/js/fix-1850
ImageFormatDetector Configuration Cleanup
2 parents bab74dc + 7d39dfc commit dfe3fb5

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

src/ImageSharp/Configuration.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,25 +122,25 @@ public int StreamProcessingBufferSize
122122
public ReadOrigin ReadOrigin { get; set; } = ReadOrigin.Current;
123123

124124
/// <summary>
125-
/// Gets or sets the <see cref="ImageFormatManager"/> that is currently in use.
125+
/// Gets or the <see cref="ImageFormatManager"/> that is currently in use.
126126
/// </summary>
127-
public ImageFormatManager ImageFormatsManager { get; set; } = new ImageFormatManager();
127+
public ImageFormatManager ImageFormatsManager { get; private set; } = new ImageFormatManager();
128128

129129
/// <summary>
130-
/// Gets or sets the <see cref="ImageSharp.Memory.MemoryAllocator"/> that is currently in use.
131-
/// Defaults to <see cref="ImageSharp.Memory.MemoryAllocator.Default"/>.
130+
/// Gets or sets the <see cref="Memory.MemoryAllocator"/> that is currently in use.
131+
/// Defaults to <see cref="MemoryAllocator.Default"/>.
132132
/// <para />
133133
/// Allocators are expensive, so it is strongly recommended to use only one busy instance per process.
134134
/// In case you need to customize it, you can ensure this by changing
135135
/// </summary>
136136
/// <remarks>
137137
/// It's possible to reduce allocator footprint by assigning a custom instance created with
138-
/// <see cref="Memory.MemoryAllocator.Create(MemoryAllocatorOptions)"/>, but note that since the default pooling
138+
/// <see cref="MemoryAllocator.Create(MemoryAllocatorOptions)"/>, but note that since the default pooling
139139
/// allocators are expensive, it is strictly recommended to use a single process-wide allocator.
140140
/// You can ensure this by altering the allocator of <see cref="Default"/>, or by implementing custom application logic that
141141
/// manages allocator lifetime.
142142
/// <para />
143-
/// If an allocator has to be dropped for some reason, <see cref="Memory.MemoryAllocator.ReleaseRetainedResources"/>
143+
/// If an allocator has to be dropped for some reason, <see cref="MemoryAllocator.ReleaseRetainedResources"/>
144144
/// shall be invoked after disposing all associated <see cref="Image"/> instances.
145145
/// </remarks>
146146
public MemoryAllocator MemoryAllocator
@@ -192,7 +192,7 @@ public void Configure(IConfigurationModule configuration)
192192
/// Creates a shallow copy of the <see cref="Configuration"/>.
193193
/// </summary>
194194
/// <returns>A new configuration instance.</returns>
195-
public Configuration Clone() => new Configuration
195+
public Configuration Clone() => new()
196196
{
197197
MaxDegreeOfParallelism = this.MaxDegreeOfParallelism,
198198
StreamProcessingBufferSize = this.StreamProcessingBufferSize,
@@ -216,7 +216,7 @@ public void Configure(IConfigurationModule configuration)
216216
/// <see cref="WebpConfigurationModule"/>.
217217
/// </summary>
218218
/// <returns>The default configuration of <see cref="Configuration"/>.</returns>
219-
internal static Configuration CreateDefaultInstance() => new Configuration(
219+
internal static Configuration CreateDefaultInstance() => new(
220220
new PngConfigurationModule(),
221221
new JpegConfigurationModule(),
222222
new GifConfigurationModule(),

src/ImageSharp/Formats/ImageFormatManager.cs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -17,27 +17,27 @@ public class ImageFormatManager
1717
/// Used for locking against as there is no ConcurrentSet type.
1818
/// <see href="https://github.com/dotnet/corefx/issues/6318"/>
1919
/// </summary>
20-
private static readonly object HashLock = new object();
20+
private static readonly object HashLock = new();
2121

2222
/// <summary>
2323
/// The list of supported <see cref="IImageEncoder"/> keyed to mime types.
2424
/// </summary>
25-
private readonly ConcurrentDictionary<IImageFormat, IImageEncoder> mimeTypeEncoders = new ConcurrentDictionary<IImageFormat, IImageEncoder>();
25+
private readonly ConcurrentDictionary<IImageFormat, IImageEncoder> mimeTypeEncoders = new();
2626

2727
/// <summary>
2828
/// The list of supported <see cref="IImageEncoder"/> keyed to mime types.
2929
/// </summary>
30-
private readonly ConcurrentDictionary<IImageFormat, IImageDecoder> mimeTypeDecoders = new ConcurrentDictionary<IImageFormat, IImageDecoder>();
30+
private readonly ConcurrentDictionary<IImageFormat, IImageDecoder> mimeTypeDecoders = new();
3131

3232
/// <summary>
3333
/// The list of supported <see cref="IImageFormat"/>s.
3434
/// </summary>
35-
private readonly HashSet<IImageFormat> imageFormats = new HashSet<IImageFormat>();
35+
private readonly HashSet<IImageFormat> imageFormats = new();
3636

3737
/// <summary>
3838
/// The list of supported <see cref="IImageFormatDetector"/>s.
3939
/// </summary>
40-
private ConcurrentBag<IImageFormatDetector> imageFormatDetectors = new ConcurrentBag<IImageFormatDetector>();
40+
private ConcurrentBag<IImageFormatDetector> imageFormatDetectors = new();
4141

4242
/// <summary>
4343
/// Initializes a new instance of the <see cref="ImageFormatManager" /> class.
@@ -113,9 +113,7 @@ public IImageFormat FindFormatByFileExtension(string extension)
113113
/// <param name="mimeType">The mime-type to discover</param>
114114
/// <returns>The <see cref="IImageFormat"/> if found; otherwise null</returns>
115115
public IImageFormat FindFormatByMimeType(string mimeType)
116-
{
117-
return this.imageFormats.FirstOrDefault(x => x.MimeTypes.Contains(mimeType, StringComparer.OrdinalIgnoreCase));
118-
}
116+
=> this.imageFormats.FirstOrDefault(x => x.MimeTypes.Contains(mimeType, StringComparer.OrdinalIgnoreCase));
119117

120118
/// <summary>
121119
/// Sets a specific image encoder as the encoder for a specific image format.
@@ -146,10 +144,7 @@ public void SetDecoder(IImageFormat imageFormat, IImageDecoder decoder)
146144
/// <summary>
147145
/// Removes all the registered image format detectors.
148146
/// </summary>
149-
public void ClearImageFormatDetectors()
150-
{
151-
this.imageFormatDetectors = new ConcurrentBag<IImageFormatDetector>();
152-
}
147+
public void ClearImageFormatDetectors() => this.imageFormatDetectors = new();
153148

154149
/// <summary>
155150
/// Adds a new detector for detecting mime types.
@@ -193,9 +188,6 @@ public IImageEncoder FindEncoder(IImageFormat format)
193188
/// <summary>
194189
/// Sets the max header size.
195190
/// </summary>
196-
private void SetMaxHeaderSize()
197-
{
198-
this.MaxHeaderSize = this.imageFormatDetectors.Max(x => x.HeaderSize);
199-
}
191+
private void SetMaxHeaderSize() => this.MaxHeaderSize = this.imageFormatDetectors.Max(x => x.HeaderSize);
200192
}
201193
}

0 commit comments

Comments
 (0)