Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some nullable disable comments #2312

Merged
merged 11 commits into from
Jan 10, 2023
5 changes: 2 additions & 3 deletions src/ImageSharp/Advanced/AdvancedImageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public static IImageEncoder DetectEncoder(this Image source, string filePath)
Guard.NotNull(filePath, nameof(filePath));

string ext = Path.GetExtension(filePath);
IImageFormat format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext);
if (format is null)
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:");
Expand All @@ -40,7 +39,7 @@ public static IImageEncoder DetectEncoder(this Image source, string filePath)
throw new NotSupportedException(sb.ToString());
}

IImageEncoder encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format);
IImageEncoder? encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format);

if (encoder is null)
{
Expand Down
11 changes: 7 additions & 4 deletions src/ImageSharp/Formats/Bmp/BmpImageFormatDetector.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats.Bmp;

Expand All @@ -15,17 +15,20 @@ 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)
{
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;
Expand Down
8 changes: 5 additions & 3 deletions src/ImageSharp/Formats/Gif/GifImageFormatDetector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats.Gif;

Expand All @@ -13,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)
Expand Down
7 changes: 5 additions & 2 deletions src/ImageSharp/Formats/IImageFormatDetector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats;

/// <summary>
Expand All @@ -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);
}
1 change: 0 additions & 1 deletion src/ImageSharp/Formats/ImageDecoderUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;
Expand Down
22 changes: 13 additions & 9 deletions src/ImageSharp/Formats/ImageFormatManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats;

Expand Down Expand Up @@ -92,8 +92,9 @@ public void AddImageFormat(IImageFormat format)
/// For the specified file extensions type find the e <see cref="IImageFormat"/>.
/// </summary>
/// <param name="extension">The extension to discover</param>
/// <returns>The <see cref="IImageFormat"/> if found otherwise null</returns>
public IImageFormat FindFormatByFileExtension(string extension)
/// <param name="format">The <see cref="IImageFormat"/> if found otherwise null</param>
/// <returns>False if no format was found</returns>
public bool TryFindFormatByFileExtension(string extension, [NotNullWhen(true)] out IImageFormat? format)
stefannikolei marked this conversation as resolved.
Show resolved Hide resolved
{
Guard.NotNullOrWhiteSpace(extension, nameof(extension));

Expand All @@ -102,15 +103,18 @@ public IImageFormat FindFormatByFileExtension(string extension)
extension = extension[1..];
}

return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase));
format = this.imageFormats.FirstOrDefault(x =>
x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase));

return format != null;
}

/// <summary>
/// For the specified mime type find the <see cref="IImageFormat"/>.
/// </summary>
/// <param name="mimeType">The mime-type to discover</param>
/// <returns>The <see cref="IImageFormat"/> if found; otherwise null</returns>
public IImageFormat FindFormatByMimeType(string mimeType)
public IImageFormat? FindFormatByMimeType(string mimeType)
=> this.imageFormats.FirstOrDefault(x => x.MimeTypes.Contains(mimeType, StringComparer.OrdinalIgnoreCase));

/// <summary>
Expand Down Expand Up @@ -160,11 +164,11 @@ public void AddImageFormatDetector(IImageFormatDetector detector)
/// </summary>
/// <param name="format">The format to discover</param>
/// <returns>The <see cref="IImageDecoder"/> if found otherwise null</returns>
public IImageDecoder FindDecoder(IImageFormat format)
public IImageDecoder? FindDecoder(IImageFormat format)
{
Guard.NotNull(format, nameof(format));

return this.mimeTypeDecoders.TryGetValue(format, out IImageDecoder decoder)
return this.mimeTypeDecoders.TryGetValue(format, out IImageDecoder? decoder)
? decoder
: null;
}
Expand All @@ -174,11 +178,11 @@ public IImageDecoder FindDecoder(IImageFormat format)
/// </summary>
/// <param name="format">The format to discover</param>
/// <returns>The <see cref="IImageEncoder"/> if found otherwise null</returns>
public IImageEncoder FindEncoder(IImageFormat format)
public IImageEncoder? FindEncoder(IImageFormat format)
{
Guard.NotNull(format, nameof(format));

return this.mimeTypeEncoders.TryGetValue(format, out IImageEncoder encoder)
return this.mimeTypeEncoders.TryGetValue(format, out IImageEncoder? encoder)
? encoder
: null;
}
Expand Down
10 changes: 7 additions & 3 deletions src/ImageSharp/Formats/Jpeg/JpegImageFormatDetector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats.Jpeg;

