mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.8 KiB
95 lines
2.8 KiB
using System.Buffers;
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace ImageSharp.Formats
|
|
{
|
|
/// <summary>
|
|
/// Like corefxlab Span, but with an AddOffset() method for efficiency.
|
|
/// TODO: When Span will be official, consider replacing this class!
|
|
/// </summary>
|
|
/// <see cref="https://github.com/dotnet/corefxlab/blob/master/src/System.Slices/System/Span.cs"/>
|
|
/// <typeparam name="T"></typeparam>
|
|
internal struct MutableSpan<T>
|
|
{
|
|
public T[] Data;
|
|
public int Offset;
|
|
|
|
public int TotalCount => Data.Length - Offset;
|
|
|
|
public MutableSpan(int size, int offset = 0)
|
|
{
|
|
Data = new T[size];
|
|
Offset = offset;
|
|
}
|
|
|
|
public MutableSpan(T[] data, int offset = 0)
|
|
{
|
|
Data = data;
|
|
Offset = offset;
|
|
}
|
|
|
|
public T this[int idx]
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] get { return Data[idx + Offset]; }
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)] set { Data[idx + Offset] = value; }
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public MutableSpan<T> Slice(int offset)
|
|
{
|
|
return new MutableSpan<T>(Data, Offset + offset);
|
|
}
|
|
|
|
public static implicit operator MutableSpan<T>(T[] data) => new MutableSpan<T>(data, 0);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public void AddOffset(int offset)
|
|
{
|
|
Offset += offset;
|
|
}
|
|
}
|
|
|
|
internal static class MutableSpanExtensions
|
|
{
|
|
public static MutableSpan<T> Slice<T>(this T[] array, int offset) => new MutableSpan<T>(array, offset);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void SaveTo(this MutableSpan<float> data, ref Vector4 v)
|
|
{
|
|
v.X = data[0];
|
|
v.Y = data[1];
|
|
v.Z = data[2];
|
|
v.W = data[3];
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void SaveTo(this MutableSpan<int> data, ref Vector4 v)
|
|
{
|
|
v.X = data[0];
|
|
v.Y = data[1];
|
|
v.Z = data[2];
|
|
v.W = data[3];
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void LoadFrom(this MutableSpan<float> data, ref Vector4 v)
|
|
{
|
|
data[0] = v.X;
|
|
data[1] = v.Y;
|
|
data[2] = v.Z;
|
|
data[3] = v.W;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static void LoadFrom(this MutableSpan<int> data, ref Vector4 v)
|
|
{
|
|
data[0] = (int)v.X;
|
|
data[1] = (int)v.Y;
|
|
data[2] = (int)v.Z;
|
|
data[3] = (int)v.W;
|
|
}
|
|
|
|
|
|
}
|
|
}
|