Browse Source

Rewrite to TryFindFormatByFileExtension

pull/2312/head
Stefan Nikolei 3 years ago
parent
commit
0f9efeb25c
  1. 3
      src/ImageSharp/Advanced/AdvancedImageExtensions.cs
  2. 11
      src/ImageSharp/Formats/ImageFormatManager.cs
  3. 10
      tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs
  4. 107
      tests/ImageSharp.Tests/Image/ImageTests.Identify.cs
  5. 4
      tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs
  6. 4
      tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs
  7. 5
      tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs

3
src/ImageSharp/Advanced/AdvancedImageExtensions.cs

@ -27,8 +27,7 @@ public static class AdvancedImageExtensions
Guard.NotNull(filePath, nameof(filePath)); Guard.NotNull(filePath, nameof(filePath));
string ext = Path.GetExtension(filePath); string ext = Path.GetExtension(filePath);
IImageFormat? format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext); if (!source.GetConfiguration().ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat? format))
if (format is null)
{ {
StringBuilder sb = new(); StringBuilder sb = new();
sb.AppendLine(CultureInfo.InvariantCulture, $"No encoder was found for extension '{ext}'. Registered encoders include:"); sb.AppendLine(CultureInfo.InvariantCulture, $"No encoder was found for extension '{ext}'. Registered encoders include:");

11
src/ImageSharp/Formats/ImageFormatManager.cs

@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats; namespace SixLabors.ImageSharp.Formats;
@ -91,8 +92,9 @@ public class ImageFormatManager
/// For the specified file extensions type find the e <see cref="IImageFormat"/>. /// For the specified file extensions type find the e <see cref="IImageFormat"/>.
/// </summary> /// </summary>
/// <param name="extension">The extension to discover</param> /// <param name="extension">The extension to discover</param>
/// <returns>The <see cref="IImageFormat"/> if found otherwise null</returns> /// <param name="format">The <see cref="IImageFormat"/> if found otherwise null</param>
public IImageFormat? FindFormatByFileExtension(string extension) /// <returns>False if no format was found</returns>
public bool TryFindFormatByFileExtension(string extension, [NotNullWhen(true)] out IImageFormat? format)
{ {
Guard.NotNullOrWhiteSpace(extension, nameof(extension)); Guard.NotNullOrWhiteSpace(extension, nameof(extension));
@ -101,7 +103,10 @@ public class ImageFormatManager
extension = extension[1..]; extension = extension[1..];
} }
return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)); format = this.imageFormats.FirstOrDefault(x =>
x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase));
return format != null;
} }
/// <summary> /// <summary>

10
tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs

@ -22,8 +22,14 @@ public partial class ImageTests
private IImageFormat LocalImageFormat => this.localImageFormatMock.Object; private IImageFormat LocalImageFormat => this.localImageFormatMock.Object;
private static readonly IImageFormat ExpectedGlobalFormat = private static IImageFormat ExpectedGlobalFormat
Configuration.Default.ImageFormatsManager.FindFormatByFileExtension("bmp"); {
get
{
Configuration.Default.ImageFormatsManager.TryFindFormatByFileExtension("bmp", out IImageFormat format);
return format!;
}
}
[Fact] [Fact]
public void FromBytes_GlobalConfiguration() public void FromBytes_GlobalConfiguration()

107
tests/ImageSharp.Tests/Image/ImageTests.Identify.cs

