Skip to content

Commit 5e060ba

Browse files
Merge pull request #1886 from ynse01/add-Abgr32-pixel-type
Add Abgr32 pixel type
2 parents ecd3db2 + a94b8c4 commit 5e060ba

File tree

71 files changed

+2129
-151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2129
-151
lines changed

src/ImageSharp/Advanced/AotCompilerTools.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ private static void SeedPixelFormats()
7575

7676
Seed<A8>();
7777
Seed<Argb32>();
78+
Seed<Abgr32>();
7879
Seed<Bgr24>();
7980
Seed<Bgr565>();
8081
Seed<Bgra32>();
@@ -149,6 +150,7 @@ private static unsafe void AotCompileImage<TPixel>()
149150
Image<TPixel> img = default;
150151
img.CloneAs<A8>(default);
151152
img.CloneAs<Argb32>(default);
153+
img.CloneAs<Abgr32>(default);
152154
img.CloneAs<Bgr24>(default);
153155
img.CloneAs<Bgr565>(default);
154156
img.CloneAs<Bgra32>(default);

src/ImageSharp/Color/Color.Conversions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ public Color(Bgra32 pixel)
8989
this.boxedHighPrecisionPixel = null;
9090
}
9191

92+
/// <summary>
93+
/// Initializes a new instance of the <see cref="Color"/> struct.
94+
/// </summary>
95+
/// <param name="pixel">The <see cref="Abgr32"/> containing the color information.</param>
96+
[MethodImpl(InliningOptions.ShortMethod)]
97+
public Color(Abgr32 pixel)
98+
{
99+
this.data = new Rgba64(pixel);
100+
this.boxedHighPrecisionPixel = null;
101+
}
102+
92103
/// <summary>
93104
/// Initializes a new instance of the <see cref="Color"/> struct.
94105
/// </summary>
@@ -177,6 +188,19 @@ internal Argb32 ToArgb32()
177188
return value;
178189
}
179190

191+
[MethodImpl(InliningOptions.ShortMethod)]
192+
internal Abgr32 ToAbgr32()
193+
{
194+
if (this.boxedHighPrecisionPixel is null)
195+
{
196+
return this.data.ToAbgr32();
197+
}
198+
199+
Abgr32 value = default;
200+
value.FromScaledVector4(this.boxedHighPrecisionPixel.ToScaledVector4());
201+
return value;
202+
}
203+
180204
[MethodImpl(InliningOptions.ShortMethod)]
181205
internal Rgb24 ToRgb24()
182206
{

src/ImageSharp/Common/Helpers/Numerics.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,5 +907,61 @@ ref MemoryMarshal.GetReference(Log2DeBruijn),
907907
/// <param name="divisor">Divisor value.</param>
908908
/// <returns>Ceiled division result.</returns>
909909
public static uint DivideCeil(uint value, uint divisor) => (value + divisor - 1) / divisor;
910+
911+
/// <summary>
912+
/// Rotates the specified value left by the specified number of bits.
913+
/// </summary>
914+
/// <param name="value">The value to rotate.</param>
915+
/// <param name="offset">The number of bits to rotate with.</param>
916+
/// <returns>The rotated value.</returns>
917+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
918+
public static uint RotateLeft(uint value, int offset)
919+
{
920+
#if SUPPORTS_BITOPERATIONS
921+
return BitOperations.RotateLeft(value, offset);
922+
#else
923+
return RotateLeftSoftwareFallback(value, offset);
924+
#endif
925+
}
926+
927+
#if !SUPPORTS_BITOPERATIONS
928+
/// <summary>
929+
/// Rotates the specified value left by the specified number of bits.
930+
/// </summary>
931+
/// <param name="value">The value to rotate.</param>
932+
/// <param name="offset">The number of bits to rotate with.</param>
933+
/// <returns>The rotated value.</returns>
934+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
935+
public static uint RotateLeftSoftwareFallback(uint value, int offset)
936+
=> (value << offset) | (value >> (32 - offset));
937+
#endif
938+
939+
/// <summary>
940+
/// Rotates the specified value right by the specified number of bits.
941+
/// </summary>
942+
/// <param name="value">The value to rotate.</param>
943+
/// <param name="offset">The number of bits to rotate with.</param>
944+
/// <returns>The rotated value.</returns>
945+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
946+
public static uint RotateRight(uint value, int offset)
947+
{
948+
#if SUPPORTS_BITOPERATIONS
949+
return BitOperations.RotateRight(value, offset);
950+
#else
951+
return RotateRightSoftwareFallback(value, offset);
952+
#endif
953+
}
954+
955+
#if !SUPPORTS_BITOPERATIONS
956+
/// <summary>
957+
/// Rotates the specified value right by the specified number of bits.
958+
/// </summary>
959+
/// <param name="value">The value to rotate.</param>
960+
/// <param name="offset">The number of bits to rotate with.</param>
961+
/// <returns>The rotated value.</returns>
962+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
963+
public static uint RotateRightSoftwareFallback(uint value, int offset)
964+
=> (value >> offset) | (value << (32 - offset));
965+
#endif
910966
}
911967
}

src/ImageSharp/Common/Helpers/Shuffle/IComponentShuffle.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
157157

