diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index 9cc045d45..eaa685293 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -171,7 +171,10 @@ internal sealed class GifEncoderCore : IImageEncoderInternals quantized = null; } - paletteQuantizer.Dispose(); + if (hasPaletteQuantizer) + { + paletteQuantizer.Dispose(); + } } private void EncodeFrame( diff --git a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs index 208455b90..4d95e060d 100644 --- a/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs +++ b/src/ImageSharp/Processing/DefaultImageProcessorContext{TPixel}.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Collections.Concurrent; using SixLabors.ImageSharp.PixelFormats; @@ -17,7 +16,7 @@ internal class DefaultImageProcessorContext : IInternalImageProcessingCo { private readonly bool mutate; private readonly Image source; - private Image destination; + private Image? destination; /// /// Initializes a new instance of the class. @@ -54,7 +53,7 @@ internal class DefaultImageProcessorContext : IInternalImageProcessingCo this.destination = this.source.Clone(); } - return this.destination; + return this.destination!; } /// @@ -87,7 +86,7 @@ internal class DefaultImageProcessorContext : IInternalImageProcessingCo } // Standard processing pipeline. - using (IImageProcessor specificProcessor = processor.CreatePixelSpecificProcessor(this.Configuration, this.destination, rectangle)) + using (IImageProcessor specificProcessor = processor.CreatePixelSpecificProcessor(this.Configuration, this.destination!, rectangle)) { specificProcessor.Execute(); } diff --git a/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs b/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs index 4cae2f858..8128d0119 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.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; @@ -16,8 +15,8 @@ internal sealed class KernelSamplingMap : IDisposable { private readonly MemoryAllocator allocator; private bool isDisposed; - private IMemoryOwner yOffsets; - private IMemoryOwner xOffsets; + private IMemoryOwner? yOffsets; + private IMemoryOwner? xOffsets; /// /// Initializes a new instance of the class. @@ -65,10 +64,10 @@ internal sealed class KernelSamplingMap : IDisposable } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Span GetRowOffsetSpan() => this.yOffsets.GetSpan(); + public Span GetRowOffsetSpan() => this.yOffsets!.GetSpan(); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Span GetColumnOffsetSpan() => this.xOffsets.GetSpan(); + public Span GetColumnOffsetSpan() => this.xOffsets!.GetSpan(); /// public void Dispose() diff --git a/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.cs b/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.cs index 9de4f862b..88b59b7dc 100644 --- a/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor.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; @@ -65,7 +64,7 @@ public class DrawImageProcessor : IImageProcessor { ProcessorFactoryVisitor visitor = new(configuration, this, source, sourceRectangle); this.Image.AcceptVisitor(visitor); - return visitor.Result; + return visitor.Result!; } private class ProcessorFactoryVisitor : IImageVisitor @@ -84,7 +83,7 @@ public class DrawImageProcessor : IImageProcessor this.sourceRectangle = sourceRectangle; } - public IImageProcessor Result { get; private set; } + public IImageProcessor? Result { get; private set; } public void Visit(Image image) where TPixelFg : unmanaged, IPixel diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs index 43d11777d..d66a38d80 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Diagnostics.CodeAnalysis; @@ -29,7 +28,7 @@ public struct OctreeQuantizer : IQuantizer private readonly Octree octree; private IMemoryOwner paletteOwner; private ReadOnlyMemory palette; - private EuclideanPixelMap pixelMap; + private EuclideanPixelMap? pixelMap; private readonly bool isDithering; private bool isDisposed; @@ -143,7 +142,7 @@ public struct OctreeQuantizer : IQuantizer // pixel and a black one. if (this.isDithering || color.Equals(default)) { - return (byte)this.pixelMap.GetClosestColor(color, out match); + return (byte)this.pixelMap!.GetClosestColor(color, out match); } ref TPixel paletteRef = ref MemoryMarshal.GetReference(this.palette.Span); @@ -158,8 +157,7 @@ public struct OctreeQuantizer : IQuantizer if (!this.isDisposed) { this.isDisposed = true; - this.paletteOwner?.Dispose(); - this.paletteOwner = null; + this.paletteOwner.Dispose(); this.pixelMap?.Dispose(); this.pixelMap = null; } @@ -183,7 +181,7 @@ public struct OctreeQuantizer : IQuantizer /// /// Store the last node quantized /// - private OctreeNode previousNode; + private OctreeNode? previousNode; /// /// Cache the previous color quantized @@ -221,7 +219,7 @@ public struct OctreeQuantizer : IQuantizer /// /// Gets the array of reducible nodes /// - private OctreeNode[] ReducibleNodes + private OctreeNode?[] ReducibleNodes { [MethodImpl(InliningOptions.ShortMethod)] get; @@ -311,7 +309,7 @@ public struct OctreeQuantizer : IQuantizer } // Reduce the node most recently added to the list at level 'index' - OctreeNode node = this.ReducibleNodes[index]; + OctreeNode node = this.ReducibleNodes[index]!; this.ReducibleNodes[index] = node.NextReducible; // Decrement the leaf count after reducing the node @@ -330,7 +328,7 @@ public struct OctreeQuantizer : IQuantizer /// /// Pointers to any child nodes /// - private readonly OctreeNode[] children; + private readonly OctreeNode?[]? children; /// /// Flag indicating that this is a leaf node @@ -395,7 +393,7 @@ public struct OctreeQuantizer : IQuantizer /// /// Gets the next reducible node /// - public OctreeNode NextReducible + public OctreeNode? NextReducible { [MethodImpl(InliningOptions.ShortMethod)] get; @@ -423,7 +421,7 @@ public struct OctreeQuantizer : IQuantizer // Go to the next level down in the tree int index = GetColorIndex(ref color, level); - OctreeNode child = this.children[index]; + OctreeNode? child = this.children![index]; if (child is null) { // Create a new child node and store it in the array @@ -448,7 +446,7 @@ public struct OctreeQuantizer : IQuantizer // Loop through all children and add their information to this node for (int index = 0; index < 8; index++) { - OctreeNode child = this.children[index]; + OctreeNode? child = this.children![index]; if (child != null) { this.red += child.red; @@ -495,7 +493,7 @@ public struct OctreeQuantizer : IQuantizer // Loop through children looking for leaves for (int i = 0; i < 8; i++) { - this.children[i]?.ConstructPalette(palette, ref index); + this.children![i]?.ConstructPalette(palette, ref index); } } } @@ -517,7 +515,7 @@ public struct OctreeQuantizer : IQuantizer } int colorIndex = GetColorIndex(ref pixel, level); - OctreeNode child = this.children[colorIndex]; + OctreeNode? child = this.children![colorIndex]; int index = 0; if (child != null) diff --git a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs index 7b351a61f..ea7413aab 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/PaletteQuantizer{TPixel}.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; @@ -68,7 +67,6 @@ internal struct PaletteQuantizer : IQuantizer /// public void Dispose() { - this.pixelMap?.Dispose(); - this.pixelMap = null; + this.pixelMap.Dispose(); } } diff --git a/src/ImageSharp/Processing/Processors/Quantization/QuantizerOptions.cs b/src/ImageSharp/Processing/Processors/Quantization/QuantizerOptions.cs index f52cfd6ea..b3d03d933 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/QuantizerOptions.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/QuantizerOptions.cs @@ -17,7 +17,7 @@ public class QuantizerOptions /// Gets or sets the algorithm to apply to the output image. /// Defaults to ; set to for no dithering. /// - public IDither Dither { get; set; } = QuantizerConstants.DefaultDither; + public IDither? Dither { get; set; } = QuantizerConstants.DefaultDither; /// /// Gets or sets the dithering scale used to adjust the amount of dither. Range 0..1. diff --git a/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs b/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs index 167cf9128..53203f94a 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/QuantizerUtilities.cs @@ -146,7 +146,7 @@ public static class QuantizerUtilities where TFrameQuantizer : struct, IQuantizer where TPixel : unmanaged, IPixel { - IDither dither = quantizer.Options.Dither; + IDither? dither = quantizer.Options.Dither; Buffer2D sourceBuffer = source.PixelBuffer; if (dither is null) diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs index e0aede058..0119558bf 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Buffers; using System.Diagnostics.CodeAnalysis; @@ -76,7 +75,7 @@ internal struct WuQuantizer : IQuantizer private ReadOnlyMemory palette; private int maxColors; private readonly Box[] colorCube; - private EuclideanPixelMap pixelMap; + private EuclideanPixelMap? pixelMap; private readonly bool isDithering; private bool isDisposed; @@ -175,7 +174,7 @@ internal struct WuQuantizer : IQuantizer { if (this.isDithering) { - return (byte)this.pixelMap.GetClosestColor(color, out match); + return (byte)this.pixelMap!.GetClosestColor(color, out match); } Rgba32 rgba = default; @@ -203,9 +202,6 @@ internal struct WuQuantizer : IQuantizer this.momentsOwner?.Dispose(); this.tagsOwner?.Dispose(); this.paletteOwner?.Dispose(); - this.momentsOwner = null; - this.tagsOwner = null; - this.paletteOwner = null; this.pixelMap?.Dispose(); this.pixelMap = null; } @@ -869,7 +865,7 @@ internal struct WuQuantizer : IQuantizer public int Volume; /// - public override readonly bool Equals(object obj) + public override readonly bool Equals(object? obj) => obj is Box box && this.Equals(box); diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs index b9ff55169..c5c2a778e 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/AffineTransformProcessor{TPixel}.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Numerics; using System.Runtime.CompilerServices; @@ -20,8 +19,8 @@ internal class AffineTransformProcessor : TransformProcessor, IR private readonly Size destinationSize; private readonly Matrix3x2 transformMatrix; private readonly IResampler resampler; - private ImageFrame source; - private ImageFrame destination; + private ImageFrame? source; + private ImageFrame? destination; /// /// Initializes a new instance of the class. @@ -53,8 +52,8 @@ internal class AffineTransformProcessor : TransformProcessor, IR where TResampler : struct, IResampler { Configuration configuration = this.Configuration; - ImageFrame source = this.source; - ImageFrame destination = this.destination; + ImageFrame source = this.source!; + ImageFrame destination = this.destination!; Matrix3x2 matrix = this.transformMatrix; // Handle transforms that result in output identical to the original. diff --git a/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs index 7d1a10926..b741dc4ee 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Linear/ProjectiveTransformProcessor{TPixel}.cs @@ -1,6 +1,5 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -#nullable disable using System.Numerics; using System.Runtime.CompilerServices; @@ -20,8 +19,8 @@ internal class ProjectiveTransformProcessor : TransformProcessor private readonly Size destinationSize; private readonly IResampler resampler; private readonly Matrix4x4 transformMatrix; - private ImageFrame source; - private ImageFrame destination; + private ImageFrame? source; + private ImageFrame? destination; /// /// Initializes a new instance of the class. @@ -53,8 +52,8 @@ internal class ProjectiveTransformProcessor : TransformProcessor where TResampler : struct, IResampler { Configuration configuration = this.Configuration; - ImageFrame source = this.source; - ImageFrame destination = this.destination; + ImageFrame source = this.source!; + ImageFrame destination = this.destination!; Matrix4x4 matrix = this.transformMatrix; // Handle transforms that result in output identical to the original. diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs index ba96e76ae..98c2523fa 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeProcessor{TPixel}.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.Advanced; @@ -21,7 +20,7 @@ internal class ResizeProcessor : TransformProcessor, IResampling private readonly int destinationHeight; private readonly IResampler resampler; private readonly Rectangle destinationRectangle; - private Image destination; + private Image? destination; public ResizeProcessor(Configuration configuration, ResizeProcessor definition, Image source, Rectangle sourceRectangle) : base(configuration, source, sourceRectangle) @@ -56,7 +55,7 @@ internal class ResizeProcessor : TransformProcessor, IResampling { Configuration configuration = this.Configuration; Image source = this.Source; - Image destination = this.destination; + Image destination = this.destination!; Rectangle sourceRectangle = this.SourceRectangle; Rectangle destinationRectangle = this.destinationRectangle; bool compand = this.options.Compand;