Browse Source

Revert recent Detect changes and normalize.

pull/2317/head
James Jackson-South 4 years ago
parent
commit
eff9b0a6a5
  1. 38
      src/ImageSharp/Image.FromBytes.cs
  2. 51
      src/ImageSharp/Image.FromFile.cs
  3. 62
      src/ImageSharp/Image.FromStream.cs
  4. 16
      tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs
  5. 16
      tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs
  6. 13
      tests/ImageSharp.Tests/Formats/ImageFormatManagerTests.cs
  7. 16
      tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs
  8. 16
      tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs
  9. 16
      tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs
  10. 16
      tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs
  11. 16
      tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs
  12. 16
      tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs
  13. 59
      tests/ImageSharp.Tests/Image/ImageTests.DetectFormat.cs
  14. 4
      tests/ImageSharp.Tests/Image/ImageTests.Save.cs
  15. 6
      tests/ImageSharp.Tests/Image/ImageTests.SaveAsync.cs
  16. 1
      tests/ImageSharp.Tests/TestImages.cs

38
src/ImageSharp/Image.FromBytes.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
@ -16,41 +15,32 @@ public abstract partial class Image
/// By reading the header on the provided byte span this calculates the images format.
/// </summary>
/// <param name="buffer">The byte span containing encoded image data to read the header from.</param>
/// <param name="format">The format or null if none found.</param>
/// <returns>returns true when format was detected otherwise false.</returns>
public static bool TryDetectFormat(ReadOnlySpan<byte> buffer, [NotNullWhen(true)] out IImageFormat? format)
=> TryDetectFormat(DecoderOptions.Default, buffer, out format);
/// <returns>The <see cref="IImageFormat"/>.</returns>
/// <exception cref="NotSupportedException">The image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static IImageFormat DetectFormat(ReadOnlySpan<byte> buffer)
=> DetectFormat(DecoderOptions.Default, buffer);
/// <summary>
/// By reading the header on the provided byte span this calculates the images format.
/// </summary>
/// <param name="options">The general decoder options.</param>
/// <param name="buffer">The byte span containing encoded image data to read the header from.</param>
/// <param name="format">The mime type or null if none found.</param>
/// <returns>The <see cref="IImageFormat"/>.</returns>
/// <exception cref="ArgumentNullException">The options are null.</exception>
/// <returns>returns true when format was detected otherwise false.</returns>
public static bool TryDetectFormat(DecoderOptions options, ReadOnlySpan<byte> buffer, [NotNullWhen(true)] out IImageFormat? format)
/// <exception cref="NotSupportedException">The image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static unsafe IImageFormat DetectFormat(DecoderOptions options, ReadOnlySpan<byte> buffer)
{
Guard.NotNull(options, nameof(options.Configuration));
Configuration configuration = options.Configuration;
int maxHeaderSize = configuration.MaxHeaderSize;
if (maxHeaderSize <= 0)
{
format = null;
return false;
}
foreach (IImageFormatDetector detector in configuration.ImageFormatsManager.FormatDetectors)
fixed (byte* ptr = buffer)
{
if (detector.TryDetectFormat(buffer, out format))
{
return true;
}
using UnmanagedMemoryStream stream = new(ptr, buffer.Length);
return DetectFormat(options, stream);
}
format = default;
return false;
}
/// <summary>

