From f4217013331df5a9fed27cd0a2f6dd7a81896e09 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 14 Jan 2023 17:16:28 +1000 Subject: [PATCH] Refactor Load APIs --- .../Advanced/AdvancedImageExtensions.cs | 18 +- src/ImageSharp/Formats/ImageDecoder.cs | 17 +- .../Formats/ImageExtensions.Save.cs | 64 +-- src/ImageSharp/Formats/ImageFormatManager.cs | 51 ++- src/ImageSharp/Image.Decode.cs | 91 ++-- src/ImageSharp/Image.FromBytes.cs | 166 +++---- src/ImageSharp/Image.FromFile.cs | 255 ++++------- src/ImageSharp/Image.FromStream.cs | 291 +++---------- src/ImageSharp/Image.LoadPixelData.cs | 2 +- src/ImageSharp/ImageExtensions.cs | 4 +- .../Formats/Bmp/ImageExtensionsTest.cs | 72 ++- .../Formats/GeneralFormatTests.cs | 9 +- .../Formats/Gif/ImageExtensionsTest.cs | 72 ++- .../Formats/ImageFormatManagerTests.cs | 8 +- .../Formats/Jpg/ImageExtensionsTest.cs | 72 ++- .../Formats/Jpg/JpegDecoderTests.Metadata.cs | 42 +- .../Formats/Jpg/JpegDecoderTests.cs | 10 +- .../Formats/Pbm/ImageExtensionsTest.cs | 72 ++- .../Formats/Png/ImageExtensionsTest.cs | 72 ++- .../Formats/Tga/ImageExtensionsTest.cs | 72 ++- .../Formats/Tga/TgaFileHeaderTests.cs | 2 +- .../Formats/Tiff/ImageExtensionsTest.cs | 72 ++- .../Formats/WebP/ImageExtensionsTests.cs | 72 ++- .../Image/ImageTests.DetectFormat.cs | 12 +- .../Image/ImageTests.Identify.cs | 26 +- ...d_FileSystemPath_PassLocalConfiguration.cs | 38 +- ..._FileSystemPath_UseDefaultConfiguration.cs | 13 +- ...s.Load_FromBytes_PassLocalConfiguration.cs | 36 +- ...s.Load_FromBytes_UseGlobalConfiguration.cs | 13 +- ....Load_FromStream_PassLocalConfiguration.cs | 54 ++- ...ts.Load_FromStream_ThrowsRightException.cs | 4 +- ...Load_FromStream_UseDefaultConfiguration.cs | 29 +- .../ImageSharp.Tests/Image/ImageTests.Save.cs | 34 +- .../Image/ImageTests.SaveAsync.cs | 88 ++-- tests/ImageSharp.Tests/Image/ImageTests.cs | 72 +-- .../Image/LargeImageIntegrationTests.cs | 2 +- .../Processors/Transforms/ResizeTests.cs | 412 ++++++++---------- .../TestUtilities/TestEnvironment.Formats.cs | 4 +- 38 files changed, 963 insertions(+), 1480 deletions(-) diff --git a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs index 4fa07931ed..c3a9c212ee 100644 --- a/src/ImageSharp/Advanced/AdvancedImageExtensions.cs +++ b/src/ImageSharp/Advanced/AdvancedImageExtensions.cs @@ -19,9 +19,9 @@ public static class AdvancedImageExtensions /// /// The source image. /// The target file path to save the image to. - /// The file path is null. - /// No encoder available for provided path. /// The matching . + /// The file path is null. + /// No encoder available for provided path. public static IImageEncoder DetectEncoder(this Image source, string filePath) { Guard.NotNull(filePath, nameof(filePath)); @@ -30,27 +30,27 @@ public static class AdvancedImageExtensions 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:"); + sb = sb.AppendLine(CultureInfo.InvariantCulture, $"No encoder was found for extension '{ext}'. Registered encoders include:"); foreach (IImageFormat fmt in source.GetConfiguration().ImageFormats) { - sb.AppendFormat(CultureInfo.InvariantCulture, " - {0} : {1}{2}", fmt.Name, string.Join(", ", fmt.FileExtensions), Environment.NewLine); + sb = sb.AppendFormat(CultureInfo.InvariantCulture, " - {0} : {1}{2}", fmt.Name, string.Join(", ", fmt.FileExtensions), Environment.NewLine); } - throw new NotSupportedException(sb.ToString()); + throw new UnknownImageFormatException(sb.ToString()); } - IImageEncoder? encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format); + IImageEncoder? encoder = source.GetConfiguration().ImageFormatsManager.GetEncoder(format); if (encoder is null) { StringBuilder sb = new(); - sb.AppendLine(CultureInfo.InvariantCulture, $"No encoder was found for extension '{ext}' using image format '{format.Name}'. Registered encoders include:"); + sb = sb.AppendLine(CultureInfo.InvariantCulture, $"No encoder was found for extension '{ext}' using image format '{format.Name}'. Registered encoders include:"); foreach (KeyValuePair enc in source.GetConfiguration().ImageFormatsManager.ImageEncoders) { - sb.AppendFormat(CultureInfo.InvariantCulture, " - {0} : {1}{2}", enc.Key, enc.Value.GetType().Name, Environment.NewLine); + sb = sb.AppendFormat(CultureInfo.InvariantCulture, " - {0} : {1}{2}", enc.Key, enc.Value.GetType().Name, Environment.NewLine); } - throw new NotSupportedException(sb.ToString()); + throw new UnknownImageFormatException(sb.ToString()); } return encoder; diff --git a/src/ImageSharp/Formats/ImageDecoder.cs b/src/ImageSharp/Formats/ImageDecoder.cs index dfb3761977..715c2f5416 100644 --- a/src/ImageSharp/Formats/ImageDecoder.cs +++ b/src/ImageSharp/Formats/ImageDecoder.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.PixelFormats; @@ -150,7 +149,7 @@ public abstract class ImageDecoder : IImageDecoder { ResizeOptions resizeOptions = new() { - Size = options.TargetSize.Value, + Size = options.TargetSize!.Value, Sampler = options.Sampler, Mode = ResizeMode.Max }; @@ -290,8 +289,18 @@ public abstract class ImageDecoder : IImageDecoder } internal void SetDecoderFormat(Configuration configuration, Image image) - => image.Metadata.DecodedImageFormat = configuration.ImageFormatsManager.FindFormatByDecoder(this); + { + if (configuration.ImageFormatsManager.TryFindFormatByDecoder(this, out IImageFormat? format)) + { + image.Metadata.DecodedImageFormat = format; + } + } internal void SetDecoderFormat(Configuration configuration, ImageInfo info) - => info.Metadata.DecodedImageFormat = configuration.ImageFormatsManager.FindFormatByDecoder(this); + { + if (configuration.ImageFormatsManager.TryFindFormatByDecoder(this, out IImageFormat? format)) + { + info.Metadata.DecodedImageFormat = format; + } + } } diff --git a/src/ImageSharp/Formats/ImageExtensions.Save.cs b/src/ImageSharp/Formats/ImageExtensions.Save.cs index 6249f8dc77..71458333f0 100644 --- a/src/ImageSharp/Formats/ImageExtensions.Save.cs +++ b/src/ImageSharp/Formats/ImageExtensions.Save.cs @@ -58,7 +58,7 @@ public static partial class ImageExtensions public static void SaveAsBmp(this Image source, string path, BmpEncoder encoder) => source.Save( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(BmpFormat.Instance)); /// /// Saves the image to the given stream with the Bmp format. @@ -72,7 +72,7 @@ public static partial class ImageExtensions public static Task SaveAsBmpAsync(this Image source, string path, BmpEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(BmpFormat.Instance), cancellationToken); /// @@ -105,7 +105,7 @@ public static partial class ImageExtensions public static void SaveAsBmp(this Image source, Stream stream, BmpEncoder encoder) => source.Save( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(BmpFormat.Instance)); /// /// Saves the image to the given stream with the Bmp format. @@ -119,7 +119,7 @@ public static partial class ImageExtensions public static Task SaveAsBmpAsync(this Image source, Stream stream, BmpEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(BmpFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(BmpFormat.Instance), cancellationToken); /// @@ -160,7 +160,7 @@ public static partial class ImageExtensions public static void SaveAsGif(this Image source, string path, GifEncoder encoder) => source.Save( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(GifFormat.Instance)); /// /// Saves the image to the given stream with the Gif format. @@ -174,7 +174,7 @@ public static partial class ImageExtensions public static Task SaveAsGifAsync(this Image source, string path, GifEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(GifFormat.Instance), cancellationToken); /// @@ -207,7 +207,7 @@ public static partial class ImageExtensions public static void SaveAsGif(this Image source, Stream stream, GifEncoder encoder) => source.Save( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(GifFormat.Instance)); /// /// Saves the image to the given stream with the Gif format. @@ -221,7 +221,7 @@ public static partial class ImageExtensions public static Task SaveAsGifAsync(this Image source, Stream stream, GifEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(GifFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(GifFormat.Instance), cancellationToken); /// @@ -262,7 +262,7 @@ public static partial class ImageExtensions public static void SaveAsJpeg(this Image source, string path, JpegEncoder encoder) => source.Save( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(JpegFormat.Instance)); /// /// Saves the image to the given stream with the Jpeg format. @@ -276,7 +276,7 @@ public static partial class ImageExtensions public static Task SaveAsJpegAsync(this Image source, string path, JpegEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(JpegFormat.Instance), cancellationToken); /// @@ -309,7 +309,7 @@ public static partial class ImageExtensions public static void SaveAsJpeg(this Image source, Stream stream, JpegEncoder encoder) => source.Save( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(JpegFormat.Instance)); /// /// Saves the image to the given stream with the Jpeg format. @@ -323,7 +323,7 @@ public static partial class ImageExtensions public static Task SaveAsJpegAsync(this Image source, Stream stream, JpegEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(JpegFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(JpegFormat.Instance), cancellationToken); /// @@ -364,7 +364,7 @@ public static partial class ImageExtensions public static void SaveAsPbm(this Image source, string path, PbmEncoder encoder) => source.Save( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PbmFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(PbmFormat.Instance)); /// /// Saves the image to the given stream with the Pbm format. @@ -378,7 +378,7 @@ public static partial class ImageExtensions public static Task SaveAsPbmAsync(this Image source, string path, PbmEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PbmFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(PbmFormat.Instance), cancellationToken); /// @@ -411,7 +411,7 @@ public static partial class ImageExtensions public static void SaveAsPbm(this Image source, Stream stream, PbmEncoder encoder) => source.Save( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PbmFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(PbmFormat.Instance)); /// /// Saves the image to the given stream with the Pbm format. @@ -425,7 +425,7 @@ public static partial class ImageExtensions public static Task SaveAsPbmAsync(this Image source, Stream stream, PbmEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PbmFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(PbmFormat.Instance), cancellationToken); /// @@ -466,7 +466,7 @@ public static partial class ImageExtensions public static void SaveAsPng(this Image source, string path, PngEncoder encoder) => source.Save( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(PngFormat.Instance)); /// /// Saves the image to the given stream with the Png format. @@ -480,7 +480,7 @@ public static partial class ImageExtensions public static Task SaveAsPngAsync(this Image source, string path, PngEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(PngFormat.Instance), cancellationToken); /// @@ -513,7 +513,7 @@ public static partial class ImageExtensions public static void SaveAsPng(this Image source, Stream stream, PngEncoder encoder) => source.Save( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(PngFormat.Instance)); /// /// Saves the image to the given stream with the Png format. @@ -527,7 +527,7 @@ public static partial class ImageExtensions public static Task SaveAsPngAsync(this Image source, Stream stream, PngEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(PngFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(PngFormat.Instance), cancellationToken); /// @@ -568,7 +568,7 @@ public static partial class ImageExtensions public static void SaveAsTga(this Image source, string path, TgaEncoder encoder) => source.Save( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TgaFormat.Instance)); /// /// Saves the image to the given stream with the Tga format. @@ -582,7 +582,7 @@ public static partial class ImageExtensions public static Task SaveAsTgaAsync(this Image source, string path, TgaEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TgaFormat.Instance), cancellationToken); /// @@ -615,7 +615,7 @@ public static partial class ImageExtensions public static void SaveAsTga(this Image source, Stream stream, TgaEncoder encoder) => source.Save( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TgaFormat.Instance)); /// /// Saves the image to the given stream with the Tga format. @@ -629,7 +629,7 @@ public static partial class ImageExtensions public static Task SaveAsTgaAsync(this Image source, Stream stream, TgaEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TgaFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TgaFormat.Instance), cancellationToken); /// @@ -670,7 +670,7 @@ public static partial class ImageExtensions public static void SaveAsWebp(this Image source, string path, WebpEncoder encoder) => source.Save( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(WebpFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(WebpFormat.Instance)); /// /// Saves the image to the given stream with the Webp format. @@ -684,7 +684,7 @@ public static partial class ImageExtensions public static Task SaveAsWebpAsync(this Image source, string path, WebpEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(WebpFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(WebpFormat.Instance), cancellationToken); /// @@ -717,7 +717,7 @@ public static partial class ImageExtensions public static void SaveAsWebp(this Image source, Stream stream, WebpEncoder encoder) => source.Save( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(WebpFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(WebpFormat.Instance)); /// /// Saves the image to the given stream with the Webp format. @@ -731,7 +731,7 @@ public static partial class ImageExtensions public static Task SaveAsWebpAsync(this Image source, Stream stream, WebpEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(WebpFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(WebpFormat.Instance), cancellationToken); /// @@ -772,7 +772,7 @@ public static partial class ImageExtensions public static void SaveAsTiff(this Image source, string path, TiffEncoder encoder) => source.Save( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TiffFormat.Instance)); /// /// Saves the image to the given stream with the Tiff format. @@ -786,7 +786,7 @@ public static partial class ImageExtensions public static Task SaveAsTiffAsync(this Image source, string path, TiffEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( path, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TiffFormat.Instance), cancellationToken); /// @@ -819,7 +819,7 @@ public static partial class ImageExtensions public static void SaveAsTiff(this Image source, Stream stream, TiffEncoder encoder) => source.Save( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance)); + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TiffFormat.Instance)); /// /// Saves the image to the given stream with the Tiff format. @@ -833,7 +833,7 @@ public static partial class ImageExtensions public static Task SaveAsTiffAsync(this Image source, Stream stream, TiffEncoder encoder, CancellationToken cancellationToken = default) => source.SaveAsync( stream, - encoder ?? source.GetConfiguration().ImageFormatsManager.FindEncoder(TiffFormat.Instance), + encoder ?? source.GetConfiguration().ImageFormatsManager.GetEncoder(TiffFormat.Instance), cancellationToken); } diff --git a/src/ImageSharp/Formats/ImageFormatManager.cs b/src/ImageSharp/Formats/ImageFormatManager.cs index 5eeb1b8120..8b4e320c5a 100644 --- a/src/ImageSharp/Formats/ImageFormatManager.cs +++ b/src/ImageSharp/Formats/ImageFormatManager.cs @@ -3,6 +3,8 @@ using System.Collections.Concurrent; using System.Diagnostics.CodeAnalysis; +using System.Globalization; +using System.Text; namespace SixLabors.ImageSharp.Formats; @@ -129,8 +131,11 @@ public class ImageFormatManager return format is not null; } - internal IImageFormat? FindFormatByDecoder(IImageDecoder decoder) - => this.mimeTypeDecoders.FirstOrDefault(x => x.Value.GetType() == decoder.GetType()).Key; + internal bool TryFindFormatByDecoder(IImageDecoder decoder, [NotNullWhen(true)] out IImageFormat? format) + { + format = this.mimeTypeDecoders.FirstOrDefault(x => x.Value.GetType() == decoder.GetType()).Key; + return format is not null; + } /// /// Sets a specific image encoder as the encoder for a specific image format. @@ -178,32 +183,54 @@ public class ImageFormatManager /// For the specified mime type find the decoder. /// /// The format to discover - /// The if found otherwise null - public IImageDecoder? FindDecoder(IImageFormat format) + /// The . + /// The format is not registered. + public IImageDecoder GetDecoder(IImageFormat format) { Guard.NotNull(format, nameof(format)); - return this.mimeTypeDecoders.TryGetValue(format, out IImageDecoder? decoder) - ? decoder - : null; + if (!this.mimeTypeDecoders.TryGetValue(format, out IImageDecoder? decoder)) + { + ThrowInvalidDecoder(this); + } + + return decoder; } /// /// For the specified mime type find the encoder. /// /// The format to discover - /// The if found otherwise null - public IImageEncoder? FindEncoder(IImageFormat format) + /// The . + /// The format is not registered. + public IImageEncoder GetEncoder(IImageFormat format) { Guard.NotNull(format, nameof(format)); - return this.mimeTypeEncoders.TryGetValue(format, out IImageEncoder? encoder) - ? encoder - : null; + if (!this.mimeTypeEncoders.TryGetValue(format, out IImageEncoder? encoder)) + { + ThrowInvalidDecoder(this); + } + + return encoder; } /// /// Sets the max header size. /// private void SetMaxHeaderSize() => this.MaxHeaderSize = this.imageFormatDetectors.Max(x => x.HeaderSize); + + [DoesNotReturn] + internal static void ThrowInvalidDecoder(ImageFormatManager manager) + { + StringBuilder sb = new(); + sb = sb.AppendLine("Image cannot be loaded. Available decoders:"); + + foreach (KeyValuePair val in manager.ImageDecoders) + { + sb = sb.AppendFormat(CultureInfo.InvariantCulture, " - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine); + } + + throw new UnknownImageFormatException(sb.ToString()); + } } diff --git a/src/ImageSharp/Image.Decode.cs b/src/ImageSharp/Image.Decode.cs index 9a28440601..ae38144c02 100644 --- a/src/ImageSharp/Image.Decode.cs +++ b/src/ImageSharp/Image.Decode.cs @@ -44,14 +44,15 @@ public abstract partial class Image /// The general configuration. /// The image stream to read the header from. /// The mime type or null if none found. - private static IImageFormat? InternalDetectFormat(Configuration configuration, Stream stream) + /// The input format is not recognized. + private static IImageFormat InternalDetectFormat(Configuration configuration, Stream stream) { // We take a minimum of the stream length vs the max header size and always check below // to ensure that only formats that headers fit within the given buffer length are tested. int headerSize = (int)Math.Min(configuration.MaxHeaderSize, stream.Length); if (headerSize <= 0) { - return null; + ImageFormatManager.ThrowInvalidDecoder(configuration.ImageFormatsManager); } // Header sizes are so small, that headersBuffer will be always stackalloc-ed in practice, @@ -85,7 +86,12 @@ public abstract partial class Image } } - return format; + if (format is null) + { + ImageFormatManager.ThrowInvalidDecoder(configuration.ImageFormatsManager); + } + + return format!; } /// @@ -93,13 +99,11 @@ public abstract partial class Image /// /// The general decoder options. /// The image stream to read the header from. - /// The or . - private static IImageDecoder? DiscoverDecoder(DecoderOptions options, Stream stream) + /// The . + private static IImageDecoder DiscoverDecoder(DecoderOptions options, Stream stream) { - IImageFormat? format = InternalDetectFormat(options.Configuration, stream); - return format is not null - ? options.Configuration.ImageFormatsManager.FindDecoder(format) - : null; + IImageFormat format = InternalDetectFormat(options.Configuration, stream); + return options.Configuration.ImageFormatsManager.GetDecoder(format); } /// @@ -111,56 +115,36 @@ public abstract partial class Image /// /// A new . /// - private static Image? Decode(DecoderOptions options, Stream stream) + private static Image Decode(DecoderOptions options, Stream stream) where TPixel : unmanaged, IPixel { - IImageDecoder? decoder = DiscoverDecoder(options, stream); - if (decoder is null) - { - return null; - } - + IImageDecoder decoder = DiscoverDecoder(options, stream); return decoder.Decode(options, stream); } - private static async Task?> DecodeAsync( + private static Task> DecodeAsync( DecoderOptions options, Stream stream, CancellationToken cancellationToken) where TPixel : unmanaged, IPixel { - IImageDecoder? decoder = DiscoverDecoder(options, stream); - if (decoder is null) - { - return null; - } - - return await decoder.DecodeAsync(options, stream, cancellationToken).ConfigureAwait(false); + IImageDecoder decoder = DiscoverDecoder(options, stream); + return decoder.DecodeAsync(options, stream, cancellationToken); } - private static Image? Decode(DecoderOptions options, Stream stream) + private static Image Decode(DecoderOptions options, Stream stream) { - IImageDecoder? decoder = DiscoverDecoder(options, stream); - if (decoder is null) - { - return null; - } - + IImageDecoder decoder = DiscoverDecoder(options, stream); return decoder.Decode(options, stream); } - private static async Task DecodeAsync( + private static Task DecodeAsync( DecoderOptions options, Stream stream, CancellationToken cancellationToken) { - IImageDecoder? decoder = DiscoverDecoder(options, stream); - if (decoder is null) - { - return null; - } - - return await decoder.DecodeAsync(options, stream, cancellationToken).ConfigureAwait(false); + IImageDecoder decoder = DiscoverDecoder(options, stream); + return decoder.DecodeAsync(options, stream, cancellationToken); } /// @@ -168,17 +152,10 @@ public abstract partial class Image /// /// The general decoder options. /// The stream. - /// - /// The or null if a suitable info detector is not found. - /// - private static ImageInfo? InternalIdentify(DecoderOptions options, Stream stream) + /// The . + private static ImageInfo InternalIdentify(DecoderOptions options, Stream stream) { - IImageDecoder? decoder = DiscoverDecoder(options, stream); - if (decoder is null) - { - return null; - } - + IImageDecoder decoder = DiscoverDecoder(options, stream); return decoder.Identify(options, stream); } @@ -188,21 +165,13 @@ public abstract partial class Image /// The general decoder options. /// The stream. /// The token to monitor for cancellation requests. - /// - /// The or null if a suitable info detector is not found. - /// - private static async Task InternalIdentifyAsync( + /// The . + private static Task InternalIdentifyAsync( DecoderOptions options, Stream stream, CancellationToken cancellationToken) { - IImageDecoder? decoder = DiscoverDecoder(options, stream); - - if (decoder is null) - { - return null; - } - - return await decoder.IdentifyAsync(options, stream, cancellationToken).ConfigureAwait(false); + IImageDecoder decoder = DiscoverDecoder(options, stream); + return decoder.IdentifyAsync(options, stream, cancellationToken); } } diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs index 9218128340..0c209214de 100644 --- a/src/ImageSharp/Image.FromBytes.cs +++ b/src/ImageSharp/Image.FromBytes.cs @@ -15,21 +15,21 @@ 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 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 data, [NotNullWhen(true)] out IImageFormat? format) - => TryDetectFormat(DecoderOptions.Default, data, out format); + public static bool TryDetectFormat(ReadOnlySpan buffer, [NotNullWhen(true)] out IImageFormat? format) + => TryDetectFormat(DecoderOptions.Default, buffer, out format); /// /// 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 byte span containing encoded image data to read the header from. /// The mime type or null if none found. /// The options are null. /// returns true when format was detected otherwise false. - public static bool TryDetectFormat(DecoderOptions options, ReadOnlySpan data, [NotNullWhen(true)] out IImageFormat? format) + public static bool TryDetectFormat(DecoderOptions options, ReadOnlySpan buffer, [NotNullWhen(true)] out IImageFormat? format) { Guard.NotNull(options, nameof(options.Configuration)); @@ -43,7 +43,7 @@ public abstract partial class Image foreach (IImageFormatDetector detector in configuration.ImageFormatsManager.FormatDetectors) { - if (detector.TryDetectFormat(data, out format)) + if (detector.TryDetectFormat(buffer, out format)) { return true; } @@ -57,7 +57,7 @@ public abstract partial class Image /// Reads the raw image information from the specified stream without fully decoding it. /// A return value indicates whether the operation succeeded. /// - /// The byte array containing encoded image data to read the header from. + /// The byte array containing encoded image data to read the header from. /// /// When this method returns, contains the raw image information; /// otherwise, the default value for the type of the parameter. @@ -66,15 +66,15 @@ public abstract partial class Image /// if the information can be read; otherwise, /// The data is null. /// The data is not readable. - public static bool TryIdentify(ReadOnlySpan data, [NotNullWhen(true)] out ImageInfo? info) - => TryIdentify(DecoderOptions.Default, data, out info); + public static bool TryIdentify(ReadOnlySpan buffer, [NotNullWhen(true)] out ImageInfo? info) + => TryIdentify(DecoderOptions.Default, buffer, out info); /// /// Reads the raw image information from the specified span of bytes without fully decoding it. /// A return value indicates whether the operation succeeded. /// /// The general decoder options. - /// The byte span containing encoded image data to read the header from. + /// The byte span containing encoded image data to read the header from. /// /// When this method returns, contains the raw image information; /// otherwise, the default value for the type of the parameter. @@ -84,141 +84,79 @@ public abstract partial class Image /// The configuration is null. /// The data is null. /// The data is not readable. - public static unsafe bool TryIdentify(DecoderOptions options, ReadOnlySpan data, [NotNullWhen(true)] out ImageInfo? info) + public static unsafe bool TryIdentify(DecoderOptions options, ReadOnlySpan buffer, [NotNullWhen(true)] out ImageInfo? info) { - fixed (byte* ptr = data) + fixed (byte* ptr = buffer) { - using UnmanagedMemoryStream stream = new(ptr, data.Length); + using UnmanagedMemoryStream stream = new(ptr, buffer.Length); return TryIdentify(options, stream, out info); } } /// - /// Load a new instance of from the given encoded byte span. + /// Creates a new instance of the class from the given byte span. + /// The pixel format is automatically determined by the decoder. /// - /// The byte span containing encoded image data. - /// The pixel format. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. - /// A new . - public static Image Load(ReadOnlySpan data) - where TPixel : unmanaged, IPixel - => Load(DecoderOptions.Default, data); - - /// - /// Load a new instance of from the given encoded byte span. - /// - /// The byte span containing image data. - /// The mime type of the decoded image. - /// The pixel format. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. - /// A new . - public static Image Load(ReadOnlySpan data, out IImageFormat format) - where TPixel : unmanaged, IPixel - => Load(DecoderOptions.Default, data, out format); + /// The byte span containing encoded image data. + /// . + /// The image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + /// The . + public static Image Load(ReadOnlySpan buffer) + => Load(DecoderOptions.Default, buffer); /// - /// Load a new instance of from the given encoded byte span. + /// Creates a new instance of the class from the given byte span. + /// The pixel format is automatically determined by the decoder. /// /// The general decoder options. - /// The byte span containing encoded image data. - /// The pixel format. + /// The byte span containing encoded image data. + /// . /// The options are null. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. - /// A new . - public static unsafe Image Load(DecoderOptions options, ReadOnlySpan data) - where TPixel : unmanaged, IPixel + /// The image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static unsafe Image Load(DecoderOptions options, ReadOnlySpan buffer) { - fixed (byte* ptr = data) + fixed (byte* ptr = buffer) { - using UnmanagedMemoryStream stream = new(ptr, data.Length); - return Load(options, stream); + using UnmanagedMemoryStream stream = new(ptr, buffer.Length); + return Load(options, stream); } } /// - /// Load a new instance of from the given encoded byte span. + /// Creates a new instance of the class from the given byte span. /// - /// The general decoder options. - /// The byte span containing image data. - /// The of the decoded image. /// The pixel format. - /// The options are null. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. - /// A new . - public static unsafe Image Load( - DecoderOptions options, - ReadOnlySpan data, - out IImageFormat format) + /// The byte span containing encoded image data. + /// . + /// The image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Image Load(ReadOnlySpan data) where TPixel : unmanaged, IPixel - { - fixed (byte* ptr = data) - { - using UnmanagedMemoryStream stream = new(ptr, data.Length); - return Load(options, stream, out format); - } - } - - /// - /// Load a new instance of from the given encoded byte span. - /// - /// The byte span containing image data. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. - /// The . - public static Image Load(ReadOnlySpan data) - => Load(DecoderOptions.Default, data); - - /// - /// Load a new instance of from the given encoded byte array. - /// - /// The byte span containing image data. - /// The detected format. - /// The decoder is null. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. - /// The . - public static Image Load(ReadOnlySpan data, out IImageFormat format) - => Load(DecoderOptions.Default, data, out format); - - /// - /// Decodes a new instance of from the given encoded byte span. - /// - /// The general decoder options. - /// The byte span containing image data. - /// The . - public static Image Load(DecoderOptions options, ReadOnlySpan data) - => Load(options, data, out _); + => Load(DecoderOptions.Default, data); /// - /// Load a new instance of from the given encoded byte span. + /// Creates a new instance of the class from the given byte span. /// + /// The pixel format. /// The general decoder options. - /// The byte span containing image data. - /// The of the decoded image.> + /// The byte span containing encoded image data. + /// . /// The options are null. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. - /// The . - public static unsafe Image Load( - DecoderOptions options, - ReadOnlySpan data, - out IImageFormat format) + /// The image format is not supported. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static unsafe Image Load(DecoderOptions options, ReadOnlySpan data) + where TPixel : unmanaged, IPixel { fixed (byte* ptr = data) { using UnmanagedMemoryStream stream = new(ptr, data.Length); - return Load(options, stream, out format); + return Load(options, stream); } } } diff --git a/src/ImageSharp/Image.FromFile.cs b/src/ImageSharp/Image.FromFile.cs index 515d8408c2..a8ed73c82e 100644 --- a/src/ImageSharp/Image.FromFile.cs +++ b/src/ImageSharp/Image.FromFile.cs @@ -16,22 +16,22 @@ 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. + /// 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 filePath, [NotNullWhen(true)] out IImageFormat? format) - => TryDetectFormat(DecoderOptions.Default, filePath, out format); + public static bool TryDetectFormat(string path, [NotNullWhen(true)] out IImageFormat? format) + => TryDetectFormat(DecoderOptions.Default, path, out format); /// /// 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 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. @@ -39,11 +39,11 @@ public abstract partial class Image /// /// if a match is found; otherwise, /// The options are null. - public static bool TryDetectFormat(DecoderOptions options, string filePath, [NotNullWhen(true)] out IImageFormat? format) + public static bool TryDetectFormat(DecoderOptions options, string path, [NotNullWhen(true)] out IImageFormat? format) { Guard.NotNull(options, nameof(options)); - using Stream file = options.Configuration.FileSystem.OpenRead(filePath); + using Stream file = options.Configuration.FileSystem.OpenRead(path); return TryDetectFormat(options, file, out format); } @@ -51,31 +51,31 @@ 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. + /// 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( - string filePath, + string path, CancellationToken cancellationToken = default) - => TryDetectFormatAsync(DecoderOptions.Default, filePath, cancellationToken); + => TryDetectFormatAsync(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 image file to open and to read the header from. /// The token to monitor for cancellation requests. /// The options are null. /// A representing the asynchronous operation. public static async Task> TryDetectFormatAsync( DecoderOptions options, - string filePath, + string path, CancellationToken cancellationToken = default) { Guard.NotNull(options, nameof(options)); - using Stream stream = options.Configuration.FileSystem.OpenRead(filePath); + using Stream stream = options.Configuration.FileSystem.OpenRead(path); return await TryDetectFormatAsync(options, stream, cancellationToken).ConfigureAwait(false); } @@ -83,22 +83,22 @@ public abstract partial class Image /// Reads the raw image information from the specified file path without fully decoding it. /// A return value indicates whether the operation succeeded. /// - /// The image file to open and to read the header from. + /// The image file to open and to read the header from. /// /// When this method returns, contains the raw image information; /// otherwise, the default value for the type of the parameter. /// This parameter is passed uninitialized. /// /// if the information can be read; otherwise, - public static bool TryIdentify(string filePath, [NotNullWhen(true)] out ImageInfo? info) - => TryIdentify(DecoderOptions.Default, filePath, out info); + public static bool TryIdentify(string path, [NotNullWhen(true)] out ImageInfo? info) + => TryIdentify(DecoderOptions.Default, path, out info); /// /// Reads the raw image information from the specified file path without fully decoding it. /// A return value indicates whether the operation succeeded. /// /// The general decoder options. - /// The image file to open and to read the header from. + /// The image file to open and to read the header from. /// /// When this method returns, contains the raw image information; /// otherwise, the default value for the type of the parameter. @@ -106,11 +106,11 @@ public abstract partial class Image /// /// if the information can be read; otherwise, /// The options are null. - public static bool TryIdentify(DecoderOptions options, string filePath, [NotNullWhen(true)] out ImageInfo? info) + public static bool TryIdentify(DecoderOptions options, string path, [NotNullWhen(true)] out ImageInfo? info) { Guard.NotNull(options, nameof(options)); - using Stream stream = options.Configuration.FileSystem.OpenRead(filePath); + using Stream stream = options.Configuration.FileSystem.OpenRead(path); return TryIdentify(options, stream, out info); } @@ -118,197 +118,134 @@ public abstract partial class Image /// Reads the raw image information from the specified stream without fully decoding it. /// A return value indicates whether the operation succeeded. /// - /// The image file to open and to read the header from. + /// The image file to open and to read the header from. /// The token to monitor for cancellation requests. - /// The configuration is null. + /// The options are null. /// /// The representing the asynchronous operation. /// public static Task> TryIdentifyAsync( - string filePath, + string path, CancellationToken cancellationToken = default) - => TryIdentifyAsync(DecoderOptions.Default, filePath, cancellationToken); + => TryIdentifyAsync(DecoderOptions.Default, path, cancellationToken); /// /// Reads the raw image information from the specified stream without fully decoding it. /// A return value indicates whether the operation succeeded. /// /// The general decoder options. - /// The image file to open and to read the header from. + /// The image file to open and to read the header from. /// The token to monitor for cancellation requests. - /// The configuration is null. + /// The options are null. /// /// The representing the asynchronous operation. /// public static async Task> TryIdentifyAsync( DecoderOptions options, - string filePath, + string path, CancellationToken cancellationToken = default) { Guard.NotNull(options, nameof(options)); - using Stream stream = options.Configuration.FileSystem.OpenRead(filePath); + using Stream stream = options.Configuration.FileSystem.OpenRead(path); return await TryIdentifyAsync(options, stream, cancellationToken).ConfigureAwait(false); } /// - /// Create a new instance of the class from the given file. + /// Creates a new instance of the class from the given file path. + /// The pixel format is automatically determined by the decoder. /// /// The file path to the image. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// 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 Image Load(string path) => Load(DecoderOptions.Default, path); /// - /// Create a new instance of the class from the given file. - /// - /// The file path to the image. - /// The mime type of the decoded image. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// A new . - public static Image Load(string path, out IImageFormat format) - => Load(DecoderOptions.Default, path, out format); - - /// - /// Create a new instance of the class from the given file. + /// Creates a new instance of the class from the given file path. + /// The pixel format is automatically determined by the decoder. /// /// The general decoder options. /// The file path to the image. - /// The configuration is null. + /// . + /// The options are null. /// The path is null. - /// Image format not recognised. - /// Image format is not supported. - /// Image contains invalid content. - /// The . + /// 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 Image Load(DecoderOptions options, string path) - => Load(options, path, out _); - - /// - /// Create a new instance of the class from the given file. - /// - /// The general decoder options. - /// The file path to the image. - /// The token to monitor for cancellation requests. - /// The configuration is null. - /// The path is null. - /// Image format not recognised. - /// Image format is not supported. - /// Image contains invalid content. - /// A representing the asynchronous operation. - public static async Task LoadAsync( - DecoderOptions options, - string path, - CancellationToken cancellationToken = default) { + Guard.NotNull(options, nameof(options)); + Guard.NotNull(path, nameof(path)); + using Stream stream = options.Configuration.FileSystem.OpenRead(path); - (Image img, _) = await LoadWithFormatAsync(options, stream, cancellationToken) - .ConfigureAwait(false); - return img; + return Load(options, stream); } /// - /// Create a new instance of the class from the given file. + /// Creates a new instance of the class from the given file path. + /// The pixel format is automatically determined by the decoder. /// /// The file path to the image. /// The token to monitor for cancellation requests. - /// The configuration is null. - /// The path is null. - /// The decoder is null. - /// Image format not recognised. - /// Image format is not supported. - /// Image contains invalid content. /// A representing the asynchronous operation. + /// 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 Task LoadAsync(string path, CancellationToken cancellationToken = default) => LoadAsync(DecoderOptions.Default, path, cancellationToken); /// - /// Create a new instance of the class from the given file. - /// - /// The file path to the image. - /// The token to monitor for cancellation requests. - /// The configuration is null. - /// The path is null. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. - /// The pixel format. - /// A representing the asynchronous operation. - public static Task> LoadAsync(string path, CancellationToken cancellationToken = default) - where TPixel : unmanaged, IPixel - => LoadAsync(DecoderOptions.Default, path, cancellationToken); - - /// - /// Create a new instance of the class from the given file. + /// Creates a new instance of the class from the given file path. + /// The pixel format is automatically determined by the decoder. /// /// The general decoder options. /// The file path to the image. /// The token to monitor for cancellation requests. - /// The configuration is null. - /// The path is null. - /// Image format not recognised. - /// Image format is not supported. - /// Image contains invalid content. - /// The pixel format. /// A representing the asynchronous operation. - public static async Task> LoadAsync( + /// The options are null. + /// 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 LoadAsync( DecoderOptions options, string path, CancellationToken cancellationToken = default) - where TPixel : unmanaged, IPixel { - Guard.NotNull(options, nameof(options)); - using Stream stream = options.Configuration.FileSystem.OpenRead(path); - (Image img, _) = - await LoadWithFormatAsync(options, stream, cancellationToken).ConfigureAwait(false); - return img; + return await LoadAsync(options, stream, cancellationToken).ConfigureAwait(false); } /// - /// Create a new instance of the class from the given file. + /// Creates a new instance of the class from the given file path. /// + /// The pixel format. /// The file path to the image. + /// . /// The path is null. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. - /// The pixel format. - /// A new . + /// 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 Image Load(string path) where TPixel : unmanaged, IPixel => Load(DecoderOptions.Default, path); /// - /// Create a new instance of the class from the given file. + /// Creates a new instance of the class from the given file path. /// - /// The file path to the image. - /// The mime type of the decoded image. - /// The path is null. - /// Image format not recognised. - /// Image contains invalid content. - /// Image format is not supported. /// The pixel format. - /// A new . - public static Image Load(string path, out IImageFormat format) - where TPixel : unmanaged, IPixel - => Load(DecoderOptions.Default, path, out format); - - /// - /// Create a new instance of the class from the given file. - /// /// The general decoder options. /// The file path to the image. - /// The configuration is null. + /// . + /// The options are null. /// The path is null. - /// Image format not recognised. - /// Image format is not supported. - /// Image contains invalid content. - /// The pixel format. - /// A new . + /// 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 Image Load(DecoderOptions options, string path) where TPixel : unmanaged, IPixel { @@ -320,47 +257,43 @@ public abstract partial class Image } /// - /// Create a new instance of the class from the given file. + /// Creates a new instance of the class from the given file path. /// - /// The general decoder options. + /// The pixel format. /// The file path to the image. - /// The mime type of the decoded image. - /// The configuration is null. + /// The token to monitor for cancellation requests. + /// A representing the asynchronous operation. /// The path is null. - /// Image format not recognised. - /// Image format is not supported. - /// Image contains invalid content. - /// The pixel format. - /// A new . - public static Image Load(DecoderOptions options, string path, out IImageFormat format) + /// 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 Task> LoadAsync(string path, CancellationToken cancellationToken = default) where TPixel : unmanaged, IPixel - { - Guard.NotNull(options, nameof(options)); - Guard.NotNull(path, nameof(path)); - - using Stream stream = options.Configuration.FileSystem.OpenRead(path); - return Load(options, stream, out format); - } + => LoadAsync(DecoderOptions.Default, path, cancellationToken); /// - /// Create a new instance of the class from the given file. - /// The pixel type is selected by the decoder. + /// Creates a new instance of the class from the given file path. /// + /// The pixel format. /// The general decoder options. /// The file path to the image. - /// The mime type of the decoded image. - /// The configuration is null. + /// The token to monitor for cancellation requests. + /// A representing the asynchronous operation. + /// The options are null. /// The path is null. - /// Image format not recognised. - /// Image format is not supported. - /// Image contains invalid content. - /// A new . - public static Image Load(DecoderOptions options, string path, out IImageFormat format) + /// 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> LoadAsync( + DecoderOptions options, + string path, + CancellationToken cancellationToken = default) + where TPixel : unmanaged, IPixel { Guard.NotNull(options, nameof(options)); Guard.NotNull(path, nameof(path)); using Stream stream = options.Configuration.FileSystem.OpenRead(path); - return Load(options, stream, out format); + return await LoadAsync(options, stream, cancellationToken).ConfigureAwait(false); } } diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index fa9285ef3a..9da77d66e4 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -2,8 +2,6 @@ // Licensed under the Six Labors Split License. using System.Diagnostics.CodeAnalysis; -using System.Globalization; -using System.Text; using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.PixelFormats; @@ -136,7 +134,6 @@ public abstract partial class Image /// /// The image stream to read the information from. /// The token to monitor for cancellation requests. - /// The options are null. /// The stream is null. /// The stream is not readable. /// Image contains invalid content. @@ -177,296 +174,130 @@ public abstract partial class Image } /// - /// Decode a new instance of the class from the given stream. - /// The pixel format is selected by the decoder. - /// - /// The stream containing image information. - /// The format type of the decoded image. - /// The stream is null. - /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// The . - public static Image Load(Stream stream, out IImageFormat format) - => Load(DecoderOptions.Default, stream, out format); - - /// - /// Decode a new instance of the class from the given stream. - /// The pixel format is selected by the decoder. + /// Creates a new instance of the class from the given stream. + /// The pixel format is automatically determined by the decoder. /// /// The stream containing image information. - /// The token to monitor for cancellation requests. + /// . /// The stream is null. /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// A representing the asynchronous operation. - public static Task<(Image Image, IImageFormat Format)> LoadWithFormatAsync(Stream stream, CancellationToken cancellationToken = default) - => LoadWithFormatAsync(DecoderOptions.Default, stream, cancellationToken); + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Image Load(Stream stream) + => Load(DecoderOptions.Default, stream); /// - /// Decode a new instance of the class from the given stream. - /// The pixel format is selected by the decoder. + /// Creates a new instance of the class from the given stream. + /// The pixel format is automatically determined by the decoder. /// + /// The general decoder options. /// The stream containing image information. + /// . + /// The options are null. /// The stream is null. /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// The . - public static Image Load(Stream stream) => Load(DecoderOptions.Default, stream); + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Image Load(DecoderOptions options, Stream stream) + => WithSeekableStream(options, stream, s => Decode(options, s)); /// - /// Decode a new instance of the class from the given stream. - /// The pixel format is selected by the decoder. + /// Creates a new instance of the class from the given stream. + /// The pixel format is automatically determined by the decoder. /// /// The stream containing image information. /// The token to monitor for cancellation requests. + /// A representing the asynchronous operation. /// The stream is null. /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// A representing the asynchronous operation. + /// The encoded image contains invalid content. + /// The encoded image format is unknown. public static Task LoadAsync(Stream stream, CancellationToken cancellationToken = default) => LoadAsync(DecoderOptions.Default, stream, cancellationToken); /// - /// Decode a new instance of the class from the given stream. - /// - /// The general decoder options. - /// The stream containing image information. - /// The options are null. - /// The stream is null. - /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// A new . - public static Image Load(DecoderOptions options, Stream stream) - => Load(options, stream, out _); - - /// - /// Decode a new instance of the class from the given stream. + /// Creates a new instance of the class from the given stream. + /// The pixel format is automatically determined by the decoder. /// /// The general decoder options. /// The stream containing image information. /// 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 or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// A representing the asynchronous operation. - public static async Task LoadAsync(DecoderOptions options, Stream stream, CancellationToken cancellationToken = default) - => (await LoadWithFormatAsync(options, stream, cancellationToken).ConfigureAwait(false)).Image; + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Task LoadAsync( + DecoderOptions options, + Stream stream, + CancellationToken cancellationToken = default) + => WithSeekableStreamAsync(options, stream, (s, ct) => DecodeAsync(options, s, ct), cancellationToken); /// - /// Create a new instance of the class from the given stream. + /// Creates a new instance of the class from the given stream. /// + /// The pixel format. /// The stream containing image information. + /// . /// The stream is null. /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// The pixel format. - /// A new . + /// The encoded image contains invalid content. + /// The encoded image format is unknown. public static Image Load(Stream stream) where TPixel : unmanaged, IPixel => Load(DecoderOptions.Default, stream); /// - /// Create a new instance of the class from the given stream. - /// - /// The stream containing image information. - /// The token to monitor for cancellation requests. - /// The stream is null. - /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// The pixel format. - /// A representing the asynchronous operation. - public static Task> LoadAsync(Stream stream, CancellationToken cancellationToken = default) - where TPixel : unmanaged, IPixel - => LoadAsync(DecoderOptions.Default, stream, cancellationToken); - - /// - /// Create a new instance of the class from the given stream. + /// Creates a new instance of the class from the given stream. /// - /// The stream containing image information. - /// The format type of the decoded image. - /// The stream is null. - /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. /// The pixel format. - /// A new . - public static Image Load(Stream stream, out IImageFormat format) - where TPixel : unmanaged, IPixel - => Load(DecoderOptions.Default, stream, out format); - - /// - /// Create a new instance of the class from the given stream. - /// - /// The stream containing image information. - /// The token to monitor for cancellation requests. - /// The stream is null. - /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// The pixel format. - /// A representing the asynchronous operation. - public static Task<(Image Image, IImageFormat Format)> LoadWithFormatAsync(Stream stream, CancellationToken cancellationToken = default) - where TPixel : unmanaged, IPixel - => LoadWithFormatAsync(DecoderOptions.Default, stream, cancellationToken); - - /// - /// Create a new instance of the class from the given stream. - /// /// The general decoder options. /// The stream containing image information. + /// . /// The options are null. /// The stream is null. /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// The pixel format. - /// A new . + /// The encoded image contains invalid content. + /// The encoded image format is unknown. public static Image Load(DecoderOptions options, Stream stream) where TPixel : unmanaged, IPixel - => Load(options, stream, out IImageFormat _); + => WithSeekableStream(options, stream, s => Decode(options, s)); /// - /// Create a new instance of the class from the given stream. + /// Creates a new instance of the class from the given stream. /// - /// The general decoder options. - /// The stream containing image information. - /// The format type of the decoded image. - /// The options are null. - /// The stream is null. - /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. /// The pixel format. - /// A representing the asynchronous operation. - public static Image Load(DecoderOptions options, Stream stream, out IImageFormat format) - where TPixel : unmanaged, IPixel - { - Image? image = WithSeekableStream(options, stream, s => Decode(options, s)); - - if (image is null) - { - ThrowNotLoaded(options); - } - - format = image.Metadata.DecodedImageFormat!; - return image; - } - - /// - /// Create a new instance of the class from the given stream. - /// - /// The general decoder options. /// The stream containing image information. /// The token to monitor for cancellation requests. - /// The options are null. - /// The stream is null. - /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// A representing the asynchronous operation. - public static async Task<(Image Image, IImageFormat Format)> LoadWithFormatAsync( - DecoderOptions options, - Stream stream, - CancellationToken cancellationToken = default) - { - Image? image = await WithSeekableStreamAsync(options, stream, (s, ct) => DecodeAsync(options, s, ct), cancellationToken) - .ConfigureAwait(false); - - if (image is null) - { - ThrowNotLoaded(options); - } - - return new(image, image.Metadata.DecodedImageFormat!); - } - - /// - /// Create a new instance of the class from the given stream. - /// - /// The general decoder options. - /// The stream containing image information. - /// The token to monitor for cancellation requests. - /// The options are null. + /// A representing the asynchronous operation. /// The stream is null. /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// The pixel format. - /// A representing the asynchronous operation. - public static async Task<(Image Image, IImageFormat Format)> LoadWithFormatAsync( - DecoderOptions options, - Stream stream, - CancellationToken cancellationToken = default) + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Task> LoadAsync(Stream stream, CancellationToken cancellationToken = default) where TPixel : unmanaged, IPixel - { - Image? image = await WithSeekableStreamAsync(options, stream, (s, ct) => DecodeAsync(options, s, ct), cancellationToken) - .ConfigureAwait(false); - - if (image is null) - { - ThrowNotLoaded(options); - } - - return new(image, image.Metadata.DecodedImageFormat!); - } + => LoadAsync(DecoderOptions.Default, stream, cancellationToken); /// - /// Create a new instance of the class from the given stream. + /// Creates a new instance of the class from the given stream. /// + /// The pixel format. /// The general decoder options. /// The stream containing image information. /// 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 or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// The pixel format. - /// A representing the asynchronous operation. - public static async Task> LoadAsync( + /// The encoded image contains invalid content. + /// The encoded image format is unknown. + public static Task> LoadAsync( DecoderOptions options, Stream stream, CancellationToken cancellationToken = default) where TPixel : unmanaged, IPixel - { - (Image img, _) = await LoadWithFormatAsync(options, stream, cancellationToken) - .ConfigureAwait(false); - return img; - } - - /// - /// Decode a new instance of the class from the given stream. - /// The pixel format is selected by the decoder. - /// - /// The general decoder options. - /// The stream containing image information. - /// The format type of the decoded image. - /// The options are null. - /// The stream is null. - /// The stream is not readable or the image format is not supported. - /// Image format not recognised. - /// Image contains invalid content. - /// A new . - public static Image Load(DecoderOptions options, Stream stream, out IImageFormat format) - { - Image? image = WithSeekableStream(options, stream, s => Decode(options, s)); - if (image is null) - { - ThrowNotLoaded(options); - } - - format = image.Metadata.DecodedImageFormat!; - return image; - } + => WithSeekableStreamAsync(options, stream, (s, ct) => DecodeAsync(options, s, ct), cancellationToken); /// /// Performs the given action against the stream ensuring that it is seekable. @@ -549,18 +380,4 @@ public abstract partial class Image return await action(memoryStream, cancellationToken).ConfigureAwait(false); } - - [DoesNotReturn] - private static void ThrowNotLoaded(DecoderOptions options) - { - StringBuilder sb = new(); - sb.AppendLine("Image cannot be loaded. Available decoders:"); - - foreach (KeyValuePair val in options.Configuration.ImageFormatsManager.ImageDecoders) - { - sb.AppendFormat(CultureInfo.InvariantCulture, " - {0} : {1}{2}", val.Key.Name, val.Value.GetType().Name, Environment.NewLine); - } - - throw new UnknownImageFormatException(sb.ToString()); - } } diff --git a/src/ImageSharp/Image.LoadPixelData.cs b/src/ImageSharp/Image.LoadPixelData.cs index 9a37adb3c0..53b672b7dd 100644 --- a/src/ImageSharp/Image.LoadPixelData.cs +++ b/src/ImageSharp/Image.LoadPixelData.cs @@ -72,7 +72,7 @@ public abstract partial class Image int count = width * height; Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data)); - var image = new Image(configuration, width, height); + Image image = new(configuration, width, height); data = data[..count]; data.CopyTo(image.Frames.RootFrame.PixelBuffer.FastMemoryGroup); diff --git a/src/ImageSharp/ImageExtensions.cs b/src/ImageSharp/ImageExtensions.cs index 7cd8a7b2f8..04930a2689 100644 --- a/src/ImageSharp/ImageExtensions.cs +++ b/src/ImageSharp/ImageExtensions.cs @@ -95,7 +95,7 @@ public static partial class ImageExtensions throw new NotSupportedException("Cannot write to the stream."); } - IImageEncoder encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format); + IImageEncoder encoder = source.GetConfiguration().ImageFormatsManager.GetEncoder(format); if (encoder is null) { @@ -139,7 +139,7 @@ public static partial class ImageExtensions throw new NotSupportedException("Cannot write to the stream."); } - IImageEncoder encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format); + IImageEncoder encoder = source.GetConfiguration().ImageFormatsManager.GetEncoder(format); if (encoder is null) { diff --git a/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs index fbf9b20a9c..1f5686cd76 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs @@ -15,15 +15,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsBmp_Path.bmp"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsBmp(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/bmp", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is BmpFormat); } [Fact] @@ -32,15 +30,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsBmpAsync_Path.bmp"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsBmpAsync(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/bmp", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is BmpFormat); } [Fact] @@ -49,15 +45,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsBmp_Path_Encoder.bmp"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsBmp(file, new BmpEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/bmp", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is BmpFormat); } [Fact] @@ -66,86 +60,76 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsBmpAsync_Path_Encoder.bmp"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsBmpAsync(file, new BmpEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/bmp", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is BmpFormat); } [Fact] public void SaveAsBmp_Stream() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsBmp(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/bmp", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is BmpFormat); } [Fact] public async Task SaveAsBmpAsync_StreamAsync() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsBmpAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/bmp", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is BmpFormat); } [Fact] public void SaveAsBmp_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsBmp(memoryStream, new BmpEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/bmp", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is BmpFormat); } [Fact] public async Task SaveAsBmpAsync_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsBmpAsync(memoryStream, new BmpEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/bmp", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is BmpFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 400af5e450..303703f74c 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -207,10 +207,10 @@ public class GeneralFormatTests foreach (TestFile file in Files) { byte[] serialized; - using (Image image = Image.Load(file.Bytes, out IImageFormat mimeType)) + using (Image image = Image.Load(file.Bytes)) using (MemoryStream memoryStream = new()) { - image.Save(memoryStream, mimeType); + image.Save(memoryStream, image.Metadata.DecodedImageFormat); memoryStream.Flush(); serialized = memoryStream.ToArray(); } @@ -264,14 +264,13 @@ public class GeneralFormatTests } [Fact] - public void IdentifyReturnsNullWithInvalidStream() + public void Identify_UnknownImageFormatException_WithInvalidStream() { byte[] invalid = new byte[10]; using MemoryStream memoryStream = new(invalid); - Image.TryIdentify(memoryStream, out ImageInfo imageInfo); - Assert.Null(imageInfo); + Assert.Throws(() => Image.TryIdentify(invalid, out ImageInfo imageInfo)); } private static IImageFormat GetFormat(string format) diff --git a/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs index 8aac817b15..3ea0c22406 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs @@ -15,15 +15,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsGif_Path.gif"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsGif(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/gif", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is GifFormat); } [Fact] @@ -32,15 +30,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsGifAsync_Path.gif"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsGifAsync(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/gif", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is GifFormat); } [Fact] @@ -49,15 +45,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsGif_Path_Encoder.gif"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsGif(file, new GifEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/gif", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is GifFormat); } [Fact] @@ -66,86 +60,76 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsGifAsync_Path_Encoder.gif"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsGifAsync(file, new GifEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/gif", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is GifFormat); } [Fact] public void SaveAsGif_Stream() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsGif(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/gif", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is GifFormat); } [Fact] public async Task SaveAsGifAsync_StreamAsync() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsGifAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/gif", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is GifFormat); } [Fact] public void SaveAsGif_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsGif(memoryStream, new GifEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/gif", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is GifFormat); } [Fact] public async Task SaveAsGifAsync_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsGifAsync(memoryStream, new GifEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/gif", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is GifFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs index bfaa2166a3..8d65eecb9f 100644 --- a/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs +++ b/tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs @@ -74,12 +74,12 @@ public class ImageFormatManagerTests { IImageEncoder encoder1 = new Mock().Object; this.FormatsManagerEmpty.SetEncoder(TestFormat.GlobalTestFormat, encoder1); - IImageEncoder found = this.FormatsManagerEmpty.FindEncoder(TestFormat.GlobalTestFormat); + IImageEncoder found = this.FormatsManagerEmpty.GetEncoder(TestFormat.GlobalTestFormat); Assert.Equal(encoder1, found); IImageEncoder encoder2 = new Mock().Object; this.FormatsManagerEmpty.SetEncoder(TestFormat.GlobalTestFormat, encoder2); - IImageEncoder found2 = this.FormatsManagerEmpty.FindEncoder(TestFormat.GlobalTestFormat); + IImageEncoder found2 = this.FormatsManagerEmpty.GetEncoder(TestFormat.GlobalTestFormat); Assert.Equal(encoder2, found2); Assert.NotEqual(found, found2); } @@ -89,12 +89,12 @@ public class ImageFormatManagerTests { IImageDecoder decoder1 = new Mock().Object; this.FormatsManagerEmpty.SetDecoder(TestFormat.GlobalTestFormat, decoder1); - IImageDecoder found = this.FormatsManagerEmpty.FindDecoder(TestFormat.GlobalTestFormat); + IImageDecoder found = this.FormatsManagerEmpty.GetDecoder(TestFormat.GlobalTestFormat); Assert.Equal(decoder1, found); IImageDecoder decoder2 = new Mock().Object; this.FormatsManagerEmpty.SetDecoder(TestFormat.GlobalTestFormat, decoder2); - IImageDecoder found2 = this.FormatsManagerEmpty.FindDecoder(TestFormat.GlobalTestFormat); + IImageDecoder found2 = this.FormatsManagerEmpty.GetDecoder(TestFormat.GlobalTestFormat); Assert.Equal(decoder2, found2); Assert.NotEqual(found, found2); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs index 0e799612da..12392568fe 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs @@ -16,15 +16,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsJpeg_Path.jpg"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsJpeg(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/jpeg", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is JpegFormat); } [Fact] @@ -33,15 +31,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsJpegAsync_Path.jpg"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsJpegAsync(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/jpeg", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is JpegFormat); } [Fact] @@ -50,15 +46,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsJpeg_Path_Encoder.jpg"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsJpeg(file, new JpegEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/jpeg", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is JpegFormat); } [Fact] @@ -67,86 +61,76 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsJpegAsync_Path_Encoder.jpg"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsJpegAsync(file, new JpegEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/jpeg", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is JpegFormat); } [Fact] public void SaveAsJpeg_Stream() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsJpeg(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/jpeg", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is JpegFormat); } [Fact] public async Task SaveAsJpegAsync_StreamAsync() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsJpegAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/jpeg", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is JpegFormat); } [Fact] public void SaveAsJpeg_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsJpeg(memoryStream, new JpegEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/jpeg", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is JpegFormat); } [Fact] public async Task SaveAsJpegAsync_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsJpegAsync(memoryStream, new JpegEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/jpeg", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is JpegFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs index 80417653d8..5fecb2f9fd 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.Metadata.cs @@ -75,8 +75,8 @@ public partial class JpegDecoderTests [MemberData(nameof(RatioFiles))] public void Decode_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) { - var testFile = TestFile.Create(imagePath); - using var stream = new MemoryStream(testFile.Bytes, false); + TestFile testFile = TestFile.Create(imagePath); + using MemoryStream stream = new(testFile.Bytes, false); using Image image = JpegDecoder.Instance.Decode(DecoderOptions.Default, stream); ImageMetadata meta = image.Metadata; Assert.Equal(xResolution, meta.HorizontalResolution); @@ -88,8 +88,8 @@ public partial class JpegDecoderTests [MemberData(nameof(RatioFiles))] public void Identify_VerifyRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) { - var testFile = TestFile.Create(imagePath); - using var stream = new MemoryStream(testFile.Bytes, false); + TestFile testFile = TestFile.Create(imagePath); + using MemoryStream stream = new(testFile.Bytes, false); ImageInfo image = JpegDecoder.Instance.Identify(DecoderOptions.Default, stream); ImageMetadata meta = image.Metadata; Assert.Equal(xResolution, meta.HorizontalResolution); @@ -101,8 +101,8 @@ public partial class JpegDecoderTests [MemberData(nameof(RatioFiles))] public async Task Identify_VerifyRatioAsync(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit) { - var testFile = TestFile.Create(imagePath); - using var stream = new MemoryStream(testFile.Bytes, false); + TestFile testFile = TestFile.Create(imagePath); + using MemoryStream stream = new(testFile.Bytes, false); ImageInfo image = await JpegDecoder.Instance.IdentifyAsync(DecoderOptions.Default, stream); ImageMetadata meta = image.Metadata; Assert.Equal(xResolution, meta.HorizontalResolution); @@ -114,8 +114,8 @@ public partial class JpegDecoderTests [MemberData(nameof(QualityFiles))] public void Identify_VerifyQuality(string imagePath, int quality) { - var testFile = TestFile.Create(imagePath); - using var stream = new MemoryStream(testFile.Bytes, false); + TestFile testFile = TestFile.Create(imagePath); + using MemoryStream stream = new(testFile.Bytes, false); ImageInfo image = JpegDecoder.Instance.Identify(DecoderOptions.Default, stream); JpegMetadata meta = image.Metadata.GetJpegMetadata(); Assert.Equal(quality, meta.Quality); @@ -125,8 +125,8 @@ public partial class JpegDecoderTests [MemberData(nameof(QualityFiles))] public void Decode_VerifyQuality(string imagePath, int quality) { - var testFile = TestFile.Create(imagePath); - using var stream = new MemoryStream(testFile.Bytes, false); + TestFile testFile = TestFile.Create(imagePath); + using MemoryStream stream = new(testFile.Bytes, false); using Image image = JpegDecoder.Instance.Decode(DecoderOptions.Default, stream); JpegMetadata meta = image.Metadata.GetJpegMetadata(); Assert.Equal(quality, meta.Quality); @@ -136,8 +136,8 @@ public partial class JpegDecoderTests [MemberData(nameof(QualityFiles))] public async Task Decode_VerifyQualityAsync(string imagePath, int quality) { - var testFile = TestFile.Create(imagePath); - using var stream = new MemoryStream(testFile.Bytes, false); + TestFile testFile = TestFile.Create(imagePath); + using MemoryStream stream = new(testFile.Bytes, false); using Image image = await JpegDecoder.Instance.DecodeAsync(DecoderOptions.Default, stream); JpegMetadata meta = image.Metadata.GetJpegMetadata(); Assert.Equal(quality, meta.Quality); @@ -153,8 +153,8 @@ public partial class JpegDecoderTests [InlineData(TestImages.Jpeg.Baseline.Jpeg411, JpegEncodingColor.YCbCrRatio411)] public void Identify_DetectsCorrectColorType(string imagePath, JpegEncodingColor expectedColorType) { - var testFile = TestFile.Create(imagePath); - using var stream = new MemoryStream(testFile.Bytes, false); + TestFile testFile = TestFile.Create(imagePath); + using MemoryStream stream = new(testFile.Bytes, false); ImageInfo image = JpegDecoder.Instance.Identify(DecoderOptions.Default, stream); JpegMetadata meta = image.Metadata.GetJpegMetadata(); Assert.Equal(expectedColorType, meta.ColorType); @@ -176,8 +176,8 @@ public partial class JpegDecoderTests private static void TestImageInfo(string imagePath, IImageDecoder decoder, bool useIdentify, Action test) { - var testFile = TestFile.Create(imagePath); - using var stream = new MemoryStream(testFile.Bytes, false); + TestFile testFile = TestFile.Create(imagePath); + using MemoryStream stream = new(testFile.Bytes, false); if (useIdentify) { ImageInfo imageInfo = decoder.Identify(DecoderOptions.Default, stream); @@ -318,10 +318,10 @@ public partial class JpegDecoderTests [Fact] public void EncodedStringTags_WriteAndRead() { - using var memoryStream = new MemoryStream(); - using (var image = Image.Load(TestFile.GetInputFileFullPath(TestImages.Jpeg.Baseline.Calliphora))) + using MemoryStream memoryStream = new(); + using (Image image = Image.Load(TestFile.GetInputFileFullPath(TestImages.Jpeg.Baseline.Calliphora))) { - var exif = new ExifProfile(); + ExifProfile exif = new(); exif.SetValue(ExifTag.GPSDateStamp, "2022-01-06"); @@ -343,7 +343,7 @@ public partial class JpegDecoderTests } memoryStream.Seek(0, SeekOrigin.Begin); - using (var image = Image.Load(memoryStream)) + using (Image image = Image.Load(memoryStream)) { ExifProfile exif = image.Metadata.ExifProfile; VerifyEncodedStrings(exif); @@ -353,7 +353,7 @@ public partial class JpegDecoderTests [Fact] public void EncodedStringTags_Read() { - using var image = Image.Load(TestFile.GetInputFileFullPath(TestImages.Jpeg.Baseline.Calliphora_EncodedStrings)); + using Image image = Image.Load(TestFile.GetInputFileFullPath(TestImages.Jpeg.Baseline.Calliphora_EncodedStrings)); ExifProfile exif = image.Metadata.ExifProfile; VerifyEncodedStrings(exif); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 18b4e1fba9..1d5a7e0ff8 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -35,7 +35,7 @@ public partial class JpegDecoderTests if (!CustomToleranceValues.TryGetValue(file, out float tolerance)) { - bool baseline = file.IndexOf("baseline", StringComparison.OrdinalIgnoreCase) >= 0; + bool baseline = file.Contains("baseline", StringComparison.OrdinalIgnoreCase); tolerance = baseline ? BaselineTolerance : ProgressiveTolerance; } @@ -68,9 +68,9 @@ public partial class JpegDecoderTests { JpegDecoderOptions options = new(); byte[] bytes = TestFile.Create(TestImages.Jpeg.Progressive.Progress).Bytes; - using var ms = new MemoryStream(bytes); - using var bufferedStream = new BufferedReadStream(Configuration.Default, ms); - using var decoder = new JpegDecoderCore(options); + using MemoryStream ms = new(bytes); + using BufferedReadStream bufferedStream = new(Configuration.Default, ms); + using JpegDecoderCore decoder = new(options); using Image image = decoder.Decode(bufferedStream, cancellationToken: default); // I don't know why these numbers are different. All I know is that the decoder works @@ -85,7 +85,7 @@ public partial class JpegDecoderTests public void Decode_NonGeneric_CreatesRgb24Image() { string file = Path.Combine(TestEnvironment.InputImagesDirectoryFullPath, TestImages.Jpeg.Baseline.Jpeg420Small); - using var image = Image.Load(file); + using Image image = Image.Load(file); Assert.IsType>(image); } diff --git a/tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs index fba734f0e7..1643e3ddac 100644 --- a/tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs @@ -15,15 +15,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsPbm_Path.pbm"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsPbm(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/x-portable-pixmap", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PbmFormat); } [Fact] @@ -32,15 +30,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsPbmAsync_Path.pbm"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsPbmAsync(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/x-portable-pixmap", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PbmFormat); } [Fact] @@ -49,15 +45,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsPbm_Path_Encoder.pbm"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsPbm(file, new PbmEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/x-portable-pixmap", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PbmFormat); } [Fact] @@ -66,86 +60,76 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsPbmAsync_Path_Encoder.pbm"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsPbmAsync(file, new PbmEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/x-portable-pixmap", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PbmFormat); } [Fact] public void SaveAsPbm_Stream() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsPbm(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/x-portable-pixmap", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is PbmFormat); } [Fact] public async Task SaveAsPbmAsync_StreamAsync() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsPbmAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/x-portable-pixmap", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is PbmFormat); } [Fact] public void SaveAsPbm_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsPbm(memoryStream, new PbmEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/x-portable-pixmap", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is PbmFormat); } [Fact] public async Task SaveAsPbmAsync_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsPbmAsync(memoryStream, new PbmEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/x-portable-pixmap", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is PbmFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs index 321250fee9..313b779e02 100644 --- a/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs @@ -16,15 +16,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsPng_Path.png"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsPng(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] @@ -33,15 +31,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsPngAsync_Path.png"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsPngAsync(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] @@ -50,15 +46,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsPng_Path_Encoder.png"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsPng(file, new PngEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] @@ -67,86 +61,76 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsPngAsync_Path_Encoder.png"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsPngAsync(file, new PngEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] public void SaveAsPng_Stream() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsPng(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] public async Task SaveAsPngAsync_StreamAsync() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsPngAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] public void SaveAsPng_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsPng(memoryStream, new PngEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] public async Task SaveAsPngAsync_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsPngAsync(memoryStream, new PngEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is PngFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs index 8c0cfd5d19..fe9612543e 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs @@ -15,15 +15,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsTga_Path.tga"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsTga(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/tga", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is TgaFormat); } [Fact] @@ -32,15 +30,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsTgaAsync_Path.tga"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsTgaAsync(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/tga", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is TgaFormat); } [Fact] @@ -49,15 +45,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsTga_Path_Encoder.tga"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsTga(file, new TgaEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/tga", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is TgaFormat); } [Fact] @@ -66,86 +60,76 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsTgaAsync_Path_Encoder.tga"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsTgaAsync(file, new TgaEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/tga", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is TgaFormat); } [Fact] public void SaveAsTga_Stream() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsTga(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/tga", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is TgaFormat); } [Fact] public async Task SaveAsTgaAsync_StreamAsync() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsTgaAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/tga", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is TgaFormat); } [Fact] public void SaveAsTga_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsTga(memoryStream, new TgaEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/tga", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is TgaFormat); } [Fact] public async Task SaveAsTgaAsync_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsTgaAsync(memoryStream, new TgaEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/tga", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is TgaFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs b/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs index 5910a86ac1..f1efe47f9a 100644 --- a/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Tga/TgaFileHeaderTests.cs @@ -26,7 +26,7 @@ public class TgaFileHeaderTests Assert.Throws(() => { - using (Image.Load(DecoderOptions.Default, stream, out IImageFormat _)) + using (Image.Load(DecoderOptions.Default, stream)) { } }); diff --git a/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs b/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs index 23806a18db..4ea4b632e5 100644 --- a/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs +++ b/tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs @@ -16,15 +16,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsTiff_Path.tiff"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsTiff(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/tiff", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is TiffFormat); } [Fact] @@ -33,15 +31,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTest)); string file = Path.Combine(dir, "SaveAsTiffAsync_Path.tiff"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsTiffAsync(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/tiff", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is TiffFormat); } [Fact] @@ -50,15 +46,13 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsTiff_Path_Encoder.tiff"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsTiff(file, new TiffEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/tiff", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is TiffFormat); } [Fact] @@ -67,86 +61,76 @@ public class ImageExtensionsTest string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsTiffAsync_Path_Encoder.tiff"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsTiffAsync(file, new TiffEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/tiff", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is TiffFormat); } [Fact] public void SaveAsTiff_Stream() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsTiff(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/tiff", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is TiffFormat); } [Fact] public async Task SaveAsTiffAsync_StreamAsync() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsTiffAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/tiff", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is TiffFormat); } [Fact] public void SaveAsTiff_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsTiff(memoryStream, new TiffEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/tiff", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is TiffFormat); } [Fact] public async Task SaveAsTiffAsync_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsTiffAsync(memoryStream, new TiffEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/tiff", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is TiffFormat); } } diff --git a/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs b/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs index 737b023f1f..5ed6c8dc41 100644 --- a/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs +++ b/tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs @@ -16,15 +16,13 @@ public class ImageExtensionsTests string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTests)); string file = Path.Combine(dir, "SaveAsWebp_Path.webp"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsWebp(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/webp", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is WebpFormat); } [Fact] @@ -33,15 +31,13 @@ public class ImageExtensionsTests string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensionsTests)); string file = Path.Combine(dir, "SaveAsWebpAsync_Path.webp"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsWebpAsync(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/webp", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is WebpFormat); } [Fact] @@ -50,15 +46,13 @@ public class ImageExtensionsTests string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsWebp_Path_Encoder.webp"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsWebp(file, new WebpEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/webp", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is WebpFormat); } [Fact] @@ -67,86 +61,76 @@ public class ImageExtensionsTests string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageExtensions)); string file = Path.Combine(dir, "SaveAsWebpAsync_Path_Encoder.webp"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsWebpAsync(file, new WebpEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/webp", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is WebpFormat); } [Fact] public void SaveAsWebp_Stream() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsWebp(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/webp", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is WebpFormat); } [Fact] public async Task SaveAsWebpAsync_StreamAsync() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsWebpAsync(memoryStream); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/webp", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is WebpFormat); } [Fact] public void SaveAsWebp_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.SaveAsWebp(memoryStream, new WebpEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/webp", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is WebpFormat); } [Fact] public async Task SaveAsWebpAsync_Stream_Encoder() { - using var memoryStream = new MemoryStream(); + using MemoryStream memoryStream = new(); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsWebpAsync(memoryStream, new WebpEncoder()); } memoryStream.Position = 0; - using (Image.Load(memoryStream, out IImageFormat mime)) - { - Assert.Equal("image/webp", mime.DefaultMimeType); - } + Image.TryDetectFormat(memoryStream, out IImageFormat format); + Assert.True(format is WebpFormat); } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs index 5a5f595ac7..846538e42f 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs @@ -125,17 +125,14 @@ public partial class ImageTests } [Fact] - public void WhenNoMatchingFormatFound_ReturnsNull() + public void WhenNoMatchingFormatFound_Throws_UnknownImageFormatException() { DecoderOptions options = new() { Configuration = new() }; - bool result = Image.TryDetectFormat(options, this.DataStream, out IImageFormat type); - - Assert.False(result); - Assert.Null(type); + Assert.Throws(() => Image.TryDetectFormat(options, this.DataStream, out IImageFormat type)); } [Fact] @@ -159,15 +156,14 @@ public partial class ImageTests } [Fact] - public async Task WhenNoMatchingFormatFoundAsync_ReturnsNull() + public Task WhenNoMatchingFormatFoundAsync_Throws_UnknownImageFormatException() { DecoderOptions options = new() { Configuration = new() }; - Attempt attempt = await Image.TryDetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false)); - Assert.Null(attempt.Value); + return Assert.ThrowsAsync(async () => await Image.TryDetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false))); } } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs b/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs index e9b61232ac..9d0aa7a752 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Identify.cs @@ -21,8 +21,6 @@ public partial class ImageTests private static byte[] ActualImageBytes => TestFile.Create(TestImages.Bmp.F).Bytes; - private IImageFormat LocalImageFormat => this.localImageFormatMock.Object; - private static IImageFormat ExpectedGlobalFormat { get @@ -131,16 +129,15 @@ public partial class ImageTests } [Fact] - public void WhenNoMatchingFormatFound_ReturnsNull() + public void WhenNoMatchingFormatFound_Throws_UnknownImageFormatException() { DecoderOptions options = new() { Configuration = new() }; - Assert.False(Image.TryIdentify(options, this.DataStream, out ImageInfo info)); - Assert.Null(info); + Assert.Throws(() => Image.TryIdentify(options, this.DataStream, out ImageInfo info)); } [Fact] - public void FromStream_ZeroLength_ReturnsNull() + public void FromStream_ZeroLength_Throws_UnknownImageFormatException() { // https://github.com/SixLabors/ImageSharp/issues/1903 using ZipArchive zipFile = new(new MemoryStream( @@ -159,8 +156,7 @@ public partial class ImageTests })); using Stream stream = zipFile.Entries[0].Open(); - Assert.False(Image.TryIdentify(stream, out ImageInfo info)); - Assert.Null(info); + Assert.Throws(() => Image.TryIdentify(stream, out ImageInfo info)); } [Fact] @@ -215,7 +211,7 @@ public partial class ImageTests } [Fact] - public async Task FromStreamAsync_ZeroLength_ReturnsNull() + public async Task FromStreamAsync_ZeroLength_Throws_UnknownImageFormatException() { // https://github.com/SixLabors/ImageSharp/issues/1903 using ZipArchive zipFile = new(new MemoryStream( @@ -234,10 +230,7 @@ public partial class ImageTests })); using Stream stream = zipFile.Entries[0].Open(); - Attempt attempt = await Image.TryIdentifyAsync(stream); - - Assert.False(attempt.Success); - Assert.Null(attempt.Value); + await Assert.ThrowsAsync(async () => await Image.TryIdentifyAsync(stream)); } [Fact] @@ -293,15 +286,12 @@ public partial class ImageTests } [Fact] - public async Task WhenNoMatchingFormatFoundAsync_ReturnsNull() + public Task WhenNoMatchingFormatFoundAsync_Throws_UnknownImageFormatException() { DecoderOptions options = new() { Configuration = new() }; AsyncStreamWrapper asyncStream = new(this.DataStream, () => false); - Attempt attempt = await Image.TryIdentifyAsync(options, asyncStream); - - Assert.False(attempt.Success); - Assert.Null(attempt.Value); + return Assert.ThrowsAsync(async () => await Image.TryIdentifyAsync(options, asyncStream)); } } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_PassLocalConfiguration.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_PassLocalConfiguration.cs index c8723e1f10..955693ff1e 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_PassLocalConfiguration.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_PassLocalConfiguration.cs @@ -18,10 +18,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.MockFilePath); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat.Sample(), img); + using (Image img = Image.Load(options, this.MockFilePath)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat.Sample(), img); + } this.TestFormat.VerifySpecificDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -34,10 +35,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.MockFilePath); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat.SampleAgnostic(), img); + using (Image img = Image.Load(options, this.MockFilePath)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat.SampleAgnostic(), img); + } this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -50,10 +52,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.MockFilePath, out IImageFormat format); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat, format); + using (Image img = Image.Load(options, this.MockFilePath)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat, img.Metadata.DecodedImageFormat); + } this.TestFormat.VerifySpecificDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -66,17 +69,18 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.MockFilePath, out IImageFormat format); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat, format); + using (Image img = Image.Load(options, this.MockFilePath)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat, img.Metadata.DecodedImageFormat); + } this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration); } [Fact] public void WhenFileNotFound_Throws() - => Assert.Throws( + => Assert.Throws( () => { DecoderOptions options = new() diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs index 67fec5e497..b355c42a6c 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FileSystemPath_UseDefaultConfiguration.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.PixelFormats; @@ -18,14 +17,14 @@ public partial class ImageTests [Fact] public void Path_Specific() { - using var img = Image.Load(this.Path); + using Image img = Image.Load(this.Path); VerifyDecodedImage(img); } [Fact] public void Path_Agnostic() { - using var img = Image.Load(this.Path); + using Image img = Image.Load(this.Path); VerifyDecodedImage(img); } @@ -53,17 +52,17 @@ public partial class ImageTests [Fact] public void Path_OutFormat_Specific() { - using var img = Image.Load(this.Path, out IImageFormat format); + using Image img = Image.Load(this.Path); VerifyDecodedImage(img); - Assert.IsType(format); + Assert.IsType(img.Metadata.DecodedImageFormat); } [Fact] public void Path_OutFormat_Agnostic() { - using var img = Image.Load(this.Path, out IImageFormat format); + using Image img = Image.Load(this.Path); VerifyDecodedImage(img); - Assert.IsType(format); + Assert.IsType(img.Metadata.DecodedImageFormat); } [Fact] diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_PassLocalConfiguration.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_PassLocalConfiguration.cs index 7946bdbed9..3a47a0ea73 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_PassLocalConfiguration.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_PassLocalConfiguration.cs @@ -20,10 +20,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.ByteSpan); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat.Sample(), img); + using (Image img = Image.Load(options, this.ByteSpan)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat.Sample(), img); + } this.TestFormat.VerifySpecificDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -36,10 +37,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.ByteSpan); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat.SampleAgnostic(), img); + using (Image img = Image.Load(options, this.ByteSpan)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat.SampleAgnostic(), img); + } this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -52,10 +54,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.ByteSpan, out IImageFormat format); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat, format); + using (Image img = Image.Load(options, this.ByteSpan)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat, img.Metadata.DecodedImageFormat); + } this.TestFormat.VerifySpecificDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -68,10 +71,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.ByteSpan, out IImageFormat format); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat, format); + using (Image img = Image.Load(options, this.ByteSpan)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat, img.Metadata.DecodedImageFormat); + } this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration); } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs index 6ea469e65c..a3d8605964 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromBytes_UseGlobalConfiguration.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.PixelFormats; @@ -20,31 +19,31 @@ public partial class ImageTests [Fact] public void Bytes_Specific() { - using var img = Image.Load(ByteSpan); + using Image img = Image.Load(ByteSpan); VerifyDecodedImage(img); } [Fact] public void Bytes_Agnostic() { - using var img = Image.Load(ByteSpan); + using Image img = Image.Load(ByteSpan); VerifyDecodedImage(img); } [Fact] public void Bytes_OutFormat_Specific() { - using var img = Image.Load(ByteSpan, out IImageFormat format); + using Image img = Image.Load(ByteSpan); VerifyDecodedImage(img); - Assert.IsType(format); + Assert.IsType(img.Metadata.DecodedImageFormat); } [Fact] public void Bytes_OutFormat_Agnostic() { - using var img = Image.Load(ByteSpan, out IImageFormat format); + using Image img = Image.Load(ByteSpan); VerifyDecodedImage(img); - Assert.IsType(format); + Assert.IsType(img.Metadata.DecodedImageFormat); } } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_PassLocalConfiguration.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_PassLocalConfiguration.cs index 46e6dfac59..9d11d777a9 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_PassLocalConfiguration.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_PassLocalConfiguration.cs @@ -18,10 +18,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.DataStream); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat.Sample(), img); + using (Image img = Image.Load(options, this.DataStream)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat.Sample(), img); + } this.TestFormat.VerifySpecificDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -34,10 +35,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.DataStream); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat.SampleAgnostic(), img); + using (Image img = Image.Load(options, this.DataStream)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat.SampleAgnostic(), img); + } this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -50,10 +52,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var stream = new NonSeekableStream(this.DataStream); - var img = Image.Load(options, stream); - - Assert.NotNull(img); + NonSeekableStream stream = new(this.DataStream); + using (Image img = Image.Load(options, stream)) + { + Assert.NotNull(img); + } this.TestFormat.VerifySpecificDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -66,10 +69,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var stream = new NonSeekableStream(this.DataStream); - Image img = await Image.LoadAsync(options, stream); - - Assert.NotNull(img); + NonSeekableStream stream = new(this.DataStream); + using (Image img = await Image.LoadAsync(options, stream)) + { + Assert.NotNull(img); + } this.TestFormat.VerifySpecificDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -82,10 +86,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.DataStream, out IImageFormat format); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat, format); + using (Image img = Image.Load(options, this.DataStream)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat, img.Metadata.DecodedImageFormat); + } this.TestFormat.VerifySpecificDecodeCall(this.Marker, this.TopLevelConfiguration); } @@ -98,10 +103,11 @@ public partial class ImageTests Configuration = this.TopLevelConfiguration }; - var img = Image.Load(options, this.DataStream, out IImageFormat format); - - Assert.NotNull(img); - Assert.Equal(this.TestFormat, format); + using (Image img = Image.Load(options, this.DataStream)) + { + Assert.NotNull(img); + Assert.Equal(this.TestFormat, img.Metadata.DecodedImageFormat); + } this.TestFormat.VerifyAgnosticDecodeCall(this.Marker, this.TopLevelConfiguration); } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs index a051176a77..9b9f968bb4 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_ThrowsRightException.cs @@ -18,7 +18,7 @@ public partial class ImageTests public void Image_Load_Throws_UnknownImageFormatException() => Assert.Throws(() => { - using (Image.Load(DecoderOptions.Default, this.Stream, out IImageFormat format)) + using (Image.Load(DecoderOptions.Default, this.Stream)) { } }); @@ -27,7 +27,7 @@ public partial class ImageTests public void Image_Load_T_Throws_UnknownImageFormatException() => Assert.Throws(() => { - using (Image.Load(DecoderOptions.Default, this.Stream, out IImageFormat format)) + using (Image.Load(DecoderOptions.Default, this.Stream)) { } }); diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_UseDefaultConfiguration.cs b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_UseDefaultConfiguration.cs index 839749c5d6..682ec2a41c 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_UseDefaultConfiguration.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Load_FromStream_UseDefaultConfiguration.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using SixLabors.ImageSharp.Formats; using SixLabors.ImageSharp.Formats.Bmp; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Tests.TestUtilities; @@ -32,42 +31,42 @@ public partial class ImageTests [Fact] public void Stream_Specific() { - using var img = Image.Load(this.Stream); + using Image img = Image.Load(this.Stream); VerifyDecodedImage(img); } [Fact] public void Stream_Agnostic() { - using var img = Image.Load(this.Stream); + using Image img = Image.Load(this.Stream); VerifyDecodedImage(img); } [Fact] public void Stream_OutFormat_Specific() { - using var img = Image.Load(this.Stream, out IImageFormat format); + using Image img = Image.Load(this.Stream); VerifyDecodedImage(img); - Assert.IsType(format); + Assert.IsType(img.Metadata.DecodedImageFormat); } [Fact] public void Stream_OutFormat_Agnostic() { - using var img = Image.Load(this.Stream, out IImageFormat format); + using Image img = Image.Load(this.Stream); VerifyDecodedImage(img); - Assert.IsType(format); + Assert.IsType(img.Metadata.DecodedImageFormat); } [Fact] public async Task Async_Stream_OutFormat_Agnostic() { this.AllowSynchronousIO = false; - (Image Image, IImageFormat Format) formattedImage = await Image.LoadWithFormatAsync(this.Stream); - using (formattedImage.Image) + Image image = await Image.LoadAsync(this.Stream); + using (image) { - VerifyDecodedImage(formattedImage.Image); - Assert.IsType(formattedImage.Format); + VerifyDecodedImage(image); + Assert.IsType(image.Metadata.DecodedImageFormat); } } @@ -91,11 +90,11 @@ public partial class ImageTests public async Task Async_Stream_OutFormat_Specific() { this.AllowSynchronousIO = false; - (Image Image, IImageFormat Format) formattedImage = await Image.LoadWithFormatAsync(this.Stream); - using (formattedImage.Image) + Image image = await Image.LoadAsync(this.Stream); + using (image) { - VerifyDecodedImage(formattedImage.Image); - Assert.IsType(formattedImage.Format); + VerifyDecodedImage(image); + Assert.IsType(image.Metadata.DecodedImageFormat); } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Save.cs b/tests/ImageSharp.Tests/Image/ImageTests.Save.cs index 88a6b5890b..8bdbb705ad 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Save.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Save.cs @@ -19,30 +19,26 @@ public partial class ImageTests string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageTests)); string file = Path.Combine(dir, "DetectedEncoding.png"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.Save(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] - public void WhenExtensionIsUnknown_Throws() + public void WhenExtensionIsUnknown_Throws_UnknownImageFormatException() { string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageTests)); string file = Path.Combine(dir, "UnknownExtensionsEncoding_Throws.tmp"); - Assert.Throws( + Assert.Throws( () => { - using (var image = new Image(10, 10)) - { - image.Save(file); - } + using Image image = new(10, 10); + image.Save(file); }); } @@ -52,27 +48,23 @@ public partial class ImageTests string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageTests)); string file = Path.Combine(dir, "SetEncoding.dat"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { image.Save(file, new PngEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] public void ThrowsWhenDisposed() { - using var image = new Image(5, 5); + using Image image = new(5, 5); image.Dispose(); IImageEncoder encoder = Mock.Of(); - using (var stream = new MemoryStream()) - { - Assert.Throws(() => image.Save(stream, encoder)); - } + using MemoryStream stream = new(); + Assert.Throws(() => image.Save(stream, encoder)); } } } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs b/tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs index 3627ba2a65..b58d0cff3c 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs @@ -21,30 +21,26 @@ public partial class ImageTests string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageTests)); string file = Path.Combine(dir, "DetectedEncodingAsync.png"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsync(file); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PngFormat); } [Fact] - public async Task WhenExtensionIsUnknown_Throws() + public Task WhenExtensionIsUnknown_Throws_UnknownImageFormatException() { string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageTests)); string file = Path.Combine(dir, "UnknownExtensionsEncoding_Throws.tmp"); - await Assert.ThrowsAsync( + return Assert.ThrowsAsync( async () => { - using (var image = new Image(10, 10)) - { - await image.SaveAsync(file); - } + using Image image = new(10, 10); + await image.SaveAsync(file); }); } @@ -54,15 +50,13 @@ public partial class ImageTests string dir = TestEnvironment.CreateOutputDirectory(nameof(ImageTests)); string file = Path.Combine(dir, "SetEncoding.dat"); - using (var image = new Image(10, 10)) + using (Image image = new(10, 10)) { await image.SaveAsync(file, new PngEncoder()); } - using (Image.Load(file, out IImageFormat mime)) - { - Assert.Equal("image/png", mime.DefaultMimeType); - } + Image.TryDetectFormat(file, out IImageFormat format); + Assert.True(format is PngFormat); } [Theory] @@ -74,39 +68,29 @@ public partial class ImageTests [InlineData("test.gif", "image/gif")] public async Task SaveStreamWithMime(string filename, string mimeType) { - using (var image = new Image(5, 5)) - { - string ext = Path.GetExtension(filename); - image.GetConfiguration().ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat format); - Assert.Equal(mimeType, format!.DefaultMimeType); - - using (var stream = new MemoryStream()) - { - var asyncStream = new AsyncStreamWrapper(stream, () => false); - await image.SaveAsync(asyncStream, format); + using Image image = new(5, 5); + string ext = Path.GetExtension(filename); + image.GetConfiguration().ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat format); + Assert.Equal(mimeType, format!.DefaultMimeType); - stream.Position = 0; + using MemoryStream stream = new(); + AsyncStreamWrapper asyncStream = new(stream, () => false); + await image.SaveAsync(asyncStream, format); - (Image Image, IImageFormat Format) imf = await Image.LoadWithFormatAsync(stream); + stream.Position = 0; - Assert.Equal(format, imf.Format); - Assert.Equal(mimeType, imf.Format.DefaultMimeType); - - imf.Image.Dispose(); - } - } + Image.TryDetectFormat(stream, out IImageFormat format2); + Assert.Equal(format, format2); } [Fact] public async Task ThrowsWhenDisposed() { - var image = new Image(5, 5); + Image image = new(5, 5); image.Dispose(); IImageEncoder encoder = Mock.Of(); - using (var stream = new MemoryStream()) - { - await Assert.ThrowsAsync(async () => await image.SaveAsync(stream, encoder)); - } + using MemoryStream stream = new(); + await Assert.ThrowsAsync(async () => await image.SaveAsync(stream, encoder)); } [Theory] @@ -118,27 +102,23 @@ public partial class ImageTests [InlineData("test.gif")] public async Task SaveAsync_NeverCallsSyncMethods(string filename) { - using (var image = new Image(5, 5)) - { - IImageEncoder encoder = image.DetectEncoder(filename); - using (var stream = new MemoryStream()) - { - var asyncStream = new AsyncStreamWrapper(stream, () => false); - await image.SaveAsync(asyncStream, encoder); - } - } + using Image image = new(5, 5); + IImageEncoder encoder = image.DetectEncoder(filename); + using MemoryStream stream = new(); + AsyncStreamWrapper asyncStream = new(stream, () => false); + await image.SaveAsync(asyncStream, encoder); } [Fact] public async Task SaveAsync_WithNonSeekableStream_IsCancellable() { - using var image = new Image(4000, 4000); - var encoder = new PngEncoder() { CompressionLevel = PngCompressionLevel.BestCompression }; - using var stream = new MemoryStream(); - var asyncStream = new AsyncStreamWrapper(stream, () => false); - var cts = new CancellationTokenSource(); + using Image image = new(4000, 4000); + PngEncoder encoder = new() { CompressionLevel = PngCompressionLevel.BestCompression }; + using MemoryStream stream = new(); + AsyncStreamWrapper asyncStream = new(stream, () => false); + CancellationTokenSource cts = new(); - var pausedStream = new PausedStream(asyncStream); + PausedStream pausedStream = new(asyncStream); pausedStream.OnWaiting(s => { cts.Cancel(); diff --git a/tests/ImageSharp.Tests/Image/ImageTests.cs b/tests/ImageSharp.Tests/Image/ImageTests.cs index 02ccfb713b..eefa81835e 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.cs @@ -23,7 +23,7 @@ public partial class ImageTests [Fact] public void Width_Height() { - using (var image = new Image(11, 23)) + using (Image image = new(11, 23)) { Assert.Equal(11, image.Width); Assert.Equal(23, image.Height); @@ -40,7 +40,7 @@ public partial class ImageTests { Configuration configuration = Configuration.Default.Clone(); - using (var image = new Image(configuration, 11, 23)) + using (Image image = new(configuration, 11, 23)) { Assert.Equal(11, image.Width); Assert.Equal(23, image.Height); @@ -58,7 +58,7 @@ public partial class ImageTests Configuration configuration = Configuration.Default.Clone(); Rgba32 color = Color.Aquamarine; - using (var image = new Image(configuration, 11, 23, color)) + using (Image image = new(configuration, 11, 23, color)) { Assert.Equal(11, image.Width); Assert.Equal(23, image.Height); @@ -77,9 +77,9 @@ public partial class ImageTests byte dirtyValue = 123; configuration.MemoryAllocator = new TestMemoryAllocator(dirtyValue); - var metadata = new ImageMetadata(); + ImageMetadata metadata = new(); - using (var image = Image.CreateUninitialized(configuration, 21, 22, metadata)) + using (Image image = Image.CreateUninitialized(configuration, 21, 22, metadata)) { Assert.Equal(21, image.Width); Assert.Equal(22, image.Height); @@ -108,7 +108,7 @@ public partial class ImageTests this.LimitBufferCapacity(100); } - using var image = new Image(this.configuration, 10, 10); + using Image image = new(this.configuration, 10, 10); Rgba32 val = image[3, 4]; Assert.Equal(default(Rgba32), val); image[3, 4] = Color.Red; @@ -116,7 +116,7 @@ public partial class ImageTests Assert.Equal(Color.Red.ToRgba32(), val); } - public static TheoryData OutOfRangeData = new TheoryData() + public static TheoryData OutOfRangeData = new() { { false, -1 }, { false, 10 }, @@ -133,7 +133,7 @@ public partial class ImageTests this.LimitBufferCapacity(100); } - using var image = new Image(this.configuration, 10, 10); + using Image image = new(this.configuration, 10, 10); ArgumentOutOfRangeException ex = Assert.Throws(() => _ = image[x, 3]); Assert.Equal("x", ex.ParamName); } @@ -147,7 +147,7 @@ public partial class ImageTests this.LimitBufferCapacity(100); } - using var image = new Image(this.configuration, 10, 10); + using Image image = new(this.configuration, 10, 10); ArgumentOutOfRangeException ex = Assert.Throws(() => image[x, 3] = default); Assert.Equal("x", ex.ParamName); } @@ -161,7 +161,7 @@ public partial class ImageTests this.LimitBufferCapacity(100); } - using var image = new Image(this.configuration, 10, 10); + using Image image = new(this.configuration, 10, 10); ArgumentOutOfRangeException ex = Assert.Throws(() => image[3, y] = default); Assert.Equal("y", ex.ParamName); } @@ -178,7 +178,7 @@ public partial class ImageTests this.LimitBufferCapacity(20); } - using var image = new Image(this.configuration, 10, 10); + using Image image = new(this.configuration, 10, 10); if (disco) { Assert.True(image.GetPixelMemoryGroup().Count > 1); @@ -204,7 +204,7 @@ public partial class ImageTests [InlineData(true)] public void CopyPixelDataTo_DestinationTooShort_Throws(bool byteSpan) { - using var image = new Image(this.configuration, 10, 10); + using Image image = new(this.configuration, 10, 10); Assert.ThrowsAny(() => { @@ -243,7 +243,7 @@ public partial class ImageTests [Fact] public void NullReference_Throws() { - using var img = new Image(1, 1); + using Image img = new(1, 1); Assert.Throws(() => img.ProcessPixelRows(null)); @@ -262,7 +262,7 @@ public partial class ImageTests public void MultipleDisposeCalls() { - var image = new Image(this.configuration, 10, 10); + Image image = new(this.configuration, 10, 10); image.Dispose(); image.Dispose(); } @@ -270,24 +270,24 @@ public partial class ImageTests [Fact] public void NonPrivateProperties_ObjectDisposedException() { - var image = new Image(this.configuration, 10, 10); - var genericImage = (Image)image; + Image image = new(this.configuration, 10, 10); + Image genericImage = (Image)image; image.Dispose(); // Image - Assert.Throws(() => { var prop = image.Frames; }); + Assert.Throws(() => { ImageFrameCollection prop = image.Frames; }); // Image - Assert.Throws(() => { var prop = genericImage.Frames; }); + Assert.Throws(() => { ImageFrameCollection prop = genericImage.Frames; }); } [Fact] public void Save_ObjectDisposedException() { - using var stream = new MemoryStream(); - var image = new Image(this.configuration, 10, 10); - var encoder = new JpegEncoder(); + using MemoryStream stream = new(); + Image image = new(this.configuration, 10, 10); + JpegEncoder encoder = new(); image.Dispose(); @@ -307,18 +307,18 @@ public partial class ImageTests [Fact] public void NonPrivateMethods_ObjectDisposedException() { - var image = new Image(this.configuration, 10, 10); - var genericImage = (Image)image; + Image image = new(this.configuration, 10, 10); + Image genericImage = (Image)image; image.Dispose(); // Image - Assert.Throws(() => { var res = image.Clone(this.configuration); }); - Assert.Throws(() => { var res = image.CloneAs(this.configuration); }); - Assert.Throws(() => { var res = image.DangerousTryGetSinglePixelMemory(out Memory _); }); + Assert.Throws(() => { Image res = image.Clone(this.configuration); }); + Assert.Throws(() => { Image res = image.CloneAs(this.configuration); }); + Assert.Throws(() => { bool res = image.DangerousTryGetSinglePixelMemory(out Memory _); }); // Image - Assert.Throws(() => { var res = genericImage.CloneAs(this.configuration); }); + Assert.Throws(() => { Image res = genericImage.CloneAs(this.configuration); }); } } @@ -327,29 +327,29 @@ public partial class ImageTests [Fact] public void KnownExtension_ReturnsEncoder() { - using var image = new Image(1, 1); + using Image image = new(1, 1); IImageEncoder encoder = image.DetectEncoder("dummy.png"); Assert.NotNull(encoder); Assert.IsType(encoder); } [Fact] - public void UnknownExtension_ThrowsNotSupportedException() + public void UnknownExtension_ThrowsUnknownImageFormatException() { - using var image = new Image(1, 1); - Assert.Throws(() => image.DetectEncoder("dummy.yolo")); + using Image image = new(1, 1); + Assert.Throws(() => image.DetectEncoder("dummy.yolo")); } [Fact] - public void NoDetectorRegisteredForKnownExtension_ThrowsNotSupportedException() + public void NoDetectorRegisteredForKnownExtension_ThrowsUnknownImageFormatException() { - var configuration = new Configuration(); - var format = new TestFormat(); + Configuration configuration = new(); + TestFormat format = new(); configuration.ImageFormatsManager.AddImageFormat(format); configuration.ImageFormatsManager.AddImageFormatDetector(new MockImageFormatDetector(format)); - using var image = new Image(configuration, 1, 1); - Assert.Throws(() => image.DetectEncoder($"dummy.{format.Extension}")); + using Image image = new(configuration, 1, 1); + Assert.Throws(() => image.DetectEncoder($"dummy.{format.Extension}")); } } } diff --git a/tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs b/tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs index 7309185c79..12caa6e7a9 100644 --- a/tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs +++ b/tests/ImageSharp.Tests/Image/LargeImageIntegrationTests.cs @@ -56,7 +56,7 @@ public class LargeImageIntegrationTests Configuration configuration = Configuration.Default.Clone(); configuration.PreferContiguousImageBuffers = true; configuration.ImageFormatsManager.TryFindFormatByFileExtension(formatInner, out IImageFormat format); - IImageEncoder encoder = configuration.ImageFormatsManager.FindEncoder(format!); + IImageEncoder encoder = configuration.ImageFormatsManager.GetEncoder(format!); string dir = TestEnvironment.CreateOutputDirectory(".Temp"); string path = Path.Combine(dir, $"{Guid.NewGuid()}.{formatInner}"); using (Image temp = new(2048, 2048)) diff --git a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs index 38615f09e9..86b9223c2e 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Transforms/ResizeTests.cs @@ -38,15 +38,13 @@ public class ResizeTests { string filePath = TestFile.GetInputFileFullPath(TestImages.Jpeg.Baseline.Calliphora); - using (var image = Image.Load(filePath)) - { - image.Mutate(x => x.Resize(image.Size() / 2)); - string path = System.IO.Path.Combine( - TestEnvironment.CreateOutputDirectory(nameof(ResizeTests)), - nameof(this.Resize_PixelAgnostic) + ".png"); + using Image image = Image.Load(filePath); + image.Mutate(x => x.Resize(image.Size() / 2)); + string path = Path.Combine( + TestEnvironment.CreateOutputDirectory(nameof(ResizeTests)), + nameof(this.Resize_PixelAgnostic) + ".png"); - image.Save(path); - } + image.Save(path); } [Theory(Skip = "Debug only, enable manually")] @@ -63,11 +61,9 @@ public class ResizeTests provider.Configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInKilobytes * 1024; - using (Image image = provider.GetImage()) - { - image.Mutate(x => x.Resize(destSize, destSize)); - image.DebugSave(provider, appendPixelTypeToFileName: false); - } + using Image image = provider.GetImage(); + image.Mutate(x => x.Resize(destSize, destSize)); + image.DebugSave(provider, appendPixelTypeToFileName: false); } [Theory] @@ -81,14 +77,12 @@ public class ResizeTests // [WithBasicTestPatternImages(15, 12, PixelTypes.Rgba32, 2, 3, 1, 2)] means: // resizing: (15, 12) -> (10, 6) // kernel dimensions: (3, 4) - using (Image image = provider.GetImage()) - { - var destSize = new Size(image.Width * wN / wD, image.Height * hN / hD); - image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false)); - FormattableString outputInfo = $"({wN}÷{wD},{hN}÷{hD})"; - image.DebugSave(provider, outputInfo, appendPixelTypeToFileName: false); - image.CompareToReferenceOutput(provider, outputInfo, appendPixelTypeToFileName: false); - } + using Image image = provider.GetImage(); + Size destSize = new Size(image.Width * wN / wD, image.Height * hN / hD); + image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false)); + FormattableString outputInfo = $"({wN}÷{wD},{hN}÷{hD})"; + image.DebugSave(provider, outputInfo, appendPixelTypeToFileName: false); + image.CompareToReferenceOutput(provider, outputInfo, appendPixelTypeToFileName: false); } private static readonly int SizeOfVector4 = Unsafe.SizeOf(); @@ -106,48 +100,44 @@ public class ResizeTests int workingBufferLimitInRows) where TPixel : unmanaged, IPixel { - using (Image image0 = provider.GetImage()) - { - Size destSize = image0.Size() / 4; - - var configuration = Configuration.CreateDefaultInstance(); - - int workingBufferSizeHintInBytes = workingBufferLimitInRows * destSize.Width * SizeOfVector4; - var allocator = new TestMemoryAllocator(); - allocator.EnableNonThreadSafeLogging(); - configuration.MemoryAllocator = allocator; - configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInBytes; - - var verticalKernelMap = ResizeKernelMap.Calculate( - default, - destSize.Height, - image0.Height, - Configuration.Default.MemoryAllocator); - int minimumWorkerAllocationInBytes = verticalKernelMap.MaxDiameter * 2 * destSize.Width * SizeOfVector4; - verticalKernelMap.Dispose(); - - using (Image image = image0.Clone(configuration)) - { - image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false)); - - image.DebugSave( - provider, - testOutputDetails: workingBufferLimitInRows, - appendPixelTypeToFileName: false); - image.CompareToReferenceOutput( - ImageComparer.TolerantPercentage(0.004f), - provider, - testOutputDetails: workingBufferLimitInRows, - appendPixelTypeToFileName: false); + using Image image0 = provider.GetImage(); + Size destSize = image0.Size() / 4; + + Configuration configuration = Configuration.CreateDefaultInstance(); + + int workingBufferSizeHintInBytes = workingBufferLimitInRows * destSize.Width * SizeOfVector4; + TestMemoryAllocator allocator = new TestMemoryAllocator(); + allocator.EnableNonThreadSafeLogging(); + configuration.MemoryAllocator = allocator; + configuration.WorkingBufferSizeHintInBytes = workingBufferSizeHintInBytes; + + ResizeKernelMap verticalKernelMap = ResizeKernelMap.Calculate( + default, + destSize.Height, + image0.Height, + Configuration.Default.MemoryAllocator); + int minimumWorkerAllocationInBytes = verticalKernelMap.MaxDiameter * 2 * destSize.Width * SizeOfVector4; + verticalKernelMap.Dispose(); + + using Image image = image0.Clone(configuration); + image.Mutate(x => x.Resize(destSize, KnownResamplers.Bicubic, false)); + + image.DebugSave( + provider, + testOutputDetails: workingBufferLimitInRows, + appendPixelTypeToFileName: false); + image.CompareToReferenceOutput( + ImageComparer.TolerantPercentage(0.004f), + provider, + testOutputDetails: workingBufferLimitInRows, + appendPixelTypeToFileName: false); - Assert.NotEmpty(allocator.AllocationLog); + Assert.NotEmpty(allocator.AllocationLog); - int maxAllocationSize = allocator.AllocationLog.Where( - e => e.ElementType == typeof(Vector4)).Max(e => e.LengthInBytes); + int maxAllocationSize = allocator.AllocationLog.Where( + e => e.ElementType == typeof(Vector4)).Max(e => e.LengthInBytes); - Assert.True(maxAllocationSize <= Math.Max(workingBufferSizeHintInBytes, minimumWorkerAllocationInBytes)); - } - } + Assert.True(maxAllocationSize <= Math.Max(workingBufferSizeHintInBytes, minimumWorkerAllocationInBytes)); } [Theory] @@ -188,13 +178,11 @@ public class ResizeTests public void Resize_Compand(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - image.Mutate(x => x.Resize(image.Size() / 2, true)); + using Image image = provider.GetImage(); + image.Mutate(x => x.Resize(image.Size() / 2, true)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider); } [Theory] @@ -223,7 +211,7 @@ public class ResizeTests provider.RunValidatingProcessorTest( x => { - var resizeOptions = new ResizeOptions() + ResizeOptions resizeOptions = new ResizeOptions() { Size = x.GetCurrentSize() / 2, Mode = ResizeMode.Crop, @@ -243,13 +231,11 @@ public class ResizeTests public void Resize_IsAppliedToAllFrames(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, KnownResamplers.Bicubic)); + using Image image = provider.GetImage(); + image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2, KnownResamplers.Bicubic)); - // Comparer fights decoder with gif-s. Could not use CompareToReferenceOutput here :( - image.DebugSave(provider, extension: "gif"); - } + // Comparer fights decoder with gif-s. Could not use CompareToReferenceOutput here :( + image.DebugSave(provider, extension: "gif"); } [Theory] @@ -265,17 +251,13 @@ public class ResizeTests public void Resize_ThrowsForWrappedMemoryImage(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image0 = provider.GetImage()) - { - Assert.True(image0.DangerousTryGetSinglePixelMemory(out Memory imageMem)); - var mmg = TestMemoryManager.CreateAsCopyOf(imageMem.Span); + using Image image0 = provider.GetImage(); + Assert.True(image0.DangerousTryGetSinglePixelMemory(out Memory imageMem)); + TestMemoryManager mmg = TestMemoryManager.CreateAsCopyOf(imageMem.Span); - using (var image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height)) - { - Assert.ThrowsAny( - () => { image1.Mutate(x => x.Resize(image0.Width / 2, image0.Height / 2, true)); }); - } - } + using Image image1 = Image.WrapMemory(mmg.Memory, image0.Width, image0.Height); + Assert.ThrowsAny( + () => { image1.Mutate(x => x.Resize(image0.Width / 2, image0.Height / 2, true)); }); } [Theory] @@ -341,7 +323,7 @@ public class ResizeTests && TestEnvironment.NetCoreVersion == null && sampler is NearestNeighborResampler; - var comparer = ImageComparer.TolerantPercentage(allowHigherInaccuracy ? 0.3f : 0.017f); + ImageComparer comparer = ImageComparer.TolerantPercentage(allowHigherInaccuracy ? 0.3f : 0.017f); // Let's make the working buffer size non-default: provider.Configuration.WorkingBufferSizeHintInBytes = 16 * 1024 * SizeOfVector4; @@ -382,27 +364,25 @@ public class ResizeTests public void ResizeFromSourceRectangle(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - var sourceRectangle = new Rectangle( - image.Width / 8, - image.Height / 8, - image.Width / 4, - image.Height / 4); - var destRectangle = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2); - - image.Mutate( - x => x.Resize( - image.Width, - image.Height, - KnownResamplers.Bicubic, - sourceRectangle, - destRectangle, - false)); - - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + using Image image = provider.GetImage(); + Rectangle sourceRectangle = new Rectangle( + image.Width / 8, + image.Height / 8, + image.Width / 4, + image.Height / 4); + Rectangle destRectangle = new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2); + + image.Mutate( + x => x.Resize( + image.Width, + image.Height, + KnownResamplers.Bicubic, + sourceRectangle, + destRectangle, + false)); + + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -410,13 +390,11 @@ public class ResizeTests public void ResizeHeightAndKeepAspect(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - image.Mutate(x => x.Resize(0, image.Height / 3, false)); + using Image image = provider.GetImage(); + image.Mutate(x => x.Resize(0, image.Height / 3, false)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -424,12 +402,10 @@ public class ResizeTests public void ResizeHeightCannotKeepAspectKeepsOnePixel(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - image.Mutate(x => x.Resize(0, 5)); - Assert.Equal(1, image.Width); - Assert.Equal(5, image.Height); - } + using Image image = provider.GetImage(); + image.Mutate(x => x.Resize(0, 5)); + Assert.Equal(1, image.Width); + Assert.Equal(5, image.Height); } [Theory] @@ -437,13 +413,11 @@ public class ResizeTests public void ResizeWidthAndKeepAspect(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - image.Mutate(x => x.Resize(image.Width / 3, 0, false)); + using Image image = provider.GetImage(); + image.Mutate(x => x.Resize(image.Width / 3, 0, false)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -451,12 +425,10 @@ public class ResizeTests public void ResizeWidthCannotKeepAspectKeepsOnePixel(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - image.Mutate(x => x.Resize(5, 0)); - Assert.Equal(5, image.Width); - Assert.Equal(1, image.Height); - } + using Image image = provider.GetImage(); + image.Mutate(x => x.Resize(5, 0)); + Assert.Equal(5, image.Width); + Assert.Equal(1, image.Height); } [Theory] @@ -464,20 +436,18 @@ public class ResizeTests public void ResizeWithBoxPadMode(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) + using Image image = provider.GetImage(); + ResizeOptions options = new ResizeOptions { - var options = new ResizeOptions - { - Size = new Size(image.Width + 200, image.Height + 200), - Mode = ResizeMode.BoxPad, - PadColor = Color.HotPink - }; + Size = new Size(image.Width + 200, image.Height + 200), + Mode = ResizeMode.BoxPad, + PadColor = Color.HotPink + }; - image.Mutate(x => x.Resize(options)); + image.Mutate(x => x.Resize(options)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -485,15 +455,13 @@ public class ResizeTests public void ResizeWithCropHeightMode(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - var options = new ResizeOptions { Size = new Size(image.Width, image.Height / 2) }; + using Image image = provider.GetImage(); + ResizeOptions options = new ResizeOptions { Size = new Size(image.Width, image.Height / 2) }; - image.Mutate(x => x.Resize(options)); + image.Mutate(x => x.Resize(options)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -501,15 +469,13 @@ public class ResizeTests public void ResizeWithCropWidthMode(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - var options = new ResizeOptions { Size = new Size(image.Width / 2, image.Height) }; + using Image image = provider.GetImage(); + ResizeOptions options = new ResizeOptions { Size = new Size(image.Width / 2, image.Height) }; - image.Mutate(x => x.Resize(options)); + image.Mutate(x => x.Resize(options)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -517,19 +483,17 @@ public class ResizeTests public void CanResizeLargeImageWithCropMode(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) + using Image image = provider.GetImage(); + ResizeOptions options = new ResizeOptions { - var options = new ResizeOptions - { - Size = new Size(480, 600), - Mode = ResizeMode.Crop - }; + Size = new Size(480, 600), + Mode = ResizeMode.Crop + }; - image.Mutate(x => x.Resize(options)); + image.Mutate(x => x.Resize(options)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -537,15 +501,13 @@ public class ResizeTests public void ResizeWithMaxMode(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) - { - var options = new ResizeOptions { Size = new Size(300, 300), Mode = ResizeMode.Max }; + using Image image = provider.GetImage(); + ResizeOptions options = new ResizeOptions { Size = new Size(300, 300), Mode = ResizeMode.Max }; - image.Mutate(x => x.Resize(options)); + image.Mutate(x => x.Resize(options)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -553,19 +515,17 @@ public class ResizeTests public void ResizeWithMinMode(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) + using Image image = provider.GetImage(); + ResizeOptions options = new ResizeOptions { - var options = new ResizeOptions - { - Size = new Size((int)Math.Round(image.Width * .75F), (int)Math.Round(image.Height * .95F)), - Mode = ResizeMode.Min - }; + Size = new Size((int)Math.Round(image.Width * .75F), (int)Math.Round(image.Height * .95F)), + Mode = ResizeMode.Min + }; - image.Mutate(x => x.Resize(options)); + image.Mutate(x => x.Resize(options)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -573,20 +533,18 @@ public class ResizeTests public void ResizeWithPadMode(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) + using Image image = provider.GetImage(); + ResizeOptions options = new ResizeOptions { - var options = new ResizeOptions - { - Size = new Size(image.Width + 200, image.Height), - Mode = ResizeMode.Pad, - PadColor = Color.Lavender - }; + Size = new Size(image.Width + 200, image.Height), + Mode = ResizeMode.Pad, + PadColor = Color.Lavender + }; - image.Mutate(x => x.Resize(options)); + image.Mutate(x => x.Resize(options)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -594,19 +552,17 @@ public class ResizeTests public void ResizeWithStretchMode(TestImageProvider provider) where TPixel : unmanaged, IPixel { - using (Image image = provider.GetImage()) + using Image image = provider.GetImage(); + ResizeOptions options = new ResizeOptions { - var options = new ResizeOptions - { - Size = new Size(image.Width / 2, image.Height), - Mode = ResizeMode.Stretch - }; + Size = new Size(image.Width / 2, image.Height), + Mode = ResizeMode.Stretch + }; - image.Mutate(x => x.Resize(options)); + image.Mutate(x => x.Resize(options)); - image.DebugSave(provider); - image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); - } + image.DebugSave(provider); + image.CompareToReferenceOutput(ValidatorComparer, provider, appendPixelTypeToFileName: false); } [Theory] @@ -622,27 +578,23 @@ public class ResizeTests return; } - using (Image image = provider.GetImage()) - { - // Don't bother saving, we're testing the EXIF metadata updates. - image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2)); - } + using Image image = provider.GetImage(); + // Don't bother saving, we're testing the EXIF metadata updates. + image.Mutate(x => x.Resize(image.Width / 2, image.Height / 2)); } [Fact] public void Issue1195() { - using (var image = new Image(2, 300)) - { - var size = new Size(50, 50); - image.Mutate(x => x - .Resize( - new ResizeOptions - { - Size = size, - Mode = ResizeMode.Max - })); - } + using Image image = new Image(2, 300); + Size size = new Size(50, 50); + image.Mutate(x => x + .Resize( + new ResizeOptions + { + Size = size, + Mode = ResizeMode.Max + })); } [Theory] @@ -653,20 +605,18 @@ public class ResizeTests [InlineData(3, 7)] public void Issue1342(int width, int height) { - using (var image = new Image(1, 1)) - { - var size = new Size(width, height); - image.Mutate(x => x - .Resize( - new ResizeOptions - { - Size = size, - Sampler = KnownResamplers.NearestNeighbor - })); + using Image image = new Image(1, 1); + Size size = new Size(width, height); + image.Mutate(x => x + .Resize( + new ResizeOptions + { + Size = size, + Sampler = KnownResamplers.NearestNeighbor + })); - Assert.Equal(width, image.Width); - Assert.Equal(height, image.Height); - } + Assert.Equal(width, image.Width); + Assert.Equal(height, image.Height); } [Theory] diff --git a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs index f44ae5b23f..b91b5631bd 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestEnvironment.Formats.cs @@ -24,13 +24,13 @@ public static partial class TestEnvironment internal static IImageDecoder GetReferenceDecoder(string filePath) { IImageFormat format = GetImageFormat(filePath); - return Configuration.ImageFormatsManager.FindDecoder(format); + return Configuration.ImageFormatsManager.GetDecoder(format); } internal static IImageEncoder GetReferenceEncoder(string filePath) { IImageFormat format = GetImageFormat(filePath); - return Configuration.ImageFormatsManager.FindEncoder(format); + return Configuration.ImageFormatsManager.GetEncoder(format); } internal static IImageFormat GetImageFormat(string filePath)