Skip to content

Commit d9192c6

Browse files
Merge pull request #2353 from stefannikolei/sn/nullable/various
Remove nullable disable from various files
2 parents ae0a51c + 1a9db45 commit d9192c6

File tree

11 files changed

+44
-44
lines changed

11 files changed

+44
-44
lines changed

src/ImageSharp/Common/Helpers/Guard.cs

Lines changed: 3 additions & 3 deletions
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 System.Runtime.CompilerServices;
65
using SixLabors.ImageSharp;
@@ -17,13 +16,14 @@ internal static partial class Guard
1716
/// <typeparam name="TValue">The type of the value.</typeparam>
1817
/// <exception cref="ArgumentException"><paramref name="value"/> is not a value type.</exception>
1918
[MethodImpl(InliningOptions.ShortMethod)]
20-
public static void MustBeValueType<TValue>(TValue value, string parameterName)
19+
public static void MustBeValueType<TValue>(TValue value, [CallerArgumentExpression("value")] string? parameterName = null)
20+
where TValue : notnull
2121
{
2222
if (value.GetType().IsValueType)
2323
{
2424
return;
2525
}
2626

27-
ThrowHelper.ThrowArgumentException("Type must be a struct.", parameterName);
27+
ThrowHelper.ThrowArgumentException("Type must be a struct.", parameterName!);
2828
}
2929
}

src/ImageSharp/ImageExtensions.cs

Lines changed: 1 addition & 2 deletions
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 System.Globalization;
65
using System.Text;
@@ -180,6 +179,6 @@ public static string ToBase64String(this Image source, IImageFormat format)
180179

181180
// Always available.
182181
stream.TryGetBuffer(out ArraySegment<byte> buffer);
183-
return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(buffer.Array, 0, (int)stream.Length)}";
182+
return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(buffer.Array ?? Array.Empty<byte>(), 0, (int)stream.Length)}";
184183
}
185184
}

src/ImageSharp/ImageFrame{TPixel}.cs

Lines changed: 16 additions & 19 deletions
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 System.Runtime.CompilerServices;
65
using System.Runtime.InteropServices;
@@ -145,7 +144,7 @@ internal ImageFrame(Configuration configuration, ImageFrame<TPixel> source)
145144
}
146145

147146
/// <inheritdoc/>
148-
public Buffer2D<TPixel> PixelBuffer { get; private set; }
147+
public Buffer2D<TPixel> PixelBuffer { get; }
149148

150149
/// <summary>
151150
/// Gets or sets the pixel at the specified position.
@@ -183,7 +182,7 @@ public void ProcessPixelRows(PixelAccessorAction<TPixel> processPixels)
183182