51
src/ImageSharp/Image.FromFile.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
@ -14,61 +13,59 @@ public abstract partial class Image
{
/// <summary>
/// Detects the encoded image format type from the specified file.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="path">The image file to open and to read the header from.</param>
/// <param name="format">
/// When this method returns, contains the format that matches the given file;
/// otherwise, the default value for the type of the <paramref name="format"/> parameter.
/// This parameter is passed uninitialized.
/// </param>
/// <returns><see langword="true"/> if a match is found; otherwise, <see langword="false"/></returns>
public static bool TryDetectFormat(string path, [NotNullWhen(true)] out IImageFormat? format)
=> TryDetectFormat(DecoderOptions.Default, path, out format);
/// <returns>The <see cref="IImageFormat"/>.</returns>
/// <exception cref="ArgumentNullException">The path is null.</exception>
/// <exception cref="NotSupportedException">The file stream is not readable or the image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static IImageFormat DetectFormat(string path)
=> DetectFormat(DecoderOptions.Default, path);
/// <summary>
/// Detects the encoded image format type from the specified file.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="options">The general decoder options.</param>
/// <param name="path">The image file to open and to read the header from.</param>
/// <param name="format">
/// When this method returns, contains the format that matches the given file;
/// otherwise, the default value for the type of the <paramref name="format"/> parameter.
/// This parameter is passed uninitialized.
/// </param>
/// <returns><see langword="true"/> if a match is found; otherwise, <see langword="false"/></returns>
/// <returns>The <see cref="IImageFormat"/>.</returns>
/// <exception cref="ArgumentNullException">The options are null.</exception>
public static bool TryDetectFormat(DecoderOptions options, string path, [NotNullWhen(true)] out IImageFormat? format)
/// <exception cref="ArgumentNullException">The path is null.</exception>
/// <exception cref="NotSupportedException">The file stream is not readable or the image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static IImageFormat DetectFormat(DecoderOptions options, string path)
{
Guard.NotNull(options, nameof(options));
using Stream file = options.Configuration.FileSystem.OpenRead(path);
return TryDetectFormat(options, file, out format);
return DetectFormat(options, file);
}
/// <summary>
/// Detects the encoded image format type from the specified file.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="path">The image file to open and to read the header from.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task{Attempt}"/> representing the asynchronous operation.</returns>
public static Task<Attempt<IImageFormat>> TryDetectFormatAsync(
/// <returns>A <see cref="Task{IImageFormat}"/> representing the asynchronous operation.</returns>
public static Task<IImageFormat> DetectFormatAsync(
string path,
CancellationToken cancellationToken = default)
=> TryDetectFormatAsync(DecoderOptions.Default, path, cancellationToken);
=> DetectFormatAsync(DecoderOptions.Default, path, cancellationToken);
/// <summary>
/// Detects the encoded image format type from the specified file.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="options">The general decoder options.</param>
/// <param name="path">The image file to open and to read the header from.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task{IImageFormat}"/> representing the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">The options are null.</exception>
/// <returns>A <see cref="Task{Attempt}"/> representing the asynchronous operation.</returns>
public static async Task<Attempt<IImageFormat>> TryDetectFormatAsync(
/// <exception cref="ArgumentNullException">The path is null.</exception>
/// <exception cref="NotSupportedException">The file stream is not readable or the image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static async Task<IImageFormat> DetectFormatAsync(
DecoderOptions options,
string path,
CancellationToken cancellationToken = default)
@ -76,7 +73,7 @@ public abstract partial class Image
Guard.NotNull(options, nameof(options));
using Stream stream = options.Configuration.FileSystem.OpenRead(path);
return await TryDetectFormatAsync(options, stream, cancellationToken).ConfigureAwait(false);
return await DetectFormatAsync(options, stream, cancellationToken).ConfigureAwait(false);
}
/// <summary>

62
src/ImageSharp/Image.FromStream.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.PixelFormats;
@ -15,79 +14,66 @@ public abstract partial class Image
{
/// <summary>
/// Detects the encoded image format type from the specified stream.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="stream">The image stream to read the header from.</param>
/// <param name="format">
/// When this method returns, contains the format that matches the given stream;
/// otherwise, the default value for the type of the <paramref name="format"/> parameter.
/// This parameter is passed uninitialized.
/// </param>
/// <returns><see langword="true"/> if a match is found; otherwise, <see langword="false"/></returns>
/// <returns>The <see cref="IImageFormat"/>.</returns>
/// <exception cref="ArgumentNullException">The stream is null.</exception>
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
public static bool TryDetectFormat(Stream stream, [NotNullWhen(true)] out IImageFormat? format)
=> TryDetectFormat(DecoderOptions.Default, stream, out format);
/// <exception cref="NotSupportedException">The stream is not readable or the image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static IImageFormat DetectFormat(Stream stream)
=> DetectFormat(DecoderOptions.Default, stream);
/// <summary>
/// Detects the encoded image format type from the specified stream.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="options">The general decoder options.</param>
/// <param name="stream">The image stream to read the header from.</param>
/// <param name="format">
/// When this method returns, contains the format that matches the given stream;
/// otherwise, the default value for the type of the <paramref name="format"/> parameter.
/// This parameter is passed uninitialized.
/// </param>
/// <returns><see langword="true"/> if a match is found; otherwise, <see langword="false"/></returns>
/// <exception cref="ArgumentNullException">The options are null.</exception>
/// <exception cref="ArgumentNullException">The stream is null.</exception>
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
public static bool TryDetectFormat(DecoderOptions options, Stream stream, [NotNullWhen(true)] out IImageFormat? format)
{
format = WithSeekableStream(options, stream, s => InternalDetectFormat(options.Configuration, s));
return format is not null;
}
/// <exception cref="NotSupportedException">The stream is not readable or the image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static IImageFormat DetectFormat(DecoderOptions options, Stream stream)
=> WithSeekableStream(options, stream, s => InternalDetectFormat(options.Configuration, s));
/// <summary>
/// Detects the encoded image format type from the specified stream.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="stream">The image stream to read the header from.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task{IImageFormat}"/> representing the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">The stream is null.</exception>
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
/// <returns>A <see cref="Task{Attempt}"/> representing the asynchronous operation.</returns>
public static Task<Attempt<IImageFormat>> TryDetectFormatAsync(
/// <exception cref="NotSupportedException">The stream is not readable or the image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static Task<IImageFormat> DetectFormatAsync(
Stream stream,
CancellationToken cancellationToken = default)
=> TryDetectFormatAsync(DecoderOptions.Default, stream, cancellationToken);
=> DetectFormatAsync(DecoderOptions.Default, stream, cancellationToken);
/// <summary>
/// Detects the encoded image format type from the specified stream.
/// A return value indicates whether the operation succeeded.
/// </summary>
/// <param name="options">The general decoder options.</param>
/// <param name="stream">The image stream to read the header from.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>A <see cref="Task{IImageFormat}"/> representing the asynchronous operation.</returns>
/// <exception cref="ArgumentNullException">The options are null.</exception>
/// <exception cref="ArgumentNullException">The stream is null.</exception>
/// <exception cref="NotSupportedException">The stream is not readable.</exception>
/// <returns>A <see cref="Task{Attempt}"/> representing the asynchronous operation.</returns>
public static async Task<Attempt<IImageFormat>> TryDetectFormatAsync(
/// <exception cref="NotSupportedException">The stream is not readable or the image format is not supported.</exception>
/// <exception cref="InvalidImageContentException">The encoded image contains invalid content.</exception>
/// <exception cref="UnknownImageFormatException">The encoded image format is unknown.</exception>
public static Task<IImageFormat> DetectFormatAsync(
DecoderOptions options,
Stream stream,
CancellationToken cancellationToken = default)
{
IImageFormat? format = await WithSeekableStreamAsync(
=> WithSeekableStreamAsync(
options,
stream,
(s, _) => Task.FromResult(InternalDetectFormat(options.Configuration, s)),
cancellationToken).ConfigureAwait(false);
return new() { Value = format };
}
cancellationToken);
/// <summary>
/// Reads the raw image information from the specified stream without fully decoding it.

16
tests/ImageSharp.Tests/Formats/Bmp/ImageExtensionsTest.cs

@ -20,7 +20,7 @@ public class ImageExtensionsTest
image.SaveAsBmp(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is BmpFormat);
}
@ -35,7 +35,7 @@ public class ImageExtensionsTest
await image.SaveAsBmpAsync(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is BmpFormat);
}
@ -50,7 +50,7 @@ public class ImageExtensionsTest
image.SaveAsBmp(file, new BmpEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is BmpFormat);
}
@ -65,7 +65,7 @@ public class ImageExtensionsTest
await image.SaveAsBmpAsync(file, new BmpEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is BmpFormat);
}
@ -81,7 +81,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is BmpFormat);
}
@ -97,7 +97,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is BmpFormat);
}
@ -113,7 +113,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is BmpFormat);
}
@ -129,7 +129,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is BmpFormat);
}
}