158158
// packed = [W Z Y X]
159159
// ROTR(8, packedArgb) = [Y Z W X]
160-
Unsafe.Add(ref dBase, i) = (packed >> 8) | (packed << 24);
160+
Unsafe.Add(ref dBase, i) = Numerics.RotateRight(packed, 8);
161161
}
162162
}
163163
}
@@ -188,7 +188,40 @@ public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
188188
// tmp1 + tmp3 = [W X Y Z]
189189
uint tmp1 = packed & 0xFF00FF00;
190190
uint tmp2 = packed & 0x00FF00FF;
191-
uint tmp3 = (tmp2 << 16) | (tmp2 >> 16);
191+
uint tmp3 = Numerics.RotateLeft(tmp2, 16);
192+
193+
Unsafe.Add(ref dBase, i) = tmp1 + tmp3;
194+
}
195+
}
196+
}
197+
198+
internal readonly struct XWZYShuffle4 : IShuffle4
199+
{
200+
public byte Control
201+
{
202+
[MethodImpl(InliningOptions.ShortMethod)]
203+
get => SimdUtils.Shuffle.MmShuffle(1, 2, 3, 0);
204+
}
205+
206+
[MethodImpl(InliningOptions.ShortMethod)]
207+
public void RunFallbackShuffle(ReadOnlySpan<byte> source, Span<byte> dest)
208+
{
209+
ref uint sBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(source));
210+
ref uint dBase = ref Unsafe.As<byte, uint>(ref MemoryMarshal.GetReference(dest));
211+
int n = source.Length / 4;
212+
213+
for (int i = 0; i < n; i++)
214+
{
215+
uint packed = Unsafe.Add(ref sBase, i);
216+
217+
// packed = [W Z Y X]
218+
// tmp1 = [0 Z 0 X]
219+
// tmp2 = [W 0 Y 0]
220+
// tmp3=ROTL(16, tmp2) = [Y 0 W 0]
221+
// tmp1 + tmp3 = [Y Z W X]
222+
uint tmp1 = packed & 0x00FF00FF;
223+
uint tmp2 = packed & 0xFF00FF00;
224+
uint tmp3 = Numerics.RotateLeft(tmp2, 16);
192225

193226
Unsafe.Add(ref dBase, i) = tmp1 + tmp3;
194227
}

src/ImageSharp/ImageSharp.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@
7575
<AutoGen>True</AutoGen>
7676
<DependentUpon>Block8x8F.Generated.tt</DependentUpon>
7777
</Compile>
78+
<Compile Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Abgr32.PixelOperations.Generated.cs">
79+
<DesignTime>True</DesignTime>
80+
<AutoGen>True</AutoGen>
81+
<DependentUpon>Abgr32.PixelOperations.Generated.tt</DependentUpon>
82+
</Compile>
7883
<Compile Update="PixelFormats\PixelOperations{TPixel}.Generated.cs">
7984
<DesignTime>True</DesignTime>
8085
<AutoGen>True</AutoGen>
@@ -166,6 +171,10 @@
166171
<Generator>TextTemplatingFileGenerator</Generator>
167172
<LastGenOutput>Block8x8F.Generated.cs</LastGenOutput>
168173
</None>
174+
<None Update="PixelFormats\PixelImplementations\PixelOperations\Generated\Abgr32.PixelOperations.Generated.tt">
175+
<Generator>TextTemplatingFileGenerator</Generator>
176+
<LastGenOutput>Abgr32.PixelOperations.Generated.cs</LastGenOutput>
177+
</None>
169178
<None Update="PixelFormats\PixelOperations{TPixel}.Generated.tt">
170179
<Generator>TextTemplatingFileGenerator</Generator>
171180
<LastGenOutput>PixelOperations{TPixel}.Generated.cs</LastGenOutput>

src/ImageSharp/PixelFormats/IPixel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ public interface IPixel
7979
/// <param name="source">The <see cref="Bgra32"/> value.</param>
8080
void FromBgra32(Bgra32 source);
8181

82+
/// <summary>
83+
/// Initializes the pixel instance from an <see cref="Abgr32"/> value.
84+
/// </summary>
85+
/// <param name="source">The <see cref="Abgr32"/> value.</param>
86+
void FromAbgr32(Abgr32 source);
87+
8288
/// <summary>
8389
/// Initializes the pixel instance from an <see cref="L8"/> value.
8490
/// </summary>

src/ImageSharp/PixelFormats/PixelImplementations/A8.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public partial struct A8 : IPixel<A8>, IPackedVector<byte>
8787
[MethodImpl(InliningOptions.ShortMethod)]
8888
public void FromBgra32(Bgra32 source) => this.PackedValue = source.A;
8989

90+
/// <inheritdoc/>
91+
[MethodImpl(InliningOptions.ShortMethod)]
92+
public void FromAbgr32(Abgr32 source) => this.PackedValue = source.A;
93+
9094
/// <inheritdoc/>
9195
[MethodImpl(InliningOptions.ShortMethod)]
9296
public void FromBgra5551(Bgra5551 source) => this.FromScaledVector4(source.ToScaledVector4());

0 commit comments

Comments
 (0)