Browse Source

Merge pull request #2353 from stefannikolei/sn/nullable/various

Remove nullable disable from various files
pull/2367/head
James Jackson-South 3 years ago
committed by GitHub
parent
commit
d9192c6a6e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      src/ImageSharp/Common/Helpers/Guard.cs
  2. 3
      src/ImageSharp/ImageExtensions.cs
  3. 35
      src/ImageSharp/ImageFrame{TPixel}.cs
  4. 7
      src/ImageSharp/IndexedImageFrame{TPixel}.cs
  5. 8
      src/ImageSharp/Memory/Buffer2D{T}.cs
  6. 11
      src/ImageSharp/Processing/Extensions/ProcessingExtensions.cs
  7. 2
      src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor.cs
  8. 2
      src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor.cs
  9. 2
      src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor.cs
  10. 4
      src/ImageSharp/Processing/ProjectiveTransformBuilder.cs
  11. 8
      tests/ImageSharp.Tests/Image/ImageFrameCollectionTests.Generic.cs

6
src/ImageSharp/Common/Helpers/Guard.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp;
@ -17,13 +16,14 @@ internal static partial class Guard
/// <typeparam name="TValue">The type of the value.</typeparam>
/// <exception cref="ArgumentException"><paramref name="value"/> is not a value type.</exception>
[MethodImpl(InliningOptions.ShortMethod)]
public static void MustBeValueType<TValue>(TValue value, string parameterName)
public static void MustBeValueType<TValue>(TValue value, [CallerArgumentExpression("value")] string? parameterName = null)
where TValue : notnull
{
if (value.GetType().IsValueType)
{
return;
}
ThrowHelper.ThrowArgumentException("Type must be a struct.", parameterName);
ThrowHelper.ThrowArgumentException("Type must be a struct.", parameterName!);
}
}

3
src/ImageSharp/ImageExtensions.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable
using System.Globalization;
using System.Text;
@ -180,6 +179,6 @@ public static partial class ImageExtensions
// Always available.
stream.TryGetBuffer(out ArraySegment<byte> buffer);
return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(buffer.Array, 0, (int)stream.Length)}";
return $"data:{format.DefaultMimeType};base64,{Convert.ToBase64String(buffer.Array ?? Array.Empty<byte>(), 0, (int)stream.Length)}";
}
}

