Browse Source

Convert DetectFormat to the Try pattern

pull/2312/head
Stefan Nikolei 3 years ago
parent
commit
4a5f525d7a
  1. 23
      src/ImageSharp/Image.FromBytes.cs
  2. 15
      src/ImageSharp/Image.FromFile.cs
  3. 17
      src/ImageSharp/Image.FromStream.cs
  4. 10
      tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs
  5. 26
      tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs

23
src/ImageSharp/Image.FromBytes.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.PixelFormats;
@ -15,18 +16,20 @@ public abstract partial class Image
/// By reading the header on the provided byte span this calculates the images format.
/// </summary>
/// <param name="data">The byte span containing encoded image data to read the header from.</param>
/// <returns>The format or null if none found.</returns>
public static IImageFormat? DetectFormat(ReadOnlySpan<byte> data)
=> DetectFormat(DecoderOptions.Default, data);
/// <param name="format">The format or null if none found.</param>
/// <returns>returns true when format was detected otherwise false.</returns>
public static bool TryDetectFormat(ReadOnlySpan<byte> data, [NotNullWhen(true)] out IImageFormat? format)
=> TryDetectFormat(DecoderOptions.Default, data, out format);
/// <summary>
/// By reading the header on the provided byte span this calculates the images format.
/// </summary>
/// <param name="options">The general decoder options.</param>
/// <param name="data">The byte span containing encoded image data to read the header from.</param>
/// <param name="format">The mime type or null if none found.</param>
/// <exception cref="ArgumentNullException">The options are null.</exception>
/// <returns>The mime type or null if none found.</returns>
public static IImageFormat? DetectFormat(DecoderOptions options, ReadOnlySpan<byte> data)
/// <returns>returns true when format was detected otherwise false.</returns>
public static bool TryDetectFormat(DecoderOptions options, ReadOnlySpan<byte> data, [NotNullWhen(true)] out IImageFormat? format)
{
Guard.NotNull(options, nameof(options.Configuration));
@ -34,18 +37,20 @@ public abstract partial class Image
int maxHeaderSize = configuration.MaxHeaderSize;
if (maxHeaderSize <= 0)
{
return null;
format = null;
return false;
}
foreach (IImageFormatDetector detector in configuration.ImageFormatsManager.FormatDetectors)
{
if (detector.TryDetectFormat(data, out IImageFormat? f))
if (detector.TryDetectFormat(data, out format))
{
return f;
return true;
}
}
return default;
format = default;
return false;
}
/// <summary>

15
src/ImageSharp/Image.FromFile.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.PixelFormats;
@ -15,23 +16,25 @@ public abstract partial class Image
/// By reading the header on the provided file this calculates the images mime type.
/// </summary>
/// <param name="filePath">The image file to open and to read the header from.</param>
/// <returns>The mime type or null if none found.</returns>
public static IImageFormat DetectFormat(string filePath)
=> DetectFormat(DecoderOptions.Default, filePath);
/// <param name="format">The mime type or null if none found.</param>
/// <returns>returns true when format was detected otherwise false.</returns>
public static bool TryDetectFormat(string filePath, [NotNullWhen(true)] out IImageFormat? format)
=> TryDetectFormat(DecoderOptions.Default, filePath, out format);
/// <summary>
/// By reading the header on the provided file this calculates the images mime type.
/// </summary>
/// <param name="options">The general decoder options.</param>
/// <param name="filePath">The image file to open and to read the header from.</param>
/// <param name="format">The mime type or null if none found.</param>
/// <exception cref="ArgumentNullException">The configuration is null.</exception>
/// <returns>The mime type or null if none found.</returns>
public static IImageFormat DetectFormat(DecoderOptions options, string filePath)
/// <returns>returns true when format was detected otherwise false.</returns>
public static bool TryDetectFormat(DecoderOptions options, string filePath, [NotNullWhen(true)] out IImageFormat? format)
{
Guard.NotNull(options, nameof(options));
using Stream file = options.Configuration.FileSystem.OpenRead(filePath);
return DetectFormat(options, file);
return TryDetectFormat(options, file, out format);
}
/// <summary>

17
src/ImageSharp/Image.FromStream.cs

