diff --git a/src/ImageSharp/Colors/PackedPixel/HalfVector4.cs b/src/ImageSharp/Colors/PackedPixel/HalfVector4.cs new file mode 100644 index 0000000000..4c4da339bb --- /dev/null +++ b/src/ImageSharp/Colors/PackedPixel/HalfVector4.cs @@ -0,0 +1,182 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System; + using System.Numerics; + + /// + /// Packed pixel type containing four 16-bit floating-point values. + /// + public struct HalfVector4 : IPackedPixel, IEquatable + { + /// + /// 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. + /// + /// The x-component. + /// The y-component. + /// The z-component. + /// The w-component. + public HalfVector4(float x, float y, float z, float w) + { + var vector = new Vector4(x, y, z, w); + this.PackedValue = PackHelper(ref vector); + } + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the initial values for the components + public HalfVector4(Vector4 vector) + { + this.PackedValue = PackHelper(ref vector); + } + + /// + public ulong PackedValue { get; set; } + + /// + /// Compares two objects for equality. + /// + /// + /// The on the left side of the operand. + /// + /// + /// The on the right side of the operand. + /// + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + public static bool operator ==(HalfVector4 left, HalfVector4 right) + { + return left.Equals(right); + } + + /// + /// Compares two objects for equality. + /// + /// + /// The on the left side of the operand. + /// + /// + /// The on the right side of the operand. + /// + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + public static bool operator !=(HalfVector4 left, HalfVector4 right) + { + return !left.Equals(right); + } + + /// + public void PackFromVector4(Vector4 vector) + { + this.PackedValue = PackHelper(ref vector); + } + + /// + public Vector4 ToVector4() + { + return new Vector4( + HalfTypeHelper.Unpack((ushort)this.PackedValue), + HalfTypeHelper.Unpack((ushort)(this.PackedValue >> 0x10)), + HalfTypeHelper.Unpack((ushort)(this.PackedValue >> 0x20)), + HalfTypeHelper.Unpack((ushort)(this.PackedValue >> 0x30))); + } + + /// + public void PackFromBytes(byte x, byte y, byte z, byte w) + { + this.PackFromVector4(new Vector4(x, y, z, w) / MaxBytes); + } + + /// + public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder) + { + Vector4 vector = this.ToVector4(); + vector *= MaxBytes; + vector += Half; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + + switch (componentOrder) + { + case ComponentOrder.ZYX: + bytes[startIndex] = (byte)vector.Z; + bytes[startIndex + 1] = (byte)vector.Y; + bytes[startIndex + 2] = (byte)vector.X; + break; + case ComponentOrder.ZYXW: + bytes[startIndex] = (byte)vector.Z; + bytes[startIndex + 1] = (byte)vector.Y; + bytes[startIndex + 2] = (byte)vector.X; + bytes[startIndex + 3] = (byte)vector.W; + break; + case ComponentOrder.XYZ: + bytes[startIndex] = (byte)vector.X; + bytes[startIndex + 1] = (byte)vector.Y; + bytes[startIndex + 2] = (byte)vector.Z; + break; + case ComponentOrder.XYZW: + bytes[startIndex] = (byte)vector.X; + bytes[startIndex + 1] = (byte)vector.Y; + bytes[startIndex + 2] = (byte)vector.Z; + bytes[startIndex + 3] = (byte)vector.W; + break; + default: + throw new NotSupportedException(); + } + } + + /// + public override string ToString() + { + return this.ToVector4().ToString(); + } + + /// + public override int GetHashCode() + { + return this.PackedValue.GetHashCode(); + } + + /// + public override bool Equals(object obj) + { + return (obj is HalfVector4) && this.Equals((HalfVector4)obj); + } + + /// + public bool Equals(HalfVector4 other) + { + return this.PackedValue.Equals(other.PackedValue); + } + + /// + /// Packs a into a . + /// + /// The vector containing the values to pack. + /// The containing the packed values. + private static ulong PackHelper(ref Vector4 vector) + { + ulong num4 = HalfTypeHelper.Pack(vector.X); + ulong num3 = (ulong)HalfTypeHelper.Pack(vector.Y) << 0x10; + ulong num2 = (ulong)HalfTypeHelper.Pack(vector.Z) << 0x20; + ulong num1 = (ulong)HalfTypeHelper.Pack(vector.W) << 0x30; + return num4 | num3 | num2 | num1; + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs index dcb4b164d8..cadc8deda7 100644 --- a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs +++ b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs @@ -322,6 +322,52 @@ namespace ImageSharp.Tests.Colors Assert.Equal(bgra, new byte[] { 0, 64, 128, 255 }); } + [Fact] + public void HalfVector4() + { + // Test PackedValue + Assert.Equal(0uL, new HalfVector4(Vector4.Zero).PackedValue); + Assert.Equal(4323521613979991040uL, new HalfVector4(Vector4.One).PackedValue); + Assert.Equal(13547034390470638592uL, new HalfVector4(-Vector4.One).PackedValue); + Assert.Equal(15360uL, new HalfVector4(Vector4.UnitX).PackedValue); + Assert.Equal(1006632960uL, new HalfVector4(Vector4.UnitY).PackedValue); + Assert.Equal(65970697666560uL, new HalfVector4(Vector4.UnitZ).PackedValue); + Assert.Equal(4323455642275676160uL, new HalfVector4(Vector4.UnitW).PackedValue); + Assert.Equal(4035285078724390502uL, new HalfVector4(0.1f, 0.3f, 0.4f, 0.5f).PackedValue); + + // Test ToVector4 + Assert.True(Equal(Vector4.Zero, new HalfVector4(Vector4.Zero).ToVector4())); + Assert.True(Equal(Vector4.One, new HalfVector4(Vector4.One).ToVector4())); + Assert.True(Equal(-Vector4.One, new HalfVector4(-Vector4.One).ToVector4())); + Assert.True(Equal(Vector4.UnitX, new HalfVector4(Vector4.UnitX).ToVector4())); + Assert.True(Equal(Vector4.UnitY, new HalfVector4(Vector4.UnitY).ToVector4())); + Assert.True(Equal(Vector4.UnitZ, new HalfVector4(Vector4.UnitZ).ToVector4())); + Assert.True(Equal(Vector4.UnitW, new HalfVector4(Vector4.UnitW).ToVector4())); + + // Test ordering + float x = .25F; + float y = .5F; + float z = .75F; + float w = 1F; + + byte[] rgb = new byte[3]; + byte[] rgba = new byte[4]; + byte[] bgr = new byte[3]; + byte[] bgra = new byte[4]; + + new HalfVector4(x, y, z, w).ToBytes(rgb, 0, ComponentOrder.XYZ); + Assert.Equal(rgb, new byte[] { 64, 128, 191 }); + + new HalfVector4(x, y, z, w).ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 64, 128, 191, 255 }); + + new HalfVector4(x, y, z, w).ToBytes(bgr, 0, ComponentOrder.ZYX); + Assert.Equal(bgr, new byte[] { 191, 128, 64 }); + + new HalfVector4(x, y, z, w).ToBytes(bgra, 0, ComponentOrder.ZYXW); + Assert.Equal(bgra, new byte[] { 191, 128, 64, 255 }); + } + // Comparison helpers with small tolerance to allow for floating point rounding during computations. public static bool Equal(float a, float b) { diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 24ff333611..641f30bfe9 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -57,6 +57,8 @@ namespace ImageSharp.Tests // Image image = file.CreateImage().To(); // Image image = file.CreateImage().To(); // Image image = file.CreateImage().To(); + // Image image = file.CreateImage().To(); + // Image image = file.CreateImage().To(); using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) {