Skip to content
Open
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
13 changes: 13 additions & 0 deletions src/ImageSharp/PixelFormats/PixelImplementations/Rgba32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,19 @@ private static Rgba32 Pack(Vector4 vector)
vector += Half;
vector = ColorNumerics.ClampToFinite(vector, Vector4.Zero, MaxBytes);

if (Vector128.IsHardwareAccelerated)
{
// The clamp leaves every lane finite and in [0, 255] (NaN maps to 0). For such values the native truncating
// conversion gives the same integers as the saturating conversion, without its handling of out-of-range lanes.
// The shuffle moves the low byte of each 32-bit lane into the first four bytes (indices above 15 give 0), which
// is the R, G, B, A field order of this struct. The indices are constants, so the JIT emits one byte shuffle.
Vector128<byte> lanes = Vector128.ConvertToInt32Native(vector.AsVector128()).AsByte();
Vector128<byte> lowBytes = Vector128.Create((byte)0, 4, 8, 12, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
uint packed = Vector128.Shuffle(lanes, lowBytes).AsUInt32().ToScalar();
return Unsafe.As<uint, Rgba32>(ref packed);
}

// Without acceleration the software shuffle costs more than four element reads.
Vector128<byte> result = Vector128.ConvertToInt32(vector.AsVector128()).AsByte();
return new Rgba32(result.GetElement(0), result.GetElement(4), result.GetElement(8), result.GetElement(12));
}
Expand Down
36 changes: 36 additions & 0 deletions tests/ImageSharp.Tests/PixelFormats/Rgba32Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Tests.TestUtilities;

namespace SixLabors.ImageSharp.Tests.PixelFormats;

Expand Down Expand Up @@ -135,6 +136,41 @@ public void Rgba32_ToScaledVector4()
Assert.Equal(1, actual.W);
}

[Fact]
public void Rgba32_FromVector4_RoundsClampsAndMapsNaNToZero() =>

// Pack has a vector path and a path for runtimes without acceleration; both must follow the rule.
FeatureTestRunner.RunWithHwIntrinsicsFeature(
RunFromVector4RoundsClampsAndMapsNaNToZero,
HwIntrinsics.AllowAll | HwIntrinsics.DisableHWIntrinsic);

private static void RunFromVector4RoundsClampsAndMapsNaNToZero()
{
// Each component: scale by 255, add one half, clamp to [0, 255] with NaN as 0, truncate.
static byte Expected(float x)
{
float scaled = (x * byte.MaxValue) + .5F;
return (byte)(scaled > byte.MaxValue ? byte.MaxValue : scaled >= 0F ? scaled : 0F);
}

float[] specials =
[
float.NaN, float.PositiveInfinity, float.NegativeInfinity, -0F, 0F, 1F, -1F, 2F, 1e30F, -1e30F, float.Epsilon,
.5F / byte.MaxValue, 1.5F / byte.MaxValue, 254.5F / byte.MaxValue, 1F - 1e-7F, 1F + 1e-7F
];

Random random = new(7);
for (int i = 0; i < 20_000; i++)
{
float Next() => i % 2 == 0 ? specials[random.Next(specials.Length)] : (float)((random.NextDouble() * 1.5) - .25);
Vector4 source = new(Next(), Next(), Next(), Next());

Rgba32 actual = Rgba32.FromVector4(source);

Assert.Equal(new Rgba32(Expected(source.X), Expected(source.Y), Expected(source.Z), Expected(source.W)), actual);
}
}

[Fact]
public void Rgba32_FromScaledVector4()
{
Expand Down
Loading