@ -19,23 +19,28 @@ public abstract partial class Image
/// By reading the header on the provided stream this calculates the images format type.
/// </summary>
/// <param name="stream">The image stream to read the header from.</param>
/// <param name="format">The format type or null if none found.</param>
/// <exception cref="ArgumentNullException">The stream is null.</exception>
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
/// <returns>The format type or null if none found.</returns>
public static IImageFormat DetectFormat(Stream stream)
=> DetectFormat(DecoderOptions.Default, stream);
/// <returns>returns true when format was detected otherwise false.</returns>
public static bool TryDetectFormat(Stream stream, [NotNullWhen(true)] out IImageFormat? format)
=> TryDetectFormat(DecoderOptions.Default, stream, out format);
/// <summary>
/// By reading the header on the provided stream this calculates the images format type.
/// </summary>
/// <param name="options">The general decoder options.</param>
/// <param name="stream">The image stream to read the header from.</param>
/// <param name="format">The format type or null if none found.</param>
/// <exception cref="ArgumentNullException">The options are null.</exception>
/// <exception cref="ArgumentNullException">The stream is null.</exception>
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
/// <returns>The format type or null if none found.</returns>
public static IImageFormat DetectFormat(DecoderOptions options, Stream stream)
=> WithSeekableStream(options, stream, s => InternalDetectFormat(options.Configuration, s));
/// <returns>returns true when format was detected otherwise false.</returns>
public static bool TryDetectFormat(DecoderOptions options, Stream stream, [NotNullWhen(true)] out IImageFormat? format)
{
format = WithSeekableStream(options, stream, s => InternalDetectFormat(options.Configuration, s));
return format != null;
}
/// <summary>
/// By reading the header on the provided stream this calculates the images format type.

10
tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs

@ -122,7 +122,13 @@ public class ImageFormatManagerTests
byte[] invalidImage = { 1, 2, 3 };
Assert.Equal(Image.DetectFormat(jpegImage), JpegFormat.Instance);
Assert.True(Image.DetectFormat(invalidImage) is null);
bool resultValidImage = Image.TryDetectFormat(jpegImage, out IImageFormat format);
bool resultInvalidImage = Image.TryDetectFormat(invalidImage, out IImageFormat formatInvalid);
Assert.True(resultValidImage);
Assert.Equal(format, JpegFormat.Instance);
Assert.False(resultInvalidImage);
Assert.True(formatInvalid is null);
}
}

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

@ -34,8 +34,9 @@ public partial class ImageTests
[Fact]
public void FromBytes_GlobalConfiguration()
{
IImageFormat type = Image.DetectFormat(this.ActualImageSpan);
bool result = Image.TryDetectFormat(this.ActualImageSpan, out IImageFormat type);
Assert.True(result);
Assert.Equal(ExpectedGlobalFormat, type);
}
@ -47,15 +48,18 @@ public partial class ImageTests
Configuration = this.LocalConfiguration
};
IImageFormat type = Image.DetectFormat(options, this.ByteArray);
bool result = Image.TryDetectFormat(options, this.ByteArray, out IImageFormat type);
Assert.True(result);
Assert.Equal(this.LocalImageFormat, type);
}
[Fact]
public void FromFileSystemPath_GlobalConfiguration()
{
IImageFormat type = Image.DetectFormat(ActualImagePath);
bool result = Image.TryDetectFormat(ActualImagePath, out IImageFormat type);
Assert.True(result);
Assert.Equal(ExpectedGlobalFormat, type);
}
@ -67,7 +71,9 @@ public partial class ImageTests
Configuration = this.LocalConfiguration
};
IImageFormat type = Image.DetectFormat(options, this.MockFilePath);
bool result = Image.TryDetectFormat(options, this.MockFilePath, out IImageFormat type);
Assert.True(result);
Assert.Equal(this.LocalImageFormat, type);
}
@ -76,7 +82,9 @@ public partial class ImageTests
{
using (var stream = new MemoryStream(this.ActualImageBytes))
{
IImageFormat type = Image.DetectFormat(stream);
bool result = Image.TryDetectFormat(stream, out IImageFormat type);
Assert.True(result);
Assert.Equal(ExpectedGlobalFormat, type);
}
}
@ -89,7 +97,9 @@ public partial class ImageTests
Configuration = this.LocalConfiguration
};
IImageFormat type = Image.DetectFormat(options, this.DataStream);
bool result = Image.TryDetectFormat(options, this.DataStream, out IImageFormat type);
Assert.True(result);
Assert.Equal(this.LocalImageFormat, type);
}
@ -101,7 +111,9 @@ public partial class ImageTests
Configuration = new()
};
IImageFormat type = Image.DetectFormat(options, this.DataStream);
bool result = Image.TryDetectFormat(options, this.DataStream, out IImageFormat type);
Assert.False(result);
Assert.Null(type);
}

Loading…
Cancel
Save