diff --git a/src/ImageSharp/Colors/PackedPixel/Short2.cs b/src/ImageSharp/Colors/PackedPixel/Short2.cs new file mode 100644 index 0000000000..e25ef9eb0d --- /dev/null +++ b/src/ImageSharp/Colors/PackedPixel/Short2.cs @@ -0,0 +1,199 @@ +// +// 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 two 16-bit signed integer values. + /// + public struct Short2 : IPackedPixel, IEquatable + { + /// + /// The maximum byte value. + /// + private static readonly Vector4 MaxBytes = new Vector4(255); + + /// + /// The half the maximum byte value. + /// + private static readonly Vector2 Half = new Vector2(127); + + /// + /// The vector value used for rounding. + /// + private static readonly Vector2 Round = new Vector2(.5F); + + /// + /// Initializes a new instance of the struct. + /// + /// The vector containing the component values. + public Short2(Vector2 vector) + { + this.PackedValue = Pack(vector.X, vector.Y); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The x-component. + /// The y-component. + public Short2(float x, float y) + { + this.PackedValue = Pack(x, y); + } + + /// + public uint 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 ==(Short2 left, Short2 right) + { + return left.PackedValue == right.PackedValue; + } + + /// + /// 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 !=(Short2 left, Short2 right) + { + return left.PackedValue != right.PackedValue; + } + + /// + public void PackFromVector4(Vector4 vector) + { + this.PackedValue = Pack(vector.X, vector.Y); + } + + /// + public Vector4 ToVector4() + { + return new Vector4((short)(this.PackedValue & 0xFFFF), (short)(this.PackedValue >> 0x10), 0, 1); + } + + /// + public void PackFromBytes(byte x, byte y, byte z, byte w) + { + Vector2 vector = new Vector2(x, y) / 255; + vector *= 65534; + vector -= new Vector2(32767); + this.PackedValue = Pack(vector.X, vector.Y); + } + + /// + public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder) + { + Vector2 vector = this.ToVector2(); + vector /= 65534; + vector *= 255; + vector += Half; + vector += Round; + + switch (componentOrder) + { + case ComponentOrder.ZYX: + bytes[startIndex] = 0; + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.X); + break; + case ComponentOrder.ZYXW: + bytes[startIndex] = 0; + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 3] = 255; + break; + case ComponentOrder.XYZ: + bytes[startIndex] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = 0; + break; + case ComponentOrder.XYZW: + bytes[startIndex] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = 0; + bytes[startIndex + 3] = 255; + break; + default: + throw new NotSupportedException(); + } + } + + /// + /// Expands the packed representation into a . + /// The vector components are typically expanded in least to greatest significance order. + /// + /// The . + public Vector2 ToVector2() + { + return new Vector2((short)(this.PackedValue & 0xFFFF), (short)(this.PackedValue >> 0x10)); + } + + /// + public override bool Equals(object obj) + { + return (obj is Short2) && this.Equals((Short2)obj); + } + + /// + public bool Equals(Short2 other) + { + return this == other; + } + + /// + public override int GetHashCode() + { + return this.PackedValue.GetHashCode(); + } + + /// + public override string ToString() + { + return this.PackedValue.ToString("x8"); + } + + /// + /// Packs the components into a . + /// + /// The x-component + /// The y-component + /// The containing the packed values. + private static uint Pack(float x, float y) + { + // Largest two byte positive number 0xFFFF >> 1; + const float MaxPos = 0x7FFF; + const float MinNeg = ~(int)MaxPos; + + // Clamp the value between min and max values + uint word2 = (uint)Math.Round(x.Clamp(MinNeg, MaxPos)) & 0xFFFF; + uint word1 = ((uint)Math.Round(y.Clamp(MinNeg, MaxPos)) & 0xFFFF) << 0x10; + + return word2 | word1; + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs index 58d9d56d72..c7d0e1c733 100644 --- a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs +++ b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs @@ -705,6 +705,55 @@ namespace ImageSharp.Tests.Colors Assert.Equal(rgba, new byte[] { 20, 38, 76, 115 }); } + [Fact] + public void Short2() + { + // Test the limits. + Assert.Equal((uint)0x0, new Short2(Vector2.Zero).PackedValue); + Assert.Equal((uint)0x7FFF7FFF, new Short2(Vector2.One * 0x7FFF).PackedValue); + Assert.Equal(0x80008000, new Short2(Vector2.One * -0x8000).PackedValue); + + // Test ToVector2. + Assert.True(Equal(Vector2.One * 0x7FFF, new Short2(Vector2.One * 0x7FFF).ToVector2())); + Assert.True(Equal(Vector2.Zero, new Short2(Vector2.Zero).ToVector2())); + Assert.True(Equal(Vector2.One * -0x8000, new Short2(Vector2.One * -0x8000).ToVector2())); + Assert.True(Equal(Vector2.UnitX * 0x7FFF, new Short2(Vector2.UnitX * 0x7FFF).ToVector2())); + Assert.True(Equal(Vector2.UnitY * 0x7FFF, new Short2(Vector2.UnitY * 0x7FFF).ToVector2())); + + // Test clamping. + Assert.True(Equal(Vector2.One * 0x7FFF, new Short2(Vector2.One * 1234567.0f).ToVector2())); + Assert.True(Equal(Vector2.One * -0x8000, new Short2(Vector2.One * -1234567.0f).ToVector2())); + + // Test ToVector4. + Assert.True(Equal(new Vector4(0x7FFF, 0x7FFF, 0, 1), (new Short2(Vector2.One * 0x7FFF)).ToVector4())); + Assert.True(Equal(new Vector4(0, 0, 0, 1), (new Short2(Vector2.Zero)).ToVector4())); + Assert.True(Equal(new Vector4(-0x8000, -0x8000, 0, 1), (new Short2(Vector2.One * -0x8000)).ToVector4())); + + // Test ordering + float x = 0x2db1; + float y = 0x361d; + Assert.Equal((uint)0x361d2db1, new Short2(x, y).PackedValue); + x = 127.5f; + y = -5.3f; + Assert.Equal(4294639744, new Short2(x, y).PackedValue); + + byte[] rgb = new byte[3]; + byte[] rgba = new byte[4]; + byte[] bgr = new byte[3]; + byte[] bgra = new byte[4]; + + new Short2(x, y).ToBytes(rgb, 0, ComponentOrder.XYZ); + + new Short2(x, y).ToBytes(rgba, 0, ComponentOrder.XYZW); + + new Short2(x, y).ToBytes(bgr, 0, ComponentOrder.ZYX); + + new Short2(x, y).ToBytes(bgra, 0, ComponentOrder.ZYXW); + + Short2 r = new Short2(); + r.ToBytes(rgba, 0, ComponentOrder.XYZW); + } + // 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 16a60d190b..6196eed438 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -70,6 +70,8 @@ namespace ImageSharp.Tests // Image image = file.CreateImage().To(v => (2F * v) - Vector4.One); // Image image = file.CreateImage().To(v => (2F * v) - Vector4.One); // Image image = file.CreateImage().To(v => (2F * v) - Vector4.One); + // Image image = file.CreateImage().To(v => (65534 * v) - new Vector4(32767)); + using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Save(output);