From 61aef4826b4ed63c12e55e6159e128583f20d713 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 1 Dec 2016 01:47:53 +1100 Subject: [PATCH] NormalizedByte2 Add NormalizedByte4 --- .../Colors/PackedPixel/NormalizedByte2.cs | 36 ++-- .../Colors/PackedPixel/NormalizedByte4.cs | 197 ++++++++++++++++++ .../Colors/PackedPixelTests.cs | 68 +++++- .../Formats/GeneralFormatTests.cs | 1 + 4 files changed, 285 insertions(+), 17 deletions(-) create mode 100644 src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs diff --git a/src/ImageSharp/Colors/PackedPixel/NormalizedByte2.cs b/src/ImageSharp/Colors/PackedPixel/NormalizedByte2.cs index a05f98e459..43378dfc59 100644 --- a/src/ImageSharp/Colors/PackedPixel/NormalizedByte2.cs +++ b/src/ImageSharp/Colors/PackedPixel/NormalizedByte2.cs @@ -19,9 +19,14 @@ namespace ImageSharp private static readonly Vector4 MaxBytes = new Vector4(255); /// - /// The half vector value. + /// The half the maximum byte value. /// - private static readonly Vector4 Half = new Vector4(0.5F); + 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. @@ -106,40 +111,47 @@ namespace ImageSharp /// public void PackFromBytes(byte x, byte y, byte z, byte w) { - this.PackFromVector4(new Vector4(x, y, z, w) / MaxBytes); + Vector4 vector = new Vector4(x, y, z, w); + vector -= Round; + vector -= Half; + vector -= Round; + vector /= Half; + this.PackFromVector4(vector); } /// public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder) { Vector4 vector = this.ToVector4(); - vector *= MaxBytes; + vector *= Half; + vector += Round; vector += Half; + vector += Round; vector = Vector4.Clamp(vector, Vector4.Zero, MaxBytes); switch (componentOrder) { case ComponentOrder.ZYX: - bytes[startIndex] = (byte)vector.Z; + bytes[startIndex] = 0; bytes[startIndex + 1] = (byte)vector.Y; bytes[startIndex + 2] = (byte)vector.X; break; case ComponentOrder.ZYXW: - bytes[startIndex] = (byte)vector.Z; + bytes[startIndex] = 0; bytes[startIndex + 1] = (byte)vector.Y; bytes[startIndex + 2] = (byte)vector.X; - bytes[startIndex + 3] = (byte)vector.W; + bytes[startIndex + 3] = 255; break; case ComponentOrder.XYZ: bytes[startIndex] = (byte)vector.X; bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + bytes[startIndex + 2] = 0; 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; + bytes[startIndex + 2] = 0; + bytes[startIndex + 3] = 255; break; default: throw new NotSupportedException(); @@ -178,8 +190,8 @@ namespace ImageSharp /// 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; + 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); } diff --git a/src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs b/src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs new file mode 100644 index 0000000000..2638ff7f50 --- /dev/null +++ b/src/ImageSharp/Colors/PackedPixel/NormalizedByte4.cs @@ -0,0 +1,197 @@ +// +// 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 8-bit signed normalized values, ranging from −1 to 1. + /// + public struct NormalizedByte4 : 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. + /// + /// The vector containing the component values. + public NormalizedByte4(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 NormalizedByte4(float x, float y, float z, float w) + { + this.PackedValue = Pack(x, y, z, w); + } + + /// + 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 ==(NormalizedByte4 left, NormalizedByte4 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 !=(NormalizedByte4 left, NormalizedByte4 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( + (sbyte)((this.PackedValue >> 0) & 0xFF) / 127F, + (sbyte)((this.PackedValue >> 8) & 0xFF) / 127F, + (sbyte)((this.PackedValue >> 16) & 0xFF) / 127F, + (sbyte)((this.PackedValue >> 24) & 0xFF) / 127F); + } + + /// + public void PackFromBytes(byte x, byte y, byte z, byte w) + { + Vector4 vector = new Vector4(x, y, z, w); + vector -= Round; + vector -= Half; + vector -= Round; + vector /= Half; + this.PackFromVector4(vector); + } + + /// + public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder) + { + Vector4 vector = this.ToVector4(); + vector *= Half; + vector += Round; + vector += Half; + vector += Round; + 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 NormalizedByte4) && this.Equals((NormalizedByte4)obj); + } + + /// + public bool Equals(NormalizedByte4 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 z-component + /// The w-component + /// The containing the packed values. + private static uint Pack(float x, float y, float z, float w) + { + uint byte4 = ((uint)Math.Round(x.Clamp(-1F, 1F) * 127F) & 0xFF) << 0; + uint byte3 = ((uint)Math.Round(y.Clamp(-1F, 1F) * 127F) & 0xFF) << 8; + uint byte2 = ((uint)Math.Round(z.Clamp(-1F, 1F) * 127F) & 0xFF) << 16; + uint byte1 = ((uint)Math.Round(w.Clamp(-1F, 1F) * 127F) & 0xFF) << 24; + + return byte4 | byte3 | 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 5e9ba9be45..9e391bea71 100644 --- a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs +++ b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs @@ -383,7 +383,7 @@ namespace ImageSharp.Tests.Colors 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 + // Test ToVector4 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())); @@ -391,6 +391,9 @@ namespace ImageSharp.Tests.Colors float x = 0.1f; float y = -0.3f; Assert.Equal(0xda0d, new NormalizedByte2(x, y).PackedValue); + NormalizedByte2 n = new NormalizedByte2(); + n.PackFromBytes(141, 90, 0, 0); + Assert.Equal(0xda0d, n.PackedValue); byte[] rgb = new byte[3]; byte[] rgba = new byte[4]; @@ -398,16 +401,71 @@ namespace ImageSharp.Tests.Colors byte[] bgra = new byte[4]; new NormalizedByte2(x, y).ToBytes(rgb, 0, ComponentOrder.XYZ); - Assert.Equal(rgb, new byte[] { 26, 0, 0 }); + Assert.Equal(rgb, new byte[] { 141, 90, 0 }); new NormalizedByte2(x, y).ToBytes(rgba, 0, ComponentOrder.XYZW); - Assert.Equal(rgba, new byte[] { 26, 0, 0, 255 }); + Assert.Equal(rgba, new byte[] { 141, 90, 0, 255 }); new NormalizedByte2(x, y).ToBytes(bgr, 0, ComponentOrder.ZYX); - Assert.Equal(bgr, new byte[] { 0, 0, 26 }); + Assert.Equal(bgr, new byte[] { 0, 90, 141 }); new NormalizedByte2(x, y).ToBytes(bgra, 0, ComponentOrder.ZYXW); - Assert.Equal(bgra, new byte[] { 0, 0, 26, 255 }); + Assert.Equal(bgra, new byte[] { 0, 90, 141, 255 }); + } + + [Fact] + public void NormalizedByte4() + { + // Test PackedValue + Assert.Equal((uint)0x0, new NormalizedByte4(Vector4.Zero).PackedValue); + Assert.Equal((uint)0x7F7F7F7F, new NormalizedByte4(Vector4.One).PackedValue); + Assert.Equal(0x81818181, new NormalizedByte4(-Vector4.One).PackedValue); + + // Test ToVector4 + Assert.True(Equal(Vector4.One, new NormalizedByte4(Vector4.One).ToVector4())); + Assert.True(Equal(Vector4.Zero, new NormalizedByte4(Vector4.Zero).ToVector4())); + Assert.True(Equal(-Vector4.One, new NormalizedByte4(-Vector4.One).ToVector4())); + Assert.True(Equal(Vector4.One, new NormalizedByte4(Vector4.One * 1234.0f).ToVector4())); + Assert.True(Equal(-Vector4.One, new NormalizedByte4(Vector4.One * -1234.0f).ToVector4())); + + // Test Ordering + float x = 0.1f; + float y = -0.3f; + float z = 0.5f; + float w = -0.7f; + Assert.Equal(0xA740DA0D, new NormalizedByte4(x, y, z, w).PackedValue); + NormalizedByte4 n = new NormalizedByte4(); + n.PackFromBytes(141, 90, 192, 39); + Assert.Equal(0xA740DA0D, n.PackedValue); + + Assert.Equal((uint)958796544, new NormalizedByte4(0.0008f, 0.15f, 0.30f, 0.45f).PackedValue); + + byte[] rgb = new byte[3]; + byte[] rgba = new byte[4]; + byte[] bgr = new byte[3]; + byte[] bgra = new byte[4]; + + new NormalizedByte4(x, y, z, w).ToBytes(rgb, 0, ComponentOrder.XYZ); + Assert.Equal(rgb, new byte[] { 141, 90, 192 }); + + new NormalizedByte4(x, y, z, w).ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 141, 90, 192, 39 }); + + new NormalizedByte4(x, y, z, w).ToBytes(bgr, 0, ComponentOrder.ZYX); + Assert.Equal(bgr, new byte[] { 192, 90, 141 }); + + new NormalizedByte4(x, y, z, w).ToBytes(bgra, 0, ComponentOrder.ZYXW); + Assert.Equal(bgra, new byte[] { 192, 90, 141, 39 }); + + // http://community.monogame.net/t/normalizedbyte4-texture2d-gives-different-results-from-xna/8012/8 + NormalizedByte4 r = new NormalizedByte4(); + r.PackFromBytes(9, 115, 202, 127); + r.ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 9, 115, 202, 127 }); + + r.PackedValue = 0x7FCA7309; + r.ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 9, 115, 202, 127 }); } // Comparison helpers with small tolerance to allow for floating point rounding during computations. diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 8da086e17b..8b621768c8 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -60,6 +60,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}")) {