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
9 changes: 3 additions & 6 deletions src/ImageSharp/Image.FromBytes.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.Formats;
using SixLabors.ImageSharp.PixelFormats;
Expand All @@ -17,7 +16,7 @@ public abstract partial class Image
/// </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)
public static IImageFormat? DetectFormat(ReadOnlySpan<byte> data)
=> DetectFormat(DecoderOptions.Default, data);

/// <summary>
Expand All @@ -27,7 +26,7 @@ public static IImageFormat DetectFormat(ReadOnlySpan<byte> data)
/// <param name="data">The byte span containing encoded image data to read the header from.</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)
public static IImageFormat? DetectFormat(DecoderOptions options, ReadOnlySpan<byte> data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to convert this and above to use the Try...Out pattern

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to convert InternalDetectFormat from Image.Decode.cs to the Try Pattern or just return IImageFormat?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally it should use the Try pattern all the way down.

Copy link
Contributor Author

@stefannikolei stefannikolei Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And at this Point it gets tricky. In Order to do this I have to also touch 'DiscoverDecoder' this already has IImageFormat as out parameter. When trying to refactor this to the try pattern we are in a situation where one ore the other could be null :(

What do you think if we change the implementation here tho that a format must be passed to DiscoverDecoder?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a second look and we wouldn't be able to adopt the Try pattern there actually. The method is used within a lambda which doesn't allow ref or out parameters..

{
Guard.NotNull(options, nameof(options.Configuration));

Expand All @@ -40,9 +39,7 @@ public static IImageFormat DetectFormat(DecoderOptions options, ReadOnlySpan<byt

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

if (f != null)
if (detector.TryDetectFormat(data, out IImageFormat? f))
{
return f;
}
Expand Down
4 changes: 1 addition & 3 deletions src/ImageSharp/Image{TPixel}.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable

using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using SixLabors.ImageSharp.Advanced;
Expand Down Expand Up @@ -420,7 +418,7 @@ private static Size ValidateFramesAndGetSize(IEnumerable<ImageFrame<TPixel>> fra
{
Guard.NotNull(frames, nameof(frames));

ImageFrame<TPixel> rootFrame = frames.FirstOrDefault();
ImageFrame<TPixel>? rootFrame = frames.FirstOrDefault();

if (rootFrame == null)
{
Expand Down
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.PixelFormats;

Expand Down Expand Up @@ -73,7 +72,7 @@ void IImageProcessor<TPixel>.Execute()
// Create an interim clone of the source image to operate on.
// Doing this allows for the application of transforms that will alter
// the dimensions of the image.
Image<TPixel> clone = default;
Image<TPixel>? clone = default;
try
{
clone = ((ICloningImageProcessor<TPixel>)this).CloneAndExecute();
Expand Down
Loading