Browse Source

Rewrite to TryDetectFormat

pull/2312/head
Stefan Nikolei 3 years ago
parent
commit
06e249584b
  1. 10
      src/ImageSharp/Formats/Bmp/BmpImageFormatDetector.cs
  2. 7
      src/ImageSharp/Formats/Gif/GifImageFormatDetector.cs
  3. 7
      src/ImageSharp/Formats/IImageFormatDetector.cs
  4. 9
      src/ImageSharp/Formats/Jpeg/JpegImageFormatDetector.cs
  5. 8
      src/ImageSharp/Formats/Pbm/PbmImageFormatDetector.cs
  6. 6
      src/ImageSharp/Formats/Png/PngImageFormatDetector.cs
  7. 7
      src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs
  8. 12
      src/ImageSharp/Formats/Tiff/TiffImageFormatDetector.cs
  9. 9
      src/ImageSharp/Formats/Webp/WebpImageFormatDetector.cs
  10. 3
      src/ImageSharp/Image.Decode.cs
  11. 4
      src/ImageSharp/Image.FromBytes.cs
  12. 7
      tests/ImageSharp.Tests/Image/MockImageFormatDetector.cs
  13. 10
      tests/ImageSharp.Tests/TestFormat.cs

10
src/ImageSharp/Formats/Bmp/BmpImageFormatDetector.cs

@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License.
using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats.Bmp;
@ -14,9 +15,11 @@ public sealed class BmpImageFormatDetector : IImageFormatDetector
public int HeaderSize => 2;
/// <inheritdoc/>
public IImageFormat? DetectFormat(ReadOnlySpan<byte> header)
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
return this.IsSupportedFileFormat(header) ? BmpFormat.Instance : null;
format = this.IsSupportedFileFormat(header) ? BmpFormat.Instance : null;
return format != null;
}
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)
@ -24,7 +27,8 @@ public sealed class BmpImageFormatDetector : IImageFormatDetector
if (header.Length >= this.HeaderSize)
{
short fileTypeMarker = BinaryPrimitives.ReadInt16LittleEndian(header);
return fileTypeMarker == BmpConstants.TypeMarkers.Bitmap || fileTypeMarker == BmpConstants.TypeMarkers.BitmapArray;
return fileTypeMarker == BmpConstants.TypeMarkers.Bitmap ||
fileTypeMarker == BmpConstants.TypeMarkers.BitmapArray;
}
return false;

7
src/ImageSharp/Formats/Gif/GifImageFormatDetector.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats.Gif;
/// <summary>
@ -12,9 +14,10 @@ public sealed class GifImageFormatDetector : IImageFormatDetector
public int HeaderSize => 6;
/// <inheritdoc/>
public IImageFormat? DetectFormat(ReadOnlySpan<byte> header)
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
return this.IsSupportedFileFormat(header) ? GifFormat.Instance : null;
format = this.IsSupportedFileFormat(header) ? GifFormat.Instance : null;
return format != null;
}
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)

7
src/ImageSharp/Formats/IImageFormatDetector.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats;
/// <summary>
@ -18,6 +20,7 @@ public interface IImageFormatDetector
/// Detect mimetype
/// </summary>
/// <param name="header">The <see cref="T:byte[]"/> containing the file header.</param>
/// <returns>returns the mime type of detected otherwise returns null</returns>
IImageFormat? DetectFormat(ReadOnlySpan<byte> header);
/// <param name="format">The mime type of detected otherwise returns null</param>
/// <returns>returns true when format was detected otherwise false.</returns>
bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format);
}

9
src/ImageSharp/Formats/Jpeg/JpegImageFormatDetector.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats.Jpeg;
/// <summary>
@ -12,8 +14,11 @@ public sealed class JpegImageFormatDetector : IImageFormatDetector
public int HeaderSize => 11;
/// <inheritdoc/>
public IImageFormat? DetectFormat(ReadOnlySpan<byte> header)
=> this.IsSupportedFileFormat(header) ? JpegFormat.Instance : null;
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
format = this.IsSupportedFileFormat(header) ? JpegFormat.Instance : null;
return format != null;
}
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)
=> header.Length >= this.HeaderSize

