Browse Source

Packed vectors now convert correctly.

Former-commit-id: e5599a44bb59ca107bc45f9e27c15d3488825a41
Former-commit-id: 77e6dea53e44f60b93a2067ef9fbf0de0ca63751
Former-commit-id: 89f8d00fcd9c1a2f56d9b892cb7f3406085ae727
pull/1/head
James Jackson-South 10 years ago
parent
commit
d54170028b
  1. 9
      GenericImage/PackedVectors/IPackedVector.cs
  2. 46
      GenericImage/PackedVectors/Rgba32.cs
  3. 51
      GenericImage/PackedVectors/Rgba64.cs

9
GenericImage/PackedVectors/IPackedVector.cs

@ -29,6 +29,15 @@
/// <param name="vector">The vector to pack.</param> /// <param name="vector">The vector to pack.</param>
void PackVector(Vector4 vector); void PackVector(Vector4 vector);
/// <summary>
/// Sets the packed representation from a <see cref="Vector4"/>.
/// </summary>
/// <param name="x">The x-component.</param>
/// <param name="y">The y-component.</param>
/// <param name="z">The z-component.</param>
/// <param name="w">The w-component.</param>
void PackBytes(byte x, byte y, byte z, byte w);
/// <summary> /// <summary>
/// Expands the packed representation into a <see cref="Vector4"/>. /// Expands the packed representation into a <see cref="Vector4"/>.
/// </summary> /// </summary>

46
GenericImage/PackedVectors/Rgba32.cs

@ -4,7 +4,7 @@
using System.Numerics; using System.Numerics;
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
public struct Rgba32 : IPackedVector<uint>, IEquatable<Rgba32> public struct Rgba32 : IPackedVector<uint>, IEquatable<Rgba32>
{ {
@ -17,18 +17,20 @@
/// <param name="a">The alpha component.</param> /// <param name="a">The alpha component.</param>
public Rgba32(float r, float g, float b, float a) 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);
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Rgba32"/> struct. /// Initializes a new instance of the <see cref="Rgba32"/> struct.
/// </summary> /// </summary>
/// <param name="vector"> /// <param name="vector">
/// Vector containing the components for the packed vector. /// The vector containing the components for the packed vector.
/// </param> /// </param>
public Rgba32(Vector4 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);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -67,18 +69,25 @@
/// <inheritdoc/> /// <inheritdoc/>
public void PackVector(Vector4 vector) public void PackVector(Vector4 vector)
{ {
Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One); Vector4 clamped = Vector4.Clamp(vector, Vector4.Zero, Vector4.One) * 255f;
this.PackedValue = Pack(clamped.X, clamped.Y, clamped.Z, clamped.W); this.PackedValue = Pack(ref clamped);
}
/// <inheritdoc/>
public void PackBytes(byte x, byte y, byte z, byte w)
{
Vector4 vector = new Vector4(x, y, z, w);
this.PackedValue = Pack(ref vector);
} }
/// <inheritdoc/> /// <inheritdoc/>
public Vector4 ToVector4() public Vector4 ToVector4()
{ {
return new Vector4( return new Vector4(
((this.PackedValue >> 16) & 0xFF) / 255f, (this.PackedValue >> 16) & 0xFF,
((this.PackedValue >> 8) & 0xFF) / 255f, (this.PackedValue >> 8) & 0xFF,
(this.PackedValue & 0xFF) / 255f, this.PackedValue & 0xFF,
((this.PackedValue >> 24) & 0xFF) / 255f); (this.PackedValue >> 24) & 0xFF) / 255f;
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -123,19 +132,18 @@
/// <summary> /// <summary>
/// Sets the packed representation from the given component values. /// Sets the packed representation from the given component values.
/// </summary> /// </summary>
/// <param name="x">The x component.</param> /// <param name="vector">
/// <param name="y">The y component.</param> /// The vector containing the components for the packed vector.
/// <param name="z">The z component.</param> /// </param>
/// <param name="w">The w component.</param>
/// <returns> /// <returns>
/// The <see cref="uint"/>. /// The <see cref="uint"/>.
/// </returns> /// </returns>
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) | return ((uint)Math.Round(vector.X) << 16) |
((uint)Math.Round(y * 255f) << 8) | ((uint)Math.Round(vector.Y) << 8) |
(uint)Math.Round(z * 255f) | (uint)Math.Round(vector.Z) |
((uint)Math.Round(w * 255f) << 24); ((uint)Math.Round(vector.W) << 24);
} }
/// <summary> /// <summary>

51
GenericImage/PackedVectors/Rgba64.cs

@ -17,18 +17,20 @@
/// <param name="a">The alpha component.</param> /// <param name="a">The alpha component.</param>
public Rgba64(float r, float g, float b, float a) 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);
} }
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="Rgba64"/> struct. /// Initializes a new instance of the <see cref="Rgba64"/> struct.
/// </summary> /// </summary>
/// <param name="vector"> /// <param name="vector">
/// Vector containing the components for the packed vector. /// The vector containing the components for the packed vector.
/// </param> /// </param>
public Rgba64(Vector4 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);
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -67,23 +69,37 @@
/// <inheritdoc/> /// <inheritdoc/>
public void PackVector(Vector4 vector) 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);
}
/// <inheritdoc/>
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);
} }
/// <inheritdoc/> /// <inheritdoc/>
public Vector4 ToVector4() public Vector4 ToVector4()
{ {
return new Vector4( return new Vector4(
(this.PackedValue & 0xFFFF) / 65535f, this.PackedValue & 0xFFFF,
((this.PackedValue >> 16) & 0xFFFF) / 65535f, (this.PackedValue >> 16) & 0xFFFF,
((this.PackedValue >> 32) & 0xFFFF) / 65535f, (this.PackedValue >> 32) & 0xFFFF,
((this.PackedValue >> 48) & 0xFFFF) / 65535f); (this.PackedValue >> 48) & 0xFFFF) / 65535f;
} }
/// <inheritdoc/> /// <inheritdoc/>
public byte[] ToBytes() 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)
};
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -116,19 +132,18 @@
/// <summary> /// <summary>
/// Sets the packed representation from the given component values. /// Sets the packed representation from the given component values.
/// </summary> /// </summary>
/// <param name="x">The x component.</param> /// <param name="vector">
/// <param name="y">The y component.</param> /// The vector containing the components for the packed vector.
/// <param name="z">The z component.</param> /// </param>
/// <param name="w">The w component.</param>
/// <returns> /// <returns>
/// The <see cref="ulong"/>. /// The <see cref="ulong"/>.
/// </returns> /// </returns>
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) | return (ulong)Math.Round(vector.X) |
((ulong)Math.Round(ImageMaths.Clamp(y, 0, 1) * 65535f) << 16) | ((ulong)Math.Round(vector.Y) << 16) |
((ulong)Math.Round(ImageMaths.Clamp(z, 0, 1) * 65535f) << 32) | ((ulong)Math.Round(vector.Z) << 32) |
((ulong)Math.Round(ImageMaths.Clamp(w, 0, 1) * 65535f) << 48); ((ulong)Math.Round(vector.W) << 48);
} }
/// <summary> /// <summary>

Loading…
Cancel
Save