From 7278f5b2239289bfbfca886d69af9873c355be76 Mon Sep 17 00:00:00 2001 From: woutware <35376607+woutware@users.noreply.github.com> Date: Fri, 27 Apr 2018 20:20:20 +0200 Subject: [PATCH] Removed some overhead in the Bgra32.ToVector4 and PackFromVector4 methods. It's now also more inline with the Rgba32 implementation. --- src/ImageSharp/PixelFormats/Bgra32.cs | 32 +++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/PixelFormats/Bgra32.cs b/src/ImageSharp/PixelFormats/Bgra32.cs index 20dfda504..de660c05b 100644 --- a/src/ImageSharp/PixelFormats/Bgra32.cs +++ b/src/ImageSharp/PixelFormats/Bgra32.cs @@ -38,6 +38,16 @@ namespace SixLabors.ImageSharp.PixelFormats /// public byte A; + /// + /// The maximum byte value. + /// + private static readonly Vector4 MaxBytes = new Vector4(255); + + /// + /// The half vector value. + /// + private static readonly Vector4 Half = new Vector4(0.5F); + /// /// Initializes a new instance of the struct. /// @@ -141,16 +151,14 @@ namespace SixLabors.ImageSharp.PixelFormats [MethodImpl(MethodImplOptions.AggressiveInlining)] public void PackFromVector4(Vector4 vector) { - var rgba = default(Rgba32); - rgba.PackFromVector4(vector); - this.PackFromRgba32(rgba); + this.Pack(ref vector); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public Vector4 ToVector4() { - return this.ToRgba32().ToVector4(); + return new Vector4(this.R, this.G, this.B, this.A) / MaxBytes; } /// @@ -243,5 +251,21 @@ namespace SixLabors.ImageSharp.PixelFormats /// The RGBA value [MethodImpl(MethodImplOptions.AggressiveInlining)] public Bgra32 ToBgra32() => this; + + /// + /// Packs a into a color. + /// + /// The vector containing the values to pack. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private void Pack(ref Vector4 vector) { + vector *= MaxBytes; + vector += Half; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + + this.R = (byte)vector.X; + this.G = (byte)vector.Y; + this.B = (byte)vector.Z; + this.A = (byte)vector.W; + } } } \ No newline at end of file