@ -25,8 +25,15 @@ public partial class ImageTests
private IImageFormat LocalImageFormat => this.localImageFormatMock.Object; private IImageFormat LocalImageFormat => this.localImageFormatMock.Object;
private static readonly IImageFormat ExpectedGlobalFormat = private static IImageFormat ExpectedGlobalFormat
Configuration.Default.ImageFormatsManager.FindFormatByFileExtension("bmp"); {
get
{
Configuration.Default.ImageFormatsManager.TryFindFormatByFileExtension("bmp", out var format);
return format!;
}
}
[Fact] [Fact]
public void FromBytes_GlobalConfiguration() public void FromBytes_GlobalConfiguration()
@ -40,10 +47,7 @@ public partial class ImageTests
[Fact] [Fact]
public void FromBytes_CustomConfiguration() public void FromBytes_CustomConfiguration()
{ {
DecoderOptions options = new() DecoderOptions options = new() { Configuration = this.LocalConfiguration };
{
Configuration = this.LocalConfiguration
};
IImageInfo info = Image.Identify(options, this.ByteArray, out IImageFormat type); IImageInfo info = Image.Identify(options, this.ByteArray, out IImageFormat type);
@ -63,10 +67,7 @@ public partial class ImageTests
[Fact] [Fact]
public void FromFileSystemPath_CustomConfiguration() public void FromFileSystemPath_CustomConfiguration()
{ {
DecoderOptions options = new() DecoderOptions options = new() { Configuration = this.LocalConfiguration };
{
Configuration = this.LocalConfiguration
};
IImageInfo info = Image.Identify(options, this.MockFilePath, out IImageFormat type); IImageInfo info = Image.Identify(options, this.MockFilePath, out IImageFormat type);
@ -119,10 +120,7 @@ public partial class ImageTests
[Fact] [Fact]
public void FromStream_CustomConfiguration() public void FromStream_CustomConfiguration()
{ {
DecoderOptions options = new() DecoderOptions options = new() { Configuration = this.LocalConfiguration };
{
Configuration = this.LocalConfiguration
};
IImageInfo info = Image.Identify(options, this.DataStream, out IImageFormat type); IImageInfo info = Image.Identify(options, this.DataStream, out IImageFormat type);
@ -133,10 +131,7 @@ public partial class ImageTests
[Fact] [Fact]
public void FromStream_CustomConfiguration_NoFormat() public void FromStream_CustomConfiguration_NoFormat()
{ {
DecoderOptions options = new() DecoderOptions options = new() { Configuration = this.LocalConfiguration };
{
Configuration = this.LocalConfiguration
};
IImageInfo info = Image.Identify(options, this.DataStream); IImageInfo info = Image.Identify(options, this.DataStream);
@ -146,10 +141,7 @@ public partial class ImageTests
[Fact] [Fact]
public void WhenNoMatchingFormatFound_ReturnsNull() public void WhenNoMatchingFormatFound_ReturnsNull()
{ {
DecoderOptions options = new() DecoderOptions options = new() { Configuration = new() };
{
Configuration = new()
};
IImageInfo info = Image.Identify(options, this.DataStream, out IImageFormat type); IImageInfo info = Image.Identify(options, this.DataStream, out IImageFormat type);
@ -164,18 +156,15 @@ public partial class ImageTests
using var zipFile = new ZipArchive(new MemoryStream( using var zipFile = new ZipArchive(new MemoryStream(
new byte[] new byte[]
{ {
0x50, 0x4B, 0x03, 0x04, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xAF, 0x50, 0x4B, 0x03, 0x04, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xAF, 0x94, 0x53, 0x00, 0x00,
0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x6D, 0x79,
0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x6D, 0x79, 0x73, 0x74, 0x65, 0x72, 0x73, 0x74, 0x65, 0x72, 0x79, 0x50, 0x4B, 0x01, 0x02, 0x3F, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00,
0x79, 0x50, 0x4B, 0x01, 0x02, 0x3F, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xAF, 0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x77, 0xAF, 0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x79, 0x73, 0x74, 0x65, 0x72, 0x79, 0x0A, 0x00, 0x20, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x46, 0x82, 0xFF, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x55, 0xA1,
0x79, 0x73, 0x74, 0x65, 0x72, 0x79, 0x0A, 0x00, 0x20, 0x00, 0x00, 0x00, 0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x55, 0xA1, 0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x50, 0x4B,
0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x46, 0x82, 0xFF, 0x91, 0x27, 0xF6, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x59, 0x00, 0x00, 0x00, 0x25, 0x00,
0xD7, 0x01, 0x55, 0xA1, 0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x55, 0xA1,
0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x50, 0x4B, 0x05, 0x06, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x59, 0x00, 0x00, 0x00, 0x25, 0x00,
0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00
})); }));
using Stream stream = zipFile.Entries[0].Open(); using Stream stream = zipFile.Entries[0].Open();
@ -236,18 +225,15 @@ public partial class ImageTests
using var zipFile = new ZipArchive(new MemoryStream( using var zipFile = new ZipArchive(new MemoryStream(
new byte[] new byte[]
{ {
0x50, 0x4B, 0x03, 0x04, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xAF, 0x50, 0x4B, 0x03, 0x04, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xAF, 0x94, 0x53, 0x00, 0x00,
0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x6D, 0x79,
0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x6D, 0x79, 0x73, 0x74, 0x65, 0x72, 0x73, 0x74, 0x65, 0x72, 0x79, 0x50, 0x4B, 0x01, 0x02, 0x3F, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00,
0x79, 0x50, 0x4B, 0x01, 0x02, 0x3F, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x77, 0xAF, 0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x77, 0xAF, 0x94, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x79, 0x73, 0x74, 0x65, 0x72, 0x79, 0x0A, 0x00, 0x20, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x46, 0x82, 0xFF, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x55, 0xA1,
0x79, 0x73, 0x74, 0x65, 0x72, 0x79, 0x0A, 0x00, 0x20, 0x00, 0x00, 0x00, 0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x55, 0xA1, 0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x50, 0x4B,
0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x46, 0x82, 0xFF, 0x91, 0x27, 0xF6, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x59, 0x00, 0x00, 0x00, 0x25, 0x00,
0xD7, 0x01, 0x55, 0xA1, 0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x55, 0xA1,
0xF9, 0x91, 0x27, 0xF6, 0xD7, 0x01, 0x50, 0x4B, 0x05, 0x06, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x59, 0x00, 0x00, 0x00, 0x25, 0x00,
0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00
})); }));
using Stream stream = zipFile.Entries[0].Open(); using Stream stream = zipFile.Entries[0].Open();
@ -258,10 +244,7 @@ public partial class ImageTests
[Fact] [Fact]
public async Task FromPathAsync_CustomConfiguration() public async Task FromPathAsync_CustomConfiguration()
{ {
DecoderOptions options = new() DecoderOptions options = new() { Configuration = this.LocalConfiguration };
{
Configuration = this.LocalConfiguration
};
IImageInfo info = await Image.IdentifyAsync(options, this.MockFilePath); IImageInfo info = await Image.IdentifyAsync(options, this.MockFilePath);
Assert.Equal(this.LocalImageInfo, info); Assert.Equal(this.LocalImageInfo, info);
@ -270,12 +253,10 @@ public partial class ImageTests
[Fact] [Fact]
public async Task IdentifyWithFormatAsync_FromPath_CustomConfiguration() public async Task IdentifyWithFormatAsync_FromPath_CustomConfiguration()
{ {
DecoderOptions options = new() DecoderOptions options = new() { Configuration = this.LocalConfiguration };
{
Configuration = this.LocalConfiguration
};
(IImageInfo ImageInfo, IImageFormat Format) info = await Image.IdentifyWithFormatAsync(options, this.MockFilePath); (IImageInfo ImageInfo, IImageFormat Format) info =
await Image.IdentifyWithFormatAsync(options, this.MockFilePath);
Assert.NotNull(info.ImageInfo); Assert.NotNull(info.ImageInfo);
Assert.Equal(this.LocalImageFormat, info.Format); Assert.Equal(this.LocalImageFormat, info.Format);
} }
@ -300,13 +281,11 @@ public partial class ImageTests
[Fact] [Fact]
public async Task FromStreamAsync_CustomConfiguration() public async Task FromStreamAsync_CustomConfiguration()
{ {
DecoderOptions options = new() DecoderOptions options = new() { Configuration = this.LocalConfiguration };
{
Configuration = this.LocalConfiguration
};
var asyncStream = new AsyncStreamWrapper(this.DataStream, () => false); var asyncStream = new AsyncStreamWrapper(this.DataStream, () => false);
(IImageInfo ImageInfo, IImageFormat Format) info = await Image.IdentifyWithFormatAsync(options, asyncStream); (IImageInfo ImageInfo, IImageFormat Format)
info = await Image.IdentifyWithFormatAsync(options, asyncStream);
Assert.Equal(this.LocalImageInfo, info.ImageInfo); Assert.Equal(this.LocalImageInfo, info.ImageInfo);
Assert.Equal(this.LocalImageFormat, info.Format); Assert.Equal(this.LocalImageFormat, info.Format);
@ -315,13 +294,11 @@ public partial class ImageTests
[Fact] [Fact]
public async Task WhenNoMatchingFormatFoundAsync_ReturnsNull() public async Task WhenNoMatchingFormatFoundAsync_ReturnsNull()
{ {
DecoderOptions options = new() DecoderOptions options = new() { Configuration = new() };
{
Configuration = new()
};
var asyncStream = new AsyncStreamWrapper(this.DataStream, () => false); var asyncStream = new AsyncStreamWrapper(this.DataStream, () => false);
(IImageInfo ImageInfo, IImageFormat Format) info = await Image.IdentifyWithFormatAsync(options, asyncStream); (IImageInfo ImageInfo, IImageFormat Format)
info = await Image.IdentifyWithFormatAsync(options, asyncStream);
Assert.Null(info.ImageInfo); Assert.Null(info.ImageInfo);
} }

4
tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs

@ -77,8 +77,8 @@ public partial class ImageTests
using (var image = new Image<Rgba32>(5, 5)) using (var image = new Image<Rgba32>(5, 5))
{ {
string ext = Path.GetExtension(filename); string ext = Path.GetExtension(filename);
IImageFormat format = image.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext); image.GetConfiguration().ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat format);
Assert.Equal(mimeType, format.DefaultMimeType); Assert.Equal(mimeType, format!.DefaultMimeType);
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
{ {

4
tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs

@ -55,8 +55,8 @@ public class LargeImageIntegrationTests
Configuration configuration = Configuration.Default.Clone(); Configuration configuration = Configuration.Default.Clone();
configuration.PreferContiguousImageBuffers = true; configuration.PreferContiguousImageBuffers = true;
IImageEncoder encoder = configuration.ImageFormatsManager.FindEncoder( configuration.ImageFormatsManager.TryFindFormatByFileExtension(formatInner, out IImageFormat format);
configuration.ImageFormatsManager.FindFormatByFileExtension(formatInner)); IImageEncoder encoder = configuration.ImageFormatsManager.FindEncoder(format!);
string dir = TestEnvironment.CreateOutputDirectory(".Temp"); string dir = TestEnvironment.CreateOutputDirectory(".Temp");
string path = Path.Combine(dir, $"{Guid.NewGuid()}.{formatInner}"); string path = Path.Combine(dir, $"{Guid.NewGuid()}.{formatInner}");
using (Image<Rgba32> temp = new(2048, 2048)) using (Image<Rgba32> temp = new(2048, 2048))

5
tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.Formats.Bmp;
using SixLabors.ImageSharp.Formats.Gif; using SixLabors.ImageSharp.Formats.Gif;
@ -36,7 +37,9 @@ public static partial class TestEnvironment
{ {
string extension = Path.GetExtension(filePath); string extension = Path.GetExtension(filePath);
return Configuration.ImageFormatsManager.FindFormatByFileExtension(extension); Configuration.ImageFormatsManager.TryFindFormatByFileExtension(extension, out IImageFormat format);
return format;
} }
private static void ConfigureCodecs( private static void ConfigureCodecs(

Loading…
Cancel
Save