diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj
index 60f88a79c7..17f7bf58f3 100644
--- a/src/ImageSharp/ImageSharp.csproj
+++ b/src/ImageSharp/ImageSharp.csproj
@@ -55,10 +55,14 @@
TextTemplatingFileGenerator
Block8x8F.Generated.cs
-
+
TextTemplatingFileGenerator
PixelOperations{TPixel}.Generated.cs
+
+ TextTemplatingFileGenerator
+ Rgba32.PixelOperations.Generated.cs
+
@@ -69,10 +73,15 @@
True
Block8x8F.Generated.tt
-
+
True
True
PixelOperations{TPixel}.Generated.tt
+
+ True
+ True
+ Rgba32.PixelOperations.Generated.tt
+
\ No newline at end of file
diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.cs b/src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.cs
similarity index 100%
rename from src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.cs
rename to src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.cs
diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt b/src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.tt
similarity index 99%
rename from src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt
rename to src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.tt
index 9cff31936a..16292489fc 100644
--- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.Generated.tt
+++ b/src/ImageSharp/PixelFormats/Generated/PixelOperations{TPixel}.Generated.tt
@@ -10,8 +10,7 @@
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
-<#
-
+<#
void GenerateToDestFormatMethods(string pixelType)
{
#>
diff --git a/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs
new file mode 100644
index 0000000000..e42c575d89
--- /dev/null
+++ b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs
@@ -0,0 +1,130 @@
+//
+
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+namespace ImageSharp
+{
+ using System;
+ using System.Numerics;
+ using System.Runtime.CompilerServices;
+ using System.Runtime.InteropServices;
+
+ using ImageSharp.Memory;
+ using ImageSharp.PixelFormats;
+
+ ///
+ /// Provides optimized overrides for bulk operations.
+ ///
+ public partial struct Rgba32
+ {
+ internal partial class PixelOperations : PixelOperations
+ {
+
+ ///
+ internal override void PackFromRgb24(Span source, Span destPixels, int count)
+ {
+ GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count);
+
+ ref Rgb24 sourceRef = ref source.DangerousGetPinnableReference();
+ ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference();
+
+ for (int i = 0; i < count; i++)
+ {
+ ref Rgb24 sp = ref Unsafe.Add(ref sourceRef, i);
+ ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
+ Unsafe.As(ref dp) = sp; dp.A = 255;
+ }
+ }
+
+
+ ///
+ internal override void ToRgb24(Span sourcePixels, Span dest, int count)
+ {
+ GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count);
+
+ ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference();
+ ref Rgb24 destRef = ref dest.DangerousGetPinnableReference();
+
+ for (int i = 0; i < count; i++)
+ {
+ ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i);
+ ref Rgb24 dp = ref Unsafe.Add(ref destRef, i);
+ dp = Unsafe.As(ref sp);
+ }
+ }
+
+
+ ///
+ internal override void PackFromBgr24(Span source, Span destPixels, int count)
+ {
+ GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count);
+
+ ref Bgr24 sourceRef = ref source.DangerousGetPinnableReference();
+ ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference();
+
+ for (int i = 0; i < count; i++)
+ {
+ ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i);
+ ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
+ dp.Bgr = sp; dp.A = 255;
+ }
+ }
+
+
+ ///
+ internal override void ToBgr24(Span sourcePixels, Span dest, int count)
+ {
+ GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count);
+
+ ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference();
+ ref Bgr24 destRef = ref dest.DangerousGetPinnableReference();
+
+ for (int i = 0; i < count; i++)
+ {
+ ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i);
+ ref Bgr24 dp = ref Unsafe.Add(ref destRef, i);
+ dp = sp.Bgr;
+ }
+ }
+
+
+ ///
+ internal override void PackFromBgra32(Span source, Span destPixels, int count)
+ {
+ GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count);
+
+ ref Bgra32 sourceRef = ref source.DangerousGetPinnableReference();
+ ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference();
+
+ for (int i = 0; i < count; i++)
+ {
+ ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i);
+ ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
+ dp = sp.ToRgba32();
+ }
+ }
+
+
+ ///
+ internal override void ToBgra32(Span sourcePixels, Span dest, int count)
+ {
+ GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count);
+
+ ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference();
+ ref Bgra32 destRef = ref dest.DangerousGetPinnableReference();
+
+ for (int i = 0; i < count; i++)
+ {
+ ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i);
+ ref Bgra32 dp = ref Unsafe.Add(ref destRef, i);
+ dp = sp.ToBgra32();
+ }
+ }
+
+
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.tt b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.tt
new file mode 100644
index 0000000000..9c01fa9157
--- /dev/null
+++ b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.tt
@@ -0,0 +1,99 @@
+<#
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+#>
+<#@ template debug="false" hostspecific="false" language="C#" #>
+<#@ assembly name="System.Core" #>
+<#@ import namespace="System.Linq" #>
+<#@ import namespace="System.Text" #>
+<#@ import namespace="System.Collections.Generic" #>
+<#@ output extension=".cs" #>
+<#
+
+ void GeneratePackFromMethod(string pixelType, string converterCode)
+ {
+ #>
+
+ ///
+ internal override void PackFrom<#=pixelType#>(Span<<#=pixelType#>> source, Span destPixels, int count)
+ {
+ GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count);
+
+ ref <#=pixelType#> sourceRef = ref source.DangerousGetPinnableReference();
+ ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference();
+
+ for (int i = 0; i < count; i++)
+ {
+ ref <#=pixelType#> sp = ref Unsafe.Add(ref sourceRef, i);
+ ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
+ <#=converterCode#>
+ }
+ }
+
+ <#
+ }
+
+ void GenerateConvertToMethod(string pixelType, string converterCode)
+ {
+ #>
+
+ ///
+ internal override void To<#=pixelType#>(Span sourcePixels, Span<<#=pixelType#>> dest, int count)
+ {
+ GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count);
+
+ ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference();
+ ref <#=pixelType#> destRef = ref dest.DangerousGetPinnableReference();
+
+ for (int i = 0; i < count; i++)
+ {
+ ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i);
+ ref <#=pixelType#> dp = ref Unsafe.Add(ref destRef, i);
+ <#=converterCode#>
+ }
+ }
+
+ <#
+ }
+
+#>
+//
+
+//
+// Copyright (c) James Jackson-South and contributors.
+// Licensed under the Apache License, Version 2.0.
+//
+
+namespace ImageSharp
+{
+ using System;
+ using System.Numerics;
+ using System.Runtime.CompilerServices;
+ using System.Runtime.InteropServices;
+
+ using ImageSharp.Memory;
+ using ImageSharp.PixelFormats;
+
+ ///
+ /// Provides optimized overrides for bulk operations.
+ ///
+ public partial struct Rgba32
+ {
+ internal partial class PixelOperations : PixelOperations
+ {
+ <#
+ GeneratePackFromMethod("Rgb24", "Unsafe.As(ref dp) = sp; dp.A = 255;");
+ GenerateConvertToMethod("Rgb24", "dp = Unsafe.As(ref sp);");
+
+ GeneratePackFromMethod("Bgr24", "dp.Bgr = sp; dp.A = 255;");
+ GenerateConvertToMethod("Bgr24", "dp = sp.Bgr;");
+
+ GeneratePackFromMethod("Bgra32", "dp = sp.ToRgba32();");
+ GenerateConvertToMethod("Bgra32", "dp = sp.ToBgra32();");
+ #>
+
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs
index f10f0ba279..63e40e9cf7 100644
--- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs
+++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs
@@ -21,7 +21,7 @@ namespace ImageSharp
///
/// implementation optimized for .
///
- internal class PixelOperations : PixelOperations
+ internal partial class PixelOperations : PixelOperations
{
///
/// SIMD optimized bulk implementation of
@@ -120,43 +120,6 @@ namespace ImageSharp
}
}
- ///
- internal override void PackFromRgb24(Span source, Span destPixels, int count)
- {
- Guard.MustBeSizedAtLeast(source, count, nameof(source));
- Guard.MustBeSizedAtLeast(destPixels, count, nameof(destPixels));
-
- ref Rgb24 sourceRef = ref source.DangerousGetPinnableReference();
- ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference();
-
- for (int i = 0; i < count; i++)
- {
- ref Rgb24 sp = ref Unsafe.Add(ref sourceRef, i);
- ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
-
- Unsafe.As(ref dp) = sp;
- dp.A = 255;
- }
- }
-
- ///
- internal override void ToRgb24(Span sourcePixels, Span dest, int count)
- {
- Guard.MustBeSizedAtLeast(sourcePixels, count, nameof(sourcePixels));
- Guard.MustBeSizedAtLeast(dest, count, nameof(dest));
-
- ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference();
- ref Rgb24 destRef = ref dest.DangerousGetPinnableReference();
-
- for (int i = 0; i < count; i++)
- {
- ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i);
- ref Rgb24 dp = ref Unsafe.Add(ref destRef, i);
-
- dp = Unsafe.As(ref sp);
- }
- }
-
///
internal override void PackFromRgba32(Span source, Span destPixels, int count)
{
@@ -173,71 +136,6 @@ namespace ImageSharp
SpanHelper.Copy(sourcePixels, dest, count);
}
- ///
- internal override void PackFromBgr24(Span source, Span destPixels, int count)
- {
- GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count);
-
- ref Bgr24 sourceRef = ref source.DangerousGetPinnableReference();
- ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference();
-
- for (int i = 0; i < count; i++)
- {
- ref Bgr24 sp = ref Unsafe.Add(ref sourceRef, i);
- ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
- dp.Bgr = sp;
- dp.A = 255;
- }
- }
-
- ///
- internal override void ToBgr24(Span sourcePixels, Span dest, int count)
- {
- GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count);
-
- ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference();
- ref Bgr24 destRef = ref dest.DangerousGetPinnableReference();
-
- for (int i = 0; i < count; i++)
- {
- ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i);
- ref Bgr24 dp = ref Unsafe.Add(ref destRef, i);
- dp = sp.Bgr;
- }
- }
-
- ///
- internal override void PackFromBgra32(Span source, Span destPixels, int count)
- {
- GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count);
-
- ref Bgra32 sourceRef = ref source.DangerousGetPinnableReference();
- ref Rgba32 destRef = ref destPixels.DangerousGetPinnableReference();
-
- for (int i = 0; i < count; i++)
- {
- ref Bgra32 sp = ref Unsafe.Add(ref sourceRef, i);
- ref Rgba32 dp = ref Unsafe.Add(ref destRef, i);
- dp = sp.ToRgba32();
- }
- }
-
- ///
- internal override void ToBgra32(Span sourcePixels, Span dest, int count)
- {
- GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count);
-
- ref Rgba32 sourceRef = ref sourcePixels.DangerousGetPinnableReference();
- ref Bgra32 destRef = ref dest.DangerousGetPinnableReference();
-
- for (int i = 0; i < count; i++)
- {
- ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i);
- ref Bgra32 dp = ref Unsafe.Add(ref destRef, i);
- dp = sp.ToBgra32();
- }
- }
-
///
/// Value type to store -s unpacked into multiple -s.
///