From 01a8e9c5c8f4d21ba5733c1834be72c5111c8afc Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 2 Dec 2016 00:43:12 +1100 Subject: [PATCH] Add Short4 --- src/ImageSharp/Colors/PackedPixel/Short2.cs | 3 +- src/ImageSharp/Colors/PackedPixel/Short4.cs | 216 ++++++++++++++++++ .../Colors/PackedPixelTests.cs | 57 +++++ 3 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 src/ImageSharp/Colors/PackedPixel/Short4.cs diff --git a/src/ImageSharp/Colors/PackedPixel/Short2.cs b/src/ImageSharp/Colors/PackedPixel/Short2.cs index e25ef9eb0d..310c44ec8a 100644 --- a/src/ImageSharp/Colors/PackedPixel/Short2.cs +++ b/src/ImageSharp/Colors/PackedPixel/Short2.cs @@ -16,7 +16,7 @@ namespace ImageSharp /// /// The maximum byte value. /// - private static readonly Vector4 MaxBytes = new Vector4(255); + private static readonly Vector2 MaxBytes = new Vector2(255); /// /// The half the maximum byte value. @@ -113,6 +113,7 @@ namespace ImageSharp vector *= 255; vector += Half; vector += Round; + vector = Vector2.Clamp(vector, Vector2.Zero, MaxBytes); switch (componentOrder) { diff --git a/src/ImageSharp/Colors/PackedPixel/Short4.cs b/src/ImageSharp/Colors/PackedPixel/Short4.cs new file mode 100644 index 0000000000..190c579ce4 --- /dev/null +++ b/src/ImageSharp/Colors/PackedPixel/Short4.cs @@ -0,0 +1,216 @@ +// +// 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 signed integer values. + /// + public struct Short4 : IPackedPixel, IEquatable + { + /// + /// The maximum byte value. + /// + private static readonly Vector4 MaxBytes = new Vector4(255); + + /// + /// The half the maximum byte value. + /// + private static readonly Vector4 Half = new Vector4(127); + + /// + /// The vector value used for rounding. + /// + private static readonly Vector4 Round = new Vector4(.5F); + + /// + /// Initializes a new instance of the struct. + /// + /// A vector containing the initial values for the components. + public Short4(Vector4 vector) + { + this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The x-component. + /// The y-component. + /// The z-component. + /// The w-component. + public Short4(float x, float y, float z, float w) + { + this.PackedValue = Pack(x, y, z, w); + } + + /// + 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 not equal to the parameter; otherwise, false. + /// + public static bool operator ==(Short4 left, Short4 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 !=(Short4 left, Short4 right) + { + return left.PackedValue != right.PackedValue; + } + + /// + public void PackFromVector4(Vector4 vector) + { + this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W); + } + + /// + public Vector4 ToVector4() + { + return new Vector4( + (short)(this.PackedValue & 0xFFFF), + (short)((this.PackedValue >> 0x10) & 0xFFFF), + (short)((this.PackedValue >> 0x20) & 0xFFFF), + (short)((this.PackedValue >> 0x30) & 0xFFFF)); + } + + /// + public void PackFromBytes(byte x, byte y, byte z, byte w) + { + Vector4 vector = new Vector4(x, y, z, w) / 255; + vector *= 65534; + vector -= new Vector4(32767); + this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W); + } + + /// + public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder) + { + Vector4 vector = this.ToVector4(); + vector /= 65534; + vector *= 255; + vector += Half; + vector += Round; + vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); + + switch (componentOrder) + { + case ComponentOrder.ZYX: + bytes[startIndex] = (byte)(float)Math.Round(vector.Z); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.X); + break; + case ComponentOrder.ZYXW: + bytes[startIndex] = (byte)(float)Math.Round(vector.Z); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 3] = (byte)(float)Math.Round(vector.W); + break; + case ComponentOrder.XYZ: + bytes[startIndex] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.Z); + break; + case ComponentOrder.XYZW: + bytes[startIndex] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.Z); + bytes[startIndex + 3] = (byte)(float)Math.Round(vector.W); + break; + default: + throw new NotSupportedException(); + } + } + + /// + /// Returns a value that indicates whether the current instance is equal to a specified object. + /// + /// The object with which to make the comparison. + /// true if the current instance is equal to the specified object; false otherwise. + public override bool Equals(object obj) + { + return (obj is Short4) && this == (Short4)obj; + } + + /// + /// Returns a value that indicates whether the current instance is equal to a specified object. + /// + /// The object with which to make the comparison. + /// true if the current instance is equal to the specified object; false otherwise. + public bool Equals(Short4 other) + { + return this == other; + } + + /// + /// Gets the hash code for the current instance. + /// + /// Hash code for the instance. + public override int GetHashCode() + { + return this.PackedValue.GetHashCode(); + } + + /// + /// Returns a string representation of the current instance. + /// + /// String that represents the object. + public override string ToString() + { + return this.PackedValue.ToString("x16"); + } + + /// + /// Packs the components into a . + /// + /// The x-component + /// The y-component + /// The z-component + /// The w-component + /// The containing the packed values. + private static ulong Pack(float x, float y, float z, float w) + { + // Largest two byte positive number 0xFFFF >> 1; + const float MaxPos = 0x7FFF; + + // Two's complement + const float MinNeg = ~(int)MaxPos; + + // Clamp the value between min and max values + ulong word4 = ((ulong)Math.Round(x.Clamp(MinNeg, MaxPos)) & 0xFFFF) << 0x00; + ulong word3 = ((ulong)Math.Round(y.Clamp(MinNeg, MaxPos)) & 0xFFFF) << 0x10; + ulong word2 = ((ulong)Math.Round(z.Clamp(MinNeg, MaxPos)) & 0xFFFF) << 0x20; + ulong word1 = ((ulong)Math.Round(w.Clamp(MinNeg, MaxPos)) & 0xFFFF) << 0x30; + + return word4 | word3 | 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 08786eddd5..785125ca13 100644 --- a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs +++ b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs @@ -760,6 +760,63 @@ namespace ImageSharp.Tests.Colors Assert.Equal(rgba, new byte[] { 20, 38, 0, 255 }); } + [Fact] + public void Short4() + { + // Test the limits. + Assert.Equal((ulong)0x0, new Short4(Vector4.Zero).PackedValue); + Assert.Equal((ulong)0x7FFF7FFF7FFF7FFF, new Short4(Vector4.One * 0x7FFF).PackedValue); + Assert.Equal(0x8000800080008000, new Short4(Vector4.One * -0x8000).PackedValue); + + // Test ToVector4. + Assert.Equal(Vector4.One * 0x7FFF, new Short4(Vector4.One * 0x7FFF).ToVector4()); + Assert.Equal(Vector4.Zero, new Short4(Vector4.Zero).ToVector4()); + Assert.Equal(Vector4.One * -0x8000, new Short4(Vector4.One * -0x8000).ToVector4()); + Assert.Equal(Vector4.UnitX * 0x7FFF, new Short4(Vector4.UnitX * 0x7FFF).ToVector4()); + Assert.Equal(Vector4.UnitY * 0x7FFF, new Short4(Vector4.UnitY * 0x7FFF).ToVector4()); + Assert.Equal(Vector4.UnitZ * 0x7FFF, new Short4(Vector4.UnitZ * 0x7FFF).ToVector4()); + Assert.Equal(Vector4.UnitW * 0x7FFF, new Short4(Vector4.UnitW * 0x7FFF).ToVector4()); + + // Test clamping. + Assert.Equal(Vector4.One * 0x7FFF, new Short4(Vector4.One * 1234567.0f).ToVector4()); + Assert.Equal(Vector4.One * -0x8000, new Short4(Vector4.One * -1234567.0f).ToVector4()); + + // Test Ordering + float x = 0.1f; + float y = -0.3f; + float z = 0.5f; + float w = -0.7f; + Assert.Equal(18446462598732840960, new Short4(x, y, z, w).PackedValue); + + x = 11547; + y = 12653; + z = 29623; + w = 193; + Assert.Equal((ulong)0x00c173b7316d2d1b, new Short4(x, y, z, w).PackedValue); + + byte[] rgb = new byte[3]; + byte[] rgba = new byte[4]; + byte[] bgr = new byte[3]; + byte[] bgra = new byte[4]; + + new Short4(x, y, z, w).ToBytes(rgb, 0, ComponentOrder.XYZ); + Assert.Equal(rgb, new byte[] { 172, 177, 243 }); + + new Short4(x, y, z, w).ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 172, 177, 243, 128 }); + + new Short4(x, y, z, w).ToBytes(bgr, 0, ComponentOrder.ZYX); + Assert.Equal(bgr, new byte[] { 243, 177, 172 }); + + new Short4(x, y, z, w).ToBytes(bgra, 0, ComponentOrder.ZYXW); + Assert.Equal(bgra, new byte[] { 243, 177, 172, 128 }); + + Short4 r = new Short4(); + r.PackFromBytes(20, 38, 0, 255); + r.ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 20, 38, 0, 255 }); + } + // Comparison helpers with small tolerance to allow for floating point rounding during computations. public static bool Equal(float a, float b) {