From eabbc796fcf9b46830f2b7eed32bbc2f0da1426d Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Fri, 13 Apr 2018 10:26:03 -0700 Subject: [PATCH] Use new AsSpan overloads --- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 13 +++--- .../Formats/Png/Zlib/ZlibDeflateStream.cs | 21 ++-------- src/ImageSharp/Image.LoadPixelData.cs | 2 +- src/ImageSharp/ImageExtensions.cs | 2 +- src/ImageSharp/Memory/BasicArrayBuffer.cs | 2 +- .../DataReader/IccDataReader.Primitives.cs | 12 +++--- .../ImageSharp.Tests/Formats/Jpg/DCTTests.cs | 42 +++++++++---------- 7 files changed, 39 insertions(+), 55 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 676a93ee0..e8a42c0c8 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -5,7 +5,6 @@ using System; using System.Buffers.Binary; using System.IO; using System.Linq; -using System.Runtime.InteropServices; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Formats.Png.Filters; using SixLabors.ImageSharp.Formats.Png.Zlib; @@ -415,8 +414,8 @@ namespace SixLabors.ImageSharp.Formats.Png /// The . private void WriteHeaderChunk(Stream stream, in PngHeader header) { - BinaryPrimitives.WriteInt32BigEndian(new Span(this.chunkDataBuffer, 0, 4), header.Width); - BinaryPrimitives.WriteInt32BigEndian(new Span(this.chunkDataBuffer, 4, 4), header.Height); + BinaryPrimitives.WriteInt32BigEndian(this.chunkDataBuffer.AsSpan(0, 4), header.Width); + BinaryPrimitives.WriteInt32BigEndian(this.chunkDataBuffer.AsSpan(4, 4), header.Height); this.chunkDataBuffer[8] = header.BitDepth; this.chunkDataBuffer[9] = (byte)header.ColorType; @@ -500,8 +499,8 @@ namespace SixLabors.ImageSharp.Formats.Png int dpmX = (int)Math.Round(image.MetaData.HorizontalResolution * 39.3700787D); int dpmY = (int)Math.Round(image.MetaData.VerticalResolution * 39.3700787D); - BinaryPrimitives.WriteInt32BigEndian(new Span(this.chunkDataBuffer, 0, 4), dpmX); - BinaryPrimitives.WriteInt32BigEndian(new Span(this.chunkDataBuffer, 4, 4), dpmY); + BinaryPrimitives.WriteInt32BigEndian(this.chunkDataBuffer.AsSpan(0, 4), dpmX); + BinaryPrimitives.WriteInt32BigEndian(this.chunkDataBuffer.AsSpan(4, 4), dpmY); this.chunkDataBuffer[8] = 1; @@ -520,7 +519,7 @@ namespace SixLabors.ImageSharp.Formats.Png // 4-byte unsigned integer of gamma * 100,000. uint gammaValue = (uint)(this.gamma * 100_000F); - BinaryPrimitives.WriteUInt32BigEndian(new Span(this.chunkDataBuffer, 0, 4), gammaValue); + BinaryPrimitives.WriteUInt32BigEndian(this.chunkDataBuffer.AsSpan(0, 4), gammaValue); this.WriteChunk(stream, PngChunkTypes.Gamma, this.chunkDataBuffer, 0, 4); } @@ -643,7 +642,7 @@ namespace SixLabors.ImageSharp.Formats.Png { stream.Write(data, offset, length); - this.crc.Update(new ReadOnlySpan(data, offset, length)); + this.crc.Update(data.AsSpan(offset, length)); } BinaryPrimitives.WriteUInt32BigEndian(this.intBuffer, (uint)this.crc.Value); diff --git a/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs b/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs index 51e6b4859..8e0bac938 100644 --- a/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs +++ b/src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs @@ -113,26 +113,13 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib public override bool CanWrite => true; /// - public override long Length - { - get - { - throw new NotSupportedException(); - } - } + public override long Length => throw new NotSupportedException(); /// public override long Position { - get - { - throw new NotSupportedException(); - } - - set - { - throw new NotSupportedException(); - } + get => throw new NotSupportedException(); + set => throw new NotSupportedException(); } /// @@ -163,7 +150,7 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib public override void Write(byte[] buffer, int offset, int count) { this.deflateStream.Write(buffer, offset, count); - this.adler32.Update(new ReadOnlySpan(buffer, offset, count)); + this.adler32.Update(buffer.AsSpan(offset, count)); } /// diff --git a/src/ImageSharp/Image.LoadPixelData.cs b/src/ImageSharp/Image.LoadPixelData.cs index b0bb03580..0179e62ac 100644 --- a/src/ImageSharp/Image.LoadPixelData.cs +++ b/src/ImageSharp/Image.LoadPixelData.cs @@ -99,7 +99,7 @@ namespace SixLabors.ImageSharp public static Image LoadPixelData(Configuration config, TPixel[] data, int width, int height) where TPixel : struct, IPixel { - return LoadPixelData(config, new Span(data), width, height); + return LoadPixelData(config, data.AsSpan(), width, height); } /// diff --git a/src/ImageSharp/ImageExtensions.cs b/src/ImageSharp/ImageExtensions.cs index 1f7e418ad..2cdb71fc0 100644 --- a/src/ImageSharp/ImageExtensions.cs +++ b/src/ImageSharp/ImageExtensions.cs @@ -143,7 +143,7 @@ namespace SixLabors.ImageSharp /// Thrown if the stream is null. public static void SavePixelData(this ImageFrame source, TPixel[] buffer) where TPixel : struct, IPixel - => SavePixelData(source, new Span(buffer)); + => SavePixelData(source, buffer.AsSpan()); /// /// Saves the raw image pixels to a byte array in row-major order. diff --git a/src/ImageSharp/Memory/BasicArrayBuffer.cs b/src/ImageSharp/Memory/BasicArrayBuffer.cs index a4810d037..dd2f7ef86 100644 --- a/src/ImageSharp/Memory/BasicArrayBuffer.cs +++ b/src/ImageSharp/Memory/BasicArrayBuffer.cs @@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.Memory public int Length { get; } - public Span Span => new Span(this.Array, 0, this.Length); + public Span Span => this.Array.AsSpan(0, this.Length); /// /// Returns a reference to specified element of the buffer. diff --git a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs index 794d77ba1..482853b14 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs @@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// the value public ushort ReadUInt16() { - return BinaryPrimitives.ReadUInt16BigEndian(new Span(this.data, this.AddIndex(2), 2)); + return BinaryPrimitives.ReadUInt16BigEndian(this.data.AsSpan(this.AddIndex(2), 2)); } /// @@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// the value public short ReadInt16() { - return BinaryPrimitives.ReadInt16BigEndian(new Span(this.data, this.AddIndex(2), 2)); + return BinaryPrimitives.ReadInt16BigEndian(this.data.AsSpan(this.AddIndex(2), 2)); } /// @@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// the value public uint ReadUInt32() { - return BinaryPrimitives.ReadUInt32BigEndian(new Span(this.data, this.AddIndex(4), 4)); + return BinaryPrimitives.ReadUInt32BigEndian(this.data.AsSpan(this.AddIndex(4), 4)); } /// @@ -45,7 +45,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// the value public int ReadInt32() { - return BinaryPrimitives.ReadInt32BigEndian(new Span(this.data, this.AddIndex(4), 4)); + return BinaryPrimitives.ReadInt32BigEndian(this.data.AsSpan(this.AddIndex(4), 4)); } /// @@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// the value public ulong ReadUInt64() { - return BinaryPrimitives.ReadUInt64BigEndian(new Span(this.data, this.AddIndex(8), 8)); + return BinaryPrimitives.ReadUInt64BigEndian(this.data.AsSpan(this.AddIndex(8), 8)); } /// @@ -63,7 +63,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc /// the value public long ReadInt64() { - return BinaryPrimitives.ReadInt64BigEndian(new Span(this.data, this.AddIndex(8), 8)); + return BinaryPrimitives.ReadInt64BigEndian(this.data.AsSpan(this.AddIndex(8), 8)); } /// diff --git a/tests/ImageSharp.Tests/Formats/Jpg/DCTTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/DCTTests.cs index ee6f5305f..1c18df76c 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/DCTTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/DCTTests.cs @@ -1,15 +1,14 @@ // ReSharper disable InconsistentNaming -namespace SixLabors.ImageSharp.Tests.Formats.Jpg -{ - using System; +using System; - using SixLabors.ImageSharp.Formats.Jpeg.Common; - using SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components; - using SixLabors.ImageSharp.Tests.Formats.Jpg.Utils; +using SixLabors.ImageSharp.Formats.Jpeg.Common; +using SixLabors.ImageSharp.Tests.Formats.Jpg.Utils; - using Xunit; - using Xunit.Abstractions; +using Xunit; +using Xunit.Abstractions; +namespace SixLabors.ImageSharp.Tests.Formats.Jpg +{ public static class DCTTests { public class FastFloatingPoint : JpegFixture @@ -19,7 +18,6 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg { } - [Fact] public void iDCT2D8x4_LeftPart() { @@ -28,10 +26,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg ReferenceImplementations.LLM_FloatingPoint_DCT.iDCT2D8x4_32f(sourceArray, expectedDestArray); - Block8x8F source = new Block8x8F(); + var source = new Block8x8F(); source.LoadFrom(sourceArray); - Block8x8F dest = new Block8x8F(); + var dest = new Block8x8F(); FastFloatingPointDCT.IDCT8x4_LeftPart(ref source, ref dest); @@ -51,12 +49,12 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg float[] sourceArray = JpegFixture.Create8x8FloatData(); float[] expectedDestArray = new float[64]; - ReferenceImplementations.LLM_FloatingPoint_DCT.iDCT2D8x4_32f(sourceArray.AsSpan().Slice(4), expectedDestArray.AsSpan().Slice(4)); + ReferenceImplementations.LLM_FloatingPoint_DCT.iDCT2D8x4_32f(sourceArray.AsSpan(4), expectedDestArray.AsSpan(4)); - Block8x8F source = new Block8x8F(); + var source = new Block8x8F(); source.LoadFrom(sourceArray); - Block8x8F dest = new Block8x8F(); + var dest = new Block8x8F(); FastFloatingPointDCT.IDCT8x4_RightPart(ref source, ref dest); @@ -115,10 +113,10 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg public void FDCT8x4_LeftPart(int seed) { Span src = JpegFixture.Create8x8RoundedRandomFloatData(-200, 200, seed); - Block8x8F srcBlock = new Block8x8F(); + var srcBlock = new Block8x8F(); srcBlock.LoadFrom(src); - Block8x8F destBlock = new Block8x8F(); + var destBlock = new Block8x8F(); float[] expectedDest = new float[64]; @@ -137,14 +135,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg public void FDCT8x4_RightPart(int seed) { Span src = JpegFixture.Create8x8RoundedRandomFloatData(-200, 200, seed); - Block8x8F srcBlock = new Block8x8F(); + var srcBlock = new Block8x8F(); srcBlock.LoadFrom(src); - Block8x8F destBlock = new Block8x8F(); + var destBlock = new Block8x8F(); float[] expectedDest = new float[64]; - ReferenceImplementations.LLM_FloatingPoint_DCT.fDCT2D8x4_32f(src.Slice(4), expectedDest.AsSpan().Slice(4)); + ReferenceImplementations.LLM_FloatingPoint_DCT.fDCT2D8x4_32f(src.Slice(4), expectedDest.AsSpan(4)); FastFloatingPointDCT.FDCT8x4_RightPart(ref srcBlock, ref destBlock); float[] actualDest = new float[64]; @@ -159,14 +157,14 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg public void TransformFDCT(int seed) { Span src = JpegFixture.Create8x8RoundedRandomFloatData(-200, 200, seed); - Block8x8F srcBlock = new Block8x8F(); + var srcBlock = new Block8x8F(); srcBlock.LoadFrom(src); - Block8x8F destBlock = new Block8x8F(); + var destBlock = new Block8x8F(); float[] expectedDest = new float[64]; float[] temp1 = new float[64]; - Block8x8F temp2 = new Block8x8F(); + var temp2 = new Block8x8F(); ReferenceImplementations.LLM_FloatingPoint_DCT.fDCT2D_llm(src, expectedDest, temp1, downscaleBy8: true); FastFloatingPointDCT.TransformFDCT(ref srcBlock, ref destBlock, ref temp2, false);