Skip to content
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

Add Abgr32 pixel type, extend IPixel<T> #1886

Merged
merged 12 commits into from
Dec 18, 2021
2 changes: 1 addition & 1 deletion src/ImageSharp/Common/Helpers/BitOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Runtime.CompilerServices;

#if !NETCOREAPP3_1
#if !NETCOREAPP3_1_OR_GREATER
namespace SixLabors.ImageSharp.Common.Helpers
{
/// <summary>
Expand Down
40 changes: 20 additions & 20 deletions src/ImageSharp/Common/Helpers/Shuffle/IComponentShuffle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
{
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
nuint n = (nuint)(uint)source.Length / 4;
nint n = (nint)source.Length / 4;
ynse01 marked this conversation as resolved.
Show resolved Hide resolved

for (nuint i = 0; i < n; i++)
for (nint i = 0; i < n; i++)
{
uint packed = Unsafe.Add(ref sBase, (int)i);
uint packed = Unsafe.Add(ref sBase, i);

// packed = [W Z Y X]
// ROTL(8, packed) = [Z Y X W]
Unsafe.Add(ref dBase, (int)i) = (packed << 8) | (packed >> 24);
Unsafe.Add(ref dBase, i) = (packed << 8) | (packed >> 24);
}
}
}
Expand All @@ -125,15 +125,15 @@ public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
{
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
nuint n = (nuint)(uint)source.Length / 4;
nint n = (nint)source.Length / 4;

for (nuint i = 0; i < n; i++)
for (nint i = 0; i < n; i++)
{
uint packed = Unsafe.Add(ref sBase, (int)i);
uint packed = Unsafe.Add(ref sBase, i);

// packed = [W Z Y X]
// REVERSE(packedArgb) = [X Y Z W]
Unsafe.Add(ref dBase, (int)i) = BinaryPrimitives.ReverseEndianness(packed);
Unsafe.Add(ref dBase, i) = BinaryPrimitives.ReverseEndianness(packed);
}
}
}
Expand All @@ -151,15 +151,15 @@ public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
{
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
nuint n = (nuint)(uint)source.Length / 4;
nint n = (nint)source.Length / 4;

for (nuint i = 0; i < n; i++)
for (nint i = 0; i < n; i++)
{
uint packed = Unsafe.Add(ref sBase, (int)i);
uint packed = Unsafe.Add(ref sBase, i);

// packed = [W Z Y X]
// ROTR(8, packedArgb) = [Y Z W X]
Unsafe.Add(ref dBase, (int)i) = (packed >> 8) | (packed << 24);
Unsafe.Add(ref dBase, i) = (packed >> 8) | (packed << 24);
}
}
}
Expand All @@ -177,11 +177,11 @@ public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
{
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
nuint n = (nuint)(uint)source.Length / 4;
nint n = (nint)source.Length / 4;

for (nuint i = 0; i < n; i++)
for (nint i = 0; i < n; i++)
{
uint packed = Unsafe.Add(ref sBase, (int)i);
uint packed = Unsafe.Add(ref sBase, i);

// packed = [W Z Y X]
// tmp1 = [W 0 Y 0]
Expand All @@ -192,7 +192,7 @@ public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
uint tmp2 = packed & 0x00FF00FF;
uint tmp3 = BitOperations.RotateLeft(tmp2, 16);

Unsafe.Add(ref dBase, (int)i) = tmp1 + tmp3;
Unsafe.Add(ref dBase, i) = tmp1 + tmp3;
}
}
}
Expand All @@ -210,11 +210,11 @@ public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
{
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
nuint n = (nuint)(uint)source.Length / 4;
nint n = (nint)source.Length / 4;

for (nuint i = 0; i < n; i++)
for (nint i = 0; i < n; i++)
{
uint packed = Unsafe.Add(ref sBase, (int)i);
uint packed = Unsafe.Add(ref sBase, i);

// packed = [W Z Y X]
// tmp1 = [0 Z 0 X]
Expand All @@ -225,7 +225,7 @@ public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
uint tmp2 = packed & 0xFF00FF00;
uint tmp3 = BitOperations.RotateLeft(tmp2, 16);

Unsafe.Add(ref dBase, (int)i) = tmp1 + tmp3;
Unsafe.Add(ref dBase, i) = tmp1 + tmp3;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ public override void ToL8(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref L8 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i);
ref L8 dp = ref Unsafe.Add(ref destRef, i);

dp.FromAbgr32(sp);
}
Expand All @@ -226,8 +226,8 @@ public override void ToL16(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref L16 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i);
ref L16 dp = ref Unsafe.Add(ref destRef, i);

dp.FromAbgr32(sp);
}
Expand All @@ -246,8 +246,8 @@ public override void ToLa16(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref La16 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i);
ref La16 dp = ref Unsafe.Add(ref destRef, i);