Expand All @@ -13,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
Expand Down
9 changes: 7 additions & 2 deletions src/ImageSharp/Formats/Pbm/PbmImageFormatDetector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats.Pbm;

Expand All @@ -17,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)
{
Expand Down
7 changes: 4 additions & 3 deletions src/ImageSharp/Formats/Png/PngImageFormatDetector.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Buffers.Binary;
using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats.Png;

Expand All @@ -15,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)
Expand Down
8 changes: 5 additions & 3 deletions src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats.Tga;

Expand All @@ -13,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)
Expand Down
13 changes: 5 additions & 8 deletions src/ImageSharp/Formats/Tiff/TiffImageFormatDetector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats.Tiff;

Expand All @@ -13,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)
Expand Down
10 changes: 7 additions & 3 deletions src/ImageSharp/Formats/Webp/WebpImageFormatDetector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Diagnostics.CodeAnalysis;

namespace SixLabors.ImageSharp.Formats.Webp;

Expand All @@ -13,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);
Expand Down
3 changes: 1 addition & 2 deletions src/ImageSharp/Image.Decode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ private static IImageFormat InternalDetectFormat(Configuration configuration, St
{
if (formatDetector.HeaderSize <= headerSize)
{
IImageFormat attemptFormat = formatDetector.DetectFormat(headersBuffer);
if (attemptFormat != null)
if (formatDetector.TryDetectFormat(headersBuffer, out IImageFormat attemptFormat))
{
format = attemptFormat;
}
Expand Down
26 changes: 14 additions & 12 deletions src/ImageSharp/Image.FromBytes.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Diagnostics.CodeAnalysis;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.PixelFormats;

Expand All @@ -16,39 +16,41 @@ public abstract partial class Image
/// By reading the header on the provided byte span this calculates the images format.
/// </summary>
/// <param name="data">The byte span containing encoded image data to read the header from.</param>
/// <returns>The format or null if none found.</returns>
public static IImageFormat DetectFormat(ReadOnlySpan<byte> data)
=> DetectFormat(DecoderOptions.Default, data);
/// <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> data, [NotNullWhen(true)] out IImageFormat? format)
=> TryDetectFormat(DecoderOptions.Default, data, out format);

/// <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="data">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>
/// <exception cref="ArgumentNullException">The options are null.</exception>
/// <returns>The mime type or null if none found.</returns>
public static IImageFormat DetectFormat(DecoderOptions options, ReadOnlySpan<byte> data)
/// <returns>returns true when format was detected otherwise false.</returns>
public static bool TryDetectFormat(DecoderOptions options, ReadOnlySpan<byte> data, [NotNullWhen(true)] out IImageFormat? format)
{
Guard.NotNull(options, nameof(options.Configuration));

Configuration configuration = options.Configuration;
int maxHeaderSize = configuration.MaxHeaderSize;
if (maxHeaderSize <= 0)
{
return null;
format = null;
return false;
}

foreach (IImageFormatDetector detector in configuration.ImageFormatsManager.FormatDetectors)
{
IImageFormat f = detector.DetectFormat(data);

if (f != null)
if (detector.TryDetectFormat(data, out format))
{
return f;
return true;
}
}

return default;
format = default;
return false;
}

/// <summary>
Expand Down
Loading