16
tests/ImageSharp.Tests/Formats/Gif/ImageExtensionsTest.cs

@ -20,7 +20,7 @@ public class ImageExtensionsTest
image.SaveAsGif(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is GifFormat);
}
@ -35,7 +35,7 @@ public class ImageExtensionsTest
await image.SaveAsGifAsync(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is GifFormat);
}
@ -50,7 +50,7 @@ public class ImageExtensionsTest
image.SaveAsGif(file, new GifEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is GifFormat);
}
@ -65,7 +65,7 @@ public class ImageExtensionsTest
await image.SaveAsGifAsync(file, new GifEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is GifFormat);
}
@ -81,7 +81,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is GifFormat);
}
@ -97,7 +97,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is GifFormat);
}
@ -113,7 +113,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is GifFormat);
}
@ -129,7 +129,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is GifFormat);
}
}

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

@ -120,15 +120,10 @@ public class ImageFormatManagerTests
jpegImage = buffer.ToArray();
}
byte[] invalidImage = { 1, 2, 3 };
bool resultValidImage = Image.TryDetectFormat(jpegImage, out IImageFormat format);
IImageFormat format = Image.DetectFormat(jpegImage);
Assert.IsType<JpegFormat>(format);
bool resultInvalidImage = Image.TryDetectFormat(invalidImage, out IImageFormat formatInvalid);
Assert.True(resultValidImage);
Assert.Equal(format, JpegFormat.Instance);
Assert.False(resultInvalidImage);
Assert.True(formatInvalid is null);
byte[] invalidImage = { 1, 2, 3 };
Assert.Throws<UnknownImageFormatException>(() => Image.DetectFormat(invalidImage));
}
}

