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

7
src/ImageSharp/IndexedImageFrame{TPixel}.cs

@ -1,6 +1,5 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using System.Buffers; using System.Buffers;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -18,8 +17,8 @@ namespace SixLabors.ImageSharp;
public sealed class IndexedImageFrame<TPixel> : IPixelSource, IDisposable public sealed class IndexedImageFrame<TPixel> : IPixelSource, IDisposable
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
private Buffer2D<byte> pixelBuffer; private readonly Buffer2D<byte> pixelBuffer;
private IMemoryOwner<TPixel> paletteOwner; private readonly IMemoryOwner<TPixel> paletteOwner;
private bool isDisposed; private bool isDisposed;
/// <summary> /// <summary>
@ -109,8 +108,6 @@ public sealed class IndexedImageFrame<TPixel> : IPixelSource, IDisposable
this.isDisposed = true; this.isDisposed = true;
this.pixelBuffer.Dispose(); this.pixelBuffer.Dispose();
this.paletteOwner.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> /// </remarks>
internal MemoryGroup<T> FastMemoryGroup { get; private set; } internal MemoryGroup<T> FastMemoryGroup { get; private set; }
internal bool IsDisposed { get; private set; }
/// <summary> /// <summary>
/// Gets a reference to the element at the specified position. /// Gets a reference to the element at the specified position.
/// </summary> /// </summary>
@ -79,7 +81,11 @@ public sealed class Buffer2D<T> : IDisposable
/// <summary> /// <summary>
/// Disposes the <see cref="Buffer2D{T}"/> instance /// Disposes the <see cref="Buffer2D{T}"/> instance
/// </summary> /// </summary>
public void Dispose() => this.FastMemoryGroup.Dispose(); public void Dispose()
{
this.FastMemoryGroup.Dispose();
this.IsDisposed = true;
}
/// <summary> /// <summary>
/// Gets a <see cref="Span{T}"/> to the row 'y' beginning from the pixel at the first pixel on that row. /// 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. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
#nullable disable
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -157,9 +156,9 @@ public static partial class ProcessingExtensions
Guard.NotNull(operation, nameof(operation)); Guard.NotNull(operation, nameof(operation));
source.EnsureNotDisposed(); source.EnsureNotDisposed();
var visitor = new ProcessingVisitor(configuration, operation, false); ProcessingVisitor visitor = new(configuration, operation, false);
source.AcceptVisitor(visitor); source.AcceptVisitor(visitor);
return visitor.ResultImage; return visitor.GetResultImage();
} }
/// <summary> /// <summary>
@ -275,6 +274,8 @@ public static partial class ProcessingExtensions
private readonly bool mutate; private readonly bool mutate;
private Image? resultImage;
public ProcessingVisitor(Configuration configuration, Action<IImageProcessingContext> operation, bool mutate) public ProcessingVisitor(Configuration configuration, Action<IImageProcessingContext> operation, bool mutate)
{ {
this.configuration = configuration; this.configuration = configuration;
@ -282,7 +283,7 @@ public static partial class ProcessingExtensions
this.mutate = mutate; this.mutate = mutate;
} }
public Image ResultImage { get; private set; } public Image GetResultImage() => this.resultImage!;
public void Visit<TPixel>(Image<TPixel> image) public void Visit<TPixel>(Image<TPixel> image)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
@ -291,7 +292,7 @@ public static partial class ProcessingExtensions
this.configuration.ImageOperationsProvider.CreateImageProcessingContext(this.configuration, image, this.mutate); this.configuration.ImageOperationsProvider.CreateImageProcessingContext(this.configuration, image, this.mutate);
this.operation(operationsRunner); 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) public AffineTransformProcessor(Matrix3x2 matrix, IResampler sampler, Size targetDimensions)
{ {
Guard.NotNull(sampler, nameof(sampler)); Guard.NotNull(sampler, nameof(sampler));
Guard.MustBeValueType(sampler, nameof(sampler)); Guard.MustBeValueType(sampler);
if (TransformUtils.IsDegenerate(matrix)) 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) public ProjectiveTransformProcessor(Matrix4x4 matrix, IResampler sampler, Size targetDimensions)
{ {
Guard.NotNull(sampler, nameof(sampler)); Guard.NotNull(sampler, nameof(sampler));
Guard.MustBeValueType(sampler, nameof(sampler)); Guard.MustBeValueType(sampler);
if (TransformUtils.IsDegenerate(matrix)) 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, nameof(options));
Guard.NotNull(options.Sampler, nameof(options.Sampler)); 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); (Size size, Rectangle rectangle) = ResizeHelper.CalculateTargetLocationAndBounds(sourceSize, options);

4
src/ImageSharp/Processing/ProjectiveTransformBuilder.cs

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

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

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

Loading…
Cancel
Save