Skip to content

Commit f1968ba

Browse files
Merge pull request #2312 from stefannikolei/stefannikolei/nullable
Remove some nullable disable comments
2 parents ddcb4b9 + 4a5f525 commit f1968ba

38 files changed

+221
-192
lines changed

src/ImageSharp/Advanced/AdvancedImageExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public static IImageEncoder DetectEncoder(this Image source, string filePath)
2727
Guard.NotNull(filePath, nameof(filePath));
2828

2929
string ext = Path.GetExtension(filePath);
30-
IImageFormat format = source.GetConfiguration().ImageFormatsManager.FindFormatByFileExtension(ext);
31-
if (format is null)
30+
if (!source.GetConfiguration().ImageFormatsManager.TryFindFormatByFileExtension(ext, out IImageFormat? format))
3231
{
3332
StringBuilder sb = new();
3433
sb.AppendLine(CultureInfo.InvariantCulture, $"No encoder was found for extension '{ext}'. Registered encoders include:");
@@ -40,7 +39,7 @@ public static IImageEncoder DetectEncoder(this Image source, string filePath)
4039
throw new NotSupportedException(sb.ToString());
4140
}
4241

43-
IImageEncoder encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format);
42+
IImageEncoder? encoder = source.GetConfiguration().ImageFormatsManager.FindEncoder(format);
4443

4544
if (encoder is null)
4645
{

src/ImageSharp/Formats/Bmp/BmpImageFormatDetector.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers.Binary;
5+
using System.Diagnostics.CodeAnalysis;
66

77
namespace SixLabors.ImageSharp.Formats.Bmp;
88

@@ -15,17 +15,20 @@ public sealed class BmpImageFormatDetector : IImageFormatDetector
1515
public int HeaderSize => 2;
1616

1717
/// <inheritdoc/>
18-
public IImageFormat DetectFormat(ReadOnlySpan<byte> header)
18+
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
1919
{
20-
return this.IsSupportedFileFormat(header) ? BmpFormat.Instance : null;
20+
format = this.IsSupportedFileFormat(header) ? BmpFormat.Instance : null;
21+
22+
return format != null;
2123
}
2224

2325
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)
2426
{
2527
if (header.Length >= this.HeaderSize)
2628
{
2729
short fileTypeMarker = BinaryPrimitives.ReadInt16LittleEndian(header);
28-
return fileTypeMarker == BmpConstants.TypeMarkers.Bitmap || fileTypeMarker == BmpConstants.TypeMarkers.BitmapArray;
30+
return fileTypeMarker == BmpConstants.TypeMarkers.Bitmap ||
31+
fileTypeMarker == BmpConstants.TypeMarkers.BitmapArray;
2932
}
3033

3134
return false;

src/ImageSharp/Formats/Gif/GifImageFormatDetector.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
3+
4+
using System.Diagnostics.CodeAnalysis;
45

56
namespace SixLabors.ImageSharp.Formats.Gif;
67

@@ -13,9 +14,10 @@ public sealed class GifImageFormatDetector : IImageFormatDetector
1314
public int HeaderSize => 6;
1415

1516
/// <inheritdoc/>
16-
public IImageFormat DetectFormat(ReadOnlySpan<byte> header)
17+
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
1718
{
18-
return this.IsSupportedFileFormat(header) ? GifFormat.Instance : null;
19+
format = this.IsSupportedFileFormat(header) ? GifFormat.Instance : null;
20+
return format != null;
1921
}
2022

2123
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)

src/ImageSharp/Formats/IImageFormatDetector.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Diagnostics.CodeAnalysis;
5+
46
namespace SixLabors.ImageSharp.Formats;
57

68
/// <summary>
@@ -18,6 +20,7 @@ public interface IImageFormatDetector
1820
/// Detect mimetype
1921
/// </summary>
2022
/// <param name="header">The <see cref="T:byte[]"/> containing the file header.</param>
21-
/// <returns>returns the mime type of detected otherwise returns null</returns>
22-
IImageFormat DetectFormat(ReadOnlySpan<byte> header);
23+
/// <param name="format">The mime type of detected otherwise returns null</param>
24+
/// <returns>returns true when format was detected otherwise false.</returns>
25+
bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format);
2326
}

src/ImageSharp/Formats/ImageDecoderUtilities.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using SixLabors.ImageSharp.IO;
65
using SixLabors.ImageSharp.Memory;

src/ImageSharp/Formats/ImageFormatManager.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Collections.Concurrent;
5+
using System.Diagnostics.CodeAnalysis;
66

77
namespace SixLabors.ImageSharp.Formats;
88

