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));
string ext = Path.GetExtension(filePath);
IImageFormat? format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext);
if (format is null)
if (!source.GetConfiguration().ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat? format))
{
StringBuilder sb = new();
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.
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats;
@ -91,8 +92,9 @@ public class ImageFormatManager
/// For the specified file extensions type find the e <see cref="IImageFormat"/>.
/// </summary>
/// <param name="extension">The extension to discover</param>
/// <returns>The <see cref="IImageFormat"/> if found otherwise null</returns>
public IImageFormat? FindFormatByFileExtension(string extension)
/// <param name="format">The <see cref="IImageFormat"/> if found otherwise null</param>
/// <returns>False if no format was found</returns>
public bool TryFindFormatByFileExtension(string extension, [NotNullWhen(true)] out IImageFormat? format)
{
Guard.NotNullOrWhiteSpace(extension, nameof(extension));
@ -101,7 +103,10 @@ public class ImageFormatManager
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>

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

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

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

@ -77,8 +77,8 @@ public partial class ImageTests
using (var image = new Image<Rgba32>(5, 5))
{
string ext = Path.GetExtension(filename);
IImageFormat format = image.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext);
Assert.Equal(mimeType, format.DefaultMimeType);
image.GetConfiguration().ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat format);
Assert.Equal(mimeType, format!.DefaultMimeType);
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.PreferContiguousImageBuffers = true;
IImageEncoder encoder = configuration.ImageFormatsManager.FindEncoder(
configuration.ImageFormatsManager.FindFormatByFileExtension(formatInner));
configuration.ImageFormatsManager.TryFindFormatByFileExtension(formatInner, out IImageFormat format);
IImageEncoder encoder = configuration.ImageFormatsManager.FindEncoder(format!);
string dir = TestEnvironment.CreateOutputDirectory(".Temp");
string path = Path.Combine(dir, $"{Guid.NewGuid()}.{formatInner}");
using (Image<Rgba32> temp = new(2048, 2048))

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

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

Loading…
Cancel
Save