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