Browse Source

Merge pull request #1936 from SixLabors/js/fix-1850

ImageFormatDetector Configuration Cleanup
pull/1947/head
James Jackson-South 4 years ago
committed by GitHub
parent
commit
dfe3fb5387
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/ImageSharp/Configuration.cs
  2. 26
      src/ImageSharp/Formats/ImageFormatManager.cs

16
src/ImageSharp/Configuration.cs

@ -122,25 +122,25 @@ namespace SixLabors.ImageSharp
public ReadOrigin ReadOrigin { get; set; } = ReadOrigin.Current; public ReadOrigin ReadOrigin { get; set; } = ReadOrigin.Current;
/// <summary> /// <summary>
/// Gets or sets the <see cref="ImageFormatManager"/> that is currently in use. /// Gets or the <see cref="ImageFormatManager"/> that is currently in use.
/// </summary> /// </summary>
public ImageFormatManager ImageFormatsManager { get; set; } = new ImageFormatManager(); public ImageFormatManager ImageFormatsManager { get; private set; } = new ImageFormatManager();
/// <summary> /// <summary>
/// Gets or sets the <see cref="ImageSharp.Memory.MemoryAllocator"/> that is currently in use. /// Gets or sets the <see cref="Memory.MemoryAllocator"/> that is currently in use.
/// Defaults to <see cref="ImageSharp.Memory.MemoryAllocator.Default"/>. /// Defaults to <see cref="MemoryAllocator.Default"/>.
/// <para /> /// <para />
/// Allocators are expensive, so it is strongly recommended to use only one busy instance per process. /// Allocators are expensive, so it is strongly recommended to use only one busy instance per process.
/// In case you need to customize it, you can ensure this by changing /// In case you need to customize it, you can ensure this by changing
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// It's possible to reduce allocator footprint by assigning a custom instance created with /// It's possible to reduce allocator footprint by assigning a custom instance created with
/// <see cref="Memory.MemoryAllocator.Create(MemoryAllocatorOptions)"/>, but note that since the default pooling /// <see cref="MemoryAllocator.Create(MemoryAllocatorOptions)"/>, but note that since the default pooling
/// allocators are expensive, it is strictly recommended to use a single process-wide allocator. /// allocators are expensive, it is strictly recommended to use a single process-wide allocator.
/// You can ensure this by altering the allocator of <see cref="Default"/>, or by implementing custom application logic that /// You can ensure this by altering the allocator of <see cref="Default"/>, or by implementing custom application logic that
/// manages allocator lifetime. /// manages allocator lifetime.
/// <para /> /// <para />
/// If an allocator has to be dropped for some reason, <see cref="Memory.MemoryAllocator.ReleaseRetainedResources"/> /// If an allocator has to be dropped for some reason, <see cref="MemoryAllocator.ReleaseRetainedResources"/>
/// shall be invoked after disposing all associated <see cref="Image"/> instances. /// shall be invoked after disposing all associated <see cref="Image"/> instances.
/// </remarks> /// </remarks>
public MemoryAllocator MemoryAllocator public MemoryAllocator MemoryAllocator
@ -192,7 +192,7 @@ namespace SixLabors.ImageSharp
/// Creates a shallow copy of the <see cref="Configuration"/>. /// Creates a shallow copy of the <see cref="Configuration"/>.
/// </summary> /// </summary>
/// <returns>A new configuration instance.</returns> /// <returns>A new configuration instance.</returns>
public Configuration Clone() => new Configuration public Configuration Clone() => new()
{ {
MaxDegreeOfParallelism = this.MaxDegreeOfParallelism, MaxDegreeOfParallelism = this.MaxDegreeOfParallelism,
StreamProcessingBufferSize = this.StreamProcessingBufferSize, StreamProcessingBufferSize = this.StreamProcessingBufferSize,
@ -216,7 +216,7 @@ namespace SixLabors.ImageSharp
/// <see cref="WebpConfigurationModule"/>. /// <see cref="WebpConfigurationModule"/>.
/// </summary> /// </summary>
/// <returns>The default configuration of <see cref="Configuration"/>.</returns> /// <returns>The default configuration of <see cref="Configuration"/>.</returns>
internal static Configuration CreateDefaultInstance() => new Configuration( internal static Configuration CreateDefaultInstance() => new(
new PngConfigurationModule(), new PngConfigurationModule(),
new JpegConfigurationModule(), new JpegConfigurationModule(),
new GifConfigurationModule(), new GifConfigurationModule(),

26
src/ImageSharp/Formats/ImageFormatManager.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
@ -17,27 +17,27 @@ namespace SixLabors.ImageSharp.Formats
/// Used for locking against as there is no ConcurrentSet type. /// Used for locking against as there is no ConcurrentSet type.
/// <see href="https://github.com/dotnet/corefx/issues/6318"/> /// <see href="https://github.com/dotnet/corefx/issues/6318"/>
/// </summary> /// </summary>
private static readonly object HashLock = new object(); private static readonly object HashLock = new();
/// <summary> /// <summary>
/// The list of supported <see cref="IImageEncoder"/> keyed to mime types. /// The list of supported <see cref="IImageEncoder"/> keyed to mime types.
/// </summary> /// </summary>
private readonly ConcurrentDictionary<IImageFormat, IImageEncoder> mimeTypeEncoders = new ConcurrentDictionary<IImageFormat, IImageEncoder>(); private readonly ConcurrentDictionary<IImageFormat, IImageEncoder> mimeTypeEncoders = new();
/// <summary> /// <summary>
/// The list of supported <see cref="IImageEncoder"/> keyed to mime types. /// The list of supported <see cref="IImageEncoder"/> keyed to mime types.
/// </summary> /// </summary>
private readonly ConcurrentDictionary<IImageFormat, IImageDecoder> mimeTypeDecoders = new ConcurrentDictionary<IImageFormat, IImageDecoder>(); private readonly ConcurrentDictionary<IImageFormat, IImageDecoder> mimeTypeDecoders = new();
/// <summary> /// <summary>
/// The list of supported <see cref="IImageFormat"/>s. /// The list of supported <see cref="IImageFormat"/>s.
/// </summary> /// </summary>
private readonly HashSet<IImageFormat> imageFormats = new HashSet<IImageFormat>(); private readonly HashSet<IImageFormat> imageFormats = new();
/// <summary> /// <summary>
/// The list of supported <see cref="IImageFormatDetector"/>s. /// The list of supported <see cref="IImageFormatDetector"/>s.
/// </summary> /// </summary>
private ConcurrentBag<IImageFormatDetector> imageFormatDetectors = new ConcurrentBag<IImageFormatDetector>(); private ConcurrentBag<IImageFormatDetector> imageFormatDetectors = new();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ImageFormatManager" /> class. /// Initializes a new instance of the <see cref="ImageFormatManager" /> class.
@ -113,9 +113,7 @@ namespace SixLabors.ImageSharp.Formats
/// <param name="mimeType">The mime-type to discover</param> /// <param name="mimeType">The mime-type to discover</param>
/// <returns>The <see cref="IImageFormat"/> if found; otherwise null</returns> /// <returns>The <see cref="IImageFormat"/> if found; otherwise null</returns>
public IImageFormat FindFormatByMimeType(string mimeType) public IImageFormat FindFormatByMimeType(string mimeType)
{ => this.imageFormats.FirstOrDefault(x => x.MimeTypes.Contains(mimeType, StringComparer.OrdinalIgnoreCase));
return this.imageFormats.FirstOrDefault(x => x.MimeTypes.Contains(mimeType, StringComparer.OrdinalIgnoreCase));
}
/// <summary> /// <summary>
/// Sets a specific image encoder as the encoder for a specific image format. /// Sets a specific image encoder as the encoder for a specific image format.
@ -146,10 +144,7 @@ namespace SixLabors.ImageSharp.Formats
/// <summary> /// <summary>
/// Removes all the registered image format detectors. /// Removes all the registered image format detectors.
/// </summary> /// </summary>
public void ClearImageFormatDetectors() public void ClearImageFormatDetectors() => this.imageFormatDetectors = new();
{
this.imageFormatDetectors = new ConcurrentBag<IImageFormatDetector>();
}
/// <summary> /// <summary>
/// Adds a new detector for detecting mime types. /// Adds a new detector for detecting mime types.
@ -193,9 +188,6 @@ namespace SixLabors.ImageSharp.Formats
/// <summary> /// <summary>
/// Sets the max header size. /// Sets the max header size.
/// </summary> /// </summary>
private void SetMaxHeaderSize() private void SetMaxHeaderSize() => this.MaxHeaderSize = this.imageFormatDetectors.Max(x => x.HeaderSize);
{
this.MaxHeaderSize = this.imageFormatDetectors.Max(x => x.HeaderSize);
}
} }
} }

Loading…
Cancel
Save