Browse Source

Use new AsSpan overloads

af/merge-core
Jason Nelson 8 years ago
parent
commit
eabbc796fc
  1. 13
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  2. 21
      src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs
  3. 2
      src/ImageSharp/Image.LoadPixelData.cs
  4. 2
      src/ImageSharp/ImageExtensions.cs
  5. 2
      src/ImageSharp/Memory/BasicArrayBuffer.cs
  6. 12
      src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs
  7. 42
      tests/ImageSharp.Tests/Formats/Jpg/DCTTests.cs

13
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
/// <param name="header">The <see cref="PngHeader"/>.</param>
private void WriteHeaderChunk(Stream stream, in PngHeader header)
{
BinaryPrimitives.WriteInt32BigEndian(new Span<byte>(this.chunkDataBuffer, 0, 4), header.Width);
BinaryPrimitives.WriteInt32BigEndian(new Span<byte>(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<byte>(this.chunkDataBuffer, 0, 4), dpmX);
BinaryPrimitives.WriteInt32BigEndian(new Span<byte>(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<byte>(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<byte>(data, offset, length));
this.crc.Update(data.AsSpan(offset, length));
}
BinaryPrimitives.WriteUInt32BigEndian(this.intBuffer, (uint)this.crc.Value);

21
src/ImageSharp/Formats/Png/Zlib/ZlibDeflateStream.cs

@ -113,26 +113,13 @@ namespace SixLabors.ImageSharp.Formats.Png.Zlib
public override bool CanWrite => true;
/// <inheritdoc/>
public override long Length
{
get
{
throw new NotSupportedException();
}
}
public override long Length => throw new NotSupportedException();
/// <inheritdoc/>
public override long Position
{
get
{
throw new NotSupportedException();
}
set
{
throw new NotSupportedException();
}
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
/// <inheritdoc/>
@ -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<byte>(buffer, offset, count));
this.adler32.Update(buffer.AsSpan(offset, count));
}
/// <inheritdoc/>

2
src/ImageSharp/Image.LoadPixelData.cs

@ -99,7 +99,7 @@ namespace SixLabors.ImageSharp
public static Image<TPixel> LoadPixelData<TPixel>(Configuration config, TPixel[] data, int width, int height)
where TPixel : struct, IPixel<TPixel>
{
return LoadPixelData(config, new Span<TPixel>(data), width, height);
return LoadPixelData(config, data.AsSpan(), width, height);
}
/// <summary>

2
src/ImageSharp/ImageExtensions.cs

@ -143,7 +143,7 @@ namespace SixLabors.ImageSharp
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SavePixelData<TPixel>(this ImageFrame<TPixel> source, TPixel[] buffer)
where TPixel : struct, IPixel<TPixel>
=> SavePixelData(source, new Span<TPixel>(buffer));
=> SavePixelData(source, buffer.AsSpan());
/// <summary>
/// Saves the raw image pixels to a byte array in row-major order.

2
src/ImageSharp/Memory/BasicArrayBuffer.cs

@ -25,7 +25,7 @@ namespace SixLabors.ImageSharp.Memory
public int Length { get; }
public Span<T> Span => new Span<T>(this.Array, 0, this.Length);
public Span<T> Span => this.Array.AsSpan(0, this.Length);
/// <summary>
/// Returns a reference to specified element of the buffer.

12
src/ImageSharp/MetaData/Profiles/ICC/DataReader/IccDataReader.Primitives.cs

@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// <returns>the value</returns>
public ushort ReadUInt16()
{
return BinaryPrimitives.ReadUInt16BigEndian(new Span<byte>(this.data, this.AddIndex(2), 2));
return BinaryPrimitives.ReadUInt16BigEndian(this.data.AsSpan(this.AddIndex(2), 2));
}
/// <summary>
@ -27,7 +27,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// <returns>the value</returns>
public short ReadInt16()
{
return BinaryPrimitives.ReadInt16BigEndian(new Span<byte>(this.data, this.AddIndex(2), 2));
return BinaryPrimitives.ReadInt16BigEndian(this.data.AsSpan(this.AddIndex(2), 2));
}
/// <summary>
@ -36,7 +36,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// <returns>the value</returns>
public uint ReadUInt32()
{
return BinaryPrimitives.ReadUInt32BigEndian(new Span<byte>(this.data, this.AddIndex(4), 4));
return BinaryPrimitives.ReadUInt32BigEndian(this.data.AsSpan(this.AddIndex(4), 4));
}
/// <summary>
@ -45,7 +45,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// <returns>the value</returns>
public int ReadInt32()
{
return BinaryPrimitives.ReadInt32BigEndian(new Span<byte>(this.data, this.AddIndex(4), 4));
return BinaryPrimitives.ReadInt32BigEndian(this.data.AsSpan(this.AddIndex(4), 4));
}
/// <summary>
@ -54,7 +54,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// <returns>the value</returns>
public ulong ReadUInt64()
{
return BinaryPrimitives.ReadUInt64BigEndian(new Span<byte>(this.data, this.AddIndex(8), 8));
return BinaryPrimitives.ReadUInt64BigEndian(this.data.AsSpan(this.AddIndex(8), 8));
}
/// <summary>
@ -63,7 +63,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// <returns>the value</returns>
public long ReadInt64()
{
return BinaryPrimitives.ReadInt64BigEndian(new Span<byte>(this.data, this.AddIndex(8), 8));
return BinaryPrimitives.ReadInt64BigEndian(this.data.AsSpan(this.AddIndex(8), 8));
}
/// <summary>

42
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<float> 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<float> 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<float> 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);

Loading…
Cancel
Save