Skip to content

.NET Standard 2.0 support #2651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ImageSharp/Color/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static Color FromPixel<TPixel>(TPixel pixel)
where TPixel : unmanaged, IPixel<TPixel>
{
// Avoid boxing in case we can convert to Vector4 safely and efficiently
PixelTypeInfo info = TPixel.GetPixelTypeInfo();
PixelTypeInfo info = Extensions.GetPixelTypeInfo<TPixel>();
if (info.ComponentInfo.HasValue && info.ComponentInfo.Value.GetMaximumComponentPrecision() <= (int)PixelComponentBitDepth.Bit32)
{
return new(pixel.ToScaledVector4());
Expand All @@ -117,7 +117,7 @@ public static void FromPixel<TPixel>(ReadOnlySpan<TPixel> source, Span<Color> de
Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination));

// Avoid boxing in case we can convert to Vector4 safely and efficiently
PixelTypeInfo info = TPixel.GetPixelTypeInfo();
PixelTypeInfo info = Extensions.GetPixelTypeInfo<TPixel>();
if (info.ComponentInfo.HasValue && info.ComponentInfo.Value.GetMaximumComponentPrecision() <= (int)PixelComponentBitDepth.Bit32)
{
for (int i = 0; i < destination.Length; i++)
Expand Down
18 changes: 12 additions & 6 deletions src/ImageSharp/ColorSpaces/Companding/SRgbCompanding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static class SRgbCompanding
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Expand(Span<Vector4> vectors)
{
#if USE_SIMD_INTRINSICS
if (Avx2.IsSupported && vectors.Length >= 2)
{
CompandAvx2(vectors, ExpandTable);
Expand All @@ -92,6 +93,7 @@ public static void Expand(Span<Vector4> vectors)
}
}
else
#endif
{
CompandScalar(vectors, ExpandTable);
}
Expand All @@ -104,6 +106,7 @@ public static void Expand(Span<Vector4> vectors)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static unsafe void Compress(Span<Vector4> vectors)
{
#if USE_SIMD_INTRINSICS
if (Avx2.IsSupported && vectors.Length >= 2)
{
CompandAvx2(vectors, CompressTable);
Expand All @@ -115,6 +118,7 @@ public static unsafe void Compress(Span<Vector4> vectors)
}
}
else
#endif
{
CompandScalar(vectors, CompressTable);
}
Expand Down Expand Up @@ -164,18 +168,19 @@ public static float Expand(float channel)
public static float Compress(float channel)
=> channel <= 0.0031308F ? 12.92F * channel : (1.055F * MathF.Pow(channel, 0.416666666666667F)) - 0.055F;

#if USE_SIMD_INTRINSICS
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void CompandAvx2(Span<Vector4> vectors, float[] table)
{
fixed (float* tablePointer = &MemoryMarshal.GetArrayDataReference(table))
fixed (float* tablePointer = table)
{
var scale = Vector256.Create((float)Scale);
Vector256<float> zero = Vector256<float>.Zero;
var offset = Vector256.Create(1);

// Divide by 2 as 4 elements per Vector4 and 8 per Vector256<float>
ref Vector256<float> vectorsBase = ref Unsafe.As<Vector4, Vector256<float>>(ref MemoryMarshal.GetReference(vectors));
ref Vector256<float> vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length / 2u);
ref Vector256<float> vectorsLast = ref Extensions.UnsafeAdd(ref vectorsBase, (uint)vectors.Length / 2u);

while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast))
{
Expand All @@ -191,20 +196,21 @@ private static unsafe void CompandAvx2(Span<Vector4> vectors, float[] table)
// Alpha is already a linear representation of opacity so we do not want to convert it.
Vector256<float> companded = Numerics.Lerp(low, high, Avx.Subtract(multiplied, truncatedF));
vectorsBase = Avx.Blend(companded, vectorsBase, Numerics.BlendAlphaControl);
vectorsBase = ref Unsafe.Add(ref vectorsBase, 1);
vectorsBase = ref Extensions.UnsafeAdd(ref vectorsBase, 1);
}
}
}
#endif

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe void CompandScalar(Span<Vector4> vectors, float[] table)
{
fixed (float* tablePointer = &MemoryMarshal.GetArrayDataReference(table))
fixed (float* tablePointer = table)
{
Vector4 zero = Vector4.Zero;
var scale = new Vector4(Scale);
ref Vector4 vectorsBase = ref MemoryMarshal.GetReference(vectors);
ref Vector4 vectorsLast = ref Unsafe.Add(ref vectorsBase, (uint)vectors.Length);
ref Vector4 vectorsLast = ref Extensions.UnsafeAdd(ref vectorsBase, (uint)vectors.Length);

while (Unsafe.IsAddressLessThan(ref vectorsBase, ref vectorsLast))
{
Expand All @@ -223,7 +229,7 @@ private static unsafe void CompandScalar(Span<Vector4> vectors, float[] table)
vectorsBase.Y = Numerics.Lerp(tablePointer[i1], tablePointer[i1 + 1], f1 - (int)i1);
vectorsBase.Z = Numerics.Lerp(tablePointer[i2], tablePointer[i2 + 1], f2 - (int)i2);

vectorsBase = ref Unsafe.Add(ref vectorsBase, 1);
vectorsBase = ref Extensions.UnsafeAdd(ref vectorsBase, 1);
}
}
}
Expand Down
52 changes: 26 additions & 26 deletions src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public void Convert(ReadOnlySpan<CieLch> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieLch sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref CieLch sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -72,8 +72,8 @@ public void Convert(ReadOnlySpan<CieLchuv> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref CieLchuv sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -105,8 +105,8 @@ public void Convert(ReadOnlySpan<CieLuv> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref CieLuv sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -138,8 +138,8 @@ public void Convert(ReadOnlySpan<CieXyy> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref CieXyy sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -171,8 +171,8 @@ public void Convert(ReadOnlySpan<CieXyz> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref CieXyz sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -203,8 +203,8 @@ public void Convert(ReadOnlySpan<Cmyk> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref Cmyk sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -236,8 +236,8 @@ public void Convert(ReadOnlySpan<Hsl> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref Hsl sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -268,8 +268,8 @@ public void Convert(ReadOnlySpan<Hsv> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref Hsv sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -301,8 +301,8 @@ public void Convert(ReadOnlySpan<HunterLab> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref HunterLab sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -334,8 +334,8 @@ public void Convert(ReadOnlySpan<Lms> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref Lms sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -367,8 +367,8 @@ public void Convert(ReadOnlySpan<LinearRgb> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref LinearRgb sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -400,8 +400,8 @@ public void Convert(ReadOnlySpan<Rgb> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref Rgb sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down Expand Up @@ -433,8 +433,8 @@ public void Convert(ReadOnlySpan<YCbCr> source, Span<CieLab> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLab dp = ref Unsafe.Add(ref destRef, i);
ref YCbCr sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLab dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLab(sp);
}
}
Expand Down
52 changes: 26 additions & 26 deletions src/ImageSharp/ColorSpaces/Conversion/ColorSpaceConverter.CieLch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public void Convert(ReadOnlySpan<CieLab> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref CieLab sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -71,8 +71,8 @@ public void Convert(ReadOnlySpan<CieLchuv> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieLchuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref CieLchuv sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -104,8 +104,8 @@ public void Convert(ReadOnlySpan<CieLuv> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieLuv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref CieLuv sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -137,8 +137,8 @@ public void Convert(ReadOnlySpan<CieXyy> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieXyy sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref CieXyy sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -170,8 +170,8 @@ public void Convert(ReadOnlySpan<CieXyz> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref CieXyz sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref CieXyz sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -202,8 +202,8 @@ public void Convert(ReadOnlySpan<Cmyk> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Cmyk sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref Cmyk sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -235,8 +235,8 @@ public void Convert(ReadOnlySpan<Hsl> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Hsl sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref Hsl sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -268,8 +268,8 @@ public void Convert(ReadOnlySpan<Hsv> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Hsv sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref Hsv sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -301,8 +301,8 @@ public void Convert(ReadOnlySpan<HunterLab> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref HunterLab sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref HunterLab sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -334,8 +334,8 @@ public void Convert(ReadOnlySpan<LinearRgb> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref LinearRgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref LinearRgb sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -367,8 +367,8 @@ public void Convert(ReadOnlySpan<Lms> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Lms sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref Lms sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -400,8 +400,8 @@ public void Convert(ReadOnlySpan<Rgb> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref Rgb sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref Rgb sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down Expand Up @@ -433,8 +433,8 @@ public void Convert(ReadOnlySpan<YCbCr> source, Span<CieLch> destination)

for (nuint i = 0; i < (uint)count; i++)
{
ref YCbCr sp = ref Unsafe.Add(ref sourceRef, i);
ref CieLch dp = ref Unsafe.Add(ref destRef, i);
ref YCbCr sp = ref Extensions.UnsafeAdd(ref sourceRef, i);
ref CieLch dp = ref Extensions.UnsafeAdd(ref destRef, i);
dp = this.ToCieLch(sp);
}
}
Expand Down
Loading