16
tests/ImageSharp.Tests/Formats/Jpg/ImageExtensionsTest.cs

@ -21,7 +21,7 @@ public class ImageExtensionsTest
image.SaveAsJpeg(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is JpegFormat);
}
@ -36,7 +36,7 @@ public class ImageExtensionsTest
await image.SaveAsJpegAsync(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is JpegFormat);
}
@ -51,7 +51,7 @@ public class ImageExtensionsTest
image.SaveAsJpeg(file, new JpegEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is JpegFormat);
}
@ -66,7 +66,7 @@ public class ImageExtensionsTest
await image.SaveAsJpegAsync(file, new JpegEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is JpegFormat);
}
@ -82,7 +82,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is JpegFormat);
}
@ -98,7 +98,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is JpegFormat);
}
@ -114,7 +114,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is JpegFormat);
}
@ -130,7 +130,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is JpegFormat);
}
}

16
tests/ImageSharp.Tests/Formats/Pbm/ImageExtensionsTest.cs

@ -20,7 +20,7 @@ public class ImageExtensionsTest
image.SaveAsPbm(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PbmFormat);
}
@ -35,7 +35,7 @@ public class ImageExtensionsTest
await image.SaveAsPbmAsync(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PbmFormat);
}
@ -50,7 +50,7 @@ public class ImageExtensionsTest
image.SaveAsPbm(file, new PbmEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PbmFormat);
}
@ -65,7 +65,7 @@ public class ImageExtensionsTest
await image.SaveAsPbmAsync(file, new PbmEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PbmFormat);
}
@ -81,7 +81,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is PbmFormat);
}
@ -97,7 +97,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is PbmFormat);
}
@ -113,7 +113,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is PbmFormat);
}
@ -129,7 +129,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is PbmFormat);
}
}

16
tests/ImageSharp.Tests/Formats/Png/ImageExtensionsTest.cs

@ -21,7 +21,7 @@ public class ImageExtensionsTest
image.SaveAsPng(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PngFormat);
}
@ -36,7 +36,7 @@ public class ImageExtensionsTest
await image.SaveAsPngAsync(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PngFormat);
}
@ -51,7 +51,7 @@ public class ImageExtensionsTest
image.SaveAsPng(file, new PngEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PngFormat);
}
@ -66,7 +66,7 @@ public class ImageExtensionsTest
await image.SaveAsPngAsync(file, new PngEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PngFormat);
}
@ -82,7 +82,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is PngFormat);
}
@ -98,7 +98,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is PngFormat);
}
@ -114,7 +114,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is PngFormat);
}
@ -130,7 +130,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is PngFormat);
}
}

16
tests/ImageSharp.Tests/Formats/Tga/ImageExtensionsTest.cs

@ -20,7 +20,7 @@ public class ImageExtensionsTest
image.SaveAsTga(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is TgaFormat);
}
@ -35,7 +35,7 @@ public class ImageExtensionsTest
await image.SaveAsTgaAsync(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is TgaFormat);
}
@ -50,7 +50,7 @@ public class ImageExtensionsTest
image.SaveAsTga(file, new TgaEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is TgaFormat);
}
@ -65,7 +65,7 @@ public class ImageExtensionsTest
await image.SaveAsTgaAsync(file, new TgaEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is TgaFormat);
}
@ -81,7 +81,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is TgaFormat);
}
@ -97,7 +97,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is TgaFormat);
}
@ -113,7 +113,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is TgaFormat);
}
@ -129,7 +129,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is TgaFormat);
}
}

