Browse Source

Add HalfVector2

af/merge-core
James Jackson-South 9 years ago
parent
commit
6e23ef4c95
  1. 2
      src/ImageSharp/Colors/PackedPixel/HalfSingle.cs
  2. 187
      src/ImageSharp/Colors/PackedPixel/HalfVector2.cs
  3. 37
      tests/ImageSharp.Tests/Colors/PackedPixelTests.cs

2
src/ImageSharp/Colors/PackedPixel/HalfSingle.cs

@ -93,7 +93,7 @@ namespace ImageSharp
/// <inheritdoc />
public void PackFromBytes(byte x, byte y, byte z, byte w)
{
this.PackFromVector4(new Vector4(x, y, z, w) / 255F);
this.PackFromVector4(new Vector4(x, y, z, w) / MaxBytes);
}
/// <inheritdoc />

187
src/ImageSharp/Colors/PackedPixel/HalfVector2.cs

@ -0,0 +1,187 @@
// <copyright file="HalfVector2.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
{
using System;
using System.Numerics;
/// <summary>
/// Packed pixel type containing two 16-bit floating-point values.
/// </summary>
public struct HalfVector2 : IPackedPixel<uint>, IEquatable<HalfVector2>
{
/// <summary>
/// The maximum byte value.
/// </summary>
private static readonly Vector4 MaxBytes = new Vector4(255);
/// <summary>
/// The half vector value.
/// </summary>
private static readonly Vector4 Half = new Vector4(0.5F);
/// <summary>
/// Initializes a new instance of the <see cref="HalfVector2"/> struct.
/// </summary>
/// <param name="x">The x-component.</param>
/// <param name="y">The y-component.</param>
public HalfVector2(float x, float y)
{
this.PackedValue = Pack(x, y);
}
/// <summary>
/// Initializes a new instance of the <see cref="HalfVector2"/> struct.
/// </summary>
/// <param name="vector">A vector containing the initial values for the components.</param>
public HalfVector2(Vector2 vector)
{
this.PackedValue = Pack(vector.X, vector.Y);
}
/// <inheritdoc />
public uint PackedValue { get; set; }
/// <summary>
/// Compares two <see cref="HalfVector2"/> objects for equality.
/// </summary>
/// <param name="left">
/// The <see cref="HalfVector2"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="HalfVector2"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator ==(HalfVector2 left, HalfVector2 right)
{
return left.Equals(right);
}
/// <summary>
/// Compares two <see cref="HalfVector2"/> objects for equality.
/// </summary>
/// <param name="left">
/// The <see cref="HalfVector2"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="HalfVector2"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
public static bool operator !=(HalfVector2 left, HalfVector2 right)
{
return !left.Equals(right);
}
/// <summary>
/// Expands the packed representation into a <see cref="Vector2"/>.
/// </summary>
/// <returns>The <see cref="Vector2"/>.</returns>
public Vector2 ToVector2()
{
Vector2 vector;
vector.X = HalfTypeHelper.Unpack((ushort)this.PackedValue);
vector.Y = HalfTypeHelper.Unpack((ushort)(this.PackedValue >> 0x10));
return vector;
}
/// <inheritdoc />
public void PackFromVector4(Vector4 vector)
{
this.PackedValue = Pack(vector.X, vector.Y);
}
/// <inheritdoc />
public Vector4 ToVector4()
{
Vector2 vector = this.ToVector2();
return new Vector4(vector.X, vector.Y, 0F, 1F);
}
/// <inheritdoc />
public void PackFromBytes(byte x, byte y, byte z, byte w)
{
this.PackFromVector4(new Vector4(x, y, z, w) / MaxBytes);
}
/// <inheritdoc />
public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder)
{
Vector4 vector = this.ToVector4();
vector *= MaxBytes;
vector += Half;
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();
}
}
/// <inheritdoc />
public override string ToString()
{
return this.ToVector2().ToString();
}
/// <inheritdoc />
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
/// <inheritdoc />
public override bool Equals(object obj)
{
return (obj is HalfVector2) && this.Equals((HalfVector2)obj);
}
/// <inheritdoc />
public bool Equals(HalfVector2 other)
{
return this.PackedValue.Equals(other.PackedValue);
}
/// <summary>
/// Packs the <see cref="float"/> components into a <see cref="uint"/>.
/// </summary>
/// <param name="x">The x-component</param>
/// <param name="y">The y-component</param>
/// <returns>The <see cref="uint"/> containing the packed values.</returns>
private static uint Pack(float x, float y)
{
uint num2 = HalfTypeHelper.Pack(x);
uint num = (uint)(HalfTypeHelper.Pack(y) << 0x10);
return num2 | num;
}
}
}

37
tests/ImageSharp.Tests/Colors/PackedPixelTests.cs

@ -285,6 +285,43 @@ namespace ImageSharp.Tests.Colors
Assert.Equal(bgra, new byte[] { 0, 0, 128, 255 });
}
[Fact]
public void HalfVector2()
{
// Test PackedValue
Assert.Equal(0u, new HalfVector2(Vector2.Zero).PackedValue);
Assert.Equal(1006648320u, new HalfVector2(Vector2.One).PackedValue);
Assert.Equal(3033345638u, new HalfVector2(0.1f, -0.3f).PackedValue);
// Test ToVector2
Assert.True(Equal(Vector2.Zero, new HalfVector2(Vector2.Zero).ToVector2()));
Assert.True(Equal(Vector2.One, new HalfVector2(Vector2.One).ToVector2()));
Assert.True(Equal(Vector2.UnitX, new HalfVector2(Vector2.UnitX).ToVector2()));
Assert.True(Equal(Vector2.UnitY, new HalfVector2(Vector2.UnitY).ToVector2()));
// Test ordering
float x = .5F;
float y = .25F;
Assert.True(Equal(new Vector4(x, y, 0, 1), new HalfVector2(x, y).ToVector4()));
byte[] rgb = new byte[3];
byte[] rgba = new byte[4];
byte[] bgr = new byte[3];
byte[] bgra = new byte[4];
new HalfVector2(x, y).ToBytes(rgb, 0, ComponentOrder.XYZ);
Assert.Equal(rgb, new byte[] { 128, 64, 0 });
new HalfVector2(x, y).ToBytes(rgba, 0, ComponentOrder.XYZW);
Assert.Equal(rgba, new byte[] { 128, 64, 0, 255 });
new HalfVector2(x, y).ToBytes(bgr, 0, ComponentOrder.ZYX);
Assert.Equal(bgr, new byte[] { 0, 64, 128 });
new HalfVector2(x, y).ToBytes(bgra, 0, ComponentOrder.ZYXW);
Assert.Equal(bgra, new byte[] { 0, 64, 128, 255 });
}
// Comparison helpers with small tolerance to allow for floating point rounding during computations.
public static bool Equal(float a, float b)
{

Loading…
Cancel
Save