35
src/ImageSharp/ImageFrame{TPixel}.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@ -145,7 +144,7 @@ public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
}
/// <inheritdoc/>
public Buffer2D<TPixel> PixelBuffer { get; private set; }
public Buffer2D<TPixel> PixelBuffer { get; }
/// <summary>
/// Gets or sets the pixel at the specified position.
@ -183,7 +182,7 @@ public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
try
{
var accessor = new PixelAccessor<TPixel>(this.PixelBuffer);
PixelAccessor<TPixel> accessor = new(this.PixelBuffer);
processPixels(accessor);
}
finally
@ -211,8 +210,8 @@ public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
try
{
var accessor1 = new PixelAccessor<TPixel>(this.PixelBuffer);
var accessor2 = new PixelAccessor<TPixel2>(frame2.PixelBuffer);
PixelAccessor<TPixel> accessor1 = new(this.PixelBuffer);
PixelAccessor<TPixel2> accessor2 = new(frame2.PixelBuffer);
processPixels(accessor1, accessor2);
}
finally
@ -247,9 +246,9 @@ public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
try
{
var accessor1 = new PixelAccessor<TPixel>(this.PixelBuffer);
var accessor2 = new PixelAccessor<TPixel2>(frame2.PixelBuffer);
var accessor3 = new PixelAccessor<TPixel3>(frame3.PixelBuffer);
PixelAccessor<TPixel> accessor1 = new(this.PixelBuffer);
PixelAccessor<TPixel2> accessor2 = new(frame2.PixelBuffer);
PixelAccessor<TPixel3> accessor3 = new(frame3.PixelBuffer);
processPixels(accessor1, accessor2, accessor3);
}
finally
@ -310,6 +309,7 @@ public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
/// Copies the pixels to a <see cref="Buffer2D{TPixel}"/> of the same size.
/// </summary>
/// <param name="target">The target pixel buffer accessor.</param>
/// <exception cref="ArgumentException">ImageFrame{TPixel}.CopyTo(): target must be of the same size!</exception>
internal void CopyTo(Buffer2D<TPixel> target)
{
if (this.Size() != target.Size())
@ -342,8 +342,7 @@ public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
if (disposing)
{
this.PixelBuffer?.Dispose();
this.PixelBuffer = null;
this.PixelBuffer.Dispose();
}
this.isDisposed = true;
@ -379,14 +378,14 @@ public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
/// </summary>
/// <param name="configuration">The configuration providing initialization code which allows extending the library.</param>
/// <returns>The <see cref="ImageFrame{TPixel}"/></returns>
internal ImageFrame<TPixel> Clone(Configuration configuration) => new ImageFrame<TPixel>(configuration, this);
internal ImageFrame<TPixel> Clone(Configuration configuration) => new(configuration, this);
/// <summary>
/// Returns a copy of the image frame in the given pixel format.
/// </summary>
/// <typeparam name="TPixel2">The pixel format.</typeparam>
/// <returns>The <see cref="ImageFrame{TPixel2}"/></returns>
internal ImageFrame<TPixel2> CloneAs<TPixel2>()
internal ImageFrame<TPixel2>? CloneAs<TPixel2>()
where TPixel2 : unmanaged, IPixel<TPixel2> => this.CloneAs<TPixel2>(this.GetConfiguration());
/// <summary>
@ -400,11 +399,11 @@ public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
{
if (typeof(TPixel2) == typeof(TPixel))
{
return this.Clone(configuration) as ImageFrame<TPixel2>;
return (this.Clone(configuration) as ImageFrame<TPixel2>)!;
}
var target = new ImageFrame<TPixel2>(configuration, this.Width, this.Height, this.Metadata.DeepClone());
var operation = new RowIntervalOperation<TPixel2>(this.PixelBuffer, target.PixelBuffer, configuration);
ImageFrame<TPixel2> target = new(configuration, this.Width, this.Height, this.Metadata.DeepClone());
RowIntervalOperation<TPixel2> operation = new(this.PixelBuffer, target.PixelBuffer, configuration);
ParallelRowIterator.IterateRowIntervals(
configuration,
@ -447,14 +446,12 @@ public sealed class ImageFrame<TPixel> : ImageFrame, IPixelSource<TPixel>
}
[MethodImpl(InliningOptions.ColdPath)]
private static void ThrowArgumentOutOfRangeException(string paramName)
{
throw new ArgumentOutOfRangeException(paramName);
}
private static void ThrowArgumentOutOfRangeException(string paramName) => throw new ArgumentOutOfRangeException(paramName);
/// <summary>
/// A <see langword="struct"/> implementing the clone logic for <see cref="ImageFrame{TPixel}"/>.
/// </summary>
/// <typeparam name="TPixel2">The type of the target pixel format.</typeparam>
private readonly struct RowIntervalOperation<TPixel2> : IRowIntervalOperation
where TPixel2 : unmanaged, IPixel<TPixel2>
{

7
src/ImageSharp/IndexedImageFrame{TPixel}.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable
using System.Buffers;
using System.Runtime.CompilerServices;
@ -18,8 +17,8 @@ namespace SixLabors.ImageSharp;
public sealed class IndexedImageFrame<TPixel> : IPixelSource, IDisposable
where TPixel : unmanaged, IPixel<TPixel>
{
private Buffer2D<byte> pixelBuffer;
private IMemoryOwner<TPixel> paletteOwner;
private readonly Buffer2D<byte> pixelBuffer;
private readonly IMemoryOwner<TPixel> paletteOwner;
private bool isDisposed;
/// <summary>
@ -109,8 +108,6 @@ public sealed class IndexedImageFrame<TPixel> : IPixelSource, IDisposable
this.isDisposed = true;
this.pixelBuffer.Dispose();
this.paletteOwner.Dispose();
this.pixelBuffer = null;
this.paletteOwner = null;
}
}
}

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

@ -55,6 +55,8 @@ public sealed class Buffer2D<T> : IDisposable
/// </remarks>
internal MemoryGroup<T> FastMemoryGroup { get; private set; }
internal bool IsDisposed { get; private set; }
/// <summary>
/// Gets a reference to the element at the specified position.
/// </summary>
@ -79,7 +81,11 @@ public sealed class Buffer2D<T> : IDisposable
/// <summary>
/// Disposes the <see cref="Buffer2D{T}"/> instance
/// </summary>
public void Dispose() => this.FastMemoryGroup.Dispose();
public void Dispose()
{
this.FastMemoryGroup.Dispose();
this.IsDisposed = true;
}
/// <summary>
/// Gets a <see cref="Span{T}"/> to the row 'y' beginning from the pixel at the first pixel on that row.

11
src/ImageSharp/Processing/Extensions/ProcessingExtensions.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
#nullable disable
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
@ -157,9 +156,9 @@ public static partial class ProcessingExtensions
Guard.NotNull(operation, nameof(operation));
source.EnsureNotDisposed();
var visitor = new ProcessingVisitor(configuration, operation, false);
ProcessingVisitor visitor = new(configuration, operation, false);
source.AcceptVisitor(visitor);
return visitor.ResultImage;
return visitor.GetResultImage();
}
/// <summary>
@ -275,6 +274,8 @@ public static partial class ProcessingExtensions
private readonly bool mutate;
private Image? resultImage;
public ProcessingVisitor(Configuration configuration, Action<IImageProcessingContext> operation, bool mutate)
{
this.configuration = configuration;
@ -282,7 +283,7 @@ public static partial class ProcessingExtensions
this.mutate = mutate;
}
public Image ResultImage { get; private set; }
public Image GetResultImage() => this.resultImage!;
public void Visit<TPixel>(Image<TPixel> image)
where TPixel : unmanaged, IPixel<TPixel>
@ -291,7 +292,7 @@ public static partial class ProcessingExtensions
this.configuration.ImageOperationsProvider.CreateImageProcessingContext(this.configuration, image, this.mutate);
this.operation(operationsRunner);
this.ResultImage = operationsRunner.GetResultImage();
this.resultImage = operationsRunner.GetResultImage();
}
}
}

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

