diff --git a/src/ImageSharp/PixelFormats/Alpha8.cs b/src/ImageSharp/PixelFormats/Alpha8.cs index 582c28c78..6a78f15db 100644 --- a/src/ImageSharp/PixelFormats/Alpha8.cs +++ b/src/ImageSharp/PixelFormats/Alpha8.cs @@ -87,40 +87,36 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - bytes[startIndex] = 0; - bytes[startIndex + 1] = 0; - bytes[startIndex + 2] = 0; + dest = default(Rgb24); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - bytes[startIndex] = 0; - bytes[startIndex + 1] = 0; - bytes[startIndex + 2] = 0; - bytes[startIndex + 3] = this.PackedValue; + dest.R = 0; + dest.G = 0; + dest.B = 0; + dest.A = this.PackedValue; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - bytes[startIndex] = 0; - bytes[startIndex + 1] = 0; - bytes[startIndex + 2] = 0; + dest = default(Bgr24); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - bytes[startIndex] = 0; - bytes[startIndex + 1] = 0; - bytes[startIndex + 2] = 0; - bytes[startIndex + 3] = this.PackedValue; + dest.R = 0; + dest.G = 0; + dest.B = 0; + dest.A = this.PackedValue; } /// diff --git a/src/ImageSharp/PixelFormats/Argb32.cs b/src/ImageSharp/PixelFormats/Argb32.cs index 2ca1183a3..56c33df33 100644 --- a/src/ImageSharp/PixelFormats/Argb32.cs +++ b/src/ImageSharp/PixelFormats/Argb32.cs @@ -255,40 +255,40 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - bytes[startIndex] = this.R; - bytes[startIndex + 1] = this.G; - bytes[startIndex + 2] = this.B; + dest.R = this.R; + dest.G = this.G; + dest.B = this.B; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - bytes[startIndex] = this.R; - bytes[startIndex + 1] = this.G; - bytes[startIndex + 2] = this.B; - bytes[startIndex + 3] = this.A; + dest.R = this.R; + dest.G = this.G; + dest.B = this.B; + dest.A = this.A; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - bytes[startIndex] = this.B; - bytes[startIndex + 1] = this.G; - bytes[startIndex + 2] = this.R; + dest.R = this.R; + dest.G = this.G; + dest.B = this.B; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - bytes[startIndex] = this.B; - bytes[startIndex + 1] = this.G; - bytes[startIndex + 2] = this.R; - bytes[startIndex + 3] = this.A; + dest.R = this.R; + dest.G = this.G; + dest.B = this.B; + dest.A = this.A; } /// diff --git a/src/ImageSharp/PixelFormats/Bgr24.cs b/src/ImageSharp/PixelFormats/Bgr24.cs index 386320495..7430e37ab 100644 --- a/src/ImageSharp/PixelFormats/Bgr24.cs +++ b/src/ImageSharp/PixelFormats/Bgr24.cs @@ -5,24 +5,34 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + /// + /// Pixel type containing three 8-bit unsigned normalized values ranging from 0 to 255. + /// The color components are stored in blue, green, red order. + /// [StructLayout(LayoutKind.Sequential)] public struct Bgr24 : IPixel { /// - /// Gets or sets the blue component. + /// The blue component. /// public byte B; /// - /// Gets or sets the green component. + /// The green component. /// public byte G; /// - /// Gets or sets the red component. + /// The red component. /// public byte R; + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Bgr24(byte r, byte g, byte b) { @@ -31,19 +41,23 @@ this.B = b; } + /// public PixelOperations CreatePixelOperations() => new PixelOperations(); + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Bgr24 other) { return this.R == other.R && this.G == other.G && this.B == other.B; } + /// public override bool Equals(object obj) { return obj?.GetType() == typeof(Bgr24) && this.Equals((Bgr24)obj); } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() { @@ -56,37 +70,44 @@ } } + /// public void PackFromBytes(byte x, byte y, byte z, byte w) { throw new NotImplementedException(); } + /// public void PackFromVector4(Vector4 vector) { throw new NotImplementedException(); } + /// public Vector4 ToVector4() { throw new NotImplementedException(); } - public void ToXyzBytes(Span bytes, int startIndex) + /// + public void ToRgb24(ref Rgb24 dest) { throw new NotImplementedException(); } - public void ToXyzwBytes(Span bytes, int startIndex) + /// + public void ToRgba32(ref Rgba32 dest) { throw new NotImplementedException(); } - public void ToZyxBytes(Span bytes, int startIndex) + /// + public void ToBgr24(ref Bgr24 dest) { throw new NotImplementedException(); } - public void ToZyxwBytes(Span bytes, int startIndex) + /// + public void ToBgra32(ref Bgra32 dest) { throw new NotImplementedException(); } diff --git a/src/ImageSharp/PixelFormats/Bgr565.cs b/src/ImageSharp/PixelFormats/Bgr565.cs index 6a568eeb5..4f6b6aa20 100644 --- a/src/ImageSharp/PixelFormats/Bgr565.cs +++ b/src/ImageSharp/PixelFormats/Bgr565.cs @@ -110,44 +110,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// diff --git a/src/ImageSharp/PixelFormats/Bgra32.cs b/src/ImageSharp/PixelFormats/Bgra32.cs index 582a0cd92..3ad6bd336 100644 --- a/src/ImageSharp/PixelFormats/Bgra32.cs +++ b/src/ImageSharp/PixelFormats/Bgra32.cs @@ -7,6 +7,10 @@ using ImageSharp.PixelFormats; + /// + /// Packed pixel type containing four 8-bit unsigned normalized values ranging from 0 to 255. + /// The color components are stored in blue, green, red, and alpha order. + /// [StructLayout(LayoutKind.Sequential)] public struct Bgra32 : IPixel, IPackedVector { @@ -45,7 +49,6 @@ this.A = 255; } - /// /// Initializes a new instance of the struct. /// @@ -62,15 +65,44 @@ this.A = a; } + /// + /// Gets or sets the packed representation of the Bgra32 struct. + /// + public uint Bgra + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + return Unsafe.As(ref this); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set + { + Unsafe.As(ref this) = value; + } + } + + /// + public uint PackedValue + { + get => this.Bgra; + set => this.Bgra = value; + } + + /// public PixelOperations CreatePixelOperations() => new PixelOperations(); + /// public bool Equals(Bgra32 other) { return this.R == other.R && this.G == other.G && this.B == other.B && this.A == other.A; } + /// public override bool Equals(object obj) => obj?.GetType() == typeof(Bgra32) && this.Equals((Bgra32)obj); + /// public override int GetHashCode() { unchecked @@ -83,64 +115,46 @@ } } + /// public void PackFromVector4(Vector4 vector) { throw new NotImplementedException(); } + /// public Vector4 ToVector4() { throw new NotImplementedException(); } + /// public void PackFromBytes(byte x, byte y, byte z, byte w) { throw new NotImplementedException(); } - public void ToXyzBytes(Span bytes, int startIndex) - { - throw new NotImplementedException(); - } - - public void ToXyzwBytes(Span bytes, int startIndex) + /// + public void ToRgb24(ref Rgb24 dest) { throw new NotImplementedException(); } - public void ToZyxBytes(Span bytes, int startIndex) + /// + public void ToRgba32(ref Rgba32 dest) { throw new NotImplementedException(); } - public void ToZyxwBytes(Span bytes, int startIndex) + /// + public void ToBgr24(ref Bgr24 dest) { throw new NotImplementedException(); } - /// - /// Gets or sets the packed representation of the Bgra32 struct. - /// - public uint Bgra - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - return Unsafe.As(ref this); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - set - { - Unsafe.As(ref this) = value; - } - } - /// - public uint PackedValue + public void ToBgra32(ref Bgra32 dest) { - get => this.Bgra; - set => this.Bgra = value; + throw new NotImplementedException(); } } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/Bgra4444.cs b/src/ImageSharp/PixelFormats/Bgra4444.cs index 2dafd0c98..2c60aa900 100644 --- a/src/ImageSharp/PixelFormats/Bgra4444.cs +++ b/src/ImageSharp/PixelFormats/Bgra4444.cs @@ -101,44 +101,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; - bytes[startIndex + 3] = (byte)vector.W; + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// diff --git a/src/ImageSharp/PixelFormats/Bgra5551.cs b/src/ImageSharp/PixelFormats/Bgra5551.cs index 67fb4e0ec..9812059a2 100644 --- a/src/ImageSharp/PixelFormats/Bgra5551.cs +++ b/src/ImageSharp/PixelFormats/Bgra5551.cs @@ -101,44 +101,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; + dest.A = (byte)vector.W; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4() * 255F; - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; + dest.A = (byte)vector.W; } /// @@ -190,5 +190,8 @@ namespace ImageSharp.PixelFormats (((int)Math.Round(z.Clamp(0, 1) * 31F) & 0x1F) << 0) | (((int)Math.Round(w.Clamp(0, 1)) & 0x1) << 15)); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() => this.ToVector4() * 255f; } } diff --git a/src/ImageSharp/PixelFormats/Byte4.cs b/src/ImageSharp/PixelFormats/Byte4.cs index 2a0e6d75c..a5fcfe8bf 100644 --- a/src/ImageSharp/PixelFormats/Byte4.cs +++ b/src/ImageSharp/PixelFormats/Byte4.cs @@ -102,44 +102,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { Vector4 vector = this.ToVector4(); - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { Vector4 vector = this.ToVector4(); - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; - bytes[startIndex + 3] = (byte)vector.W; + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; + dest.A = (byte)vector.W; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { Vector4 vector = this.ToVector4(); - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { Vector4 vector = this.ToVector4(); - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; + dest.A = (byte)vector.W; } /// diff --git a/src/ImageSharp/PixelFormats/HalfSingle.cs b/src/ImageSharp/PixelFormats/HalfSingle.cs index b6f93d059..441ee5417 100644 --- a/src/ImageSharp/PixelFormats/HalfSingle.cs +++ b/src/ImageSharp/PixelFormats/HalfSingle.cs @@ -111,60 +111,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// @@ -192,5 +176,15 @@ namespace ImageSharp.PixelFormats { return this.PackedValue.GetHashCode(); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() + { + Vector4 vector = this.ToVector4(); + vector *= MaxBytes; + vector += Half; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + return vector; + } } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/HalfVector2.cs b/src/ImageSharp/PixelFormats/HalfVector2.cs index e96c5c076..373b4f970 100644 --- a/src/ImageSharp/PixelFormats/HalfVector2.cs +++ b/src/ImageSharp/PixelFormats/HalfVector2.cs @@ -125,60 +125,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; + dest.A = 255; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; + dest.A = 255; } /// @@ -220,5 +204,15 @@ namespace ImageSharp.PixelFormats uint num = (uint)(HalfTypeHelper.Pack(y) << 0x10); return num2 | num; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() + { + Vector4 vector = this.ToVector4(); + vector *= MaxBytes; + vector += Half; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + return vector; + } } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/HalfVector4.cs b/src/ImageSharp/PixelFormats/HalfVector4.cs index d84a40198..cd079146e 100644 --- a/src/ImageSharp/PixelFormats/HalfVector4.cs +++ b/src/ImageSharp/PixelFormats/HalfVector4.cs @@ -118,60 +118,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; + dest.A = (byte)vector.W; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4(); - vector *= MaxBytes; - vector += Half; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; + dest.A = (byte)vector.W; } /// @@ -214,5 +198,15 @@ namespace ImageSharp.PixelFormats ulong num1 = (ulong)HalfTypeHelper.Pack(vector.W) << 0x30; return num4 | num3 | num2 | num1; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() + { + Vector4 vector = this.ToVector4(); + vector *= MaxBytes; + vector += Half; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + return vector; + } } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/IPixel.cs b/src/ImageSharp/PixelFormats/IPixel.cs index 07b0e03e8..9acaa4eba 100644 --- a/src/ImageSharp/PixelFormats/IPixel.cs +++ b/src/ImageSharp/PixelFormats/IPixel.cs @@ -44,42 +44,37 @@ namespace ImageSharp.PixelFormats /// /// Sets the packed representation from the given byte array. /// + /// + /// TODO: Consider defining multiple PackFrom***() methods instead. (Similar to the opposite direction API.) + /// /// The x-component. /// The y-component. /// The z-component. /// The w-component. void PackFromBytes(byte x, byte y, byte z, byte w); - + /// - /// Expands the packed representation into a given byte array. - /// Output is expanded to X-> Y-> Z order. Equivalent to R-> G-> B in + /// Converts the pixel to format. /// - /// The bytes to set the color in. - /// The starting index of the . - void ToXyzBytes(Span bytes, int startIndex); + /// The destination pixel to write to + void ToRgb24(ref Rgb24 dest); /// - /// Expands the packed representation into a given byte array. - /// Output is expanded to X-> Y-> Z-> W order. Equivalent to R-> G-> B-> A in + /// Converts the pixel to format. /// - /// The bytes to set the color in. - /// The starting index of the . - void ToXyzwBytes(Span bytes, int startIndex); + /// The destination pixel to write to + void ToRgba32(ref Rgba32 dest); /// - /// Expands the packed representation into a given byte array. - /// Output is expanded to Z-> Y-> X order. Equivalent to B-> G-> R in + /// Converts the pixel to format. /// - /// The bytes to set the color in. - /// The starting index of the . - void ToZyxBytes(Span bytes, int startIndex); + /// The destination pixel to write to + void ToBgr24(ref Bgr24 dest); /// - /// Expands the packed representation into a given byte array. - /// Output is expanded to Z-> Y-> X-> W order. Equivalent to B-> G-> R-> A in + /// Converts the pixel to format. /// - /// The bytes to set the color in. - /// The starting index of the . - void ToZyxwBytes(Span bytes, int startIndex); + /// The destination pixel to write to + void ToBgra32(ref Bgra32 dest); } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/NormalizedByte2.cs b/src/ImageSharp/PixelFormats/NormalizedByte2.cs index d1193e9c1..44641710b 100644 --- a/src/ImageSharp/PixelFormats/NormalizedByte2.cs +++ b/src/ImageSharp/PixelFormats/NormalizedByte2.cs @@ -134,68 +134,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = 0; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = 0; - bytes[startIndex + 3] = 255; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; + dest.A = 255; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = 0; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = 0; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = 255; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; + dest.A = 255; } /// @@ -238,5 +214,17 @@ namespace ImageSharp.PixelFormats return (ushort)(byte2 | byte1); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() + { + Vector4 vector = this.ToVector4(); + vector *= Half; + vector += Round; + vector += Half; + vector += Round; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + return vector; + } } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/NormalizedByte4.cs b/src/ImageSharp/PixelFormats/NormalizedByte4.cs index 565f26971..86f00ed84 100644 --- a/src/ImageSharp/PixelFormats/NormalizedByte4.cs +++ b/src/ImageSharp/PixelFormats/NormalizedByte4.cs @@ -127,68 +127,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// @@ -235,5 +211,17 @@ namespace ImageSharp.PixelFormats return byte4 | byte3 | byte2 | byte1; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() + { + Vector4 vector = this.ToVector4(); + vector *= Half; + vector += Round; + vector += Half; + vector += Round; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + return vector; + } } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/NormalizedShort2.cs b/src/ImageSharp/PixelFormats/NormalizedShort2.cs index 5b791eb25..fb11c16af 100644 --- a/src/ImageSharp/PixelFormats/NormalizedShort2.cs +++ b/src/ImageSharp/PixelFormats/NormalizedShort2.cs @@ -121,68 +121,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = 0; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = 0; - bytes[startIndex + 3] = 255; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; + dest.A = 255; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = 0; - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = 0; - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); - bytes[startIndex + 3] = 255; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; + dest.A = 255; } /// @@ -245,5 +221,17 @@ namespace ImageSharp.PixelFormats return word2 | word1; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() + { + Vector4 vector = this.ToVector4(); + vector *= Half; + vector += Round; + vector += Half; + vector += Round; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + return vector; + } } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/NormalizedShort4.cs b/src/ImageSharp/PixelFormats/NormalizedShort4.cs index 047c6a250..0da68b65e 100644 --- a/src/ImageSharp/PixelFormats/NormalizedShort4.cs +++ b/src/ImageSharp/PixelFormats/NormalizedShort4.cs @@ -129,68 +129,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4(); - vector *= Half; - vector += Round; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// @@ -241,5 +217,17 @@ namespace ImageSharp.PixelFormats return word4 | word3 | word2 | word1; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() + { + Vector4 vector = this.ToVector4(); + vector *= Half; + vector += Round; + vector += Half; + vector += Round; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + return vector; + } } } diff --git a/src/ImageSharp/PixelFormats/PixelConversionExtensions.cs b/src/ImageSharp/PixelFormats/PixelConversionExtensions.cs new file mode 100644 index 000000000..c75719dc6 --- /dev/null +++ b/src/ImageSharp/PixelFormats/PixelConversionExtensions.cs @@ -0,0 +1,76 @@ +namespace ImageSharp.PixelFormats +{ + using System; + using System.Runtime.CompilerServices; + + /// + /// Extension methods for copying single pixel data into byte Spans. + /// TODO: This utility class exists for legacy reasons. Need to do a lot of chore work to remove it (mostly in test classes). + /// + internal static class PixelConversionExtensions + { + /// + /// Expands the packed representation into a given byte array. + /// Output is expanded to X-> Y-> Z order. Equivalent to R-> G-> B in + /// + /// The pixel type. + /// The pixel to copy the data from. + /// The bytes to set the color in. + /// The starting index of the . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ToXyzBytes(this TPixel pixel, Span bytes, int startIndex) + where TPixel : struct, IPixel + { + ref Rgb24 dest = ref Unsafe.As(ref bytes[startIndex]); + pixel.ToRgb24(ref dest); + } + + /// + /// Expands the packed representation into a given byte array. + /// Output is expanded to X-> Y-> Z-> W order. Equivalent to R-> G-> B-> A in + /// + /// The pixel type. + /// The pixel to copy the data from. + /// The bytes to set the color in. + /// The starting index of the . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ToXyzwBytes(this TPixel pixel, Span bytes, int startIndex) + where TPixel : struct, IPixel + { + ref Rgba32 dest = ref Unsafe.As(ref bytes[startIndex]); + pixel.ToRgba32(ref dest); + } + + /// + /// Expands the packed representation into a given byte array. + /// Output is expanded to Z-> Y-> X order. Equivalent to B-> G-> R in + /// + /// The pixel type. + /// The pixel to copy the data from. + /// The bytes to set the color in. + /// The starting index of the . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ToZyxBytes(this TPixel pixel, Span bytes, int startIndex) + where TPixel : struct, IPixel + { + ref Bgr24 dest = ref Unsafe.As(ref bytes[startIndex]); + pixel.ToBgr24(ref dest); + } + + /// + /// Expands the packed representation into a given byte array. + /// Output is expanded to Z-> Y-> X-> W order. Equivalent to B-> G-> R-> A in + /// + /// The pixel type. + /// The pixel to copy the data from. + /// The bytes to set the color in. + /// The starting index of the . + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void ToZyxwBytes(this TPixel pixel, Span bytes, int startIndex) + where TPixel : struct, IPixel + { + ref Bgra32 dest = ref Unsafe.As(ref bytes[startIndex]); + pixel.ToBgra32(ref dest); + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs index 5c51b59f8..918662144 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs @@ -93,7 +93,7 @@ namespace ImageSharp.PixelFormats } /// - /// Bulk version of . + /// Bulk version of . /// /// The to the source colors. /// The to the destination bytes. @@ -139,7 +139,7 @@ namespace ImageSharp.PixelFormats } /// - /// Bulk version of + /// Bulk version of /// /// The to the source colors. /// The to the destination bytes. @@ -185,7 +185,7 @@ namespace ImageSharp.PixelFormats } /// - /// Bulk version of . + /// Bulk version of . /// /// The to the source colors. /// The to the destination bytes. @@ -231,7 +231,7 @@ namespace ImageSharp.PixelFormats } /// - /// Bulk version of . + /// Bulk version of . /// /// The to the source colors. /// The to the destination bytes. diff --git a/src/ImageSharp/PixelFormats/Rg32.cs b/src/ImageSharp/PixelFormats/Rg32.cs index 0faf2fe80..86b516ede 100644 --- a/src/ImageSharp/PixelFormats/Rg32.cs +++ b/src/ImageSharp/PixelFormats/Rg32.cs @@ -114,48 +114,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; + dest.A = (byte)vector.W; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)vector.X; + dest.G = (byte)vector.Y; + dest.B = (byte)vector.Z; + dest.A = (byte)vector.W; } /// @@ -197,5 +193,8 @@ namespace ImageSharp.PixelFormats ((int)Math.Round(x.Clamp(0, 1) * 65535F) & 0xFFFF) | (((int)Math.Round(y.Clamp(0, 1) * 65535F) & 0xFFFF) << 16)); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() => this.ToVector4() * 255f; } } diff --git a/src/ImageSharp/PixelFormats/Rgb24.cs b/src/ImageSharp/PixelFormats/Rgb24.cs index d52c86892..196c02d7c 100644 --- a/src/ImageSharp/PixelFormats/Rgb24.cs +++ b/src/ImageSharp/PixelFormats/Rgb24.cs @@ -5,24 +5,34 @@ namespace ImageSharp.PixelFormats using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + /// + /// Pixel type containing three 8-bit unsigned normalized values ranging from 0 to 255. + /// The color components are stored in red, green, blue order. + /// [StructLayout(LayoutKind.Sequential)] public struct Rgb24 : IPixel { /// - /// Gets or sets the red component. + /// The red component. /// public byte R; /// - /// Gets or sets the green component. + /// The green component. /// public byte G; /// - /// Gets or sets the blue component. + /// The blue component. /// public byte B; + /// + /// Initializes a new instance of the struct. + /// + /// The red component. + /// The green component. + /// The blue component. [MethodImpl(MethodImplOptions.AggressiveInlining)] public Rgb24(byte r, byte g, byte b) { @@ -31,19 +41,23 @@ namespace ImageSharp.PixelFormats this.B = b; } + /// public PixelOperations CreatePixelOperations() => new PixelOperations(); + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(Rgb24 other) { return this.R == other.R && this.G == other.G && this.B == other.B; } + /// public override bool Equals(object obj) { return obj?.GetType() == typeof(Rgb24) && this.Equals((Rgb24)obj); } + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public override int GetHashCode() { @@ -56,37 +70,44 @@ namespace ImageSharp.PixelFormats } } + /// public void PackFromBytes(byte x, byte y, byte z, byte w) { throw new NotImplementedException(); } + /// public void PackFromVector4(Vector4 vector) { throw new NotImplementedException(); } + /// public Vector4 ToVector4() { throw new NotImplementedException(); } - public void ToXyzBytes(Span bytes, int startIndex) + /// + public void ToRgb24(ref Rgb24 dest) { throw new NotImplementedException(); } - public void ToXyzwBytes(Span bytes, int startIndex) + /// + public void ToRgba32(ref Rgba32 dest) { throw new NotImplementedException(); } - public void ToZyxBytes(Span bytes, int startIndex) + /// + public void ToBgr24(ref Bgr24 dest) { throw new NotImplementedException(); } - public void ToZyxwBytes(Span bytes, int startIndex) + /// + public void ToBgra32(ref Bgra32 dest) { throw new NotImplementedException(); } diff --git a/src/ImageSharp/PixelFormats/Rgba1010102.cs b/src/ImageSharp/PixelFormats/Rgba1010102.cs index 8eaf5f771..935f69795 100644 --- a/src/ImageSharp/PixelFormats/Rgba1010102.cs +++ b/src/ImageSharp/PixelFormats/Rgba1010102.cs @@ -108,48 +108,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// diff --git a/src/ImageSharp/PixelFormats/Rgba32.cs b/src/ImageSharp/PixelFormats/Rgba32.cs index 32df4de7e..840018792 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.cs @@ -255,42 +255,37 @@ namespace ImageSharp return hexOrder.ToString("X8"); } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - bytes[startIndex] = this.R; - bytes[startIndex + 1] = this.G; - bytes[startIndex + 2] = this.B; + dest = Unsafe.As(ref this); } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - bytes[startIndex] = this.R; - bytes[startIndex + 1] = this.G; - bytes[startIndex + 2] = this.B; - bytes[startIndex + 3] = this.A; + dest = this; } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - bytes[startIndex] = this.B; - bytes[startIndex + 1] = this.G; - bytes[startIndex + 2] = this.R; + dest.R = this.R; + dest.G = this.G; + dest.B = this.B; } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - bytes[startIndex] = this.B; - bytes[startIndex + 1] = this.G; - bytes[startIndex + 2] = this.R; - bytes[startIndex + 3] = this.A; + dest.R = this.R; + dest.G = this.G; + dest.B = this.B; + dest.A = this.A; } /// diff --git a/src/ImageSharp/PixelFormats/Rgba64.cs b/src/ImageSharp/PixelFormats/Rgba64.cs index 2101d4ba0..92a839ddf 100644 --- a/src/ImageSharp/PixelFormats/Rgba64.cs +++ b/src/ImageSharp/PixelFormats/Rgba64.cs @@ -107,48 +107,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { Vector4 vector = this.ToVector4() * 255F; - - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// diff --git a/src/ImageSharp/PixelFormats/RgbaVector.cs b/src/ImageSharp/PixelFormats/RgbaVector.cs index 314ac31e3..f3498705d 100644 --- a/src/ImageSharp/PixelFormats/RgbaVector.cs +++ b/src/ImageSharp/PixelFormats/RgbaVector.cs @@ -233,50 +233,46 @@ namespace ImageSharp.PixelFormats return hexOrder.ToString("X8"); } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; - vector += Half; - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; - vector += Half; - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; - vector += Half; - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } - /// + /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; - vector += Half; - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// @@ -320,5 +316,8 @@ namespace ImageSharp.PixelFormats { return this.backingVector.GetHashCode(); } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() => Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/Short2.cs b/src/ImageSharp/PixelFormats/Short2.cs index a6fe95b49..d369a6aae 100644 --- a/src/ImageSharp/PixelFormats/Short2.cs +++ b/src/ImageSharp/PixelFormats/Short2.cs @@ -119,68 +119,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector2 vector = this.ToVector2(); - vector /= 65534; - vector *= 255; - vector += Half; - vector += Round; - vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = 0; + Vector2 vector = this.ToScaledVector2(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector2 vector = this.ToVector2(); - vector /= 65534; - vector *= 255; - vector += Half; - vector += Round; - vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = 0; - bytes[startIndex + 3] = 255; + Vector2 vector = this.ToScaledVector2(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; + dest.A = 255; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector2 vector = this.ToVector2(); - vector /= 65534; - vector *= 255; - vector += Half; - vector += Round; - vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes); - - bytes[startIndex] = 0; - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); + Vector2 vector = this.ToScaledVector2(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector2 vector = this.ToVector2(); - vector /= 65534; - vector *= 255; - vector += Half; - vector += Round; - vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes); - - bytes[startIndex] = 0; - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); - bytes[startIndex + 3] = 255; + Vector2 vector = this.ToScaledVector2(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = 0; + dest.A = 255; } /// @@ -239,5 +215,17 @@ namespace ImageSharp.PixelFormats return word2 | word1; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector2 ToScaledVector2() + { + Vector2 vector = this.ToVector2(); + vector /= 65534; + vector *= 255; + vector += Half; + vector += Round; + vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes); + return vector; + } } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/Short4.cs b/src/ImageSharp/PixelFormats/Short4.cs index f254c7379..9f7a5d887 100644 --- a/src/ImageSharp/PixelFormats/Short4.cs +++ b/src/ImageSharp/PixelFormats/Short4.cs @@ -125,68 +125,44 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(Span bytes, int startIndex) + public void ToRgb24(ref Rgb24 dest) { - Vector4 vector = this.ToVector4(); - vector /= 65534; - vector *= 255; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(Span bytes, int startIndex) + public void ToRgba32(ref Rgba32 dest) { - Vector4 vector = this.ToVector4(); - vector /= 65534; - vector *= 255; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.X); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(Span bytes, int startIndex) + public void ToBgr24(ref Bgr24 dest) { - Vector4 vector = this.ToVector4(); - vector /= 65534; - vector *= 255; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); } /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(Span bytes, int startIndex) + public void ToBgra32(ref Bgra32 dest) { - Vector4 vector = this.ToVector4(); - vector /= 65534; - vector *= 255; - vector += Half; - vector += Round; - vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); - - bytes[startIndex] = (byte)MathF.Round(vector.Z); - bytes[startIndex + 1] = (byte)MathF.Round(vector.Y); - bytes[startIndex + 2] = (byte)MathF.Round(vector.X); - bytes[startIndex + 3] = (byte)MathF.Round(vector.W); + Vector4 vector = this.ToScaledVector4(); + dest.R = (byte)MathF.Round(vector.X); + dest.G = (byte)MathF.Round(vector.Y); + dest.B = (byte)MathF.Round(vector.Z); + dest.A = (byte)MathF.Round(vector.W); } /// @@ -246,5 +222,17 @@ namespace ImageSharp.PixelFormats return word4 | word3 | word2 | word1; } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private Vector4 ToScaledVector4() + { + Vector4 vector = this.ToVector4(); + vector /= 65534; + vector *= 255; + vector += Half; + vector += Round; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + return vector; + } } } \ No newline at end of file diff --git a/src/Shared/stylecop.json b/src/Shared/stylecop.json index b74a0af91..df8f120a5 100644 --- a/src/Shared/stylecop.json +++ b/src/Shared/stylecop.json @@ -3,11 +3,7 @@ "settings": { "documentationRules": { "companyName": "James Jackson-South", - "copyrightText": "Copyright (c) James Jackson-South and contributors.\nLicensed under the Apache License, Version 2.0.", - - "documentInterfaces": false, - "documentInternalElements": false, - "documentExposedElements": false + "copyrightText": "Copyright (c) James Jackson-South and contributors.\nLicensed under the Apache License, Version 2.0." } } } \ No newline at end of file