From 7c24e7077e44b1c8ecd2b8c734889ce7e92e2660 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 29 Nov 2016 22:24:48 +1100 Subject: [PATCH] Add Byte4 --- src/ImageSharp/Colors/Color.cs | 17 +- src/ImageSharp/Colors/PackedPixel/Byte4.cs | 178 ++++++++++++++++++ .../Colors/PackedVectorTests.cs | 51 +++++ .../Formats/GeneralFormatTests.cs | 1 + 4 files changed, 246 insertions(+), 1 deletion(-) create mode 100644 src/ImageSharp/Colors/PackedPixel/Byte4.cs diff --git a/src/ImageSharp/Colors/Color.cs b/src/ImageSharp/Colors/Color.cs index c6c33edda0..109544ada3 100644 --- a/src/ImageSharp/Colors/Color.cs +++ b/src/ImageSharp/Colors/Color.cs @@ -10,7 +10,7 @@ namespace ImageSharp using System.Numerics; /// - /// Packed vector type containing four 8-bit unsigned normalized values ranging from 0 to 255. + /// Packed pixel type containing four 8-bit unsigned normalized values ranging from 0 to 255. /// The color components are stored in red, green, blue, and alpha order. /// /// @@ -19,9 +19,24 @@ namespace ImageSharp /// public partial struct Color : IPackedPixel, IEquatable { + /// + /// The shift count for the red component + /// private const int RedShift = 0; + + /// + /// The shift count for the green component + /// private const int GreenShift = 8; + + /// + /// The shift count for the blue component + /// private const int BlueShift = 16; + + /// + /// The shift count for the alpha component + /// private const int AlphaShift = 24; /// diff --git a/src/ImageSharp/Colors/PackedPixel/Byte4.cs b/src/ImageSharp/Colors/PackedPixel/Byte4.cs new file mode 100644 index 0000000000..5ba35d037a --- /dev/null +++ b/src/ImageSharp/Colors/PackedPixel/Byte4.cs @@ -0,0 +1,178 @@ +// +// 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 unsigned integer values, ranging from 0 to 255. + /// + public struct Byte4 : IPackedPixel, IEquatable, IPackedVector + { + /// + /// Initializes a new instance of the struct. + /// + /// + /// A vector containing the initial values for the components of the Byte4 structure. + /// + public Byte4(Vector4 vector) + { + this.PackedValue = Pack(ref vector); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The x-component + /// The y-component + /// The z-component + /// The w-component + public Byte4(float x, float y, float z, float w) + { + Vector4 vector = new Vector4(x, y, z, w); + this.PackedValue = Pack(ref vector); + } + + /// + 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 ==(Byte4 left, Byte4 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 !=(Byte4 left, Byte4 right) + { + return left.PackedValue != right.PackedValue; + } + + /// + /// Sets the packed representation from a Vector4. + /// + /// The vector to create the packed representation from. + public void PackFromVector4(Vector4 vector) + { + this.PackedValue = Pack(ref vector); + } + + /// + /// Expands the packed representation into a Vector4. + /// + /// The expanded vector. + public Vector4 ToVector4() + { + return new Vector4( + this.PackedValue & 0xFF, + (this.PackedValue >> 0x8) & 0xFF, + (this.PackedValue >> 0x10) & 0xFF, + (this.PackedValue >> 0x18) & 0xFF); + } + + /// + public void PackFromBytes(byte x, byte y, byte z, byte w) + { + this.PackFromVector4(new Vector4(x, y, z, w) / 255F); + } + + /// + public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder) + { + Vector4 vector = this.ToVector4() * 255F; + + 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 Byte4) && this.Equals((Byte4)obj); + } + + /// + public bool Equals(Byte4 other) + { + return this == other; + } + + /// + 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("x8"); + } + + /// + /// Packs a vector into a uint. + /// + /// The vector containing the values to pack. + /// The containing the packed values. + private static uint Pack(ref Vector4 vector) + { + const float Max = 255F; + const float Min = 0F; + + // Clamp the value between min and max values + uint byte4 = (uint)Math.Round(vector.X.Clamp(Min, Max)) & 0xFF; + uint byte3 = ((uint)Math.Round(vector.Y.Clamp(Min, Max)) & 0xFF) << 0x8; + uint byte2 = ((uint)Math.Round(vector.Z.Clamp(Min, Max)) & 0xFF) << 0x10; + uint byte1 = ((uint)Math.Round(vector.W.Clamp(Min, Max)) & 0xFF) << 0x18; + + return byte4 | byte3 | byte2 | byte1; + } + } +} diff --git a/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs b/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs index b24381ace7..5ba7a3e9a7 100644 --- a/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs +++ b/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs @@ -200,6 +200,57 @@ namespace ImageSharp.Tests.Colors Assert.Equal(bgra, new byte[] { 131, 0, 24, 0 }); } + [Fact] + public void Byte4() + { + // Test the limits. + Assert.Equal((uint)0x0, new Byte4(Vector4.Zero).PackedValue); + Assert.Equal(0xFFFFFFFF, new Byte4(Vector4.One * 255).PackedValue); + + // Test ToVector4. + Assert.True(Equal(Vector4.One * 255, new Byte4(Vector4.One * 255).ToVector4())); + Assert.True(Equal(Vector4.Zero, new Byte4(Vector4.Zero).ToVector4())); + Assert.True(Equal(Vector4.UnitX * 255, new Byte4(Vector4.UnitX * 255).ToVector4())); + Assert.True(Equal(Vector4.UnitY * 255, new Byte4(Vector4.UnitY * 255).ToVector4())); + Assert.True(Equal(Vector4.UnitZ * 255, new Byte4(Vector4.UnitZ * 255).ToVector4())); + Assert.True(Equal(Vector4.UnitW * 255, new Byte4(Vector4.UnitW * 255).ToVector4())); + + // Test clamping. + Assert.True(Equal(Vector4.Zero, new Byte4(Vector4.One * -1234.0f).ToVector4())); + Assert.True(Equal(Vector4.One * 255, new Byte4(Vector4.One * 1234.0f).ToVector4())); + + // Test ordering + float x = 0x2d; + float y = 0x36; + float z = 0x7b; + float w = 0x1a; + Assert.Equal((uint)0x1a7b362d, new Byte4(x, y, z, w).PackedValue); + + x = 127.5f; + y = -12.3f; + z = 0.5f; + w = -0.7f; + Assert.Equal((uint)128, new Byte4(x, y, z, w).PackedValue); + + // Test ordering + byte[] rgb = new byte[3]; + byte[] rgba = new byte[4]; + byte[] bgr = new byte[3]; + byte[] bgra = new byte[4]; + + new Byte4(x, y, z, w).ToBytes(rgb, 0, ComponentOrder.XYZ); + Assert.Equal(rgb, new byte[] { 128, 0, 0 }); + + new Byte4(x, y, z, w).ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 128, 0, 0, 0 }); + + new Byte4(x, y, z, w).ToBytes(bgr, 0, ComponentOrder.ZYX); + Assert.Equal(bgr, new byte[] { 0, 0, 128 }); + + new Byte4(x, y, z, w).ToBytes(bgra, 0, ComponentOrder.ZYXW); + Assert.Equal(bgra, new byte[] { 0, 0, 128, 0 }); + } + // 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 db4f3c4663..f9a44f574c 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -55,6 +55,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}")) {