diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs index 414ed05c26..96ef84510f 100644 --- a/src/ImageSharp/Image.FromBytes.cs +++ b/src/ImageSharp/Image.FromBytes.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Diagnostics.CodeAnalysis; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.PixelFormats; @@ -16,41 +15,32 @@ public abstract partial class Image /// By reading the header on the provided byte span this calculates the images format. /// /// The byte span containing encoded image data to read the header from. - /// The format or null if none found. - /// returns true when format was detected otherwise false. - public static bool TryDetectFormat(ReadOnlySpan buffer, [NotNullWhen(true)] out IImageFormat? format) - => TryDetectFormat(DecoderOptions.Default, buffer, out format); + /// The . + /// The image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static IImageFormat DetectFormat(ReadOnlySpan buffer) + => DetectFormat(DecoderOptions.Default, buffer); /// /// By reading the header on the provided byte span this calculates the images format. /// /// The general decoder options. /// The byte span containing encoded image data to read the header from. - /// The mime type or null if none found. + /// The . /// The options are null. - /// returns true when format was detected otherwise false. - public static bool TryDetectFormat(DecoderOptions options, ReadOnlySpan buffer, [NotNullWhen(true)] out IImageFormat? format) + /// The image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static unsafe IImageFormat DetectFormat(DecoderOptions options, ReadOnlySpan buffer) { Guard.NotNull(options, nameof(options.Configuration)); - Configuration configuration = options.Configuration; - int maxHeaderSize = configuration.MaxHeaderSize; - if (maxHeaderSize <= 0) - { - format = null; - return false; - } - - foreach (IImageFormatDetector detector in configuration.ImageFormatsManager.FormatDetectors) + fixed (byte* ptr = buffer) { - if (detector.TryDetectFormat(buffer, out format)) - { - return true; - } + using UnmanagedMemoryStream stream = new(ptr, buffer.Length); + return DetectFormat(options, stream); } - - format = default; - return false; } /// diff --git a/src/ImageSharp/Image.FromFile.cs b/src/ImageSharp/Image.FromFile.cs index 4bd36397f9..24a737493c 100644 --- a/src/ImageSharp/Image.FromFile.cs +++ b/src/ImageSharp/Image.FromFile.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Diagnostics.CodeAnalysis; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.PixelFormats; @@ -14,61 +13,59 @@ public abstract partial class Image { /// /// Detects the encoded image format type from the specified file. - /// A return value indicates whether the operation succeeded. /// /// The image file to open and to read the header from. - /// - /// When this method returns, contains the format that matches the given file; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// - /// if a match is found; otherwise, - public static bool TryDetectFormat(string path, [NotNullWhen(true)] out IImageFormat? format) - => TryDetectFormat(DecoderOptions.Default, path, out format); + /// The . + /// The path is null. + /// The file stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static IImageFormat DetectFormat(string path) + => DetectFormat(DecoderOptions.Default, path); /// /// Detects the encoded image format type from the specified file. - /// A return value indicates whether the operation succeeded. /// /// The general decoder options. /// The image file to open and to read the header from. - /// - /// When this method returns, contains the format that matches the given file; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// - /// if a match is found; otherwise, + /// The . /// The options are null. - public static bool TryDetectFormat(DecoderOptions options, string path, [NotNullWhen(true)] out IImageFormat? format) + /// The path is null. + /// The file stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static IImageFormat DetectFormat(DecoderOptions options, string path) { Guard.NotNull(options, nameof(options)); using Stream file = options.Configuration.FileSystem.OpenRead(path); - return TryDetectFormat(options, file, out format); + return DetectFormat(options, file); } /// /// Detects the encoded image format type from the specified file. - /// A return value indicates whether the operation succeeded. /// /// The image file to open and to read the header from. /// The token to monitor for cancellation requests. - /// A representing the asynchronous operation. - public static Task> TryDetectFormatAsync( + /// A representing the asynchronous operation. + public static Task DetectFormatAsync( string path, CancellationToken cancellationToken = default) - => TryDetectFormatAsync(DecoderOptions.Default, path, cancellationToken); + => DetectFormatAsync(DecoderOptions.Default, path, cancellationToken); /// /// Detects the encoded image format type from the specified file. - /// A return value indicates whether the operation succeeded. /// /// The general decoder options. /// The image file to open and to read the header from. /// The token to monitor for cancellation requests. + /// A representing the asynchronous operation. /// The options are null. - /// A representing the asynchronous operation. - public static async Task> TryDetectFormatAsync( + /// The path is null. + /// The file stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static async Task DetectFormatAsync( DecoderOptions options, string path, CancellationToken cancellationToken = default) @@ -76,7 +73,7 @@ public abstract partial class Image Guard.NotNull(options, nameof(options)); using Stream stream = options.Configuration.FileSystem.OpenRead(path); - return await TryDetectFormatAsync(options, stream, cancellationToken).ConfigureAwait(false); + return await DetectFormatAsync(options, stream, cancellationToken).ConfigureAwait(false); } /// diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index b48055ea75..1748ca00e0 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Diagnostics.CodeAnalysis; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.PixelFormats; @@ -15,79 +14,66 @@ public abstract partial class Image { /// /// Detects the encoded image format type from the specified stream. - /// A return value indicates whether the operation succeeded. /// /// The image stream to read the header from. - /// - /// When this method returns, contains the format that matches the given stream; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// - /// if a match is found; otherwise, + /// The . /// The stream is null. - /// The stream is not readable. - public static bool TryDetectFormat(Stream stream, [NotNullWhen(true)] out IImageFormat? format) - => TryDetectFormat(DecoderOptions.Default, stream, out format); + /// The stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static IImageFormat DetectFormat(Stream stream) + => DetectFormat(DecoderOptions.Default, stream); /// /// Detects the encoded image format type from the specified stream. - /// A return value indicates whether the operation succeeded. /// /// The general decoder options. /// The image stream to read the header from. - /// - /// When this method returns, contains the format that matches the given stream; - /// otherwise, the default value for the type of the parameter. - /// This parameter is passed uninitialized. - /// /// if a match is found; otherwise, /// The options are null. /// The stream is null. - /// The stream is not readable. - public static bool TryDetectFormat(DecoderOptions options, Stream stream, [NotNullWhen(true)] out IImageFormat? format) - { - format = WithSeekableStream(options, stream, s => InternalDetectFormat(options.Configuration, s)); - return format is not null; - } + /// The stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static IImageFormat DetectFormat(DecoderOptions options, Stream stream) + => WithSeekableStream(options, stream, s => InternalDetectFormat(options.Configuration, s)); /// /// Detects the encoded image format type from the specified stream. - /// A return value indicates whether the operation succeeded. /// /// The image stream to read the header from. /// The token to monitor for cancellation requests. + /// A representing the asynchronous operation. /// The stream is null. - /// The stream is not readable. - /// A representing the asynchronous operation. - public static Task> TryDetectFormatAsync( + /// The stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Task DetectFormatAsync( Stream stream, CancellationToken cancellationToken = default) - => TryDetectFormatAsync(DecoderOptions.Default, stream, cancellationToken); + => DetectFormatAsync(DecoderOptions.Default, stream, cancellationToken); /// /// Detects the encoded image format type from the specified stream. - /// A return value indicates whether the operation succeeded. /// /// The general decoder options. /// The image stream to read the header from. /// The token to monitor for cancellation requests. + /// A representing the asynchronous operation. /// The options are null. /// The stream is null. - /// The stream is not readable. - /// A representing the asynchronous operation. - public static async Task> TryDetectFormatAsync( + /// The stream is not readable or the image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Task DetectFormatAsync( DecoderOptions options, Stream stream, CancellationToken cancellationToken = default) - { - IImageFormat? format = await WithSeekableStreamAsync( + => WithSeekableStreamAsync( options, stream, (s, _) => Task.FromResult(InternalDetectFormat(options.Configuration, s)), - cancellationToken).ConfigureAwait(false); - - return new() { Value = format }; - } + cancellationToken); /// /// Reads the raw image information from the specified stream without fully decoding it. diff --git a/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs index 1f5686cd76..14b017bee1 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs @@ -20,7 +20,7 @@ public class ImageExtensionsTest image.SaveAsBmp(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is BmpFormat); } @@ -35,7 +35,7 @@ public class ImageExtensionsTest await image.SaveAsBmpAsync(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is BmpFormat); } @@ -50,7 +50,7 @@ public class ImageExtensionsTest image.SaveAsBmp(file, new BmpEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is BmpFormat); } @@ -65,7 +65,7 @@ public class ImageExtensionsTest await image.SaveAsBmpAsync(file, new BmpEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is BmpFormat); } @@ -81,7 +81,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is BmpFormat); } @@ -97,7 +97,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is BmpFormat); } @@ -113,7 +113,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is BmpFormat); } @@ -129,7 +129,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is BmpFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs index 3ea0c22406..97fdb38e76 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs @@ -20,7 +20,7 @@ public class ImageExtensionsTest image.SaveAsGif(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is GifFormat); } @@ -35,7 +35,7 @@ public class ImageExtensionsTest await image.SaveAsGifAsync(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is GifFormat); } @@ -50,7 +50,7 @@ public class ImageExtensionsTest image.SaveAsGif(file, new GifEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is GifFormat); } @@ -65,7 +65,7 @@ public class ImageExtensionsTest await image.SaveAsGifAsync(file, new GifEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is GifFormat); } @@ -81,7 +81,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is GifFormat); } @@ -97,7 +97,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is GifFormat); } @@ -113,7 +113,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is GifFormat); } @@ -129,7 +129,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is GifFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs index 8d65eecb9f..74c7fc1d09 100644 --- a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs +++ b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs @@ -120,15 +120,10 @@ public class ImageFormatManagerTests jpegImage = buffer.ToArray(); } - byte[] invalidImage = { 1, 2, 3 }; - - bool resultValidImage = Image.TryDetectFormat(jpegImage, out IImageFormat format); + IImageFormat format = Image.DetectFormat(jpegImage); + Assert.IsType(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); + byte[] invalidImage = { 1, 2, 3 }; + Assert.Throws(() => Image.DetectFormat(invalidImage)); } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs index 12392568fe..b1f7997323 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs @@ -21,7 +21,7 @@ public class ImageExtensionsTest image.SaveAsJpeg(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is JpegFormat); } @@ -36,7 +36,7 @@ public class ImageExtensionsTest await image.SaveAsJpegAsync(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is JpegFormat); } @@ -51,7 +51,7 @@ public class ImageExtensionsTest image.SaveAsJpeg(file, new JpegEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is JpegFormat); } @@ -66,7 +66,7 @@ public class ImageExtensionsTest await image.SaveAsJpegAsync(file, new JpegEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is JpegFormat); } @@ -82,7 +82,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is JpegFormat); } @@ -98,7 +98,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is JpegFormat); } @@ -114,7 +114,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is JpegFormat); } @@ -130,7 +130,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is JpegFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs index 1643e3ddac..5cbdd3dab0 100644 --- a/tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs @@ -20,7 +20,7 @@ public class ImageExtensionsTest image.SaveAsPbm(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PbmFormat); } @@ -35,7 +35,7 @@ public class ImageExtensionsTest await image.SaveAsPbmAsync(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PbmFormat); } @@ -50,7 +50,7 @@ public class ImageExtensionsTest image.SaveAsPbm(file, new PbmEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PbmFormat); } @@ -65,7 +65,7 @@ public class ImageExtensionsTest await image.SaveAsPbmAsync(file, new PbmEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PbmFormat); } @@ -81,7 +81,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is PbmFormat); } @@ -97,7 +97,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is PbmFormat); } @@ -113,7 +113,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is PbmFormat); } @@ -129,7 +129,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is PbmFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs index 313b779e02..a03e1a7aa4 100644 --- a/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs @@ -21,7 +21,7 @@ public class ImageExtensionsTest image.SaveAsPng(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PngFormat); } @@ -36,7 +36,7 @@ public class ImageExtensionsTest await image.SaveAsPngAsync(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PngFormat); } @@ -51,7 +51,7 @@ public class ImageExtensionsTest image.SaveAsPng(file, new PngEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PngFormat); } @@ -66,7 +66,7 @@ public class ImageExtensionsTest await image.SaveAsPngAsync(file, new PngEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PngFormat); } @@ -82,7 +82,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is PngFormat); } @@ -98,7 +98,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is PngFormat); } @@ -114,7 +114,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is PngFormat); } @@ -130,7 +130,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is PngFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs index fe9612543e..9b6daee4c9 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs @@ -20,7 +20,7 @@ public class ImageExtensionsTest image.SaveAsTga(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is TgaFormat); } @@ -35,7 +35,7 @@ public class ImageExtensionsTest await image.SaveAsTgaAsync(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is TgaFormat); } @@ -50,7 +50,7 @@ public class ImageExtensionsTest image.SaveAsTga(file, new TgaEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is TgaFormat); } @@ -65,7 +65,7 @@ public class ImageExtensionsTest await image.SaveAsTgaAsync(file, new TgaEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is TgaFormat); } @@ -81,7 +81,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is TgaFormat); } @@ -97,7 +97,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is TgaFormat); } @@ -113,7 +113,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is TgaFormat); } @@ -129,7 +129,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is TgaFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs index 4ea4b632e5..bf9e73ea6e 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs @@ -21,7 +21,7 @@ public class ImageExtensionsTest image.SaveAsTiff(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is TiffFormat); } @@ -36,7 +36,7 @@ public class ImageExtensionsTest await image.SaveAsTiffAsync(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is TiffFormat); } @@ -51,7 +51,7 @@ public class ImageExtensionsTest image.SaveAsTiff(file, new TiffEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is TiffFormat); } @@ -66,7 +66,7 @@ public class ImageExtensionsTest await image.SaveAsTiffAsync(file, new TiffEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is TiffFormat); } @@ -82,7 +82,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is TiffFormat); } @@ -98,7 +98,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is TiffFormat); } @@ -114,7 +114,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is TiffFormat); } @@ -130,7 +130,7 @@ public class ImageExtensionsTest memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is TiffFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs b/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs index 5ed6c8dc41..ea13fd7125 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs @@ -21,7 +21,7 @@ public class ImageExtensionsTests image.SaveAsWebp(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is WebpFormat); } @@ -36,7 +36,7 @@ public class ImageExtensionsTests await image.SaveAsWebpAsync(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is WebpFormat); } @@ -51,7 +51,7 @@ public class ImageExtensionsTests image.SaveAsWebp(file, new WebpEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is WebpFormat); } @@ -66,7 +66,7 @@ public class ImageExtensionsTests await image.SaveAsWebpAsync(file, new WebpEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is WebpFormat); } @@ -82,7 +82,7 @@ public class ImageExtensionsTests memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is WebpFormat); } @@ -98,7 +98,7 @@ public class ImageExtensionsTests memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is WebpFormat); } @@ -114,7 +114,7 @@ public class ImageExtensionsTests memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is WebpFormat); } @@ -130,7 +130,7 @@ public class ImageExtensionsTests memoryStream.Position = 0; - Image.TryDetectFormat(memoryStream, out IImageFormat format); + IImageFormat format = Image.DetectFormat(memoryStream); Assert.True(format is WebpFormat); } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs index 846538e42f..f9a68c8bf6 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs @@ -34,10 +34,8 @@ public partial class ImageTests [Fact] public void FromBytes_GlobalConfiguration() { - bool result = Image.TryDetectFormat(ActualImageSpan, out IImageFormat type); - - Assert.True(result); - Assert.Equal(ExpectedGlobalFormat, type); + IImageFormat format = Image.DetectFormat(ActualImageSpan); + Assert.Equal(ExpectedGlobalFormat, format); } [Fact] @@ -48,28 +46,22 @@ public partial class ImageTests Configuration = this.LocalConfiguration }; - bool result = Image.TryDetectFormat(options, this.ByteArray, out IImageFormat type); - - Assert.True(result); - Assert.Equal(this.LocalImageFormat, type); + IImageFormat format = Image.DetectFormat(options, this.ByteArray); + Assert.Equal(this.LocalImageFormat, format); } [Fact] public void FromFileSystemPath_GlobalConfiguration() { - bool result = Image.TryDetectFormat(ActualImagePath, out IImageFormat type); - - Assert.True(result); - Assert.Equal(ExpectedGlobalFormat, type); + IImageFormat format = Image.DetectFormat(ActualImagePath); + Assert.Equal(ExpectedGlobalFormat, format); } [Fact] public async Task FromFileSystemPathAsync_GlobalConfiguration() { - Attempt attempt = await Image.TryDetectFormatAsync(ActualImagePath); - - Assert.True(attempt.Success); - Assert.Equal(ExpectedGlobalFormat, attempt.Value); + IImageFormat format = await Image.DetectFormatAsync(ActualImagePath); + Assert.Equal(ExpectedGlobalFormat, format); } [Fact] @@ -80,10 +72,8 @@ public partial class ImageTests Configuration = this.LocalConfiguration }; - bool result = Image.TryDetectFormat(options, this.MockFilePath, out IImageFormat type); - - Assert.True(result); - Assert.Equal(this.LocalImageFormat, type); + IImageFormat format = Image.DetectFormat(options, this.MockFilePath); + Assert.Equal(this.LocalImageFormat, format); } [Fact] @@ -94,20 +84,17 @@ public partial class ImageTests Configuration = this.LocalConfiguration }; - Attempt attempt = await Image.TryDetectFormatAsync(options, this.MockFilePath); - - Assert.True(attempt.Success); - Assert.Equal(this.LocalImageFormat, attempt.Value); + IImageFormat format = await Image.DetectFormatAsync(options, this.MockFilePath); + Assert.Equal(this.LocalImageFormat, format); } [Fact] public void FromStream_GlobalConfiguration() { using MemoryStream stream = new(ActualImageBytes); - bool result = Image.TryDetectFormat(stream, out IImageFormat type); + IImageFormat format = Image.DetectFormat(stream); - Assert.True(result); - Assert.Equal(ExpectedGlobalFormat, type); + Assert.Equal(ExpectedGlobalFormat, format); } [Fact] @@ -118,10 +105,8 @@ public partial class ImageTests Configuration = this.LocalConfiguration }; - bool result = Image.TryDetectFormat(options, this.DataStream, out IImageFormat type); - - Assert.True(result); - Assert.Equal(this.LocalImageFormat, type); + IImageFormat format = Image.DetectFormat(options, this.DataStream); + Assert.Equal(this.LocalImageFormat, format); } [Fact] @@ -132,15 +117,15 @@ public partial class ImageTests Configuration = new() }; - Assert.Throws(() => Image.TryDetectFormat(options, this.DataStream, out IImageFormat type)); + Assert.Throws(() => Image.DetectFormat(options, this.DataStream)); } [Fact] public async Task FromStreamAsync_GlobalConfiguration() { using MemoryStream stream = new(ActualImageBytes); - Attempt attempt = await Image.TryDetectFormatAsync(new AsyncStreamWrapper(stream, () => false)); - Assert.Equal(ExpectedGlobalFormat, attempt.Value); + IImageFormat format = await Image.DetectFormatAsync(new AsyncStreamWrapper(stream, () => false)); + Assert.Equal(ExpectedGlobalFormat, format); } [Fact] @@ -151,8 +136,8 @@ public partial class ImageTests Configuration = this.LocalConfiguration }; - Attempt attempt = await Image.TryDetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false)); - Assert.Equal(this.LocalImageFormat, attempt.Value); + IImageFormat format = await Image.DetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false)); + Assert.Equal(this.LocalImageFormat, format); } [Fact] @@ -163,7 +148,7 @@ public partial class ImageTests Configuration = new() }; - return Assert.ThrowsAsync(async () => await Image.TryDetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false))); + return Assert.ThrowsAsync(async () => await Image.DetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false))); } } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Save.cs b/tests/ImageSharp.Tests/Image/ImageTests.Save.cs index 8bdbb705ad..867930c4cf 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Save.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Save.cs @@ -24,7 +24,7 @@ public partial class ImageTests image.Save(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PngFormat); } @@ -53,7 +53,7 @@ public partial class ImageTests image.Save(file, new PngEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PngFormat); } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs b/tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs index b58d0cff3c..87794f3357 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs @@ -26,7 +26,7 @@ public partial class ImageTests await image.SaveAsync(file); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PngFormat); } @@ -55,7 +55,7 @@ public partial class ImageTests await image.SaveAsync(file, new PngEncoder()); } - Image.TryDetectFormat(file, out IImageFormat format); + IImageFormat format = Image.DetectFormat(file); Assert.True(format is PngFormat); } @@ -79,7 +79,7 @@ public partial class ImageTests stream.Position = 0; - Image.TryDetectFormat(stream, out IImageFormat format2); + IImageFormat format2 = Image.DetectFormat(stream); Assert.Equal(format, format2); } diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index 638f7dfb71..29223b1fe0 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. - // ReSharper disable InconsistentNaming // ReSharper disable MemberHidesStaticFromOuterClass namespace SixLabors.ImageSharp.Tests;