diff --git a/src/ImageSharp/Colors/PackedPixel/NormalizedByte2.cs b/src/ImageSharp/Colors/PackedPixel/NormalizedByte2.cs new file mode 100644 index 0000000000..a05f98e459 --- /dev/null +++ b/src/ImageSharp/Colors/PackedPixel/NormalizedByte2.cs @@ -0,0 +1,187 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System; + using System.Numerics; + + /// + /// Packed packed pixel type containing two 8-bit signed normalized values, ranging from −1 to 1. + /// + public struct NormalizedByte2 : 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 vector containing the component values. + public NormalizedByte2(Vector2 vector) + { + this.PackedValue = Pack(vector.X, vector.Y); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The x-component. + /// The y-component. + public NormalizedByte2(float x, float y) + { + this.PackedValue = Pack(x, y); + } + + /// + public ushort 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 ==(NormalizedByte2 left, NormalizedByte2 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 !=(NormalizedByte2 left, NormalizedByte2 right) + { + return left.PackedValue != right.PackedValue; + } + + /// + /// 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( + ((sbyte)((this.PackedValue >> 0) & 0xFF)) / 127F, + ((sbyte)((this.PackedValue >> 8) & 0xFF)) / 127F); + } + + /// + public void PackFromVector4(Vector4 vector) + { + this.PackedValue = Pack(vector.X, vector.Y); + } + + /// + public Vector4 ToVector4() + { + return new Vector4(this.ToVector2(), 0F, 1F); + } + + /// + 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 bool Equals(object obj) + { + return (obj is NormalizedByte2) && this.Equals((NormalizedByte2)obj); + } + + /// + public bool Equals(NormalizedByte2 other) + { + return this.PackedValue == other.PackedValue; + } + + /// + public override int GetHashCode() + { + return this.PackedValue.GetHashCode(); + } + + /// + public override string ToString() + { + return this.PackedValue.ToString("X"); + } + + /// + /// Packs the components into a . + /// + /// The x-component + /// The y-component + /// The containing the packed values. + private static ushort Pack(float x, float y) + { + int byte2 = (((ushort)Math.Round(x.Clamp(-1F, 1F) * 127F)) & 0xFF) << 0; + int byte1 = (((ushort)Math.Round(y.Clamp(-1F, 1F) * 127F)) & 0xFF) << 8; + + return (ushort)(byte2 | byte1); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs index cadc8deda7..5e9ba9be45 100644 --- a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs +++ b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs @@ -368,6 +368,48 @@ namespace ImageSharp.Tests.Colors Assert.Equal(bgra, new byte[] { 191, 128, 64, 255 }); } + [Fact] + public void NormalizedByte2() + { + // Test PackedValue + Assert.Equal(0x0, new NormalizedByte2(Vector2.Zero).PackedValue); + Assert.Equal(0x7F7F, new NormalizedByte2(Vector2.One).PackedValue); + Assert.Equal(0x8181, new NormalizedByte2(-Vector2.One).PackedValue); + + // Test ToVector2 + Assert.True(Equal(Vector2.One, new NormalizedByte2(Vector2.One).ToVector2())); + Assert.True(Equal(Vector2.Zero, new NormalizedByte2(Vector2.Zero).ToVector2())); + Assert.True(Equal(-Vector2.One, new NormalizedByte2(-Vector2.One).ToVector2())); + Assert.True(Equal(Vector2.One, new NormalizedByte2(Vector2.One * 1234.0f).ToVector2())); + Assert.True(Equal(-Vector2.One, new NormalizedByte2(Vector2.One * -1234.0f).ToVector2())); + + // Test ToVector2 + Assert.True(Equal(new Vector4(1, 1, 0, 1), new NormalizedByte2(Vector2.One).ToVector4())); + Assert.True(Equal(new Vector4(0, 0, 0, 1), new NormalizedByte2(Vector2.Zero).ToVector4())); + + // Test Ordering + float x = 0.1f; + float y = -0.3f; + Assert.Equal(0xda0d, new NormalizedByte2(x, y).PackedValue); + + byte[] rgb = new byte[3]; + byte[] rgba = new byte[4]; + byte[] bgr = new byte[3]; + byte[] bgra = new byte[4]; + + new NormalizedByte2(x, y).ToBytes(rgb, 0, ComponentOrder.XYZ); + Assert.Equal(rgb, new byte[] { 26, 0, 0 }); + + new NormalizedByte2(x, y).ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 26, 0, 0, 255 }); + + new NormalizedByte2(x, y).ToBytes(bgr, 0, ComponentOrder.ZYX); + Assert.Equal(bgr, new byte[] { 0, 0, 26 }); + + new NormalizedByte2(x, y).ToBytes(bgra, 0, ComponentOrder.ZYXW); + Assert.Equal(bgra, new byte[] { 0, 0, 26, 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 641f30bfe9..8da086e17b 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -59,6 +59,7 @@ namespace ImageSharp.Tests // 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}")) {