184183
try
185184
{
186-
var accessor = new PixelAccessor<TPixel>(this.PixelBuffer);
185+
PixelAccessor<TPixel> accessor = new(this.PixelBuffer);
187186
processPixels(accessor);
188187
}
189188
finally
@@ -211,8 +210,8 @@ public void ProcessPixelRows<TPixel2>(
211210

212211
try
213212
{
214-
var accessor1 = new PixelAccessor<TPixel>(this.PixelBuffer);
215-
var accessor2 = new PixelAccessor<TPixel2>(frame2.PixelBuffer);
213+
PixelAccessor<TPixel> accessor1 = new(this.PixelBuffer);
214+
PixelAccessor<TPixel2> accessor2 = new(frame2.PixelBuffer);
216215
processPixels(accessor1, accessor2);
217216
}
218217
finally
@@ -247,9 +246,9 @@ public void ProcessPixelRows<TPixel2, TPixel3>(
247246

248247
try
249248
{
250-
var accessor1 = new PixelAccessor<TPixel>(this.PixelBuffer);
251-
var accessor2 = new PixelAccessor<TPixel2>(frame2.PixelBuffer);
252-
var accessor3 = new PixelAccessor<TPixel3>(frame3.PixelBuffer);
249+
PixelAccessor<TPixel> accessor1 = new(this.PixelBuffer);
250+
PixelAccessor<TPixel2> accessor2 = new(frame2.PixelBuffer);
251+
PixelAccessor<TPixel3> accessor3 = new(frame3.PixelBuffer);
253252
processPixels(accessor1, accessor2, accessor3);
254253
}
255254
finally
@@ -310,6 +309,7 @@ public bool DangerousTryGetSinglePixelMemory(out Memory<TPixel> memory)
310309
/// Copies the pixels to a <see cref="Buffer2D{TPixel}"/> of the same size.
311310
/// </summary>
312311
/// <param name="target">The target pixel buffer accessor.</param>
312+
/// <exception cref="ArgumentException">ImageFrame{TPixel}.CopyTo(): target must be of the same size!</exception>
313313
internal void CopyTo(Buffer2D<TPixel> target)
314314
{
315315
if (this.Size() != target.Size())
@@ -342,8 +342,7 @@ protected override void Dispose(bool disposing)
342342

343343
if (disposing)
344344
{
345-
this.PixelBuffer?.Dispose();
346-
this.PixelBuffer = null;
345+
this.PixelBuffer.Dispose();
347346
}
348347

349348
this.isDisposed = true;
@@ -379,14 +378,14 @@ internal override void CopyPixelsTo<TDestinationPixel>(MemoryGroup<TDestinationP
379378
/// </summary>
380379
/// <param name="configuration">The configuration providing initialization code which allows extending the library.</param>
381380
/// <returns>The <see cref="ImageFrame{TPixel}"/></returns>
382-
internal ImageFrame<TPixel> Clone(Configuration configuration) => new ImageFrame<TPixel>(configuration, this);
381+
internal ImageFrame<TPixel> Clone(Configuration configuration) => new(configuration, this);
383382

384383
/// <summary>
385384
/// Returns a copy of the image frame in the given pixel format.
386385
/// </summary>
387386
/// <typeparam name="TPixel2">The pixel format.</typeparam>
388387
/// <returns>The <see cref="ImageFrame{TPixel2}"/></returns>
389-
internal ImageFrame<TPixel2> CloneAs<TPixel2>()
388+
internal ImageFrame<TPixel2>? CloneAs<TPixel2>()
390389
where TPixel2 : unmanaged, IPixel<TPixel2> => this.CloneAs<TPixel2>(this.GetConfiguration());
391390

392391
/// <summary>
@@ -400,11 +399,11 @@ internal ImageFrame<TPixel2> CloneAs<TPixel2>(Configuration configuration)
400399
{
401400
if (typeof(TPixel2) == typeof(TPixel))
402401
{
403-
return this.Clone(configuration) as ImageFrame<TPixel2>;
402+
return (this.Clone(configuration) as ImageFrame<TPixel2>)!;
404403
}
405404

406-
var target = new ImageFrame<TPixel2>(configuration, this.Width, this.Height, this.Metadata.DeepClone());
407-
var operation = new RowIntervalOperation<TPixel2>(this.PixelBuffer, target.PixelBuffer, configuration);
405+
ImageFrame<TPixel2> target = new(configuration, this.Width, this.Height, this.Metadata.DeepClone());
406+
RowIntervalOperation<TPixel2> operation = new(this.PixelBuffer, target.PixelBuffer, configuration);
408407

409408
ParallelRowIterator.IterateRowIntervals(
410409
configuration,
@@ -447,14 +446,12 @@ private void VerifyCoords(int x, int y)
447446
}
448447

449448
[MethodImpl(InliningOptions.ColdPath)]
450-
private static void ThrowArgumentOutOfRangeException(string paramName)
451-
{
452-
throw new ArgumentOutOfRangeException(paramName);
453-
}
449+
private static void ThrowArgumentOutOfRangeException(string paramName) => throw new ArgumentOutOfRangeException(paramName);
454450

455451
/// <summary>
456452
/// A <see langword="struct"/> implementing the clone logic for <see cref="ImageFrame{TPixel}"/>.
457453
/// </summary>
454+
/// <typeparam name="TPixel2">The type of the target pixel format.</typeparam>
458455
private readonly struct RowIntervalOperation<TPixel2> : IRowIntervalOperation
459456
where TPixel2 : unmanaged, IPixel<TPixel2>
460457
{

src/ImageSharp/IndexedImageFrame{TPixel}.cs

Lines changed: 2 additions & 5 deletions
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 System.Buffers;
65
using System.Runtime.CompilerServices;
@@ -18,8 +17,8 @@ namespace SixLabors.ImageSharp;
1817
public sealed class IndexedImageFrame<TPixel> : IPixelSource, IDisposable
1918
where TPixel : unmanaged, IPixel<TPixel>
2019
{
21-
private Buffer2D<byte> pixelBuffer;
22-
private IMemoryOwner<TPixel> paletteOwner;
20+
private readonly Buffer2D<byte> pixelBuffer;
21+
private readonly IMemoryOwner<TPixel> paletteOwner;
2322
private bool isDisposed;
2423

2524
/// <summary>
@@ -109,8 +108,6 @@ public void Dispose()
109108
this.isDisposed = true;
110109
this.pixelBuffer.Dispose();
111110
this.paletteOwner.Dispose();
112-
this.pixelBuffer = null;
113-
this.paletteOwner = null;
114111
}
115112
}
116113
}

src/ImageSharp/Memory/Buffer2D{T}.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ internal Buffer2D(MemoryGroup<T> memoryGroup, int width, int height)
5555
/// </remarks>
5656
internal MemoryGroup<T> FastMemoryGroup { get; private set; }
5757

58+
internal bool IsDisposed { get; private set; }
59+
5860
/// <summary>
5961
/// Gets a reference to the element at the specified position.
6062
/// </summary>
@@ -79,7 +81,11 @@ internal Buffer2D(MemoryGroup<T> memoryGroup, int width, int height)
7981
/// <summary>
8082
/// Disposes the <see cref="Buffer2D{T}"/> instance
8183
/// </summary>
82-
public void Dispose() => this.FastMemoryGroup.Dispose();
84+
public void Dispose()
85+
{
86+
this.FastMemoryGroup.Dispose();
87+
this.IsDisposed = true;
88+
}
8389

8490
/// <summary>
8591
/// Gets a <see cref="Span{T}"/> to the row 'y' beginning from the pixel at the first pixel on that row.

src/ImageSharp/Processing/Extensions/ProcessingExtensions.cs

Lines changed: 6 additions & 5 deletions
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.Advanced;
65
using SixLabors.ImageSharp.PixelFormats;
@@ -157,9 +156,9 @@ public static Image Clone(this Image source, Configuration configuration, Action
157156
Guard.NotNull(operation, nameof(operation));
158157
source.EnsureNotDisposed();
159158

160-
var visitor = new ProcessingVisitor(configuration, operation, false);
159+
ProcessingVisitor visitor = new(configuration, operation, false);
161160
source.AcceptVisitor(visitor);
162-
return visitor.ResultImage;
161+
return visitor.GetResultImage();
163162
}
164163

165164
/// <summary>
@@ -275,14 +274,16 @@ private class ProcessingVisitor : IImageVisitor
275274

276275
private readonly bool mutate;
277276

277+
private Image? resultImage;
278+
278279
public ProcessingVisitor(Configuration configuration, Action<IImageProcessingContext> operation, bool mutate)
279280
{
280281
this.configuration = configuration;
281282
this.operation = operation;
282283
this.mutate = mutate;
283284
}
284285

285-
public Image ResultImage { get; private set; }
286+
public Image GetResultImage() => this.resultImage!;
286287

287288
public void Visit<TPixel>(Image<TPixel> image)
288289
where TPixel : unmanaged, IPixel<TPixel>
@@ -291,7 +292,7 @@ public void Visit<TPixel>(Image<TPixel> image)
291292
this.configuration.ImageOperationsProvider.CreateImageProcessingContext(this.configuration, image, this.mutate);
292293

293294
this.operation(operationsRunner);
294-
this.ResultImage = operationsRunner.GetResultImage();
295+
this.resultImage = operationsRunner.GetResultImage();
295296
}
296297
}
297298
}

src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class AffineTransformProcessor : CloningImageProcessor
1919
public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler, Size targetDimensions)
2020
{
2121
Guard.NotNull(sampler, nameof(sampler));
22-
Guard.MustBeValueType(sampler, nameof(sampler));
22+
Guard.MustBeValueType(sampler);
2323

2424
if (TransformUtils.IsDegenerate(matrix))
2525
{

src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public sealed class ProjectiveTransformProcessor : CloningImageProcessor
1919
public ProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler, Size targetDimensions)
2020
{
2121
Guard.NotNull(sampler, nameof(sampler));
22-
Guard.MustBeValueType(sampler, nameof(sampler));
22+
Guard.MustBeValueType(sampler);
2323

2424
if (TransformUtils.IsDegenerate(matrix))
2525
{

src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public ResizeProcessor(ResizeOptions options, Size sourceSize)
1717
{
1818
Guard.NotNull(options, nameof(options));
1919
Guard.NotNull(options.Sampler, nameof(options.Sampler));
20-
Guard.MustBeValueType(options.Sampler, nameof(options.Sampler));
20+
Guard.MustBeValueType(options.Sampler);
2121

2222
(Size size, Rectangle rectangle) = ResizeHelper.CalculateTargetLocationAndBounds(sourceSize, options);
2323

src/ImageSharp/Processing/ProjectiveTransformBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Processing;
1111
/// </summary>
1212
public class ProjectiveTransformBuilder
1313
{
14-
private readonly List<Func<Size, Matrix4x4>> matrixFactories = new List<Func<Size, Matrix4x4>>();
14+
private readonly List<Func<Size, Matrix4x4>> matrixFactories = new();
1515

1616
/// <summary>
1717
/// Prepends a matrix that performs a tapering projective transform.
@@ -313,7 +313,7 @@ public Matrix4x4 BuildMatrix(Rectangle sourceRectangle)
313313
Guard.MustBeGreaterThan(sourceRectangle.Height, 0, nameof(sourceRectangle));
314314

315315
// Translate the origin matrix to cater for source rectangle offsets.
316-
var matrix = Matrix4x4.CreateTranslation(new Vector3(-sourceRectangle.Location, 0));
316+
Matrix4x4 matrix = Matrix4x4.CreateTranslation(new Vector3(-sourceRectangle.Location, 0));
317317

318318
Size size = sourceRectangle.Size;
319319

0 commit comments

Comments
 (0)