dp.FromAbgr32(sp);
}
Expand All @@ -266,8 +266,8 @@ public override void ToLa32(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref La32 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i);
ref La32 dp = ref Unsafe.Add(ref destRef, i);

dp.FromAbgr32(sp);
}
Expand All @@ -286,8 +286,8 @@ public override void ToRgb48(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref Rgb48 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);

dp.FromAbgr32(sp);
}
Expand All @@ -306,8 +306,8 @@ public override void ToRgba64(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref Rgba64 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);

dp.FromAbgr32(sp);
}
Expand All @@ -326,8 +326,8 @@ public override void ToBgra5551(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Abgr32 sp = ref Unsafe.Add(ref sourceRef, i);
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);

dp.FromAbgr32(sp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ public override void ToL8(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref L8 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i);
ref L8 dp = ref Unsafe.Add(ref destRef, i);

dp.FromArgb32(sp);
}
Expand All @@ -226,8 +226,8 @@ public override void ToL16(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref L16 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i);
ref L16 dp = ref Unsafe.Add(ref destRef, i);

dp.FromArgb32(sp);
}
Expand All @@ -246,8 +246,8 @@ public override void ToLa16(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref La16 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i);
ref La16 dp = ref Unsafe.Add(ref destRef, i);

dp.FromArgb32(sp);
}
Expand All @@ -266,8 +266,8 @@ public override void ToLa32(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref La32 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i);
ref La32 dp = ref Unsafe.Add(ref destRef, i);

dp.FromArgb32(sp);
}
Expand All @@ -286,8 +286,8 @@ public override void ToRgb48(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref Rgb48 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);

dp.FromArgb32(sp);
}
Expand All @@ -306,8 +306,8 @@ public override void ToRgba64(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref Rgba64 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);

dp.FromArgb32(sp);
}
Expand All @@ -326,8 +326,8 @@ public override void ToBgra5551(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i);
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);

dp.FromArgb32(sp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ public override void ToL8(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref L8 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i);
ref L8 dp = ref Unsafe.Add(ref destRef, i);

dp.FromBgr24(sp);
}
Expand All @@ -226,8 +226,8 @@ public override void ToL16(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref L16 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i);
ref L16 dp = ref Unsafe.Add(ref destRef, i);

dp.FromBgr24(sp);
}
Expand All @@ -246,8 +246,8 @@ public override void ToLa16(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref La16 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i);
ref La16 dp = ref Unsafe.Add(ref destRef, i);

dp.FromBgr24(sp);
}
Expand All @@ -266,8 +266,8 @@ public override void ToLa32(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref La32 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i);
ref La32 dp = ref Unsafe.Add(ref destRef, i);

dp.FromBgr24(sp);
}
Expand All @@ -286,8 +286,8 @@ public override void ToRgb48(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref Rgb48 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgb48 dp = ref Unsafe.Add(ref destRef, i);

dp.FromBgr24(sp);
}
Expand All @@ -306,8 +306,8 @@ public override void ToRgba64(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref Rgba64 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i);
ref Rgba64 dp = ref Unsafe.Add(ref destRef, i);

dp.FromBgr24(sp);
}
Expand All @@ -326,8 +326,8 @@ public override void ToBgra5551(

for (nint i = 0; i < sourcePixels.Length; i++)
{
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, (int)i);
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, (int)i);
ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i);
ref Bgra5551 dp = ref Unsafe.Add(ref destRef, i);

dp.FromBgr24(sp);
}
Expand Down
Loading