diff --git a/src/ImageSharp/Colors/PackedPixel/Alpha8.cs b/src/ImageSharp/Colors/PackedPixel/Alpha8.cs
index 5f2bf86d45..0ea1202132 100644
--- a/src/ImageSharp/Colors/PackedPixel/Alpha8.cs
+++ b/src/ImageSharp/Colors/PackedPixel/Alpha8.cs
@@ -9,7 +9,7 @@ namespace ImageSharp
using System.Numerics;
///
- /// Packed vector type containing a single 8 bit normalized W values that is ranging from 0 to 1.
+ /// Packed pixel type containing a single 8 bit normalized W values that is ranging from 0 to 1.
///
public struct Alpha8 : IPackedPixel, IEquatable
{
diff --git a/src/ImageSharp/Colors/PackedPixel/Bgr565.cs b/src/ImageSharp/Colors/PackedPixel/Bgr565.cs
index 23558f305c..72a1668b8b 100644
--- a/src/ImageSharp/Colors/PackedPixel/Bgr565.cs
+++ b/src/ImageSharp/Colors/PackedPixel/Bgr565.cs
@@ -9,8 +9,7 @@ namespace ImageSharp
using System.Numerics;
///
- /// Packed vector type containing unsigned normalized values ranging from 0 to 1.
- /// The x and z components use 5 bits, and the y component uses 6 bits.
+ /// Packed pixel type containing unsigned normalized values ranging from 0 to 1. The x and z components use 5 bits, and the y component uses 6 bits.
///
public struct Bgr565 : IPackedPixel, IEquatable
{
@@ -155,7 +154,7 @@ namespace ImageSharp
}
///
- /// Packs a components into a .
+ /// Packs the components into a .
///
/// The x-component
/// The y-component
diff --git a/src/ImageSharp/Colors/PackedPixel/Bgra4444.cs b/src/ImageSharp/Colors/PackedPixel/Bgra4444.cs
new file mode 100644
index 0000000000..630415d03d
--- /dev/null
+++ b/src/ImageSharp/Colors/PackedPixel/Bgra4444.cs
@@ -0,0 +1,164 @@
+//
+// 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 unsigned normalized values, ranging from 0 to 1, using 4 bits each for x, y, z, and w.
+ ///
+ public struct Bgra4444 : IPackedPixel, IEquatable
+ {
+ ///
+ /// Initializes a new instance of the struct.
+ ///
+ /// The x-component
+ /// The y-component
+ /// The z-component
+ /// The w-component
+ public Bgra4444(float x, float y, float z, float w)
+ {
+ this.PackedValue = Pack(x, y, z, w);
+ }
+
+ ///
+ /// Initializes a new instance of the struct.
+ ///
+ /// The vector containing the components for the packed vector.
+ public Bgra4444(Vector4 vector)
+ {
+ this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
+ }
+
+ ///
+ 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 ==(Bgra4444 left, Bgra4444 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 !=(Bgra4444 left, Bgra4444 right)
+ {
+ return left.PackedValue != right.PackedValue;
+ }
+
+ ///
+ public Vector4 ToVector4()
+ {
+ const float Max = 1 / 15F;
+
+ return new Vector4(
+ ((this.PackedValue >> 8) & 0x0F) * Max,
+ ((this.PackedValue >> 4) & 0x0F) * Max,
+ (this.PackedValue & 0x0F) * Max,
+ ((this.PackedValue >> 12) & 0x0F) * Max);
+ }
+
+ ///
+ public void PackFromVector4(Vector4 vector)
+ {
+ this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
+ }
+
+ ///
+ 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 Bgra4444) && this.Equals((Bgra4444)obj);
+ }
+
+ ///
+ public bool Equals(Bgra4444 other)
+ {
+ return this.PackedValue == other.PackedValue;
+ }
+
+ ///
+ public override string ToString()
+ {
+ return this.ToVector4().ToString();
+ }
+
+ ///
+ public override int GetHashCode()
+ {
+ return this.PackedValue.GetHashCode();
+ }
+
+ ///
+ /// Packs the components into a .
+ ///
+ /// The x-component
+ /// The y-component
+ /// The z-component
+ /// The w-component
+ /// The containing the packed values.
+ private static ushort Pack(float x, float y, float z, float w)
+ {
+ return (ushort)((((int)Math.Round(w.Clamp(0, 1) * 15F) & 0x0F) << 12) |
+ (((int)Math.Round(x.Clamp(0, 1) * 15F) & 0x0F) << 8) |
+ (((int)Math.Round(y.Clamp(0, 1) * 15F) & 0x0F) << 4) |
+ ((int)Math.Round(z.Clamp(0, 1) * 15F) & 0x0F));
+ }
+ }
+}
diff --git a/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs b/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs
index 22bb179319..0f1b537de0 100644
--- a/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs
+++ b/tests/ImageSharp.Tests/Colors/PackedVectorTests.cs
@@ -84,6 +84,8 @@ namespace ImageSharp.Tests.Colors
float z = 0.5F;
Assert.Equal(6160, new Bgr565(x, y, z).PackedValue);
+
+ // Test ordering
byte[] rgb = new byte[3];
byte[] rgba = new byte[4];
byte[] bgr = new byte[3];
@@ -102,6 +104,56 @@ namespace ImageSharp.Tests.Colors
Assert.Equal(bgra, new byte[] { 131, 0, 24, 255 });
}
+ [Fact]
+ public void Bgra4444()
+ {
+ // Test the limits.
+ Assert.Equal(0x0, new Bgra4444(Vector4.Zero).PackedValue);
+ Assert.Equal(0xFFFF, new Bgra4444(Vector4.One).PackedValue);
+
+ // Test ToVector4.
+ Assert.True(Equal(Vector4.One, new Bgra4444(Vector4.One).ToVector4()));
+ Assert.True(Equal(Vector4.Zero, new Bgra4444(Vector4.Zero).ToVector4()));
+ Assert.True(Equal(Vector4.UnitX, new Bgra4444(Vector4.UnitX).ToVector4()));
+ Assert.True(Equal(Vector4.UnitY, new Bgra4444(Vector4.UnitY).ToVector4()));
+ Assert.True(Equal(Vector4.UnitZ, new Bgra4444(Vector4.UnitZ).ToVector4()));
+ Assert.True(Equal(Vector4.UnitW, new Bgra4444(Vector4.UnitW).ToVector4()));
+
+ // Test clamping.
+ Assert.True(Equal(Vector4.Zero, new Bgra4444(Vector4.One * -1234.0f).ToVector4()));
+ Assert.True(Equal(Vector4.One, new Bgra4444(Vector4.One * 1234.0f).ToVector4()));
+
+ // Make sure the swizzle is correct.
+ Assert.Equal(0x0F00, new Bgra4444(Vector4.UnitX).PackedValue);
+ Assert.Equal(0x00F0, new Bgra4444(Vector4.UnitY).PackedValue);
+ Assert.Equal(0x000F, new Bgra4444(Vector4.UnitZ).PackedValue);
+ Assert.Equal(0xF000, new Bgra4444(Vector4.UnitW).PackedValue);
+
+ float x = 0.1f;
+ float y = -0.3f;
+ float z = 0.5f;
+ float w = -0.7f;
+ Assert.Equal(520, new Bgra4444(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 Bgra4444(x, y, z, w).ToBytes(rgb, 0, ComponentOrder.XYZ);
+ Assert.Equal(rgb, new byte[] { 34, 0, 136 });
+
+ new Bgra4444(x, y, z, w).ToBytes(rgba, 0, ComponentOrder.XYZW);
+ Assert.Equal(rgba, new byte[] { 34, 0, 136, 0 });
+
+ new Bgra4444(x, y, z, w).ToBytes(bgr, 0, ComponentOrder.ZYX);
+ Assert.Equal(bgr, new byte[] { 136, 0, 34 });
+
+ new Bgra4444(x, y, z, w).ToBytes(bgra, 0, ComponentOrder.ZYXW);
+ Assert.Equal(bgra, new byte[] { 136, 0, 34, 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 542a131d8f..77057cd7b0 100644
--- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs
+++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs
@@ -53,6 +53,7 @@ namespace ImageSharp.Tests
{
Image image = file.CreateImage();
// Image image = file.CreateImage().To();
+ // Image image = file.CreateImage().To();
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
{