From 6fc84a82f6a41ff22545d81f43330a04a678b6d1 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 24 Jul 2022 01:05:31 +1000 Subject: [PATCH] Remove other obsolete conditions and warnings --- shared-infrastructure | 2 +- .../ColorSpaces/Companding/SRgbCompanding.cs | 4 +- .../VonKriesChromaticAdaptation.cs | 2 +- src/ImageSharp/Common/Helpers/HexConverter.cs | 2 +- .../Compression/Zlib/DeflaterOutputStream.cs | 4 +- .../Compression/Zlib/DeflaterPendingBuffer.cs | 2 +- .../Diagnostics/MemoryDiagnostics.cs | 6 - src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 12 +- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 4 +- src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs | 16 +- src/ImageSharp/Formats/Gif/GifEncoderCore.cs | 2 +- .../Gif/Sections/GifImageDescriptor.cs | 2 +- .../GifNetscapeLoopingApplicationExtension.cs | 2 +- .../Sections/GifXmpApplicationExtension.cs | 4 +- src/ImageSharp/Formats/ImageFormatManager.cs | 2 +- .../Components/Decoder/ProfileResolver.cs | 2 +- .../Decoder/SpectralConverter{TPixel}.cs | 2 +- .../Formats/Jpeg/JpegDecoderCore.cs | 16 +- src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs | 6 +- src/ImageSharp/Formats/Pbm/PlainEncoder.cs | 16 +- .../Formats/Png/Chunks/PhysicalChunkData.cs | 4 +- .../Formats/Png/Filters/NoneFilter.cs | 4 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 167 ++++++++---------- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 16 +- src/ImageSharp/Formats/Png/PngHeader.cs | 4 +- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 4 +- .../Compressors/PackBitsCompressor.cs | 2 +- .../Compression/Compressors/PackBitsWriter.cs | 2 +- .../Compressors/TiffCcittCompressor.cs | 2 +- .../Decompressors/CcittReferenceScanline.cs | 6 +- .../Decompressors/JpegTiffCompression.cs | 4 +- .../Decompressors/PackBitsTiffCompression.cs | 2 +- .../Decompressors/T6TiffCompression.cs | 18 +- .../Decompressors/WebpTiffCompression.cs | 2 +- .../Tiff/Compression/HorizontalPredictor.cs | 4 +- .../BlackIsZero24TiffColor{TPixel}.cs | 2 +- .../Rgb242424TiffColor{TPixel}.cs | 2 +- .../Rgb24PlanarTiffColor{TPixel}.cs | 2 +- .../Rgba24242424TiffColor{TPixel}.cs | 2 +- .../Rgba24PlanarTiffColor{TPixel}.cs | 2 +- .../WhiteIsZero24TiffColor{TPixel}.cs | 2 +- .../Tiff/Writers/TiffBiColorWriter{TPixel}.cs | 4 +- .../TiffCompositeColorWriter{TPixel}.cs | 2 +- .../Tiff/Writers/TiffPaletteWriter{TPixel}.cs | 6 +- .../Formats/Webp/BitReader/BitReaderBase.cs | 2 +- .../Formats/Webp/BitWriter/BitWriterBase.cs | 4 +- .../Formats/Webp/BitWriter/Vp8BitWriter.cs | 4 +- .../Webp/Lossless/BackwardReferenceEncoder.cs | 6 +- .../Formats/Webp/Lossless/HuffmanUtils.cs | 10 +- .../Formats/Webp/Lossless/NearLosslessEnc.cs | 2 +- .../Formats/Webp/Lossless/PredictorEncoder.cs | 30 ++-- .../Formats/Webp/Lossless/Vp8LEncoder.cs | 50 +++--- .../Formats/Webp/Lossless/Vp8LHashChain.cs | 14 +- .../Webp/Lossless/WebpLosslessDecoder.cs | 26 +-- src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs | 2 +- .../Formats/Webp/Lossy/Vp8EncIterator.cs | 28 +-- .../Formats/Webp/Lossy/Vp8Encoder.cs | 2 +- .../Formats/Webp/Lossy/WebpLossyDecoder.cs | 72 ++++---- .../Formats/Webp/WebpAnimationDecoder.cs | 4 +- .../Formats/Webp/WebpImageFormatDetector.cs | 2 +- src/ImageSharp/IO/ChunkedMemoryStream.cs | 4 +- src/ImageSharp/Image.LoadPixelData.cs | 2 +- src/ImageSharp/ImageFrame.LoadPixelData.cs | 2 +- src/ImageSharp/IndexedImageFrame{TPixel}.cs | 2 +- .../Allocators/Internals/Gen2GcCallback.cs | 24 ++- .../Internals/UniformUnmanagedMemoryPool.cs | 10 +- ...iformUnmanagedMemoryPoolMemoryAllocator.cs | 10 +- src/ImageSharp/Memory/Buffer2D{T}.cs | 2 +- src/ImageSharp/Memory/ByteMemoryManager{T}.cs | 2 +- .../MemoryGroupExtensions.cs | 6 +- .../DiscontiguousBuffers/MemoryGroup{T}.cs | 2 +- .../Memory/MemoryOwnerExtensions.cs | 2 +- .../Profiles/Exif/ExifEncodedStringHelpers.cs | 21 +-- .../Metadata/Profiles/Exif/ExifReader.cs | 51 ++---- .../Metadata/Profiles/Exif/ExifWriter.cs | 8 +- .../DataReader/IccDataReader.Primitives.cs | 2 +- .../DataWriter/IccDataWriter.Primitives.cs | 2 +- .../Metadata/Profiles/IPTC/IptcValue.cs | 2 +- .../PixelFormats/PixelBlender{TPixel}.cs | 12 +- .../PixelImplementations/Rgb24.cs | 14 +- .../PixelImplementations/Rgba32.cs | 2 +- .../PixelFormats/PixelOperations{TPixel}.cs | 6 +- .../Utils/Vector4Converters.RgbaCompatible.cs | 4 +- .../Convolution/BokehBlurProcessor{TPixel}.cs | 18 +- .../Convolution2DRowOperation{TPixel}.cs | 4 +- .../Convolution2PassProcessor{TPixel}.cs | 16 +- .../ConvolutionProcessor{TPixel}.cs | 4 +- .../Convolution/KernelSamplingMap.cs | 4 +- .../Processors/Dithering/OrderedDither.cs | 2 +- .../Filters/OpaqueProcessor{TPixel}.cs | 4 +- ...lHistogramEqualizationProcessor{TPixel}.cs | 10 -- .../Quantization/OctreeQuantizer{TPixel}.cs | 2 +- .../Quantization/WuQuantizer{TPixel}.cs | 4 +- .../Transforms/CropProcessor{TPixel}.cs | 4 +- .../Transforms/Resize/ResizeWorker.cs | 2 +- .../Helpers/RuntimeEnvironmentTests.cs | 12 +- .../UniformUnmanagedMemoryPoolTests.Trim.cs | 14 +- ...niformUnmanagedPoolMemoryAllocatorTests.cs | 2 - .../PixelOperations/PixelOperationsTests.cs | 2 - .../Primitives/ColorMatrixTests.cs | 48 ++--- .../Processing/Convolution/DetectEdgesTest.cs | 14 +- .../Processors/Convolution/DetectEdgesTest.cs | 1 - .../TestDataIcc/IccTestDataCurves.cs | 2 - .../TestDataIcc/IccTestDataProfiles.cs | 4 - .../TestDataIcc/IccTestDataTagDataEntry.cs | 6 - .../TestUtilities/PausedStream.cs | 2 - .../TestUtilities/PixelTypes.cs | 3 - .../TestUtilities/TestImageExtensions.cs | 2 - 108 files changed, 423 insertions(+), 586 deletions(-) diff --git a/shared-infrastructure b/shared-infrastructure index e12c30b3b1..86af5a8219 160000 --- a/shared-infrastructure +++ b/shared-infrastructure @@ -1 +1 @@ -Subproject commit e12c30b3b1427815cffa22ac95c77ba29f65fe5a +Subproject commit 86af5a82198c21ce6889370428eb97d1fa7b41ff diff --git a/src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs b/src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs index c15cdf8a8a..82e9a123be 100644 --- a/src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs +++ b/src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs @@ -89,7 +89,7 @@ namespace SixLabors.ImageSharp.ColorSpaces.Companding if (Numerics.Modulo2(vectors.Length) != 0) { // Vector4 fits neatly in pairs. Any overlap has to be equal to 1. - Expand(ref MemoryMarshal.GetReference(vectors.Slice(vectors.Length - 1))); + Expand(ref MemoryMarshal.GetReference(vectors[^1..])); } } else @@ -112,7 +112,7 @@ namespace SixLabors.ImageSharp.ColorSpaces.Companding if (Numerics.Modulo2(vectors.Length) != 0) { // Vector4 fits neatly in pairs. Any overlap has to be equal to 1. - Compress(ref MemoryMarshal.GetReference(vectors.Slice(vectors.Length - 1))); + Compress(ref MemoryMarshal.GetReference(vectors[^1..])); } } else diff --git a/src/ImageSharp/ColorSpaces/Conversion/Implementation/VonKriesChromaticAdaptation.cs b/src/ImageSharp/ColorSpaces/Conversion/Implementation/VonKriesChromaticAdaptation.cs index b107aacafa..5723d26914 100644 --- a/src/ImageSharp/ColorSpaces/Conversion/Implementation/VonKriesChromaticAdaptation.cs +++ b/src/ImageSharp/ColorSpaces/Conversion/Implementation/VonKriesChromaticAdaptation.cs @@ -75,7 +75,7 @@ namespace SixLabors.ImageSharp.ColorSpaces.Conversion if (sourceWhitePoint.Equals(destinationWhitePoint)) { - source.CopyTo(destination.Slice(0, count)); + source.CopyTo(destination[..count]); return; } diff --git a/src/ImageSharp/Common/Helpers/HexConverter.cs b/src/ImageSharp/Common/Helpers/HexConverter.cs index 27a5a40f6d..40f80a86f8 100644 --- a/src/ImageSharp/Common/Helpers/HexConverter.cs +++ b/src/ImageSharp/Common/Helpers/HexConverter.cs @@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp.Common.Helpers { // Slightly better performance in the loop below, allows us to skip a bounds check // while still supporting output buffers that are larger than necessary - bytes = bytes.Slice(0, chars.Length / 2); + bytes = bytes[..(chars.Length / 2)]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs b/src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs index 9ba9d34b13..5b8673da8e 100644 --- a/src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs +++ b/src/ImageSharp/Compression/Zlib/DeflaterOutputStream.cs @@ -95,7 +95,7 @@ namespace SixLabors.ImageSharp.Compression.Zlib break; } - this.rawStream.Write(this.buffer.Span.Slice(0, deflateCount)); + this.rawStream.Write(this.buffer.Span[..deflateCount]); } if (!this.deflater.IsNeedingInput) @@ -115,7 +115,7 @@ namespace SixLabors.ImageSharp.Compression.Zlib break; } - this.rawStream.Write(this.buffer.Span.Slice(0, len)); + this.rawStream.Write(this.buffer.Span[..len]); } if (!this.deflater.IsFinished) diff --git a/src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs b/src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs index b24150faf4..1ab6349f02 100644 --- a/src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs +++ b/src/ImageSharp/Compression/Zlib/DeflaterPendingBuffer.cs @@ -75,7 +75,7 @@ namespace SixLabors.ImageSharp.Compression.Zlib { Unsafe.CopyBlockUnaligned( ref this.buffer.Span[this.end], - ref MemoryMarshal.GetReference(block.Slice(offset)), + ref MemoryMarshal.GetReference(block[offset..]), unchecked((uint)length)); this.end += length; diff --git a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs index 274486cec5..33af606b9f 100644 --- a/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs +++ b/src/ImageSharp/Diagnostics/MemoryDiagnostics.cs @@ -85,16 +85,10 @@ namespace SixLabors.ImageSharp.Diagnostics } // Schedule on the ThreadPool, to avoid user callback messing up the finalizer thread. -#if NETSTANDARD2_1 || NETCOREAPP2_1_OR_GREATER ThreadPool.QueueUserWorkItem( stackTrace => undisposedAllocation?.Invoke(stackTrace), allocationStackTrace, preferLocal: false); -#else - ThreadPool.QueueUserWorkItem( - stackTrace => undisposedAllocation?.Invoke((string)stackTrace), - allocationStackTrace); -#endif } } } diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index 3a96c40223..150f8eadac 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -597,7 +597,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp this.stream.Read(run, 0, run.Length); - run.AsSpan().CopyTo(buffer.Slice(count)); + run.AsSpan().CopyTo(buffer[count..]); count += run.Length; @@ -676,7 +676,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp this.stream.Read(run, 0, run.Length); - run.AsSpan().CopyTo(buffer.Slice(start: uncompressedPixels * 3)); + run.AsSpan().CopyTo(buffer[(uncompressedPixels * 3)..]); uncompressedPixels += length; @@ -891,7 +891,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp int offset = 0; for (int x = 0; x < width; x++) { - short temp = BinaryPrimitives.ReadInt16LittleEndian(bufferSpan.Slice(offset)); + short temp = BinaryPrimitives.ReadInt16LittleEndian(bufferSpan[offset..]); // Rescale values, so the values range from 0 to 255. int r = (redMaskBits == 5) ? GetBytesFrom5BitValue((temp & redMask) >> rightShiftRedMask) : GetBytesFrom6BitValue((temp & redMask) >> rightShiftRedMask); @@ -1122,7 +1122,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp int offset = 0; for (int x = 0; x < width; x++) { - uint temp = BinaryPrimitives.ReadUInt32LittleEndian(bufferSpan.Slice(offset)); + uint temp = BinaryPrimitives.ReadUInt32LittleEndian(bufferSpan[offset..]); if (unusualBitMask) { @@ -1246,7 +1246,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp var bitfieldsBuffer = new byte[12]; this.stream.Read(bitfieldsBuffer, 0, 12); Span data = bitfieldsBuffer.AsSpan(); - this.infoHeader.RedMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(0, 4)); + this.infoHeader.RedMask = BinaryPrimitives.ReadInt32LittleEndian(data[..4]); this.infoHeader.GreenMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)); this.infoHeader.BlueMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)); } @@ -1255,7 +1255,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp var bitfieldsBuffer = new byte[16]; this.stream.Read(bitfieldsBuffer, 0, 16); Span data = bitfieldsBuffer.AsSpan(); - this.infoHeader.RedMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(0, 4)); + this.infoHeader.RedMask = BinaryPrimitives.ReadInt32LittleEndian(data[..4]); this.infoHeader.GreenMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)); this.infoHeader.BlueMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)); this.infoHeader.AlphaMask = BinaryPrimitives.ReadInt32LittleEndian(data.Slice(12, 4)); diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index f71275b7cc..9671b7d27e 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -257,7 +257,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp stream.Write(iccProfileData); BinaryPrimitives.WriteInt32LittleEndian(buffer, streamPositionAfterImageData); stream.Position = BmpFileHeader.Size + 112; - stream.Write(buffer.Slice(0, 4)); + stream.Write(buffer[..4]); } } @@ -618,7 +618,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp where TPixel : unmanaged, IPixel { int quantizedColorBytes = quantizedColorPalette.Length * 4; - PixelOperations.Instance.ToBgra32(this.configuration, quantizedColorPalette, MemoryMarshal.Cast(colorPalette.Slice(0, quantizedColorBytes))); + PixelOperations.Instance.ToBgra32(this.configuration, quantizedColorPalette, MemoryMarshal.Cast(colorPalette[..quantizedColorBytes])); Span colorPaletteAsUInt = MemoryMarshal.Cast(colorPalette); for (int i = 0; i < colorPaletteAsUInt.Length; i++) { diff --git a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs index 823569b8ad..9c0efcb6d3 100644 --- a/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs +++ b/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs @@ -308,7 +308,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// The parsed header. /// public static BmpInfoHeader ParseCore(ReadOnlySpan data) => new( - headerSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(0, 4)), + headerSize: BinaryPrimitives.ReadInt32LittleEndian(data[..4]), width: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(4, 2)), height: BinaryPrimitives.ReadUInt16LittleEndian(data.Slice(6, 2)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(8, 2)), @@ -322,7 +322,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// The parsed header. /// public static BmpInfoHeader ParseOs22Short(ReadOnlySpan data) => new( - headerSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(0, 4)), + headerSize: BinaryPrimitives.ReadInt32LittleEndian(data[..4]), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), @@ -335,7 +335,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// The parsed header. /// public static BmpInfoHeader ParseV3(ReadOnlySpan data) => new( - headerSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(0, 4)), + headerSize: BinaryPrimitives.ReadInt32LittleEndian(data[..4]), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), @@ -356,7 +356,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// The parsed header. /// public static BmpInfoHeader ParseAdobeV3(ReadOnlySpan data, bool withAlpha = true) => new( - headerSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(0, 4)), + headerSize: BinaryPrimitives.ReadInt32LittleEndian(data[..4]), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), @@ -383,7 +383,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp public static BmpInfoHeader ParseOs2Version2(ReadOnlySpan data) { var infoHeader = new BmpInfoHeader( - headerSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(0, 4)), + headerSize: BinaryPrimitives.ReadInt32LittleEndian(data[..4]), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), @@ -430,7 +430,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp /// The parsed header. /// public static BmpInfoHeader ParseV4(ReadOnlySpan data) => new( - headerSize: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(0, 4)), + headerSize: BinaryPrimitives.ReadInt32LittleEndian(data[..4]), width: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(4, 4)), height: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(8, 4)), planes: BinaryPrimitives.ReadInt16LittleEndian(data.Slice(12, 2)), @@ -482,7 +482,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp public void WriteV3Header(Span buffer) { buffer.Clear(); - BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(0, 4), SizeV3); + BinaryPrimitives.WriteInt32LittleEndian(buffer[..4], SizeV3); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(4, 4), this.Width); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(8, 4), this.Height); BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(12, 2), this.Planes); @@ -502,7 +502,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp public void WriteV4Header(Span buffer) { buffer.Clear(); - BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(0, 4), SizeV4); + BinaryPrimitives.WriteInt32LittleEndian(buffer[..4], SizeV4); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(4, 4), this.Width); BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(8, 4), this.Height); BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(12, 2), this.Planes); diff --git a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs index bb45c8364b..0f7606a432 100644 --- a/src/ImageSharp/Formats/Gif/GifEncoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifEncoderCore.cs @@ -448,7 +448,7 @@ namespace SixLabors.ImageSharp.Formats.Gif extensionBuffer[0] = GifConstants.ExtensionIntroducer; extensionBuffer[1] = extension.Label; - extension.WriteTo(extensionBuffer.Slice(2)); + extension.WriteTo(extensionBuffer[2..]); extensionBuffer[extensionSize + 2] = GifConstants.Terminator; diff --git a/src/ImageSharp/Formats/Gif/Sections/GifImageDescriptor.cs b/src/ImageSharp/Formats/Gif/Sections/GifImageDescriptor.cs index 07e0f6f0ce..9f7755b013 100644 --- a/src/ImageSharp/Formats/Gif/Sections/GifImageDescriptor.cs +++ b/src/ImageSharp/Formats/Gif/Sections/GifImageDescriptor.cs @@ -71,7 +71,7 @@ namespace SixLabors.ImageSharp.Formats.Gif { buffer[0] = GifConstants.ImageDescriptorLabel; - ref GifImageDescriptor dest = ref Unsafe.As(ref MemoryMarshal.GetReference(buffer.Slice(1))); + ref GifImageDescriptor dest = ref Unsafe.As(ref MemoryMarshal.GetReference(buffer[1..])); dest = this; } diff --git a/src/ImageSharp/Formats/Gif/Sections/GifNetscapeLoopingApplicationExtension.cs b/src/ImageSharp/Formats/Gif/Sections/GifNetscapeLoopingApplicationExtension.cs index d9ee93883d..8965ae345d 100644 --- a/src/ImageSharp/Formats/Gif/Sections/GifNetscapeLoopingApplicationExtension.cs +++ b/src/ImageSharp/Formats/Gif/Sections/GifNetscapeLoopingApplicationExtension.cs @@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.Formats.Gif public static GifNetscapeLoopingApplicationExtension Parse(ReadOnlySpan buffer) { - ushort repeatCount = BinaryPrimitives.ReadUInt16LittleEndian(buffer.Slice(0, 2)); + ushort repeatCount = BinaryPrimitives.ReadUInt16LittleEndian(buffer[..2]); return new GifNetscapeLoopingApplicationExtension(repeatCount); } diff --git a/src/ImageSharp/Formats/Gif/Sections/GifXmpApplicationExtension.cs b/src/ImageSharp/Formats/Gif/Sections/GifXmpApplicationExtension.cs index d3a621e7be..9f8d8e4d07 100644 --- a/src/ImageSharp/Formats/Gif/Sections/GifXmpApplicationExtension.cs +++ b/src/ImageSharp/Formats/Gif/Sections/GifXmpApplicationExtension.cs @@ -55,11 +55,11 @@ namespace SixLabors.ImageSharp.Formats.Gif // Write "XMP DataXMP" ReadOnlySpan idBytes = GifConstants.XmpApplicationIdentificationBytes; - idBytes.CopyTo(buffer.Slice(bytesWritten)); + idBytes.CopyTo(buffer[bytesWritten..]); bytesWritten += idBytes.Length; // XMP Data itself - this.Data.CopyTo(buffer.Slice(bytesWritten)); + this.Data.CopyTo(buffer[bytesWritten..]); bytesWritten += this.Data.Length; // Write the Magic Trailer diff --git a/src/ImageSharp/Formats/ImageFormatManager.cs b/src/ImageSharp/Formats/ImageFormatManager.cs index 9f3f0d47fe..78e797dcd7 100644 --- a/src/ImageSharp/Formats/ImageFormatManager.cs +++ b/src/ImageSharp/Formats/ImageFormatManager.cs @@ -101,7 +101,7 @@ namespace SixLabors.ImageSharp.Formats if (extension[0] == '.') { - extension = extension.Substring(1); + extension = extension[1..]; } return this.imageFormats.FirstOrDefault(x => x.FileExtensions.Contains(extension, StringComparer.OrdinalIgnoreCase)); diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ProfileResolver.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ProfileResolver.cs index 9f1d4d91ff..ed678bd2b2 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/ProfileResolver.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/ProfileResolver.cs @@ -89,7 +89,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder public static bool IsProfile(ReadOnlySpan bytesToCheck, ReadOnlySpan profileIdentifier) { return bytesToCheck.Length >= profileIdentifier.Length - && bytesToCheck.Slice(0, profileIdentifier.Length).SequenceEqual(profileIdentifier); + && bytesToCheck[..profileIdentifier.Length].SequenceEqual(profileIdentifier); } } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs index d460d9497c..fafbabe25b 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/SpectralConverter{TPixel}.cs @@ -162,7 +162,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Components.Decoder { Span proxyRow = this.paddedProxyPixelRow.GetSpan(); PixelOperations.Instance.PackFromRgbPlanes(this.configuration, r, g, b, proxyRow); - proxyRow.Slice(0, width).CopyTo(this.pixelBuffer.DangerousGetRowSpan(yy)); + proxyRow[..width].CopyTo(this.pixelBuffer.DangerousGetRowSpan(yy)); } } diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 11187fb910..66fc6ce808 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -810,7 +810,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg remaining = 0; } - if (ProfileResolver.IsProfile(this.temp, ProfileResolver.XmpMarker.Slice(0, ExifMarkerLength))) + if (ProfileResolver.IsProfile(this.temp, ProfileResolver.XmpMarker[..ExifMarkerLength])) { const int remainingXmpMarkerBytes = XmpMarkerLength - ExifMarkerLength; if (remaining < remainingXmpMarkerBytes || this.IgnoreMetadata) @@ -912,13 +912,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg while (blockDataSpan.Length > 12) { - if (!ProfileResolver.IsProfile(blockDataSpan.Slice(0, 4), ProfileResolver.AdobeImageResourceBlockMarker)) + if (!ProfileResolver.IsProfile(blockDataSpan[..4], ProfileResolver.AdobeImageResourceBlockMarker)) { return; } - blockDataSpan = blockDataSpan.Slice(4); - Span imageResourceBlockId = blockDataSpan.Slice(0, 2); + blockDataSpan = blockDataSpan[4..]; + Span imageResourceBlockId = blockDataSpan[..2]; if (ProfileResolver.IsProfile(imageResourceBlockId, ProfileResolver.AdobeIptcMarker)) { int resourceBlockNameLength = ReadImageResourceNameLength(blockDataSpan); @@ -942,7 +942,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg break; } - blockDataSpan = blockDataSpan.Slice(dataStartIdx + resourceDataSize); + blockDataSpan = blockDataSpan[(dataStartIdx + resourceDataSize)..]; } } } @@ -1322,9 +1322,9 @@ namespace SixLabors.ImageSharp.Formats.Jpeg using (IMemoryOwner buffer = this.Configuration.MemoryAllocator.Allocate(totalBufferSize)) { Span bufferSpan = buffer.GetSpan(); - Span huffmanLengthsSpan = bufferSpan.Slice(0, codeLengthsByteSize); + Span huffmanLengthsSpan = bufferSpan[..codeLengthsByteSize]; Span huffmanValuesSpan = bufferSpan.Slice(codeLengthsByteSize, codeValuesMaxByteSize); - Span tableWorkspace = MemoryMarshal.Cast(bufferSpan.Slice(codeLengthsByteSize + codeValuesMaxByteSize)); + Span tableWorkspace = MemoryMarshal.Cast(bufferSpan[(codeLengthsByteSize + codeValuesMaxByteSize)..]); for (int i = 2; i < remaining;) { @@ -1367,7 +1367,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg tableType, tableIndex, huffmanLengthsSpan, - huffmanValuesSpan.Slice(0, codeLengthSum), + huffmanValuesSpan[..codeLengthSum], tableWorkspace); } } diff --git a/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs b/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs index 25c7ee450e..431eea4c42 100644 --- a/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs +++ b/src/ImageSharp/Formats/Pbm/PbmEncoderCore.cs @@ -145,17 +145,17 @@ namespace SixLabors.ImageSharp.Formats.Pbm buffer[1] = signature; buffer[2] = NewLine; - Utf8Formatter.TryFormat(pixelSize.Width, buffer.Slice(written), out int bytesWritten); + Utf8Formatter.TryFormat(pixelSize.Width, buffer[written..], out int bytesWritten); written += bytesWritten; buffer[written++] = Space; - Utf8Formatter.TryFormat(pixelSize.Height, buffer.Slice(written), out bytesWritten); + Utf8Formatter.TryFormat(pixelSize.Height, buffer[written..], out bytesWritten); written += bytesWritten; buffer[written++] = NewLine; if (this.colorType != PbmColorType.BlackAndWhite) { int maxPixelValue = this.componentType == PbmComponentType.Short ? 65535 : 255; - Utf8Formatter.TryFormat(maxPixelValue, buffer.Slice(written), out bytesWritten); + Utf8Formatter.TryFormat(maxPixelValue, buffer[written..], out bytesWritten); written += bytesWritten; buffer[written++] = NewLine; } diff --git a/src/ImageSharp/Formats/Pbm/PlainEncoder.cs b/src/ImageSharp/Formats/Pbm/PlainEncoder.cs index b858d1e63a..d989b6e660 100644 --- a/src/ImageSharp/Formats/Pbm/PlainEncoder.cs +++ b/src/ImageSharp/Formats/Pbm/PlainEncoder.cs @@ -94,7 +94,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm int written = 0; for (int x = 0; x < width; x++) { - Utf8Formatter.TryFormat(rowSpan[x].PackedValue, plainSpan.Slice(written), out int bytesWritten, DecimalFormat); + Utf8Formatter.TryFormat(rowSpan[x].PackedValue, plainSpan[written..], out int bytesWritten, DecimalFormat); written += bytesWritten; plainSpan[written++] = Space; } @@ -127,7 +127,7 @@ namespace SixLabors.ImageSharp.Formats.Pbm int written = 0; for (int x = 0; x < width; x++) { - Utf8Formatter.TryFormat(rowSpan[x].PackedValue, plainSpan.Slice(written), out int bytesWritten, DecimalFormat); + Utf8Formatter.TryFormat(rowSpan[x].PackedValue, plainSpan[written..], out int bytesWritten, DecimalFormat); written += bytesWritten; plainSpan[written++] = Space; } @@ -160,13 +160,13 @@ namespace SixLabors.ImageSharp.Formats.Pbm int written = 0; for (int x = 0; x < width; x++) { - Utf8Formatter.TryFormat(rowSpan[x].R, plainSpan.Slice(written), out int bytesWritten, DecimalFormat); + Utf8Formatter.TryFormat(rowSpan[x].R, plainSpan[written..], out int bytesWritten, DecimalFormat); written += bytesWritten; plainSpan[written++] = Space; - Utf8Formatter.TryFormat(rowSpan[x].G, plainSpan.Slice(written), out bytesWritten, DecimalFormat); + Utf8Formatter.TryFormat(rowSpan[x].G, plainSpan[written..], out bytesWritten, DecimalFormat); written += bytesWritten; plainSpan[written++] = Space; - Utf8Formatter.TryFormat(rowSpan[x].B, plainSpan.Slice(written), out bytesWritten, DecimalFormat); + Utf8Formatter.TryFormat(rowSpan[x].B, plainSpan[written..], out bytesWritten, DecimalFormat); written += bytesWritten; plainSpan[written++] = Space; } @@ -199,13 +199,13 @@ namespace SixLabors.ImageSharp.Formats.Pbm int written = 0; for (int x = 0; x < width; x++) { - Utf8Formatter.TryFormat(rowSpan[x].R, plainSpan.Slice(written), out int bytesWritten, DecimalFormat); + Utf8Formatter.TryFormat(rowSpan[x].R, plainSpan[written..], out int bytesWritten, DecimalFormat); written += bytesWritten; plainSpan[written++] = Space; - Utf8Formatter.TryFormat(rowSpan[x].G, plainSpan.Slice(written), out bytesWritten, DecimalFormat); + Utf8Formatter.TryFormat(rowSpan[x].G, plainSpan[written..], out bytesWritten, DecimalFormat); written += bytesWritten; plainSpan[written++] = Space; - Utf8Formatter.TryFormat(rowSpan[x].B, plainSpan.Slice(written), out bytesWritten, DecimalFormat); + Utf8Formatter.TryFormat(rowSpan[x].B, plainSpan[written..], out bytesWritten, DecimalFormat); written += bytesWritten; plainSpan[written++] = Space; } diff --git a/src/ImageSharp/Formats/Png/Chunks/PhysicalChunkData.cs b/src/ImageSharp/Formats/Png/Chunks/PhysicalChunkData.cs index 8ad2c5a4ab..31b845e19b 100644 --- a/src/ImageSharp/Formats/Png/Chunks/PhysicalChunkData.cs +++ b/src/ImageSharp/Formats/Png/Chunks/PhysicalChunkData.cs @@ -47,7 +47,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Chunks /// The parsed PhysicalChunkData. public static PhysicalChunkData Parse(ReadOnlySpan data) { - uint hResolution = BinaryPrimitives.ReadUInt32BigEndian(data.Slice(0, 4)); + uint hResolution = BinaryPrimitives.ReadUInt32BigEndian(data[..4]); uint vResolution = BinaryPrimitives.ReadUInt32BigEndian(data.Slice(4, 4)); byte unit = data[8]; @@ -102,7 +102,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Chunks /// The buffer. public void WriteTo(Span buffer) { - BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(0, 4), this.XAxisPixelsPerUnit); + BinaryPrimitives.WriteUInt32BigEndian(buffer[..4], this.XAxisPixelsPerUnit); BinaryPrimitives.WriteUInt32BigEndian(buffer.Slice(4, 4), this.YAxisPixelsPerUnit); buffer[8] = this.UnitSpecifier; } diff --git a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs index fd2d9eab49..c0e14d4ab0 100644 --- a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs @@ -23,8 +23,8 @@ namespace SixLabors.ImageSharp.Formats.Png.Filters { // Insert a byte before the data. result[0] = 0; - result = result.Slice(1); - scanline.Slice(0, Math.Min(scanline.Length, result.Length)).CopyTo(result); + result = result[1..]; + scanline[..Math.Min(scanline.Length, result.Length)].CopyTo(result); } } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 784d9aa11f..69a23c96c8 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -4,7 +4,6 @@ using System; using System.Buffers; using System.Buffers.Binary; -using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Runtime.CompilerServices; @@ -164,10 +163,10 @@ namespace SixLabors.ImageSharp.Formats.Png this.ReadHeaderChunk(pngMetadata, chunk.Data.GetSpan()); break; case PngChunkType.Physical: - this.ReadPhysicalChunk(metadata, chunk.Data.GetSpan()); + ReadPhysicalChunk(metadata, chunk.Data.GetSpan()); break; case PngChunkType.Gamma: - this.ReadGammaChunk(pngMetadata, chunk.Data.GetSpan()); + ReadGammaChunk(pngMetadata, chunk.Data.GetSpan()); break; case PngChunkType.Data: if (image is null) @@ -203,7 +202,7 @@ namespace SixLabors.ImageSharp.Formats.Png { byte[] exifData = new byte[chunk.Length]; chunk.Data.GetSpan().CopyTo(exifData); - this.MergeOrSetExifProfile(metadata, new ExifProfile(exifData), replaceExistingKeys: true); + MergeOrSetExifProfile(metadata, new ExifProfile(exifData), replaceExistingKeys: true); } break; @@ -269,7 +268,7 @@ namespace SixLabors.ImageSharp.Formats.Png break; } - this.ReadPhysicalChunk(metadata, chunk.Data.GetSpan()); + ReadPhysicalChunk(metadata, chunk.Data.GetSpan()); break; case PngChunkType.Gamma: if (this.colorMetadataOnly) @@ -278,7 +277,7 @@ namespace SixLabors.ImageSharp.Formats.Png break; } - this.ReadGammaChunk(pngMetadata, chunk.Data.GetSpan()); + ReadGammaChunk(pngMetadata, chunk.Data.GetSpan()); break; case PngChunkType.Data: @@ -340,7 +339,7 @@ namespace SixLabors.ImageSharp.Formats.Png { byte[] exifData = new byte[chunk.Length]; chunk.Data.GetSpan().CopyTo(exifData); - this.MergeOrSetExifProfile(metadata, new ExifProfile(exifData), replaceExistingKeys: true); + MergeOrSetExifProfile(metadata, new ExifProfile(exifData), replaceExistingKeys: true); } break; @@ -429,7 +428,7 @@ namespace SixLabors.ImageSharp.Formats.Png /// /// The metadata to read to. /// The data containing physical data. - private void ReadPhysicalChunk(ImageMetadata metadata, ReadOnlySpan data) + private static void ReadPhysicalChunk(ImageMetadata metadata, ReadOnlySpan data) { var physicalChunk = PhysicalChunkData.Parse(data); @@ -446,7 +445,7 @@ namespace SixLabors.ImageSharp.Formats.Png /// /// The metadata to read to. /// The data containing physical data. - private void ReadGammaChunk(PngMetadata pngMetadata, ReadOnlySpan data) + private static void ReadGammaChunk(PngMetadata pngMetadata, ReadOnlySpan data) { if (data.Length < 4) { @@ -749,7 +748,7 @@ namespace SixLabors.ImageSharp.Formats.Png Span rowSpan = pixels.PixelBuffer.DangerousGetRowSpan(this.currentRow); // Trim the first marker byte from the buffer - ReadOnlySpan trimmed = defilteredScanline.Slice(1, defilteredScanline.Length - 1); + ReadOnlySpan trimmed = defilteredScanline[1..]; // Convert 1, 2, and 4 bit pixel data into the 8 bit equivalent. IMemoryOwner buffer = null; @@ -841,7 +840,7 @@ namespace SixLabors.ImageSharp.Formats.Png where TPixel : unmanaged, IPixel { // Trim the first marker byte from the buffer - ReadOnlySpan trimmed = defilteredScanline.Slice(1, defilteredScanline.Length - 1); + ReadOnlySpan trimmed = defilteredScanline[1..]; // Convert 1, 2, and 4 bit pixel data into the 8 bit equivalent. IMemoryOwner buffer = null; @@ -941,7 +940,7 @@ namespace SixLabors.ImageSharp.Formats.Png { if (this.header.BitDepth == 16) { - ushort rc = BinaryPrimitives.ReadUInt16LittleEndian(alpha.Slice(0, 2)); + ushort rc = BinaryPrimitives.ReadUInt16LittleEndian(alpha[..2]); ushort gc = BinaryPrimitives.ReadUInt16LittleEndian(alpha.Slice(2, 2)); ushort bc = BinaryPrimitives.ReadUInt16LittleEndian(alpha.Slice(4, 2)); @@ -963,7 +962,7 @@ namespace SixLabors.ImageSharp.Formats.Png { if (this.header.BitDepth == 16) { - pngMetadata.TransparentL16 = new L16(BinaryPrimitives.ReadUInt16LittleEndian(alpha.Slice(0, 2))); + pngMetadata.TransparentL16 = new L16(BinaryPrimitives.ReadUInt16LittleEndian(alpha[..2])); } else { @@ -1014,13 +1013,13 @@ namespace SixLabors.ImageSharp.Formats.Png return; } - ReadOnlySpan keywordBytes = data.Slice(0, zeroIndex); - if (!this.TryReadTextKeyword(keywordBytes, out string name)) + ReadOnlySpan keywordBytes = data[..zeroIndex]; + if (!TryReadTextKeyword(keywordBytes, out string name)) { return; } - string value = PngConstants.Encoding.GetString(data.Slice(zeroIndex + 1)); + string value = PngConstants.Encoding.GetString(data[(zeroIndex + 1)..]); if (!this.TryReadTextChunkMetadata(baseMetadata, name, value)) { @@ -1042,7 +1041,7 @@ namespace SixLabors.ImageSharp.Formats.Png } int zeroIndex = data.IndexOf((byte)0); - if (zeroIndex < PngConstants.MinTextKeywordLength || zeroIndex > PngConstants.MaxTextKeywordLength) + if (zeroIndex is < PngConstants.MinTextKeywordLength or > PngConstants.MaxTextKeywordLength) { return; } @@ -1054,13 +1053,13 @@ namespace SixLabors.ImageSharp.Formats.Png return; } - ReadOnlySpan keywordBytes = data.Slice(0, zeroIndex); - if (!this.TryReadTextKeyword(keywordBytes, out string name)) + ReadOnlySpan keywordBytes = data[..zeroIndex]; + if (!TryReadTextKeyword(keywordBytes, out string name)) { return; } - ReadOnlySpan compressedData = data.Slice(zeroIndex + 2); + ReadOnlySpan compressedData = data[(zeroIndex + 2)..]; if (this.TryUncompressTextData(compressedData, PngConstants.Encoding, out string uncompressed) && !this.TryReadTextChunkMetadata(baseMetadata, name, uncompressed)) @@ -1105,19 +1104,19 @@ namespace SixLabors.ImageSharp.Formats.Png ReadOnlySpan dataSpan = data.AsSpan(); dataSpan = dataSpan.TrimStart(); - if (!StringEqualsInsensitive(dataSpan.Slice(0, 4), "exif".AsSpan())) + if (!StringEqualsInsensitive(dataSpan[..4], "exif".AsSpan())) { // "exif" identifier is missing from the beginning of the text chunk return false; } // Skip to the data length - dataSpan = dataSpan.Slice(4).TrimStart(); + dataSpan = dataSpan[4..].TrimStart(); int dataLengthEnd = dataSpan.IndexOf('\n'); - int dataLength = ParseInt32(dataSpan.Slice(0, dataSpan.IndexOf('\n'))); + int dataLength = ParseInt32(dataSpan[..dataSpan.IndexOf('\n')]); // Skip to the hex-encoded data - dataSpan = dataSpan.Slice(dataLengthEnd).Trim(); + dataSpan = dataSpan[dataLengthEnd..].Trim(); // Sequence of bytes for the exif header ("Exif" ASCII and two zero bytes). // This doesn't actually allocate. @@ -1142,15 +1141,15 @@ namespace SixLabors.ImageSharp.Formats.Png tempExifBuf = new byte[exifHeader.Length]; } - HexConverter.HexStringToBytes(dataSpan.Slice(0, exifHeader.Length * 2), tempExifBuf); - if (!tempExifBuf.AsSpan().Slice(0, exifHeader.Length).SequenceEqual(exifHeader)) + HexConverter.HexStringToBytes(dataSpan[..(exifHeader.Length * 2)], tempExifBuf); + if (!tempExifBuf.AsSpan()[..exifHeader.Length].SequenceEqual(exifHeader)) { // Exif header in the hex data is not valid return false; } // Skip over the exif header we just tested - dataSpan = dataSpan.Slice(exifHeader.Length * 2); + dataSpan = dataSpan[(exifHeader.Length * 2)..]; dataLength -= exifHeader.Length; // Load the hex-encoded data, one line at a time @@ -1161,12 +1160,12 @@ namespace SixLabors.ImageSharp.Formats.Png int newlineIndex = dataSpan.IndexOf('\n'); if (newlineIndex != -1) { - lineSpan = dataSpan.Slice(0, newlineIndex); + lineSpan = dataSpan[..newlineIndex]; } - i += HexConverter.HexStringToBytes(lineSpan, exifBlob.AsSpan().Slice(i)); + i += HexConverter.HexStringToBytes(lineSpan, exifBlob.AsSpan()[i..]); - dataSpan = dataSpan.Slice(newlineIndex + 1); + dataSpan = dataSpan[(newlineIndex + 1)..]; } } catch @@ -1174,7 +1173,7 @@ namespace SixLabors.ImageSharp.Formats.Png return false; } - this.MergeOrSetExifProfile(metadata, new ExifProfile(exifBlob), replaceExistingKeys: false); + MergeOrSetExifProfile(metadata, new ExifProfile(exifBlob), replaceExistingKeys: false); return true; } @@ -1198,13 +1197,13 @@ namespace SixLabors.ImageSharp.Formats.Png return; } - ReadOnlySpan keywordBytes = data.Slice(0, zeroIndex); - if (!this.TryReadTextKeyword(keywordBytes, out string name)) + ReadOnlySpan keywordBytes = data[..zeroIndex]; + if (!TryReadTextKeyword(keywordBytes, out string name)) { return; } - ReadOnlySpan compressedData = data.Slice(zeroIndex + 2); + ReadOnlySpan compressedData = data[(zeroIndex + 2)..]; if (this.TryUncompressZlibData(compressedData, out byte[] iccpProfileBytes)) { @@ -1222,29 +1221,27 @@ namespace SixLabors.ImageSharp.Formats.Png { fixed (byte* compressedDataBase = compressedData) { - using (IMemoryOwner destBuffer = this.memoryAllocator.Allocate(this.Configuration.StreamProcessingBufferSize)) - using (var memoryStreamOutput = new MemoryStream(compressedData.Length)) - using (var memoryStreamInput = new UnmanagedMemoryStream(compressedDataBase, compressedData.Length)) - using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStreamInput)) - using (var inflateStream = new ZlibInflateStream(bufferedStream)) + using IMemoryOwner destBuffer = this.memoryAllocator.Allocate(this.Configuration.StreamProcessingBufferSize); + using var memoryStreamOutput = new MemoryStream(compressedData.Length); + using var memoryStreamInput = new UnmanagedMemoryStream(compressedDataBase, compressedData.Length); + using var bufferedStream = new BufferedReadStream(this.Configuration, memoryStreamInput); + using var inflateStream = new ZlibInflateStream(bufferedStream); + Span destUncompressedData = destBuffer.GetSpan(); + if (!inflateStream.AllocateNewBytes(compressedData.Length, false)) { - Span destUncompressedData = destBuffer.GetSpan(); - if (!inflateStream.AllocateNewBytes(compressedData.Length, false)) - { - uncompressedBytesArray = Array.Empty(); - return false; - } - - int bytesRead = inflateStream.CompressedStream.Read(destUncompressedData, 0, destUncompressedData.Length); - while (bytesRead != 0) - { - memoryStreamOutput.Write(destUncompressedData.Slice(0, bytesRead)); - bytesRead = inflateStream.CompressedStream.Read(destUncompressedData, 0, destUncompressedData.Length); - } + uncompressedBytesArray = Array.Empty(); + return false; + } - uncompressedBytesArray = memoryStreamOutput.ToArray(); - return true; + int bytesRead = inflateStream.CompressedStream.Read(destUncompressedData, 0, destUncompressedData.Length); + while (bytesRead != 0) + { + memoryStreamOutput.Write(destUncompressedData[..bytesRead]); + bytesRead = inflateStream.CompressedStream.Read(destUncompressedData, 0, destUncompressedData.Length); } + + uncompressedBytesArray = memoryStreamOutput.ToArray(); + return true; } } @@ -1256,31 +1253,14 @@ namespace SixLabors.ImageSharp.Formats.Png /// The second to compare. /// True if the spans were identical, false otherwise. private static bool StringEqualsInsensitive(ReadOnlySpan span1, ReadOnlySpan span2) - { -#pragma warning disable IDE0022 // Use expression body for methods -#if NETSTANDARD2_1 || NETCOREAPP2_1_OR_GREATER - return span1.Equals(span2, StringComparison.OrdinalIgnoreCase); -#else - return span1.ToString().Equals(span2.ToString(), StringComparison.OrdinalIgnoreCase); -#endif -#pragma warning restore IDE0022 // Use expression body for methods - } + => span1.Equals(span2, StringComparison.OrdinalIgnoreCase); /// /// int.Parse() a ReadOnlySpan<char>, with a fallback for older frameworks. /// /// The to parse. /// The parsed . - private static int ParseInt32(ReadOnlySpan span) - { -#pragma warning disable IDE0022 // Use expression body for methods -#if NETSTANDARD2_1 || NETCOREAPP2_1_OR_GREATER - return int.Parse(span); -#else - return int.Parse(span.ToString()); -#endif -#pragma warning restore IDE0022 // Use expression body for methods - } + private static int ParseInt32(ReadOnlySpan span) => int.Parse(span); /// /// Sets the in to , @@ -1291,7 +1271,7 @@ namespace SixLabors.ImageSharp.Formats.Png /// If already contains an , /// controls whether existing exif tags in will be overwritten with any conflicting /// tags from . - private void MergeOrSetExifProfile(ImageMetadata metadata, ExifProfile newProfile, bool replaceExistingKeys) + private static void MergeOrSetExifProfile(ImageMetadata metadata, ExifProfile newProfile, bool replaceExistingKeys) { if (metadata.ExifProfile is null) { @@ -1331,13 +1311,13 @@ namespace SixLabors.ImageSharp.Formats.Png PngMetadata pngMetadata = metadata.GetPngMetadata(); int zeroIndexKeyword = data.IndexOf((byte)0); - if (zeroIndexKeyword < PngConstants.MinTextKeywordLength || zeroIndexKeyword > PngConstants.MaxTextKeywordLength) + if (zeroIndexKeyword is < PngConstants.MinTextKeywordLength or > PngConstants.MaxTextKeywordLength) { return; } byte compressionFlag = data[zeroIndexKeyword + 1]; - if (!(compressionFlag == 0 || compressionFlag == 1)) + if (compressionFlag is not (0 or 1)) { return; } @@ -1350,7 +1330,7 @@ namespace SixLabors.ImageSharp.Formats.Png } int langStartIdx = zeroIndexKeyword + 3; - int languageLength = data.Slice(langStartIdx).IndexOf((byte)0); + int languageLength = data[langStartIdx..].IndexOf((byte)0); if (languageLength < 0) { return; @@ -1359,11 +1339,11 @@ namespace SixLabors.ImageSharp.Formats.Png string language = PngConstants.LanguageEncoding.GetString(data.Slice(langStartIdx, languageLength)); int translatedKeywordStartIdx = langStartIdx + languageLength + 1; - int translatedKeywordLength = data.Slice(translatedKeywordStartIdx).IndexOf((byte)0); + int translatedKeywordLength = data[translatedKeywordStartIdx..].IndexOf((byte)0); string translatedKeyword = PngConstants.TranslatedEncoding.GetString(data.Slice(translatedKeywordStartIdx, translatedKeywordLength)); - ReadOnlySpan keywordBytes = data.Slice(0, zeroIndexKeyword); - if (!this.TryReadTextKeyword(keywordBytes, out string keyword)) + ReadOnlySpan keywordBytes = data[..zeroIndexKeyword]; + if (!TryReadTextKeyword(keywordBytes, out string keyword)) { return; } @@ -1371,21 +1351,21 @@ namespace SixLabors.ImageSharp.Formats.Png int dataStartIdx = translatedKeywordStartIdx + translatedKeywordLength + 1; if (compressionFlag == 1) { - ReadOnlySpan compressedData = data.Slice(dataStartIdx); + ReadOnlySpan compressedData = data[dataStartIdx..]; if (this.TryUncompressTextData(compressedData, PngConstants.TranslatedEncoding, out string uncompressed)) { pngMetadata.TextData.Add(new PngTextData(keyword, uncompressed, language, translatedKeyword)); } } - else if (this.IsXmpTextData(keywordBytes)) + else if (IsXmpTextData(keywordBytes)) { - var xmpProfile = new XmpProfile(data.Slice(dataStartIdx).ToArray()); + var xmpProfile = new XmpProfile(data[dataStartIdx..].ToArray()); metadata.XmpProfile = xmpProfile; } else { - string value = PngConstants.TranslatedEncoding.GetString(data.Slice(dataStartIdx)); + string value = PngConstants.TranslatedEncoding.GetString(data[dataStartIdx..]); pngMetadata.TextData.Add(new PngTextData(keyword, value, language, translatedKeyword)); } } @@ -1620,14 +1600,14 @@ namespace SixLabors.ImageSharp.Formats.Png /// The keyword bytes. /// The name. /// True, if the keyword could be read and is valid. - private bool TryReadTextKeyword(ReadOnlySpan keywordBytes, out string name) + private static bool TryReadTextKeyword(ReadOnlySpan keywordBytes, out string name) { name = string.Empty; // Keywords shall contain only printable Latin-1. foreach (byte c in keywordBytes) { - if (!((c >= 32 && c <= 126) || (c >= 161 && c <= 255))) + if (c is not ((>= 32 and <= 126) or (>= 161 and <= 255))) { return false; } @@ -1635,21 +1615,12 @@ namespace SixLabors.ImageSharp.Formats.Png // Keywords should not be empty or have leading or trailing whitespace. name = PngConstants.Encoding.GetString(keywordBytes); - if (string.IsNullOrWhiteSpace(name) || name.StartsWith(" ") || name.EndsWith(" ")) - { - return false; - } - - return true; + return !string.IsNullOrWhiteSpace(name) && !name.StartsWith(" ") && !name.EndsWith(" "); } - private bool IsXmpTextData(ReadOnlySpan keywordBytes) => keywordBytes.SequenceEqual(PngConstants.XmpKeyword); + private static bool IsXmpTextData(ReadOnlySpan keywordBytes) => keywordBytes.SequenceEqual(PngConstants.XmpKeyword); private void SwapScanlineBuffers() - { - IMemoryOwner temp = this.previousScanline; - this.previousScanline = this.scanline; - this.scanline = temp; - } + => (this.scanline, this.previousScanline) = (this.previousScanline, this.scanline); } } diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 9ae8f299a2..eb1d4be9b3 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -694,7 +694,7 @@ namespace SixLabors.ImageSharp.Formats.Png int bytesWritten = PngConstants.XmpKeyword.Length; // Write the iTxt header (all zeros in this case). - Span iTxtHeader = payload.Slice(bytesWritten); + Span iTxtHeader = payload[bytesWritten..]; iTxtHeader[4] = 0; iTxtHeader[3] = 0; iTxtHeader[2] = 0; @@ -703,7 +703,7 @@ namespace SixLabors.ImageSharp.Formats.Png bytesWritten += 5; // And the XMP data itself. - xmpData.CopyTo(payload.Slice(bytesWritten)); + xmpData.CopyTo(payload[bytesWritten..]); this.WriteChunk(stream, PngChunkType.InternationalText, payload); } } @@ -731,7 +731,7 @@ namespace SixLabors.ImageSharp.Formats.Png int bytesWritten = ColorProfileName.Length; outputBytes[bytesWritten++] = 0; // Null separator. outputBytes[bytesWritten++] = 0; // Compression. - compressedData.CopyTo(outputBytes.Slice(bytesWritten)); + compressedData.CopyTo(outputBytes[bytesWritten..]); this.WriteChunk(stream, PngChunkType.EmbeddedColorProfile, outputBytes); } } @@ -792,13 +792,13 @@ namespace SixLabors.ImageSharp.Formats.Png } outputBytes[bytesWritten++] = 0; - languageTag.CopyTo(outputBytes.Slice(bytesWritten)); + languageTag.CopyTo(outputBytes[bytesWritten..]); bytesWritten += languageTag.Length; outputBytes[bytesWritten++] = 0; - translatedKeyword.CopyTo(outputBytes.Slice(bytesWritten)); + translatedKeyword.CopyTo(outputBytes[bytesWritten..]); bytesWritten += translatedKeyword.Length; outputBytes[bytesWritten++] = 0; - textBytes.CopyTo(outputBytes.Slice(bytesWritten)); + textBytes.CopyTo(outputBytes[bytesWritten..]); this.WriteChunk(stream, PngChunkType.InternationalText, outputBytes); } } @@ -816,7 +816,7 @@ namespace SixLabors.ImageSharp.Formats.Png int bytesWritten = textData.Keyword.Length; outputBytes[bytesWritten++] = 0; // Null separator. outputBytes[bytesWritten++] = 0; // Compression. - compressedData.CopyTo(outputBytes.Slice(bytesWritten)); + compressedData.CopyTo(outputBytes[bytesWritten..]); this.WriteChunk(stream, PngChunkType.CompressedText, outputBytes); } } @@ -830,7 +830,7 @@ namespace SixLabors.ImageSharp.Formats.Png PngConstants.Encoding.GetBytes(textData.Keyword).CopyTo(outputBytes); int bytesWritten = textData.Keyword.Length; outputBytes[bytesWritten++] = 0; - PngConstants.Encoding.GetBytes(textData.Value).CopyTo(outputBytes.Slice(bytesWritten)); + PngConstants.Encoding.GetBytes(textData.Value).CopyTo(outputBytes[bytesWritten..]); this.WriteChunk(stream, PngChunkType.Text, outputBytes); } } diff --git a/src/ImageSharp/Formats/Png/PngHeader.cs b/src/ImageSharp/Formats/Png/PngHeader.cs index da747a1897..0837e00631 100644 --- a/src/ImageSharp/Formats/Png/PngHeader.cs +++ b/src/ImageSharp/Formats/Png/PngHeader.cs @@ -116,7 +116,7 @@ namespace SixLabors.ImageSharp.Formats.Png /// The buffer to write to. public void WriteTo(Span buffer) { - BinaryPrimitives.WriteInt32BigEndian(buffer.Slice(0, 4), this.Width); + BinaryPrimitives.WriteInt32BigEndian(buffer[..4], this.Width); BinaryPrimitives.WriteInt32BigEndian(buffer.Slice(4, 4), this.Height); buffer[8] = this.BitDepth; @@ -134,7 +134,7 @@ namespace SixLabors.ImageSharp.Formats.Png public static PngHeader Parse(ReadOnlySpan data) { return new PngHeader( - width: BinaryPrimitives.ReadInt32BigEndian(data.Slice(0, 4)), + width: BinaryPrimitives.ReadInt32BigEndian(data[..4]), height: BinaryPrimitives.ReadInt32BigEndian(data.Slice(4, 4)), bitDepth: data[8], colorType: (PngColorType)data[9], diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index eaf4e63a0a..eabf14768b 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -772,7 +772,7 @@ namespace SixLabors.ImageSharp.Formats.Tga int bufferIdx = uncompressedPixels * bytesPerPixel; for (int i = 0; i < runLength + 1; i++, uncompressedPixels++) { - pixel.AsSpan().CopyTo(buffer.Slice(bufferIdx)); + pixel.AsSpan().CopyTo(buffer[bufferIdx..]); bufferIdx += bytesPerPixel; } } @@ -784,7 +784,7 @@ namespace SixLabors.ImageSharp.Formats.Tga for (int i = 0; i < runLength + 1; i++, uncompressedPixels++) { this.currentStream.Read(pixel, 0, bytesPerPixel); - pixel.AsSpan().CopyTo(buffer.Slice(bufferIdx)); + pixel.AsSpan().CopyTo(buffer[bufferIdx..]); bufferIdx += bytesPerPixel; } } diff --git a/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsCompressor.cs b/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsCompressor.cs index 01613222e3..472ceb21e4 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsCompressor.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsCompressor.cs @@ -39,7 +39,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors { Span row = rows.Slice(i * this.BytesPerRow, this.BytesPerRow); int size = PackBitsWriter.PackBits(row, span); - this.Output.Write(span.Slice(0, size)); + this.Output.Write(span[..size]); } } diff --git a/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsWriter.cs b/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsWriter.cs index 06e2d663c9..5c9a707291 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsWriter.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Compressors/PackBitsWriter.cs @@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors int literalRunStart = end - literalRunLength; sbyte runLength = (sbyte)(literalRunLength - 1); compressedRowSpan[compressedRowPos] = (byte)runLength; - rowSpan.Slice(literalRunStart, literalRunLength).CopyTo(compressedRowSpan.Slice(compressedRowPos + 1)); + rowSpan.Slice(literalRunStart, literalRunLength).CopyTo(compressedRowSpan[(compressedRowPos + 1)..]); } private static void WriteRun(ReadOnlySpan rowSpan, int start, int runLength, Span compressedRowSpan, int compressedRowPos) diff --git a/src/ImageSharp/Formats/Tiff/Compression/Compressors/TiffCcittCompressor.cs b/src/ImageSharp/Formats/Tiff/Compression/Compressors/TiffCcittCompressor.cs index 511c6914f3..b1f5c85d9e 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Compressors/TiffCcittCompressor.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Compressors/TiffCcittCompressor.cs @@ -511,7 +511,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Compressors // Write the compressed data to the stream. int bytesToWrite = this.bitPosition != 0 ? this.bytePosition + 1 : this.bytePosition; - this.Output.Write(compressedData.Slice(0, bytesToWrite)); + this.Output.Write(compressedData[..bytesToWrite]); } /// diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/CcittReferenceScanline.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/CcittReferenceScanline.cs index bb2828b912..1b66cbf80b 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/CcittReferenceScanline.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/CcittReferenceScanline.cs @@ -100,7 +100,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors offset = a0; } - ReadOnlySpan searchSpace = this.scanLine.Slice(offset); + ReadOnlySpan searchSpace = this.scanLine[offset..]; byte searchByte = (byte)~a0Byte; int index = searchSpace.IndexOf(searchByte); if (index < 0) @@ -120,7 +120,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors return this.scanLine.Length; } - searchSpace = searchSpace.Slice(index); + searchSpace = searchSpace[index..]; offset += index; index = searchSpace.IndexOf((byte)~searchByte); if (index < 0) @@ -143,7 +143,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors byte searchByte = (byte)~this.scanLine[b1]; int offset = b1 + 1; - ReadOnlySpan searchSpace = this.scanLine.Slice(offset); + ReadOnlySpan searchSpace = this.scanLine[offset..]; int index = searchSpace.IndexOf(searchByte); if (index == -1) { diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs index 0d9a6ba139..4a2909fdf2 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/JpegTiffCompression.cs @@ -105,7 +105,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors { Span pixelRowSpan = pixelBuffer.DangerousGetRowSpan(y); Span rgbBytes = MemoryMarshal.AsBytes(pixelRowSpan); - rgbBytes.CopyTo(buffer.Slice(offset)); + rgbBytes.CopyTo(buffer[offset..]); offset += rgbBytes.Length; } } @@ -117,7 +117,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors { Span pixelRowSpan = pixelBuffer.DangerousGetRowSpan(y); Span rgbBytes = MemoryMarshal.AsBytes(pixelRowSpan); - rgbBytes.CopyTo(buffer.Slice(offset)); + rgbBytes.CopyTo(buffer[offset..]); offset += rgbBytes.Length; } } diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/PackBitsTiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/PackBitsTiffCompression.cs index 9df331b6ea..c3ba82e8f7 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/PackBitsTiffCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/PackBitsTiffCompression.cs @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors TiffThrowHelper.ThrowImageFormatException("Tiff packbits compression error: not enough data."); } - compressedData.Slice(literalOffset, literalLength).CopyTo(buffer.Slice(decompressedOffset)); + compressedData.Slice(literalOffset, literalLength).CopyTo(buffer[decompressedOffset..]); compressedOffset += literalLength + 1; decompressedOffset += literalLength; diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs index 2373e7b9be..41bea9de88 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/T6TiffCompression.cs @@ -55,7 +55,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors buffer.Clear(); using System.Buffers.IMemoryOwner scanLineBuffer = this.Allocator.Allocate(this.width * 2); - Span scanLine = scanLineBuffer.GetSpan().Slice(0, this.width); + Span scanLine = scanLineBuffer.GetSpan()[..this.width]; Span referenceScanLineSpan = scanLineBuffer.GetSpan().Slice(this.width, this.width); var bitReader = new T6BitReader(stream, this.FillOrder, byteCount); @@ -154,7 +154,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors case CcittTwoDimensionalCodeType.Pass: int b2 = referenceScanline.FindB2(b1); - scanline.Slice(unpacked, b2 - unpacked).Fill(fillByte); + scanline[unpacked..b2].Fill(fillByte); unpacked = b2; a0 = b2; break; @@ -189,7 +189,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors case CcittTwoDimensionalCodeType.Vertical0: a1 = b1; - scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte); + scanline[unpacked..a1].Fill(fillByte); unpacked = a1; a0 = a1; fillByte = (byte)~fillByte; @@ -198,7 +198,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors case CcittTwoDimensionalCodeType.VerticalR1: a1 = b1 + 1; - scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte); + scanline[unpacked..a1].Fill(fillByte); unpacked = a1; a0 = a1; fillByte = (byte)~fillByte; @@ -207,7 +207,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors case CcittTwoDimensionalCodeType.VerticalR2: a1 = b1 + 2; - scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte); + scanline[unpacked..a1].Fill(fillByte); unpacked = a1; a0 = a1; fillByte = (byte)~fillByte; @@ -216,7 +216,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors case CcittTwoDimensionalCodeType.VerticalR3: a1 = b1 + 3; - scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte); + scanline[unpacked..a1].Fill(fillByte); unpacked = a1; a0 = a1; fillByte = (byte)~fillByte; @@ -225,7 +225,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors case CcittTwoDimensionalCodeType.VerticalL1: a1 = b1 - 1; - scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte); + scanline[unpacked..a1].Fill(fillByte); unpacked = a1; a0 = a1; fillByte = (byte)~fillByte; @@ -234,7 +234,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors case CcittTwoDimensionalCodeType.VerticalL2: a1 = b1 - 2; - scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte); + scanline[unpacked..a1].Fill(fillByte); unpacked = a1; a0 = a1; fillByte = (byte)~fillByte; @@ -243,7 +243,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors case CcittTwoDimensionalCodeType.VerticalL3: a1 = b1 - 3; - scanline.Slice(unpacked, a1 - unpacked).Fill(fillByte); + scanline[unpacked..a1].Fill(fillByte); unpacked = a1; a0 = a1; fillByte = (byte)~fillByte; diff --git a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/WebpTiffCompression.cs b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/WebpTiffCompression.cs index 0d63382ff1..5a70baa090 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/Decompressors/WebpTiffCompression.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/Decompressors/WebpTiffCompression.cs @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression.Decompressors { Span pixelRowSpan = pixelBuffer.DangerousGetRowSpan(y); Span rgbBytes = MemoryMarshal.AsBytes(pixelRowSpan); - rgbBytes.CopyTo(buffer.Slice(offset)); + rgbBytes.CopyTo(buffer[offset..]); offset += rgbBytes.Length; } } diff --git a/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs b/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs index ab8f51844c..406d6ef743 100644 --- a/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs +++ b/src/ImageSharp/Formats/Tiff/Compression/HorizontalPredictor.cs @@ -235,7 +235,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression for (int y = 0; y < height; y++) { Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); - Span rowRgb = MemoryMarshal.Cast(rowBytes).Slice(0, width); + Span rowRgb = MemoryMarshal.Cast(rowBytes)[..width]; ref Rgb24 rowRgbBase = ref MemoryMarshal.GetReference(rowRgb); byte r = rowRgbBase.R; byte g = rowRgbBase.G; @@ -260,7 +260,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Compression for (int y = 0; y < height; y++) { Span rowBytes = pixelBytes.Slice(y * rowBytesCount, rowBytesCount); - Span rowRgb = MemoryMarshal.Cast(rowBytes).Slice(0, width); + Span rowRgb = MemoryMarshal.Cast(rowBytes)[..width]; ref Rgba32 rowRgbBase = ref MemoryMarshal.GetReference(rowRgb); byte r = rowRgbBase.R; byte g = rowRgbBase.G; diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs index e9be797b4b..d68d70e9b1 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/BlackIsZero24TiffColor{TPixel}.cs @@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation Span buffer = stackalloc byte[4]; int bufferStartIdx = this.isBigEndian ? 1 : 0; - Span bufferSpan = buffer.Slice(bufferStartIdx); + Span bufferSpan = buffer[bufferStartIdx..]; int offset = 0; for (int y = top; y < top + height; y++) { diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb242424TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb242424TiffColor{TPixel}.cs index b74c43cebd..10c61a6d0e 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb242424TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb242424TiffColor{TPixel}.cs @@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation Span buffer = stackalloc byte[4]; int bufferStartIdx = this.isBigEndian ? 1 : 0; - Span bufferSpan = buffer.Slice(bufferStartIdx); + Span bufferSpan = buffer[bufferStartIdx..]; for (int y = top; y < top + height; y++) { Span pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb24PlanarTiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb24PlanarTiffColor{TPixel}.cs index 94ba69785e..0746610951 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb24PlanarTiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgb24PlanarTiffColor{TPixel}.cs @@ -35,7 +35,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation Span redData = data[0].GetSpan(); Span greenData = data[1].GetSpan(); Span blueData = data[2].GetSpan(); - Span bufferSpan = buffer.Slice(bufferStartIdx); + Span bufferSpan = buffer[bufferStartIdx..]; int offset = 0; for (int y = top; y < top + height; y++) diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24242424TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24242424TiffColor{TPixel}.cs index ab5a21ac2c..be10495e75 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24242424TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24242424TiffColor{TPixel}.cs @@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation Span buffer = stackalloc byte[4]; int bufferStartIdx = this.isBigEndian ? 1 : 0; - Span bufferSpan = buffer.Slice(bufferStartIdx); + Span bufferSpan = buffer[bufferStartIdx..]; for (int y = top; y < top + height; y++) { Span pixelRow = pixels.DangerousGetRowSpan(y).Slice(left, width); diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24PlanarTiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24PlanarTiffColor{TPixel}.cs index 47c2ae5ad3..45f530d627 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24PlanarTiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/Rgba24PlanarTiffColor{TPixel}.cs @@ -43,7 +43,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation Span greenData = data[1].GetSpan(); Span blueData = data[2].GetSpan(); Span alphaData = data[3].GetSpan(); - Span bufferSpan = buffer.Slice(bufferStartIdx); + Span bufferSpan = buffer[bufferStartIdx..]; bool hasAssociatedAlpha = this.extraSamplesType.HasValue && this.extraSamplesType == TiffExtraSampleType.AssociatedAlphaData; int offset = 0; diff --git a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero24TiffColor{TPixel}.cs b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero24TiffColor{TPixel}.cs index 2caede066a..36832a198a 100644 --- a/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero24TiffColor{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/PhotometricInterpretation/WhiteIsZero24TiffColor{TPixel}.cs @@ -32,7 +32,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation int bufferStartIdx = this.isBigEndian ? 1 : 0; const uint maxValue = 0xFFFFFF; - Span bufferSpan = buffer.Slice(bufferStartIdx); + Span bufferSpan = buffer[bufferStartIdx..]; int offset = 0; for (int y = top; y < top + height; y++) { diff --git a/src/ImageSharp/Formats/Tiff/Writers/TiffBiColorWriter{TPixel}.cs b/src/ImageSharp/Formats/Tiff/Writers/TiffBiColorWriter{TPixel}.cs index 8d9c6c2b4e..926dbf1c0f 100644 --- a/src/ImageSharp/Formats/Tiff/Writers/TiffBiColorWriter{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/Writers/TiffBiColorWriter{TPixel}.cs @@ -55,7 +55,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers grayRowIdx++; } - compressor.CompressStrip(pixelAsGraySpan.Slice(0, stripPixels), height); + compressor.CompressStrip(pixelAsGraySpan[..stripPixels], height); }); } else @@ -76,7 +76,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers { int bitIndex = 0; int byteIndex = 0; - Span outputRow = rows.Slice(outputRowIdx * this.BytesPerRow); + Span outputRow = rows[(outputRowIdx * this.BytesPerRow)..]; Span pixelsBlackWhiteRow = blackWhiteBuffer.DangerousGetRowSpan(row); PixelOperations.Instance.ToL8Bytes(this.Configuration, pixelsBlackWhiteRow, pixelAsGraySpan, width); for (int x = 0; x < this.Image.Width; x++) diff --git a/src/ImageSharp/Formats/Tiff/Writers/TiffCompositeColorWriter{TPixel}.cs b/src/ImageSharp/Formats/Tiff/Writers/TiffCompositeColorWriter{TPixel}.cs index 19b5e5f88a..5bb39771e7 100644 --- a/src/ImageSharp/Formats/Tiff/Writers/TiffCompositeColorWriter{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/Writers/TiffCompositeColorWriter{TPixel}.cs @@ -31,7 +31,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers this.rowBuffer.Clear(); - Span outputRowSpan = this.rowBuffer.GetSpan().Slice(0, this.BytesPerRow * height); + Span outputRowSpan = this.rowBuffer.GetSpan()[..(this.BytesPerRow * height)]; int width = this.Image.Width; using IMemoryOwner stripPixelBuffer = this.MemoryAllocator.Allocate(height * width); diff --git a/src/ImageSharp/Formats/Tiff/Writers/TiffPaletteWriter{TPixel}.cs b/src/ImageSharp/Formats/Tiff/Writers/TiffPaletteWriter{TPixel}.cs index 8e123c22f9..19d071c9ed 100644 --- a/src/ImageSharp/Formats/Tiff/Writers/TiffPaletteWriter{TPixel}.cs +++ b/src/ImageSharp/Formats/Tiff/Writers/TiffPaletteWriter{TPixel}.cs @@ -83,7 +83,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers } } - compressor.CompressStrip(rows4bit.Slice(0, idx4bitRows), height); + compressor.CompressStrip(rows4bit[..idx4bitRows], height); } else { @@ -99,7 +99,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers indexedPixelsRowIdx++; } - compressor.CompressStrip(indexedPixels.Slice(0, stripPixels), height); + compressor.CompressStrip(indexedPixels[..stripPixels], height); } } @@ -119,7 +119,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers int quantizedColorBytes = quantizedColors.Length * 3 * 2; // In the ColorMap, black is represented by 0, 0, 0 and white is represented by 65535, 65535, 65535. - Span quantizedColorRgb48 = MemoryMarshal.Cast(colorPalette.Slice(0, quantizedColorBytes)); + Span quantizedColorRgb48 = MemoryMarshal.Cast(colorPalette[..quantizedColorBytes]); PixelOperations.Instance.ToRgb48(this.Configuration, quantizedColors, quantizedColorRgb48); // It can happen that the quantized colors are less than the expected maximum per channel. diff --git a/src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs b/src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs index 50af4b19d2..ae8ac31dd9 100644 --- a/src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs +++ b/src/ImageSharp/Formats/Webp/BitReader/BitReaderBase.cs @@ -30,7 +30,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitReader { this.Data = memoryAllocator.Allocate(bytesToRead); Span dataSpan = this.Data.Memory.Span; - input.Read(dataSpan.Slice(0, bytesToRead), 0, bytesToRead); + input.Read(dataSpan[..bytesToRead], 0, bytesToRead); } protected virtual void Dispose(bool disposing) diff --git a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs index ecf87d8b53..428403f606 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/BitWriterBase.cs @@ -258,9 +258,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter BinaryPrimitives.WriteUInt32LittleEndian(buf, flags); stream.Write(buf); BinaryPrimitives.WriteUInt32LittleEndian(buf, width - 1); - stream.Write(buf.Slice(0, 3)); + stream.Write(buf[..3]); BinaryPrimitives.WriteUInt32LittleEndian(buf, height - 1); - stream.Write(buf.Slice(0, 3)); + stream.Write(buf[..3]); } } } diff --git a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs index 7b9a79a2e5..81cb9cc248 100644 --- a/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs +++ b/src/ImageSharp/Formats/Webp/BitWriter/Vp8BitWriter.cs @@ -215,7 +215,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter { if (this.PutBit(s >= 2, p[0])) { - p = p.Slice(1); + p = p[1..]; } this.PutBit(s & 1, p[1]); @@ -711,7 +711,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.BitWriter Span vp8ChunkHeader = stackalloc byte[WebpConstants.ChunkHeaderSize]; WebpConstants.Vp8MagicBytes.AsSpan().CopyTo(vp8ChunkHeader); - BinaryPrimitives.WriteUInt32LittleEndian(vp8ChunkHeader.Slice(4), size); + BinaryPrimitives.WriteUInt32LittleEndian(vp8ChunkHeader[4..], size); stream.Write(vp8ChunkHeader); } diff --git a/src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs index 8c5aa43f5f..88d6261cfe 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/BackwardReferenceEncoder.cs @@ -252,7 +252,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless BackwardReferencesHashChainDistanceOnly(xSize, ySize, memoryAllocator, bgra, cacheBits, hashChain, refsSrc, distArrayBuffer); int chosenPathSize = TraceBackwards(distArray, distArraySize); - Span chosenPath = distArray.Slice(distArraySize - chosenPathSize); + Span chosenPath = distArray[(distArraySize - chosenPathSize)..]; BackwardReferencesHashChainFollowChosenPath(bgra, cacheBits, chosenPath, chosenPathSize, hashChain, refsDst); } @@ -741,8 +741,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless while (i < pixelCount) { int maxLen = LosslessUtils.MaxFindCopyLength(pixelCount - i); - int rleLen = LosslessUtils.FindMatchLength(bgra.Slice(i), bgra.Slice(i - 1), 0, maxLen); - int prevRowLen = i < xSize ? 0 : LosslessUtils.FindMatchLength(bgra.Slice(i), bgra.Slice(i - xSize), 0, maxLen); + int rleLen = LosslessUtils.FindMatchLength(bgra[i..], bgra[(i - 1)..], 0, maxLen); + int prevRowLen = i < xSize ? 0 : LosslessUtils.FindMatchLength(bgra[i..], bgra[(i - xSize)..], 0, maxLen); if (rleLen >= prevRowLen && rleLen >= MinLength) { refs.Add(PixOrCopy.CreateCopy(1, (ushort)rleLen)); diff --git a/src/ImageSharp/Formats/Webp/Lossless/HuffmanUtils.cs b/src/ImageSharp/Formats/Webp/Lossless/HuffmanUtils.cs index 7bd206391b..515ae7a7d9 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/HuffmanUtils.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/HuffmanUtils.cs @@ -402,7 +402,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless BitsUsed = len, Value = (uint)sorted[symbol++] }; - ReplicateValue(table.Slice(key), step, tableSize, huffmanCode); + ReplicateValue(table[key..], step, tableSize, huffmanCode); key = GetNextKey(key, len); } @@ -426,7 +426,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless { if ((key & mask) != low) { - tableSpan = tableSpan.Slice(tableSize); + tableSpan = tableSpan[tableSize..]; tablePos += tableSize; tableBits = NextTableBitSize(counts, len, rootBits); tableSize = 1 << tableBits; @@ -444,7 +444,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless BitsUsed = len - rootBits, Value = (uint)sorted[symbol++] }; - ReplicateValue(tableSpan.Slice(key >> rootBits), step, tableSize, huffmanCode); + ReplicateValue(tableSpan[(key >> rootBits)..], step, tableSize, huffmanCode); key = GetNextKey(key, len); } } @@ -576,8 +576,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless { if (tree[0].PoolIndexLeft >= 0) { - SetBitDepths(pool.Slice(tree[0].PoolIndexLeft), pool, bitDepths, level + 1); - SetBitDepths(pool.Slice(tree[0].PoolIndexRight), pool, bitDepths, level + 1); + SetBitDepths(pool[tree[0].PoolIndexLeft..], pool, bitDepths, level + 1); + SetBitDepths(pool[tree[0].PoolIndexRight..], pool, bitDepths, level + 1); } else { diff --git a/src/ImageSharp/Formats/Webp/Lossless/NearLosslessEnc.cs b/src/ImageSharp/Formats/Webp/Lossless/NearLosslessEnc.cs index db1273f4e7..b2753ab223 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/NearLosslessEnc.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/NearLosslessEnc.cs @@ -44,7 +44,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless Span prevRow = copyBuffer; Span currRow = copyBuffer.Slice(xSize, xSize); Span nextRow = copyBuffer.Slice(xSize * 2, xSize); - argbSrc.Slice(0, xSize).CopyTo(currRow); + argbSrc[..xSize].CopyTo(currRow); argbSrc.Slice(xSize, xSize).CopyTo(nextRow); int srcOffset = 0; diff --git a/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs index e2dd8c644d..1383eba78d 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/PredictorEncoder.cs @@ -230,8 +230,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless // to allow the top right pixel to point to the leftmost pixel of the next row // when at the right edge. Span upperRow = argbScratch; - Span currentRow = upperRow.Slice(width + 1); - Span maxDiffs = MemoryMarshal.Cast(currentRow.Slice(width + 1)); + Span currentRow = upperRow[(width + 1)..]; + Span maxDiffs = MemoryMarshal.Cast(currentRow[(width + 1)..]); float bestDiff = MaxDiffCost; int bestMode = 0; uint[] residuals = new uint[1 << WebpConstants.MaxTransformBits]; @@ -250,7 +250,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless // in all cases (wrapping to the leftmost pixel of the next row if it does // not exist). Span src = argb.Slice(((startY - 1) * width) + contextStartX, maxX + haveLeft + 1); - Span dst = currentRow.Slice(contextStartX); + Span dst = currentRow[contextStartX..]; src.CopyTo(dst); } @@ -267,14 +267,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless // not exist in the currentRow). int offset = (y * width) + contextStartX; Span src = argb.Slice(offset, maxX + haveLeft + (y + 1 < height ? 1 : 0)); - Span dst = currentRow.Slice(contextStartX); + Span dst = currentRow[contextStartX..]; src.CopyTo(dst); if (nearLossless) { if (maxQuantization > 1 && y >= 1 && y + 1 < height) { - MaxDiffsForRow(contextWidth, width, argb, offset, maxDiffs.Slice(contextStartX), usedSubtractGreen); + MaxDiffsForRow(contextWidth, width, argb, offset, maxDiffs[contextStartX..], usedSubtractGreen); } } @@ -589,10 +589,10 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless // to allow the top right pixel to point to the leftmost pixel of the next row // when at the right edge. Span upperRow = argbScratch; - Span currentRow = upperRow.Slice(width + 1); - Span currentMaxDiffs = MemoryMarshal.Cast(currentRow.Slice(width + 1)); + Span currentRow = upperRow[(width + 1)..]; + Span currentMaxDiffs = MemoryMarshal.Cast(currentRow[(width + 1)..]); - Span lowerMaxDiffs = currentMaxDiffs.Slice(width); + Span lowerMaxDiffs = currentMaxDiffs[width..]; Span scratch = stackalloc short[8]; for (int y = 0; y < height; y++) { @@ -604,7 +604,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless if (lowEffort) { - PredictBatch(PredLowEffort, 0, y, width, currentRow, upperRow, argb.Slice(y * width), scratch); + PredictBatch(PredLowEffort, 0, y, width, currentRow, upperRow, argb[(y * width)..], scratch); } else { @@ -645,7 +645,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless transparentColorMode, usedSubtractGreen, nearLossless, - argb.Slice((y * width) + x), + argb[((y * width) + x)..], scratch); x = xEnd; @@ -820,14 +820,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless { int xScan = GetMin(maxTileSize, xSize - tileX); int yScan = GetMin(maxTileSize, ySize - tileY); - argb = argb.Slice((tileY * xSize) + tileX); + argb = argb[((tileY * xSize) + tileX)..]; while (yScan-- > 0) { LosslessUtils.TransformColor(colorTransform, argb, xScan); if (argb.Length > xSize) { - argb = argb.Slice(xSize); + argb = argb[xSize..]; } } } @@ -853,7 +853,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless int allYMax = GetMin(tileYOffset + maxTileSize, ySize); int tileWidth = allXMax - tileXOffset; int tileHeight = allYMax - tileYOffset; - Span tileArgb = argb.Slice((tileYOffset * xSize) + tileXOffset); + Span tileArgb = argb[((tileYOffset * xSize) + tileXOffset)..]; var bestTx = default(Vp8LMultipliers); @@ -954,7 +954,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless int greenToRed, int[] accumulatedRedHisto) { - Span histo = scratch.Slice(0, 256); + Span histo = scratch[..256]; histo.Clear(); ColorSpaceTransformUtils.CollectColorRedTransforms(argb, stride, tileWidth, tileHeight, greenToRed, histo); @@ -992,7 +992,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless int redToBlue, int[] accumulatedBlueHisto) { - Span histo = scratch.Slice(0, 256); + Span histo = scratch[..256]; histo.Clear(); ColorSpaceTransformUtils.CollectColorBlueTransforms(argb, stride, tileWidth, tileHeight, greenToBlue, redToBlue, histo); diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs index f2b855a37b..473c92d8de 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LEncoder.cs @@ -1105,24 +1105,24 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless AddSingle( pix, - histo.Slice((int)HistoIx.HistoAlpha * 256), - histo.Slice((int)HistoIx.HistoRed * 256), - histo.Slice((int)HistoIx.HistoGreen * 256), - histo.Slice((int)HistoIx.HistoBlue * 256)); + histo[..], + histo[((int)HistoIx.HistoRed * 256)..], + histo[((int)HistoIx.HistoGreen * 256)..], + histo[((int)HistoIx.HistoBlue * 256)..]); AddSingle( pixDiff, - histo.Slice((int)HistoIx.HistoAlphaPred * 256), - histo.Slice((int)HistoIx.HistoRedPred * 256), - histo.Slice((int)HistoIx.HistoGreenPred * 256), - histo.Slice((int)HistoIx.HistoBluePred * 256)); + histo[((int)HistoIx.HistoAlphaPred * 256)..], + histo[((int)HistoIx.HistoRedPred * 256)..], + histo[((int)HistoIx.HistoGreenPred * 256)..], + histo[((int)HistoIx.HistoBluePred * 256)..]); AddSingleSubGreen( pix, - histo.Slice((int)HistoIx.HistoRedSubGreen * 256), - histo.Slice((int)HistoIx.HistoBlueSubGreen * 256)); + histo[((int)HistoIx.HistoRedSubGreen * 256)..], + histo[((int)HistoIx.HistoBlueSubGreen * 256)..]); AddSingleSubGreen( pixDiff, - histo.Slice((int)HistoIx.HistoRedPredSubGreen * 256), - histo.Slice((int)HistoIx.HistoBluePredSubGreen * 256)); + histo[((int)HistoIx.HistoRedPredSubGreen * 256)..], + histo[((int)HistoIx.HistoBluePredSubGreen * 256)..]); // Approximate the palette by the entropy of the multiplicative hash. uint hash = HashPix(pix); @@ -1213,8 +1213,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless new[] { (byte)HistoIx.HistoRedPredSubGreen, (byte)HistoIx.HistoBluePredSubGreen }, new[] { (byte)HistoIx.HistoRed, (byte)HistoIx.HistoBlue } }; - Span redHisto = histo.Slice(256 * histoPairs[(int)minEntropyIx][0]); - Span blueHisto = histo.Slice(256 * histoPairs[(int)minEntropyIx][1]); + Span redHisto = histo[(256 * histoPairs[(int)minEntropyIx][0])..]; + Span blueHisto = histo[(256 * histoPairs[(int)minEntropyIx][1])..]; for (int i = 1; i < 256; i++) { if ((redHisto[i] | blueHisto[i]) != 0) @@ -1245,14 +1245,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless return false; } -#if NET5_0_OR_GREATER - var paletteSlice = palette.Slice(0, this.PaletteSize); + Span paletteSlice = palette[..this.PaletteSize]; paletteSlice.Sort(); -#else - uint[] paletteArray = palette.Slice(0, this.PaletteSize).ToArray(); - Array.Sort(paletteArray); - paletteArray.CopyTo(palette); -#endif if (PaletteHasNonMonotonousDeltas(palette, this.PaletteSize)) { @@ -1352,8 +1346,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless } BundleColorMap(tmpRow, width, xBits, dst); - src = src.Slice(srcStride); - dst = dst.Slice(dstStride); + src = src[srcStride..]; + dst = dst[dstStride..]; } } else @@ -1449,8 +1443,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless LosslessUtils.BundleColorMap(tmpRow, width, xBits, dst); - src = src.Slice(srcStride); - dst = dst.Slice(dstStride); + src = src[srcStride..]; + dst = dst[dstStride..]; } } @@ -1474,8 +1468,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless LosslessUtils.BundleColorMap(tmpRow, width, xBits, dst); - src = src.Slice(srcStride); - dst = dst.Slice(dstStride); + src = src[srcStride..]; + dst = dst[dstStride..]; } } @@ -1484,7 +1478,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless /// private static void PrepareMapToPalette(Span palette, int numColors, uint[] sorted, uint[] idxMap) { - palette.Slice(0, numColors).CopyTo(sorted); + palette[..numColors].CopyTo(sorted); Array.Sort(sorted, PaletteCompareColorsForSort); for (int i = 0; i < numColors; i++) { diff --git a/src/ImageSharp/Formats/Webp/Lossless/Vp8LHashChain.cs b/src/ImageSharp/Formats/Webp/Lossless/Vp8LHashChain.cs index eea1dd363a..a496719179 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/Vp8LHashChain.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/Vp8LHashChain.cs @@ -125,7 +125,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless else { // Just move one pixel forward. - hashCode = GetPixPairHash64(bgra.Slice(pos)); + hashCode = GetPixPairHash64(bgra[pos..]); chain[pos] = hashToFirstIndex[(int)hashCode]; hashToFirstIndex[(int)hashCode] = pos++; bgraComp = bgraCompNext; @@ -133,7 +133,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless } // Process the penultimate pixel. - chain[pos] = hashToFirstIndex[(int)GetPixPairHash64(bgra.Slice(pos))]; + chain[pos] = hashToFirstIndex[(int)GetPixPairHash64(bgra[pos..])]; // Find the best match interval at each pixel, defined by an offset to the // pixel and a length. The right-most pixel cannot match anything to the right @@ -157,7 +157,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless // Heuristic: use the comparison with the above line as an initialization. if (basePosition >= (uint)xSize) { - currLength = LosslessUtils.FindMatchLength(bgra.Slice(bgraStart - xSize), bgra.Slice(bgraStart), bestLength, maxLen); + currLength = LosslessUtils.FindMatchLength(bgra[(bgraStart - xSize)..], bgra[bgraStart..], bestLength, maxLen); if (currLength > bestLength) { bestLength = currLength; @@ -168,7 +168,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless } // Heuristic: compare to the previous pixel. - currLength = LosslessUtils.FindMatchLength(bgra.Slice(bgraStart - 1), bgra.Slice(bgraStart), bestLength, maxLen); + currLength = LosslessUtils.FindMatchLength(bgra[(bgraStart - 1)..], bgra[bgraStart..], bestLength, maxLen); if (currLength > bestLength) { bestLength = currLength; @@ -184,7 +184,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless } } - uint bestBgra = bgra.Slice(bgraStart)[bestLength]; + uint bestBgra = bgra[bgraStart..][bestLength]; for (; pos >= minPos && (--iter > 0); pos = chain[pos]) { @@ -193,12 +193,12 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless continue; } - currLength = LosslessUtils.VectorMismatch(bgra.Slice(pos), bgra.Slice(bgraStart), maxLen); + currLength = LosslessUtils.VectorMismatch(bgra[pos..], bgra[bgraStart..], maxLen); if (bestLength < currLength) { bestLength = currLength; bestDistance = (uint)(basePosition - pos); - bestBgra = bgra.Slice(bgraStart)[bestLength]; + bestBgra = bgra[bgraStart..][bestLength]; // Stop if we have reached a good enough length. if (bestLength >= lengthMax) diff --git a/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs b/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs index ff2b808dd7..fafd5b49ca 100644 --- a/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossless/WebpLosslessDecoder.cs @@ -199,8 +199,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless Span pixelRow = pixels.DangerousGetRowSpan(y); PixelOperations.Instance.FromBgra32Bytes( this.configuration, - rowAsBytes.Slice(0, bytesPerRow), - pixelRow.Slice(0, width), + rowAsBytes[..bytesPerRow], + pixelRow[..width], width); } } @@ -441,7 +441,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless } // TODO: Avoid allocation. - hTreeGroup.HTrees.Add(huffmanTable.Slice(0, size).ToArray()); + hTreeGroup.HTrees.Add(huffmanTable[..size].ToArray()); HuffmanCode huffTableZero = huffmanTable[0]; if (isTrivialLiteral && LiteralMap[j] == 1) @@ -450,7 +450,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless } totalSize += huffTableZero.BitsUsed; - huffmanTable = huffmanTable.Slice(size); + huffmanTable = huffmanTable[size..]; if (j <= HuffIndex.Alpha) { @@ -862,14 +862,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless private uint ReadSymbol(Span table) { uint val = (uint)this.bitReader.PrefetchBits(); - Span tableSpan = table.Slice((int)(val & HuffmanUtils.HuffmanTableMask)); + Span tableSpan = table[(int)(val & HuffmanUtils.HuffmanTableMask)..]; int nBits = tableSpan[0].BitsUsed - HuffmanUtils.HuffmanTableBits; if (nBits > 0) { this.bitReader.AdvanceBitPosition(HuffmanUtils.HuffmanTableBits); val = (uint)this.bitReader.PrefetchBits(); - tableSpan = tableSpan.Slice((int)tableSpan[0].Value); - tableSpan = tableSpan.Slice((int)val & ((1 << nBits) - 1)); + tableSpan = tableSpan[(int)tableSpan[0].Value..]; + tableSpan = tableSpan[((int)val & ((1 << nBits) - 1))..]; } this.bitReader.AdvanceBitPosition(tableSpan[0].BitsUsed); @@ -949,14 +949,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless { // no overlap. Span src = pixelData.Slice(start, length); - Span dest = pixelData.Slice(decodedPixels); + Span dest = pixelData[decodedPixels..]; src.CopyTo(dest); } else { // There is overlap between the backward reference distance and the pixels to copy. - Span src = pixelData.Slice(start); - Span dest = pixelData.Slice(decodedPixels); + Span src = pixelData[start..]; + Span dest = pixelData[decodedPixels..]; for (int i = 0; i < length; i++) { dest[i] = src[i]; @@ -977,12 +977,12 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossless if (dist >= length) { // no overlap. - data.Slice(pos - dist, length).CopyTo(data.Slice(pos)); + data.Slice(pos - dist, length).CopyTo(data[pos..]); } else { - Span dst = data.Slice(pos); - Span src = data.Slice(pos - dist); + Span dst = data[pos..]; + Span src = data[(pos - dist)..]; for (int i = 0; i < length; i++) { dst[i] = src[i]; diff --git a/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs b/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs index 4d60f0ae3d..c169ba6f9e 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/QuantEnc.cs @@ -124,7 +124,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy Span tmpLevels = new short[16]; do { - int numBlocks = 1; + const int numBlocks = 1; rdi4.Clear(); int mode; int bestMode = -1; diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs index 25894905a7..c1dedeba45 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8EncIterator.cs @@ -288,9 +288,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy { int yStartIdx = ((this.Y * yStride) + this.X) * 16; int uvStartIdx = ((this.Y * uvStride) + this.X) * 8; - Span ySrc = y.Slice(yStartIdx); - Span uSrc = u.Slice(uvStartIdx); - Span vSrc = v.Slice(uvStartIdx); + Span ySrc = y[yStartIdx..]; + Span uSrc = u[uvStartIdx..]; + Span vSrc = v[uvStartIdx..]; int w = Math.Min(width - (this.X * 16), 16); int h = Math.Min(height - (this.Y * 16), 16); int uvw = (w + 1) >> 1; @@ -331,9 +331,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy vLeft[0] = v[uvStartIdx - 1 - uvStride]; } - this.ImportLine(y.Slice(yStartIdx - 1), yStride, yLeft.Slice(1), h, 16); - this.ImportLine(u.Slice(uvStartIdx - 1), uvStride, uLeft.Slice(1), uvh, 8); - this.ImportLine(v.Slice(uvStartIdx - 1), uvStride, vLeft.Slice(1), uvh, 8); + this.ImportLine(y[(yStartIdx - 1)..], yStride, yLeft[1..], h, 16); + this.ImportLine(u[(uvStartIdx - 1)..], uvStride, uLeft[1..], uvh, 8); + this.ImportLine(v[(uvStartIdx - 1)..], uvStride, vLeft[1..], uvh, 8); } Span yTop = this.YTop.AsSpan(this.yTopIdx, 16); @@ -344,9 +344,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy } else { - this.ImportLine(y.Slice(yStartIdx - yStride), 1, yTop, w, 16); - this.ImportLine(u.Slice(uvStartIdx - uvStride), 1, this.UvTop.AsSpan(this.uvTopIdx, 8), uvw, 8); - this.ImportLine(v.Slice(uvStartIdx - uvStride), 1, this.UvTop.AsSpan(this.uvTopIdx + 8, 8), uvw, 8); + this.ImportLine(y[(yStartIdx - yStride)..], 1, yTop, w, 16); + this.ImportLine(u[(uvStartIdx - uvStride)..], 1, this.UvTop.AsSpan(this.uvTopIdx, 8), uvw, 8); + this.ImportLine(v[(uvStartIdx - uvStride)..], 1, this.UvTop.AsSpan(this.uvTopIdx + 8, 8), uvw, 8); } } @@ -490,8 +490,8 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy Span preds = this.Preds.AsSpan(this.predIdx); for (int y = 0; y < 4; y++) { - preds.Slice(0, 4).Fill((byte)mode); - preds = preds.Slice(this.predsWidth); + preds[..4].Fill((byte)mode); + preds = preds[this.predsWidth..]; } this.CurrentMacroBlockInfo.MacroBlockType = Vp8MacroBlockType.I16X16; @@ -679,7 +679,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy public bool RotateI4(Span yuvOut) { - Span blk = yuvOut.Slice(WebpLookupTables.Vp8Scan[this.I4]); + Span blk = yuvOut[WebpLookupTables.Vp8Scan[this.I4]..]; Span top = this.I4Boundary.AsSpan(); int topOffset = this.I4BoundaryIdx; int i; @@ -830,7 +830,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy for (int i = 0; i < h; i++) { // memcpy(dst, src, w); - src.Slice(srcIdx, w).CopyTo(dst.Slice(dstIdx)); + src.Slice(srcIdx, w).CopyTo(dst[dstIdx..]); if (w < size) { // memset(dst + w, dst[w - 1], size - w); @@ -844,7 +844,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy for (int i = h; i < size; i++) { // memcpy(dst, dst - BPS, size); - dst.Slice(dstIdx - WebpConstants.Bps, size).CopyTo(dst.Slice(dstIdx)); + dst.Slice(dstIdx - WebpConstants.Bps, size).CopyTo(dst[dstIdx..]); dstIdx += WebpConstants.Bps; } } diff --git a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs index 8eaaeda7ac..9dbbccbc74 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/Vp8Encoder.cs @@ -382,7 +382,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy (uint)width, (uint)height, hasAlpha, - alphaData.Slice(0, alphaDataSize), + alphaData[..alphaDataSize], this.alphaCompression && alphaCompressionSucceeded); } diff --git a/src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs b/src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs index 0e789060a2..b290ee58bf 100644 --- a/src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs +++ b/src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs @@ -252,7 +252,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy } top.CopyTo(modes); - modes = modes.Slice(4); + modes = modes[4..]; left[y] = (byte)yMode; } } @@ -290,9 +290,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy const int vOff = uOff + 16; Span yuv = dec.YuvBuffer.Memory.Span; - Span yDst = yuv.Slice(yOff); - Span uDst = yuv.Slice(uOff); - Span vDst = yuv.Slice(vOff); + Span yDst = yuv[yOff..]; + Span uDst = yuv[uOff..]; + Span vDst = yuv[vOff..]; // Initialize left-most block. int end = 16 * WebpConstants.Bps; @@ -349,17 +349,17 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy { int srcIdx = (i * WebpConstants.Bps) + 12 + yOff; int dstIdx = (i * WebpConstants.Bps) - 4 + yOff; - yuv.Slice(srcIdx, 4).CopyTo(yuv.Slice(dstIdx)); + yuv.Slice(srcIdx, 4).CopyTo(yuv[dstIdx..]); } for (int i = -1; i < 8; i++) { int srcIdx = (i * WebpConstants.Bps) + 4 + uOff; int dstIdx = (i * WebpConstants.Bps) - 4 + uOff; - yuv.Slice(srcIdx, 4).CopyTo(yuv.Slice(dstIdx)); + yuv.Slice(srcIdx, 4).CopyTo(yuv[dstIdx..]); srcIdx = (i * WebpConstants.Bps) + 4 + vOff; dstIdx = (i * WebpConstants.Bps) - 4 + vOff; - yuv.Slice(srcIdx, 4).CopyTo(yuv.Slice(dstIdx)); + yuv.Slice(srcIdx, 4).CopyTo(yuv[dstIdx..]); } } @@ -369,15 +369,15 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy uint bits = block.NonZeroY; if (mby > 0) { - topYuv.Y.CopyTo(yuv.Slice(yOff - WebpConstants.Bps)); - topYuv.U.CopyTo(yuv.Slice(uOff - WebpConstants.Bps)); - topYuv.V.CopyTo(yuv.Slice(vOff - WebpConstants.Bps)); + topYuv.Y.CopyTo(yuv[(yOff - WebpConstants.Bps)..]); + topYuv.U.CopyTo(yuv[(uOff - WebpConstants.Bps)..]); + topYuv.V.CopyTo(yuv[(vOff - WebpConstants.Bps)..]); } // Predict and add residuals. if (block.IsI4x4) { - Span topRight = yuv.Slice(yOff - WebpConstants.Bps + 16); + Span topRight = yuv[(yOff - WebpConstants.Bps + 16)..]; if (mby > 0) { if (mbx >= dec.MbWidth - 1) @@ -396,14 +396,14 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy } // Replicate the top-right pixels below. - Span topRightUint = MemoryMarshal.Cast(yuv.Slice(yOff - WebpConstants.Bps + 16)); + Span topRightUint = MemoryMarshal.Cast(yuv[(yOff - WebpConstants.Bps + 16)..]); topRightUint[WebpConstants.Bps] = topRightUint[2 * WebpConstants.Bps] = topRightUint[3 * WebpConstants.Bps] = topRightUint[0]; // Predict and add residuals for all 4x4 blocks in turn. for (int n = 0; n < 16; ++n, bits <<= 2) { int offset = yOff + WebpConstants.Scan[n]; - Span dst = yuv.Slice(offset); + Span dst = yuv[offset..]; byte lumaMode = block.Modes[n]; switch (lumaMode) { @@ -475,7 +475,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy { for (int n = 0; n < 16; ++n, bits <<= 2) { - this.DoTransform(bits, coeffs.AsSpan(n * 16), yDst.Slice(WebpConstants.Scan[n]), this.scratch); + this.DoTransform(bits, coeffs.AsSpan(n * 16), yDst[WebpConstants.Scan[n]..], this.scratch); } } } @@ -527,19 +527,19 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy } // Transfer reconstructed samples from yuv_buffer cache to final destination. - Span yOut = dec.CacheY.Memory.Span.Slice(dec.CacheYOffset + (mbx * 16)); - Span uOut = dec.CacheU.Memory.Span.Slice(dec.CacheUvOffset + (mbx * 8)); - Span vOut = dec.CacheV.Memory.Span.Slice(dec.CacheUvOffset + (mbx * 8)); + Span yOut = dec.CacheY.Memory.Span[(dec.CacheYOffset + (mbx * 16))..]; + Span uOut = dec.CacheU.Memory.Span[(dec.CacheUvOffset + (mbx * 8))..]; + Span vOut = dec.CacheV.Memory.Span[(dec.CacheUvOffset + (mbx * 8))..]; for (int j = 0; j < 16; j++) { - yDst.Slice(j * WebpConstants.Bps, Math.Min(16, yOut.Length)).CopyTo(yOut.Slice(j * dec.CacheYStride)); + yDst.Slice(j * WebpConstants.Bps, Math.Min(16, yOut.Length)).CopyTo(yOut[(j * dec.CacheYStride)..]); } for (int j = 0; j < 8; j++) { int jUvStride = j * dec.CacheUvStride; - uDst.Slice(j * WebpConstants.Bps, Math.Min(8, uOut.Length)).CopyTo(uOut.Slice(jUvStride)); - vDst.Slice(j * WebpConstants.Bps, Math.Min(8, vOut.Length)).CopyTo(vOut.Slice(jUvStride)); + uDst.Slice(j * WebpConstants.Bps, Math.Min(8, uOut.Length)).CopyTo(uOut[jUvStride..]); + vDst.Slice(j * WebpConstants.Bps, Math.Min(8, vOut.Length)).CopyTo(vOut[jUvStride..]); } } } @@ -649,9 +649,9 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy } else { - io.Y = dec.CacheY.Memory.Span.Slice(dec.CacheYOffset); - io.U = dec.CacheU.Memory.Span.Slice(dec.CacheUvOffset); - io.V = dec.CacheV.Memory.Span.Slice(dec.CacheUvOffset); + io.Y = dec.CacheY.Memory.Span[dec.CacheYOffset..]; + io.U = dec.CacheU.Memory.Span[dec.CacheUvOffset..]; + io.V = dec.CacheV.Memory.Span[dec.CacheUvOffset..]; } if (!isLastRow) @@ -696,7 +696,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy int bpp = 3; int bufferStride = bpp * io.Width; int dstStartIdx = io.MbY * bufferStride; - Span dst = buf.Slice(dstStartIdx); + Span dst = buf[dstStartIdx..]; int yEnd = io.MbY + io.MbH; int mbw = io.MbW; int uvw = (mbw + 1) / 2; @@ -711,7 +711,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy else { // We can finish the left-over line from previous call. - YuvConversion.UpSample(tmpYBuffer, curY, topU, topV, curU, curV, buf.Slice(dstStartIdx - bufferStride), dst, mbw, uvBuffer); + YuvConversion.UpSample(tmpYBuffer, curY, topU, topV, curU, curV, buf[(dstStartIdx - bufferStride)..], dst, mbw, uvBuffer); numLinesOut++; } @@ -722,21 +722,21 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy { topU = curU; topV = curV; - curU = curU.Slice(io.UvStride); - curV = curV.Slice(io.UvStride); - YuvConversion.UpSample(curY.Slice(io.YStride), curY.Slice(ioStride2), topU, topV, curU, curV, dst.Slice(bufferStride), dst.Slice(bufferStride2), mbw, uvBuffer); - curY = curY.Slice(ioStride2); - dst = dst.Slice(bufferStride2); + curU = curU[io.UvStride..]; + curV = curV[io.UvStride..]; + YuvConversion.UpSample(curY[io.YStride..], curY[ioStride2..], topU, topV, curU, curV, dst[bufferStride..], dst[bufferStride2..], mbw, uvBuffer); + curY = curY[ioStride2..]; + dst = dst[bufferStride2..]; } // Move to last row. - curY = curY.Slice(io.YStride); + curY = curY[io.YStride..]; if (yEnd < io.Height) { // Save the unfinished samples for next call (as we're not done yet). - curY.Slice(0, mbw).CopyTo(tmpYBuffer); - curU.Slice(0, uvw).CopyTo(tmpUBuffer); - curV.Slice(0, uvw).CopyTo(tmpVBuffer); + curY[..mbw].CopyTo(tmpYBuffer); + curU[..uvw].CopyTo(tmpUBuffer); + curV[..uvw].CopyTo(tmpVBuffer); // The upsampler leaves a row unfinished behind (except for the very last row). numLinesOut--; @@ -746,7 +746,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy // Process the very last row of even-sized picture. if ((yEnd & 1) == 0) { - YuvConversion.UpSample(curY, default, curU, curV, curU, curV, dst.Slice(bufferStride), default, mbw, uvBuffer); + YuvConversion.UpSample(curY, default, curU, curV, curU, curV, dst[bufferStride..], default, mbw, uvBuffer); } } @@ -1149,7 +1149,7 @@ namespace SixLabors.ImageSharp.Formats.Webp.Lossy dec.Vp8BitReaders[p] = new Vp8BitReader(this.bitReader.Data, (uint)pSize, partStart); partStart += pSize; sizeLeft -= pSize; - sz = sz.Slice(3); + sz = sz[3..]; } dec.Vp8BitReaders[lastPart] = new Vp8BitReader(this.bitReader.Data, (uint)sizeLeft, partStart); diff --git a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs index 7bd4a3eb5b..d327e72932 100644 --- a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs +++ b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs @@ -294,8 +294,8 @@ namespace SixLabors.ImageSharp.Formats.Webp for (int y = frameY; y < frameY + frameHeight; y++) { Span framePixelRow = imageFramePixels.DangerousGetRowSpan(y); - Span decodedPixelRow = decodedImage.DangerousGetRowSpan(decodedRowIdx++).Slice(0, frameWidth); - decodedPixelRow.TryCopyTo(framePixelRow.Slice(frameX)); + Span decodedPixelRow = decodedImage.DangerousGetRowSpan(decodedRowIdx++)[..frameWidth]; + decodedPixelRow.TryCopyTo(framePixelRow[frameX..]); } } diff --git a/src/ImageSharp/Formats/Webp/WebpImageFormatDetector.cs b/src/ImageSharp/Formats/Webp/WebpImageFormatDetector.cs index fa270b5281..9e672afb3e 100644 --- a/src/ImageSharp/Formats/Webp/WebpImageFormatDetector.cs +++ b/src/ImageSharp/Formats/Webp/WebpImageFormatDetector.cs @@ -23,7 +23,7 @@ namespace SixLabors.ImageSharp.Formats.Webp /// /// The header bytes. /// True, if its a valid RIFF FourCC. - private bool IsRiffContainer(ReadOnlySpan header) => header.Slice(0, 4).SequenceEqual(WebpConstants.RiffFourCc); + private bool IsRiffContainer(ReadOnlySpan header) => header[..4].SequenceEqual(WebpConstants.RiffFourCc); /// /// Checks if 'WEBP' is present in the header. diff --git a/src/ImageSharp/IO/ChunkedMemoryStream.cs b/src/ImageSharp/IO/ChunkedMemoryStream.cs index 44ec7aa291..49e3d7f2fc 100644 --- a/src/ImageSharp/IO/ChunkedMemoryStream.cs +++ b/src/ImageSharp/IO/ChunkedMemoryStream.cs @@ -286,7 +286,7 @@ namespace SixLabors.ImageSharp.IO } int readCount = Math.Min(count, chunkSize - this.readOffset); - chunkBuffer.Slice(this.readOffset, readCount).CopyTo(buffer.Slice(offset)); + chunkBuffer.Slice(this.readOffset, readCount).CopyTo(buffer[offset..]); offset += readCount; count -= readCount; this.readOffset += readCount; @@ -382,7 +382,7 @@ namespace SixLabors.ImageSharp.IO } int copyCount = Math.Min(count, chunkSize - this.writeOffset); - buffer.Slice(offset, copyCount).CopyTo(chunkBuffer.Slice(this.writeOffset)); + buffer.Slice(offset, copyCount).CopyTo(chunkBuffer[this.writeOffset..]); offset += copyCount; count -= copyCount; diff --git a/src/ImageSharp/Image.LoadPixelData.cs b/src/ImageSharp/Image.LoadPixelData.cs index 93563c794d..ab96a1c72c 100644 --- a/src/ImageSharp/Image.LoadPixelData.cs +++ b/src/ImageSharp/Image.LoadPixelData.cs @@ -130,7 +130,7 @@ namespace SixLabors.ImageSharp Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data)); var image = new Image(configuration, width, height); - data = data.Slice(0, count); + data = data[..count]; data.CopyTo(image.Frames.RootFrame.PixelBuffer.FastMemoryGroup); return image; diff --git a/src/ImageSharp/ImageFrame.LoadPixelData.cs b/src/ImageSharp/ImageFrame.LoadPixelData.cs index f6432757ea..b1f2b14bdc 100644 --- a/src/ImageSharp/ImageFrame.LoadPixelData.cs +++ b/src/ImageSharp/ImageFrame.LoadPixelData.cs @@ -44,7 +44,7 @@ namespace SixLabors.ImageSharp var image = new ImageFrame(configuration, width, height); - data = data.Slice(0, count); + data = data[..count]; data.CopyTo(image.PixelBuffer.FastMemoryGroup); return image; diff --git a/src/ImageSharp/IndexedImageFrame{TPixel}.cs b/src/ImageSharp/IndexedImageFrame{TPixel}.cs index 87bdbff698..2bd5099028 100644 --- a/src/ImageSharp/IndexedImageFrame{TPixel}.cs +++ b/src/ImageSharp/IndexedImageFrame{TPixel}.cs @@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp // Copy the palette over. We want the lifetime of this frame to be independant of any palette source. this.paletteOwner = configuration.MemoryAllocator.Allocate(palette.Length); palette.Span.CopyTo(this.paletteOwner.GetSpan()); - this.Palette = this.paletteOwner.Memory.Slice(0, palette.Length); + this.Palette = this.paletteOwner.Memory[..palette.Length]; } /// diff --git a/src/ImageSharp/Memory/Allocators/Internals/Gen2GcCallback.cs b/src/ImageSharp/Memory/Allocators/Internals/Gen2GcCallback.cs index dd30efa066..54d8e17f01 100644 --- a/src/ImageSharp/Memory/Allocators/Internals/Gen2GcCallback.cs +++ b/src/ImageSharp/Memory/Allocators/Internals/Gen2GcCallback.cs @@ -1,9 +1,8 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. // Port of BCL internal utility: // https://github.com/dotnet/runtime/blob/57bfe474518ab5b7cfe6bf7424a79ce3af9d6657/src/libraries/System.Private.CoreLib/src/System/Gen2GcCallback.cs -#if NETCOREAPP3_1_OR_GREATER using System; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; @@ -20,10 +19,7 @@ namespace SixLabors.ImageSharp.Memory.Internals private readonly Func callback1; private GCHandle weakTargetObj; - private Gen2GcCallback(Func callback) - { - this.callback0 = callback; - } + private Gen2GcCallback(Func callback) => this.callback0 = callback; private Gen2GcCallback(Func callback, object targetObj) { @@ -92,24 +88,24 @@ namespace SixLabors.ImageSharp.Memory.Internals /// Schedule 'callback' to be called in the next GC. If the callback returns true it is /// rescheduled for the next Gen 2 GC. Otherwise the callbacks stop. /// - public static void Register(Func callback) - { + public static void Register(Func callback) => + // Create a unreachable object that remembers the callback function and target object. _ = new Gen2GcCallback(callback); - } /// + /// /// Schedule 'callback' to be called in the next GC. If the callback returns true it is /// rescheduled for the next Gen 2 GC. Otherwise the callbacks stop. - /// + /// + /// /// NOTE: This callback will be kept alive until either the callback function returns false, /// or the target object dies. + /// /// - public static void Register(Func callback, object targetObj) - { + public static void Register(Func callback, object targetObj) => + // Create a unreachable object that remembers the callback function and target object. _ = new Gen2GcCallback(callback, targetObj); - } } } -#endif diff --git a/src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs b/src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs index e960aef0db..756f5a9955 100644 --- a/src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs +++ b/src/ImageSharp/Memory/Allocators/Internals/UniformUnmanagedMemoryPool.cs @@ -1,4 +1,4 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System; @@ -40,9 +40,7 @@ namespace SixLabors.ImageSharp.Memory.Internals if (trimSettings.Enabled) { UpdateTimer(trimSettings, this); -#if NETCOREAPP3_1_OR_GREATER Gen2GcCallback.Register(s => ((UniformUnmanagedMemoryPool)s).Trim(), this); -#endif this.lastTrimTimestamp = Stopwatch.ElapsedMilliseconds; } } @@ -326,14 +324,8 @@ namespace SixLabors.ImageSharp.Memory.Internals private bool IsHighMemoryPressure() { -#if NETCOREAPP3_1_OR_GREATER GCMemoryInfo memoryInfo = GC.GetGCMemoryInfo(); return memoryInfo.MemoryLoadBytes >= memoryInfo.HighMemoryLoadThresholdBytes * this.trimSettings.HighPressureThresholdRate; -#else - // We don't have high pressure detection triggering full trimming on other platforms, - // to counterpart this, the maximum pool size is small. - return false; -#endif } public class TrimSettings diff --git a/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs b/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs index e79eb4967e..af04718e83 100644 --- a/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs +++ b/src/ImageSharp/Memory/Allocators/UniformUnmanagedMemoryPoolMemoryAllocator.cs @@ -4,7 +4,6 @@ using System; using System.Buffers; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using SixLabors.ImageSharp.Memory.Internals; namespace SixLabors.ImageSharp.Memory @@ -73,11 +72,9 @@ namespace SixLabors.ImageSharp.Memory this.nonPoolAllocator = new UnmanagedMemoryAllocator(unmanagedBufferSizeInBytes); } -#if NETCOREAPP3_1_OR_GREATER // This delegate allows overriding the method returning the available system memory, // so we can test our workaround for https://github.com/dotnet/runtime/issues/65466 internal static Func GetTotalAvailableMemoryBytes { get; set; } = () => GC.GetGCMemoryInfo().TotalAvailableMemoryBytes; -#endif /// protected internal override int GetBufferCapacityInBytes() => this.poolBufferSizeInBytes; @@ -151,11 +148,9 @@ namespace SixLabors.ImageSharp.Memory private static long GetDefaultMaxPoolSizeBytes() { -#if NETCOREAPP3_1_OR_GREATER - // On 64 bit .NET Core 3.1+, set the pool size to a portion of the total available memory. - // There is a bug in GC.GetGCMemoryInfo() on .NET 5 + 32 bit, making TotalAvailableMemoryBytes unreliable: + // On 64 bit set the pool size to a portion of the total available memory. // https://github.com/dotnet/runtime/issues/55126#issuecomment-876779327 - if (Environment.Is64BitProcess || !RuntimeInformation.FrameworkDescription.StartsWith(".NET 5.0")) + if (Environment.Is64BitProcess) { long total = GetTotalAvailableMemoryBytes(); @@ -165,7 +160,6 @@ namespace SixLabors.ImageSharp.Memory return total / 8; } } -#endif // Stick to a conservative value of 128 Megabytes on other platforms and 32 bit .NET 5.0: return 128 * OneMegabyte; diff --git a/src/ImageSharp/Memory/Buffer2D{T}.cs b/src/ImageSharp/Memory/Buffer2D{T}.cs index f35ec9b617..c73b96ae60 100644 --- a/src/ImageSharp/Memory/Buffer2D{T}.cs +++ b/src/ImageSharp/Memory/Buffer2D{T}.cs @@ -120,7 +120,7 @@ namespace SixLabors.ImageSharp.Memory return false; } - paddedSpan = slice.Slice(0, stride); + paddedSpan = slice[..stride]; return true; } diff --git a/src/ImageSharp/Memory/ByteMemoryManager{T}.cs b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs index fd9a06db06..f6d79642a3 100644 --- a/src/ImageSharp/Memory/ByteMemoryManager{T}.cs +++ b/src/ImageSharp/Memory/ByteMemoryManager{T}.cs @@ -47,7 +47,7 @@ namespace SixLabors.ImageSharp.Memory // We need to adjust the offset into the wrapped byte segment, // as the input index refers to the target-cast memory of T. // We just have to shift this index by the byte size of T. - return this.memory.Slice(elementIndex * Unsafe.SizeOf()).Pin(); + return this.memory[(elementIndex * Unsafe.SizeOf())..].Pin(); } /// diff --git a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs index fc8e150d91..51774c8816 100644 --- a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs +++ b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs @@ -82,7 +82,7 @@ namespace SixLabors.ImageSharp.Memory cur.GetSpan(fwd).CopyTo(target); cur.Forward(fwd); - target = target.Slice(fwd); + target = target[fwd..]; position += fwd; } } @@ -102,9 +102,9 @@ namespace SixLabors.ImageSharp.Memory while (!source.IsEmpty) { int fwd = Math.Min(cur.LookAhead(), source.Length); - source.Slice(0, fwd).CopyTo(cur.GetSpan(fwd)); + source[..fwd].CopyTo(cur.GetSpan(fwd)); cur.Forward(fwd); - source = source.Slice(fwd); + source = source[fwd..]; } } diff --git a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs index ace02c8fa4..f044e6cb9e 100644 --- a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs +++ b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroup{T}.cs @@ -285,7 +285,7 @@ namespace SixLabors.ImageSharp.Memory { long bufferIdx = Math.DivRem(start, this.BufferLength, out long bufferStart); Memory memory = this[(int)bufferIdx]; - return memory.Span.Slice((int)bufferStart); + return memory.Span[(int)bufferStart..]; } public static bool CanSwapContent(MemoryGroup target, MemoryGroup source) => diff --git a/src/ImageSharp/Memory/MemoryOwnerExtensions.cs b/src/ImageSharp/Memory/MemoryOwnerExtensions.cs index 0c415806fb..8c3d49d1eb 100644 --- a/src/ImageSharp/Memory/MemoryOwnerExtensions.cs +++ b/src/ImageSharp/Memory/MemoryOwnerExtensions.cs @@ -44,7 +44,7 @@ namespace SixLabors.ImageSharp.Memory [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Span Slice(this IMemoryOwner buffer, int start) { - return buffer.GetSpan().Slice(start); + return buffer.GetSpan()[start..]; } /// diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs index 03f1566d24..f5f884f289 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifEncodedStringHelpers.cs @@ -57,7 +57,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif { if (TryDetect(buffer, out CharacterCode code)) { - string text = GetEncoding(code).GetString(buffer.Slice(CharacterCodeBytesLength)); + string text = GetEncoding(code).GetString(buffer[CharacterCodeBytesLength..]); encodedString = new EncodedString(code, text); return true; } @@ -74,30 +74,13 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif GetCodeBytes(encodedString.Code).CopyTo(destination); string text = encodedString.Text; - int count = Write(GetEncoding(encodedString.Code), text, destination.Slice(CharacterCodeBytesLength)); + int count = Write(GetEncoding(encodedString.Code), text, destination[CharacterCodeBytesLength..]); return CharacterCodeBytesLength + count; } public static unsafe int Write(Encoding encoding, string value, Span destination) -#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP2_1_OR_GREATER || NET => encoding.GetBytes(value.AsSpan(), destination); -#else - { - if (value.Length == 0) - { - return 0; - } - - fixed (char* c = value) - { - fixed (byte* b = destination) - { - return encoding.GetBytes(c, value.Length, b, destination.Length); - } - } - } -#endif private static bool TryDetect(ReadOnlySpan buffer, out CharacterCode code) { diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs index dc6fad9094..b2b1b28317 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs @@ -150,7 +150,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif foreach ((ulong Offset, ExifDataType DataType, ulong NumberOfComponents, ExifValue Exif) tag in this.BigValues) { ulong size = tag.NumberOfComponents * ExifDataTypes.GetSize(tag.DataType); - this.ReadBigValue(values, tag, buf.Slice(0, (int)size)); + this.ReadBigValue(values, tag, buf[..(int)size]); } } else @@ -160,7 +160,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif foreach ((ulong Offset, ExifDataType DataType, ulong NumberOfComponents, ExifValue Exif) tag in this.BigValues) { ulong size = tag.NumberOfComponents * ExifDataTypes.GetSize(tag.DataType); - this.ReadBigValue(values, tag, buf.Slice(0, (int)size)); + this.ReadBigValue(values, tag, buf[..(int)size]); } } @@ -247,13 +247,13 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif if (nullCharIndex > -1) { - buffer = buffer.Slice(0, nullCharIndex); + buffer = buffer[..nullCharIndex]; } return encoding.GetString(buffer); } - private byte ConvertToByte(ReadOnlySpan buffer) => buffer[0]; + private static byte ConvertToByte(ReadOnlySpan buffer) => buffer[0]; private object ConvertValue(ExifDataType dataType, ReadOnlySpan buffer, bool isArray) { @@ -272,7 +272,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif case ExifDataType.Undefined: if (!isArray) { - return this.ConvertToByte(buffer); + return ConvertToByte(buffer); } return buffer.ToArray(); @@ -414,7 +414,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif } else { - object value = this.ConvertValue(dataType, offsetBuffer.Slice(0, (int)size), numberOfComponents > 1 || exifValue.IsArray); + object value = this.ConvertValue(dataType, offsetBuffer[..(int)size], numberOfComponents > 1 || exifValue.IsArray); this.Add(values, exifValue, value); } } @@ -443,31 +443,14 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif numberOfComponents = 8 / ExifDataTypes.GetSize(dataType); } - // The StripOffsets, StripByteCounts, TileOffsets, and TileByteCounts tags are allowed to have the datatype TIFF_LONG8 in BigTIFF. - // Old datatypes TIFF_LONG, and TIFF_SHORT where allowed in the TIFF 6.0 specification, are still valid in BigTIFF, too. - // Likewise, tags that point to other IFDs, like e.g. the SubIFDs tag, are now allowed to have the datatype TIFF_IFD8 in BigTIFF. - // Again, the old datatypes TIFF_IFD, and the hardly recommendable TIFF_LONG, are still valid, too. - // https://www.awaresystems.be/imaging/tiff/bigtiff.html - ExifValue exifValue; - switch (tag) - { - case ExifTagValue.StripOffsets: - exifValue = new ExifLong8Array(ExifTagValue.StripOffsets); - break; - case ExifTagValue.StripByteCounts: - exifValue = new ExifLong8Array(ExifTagValue.StripByteCounts); - break; - case ExifTagValue.TileOffsets: - exifValue = new ExifLong8Array(ExifTagValue.TileOffsets); - break; - case ExifTagValue.TileByteCounts: - exifValue = new ExifLong8Array(ExifTagValue.TileByteCounts); - break; - default: - exifValue = ExifValues.Create(tag) ?? ExifValues.Create(tag, dataType, numberOfComponents); - break; - } - + ExifValue exifValue = tag switch + { + ExifTagValue.StripOffsets => new ExifLong8Array(ExifTagValue.StripOffsets), + ExifTagValue.StripByteCounts => new ExifLong8Array(ExifTagValue.StripByteCounts), + ExifTagValue.TileOffsets => new ExifLong8Array(ExifTagValue.TileOffsets), + ExifTagValue.TileByteCounts => new ExifLong8Array(ExifTagValue.TileByteCounts), + _ => ExifValues.Create(tag) ?? ExifValues.Create(tag, dataType, numberOfComponents), + }; if (exifValue is null) { this.AddInvalidTag(new UnkownExifTag(tag)); @@ -488,7 +471,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif } else { - object value = this.ConvertValue(dataType, offsetBuffer.Slice(0, (int)size), numberOfComponents > 1 || exifValue.IsArray); + object value = this.ConvertValue(dataType, offsetBuffer[..(int)size], numberOfComponents > 1 || exifValue.IsArray); this.Add(values, exifValue, value); } } @@ -644,7 +627,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return default; } - uint numerator = this.ConvertToUInt32(buffer.Slice(0, 4)); + uint numerator = this.ConvertToUInt32(buffer[..4]); uint denominator = this.ConvertToUInt32(buffer.Slice(4, 4)); return new Rational(numerator, denominator, false); @@ -671,7 +654,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif return default; } - int numerator = this.ConvertToInt32(buffer.Slice(0, 4)); + int numerator = this.ConvertToInt32(buffer[..4]); int denominator = this.ConvertToInt32(buffer.Slice(4, 4)); return new SignedRational(numerator, denominator, false); diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs index 6e98128d5b..ed22ff6476 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs @@ -367,13 +367,13 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif private static void WriteRational(Span destination, in Rational value) { - BinaryPrimitives.WriteUInt32LittleEndian(destination.Slice(0, 4), value.Numerator); + BinaryPrimitives.WriteUInt32LittleEndian(destination[..4], value.Numerator); BinaryPrimitives.WriteUInt32LittleEndian(destination.Slice(4, 4), value.Denominator); } private static void WriteSignedRational(Span destination, in SignedRational value) { - BinaryPrimitives.WriteInt32LittleEndian(destination.Slice(0, 4), value.Numerator); + BinaryPrimitives.WriteInt32LittleEndian(destination[..4], value.Numerator); BinaryPrimitives.WriteInt32LittleEndian(destination.Slice(4, 4), value.Denominator); } @@ -435,11 +435,11 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Exif if (ExifUcs2StringHelpers.IsUcs2Tag((ExifTagValue)(ushort)exifValue.Tag)) { - return offset + ExifUcs2StringHelpers.Write((string)value, destination.Slice(offset)); + return offset + ExifUcs2StringHelpers.Write((string)value, destination[offset..]); } else if (value is EncodedString encodedString) { - return offset + ExifEncodedStringHelpers.Write(encodedString, destination.Slice(offset)); + return offset + ExifEncodedStringHelpers.Write(encodedString, destination[offset..]); } if (exifValue.IsArray) diff --git a/src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.Primitives.cs b/src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.Primitives.cs index c9f4bab45c..d63bb74491 100644 --- a/src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.Primitives.cs +++ b/src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.Primitives.cs @@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Icc int pos = value.IndexOf('\0'); if (pos >= 0) { - value = value.Substring(0, pos); + value = value[..pos]; } return value; diff --git a/src/ImageSharp/Metadata/Profiles/ICC/DataWriter/IccDataWriter.Primitives.cs b/src/ImageSharp/Metadata/Profiles/ICC/DataWriter/IccDataWriter.Primitives.cs index 180fdba030..d3113b0140 100644 --- a/src/ImageSharp/Metadata/Profiles/ICC/DataWriter/IccDataWriter.Primitives.cs +++ b/src/ImageSharp/Metadata/Profiles/ICC/DataWriter/IccDataWriter.Primitives.cs @@ -213,7 +213,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Icc lengthAdjust = 1; } - value = value.Substring(0, Math.Min(length - lengthAdjust, value.Length)); + value = value[..Math.Min(length - lengthAdjust, value.Length)]; byte[] textData = Encoding.ASCII.GetBytes(value); int actualLength = Math.Min(length - lengthAdjust, textData.Length); diff --git a/src/ImageSharp/Metadata/Profiles/IPTC/IptcValue.cs b/src/ImageSharp/Metadata/Profiles/IPTC/IptcValue.cs index f0d40a718c..1a74ea4183 100644 --- a/src/ImageSharp/Metadata/Profiles/IPTC/IptcValue.cs +++ b/src/ImageSharp/Metadata/Profiles/IPTC/IptcValue.cs @@ -101,7 +101,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Iptc byte[] valueBytes; if (this.Strict && value.Length > maxLength) { - string cappedValue = value.Substring(0, maxLength); + string cappedValue = value[..maxLength]; valueBytes = this.encoding.GetBytes(cappedValue); // It is still possible that the bytes of the string exceed the limit. diff --git a/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs index fe9acd4aa9..3f325eed8e 100644 --- a/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs @@ -58,14 +58,14 @@ namespace SixLabors.ImageSharp.PixelFormats Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); - ReadOnlySpan sourcePixels = background.Slice(0, background.Length); + ReadOnlySpan sourcePixels = background[..]; PixelOperations.Instance.ToVector4(configuration, sourcePixels, backgroundSpan, PixelConversionModifiers.Scale); - ReadOnlySpan sourcePixels1 = source.Slice(0, background.Length); + ReadOnlySpan sourcePixels1 = source[..background.Length]; PixelOperations.Instance.ToVector4(configuration, sourcePixels1, sourceSpan, PixelConversionModifiers.Scale); this.BlendFunction(destinationSpan, backgroundSpan, sourceSpan, amount); - Span sourceVectors = destinationSpan.Slice(0, background.Length); + Span sourceVectors = destinationSpan[..background.Length]; PixelOperations.Instance.FromVector4Destructive(configuration, sourceVectors, destination, PixelConversionModifiers.Scale); } } @@ -120,14 +120,14 @@ namespace SixLabors.ImageSharp.PixelFormats Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); - ReadOnlySpan sourcePixels = background.Slice(0, background.Length); + ReadOnlySpan sourcePixels = background[..]; PixelOperations.Instance.ToVector4(configuration, sourcePixels, backgroundSpan, PixelConversionModifiers.Scale); - ReadOnlySpan sourcePixels1 = source.Slice(0, background.Length); + ReadOnlySpan sourcePixels1 = source[..background.Length]; PixelOperations.Instance.ToVector4(configuration, sourcePixels1, sourceSpan, PixelConversionModifiers.Scale); this.BlendFunction(destinationSpan, backgroundSpan, sourceSpan, amount); - Span sourceVectors = destinationSpan.Slice(0, background.Length); + Span sourceVectors = destinationSpan[..background.Length]; PixelOperations.Instance.FromVector4Destructive(configuration, sourceVectors, destination, PixelConversionModifiers.Scale); } } diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs index 96d4579c7f..240e85ff15 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Rgb24.cs @@ -59,7 +59,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// The . /// The . [MethodImpl(InliningOptions.ShortMethod)] - public static implicit operator Color(Rgb24 source) => new Color(source); + public static implicit operator Color(Rgb24 source) => new(source); /// /// Converts a to . @@ -210,17 +210,7 @@ namespace SixLabors.ImageSharp.PixelFormats /// [MethodImpl(InliningOptions.ShortMethod)] - public void FromRgba32(Rgba32 source) - { -#if NETSTANDARD2_0 - // See https://github.com/SixLabors/ImageSharp/issues/1275 - this.R = source.R; - this.G = source.G; - this.B = source.B; -#else - this = source.Rgb; -#endif - } + public void FromRgba32(Rgba32 source) => this = source.Rgb; /// [MethodImpl(InliningOptions.ShortMethod)] diff --git a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs index 599857961c..55dd80af10 100644 --- a/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs +++ b/src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs @@ -517,7 +517,7 @@ namespace SixLabors.ImageSharp.PixelFormats { if (hex[0] == '#') { - hex = hex.Substring(1); + hex = hex[1..]; } if (hex.Length == 8) diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs index 94dc69388b..6721627de8 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs @@ -134,9 +134,9 @@ namespace SixLabors.ImageSharp.PixelFormats int remainder = sourcePixels.Length - endOfCompleteSlices; if (remainder > 0) { - ReadOnlySpan s = sourcePixels.Slice(endOfCompleteSlices); - Span d = destinationPixels.Slice(endOfCompleteSlices); - vectorSpan = vectorSpan.Slice(0, remainder); + ReadOnlySpan s = sourcePixels[endOfCompleteSlices..]; + Span d = destinationPixels[endOfCompleteSlices..]; + vectorSpan = vectorSpan[..remainder]; PixelOperations.Instance.ToVector4(configuration, s, vectorSpan, PixelConversionModifiers.Scale); this.FromVector4Destructive(configuration, vectorSpan, d, PixelConversionModifiers.Scale); } diff --git a/src/ImageSharp/PixelFormats/Utils/Vector4Converters.RgbaCompatible.cs b/src/ImageSharp/PixelFormats/Utils/Vector4Converters.RgbaCompatible.cs index 38250b24a5..92007732dc 100644 --- a/src/ImageSharp/PixelFormats/Utils/Vector4Converters.RgbaCompatible.cs +++ b/src/ImageSharp/PixelFormats/Utils/Vector4Converters.RgbaCompatible.cs @@ -56,7 +56,7 @@ namespace SixLabors.ImageSharp.PixelFormats.Utils // Using the last quarter of 'destVectors' as a temporary buffer to avoid allocation: int countWithoutLastItem = count - 1; - ReadOnlySpan reducedSource = sourcePixels.Slice(0, countWithoutLastItem); + ReadOnlySpan reducedSource = sourcePixels[..countWithoutLastItem]; Span lastQuarterOfDestBuffer = MemoryMarshal.Cast(destVectors).Slice((3 * count) + 1, countWithoutLastItem); pixelOperations.ToRgba32(configuration, reducedSource, lastQuarterOfDestBuffer); @@ -64,7 +64,7 @@ namespace SixLabors.ImageSharp.PixelFormats.Utils // but we are always reading/writing at different positions: SimdUtils.ByteToNormalizedFloat( MemoryMarshal.Cast(lastQuarterOfDestBuffer), - MemoryMarshal.Cast(destVectors.Slice(0, countWithoutLastItem))); + MemoryMarshal.Cast(destVectors[..countWithoutLastItem])); destVectors[countWithoutLastItem] = sourcePixels[countWithoutLastItem].ToVector4(); diff --git a/src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs index eb36183ad1..e0221f8152 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/BokehBlurProcessor{TPixel}.cs @@ -295,8 +295,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int y, Span span) { - Span targetRowSpan = this.targetPixels.DangerousGetRowSpan(y).Slice(this.bounds.X); - PixelOperations.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, span.Length), span, PixelConversionModifiers.Premultiply); + Span targetRowSpan = this.targetPixels.DangerousGetRowSpan(y)[this.bounds.X..]; + PixelOperations.Instance.ToVector4(this.configuration, targetRowSpan[..span.Length], span, PixelConversionModifiers.Premultiply); ref Vector4 baseRef = ref MemoryMarshal.GetReference(span); for (int x = 0; x < this.bounds.Width; x++) @@ -335,9 +335,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int y, Span span) { - Span targetRowSpan = this.targetPixels.DangerousGetRowSpan(y).Slice(this.bounds.X); + Span targetRowSpan = this.targetPixels.DangerousGetRowSpan(y)[this.bounds.X..]; - PixelOperations.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, span.Length), span, PixelConversionModifiers.Premultiply); + PixelOperations.Instance.ToVector4(this.configuration, targetRowSpan[..span.Length], span, PixelConversionModifiers.Premultiply); Numerics.CubePowOnXYZ(span); @@ -378,8 +378,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution Vector4 low = Vector4.Zero; var high = new Vector4(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity); - Span targetPixelSpan = this.targetPixels.DangerousGetRowSpan(y).Slice(this.bounds.X); - Span sourceRowSpan = this.sourceValues.DangerousGetRowSpan(y).Slice(this.bounds.X); + Span targetPixelSpan = this.targetPixels.DangerousGetRowSpan(y)[this.bounds.X..]; + Span sourceRowSpan = this.sourceValues.DangerousGetRowSpan(y)[this.bounds.X..]; ref Vector4 sourceRef = ref MemoryMarshal.GetReference(sourceRowSpan); for (int x = 0; x < this.bounds.Width; x++) @@ -391,7 +391,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution v.Z = MathF.Pow(clamp.Z, this.inverseGamma); } - PixelOperations.Instance.FromVector4Destructive(this.configuration, sourceRowSpan.Slice(0, this.bounds.Width), targetPixelSpan, PixelConversionModifiers.Premultiply); + PixelOperations.Instance.FromVector4Destructive(this.configuration, sourceRowSpan[..this.bounds.Width], targetPixelSpan, PixelConversionModifiers.Premultiply); } } @@ -428,9 +428,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution Numerics.Clamp(MemoryMarshal.Cast(sourceRowSpan), 0, float.PositiveInfinity); Numerics.CubeRootOnXYZ(sourceRowSpan); - Span targetPixelSpan = this.targetPixels.DangerousGetRowSpan(y).Slice(this.bounds.X); + Span targetPixelSpan = this.targetPixels.DangerousGetRowSpan(y)[this.bounds.X..]; - PixelOperations.Instance.FromVector4Destructive(this.configuration, sourceRowSpan.Slice(0, this.bounds.Width), targetPixelSpan, PixelConversionModifiers.Premultiply); + PixelOperations.Instance.FromVector4Destructive(this.configuration, sourceRowSpan[..this.bounds.Width], targetPixelSpan, PixelConversionModifiers.Premultiply); } } } diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DRowOperation{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DRowOperation{TPixel}.cs index 5ccaa77c47..8b4eaab801 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DRowOperation{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DRowOperation{TPixel}.cs @@ -67,7 +67,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution // Span is 3x bounds. int boundsX = this.bounds.X; int boundsWidth = this.bounds.Width; - Span sourceBuffer = span.Slice(0, boundsWidth); + Span sourceBuffer = span[..boundsWidth]; Span targetYBuffer = span.Slice(boundsWidth, boundsWidth); Span targetXBuffer = span.Slice(boundsWidth * 2, boundsWidth); @@ -133,7 +133,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution // Span is 3x bounds. int boundsX = this.bounds.X; int boundsWidth = this.bounds.Width; - Span sourceBuffer = span.Slice(0, boundsWidth); + Span sourceBuffer = span[..boundsWidth]; Span targetYBuffer = span.Slice(boundsWidth, boundsWidth); Span targetXBuffer = span.Slice(boundsWidth * 2, boundsWidth); diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs index db9557124a..697351abc4 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor{TPixel}.cs @@ -166,8 +166,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution int boundsWidth = this.bounds.Width; int kernelSize = this.kernel.Length; - Span sourceBuffer = span.Slice(0, this.bounds.Width); - Span targetBuffer = span.Slice(this.bounds.Width); + Span sourceBuffer = span[..this.bounds.Width]; + Span targetBuffer = span[this.bounds.Width..]; // Clear the target buffer for each row run. targetBuffer.Clear(); @@ -228,8 +228,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution int boundsWidth = this.bounds.Width; int kernelSize = this.kernel.Length; - Span sourceBuffer = span.Slice(0, this.bounds.Width); - Span targetBuffer = span.Slice(this.bounds.Width); + Span sourceBuffer = span[..this.bounds.Width]; + Span targetBuffer = span[this.bounds.Width..]; // Clear the target buffer for each row run. targetBuffer.Clear(); @@ -327,8 +327,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution int boundsWidth = this.bounds.Width; int kernelSize = this.kernel.Length; - Span sourceBuffer = span.Slice(0, this.bounds.Width); - Span targetBuffer = span.Slice(this.bounds.Width); + Span sourceBuffer = span[..this.bounds.Width]; + Span targetBuffer = span[this.bounds.Width..]; ref int sampleRowBase = ref Unsafe.Add(ref MemoryMarshal.GetReference(this.map.GetRowOffsetSpan()), (y - this.bounds.Y) * kernelSize); @@ -392,8 +392,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution int boundsWidth = this.bounds.Width; int kernelSize = this.kernel.Length; - Span sourceBuffer = span.Slice(0, this.bounds.Width); - Span targetBuffer = span.Slice(this.bounds.Width); + Span sourceBuffer = span[..this.bounds.Width]; + Span targetBuffer = span[this.bounds.Width..]; ref int sampleRowBase = ref Unsafe.Add(ref MemoryMarshal.GetReference(this.map.GetRowOffsetSpan()), (y - this.bounds.Y) * kernelSize); diff --git a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs index fa0a012296..ed8b63ab2e 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor{TPixel}.cs @@ -114,8 +114,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution // Span is 2x bounds. int boundsX = this.bounds.X; int boundsWidth = this.bounds.Width; - Span sourceBuffer = span.Slice(0, this.bounds.Width); - Span targetBuffer = span.Slice(this.bounds.Width); + Span sourceBuffer = span[..this.bounds.Width]; + Span targetBuffer = span[this.bounds.Width..]; ref Vector4 targetRowRef = ref MemoryMarshal.GetReference(span); Span targetRowSpan = this.targetPixels.DangerousGetRowSpan(y).Slice(boundsX, boundsWidth); diff --git a/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs b/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs index 06d4a27556..a455df412d 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/KernelSamplingMap.cs @@ -110,8 +110,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Convolution switch (borderMode) { case BorderWrappingMode.Repeat: - Numerics.Clamp(span.Slice(0, affectedSize), min, max); - Numerics.Clamp(span.Slice(span.Length - affectedSize), min, max); + Numerics.Clamp(span[..affectedSize], min, max); + Numerics.Clamp(span[^affectedSize..], min, max); break; case BorderWrappingMode.Mirror: var min2dec = min + min - 1; diff --git a/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs b/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs index 0e85b20eab..4c6e8e043b 100644 --- a/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs +++ b/src/ImageSharp/Processing/Processors/Dithering/OrderedDither.cs @@ -124,7 +124,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Dithering for (int y = bounds.Top; y < bounds.Bottom; y++) { ReadOnlySpan sourceRow = sourceBuffer.DangerousGetRowSpan(y).Slice(bounds.X, bounds.Width); - Span destRow = destination.GetWritablePixelRowSpanUnsafe(y - bounds.Y).Slice(0, sourceRow.Length); + Span destRow = destination.GetWritablePixelRowSpanUnsafe(y - bounds.Y)[..sourceRow.Length]; for (int x = 0; x < sourceRow.Length; x++) { diff --git a/src/ImageSharp/Processing/Processors/Filters/OpaqueProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Filters/OpaqueProcessor{TPixel}.cs index 5c1282666d..7cc115137d 100644 --- a/src/ImageSharp/Processing/Processors/Filters/OpaqueProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Filters/OpaqueProcessor{TPixel}.cs @@ -51,8 +51,8 @@ namespace SixLabors.ImageSharp.Processing.Processors.Filters [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int y, Span span) { - Span targetRowSpan = this.target.DangerousGetRowSpan(y).Slice(this.bounds.X); - PixelOperations.Instance.ToVector4(this.configuration, targetRowSpan.Slice(0, span.Length), span, PixelConversionModifiers.Scale); + Span targetRowSpan = this.target.DangerousGetRowSpan(y)[this.bounds.X..]; + PixelOperations.Instance.ToVector4(this.configuration, targetRowSpan[..span.Length], span, PixelConversionModifiers.Scale); ref Vector4 baseRef = ref MemoryMarshal.GetReference(span); for (int x = 0; x < this.bounds.Width; x++) diff --git a/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs index 433207562a..c27118be4e 100644 --- a/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Normalization/GlobalHistogramEqualizationProcessor{TPixel}.cs @@ -107,12 +107,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization } /// -#if NETSTANDARD2_0 - // https://github.com/SixLabors/ImageSharp/issues/1204 - [MethodImpl(MethodImplOptions.NoOptimization)] -#else [MethodImpl(InliningOptions.ShortMethod)] -#endif public void Invoke(int y) { ref int histogramBase = ref MemoryMarshal.GetReference(this.histogramBuffer.GetSpan()); @@ -156,12 +151,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Normalization } /// -#if NETSTANDARD2_0 - // https://github.com/SixLabors/ImageSharp/issues/1204 - [MethodImpl(MethodImplOptions.NoOptimization)] -#else [MethodImpl(InliningOptions.ShortMethod)] -#endif public void Invoke(int y) { ref int cdfBase = ref MemoryMarshal.GetReference(this.cdfBuffer.GetSpan()); diff --git a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs index 4e47b0ac54..a066bfdc4a 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/OctreeQuantizer{TPixel}.cs @@ -108,7 +108,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization } this.octree.Palletize(paletteSpan, max, ref paletteIndex); - ReadOnlyMemory result = this.paletteOwner.Memory.Slice(0, paletteSpan.Length); + ReadOnlyMemory result = this.paletteOwner.Memory[..paletteSpan.Length]; // When called multiple times by QuantizerUtilities.BuildPalette // this prevents memory churn caused by reallocation. diff --git a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs index 576e34a269..39c753722c 100644 --- a/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Quantization/WuQuantizer{TPixel}.cs @@ -127,7 +127,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization this.BuildCube(); // Slice again since maxColors has been updated since the buffer was created. - Span paletteSpan = this.paletteOwner.GetSpan().Slice(0, this.maxColors); + Span paletteSpan = this.paletteOwner.GetSpan()[..this.maxColors]; ReadOnlySpan momentsSpan = this.momentsOwner.GetSpan(); for (int k = 0; k < paletteSpan.Length; k++) { @@ -142,7 +142,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization } } - ReadOnlyMemory result = this.paletteOwner.Memory.Slice(0, paletteSpan.Length); + ReadOnlyMemory result = this.paletteOwner.Memory[..paletteSpan.Length]; if (this.isDithering) { // When called multiple times by QuantizerUtilities.BuildPalette diff --git a/src/ImageSharp/Processing/Processors/Transforms/CropProcessor{TPixel}.cs b/src/ImageSharp/Processing/Processors/Transforms/CropProcessor{TPixel}.cs index 92f3e8b98b..32c93c2812 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/CropProcessor{TPixel}.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/CropProcessor{TPixel}.cs @@ -86,9 +86,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms [MethodImpl(InliningOptions.ShortMethod)] public void Invoke(int y) { - Span sourceRow = this.source.DangerousGetRowSpan(y).Slice(this.bounds.Left); + Span sourceRow = this.source.DangerousGetRowSpan(y)[this.bounds.Left..]; Span targetRow = this.destination.DangerousGetRowSpan(y - this.bounds.Top); - sourceRow.Slice(0, this.bounds.Width).CopyTo(targetRow); + sourceRow[..this.bounds.Width].CopyTo(targetRow); } } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs index 888143af42..cb8b68b572 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/Resize/ResizeWorker.cs @@ -102,7 +102,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Transforms [MethodImpl(InliningOptions.ShortMethod)] public Span GetColumnSpan(int x, int startY) - => this.transposedFirstPassBuffer.DangerousGetRowSpan(x).Slice(startY - this.currentWindow.Min); + => this.transposedFirstPassBuffer.DangerousGetRowSpan(x)[(startY - this.currentWindow.Min)..]; public void Initialize() => this.CalculateFirstPassValues(this.currentWindow); diff --git a/tests/ImageSharp.Tests/Helpers/RuntimeEnvironmentTests.cs b/tests/ImageSharp.Tests/Helpers/RuntimeEnvironmentTests.cs index 39a5dcccea..d61b8b86c2 100644 --- a/tests/ImageSharp.Tests/Helpers/RuntimeEnvironmentTests.cs +++ b/tests/ImageSharp.Tests/Helpers/RuntimeEnvironmentTests.cs @@ -4,22 +4,12 @@ using System.Runtime.InteropServices; using Xunit; -#pragma warning disable IDE0022 // Use expression body for methods namespace SixLabors.ImageSharp.Tests.Helpers { public class RuntimeEnvironmentTests { [Fact] - public void CanDetectNetCore() - { -#if NET5_0_OR_GREATER - Assert.False(RuntimeEnvironment.IsNetCore); -#elif NETCOREAPP - Assert.True(RuntimeEnvironment.IsNetCore); -#else - Assert.False(RuntimeEnvironment.IsNetCore); -#endif - } + public void CanDetectNetCore() => Assert.False(RuntimeEnvironment.IsNetCore); [Fact] public void CanDetectOSPlatform() diff --git a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedMemoryPoolTests.Trim.cs b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedMemoryPoolTests.Trim.cs index a65fd2325f..5b9d430af0 100644 --- a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedMemoryPoolTests.Trim.cs +++ b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedMemoryPoolTests.Trim.cs @@ -1,11 +1,9 @@ -// Copyright (c) Six Labors. +// Copyright (c) Six Labors. // Licensed under the Six Labors Split License. using System; using System.Collections.Generic; -using System.IO; using System.Runtime.CompilerServices; -using System.Text; using System.Threading; using Microsoft.DotNet.RemoteExecutor; using SixLabors.ImageSharp.Memory.Internals; @@ -109,7 +107,6 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators } } -#if NETCOREAPP3_1_OR_GREATER public static readonly bool Is32BitProcess = !Environment.Is64BitProcess; private static readonly List PressureArrays = new(); @@ -121,21 +118,21 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators static void RunTest() { Assert.False(Environment.Is64BitProcess); - const int OneMb = 1 << 20; + const int oneMb = 1 << 20; var trimSettings = new UniformUnmanagedMemoryPool.TrimSettings { HighPressureThresholdRate = 0.2f }; GCMemoryInfo memInfo = GC.GetGCMemoryInfo(); - int highLoadThreshold = (int)(memInfo.HighMemoryLoadThresholdBytes / OneMb); + int highLoadThreshold = (int)(memInfo.HighMemoryLoadThresholdBytes / oneMb); highLoadThreshold = (int)(trimSettings.HighPressureThresholdRate * highLoadThreshold); - var pool = new UniformUnmanagedMemoryPool(OneMb, 16, trimSettings); + var pool = new UniformUnmanagedMemoryPool(oneMb, 16, trimSettings); pool.Return(pool.Rent(16)); Assert.Equal(16, UnmanagedMemoryHandle.TotalOutstandingHandles); for (int i = 0; i < highLoadThreshold; i++) { - byte[] array = new byte[OneMb]; + byte[] array = new byte[oneMb]; TouchPage(array); PressureArrays.Add(array); } @@ -163,7 +160,6 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators } } } -#endif } } } diff --git a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs index e002fcc8e2..2cd1c696da 100644 --- a/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs +++ b/tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedPoolMemoryAllocatorTests.cs @@ -380,7 +380,6 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators } } -#if NETCOREAPP3_1_OR_GREATER [Fact] public void Issue2001_NegativeMemoryReportedByGc() { @@ -393,6 +392,5 @@ namespace SixLabors.ImageSharp.Tests.Memory.Allocators _ = MemoryAllocator.Create(); } } -#endif } } diff --git a/tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs b/tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs index c22b5dbcd0..3a6c04540b 100644 --- a/tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/PixelFormats/PixelOperations/PixelOperationsTests.cs @@ -20,13 +20,11 @@ namespace SixLabors.ImageSharp.Tests.PixelFormats.PixelOperations [Trait("Category", "PixelFormats")] public partial class PixelOperationsTests { -#pragma warning disable SA1313 // Parameter names should begin with lower-case letter [Theory] [WithBlankImages(1, 1, PixelTypes.All)] public void GetGlobalInstance(TestImageProvider _) where T : unmanaged, IPixel => Assert.NotNull(PixelOperations.Instance); } -#pragma warning restore SA1313 // Parameter names should begin with lower-case letter public abstract class PixelOperationsTests : MeasureFixture where TPixel : unmanaged, IPixel diff --git a/tests/ImageSharp.Tests/Primitives/ColorMatrixTests.cs b/tests/ImageSharp.Tests/Primitives/ColorMatrixTests.cs index c214a4caab..2e418c5a7a 100644 --- a/tests/ImageSharp.Tests/Primitives/ColorMatrixTests.cs +++ b/tests/ImageSharp.Tests/Primitives/ColorMatrixTests.cs @@ -10,7 +10,7 @@ namespace SixLabors.ImageSharp.Tests.Primitives { public class ColorMatrixTests { - private readonly ApproximateFloatComparer approximateFloatComparer = new ApproximateFloatComparer(1e-6f); + private readonly ApproximateFloatComparer approximateFloatComparer = new(1e-6f); [Fact] public void ColorMatrixIdentityIsCorrect() @@ -47,8 +47,8 @@ namespace SixLabors.ImageSharp.Tests.Primitives [Fact] public void ColorMatrixMultiply() { - ColorMatrix value1 = this.CreateAllTwos(); - ColorMatrix value2 = this.CreateAllThrees(); + ColorMatrix value1 = CreateAllTwos(); + ColorMatrix value2 = CreateAllThrees(); var m = default(ColorMatrix); @@ -88,35 +88,34 @@ namespace SixLabors.ImageSharp.Tests.Primitives [Fact] public void ColorMatrixMultiplyScalar() { - ColorMatrix m = this.CreateAllTwos(); - Assert.Equal(this.CreateAllFours(), m * 2, this.approximateFloatComparer); + ColorMatrix m = CreateAllTwos(); + Assert.Equal(CreateAllFours(), m * 2, this.approximateFloatComparer); } [Fact] public void ColorMatrixSubtract() { - ColorMatrix m = this.CreateAllOnes() + this.CreateAllTwos(); - Assert.Equal(this.CreateAllThrees(), m); + ColorMatrix m = CreateAllOnes() + CreateAllTwos(); + Assert.Equal(CreateAllThrees(), m); } [Fact] public void ColorMatrixNegate() { - ColorMatrix m = this.CreateAllOnes() * -1F; - Assert.Equal(m, -this.CreateAllOnes()); + ColorMatrix m = CreateAllOnes() * -1F; + Assert.Equal(m, -CreateAllOnes()); } [Fact] public void ColorMatrixAdd() { - ColorMatrix m = this.CreateAllOnes() + this.CreateAllTwos(); - Assert.Equal(this.CreateAllThrees(), m); + ColorMatrix m = CreateAllOnes() + CreateAllTwos(); + Assert.Equal(CreateAllThrees(), m); } [Fact] public void ColorMatrixHashCode() { -#if NETCOREAPP2_1 ColorMatrix m = KnownFilterMatrices.CreateBrightnessFilter(.5F); HashCode hash = default; hash.Add(m.M11); @@ -141,7 +140,6 @@ namespace SixLabors.ImageSharp.Tests.Primitives hash.Add(m.M54); Assert.Equal(hash.ToHashCode(), m.GetHashCode()); -#endif } [Fact] @@ -163,9 +161,8 @@ namespace SixLabors.ImageSharp.Tests.Primitives Assert.Equal(expected, m.ToString()); } - private ColorMatrix CreateAllOnes() - { - return new ColorMatrix + private static ColorMatrix CreateAllOnes() + => new() { M11 = 1F, M12 = 1F, @@ -188,11 +185,9 @@ namespace SixLabors.ImageSharp.Tests.Primitives M53 = 1F, M54 = 1F }; - } - private ColorMatrix CreateAllTwos() - { - return new ColorMatrix + private static ColorMatrix CreateAllTwos() + => new() { M11 = 2F, M12 = 2F, @@ -215,11 +210,9 @@ namespace SixLabors.ImageSharp.Tests.Primitives M53 = 2F, M54 = 2F }; - } - private ColorMatrix CreateAllThrees() - { - return new ColorMatrix + private static ColorMatrix CreateAllThrees() + => new() { M11 = 3F, M12 = 3F, @@ -242,11 +235,9 @@ namespace SixLabors.ImageSharp.Tests.Primitives M53 = 3F, M54 = 3F }; - } - private ColorMatrix CreateAllFours() - { - return new ColorMatrix + private static ColorMatrix CreateAllFours() + => new() { M11 = 4F, M12 = 4F, @@ -269,6 +260,5 @@ namespace SixLabors.ImageSharp.Tests.Primitives M53 = 4F, M54 = 4F }; - } } } diff --git a/tests/ImageSharp.Tests/Processing/Convolution/DetectEdgesTest.cs b/tests/ImageSharp.Tests/Processing/Convolution/DetectEdgesTest.cs index b8668b2117..9e0fd2d938 100644 --- a/tests/ImageSharp.Tests/Processing/Convolution/DetectEdgesTest.cs +++ b/tests/ImageSharp.Tests/Processing/Convolution/DetectEdgesTest.cs @@ -1,7 +1,6 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. -using System.Diagnostics.CodeAnalysis; using SixLabors.ImageSharp.Processing; using SixLabors.ImageSharp.Processing.Processors.Convolution; using Xunit; @@ -9,7 +8,6 @@ using Xunit; namespace SixLabors.ImageSharp.Tests.Processing.Convolution { [Trait("Category", "Processors")] - [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "OK. Used for TheoryData compatibility.")] public class DetectEdgesTest : BaseImageOperationsExtensionTest { [Fact] @@ -32,8 +30,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Convolution Assert.Equal(KnownEdgeDetectorKernels.Sobel, processor.Kernel); } - public static TheoryData EdgeDetector2DKernelData = - new TheoryData + public static TheoryData EdgeDetector2DKernelData { get; } = + new() { { KnownEdgeDetectorKernels.Kayyali, true }, { KnownEdgeDetectorKernels.Kayyali, false }, @@ -91,8 +89,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Convolution Assert.Equal(kernel, processor.Kernel); } - public static TheoryData EdgeDetectorKernelData = - new TheoryData + public static TheoryData EdgeDetectorKernelData { get; } = + new() { { KnownEdgeDetectorKernels.Laplacian3x3, true }, { KnownEdgeDetectorKernels.Laplacian3x3, false }, @@ -146,8 +144,8 @@ namespace SixLabors.ImageSharp.Tests.Processing.Convolution Assert.Equal(kernel, processor.Kernel); } - public static TheoryData EdgeDetectorCompassKernelData = - new TheoryData + public static TheoryData EdgeDetectorCompassKernelData { get; } = + new() { { KnownEdgeDetectorKernels.Kirsch, true }, { KnownEdgeDetectorKernels.Kirsch, false }, diff --git a/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs b/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs index 90f4074845..02bdb82678 100644 --- a/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs +++ b/tests/ImageSharp.Tests/Processing/Processors/Convolution/DetectEdgesTest.cs @@ -13,7 +13,6 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution { [Trait("Category", "Processors")] [GroupOutput("Convolution")] - [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "OK. Used for TheoryData compatibility.")] public class DetectEdgesTest { private static readonly ImageComparer OpaqueComparer = ImageComparer.TolerantPercentage(0.01F); diff --git a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataCurves.cs b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataCurves.cs index d79880d8cc..1b91a11b59 100644 --- a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataCurves.cs +++ b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataCurves.cs @@ -8,7 +8,6 @@ namespace SixLabors.ImageSharp.Tests { internal static class IccTestDataCurves { -#pragma warning disable SA1118 // Parameter should not span multiple lines /// /// Channels: 3 /// @@ -26,7 +25,6 @@ namespace SixLabors.ImageSharp.Tests new IccResponseNumber[] { IccTestDataNonPrimitives.ResponseNumber_Val3, IccTestDataNonPrimitives.ResponseNumber_Val4 }, new IccResponseNumber[] { IccTestDataNonPrimitives.ResponseNumber_Val5, IccTestDataNonPrimitives.ResponseNumber_Val6 }, }); -#pragma warning restore SA1118 // Parameter should not span multiple lines /// /// Channels: 3 diff --git a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs index 1710dba9ae..feabf0e194 100644 --- a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs +++ b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataProfiles.cs @@ -78,7 +78,6 @@ namespace SixLabors.ImageSharp.Tests 0x64, 0x63, 0x62, 0x61, // CreatorSignature }, profileId, -#pragma warning disable SA1118 // Parameter should not span multiple lines new byte[] { // Padding @@ -93,12 +92,10 @@ namespace SixLabors.ImageSharp.Tests (byte)(nrOfEntries >> 8), (byte)nrOfEntries }); -#pragma warning restore SA1118 // Parameter should not span multiple lines } public static readonly byte[] Profile_Random_Array = ArrayHelper.Concat( CreateHeaderRandomArray(168, 2, Profile_Random_Id_Array), -#pragma warning disable SA1118 // Parameter should not span multiple lines new byte[] { 0x00, 0x00, 0x00, 0x00, // tag signature (Unknown) @@ -108,7 +105,6 @@ namespace SixLabors.ImageSharp.Tests 0x00, 0x00, 0x00, 0x9C, // tag offset (156) 0x00, 0x00, 0x00, 0x0C, // tag size (12) }, -#pragma warning restore SA1118 // Parameter should not span multiple lines IccTestDataTagDataEntry.TagDataEntryHeader_UnknownArr, IccTestDataTagDataEntry.Unknown_Arr); diff --git a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataTagDataEntry.cs b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataTagDataEntry.cs index 9cbf4e1206..9b56bb98cb 100644 --- a/tests/ImageSharp.Tests/TestDataIcc/IccTestDataTagDataEntry.cs +++ b/tests/ImageSharp.Tests/TestDataIcc/IccTestDataTagDataEntry.cs @@ -237,8 +237,6 @@ namespace SixLabors.ImageSharp.Tests new IccCurveTagDataEntry[] { Curve_Val_1, Curve_Val_2, Curve_Val_0 }, IccTestDataLut.CLUT_Val16, new IccCurveTagDataEntry[] { Curve_Val_2, Curve_Val_1 }); - -#pragma warning disable SA1115 // Parameter should follow comma public static readonly byte[] LutAToB_Arr = ArrayHelper.Concat( new byte[] { 0x02, 0x03, 0x00, 0x00 }, new byte[] { 0x00, 0x00, 0x00, 0x20 }, // b: 32 @@ -275,7 +273,6 @@ namespace SixLabors.ImageSharp.Tests CurveFull_1, // 14 bytes new byte[] { 0x00, 0x00 }); // Padding -#pragma warning restore SA1115 // Parameter should follow comma public static readonly object[][] LutAToBTagDataEntryTestData = { @@ -293,8 +290,6 @@ namespace SixLabors.ImageSharp.Tests null, IccTestDataLut.CLUT_Val16, new[] { Curve_Val_2, Curve_Val_1, Curve_Val_0 }); - -#pragma warning disable SA1115 // Parameter should follow comma public static readonly byte[] LutBToA_Arr = ArrayHelper.Concat( new byte[] { 0x02, 0x03, 0x00, 0x00 }, @@ -320,7 +315,6 @@ namespace SixLabors.ImageSharp.Tests new byte[] { 0x00, 0x00 }, // Padding CurveFull_0); // 12 bytes -#pragma warning restore SA1115 // Parameter should follow comma public static readonly object[][] LutBToATagDataEntryTestData = { diff --git a/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs b/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs index 4dd1e010fc..511b2cef5e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs @@ -122,7 +122,6 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities protected override void Dispose(bool disposing) => this.innerStream.Dispose(); -#if NETCOREAPP public override void CopyTo(Stream destination, int bufferSize) => this.Await(() => this.innerStream.CopyTo(destination, bufferSize)); public override int Read(Span buffer) @@ -140,6 +139,5 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities } public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) => this.Await(() => this.innerStream.WriteAsync(buffer, cancellationToken)); -#endif } } diff --git a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs index e78659aa6a..e0a6ec860c 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs @@ -12,7 +12,6 @@ namespace SixLabors.ImageSharp.Tests [Flags] public enum PixelTypes { -#pragma warning disable SA1602 // Enumeration items should be documented Undefined = 0, A8 = 1 << 0, @@ -75,7 +74,5 @@ namespace SixLabors.ImageSharp.Tests // "All" is handled as a separate, individual case instead of using bitwise OR All = 30 - -#pragma warning restore SA1602 // Enumeration items should be documented } } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs index 0bb0cf2626..86dfb11eee 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs @@ -781,7 +781,6 @@ namespace SixLabors.ImageSharp.Tests internal class AllocatorBufferCapacityConfigurator { -#pragma warning disable CS0618 // 'ArrayPoolMemoryAllocator' is obsolete private readonly TestMemoryAllocator allocator; private readonly int pixelSizeInBytes; @@ -790,7 +789,6 @@ namespace SixLabors.ImageSharp.Tests this.allocator = allocator; this.pixelSizeInBytes = pixelSizeInBytes; } -#pragma warning restore CS0618 public void InBytes(int totalBytes) => this.allocator.BufferCapacityInBytes = totalBytes;