From b7f07ee845f63bbda6cdeedc9efa6abc3fd95472 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 19 Apr 2018 19:42:17 +1000 Subject: [PATCH] Add optimized conversion overloads --- src/ImageSharp/PixelFormats/Argb32.cs | 7 ++++ .../Rgba32.PixelOperations.Generated.cs | 32 +++++++++++++++++++ .../Rgba32.PixelOperations.Generated.tt | 3 ++ src/ImageSharp/PixelFormats/Rgba32.cs | 17 ++++++++-- 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/PixelFormats/Argb32.cs b/src/ImageSharp/PixelFormats/Argb32.cs index 858184289..603821410 100644 --- a/src/ImageSharp/PixelFormats/Argb32.cs +++ b/src/ImageSharp/PixelFormats/Argb32.cs @@ -285,6 +285,13 @@ namespace SixLabors.ImageSharp.PixelFormats dest.A = this.A; } + /// + /// Converts the pixel to format. + /// + /// The RGBA value + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Rgba32 ToRgba32() => new Rgba32(this.R, this.G, this.B, this.A); + /// public override bool Equals(object obj) { diff --git a/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs index a8e68e36d..e68efba25 100644 --- a/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs +++ b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.cs @@ -112,6 +112,38 @@ namespace SixLabors.ImageSharp.PixelFormats } } + /// + internal override void PackFromArgb32(ReadOnlySpan source, Span destPixels, int count) + { + GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); + + ref Argb32 sourceRef = ref MemoryMarshal.GetReference(source); + ref Rgba32 destRef = ref MemoryMarshal.GetReference(destPixels); + + for (int i = 0; i < count; i++) + { + ref Argb32 sp = ref Unsafe.Add(ref sourceRef, i); + ref Rgba32 dp = ref Unsafe.Add(ref destRef, i); + dp = sp.ToRgba32(); + } + } + + /// + internal override void ToArgb32(ReadOnlySpan sourcePixels, Span dest, int count) + { + GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); + + ref Rgba32 sourceRef = ref MemoryMarshal.GetReference(sourcePixels); + ref Argb32 destRef = ref MemoryMarshal.GetReference(dest); + + for (int i = 0; i < count; i++) + { + ref Rgba32 sp = ref Unsafe.Add(ref sourceRef, i); + ref Argb32 dp = ref Unsafe.Add(ref destRef, i); + dp = sp.ToArgb32(); + } + } + } } } \ 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 index 4a88bbad7..a73433339 100644 --- a/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.tt +++ b/src/ImageSharp/PixelFormats/Generated/Rgba32.PixelOperations.Generated.tt @@ -79,6 +79,9 @@ namespace SixLabors.ImageSharp.PixelFormats GeneratePackFromMethod("Bgra32", "dp = sp.ToRgba32();"); GenerateConvertToMethod("Bgra32", "dp = sp.ToBgra32();"); + + GeneratePackFromMethod("Argb32", "dp = sp.ToRgba32();"); + GenerateConvertToMethod("Argb32", "dp = sp.ToArgb32();"); #> } diff --git a/src/ImageSharp/PixelFormats/Rgba32.cs b/src/ImageSharp/PixelFormats/Rgba32.cs index feafde1fa..220f835b9 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.cs @@ -277,7 +277,8 @@ namespace SixLabors.ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void PackFromArgb32(Argb32 source) { + public void PackFromArgb32(Argb32 source) + { Pack(source.R, source.G, source.B, source.A); } @@ -307,7 +308,8 @@ namespace SixLabors.ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToArgb32(ref Argb32 dest) { + public void ToArgb32(ref Argb32 dest) + { dest.R = this.R; dest.G = this.G; dest.B = this.B; @@ -372,6 +374,17 @@ namespace SixLabors.ImageSharp.PixelFormats return new Bgra32(this.R, this.G, this.B, this.A); } + /// + /// Gets the value of this struct as . + /// Useful for changing the component order. + /// + /// A value. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public Argb32 ToArgb32() + { + return new Argb32(this.R, this.G, this.B, this.A); + } + /// public override bool Equals(object obj) {