diff --git a/GenericImage/PackedVectors/IPackedVector.cs b/GenericImage/PackedVectors/IPackedVector.cs
index 7bd809094..860409ba2 100644
--- a/GenericImage/PackedVectors/IPackedVector.cs
+++ b/GenericImage/PackedVectors/IPackedVector.cs
@@ -29,6 +29,15 @@
/// The vector to pack.
void PackVector(Vector4 vector);
+ ///
+ /// Sets the packed representation from a .
+ ///
+ /// The x-component.
+ /// The y-component.
+ /// The z-component.
+ /// The w-component.
+ void PackBytes(byte x, byte y, byte z, byte w);
+
///
/// Expands the packed representation into a .
///
diff --git a/GenericImage/PackedVectors/Rgba32.cs b/GenericImage/PackedVectors/Rgba32.cs
index 40bf1a894..1786b3681 100644
--- a/GenericImage/PackedVectors/Rgba32.cs
+++ b/GenericImage/PackedVectors/Rgba32.cs
@@ -4,7 +4,7 @@
using System.Numerics;
///
- /// Packed vector type containing four 8-bit unsigned normalized values ranging from 0 to 255.
+ /// Packed vector type containing four 8-bit unsigned normalized values ranging from 0 to 1.
///
public struct Rgba32 : IPackedVector, IEquatable
{
@@ -17,18 +17,20 @@
/// The alpha component.
public Rgba32(float r, float g, float b, float a)
{
- this.PackedValue = Pack(r, g, b, a);
+ Vector4 clamped = Vector4.Clamp(new Vector4(r, g, b, a), Vector4.Zero, Vector4.One) * 255f;
+ this.PackedValue = Pack(ref clamped);
}
///
/// Initializes a new instance of the struct.
///
///
- /// Vector containing the components for the packed vector.
+ /// The vector containing the components for the packed vector.
///
public Rgba32(Vector4 vector)
{
- this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
+ Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * 255f;
+ this.PackedValue = Pack(ref clamped);
}
///
@@ -67,18 +69,25 @@
///
public void PackVector(Vector4 vector)
{
- Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One);
- this.PackedValue = Pack(clamped.X, clamped.Y, clamped.Z, clamped.W);
+ Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * 255f;
+ this.PackedValue = Pack(ref clamped);
+ }
+
+ ///
+ public void PackBytes(byte x, byte y, byte z, byte w)
+ {
+ Vector4 vector = new Vector4(x, y, z, w);
+ this.PackedValue = Pack(ref vector);
}
///
public Vector4 ToVector4()
{
return new Vector4(
- ((this.PackedValue >> 16) & 0xFF) / 255f,
- ((this.PackedValue >> 8) & 0xFF) / 255f,
- (this.PackedValue & 0xFF) / 255f,
- ((this.PackedValue >> 24) & 0xFF) / 255f);
+ (this.PackedValue >> 16) & 0xFF,
+ (this.PackedValue >> 8) & 0xFF,
+ this.PackedValue & 0xFF,
+ (this.PackedValue >> 24) & 0xFF) / 255f;
}
///
@@ -123,19 +132,18 @@
///
/// Sets the packed representation from the given component values.
///
- /// The x component.
- /// The y component.
- /// The z component.
- /// The w component.
+ ///
+ /// The vector containing the components for the packed vector.
+ ///
///
/// The .
///
- private static uint Pack(float x, float y, float z, float w)
+ private static uint Pack(ref Vector4 vector)
{
- return ((uint)Math.Round(x * 255f) << 16) |
- ((uint)Math.Round(y * 255f) << 8) |
- (uint)Math.Round(z * 255f) |
- ((uint)Math.Round(w * 255f) << 24);
+ return ((uint)Math.Round(vector.X) << 16) |
+ ((uint)Math.Round(vector.Y) << 8) |
+ (uint)Math.Round(vector.Z) |
+ ((uint)Math.Round(vector.W) << 24);
}
///
diff --git a/GenericImage/PackedVectors/Rgba64.cs b/GenericImage/PackedVectors/Rgba64.cs
index ff529d15e..06821f63e 100644
--- a/GenericImage/PackedVectors/Rgba64.cs
+++ b/GenericImage/PackedVectors/Rgba64.cs
@@ -17,18 +17,20 @@
/// The alpha component.
public Rgba64(float r, float g, float b, float a)
{
- this.PackedValue = Pack(r, g, b, a);
+ Vector4 clamped = Vector4.Clamp(new Vector4(r, g, b, a), Vector4.Zero, Vector4.One) * 65535f;
+ this.PackedValue = Pack(ref clamped);
}
///
/// Initializes a new instance of the struct.
///
///
- /// Vector containing the components for the packed vector.
+ /// The vector containing the components for the packed vector.
///
public Rgba64(Vector4 vector)
{
- this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
+ Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * 65535f;
+ this.PackedValue = Pack(ref clamped);
}
///
@@ -67,23 +69,37 @@
///
public void PackVector(Vector4 vector)
{
- this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W);
+ Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * 65535f;
+ this.PackedValue = Pack(ref clamped);
+ }
+
+ ///
+ public void PackBytes(byte x, byte y, byte z, byte w)
+ {
+ Vector4 vector = (new Vector4(x, y, z, w) / 255f) * 65535f;
+ this.PackedValue = Pack(ref vector);
}
///
public Vector4 ToVector4()
{
return new Vector4(
- (this.PackedValue & 0xFFFF) / 65535f,
- ((this.PackedValue >> 16) & 0xFFFF) / 65535f,
- ((this.PackedValue >> 32) & 0xFFFF) / 65535f,
- ((this.PackedValue >> 48) & 0xFFFF) / 65535f);
+ this.PackedValue & 0xFFFF,
+ (this.PackedValue >> 16) & 0xFFFF,
+ (this.PackedValue >> 32) & 0xFFFF,
+ (this.PackedValue >> 48) & 0xFFFF) / 65535f;
}
///
public byte[] ToBytes()
{
- throw new NotImplementedException();
+ return new[]
+ {
+ (byte)((this.PackedValue >> 40) & 255),
+ (byte)((this.PackedValue >> 24) & 255),
+ (byte)((this.PackedValue >> 8) & 255),
+ (byte)((this.PackedValue >> 56) & 255)
+ };
}
///
@@ -116,19 +132,18 @@
///
/// Sets the packed representation from the given component values.
///
- /// The x component.
- /// The y component.
- /// The z component.
- /// The w component.
+ ///
+ /// The vector containing the components for the packed vector.
+ ///
///
/// The .
///
- private static ulong Pack(float x, float y, float z, float w)
+ private static ulong Pack(ref Vector4 vector)
{
- return (ulong)Math.Round(ImageMaths.Clamp(x, 0, 1) * 65535f) |
- ((ulong)Math.Round(ImageMaths.Clamp(y, 0, 1) * 65535f) << 16) |
- ((ulong)Math.Round(ImageMaths.Clamp(z, 0, 1) * 65535f) << 32) |
- ((ulong)Math.Round(ImageMaths.Clamp(w, 0, 1) * 65535f) << 48);
+ return (ulong)Math.Round(vector.X) |
+ ((ulong)Math.Round(vector.Y) << 16) |
+ ((ulong)Math.Round(vector.Z) << 32) |
+ ((ulong)Math.Round(vector.W) << 48);
}
///