@@ -92,8 +92,9 @@ public void AddImageFormat(IImageFormat format)
9292
/// For the specified file extensions type find the e <see cref="IImageFormat"/>.
9393
/// </summary>
9494
/// <param name="extension">The extension to discover</param>
95-
/// <returns>The <see cref="IImageFormat"/> if found otherwise null</returns>
96-
public IImageFormat FindFormatByFileExtension(string extension)
95+
/// <param name="format">The <see cref="IImageFormat"/> if found otherwise null</param>
96+
/// <returns>False if no format was found</returns>
97+
public bool TryFindFormatByFileExtension(string extension, [NotNullWhen(true)] out IImageFormat? format)
9798
{
9899
Guard.NotNullOrWhiteSpace(extension, nameof(extension));
99100

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

105-
return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase));
106+
format = this.imageFormats.FirstOrDefault(x =>
107+
x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase));
108+
109+
return format != null;
106110
}
107111

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

116120
/// <summary>
@@ -160,11 +164,11 @@ public void AddImageFormatDetector(IImageFormatDetector detector)
160164
/// </summary>
161165
/// <param name="format">The format to discover</param>
162166
/// <returns>The <see cref="IImageDecoder"/> if found otherwise null</returns>
163-
public IImageDecoder FindDecoder(IImageFormat format)
167+
public IImageDecoder? FindDecoder(IImageFormat format)
164168
{
165169
Guard.NotNull(format, nameof(format));
166170

167-
return this.mimeTypeDecoders.TryGetValue(format, out IImageDecoder decoder)
171+
return this.mimeTypeDecoders.TryGetValue(format, out IImageDecoder? decoder)
168172
? decoder
169173
: null;
170174
}
@@ -174,11 +178,11 @@ public IImageDecoder FindDecoder(IImageFormat format)
174178
/// </summary>
175179
/// <param name="format">The format to discover</param>
176180
/// <returns>The <see cref="IImageEncoder"/> if found otherwise null</returns>
177-
public IImageEncoder FindEncoder(IImageFormat format)
181+
public IImageEncoder? FindEncoder(IImageFormat format)
178182
{
179183
Guard.NotNull(format, nameof(format));
180184

181-
return this.mimeTypeEncoders.TryGetValue(format, out IImageEncoder encoder)
185+
return this.mimeTypeEncoders.TryGetValue(format, out IImageEncoder? encoder)
182186
? encoder
183187
: null;
184188
}

src/ImageSharp/Formats/Jpeg/JpegImageFormatDetector.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
3+
4+
using System.Diagnostics.CodeAnalysis;
45

56
namespace SixLabors.ImageSharp.Formats.Jpeg;
67

@@ -13,8 +14,11 @@ public sealed class JpegImageFormatDetector : IImageFormatDetector
1314
public int HeaderSize => 11;
1415

1516
/// <inheritdoc/>
16-
public IImageFormat DetectFormat(ReadOnlySpan<byte> header)
17-
=> this.IsSupportedFileFormat(header) ? JpegFormat.Instance : null;
17+
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
18+
{
19+
format = this.IsSupportedFileFormat(header) ? JpegFormat.Instance : null;
20+
return format != null;
21+
}
1822

1923
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)
2024
=> header.Length >= this.HeaderSize

src/ImageSharp/Formats/Pbm/PbmImageFormatDetector.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
3+
4+
using System.Diagnostics.CodeAnalysis;
45

56
namespace SixLabors.ImageSharp.Formats.Pbm;
67

@@ -17,7 +18,11 @@ public sealed class PbmImageFormatDetector : IImageFormatDetector
1718
public int HeaderSize => 2;
1819

1920
/// <inheritdoc/>
20-
public IImageFormat DetectFormat(ReadOnlySpan<byte> header) => IsSupportedFileFormat(header) ? PbmFormat.Instance : null;
21+
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
22+
{
23+
format = IsSupportedFileFormat(header) ? PbmFormat.Instance : null;
24+
return format != null;
25+
}
2126

2227
private static bool IsSupportedFileFormat(ReadOnlySpan<byte> header)
2328
{

src/ImageSharp/Formats/Png/PngImageFormatDetector.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers.Binary;
5+
using System.Diagnostics.CodeAnalysis;
66

77
namespace SixLabors.ImageSharp.Formats.Png;
88

@@ -15,9 +15,10 @@ public sealed class PngImageFormatDetector : IImageFormatDetector
1515
public int HeaderSize => 8;
1616

1717
/// <inheritdoc/>
18-
public IImageFormat DetectFormat(ReadOnlySpan<byte> header)
18+
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format)
1919
{
20-
return this.IsSupportedFileFormat(header) ? PngFormat.Instance : null;
20+
format = this.IsSupportedFileFormat(header) ? PngFormat.Instance : null;
21+
return format != null;
2122
}
2223

2324
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header)

0 commit comments

Comments
 (0)