@ -19,7 +19,7 @@ public class AffineTransformProcessor : CloningImageProcessor
public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler, Size targetDimensions)
{
Guard.NotNull(sampler, nameof(sampler));
Guard.MustBeValueType(sampler, nameof(sampler));
Guard.MustBeValueType(sampler);
if (TransformUtils.IsDegenerate(matrix))
{

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

@ -19,7 +19,7 @@ public sealed class ProjectiveTransformProcessor : CloningImageProcessor
public ProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler, Size targetDimensions)
{
Guard.NotNull(sampler, nameof(sampler));
Guard.MustBeValueType(sampler, nameof(sampler));
Guard.MustBeValueType(sampler);
if (TransformUtils.IsDegenerate(matrix))
{

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

@ -17,7 +17,7 @@ public class ResizeProcessor : CloningImageProcessor
{
Guard.NotNull(options, nameof(options));
Guard.NotNull(options.Sampler, nameof(options.Sampler));
Guard.MustBeValueType(options.Sampler, nameof(options.Sampler));
Guard.MustBeValueType(options.Sampler);
(Size size, Rectangle rectangle) = ResizeHelper.CalculateTargetLocationAndBounds(sourceSize, options);

4
src/ImageSharp/Processing/ProjectiveTransformBuilder.cs

@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Processing;
/// </summary>
public class ProjectiveTransformBuilder
{
private readonly List<Func<Size, Matrix4x4>> matrixFactories = new List<Func<Size, Matrix4x4>>();
private readonly List<Func<Size, Matrix4x4>> matrixFactories = new();
/// <summary>
/// Prepends a matrix that performs a tapering projective transform.
@ -313,7 +313,7 @@ public class ProjectiveTransformBuilder
Guard.MustBeGreaterThan(sourceRectangle.Height, 0, nameof(sourceRectangle));
// Translate the origin matrix to cater for source rectangle offsets.
var matrix = Matrix4x4.CreateTranslation(new Vector3(-sourceRectangle.Location, 0));
Matrix4x4 matrix = Matrix4x4.CreateTranslation(new Vector3(-sourceRectangle.Location, 0));
Size size = sourceRectangle.Size;

8
tests/ImageSharp.Tests/Image/ImageFrameCollectionTests.Generic.cs

@ -182,12 +182,12 @@ public abstract partial class ImageFrameCollectionTests
new[] { imageFrame1, imageFrame2 });
IPixelSource<Rgba32>[] framesSnapShot = collection.OfType<IPixelSource<Rgba32>>().ToArray();
Assert.All(framesSnapShot, f => Assert.False(f.PixelBuffer.IsDisposed));
collection.Dispose();
Assert.All(
framesSnapShot,
f => // The pixel source of the frame is null after its been disposed.
Assert.Null(f.PixelBuffer));
Assert.All(framesSnapShot, f => Assert.True(f.PixelBuffer.IsDisposed));
}
[Theory]

Loading…
Cancel
Save