16
tests/ImageSharp.Tests/Formats/Tiff/ImageExtensionsTest.cs

@ -21,7 +21,7 @@ public class ImageExtensionsTest
image.SaveAsTiff(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is TiffFormat);
}
@ -36,7 +36,7 @@ public class ImageExtensionsTest
await image.SaveAsTiffAsync(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is TiffFormat);
}
@ -51,7 +51,7 @@ public class ImageExtensionsTest
image.SaveAsTiff(file, new TiffEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is TiffFormat);
}
@ -66,7 +66,7 @@ public class ImageExtensionsTest
await image.SaveAsTiffAsync(file, new TiffEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is TiffFormat);
}
@ -82,7 +82,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is TiffFormat);
}
@ -98,7 +98,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is TiffFormat);
}
@ -114,7 +114,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is TiffFormat);
}
@ -130,7 +130,7 @@ public class ImageExtensionsTest
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is TiffFormat);
}
}

16
tests/ImageSharp.Tests/Formats/WebP/ImageExtensionsTests.cs

@ -21,7 +21,7 @@ public class ImageExtensionsTests
image.SaveAsWebp(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is WebpFormat);
}
@ -36,7 +36,7 @@ public class ImageExtensionsTests
await image.SaveAsWebpAsync(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is WebpFormat);
}
@ -51,7 +51,7 @@ public class ImageExtensionsTests
image.SaveAsWebp(file, new WebpEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is WebpFormat);
}
@ -66,7 +66,7 @@ public class ImageExtensionsTests
await image.SaveAsWebpAsync(file, new WebpEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is WebpFormat);
}
@ -82,7 +82,7 @@ public class ImageExtensionsTests
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is WebpFormat);
}
@ -98,7 +98,7 @@ public class ImageExtensionsTests
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is WebpFormat);
}
@ -114,7 +114,7 @@ public class ImageExtensionsTests
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is WebpFormat);
}
@ -130,7 +130,7 @@ public class ImageExtensionsTests
memoryStream.Position = 0;
Image.TryDetectFormat(memoryStream, out IImageFormat format);
IImageFormat format = Image.DetectFormat(memoryStream);
Assert.True(format is WebpFormat);
}
}

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