8
src/ImageSharp/Formats/Pbm/PbmImageFormatDetector.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats.Pbm;
/// <summary>
@ -16,7 +18,11 @@ public sealed class PbmImageFormatDetector : IImageFormatDetector
public int HeaderSize => 2;
/// <inheritdoc/>
public IImageFormat? DetectFormat(ReadOnlySpan<byte> header) => IsSupportedFileFormat(header) ? PbmFormat.Instance : null;
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
format = IsSupportedFileFormat(header) ? PbmFormat.Instance : null;
return format != null;
}
private static bool IsSupportedFileFormat(ReadOnlySpan<byte> header)
{

6
src/ImageSharp/Formats/Png/PngImageFormatDetector.cs

@ -2,6 +2,7 @@
// Licensed under the Six Labors Split License.
using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats.Png;
@ -14,9 +15,10 @@ public sealed class PngImageFormatDetector : IImageFormatDetector
public int HeaderSize => 8;
/// <inheritdoc/>
public IImageFormat? DetectFormat(ReadOnlySpan<byte> header)
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
return this.IsSupportedFileFormat(header) ? PngFormat.Instance : null;
format = this.IsSupportedFileFormat(header) ? PngFormat.Instance : null;
return format != null;
}
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)

7
src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats.Tga;
/// <summary>
@ -12,9 +14,10 @@ public sealed class TgaImageFormatDetector : IImageFormatDetector
public int HeaderSize => 16;
/// <inheritdoc/>
public IImageFormat? DetectFormat(ReadOnlySpan<byte> header)
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
return this.IsSupportedFileFormat(header) ? TgaFormat.Instance : null;
format = this.IsSupportedFileFormat(header) ? TgaFormat.Instance : null;
return format != null;
}
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)

12
src/ImageSharp/Formats/Tiff/TiffImageFormatDetector.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats.Tiff;
/// <summary>
@ -12,14 +14,10 @@ public sealed class TiffImageFormatDetector : IImageFormatDetector
public int HeaderSize => 8;
/// <inheritdoc/>
public IImageFormat? DetectFormat(ReadOnlySpan<byte> header)
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
if (this.IsSupportedFileFormat(header))
{
return TiffFormat.Instance;
}
return null;
format = this.IsSupportedFileFormat(header) ? TiffFormat.Instance : null;
return format != null;
}
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)

9
src/ImageSharp/Formats/Webp/WebpImageFormatDetector.cs

@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
namespace SixLabors.ImageSharp.Formats.Webp;
/// <summary>
@ -12,8 +14,11 @@ public sealed class WebpImageFormatDetector : IImageFormatDetector
public int HeaderSize => 12;
/// <inheritdoc />
public IImageFormat? DetectFormat(ReadOnlySpan<byte> header)
=> this.IsSupportedFileFormat(header) ? WebpFormat.Instance : null;
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
format = this.IsSupportedFileFormat(header) ? WebpFormat.Instance : null;
return format != null;
}
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)
=> header.Length >= this.HeaderSize && IsRiffContainer(header) && IsWebpFile(header);

3
src/ImageSharp/Image.Decode.cs

@ -82,8 +82,7 @@ public abstract partial class Image
{
if (formatDetector.HeaderSize <= headerSize)
{
IImageFormat attemptFormat = formatDetector.DetectFormat(headersBuffer);
if (attemptFormat != null)
if (formatDetector.TryDetectFormat(headersBuffer, out IImageFormat attemptFormat))
{
format = attemptFormat;
}

4
src/ImageSharp/Image.FromBytes.cs

@ -39,9 +39,7 @@ public abstract partial class Image
foreach (IImageFormatDetector detector in configuration.ImageFormatsManager.FormatDetectors)
{
IImageFormat? f = detector.DetectFormat(data);
if (f != null)
if (detector.TryDetectFormat(data, out IImageFormat? f))
{
return f;
}

7
tests/ImageSharp.Tests/Image/MockImageFormatDetector.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
using SixLabors.ImageSharp.Formats;
namespace SixLabors.ImageSharp.Tests;
@ -19,8 +20,10 @@ public class MockImageFormatDetector : IImageFormatDetector
public int HeaderSize => 1;
public IImageFormat DetectFormat(ReadOnlySpan<byte> header)
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
return this.localImageFormatMock;
format = this.localImageFormatMock;
return true;
}
}

10
tests/ImageSharp.Tests/TestFormat.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
using System.Numerics;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;
@ -174,14 +175,11 @@ public class TestFormat : IImageFormatConfigurationModule, IImageFormat
public int HeaderSize => this.testFormat.HeaderSize;
public IImageFormat DetectFormat(ReadOnlySpan<byte> header)
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
{
if (this.testFormat.IsSupportedFileFormat(header))
{
return this.testFormat;
}
format = this.testFormat.IsSupportedFileFormat(header) ? this.testFormat : null;
return null;
return format != null;
}
public TestHeader(TestFormat testFormat) => this.testFormat = testFormat;

Loading…
Cancel
Save