@ -34,10 +34,8 @@ public partial class ImageTests
[Fact]
public void FromBytes_GlobalConfiguration()
{
bool result = Image.TryDetectFormat(ActualImageSpan, out IImageFormat type);
Assert.True(result);
Assert.Equal(ExpectedGlobalFormat, type);
IImageFormat format = Image.DetectFormat(ActualImageSpan);
Assert.Equal(ExpectedGlobalFormat, format);
}
[Fact]
@ -48,28 +46,22 @@ public partial class ImageTests
Configuration = this.LocalConfiguration
};
bool result = Image.TryDetectFormat(options, this.ByteArray, out IImageFormat type);
Assert.True(result);
Assert.Equal(this.LocalImageFormat, type);
IImageFormat format = Image.DetectFormat(options, this.ByteArray);
Assert.Equal(this.LocalImageFormat, format);
}
[Fact]
public void FromFileSystemPath_GlobalConfiguration()
{
bool result = Image.TryDetectFormat(ActualImagePath, out IImageFormat type);
Assert.True(result);
Assert.Equal(ExpectedGlobalFormat, type);
IImageFormat format = Image.DetectFormat(ActualImagePath);
Assert.Equal(ExpectedGlobalFormat, format);
}
[Fact]
public async Task FromFileSystemPathAsync_GlobalConfiguration()
{
Attempt<IImageFormat> attempt = await Image.TryDetectFormatAsync(ActualImagePath);
Assert.True(attempt.Success);
Assert.Equal(ExpectedGlobalFormat, attempt.Value);
IImageFormat format = await Image.DetectFormatAsync(ActualImagePath);
Assert.Equal(ExpectedGlobalFormat, format);
}
[Fact]
@ -80,10 +72,8 @@ public partial class ImageTests
Configuration = this.LocalConfiguration
};
bool result = Image.TryDetectFormat(options, this.MockFilePath, out IImageFormat type);
Assert.True(result);
Assert.Equal(this.LocalImageFormat, type);
IImageFormat format = Image.DetectFormat(options, this.MockFilePath);
Assert.Equal(this.LocalImageFormat, format);
}
[Fact]
@ -94,20 +84,17 @@ public partial class ImageTests
Configuration = this.LocalConfiguration
};
Attempt<IImageFormat> attempt = await Image.TryDetectFormatAsync(options, this.MockFilePath);
Assert.True(attempt.Success);
Assert.Equal(this.LocalImageFormat, attempt.Value);
IImageFormat format = await Image.DetectFormatAsync(options, this.MockFilePath);
Assert.Equal(this.LocalImageFormat, format);
}
[Fact]
public void FromStream_GlobalConfiguration()
{
using MemoryStream stream = new(ActualImageBytes);
bool result = Image.TryDetectFormat(stream, out IImageFormat type);
IImageFormat format = Image.DetectFormat(stream);
Assert.True(result);
Assert.Equal(ExpectedGlobalFormat, type);
Assert.Equal(ExpectedGlobalFormat, format);
}
[Fact]
@ -118,10 +105,8 @@ public partial class ImageTests
Configuration = this.LocalConfiguration
};
bool result = Image.TryDetectFormat(options, this.DataStream, out IImageFormat type);
Assert.True(result);
Assert.Equal(this.LocalImageFormat, type);
IImageFormat format = Image.DetectFormat(options, this.DataStream);
Assert.Equal(this.LocalImageFormat, format);
}
[Fact]
@ -132,15 +117,15 @@ public partial class ImageTests
Configuration = new()
};
Assert.Throws<UnknownImageFormatException>(() => Image.TryDetectFormat(options, this.DataStream, out IImageFormat type));
Assert.Throws<UnknownImageFormatException>(() => Image.DetectFormat(options, this.DataStream));
}
[Fact]
public async Task FromStreamAsync_GlobalConfiguration()
{
using MemoryStream stream = new(ActualImageBytes);
Attempt<IImageFormat> attempt = await Image.TryDetectFormatAsync(new AsyncStreamWrapper(stream, () => false));
Assert.Equal(ExpectedGlobalFormat, attempt.Value);
IImageFormat format = await Image.DetectFormatAsync(new AsyncStreamWrapper(stream, () => false));
Assert.Equal(ExpectedGlobalFormat, format);
}
[Fact]
@ -151,8 +136,8 @@ public partial class ImageTests
Configuration = this.LocalConfiguration
};
Attempt<IImageFormat> attempt = await Image.TryDetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false));
Assert.Equal(this.LocalImageFormat, attempt.Value);
IImageFormat format = await Image.DetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false));
Assert.Equal(this.LocalImageFormat, format);
}
[Fact]
@ -163,7 +148,7 @@ public partial class ImageTests
Configuration = new()
};
return Assert.ThrowsAsync<UnknownImageFormatException>(async () => await Image.TryDetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false)));
return Assert.ThrowsAsync<UnknownImageFormatException>(async () => await Image.DetectFormatAsync(options, new AsyncStreamWrapper(this.DataStream, () => false)));
}
}
}

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

@ -24,7 +24,7 @@ public partial class ImageTests
image.Save(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PngFormat);
}
@ -53,7 +53,7 @@ public partial class ImageTests
image.Save(file, new PngEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PngFormat);
}

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

@ -26,7 +26,7 @@ public partial class ImageTests
await image.SaveAsync(file);
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PngFormat);
}
@ -55,7 +55,7 @@ public partial class ImageTests
await image.SaveAsync(file, new PngEncoder());
}
Image.TryDetectFormat(file, out IImageFormat format);
IImageFormat format = Image.DetectFormat(file);
Assert.True(format is PngFormat);
}
@ -79,7 +79,7 @@ public partial class ImageTests
stream.Position = 0;
Image.TryDetectFormat(stream, out IImageFormat format2);
IImageFormat format2 = Image.DetectFormat(stream);
Assert.Equal(format, format2);
}

1
tests/ImageSharp.Tests/TestImages.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
// ReSharper disable InconsistentNaming
// ReSharper disable MemberHidesStaticFromOuterClass
namespace SixLabors.ImageSharp.Tests;

Loading…
Cancel
Save