Browse Source

Add Rg32

af/merge-core
James Jackson-South 9 years ago
parent
commit
3451e72b33
  1. 173
      src/ImageSharp/Colors/PackedPixel/Rg32.cs
  2. 42
      tests/ImageSharp.Tests/Colors/PackedPixelTests.cs
  3. 1
      tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

173
src/ImageSharp/Colors/PackedPixel/Rg32.cs

@ -0,0 +1,173 @@
// <copyright file="Rg32.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 unsigned normalized values ranging from 0 to 1.
/// </summary>
public struct Rg32 : IPackedPixel<uint>, IEquatable<Rg32>, IPackedVector
{
/// <summary>
/// Initializes a new instance of the <see cref="Rg32"/> struct.
/// </summary>
/// <param name="x">The x-component</param>
/// <param name="y">The y-component</param>
public Rg32(float x, float y)
{
this.PackedValue = Pack(x, y);
}
/// <summary>
/// Initializes a new instance of the <see cref="Rg32"/> struct.
/// </summary>
/// <param name="vector">The vector containing the component values.</param>
public Rg32(Vector2 vector)
{
this.PackedValue = Pack(vector.X, vector.Y);
}
/// <inheritdoc />
public uint PackedValue { get; set; }
/// <summary>
/// Compares two <see cref="Rg32"/> objects for equality.
/// </summary>
/// <param name="left">
/// The <see cref="Rg32"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="Rg32"/> 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 ==(Rg32 left, Rg32 right)
{
return left.PackedValue == right.PackedValue;
}
/// <summary>
/// Compares two <see cref="Rg32"/> objects for equality.
/// </summary>
/// <param name="left">
/// The <see cref="Rg32"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="Rg32"/> 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 !=(Rg32 left, Rg32 right)
{
return left.PackedValue != right.PackedValue;
}
/// <summary>
/// Expands the packed representation into a <see cref="Vector2"/>.
/// The vector components are typically expanded in least to greatest significance order.
/// </summary>
/// <returns>The <see cref="Vector2"/>.</returns>
public Vector2 ToVector2()
{
return new Vector2(
(this.PackedValue & 0xFFFF) / 65535F,
((this.PackedValue >> 16) & 0xFFFF) / 65535F);
}
/// <inheritdoc />
public void PackFromVector4(Vector4 vector)
{
this.PackedValue = Pack(vector.X, vector.Y);
}
/// <inheritdoc />
public Vector4 ToVector4()
{
return new Vector4(this.ToVector2(), 0F, 1F);
}
/// <inheritdoc />
public void PackFromBytes(byte x, byte y, byte z, byte w)
{
this.PackFromVector4(new Vector4(x, y, z, w) / 255F);
}
/// <inheritdoc />
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();
}
}
/// <inheritdoc />
public override bool Equals(object obj)
{
return (obj is Rg32) && this.Equals((Rg32)obj);
}
/// <inheritdoc />
public bool Equals(Rg32 other)
{
return this.PackedValue == other.PackedValue;
}
/// <inheritdoc />
public override string ToString()
{
return this.ToVector2().ToString();
}
/// <inheritdoc />
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
/// <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)
{
return (uint)(
((int)Math.Round(x.Clamp(0, 1) * 65535F) & 0xFFFF) |
(((int)Math.Round(y.Clamp(0, 1) * 65535F) & 0xFFFF) << 16));
}
}
}

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

@ -564,6 +564,48 @@ namespace ImageSharp.Tests.Colors
Assert.Equal(rgba, new byte[] { 9, 115, 202, 127 });
}
[Fact]
public void Rg32()
{
// Test the limits.
Assert.Equal((uint)0x0, new Rg32(Vector2.Zero).PackedValue);
Assert.Equal(0xFFFFFFFF, new Rg32(Vector2.One).PackedValue);
// Test ToVector2
Assert.True(Equal(Vector2.Zero, new Rg32(Vector2.Zero).ToVector2()));
Assert.True(Equal(Vector2.One, new Rg32(Vector2.One).ToVector2()));
// Test clamping.
Assert.True(Equal(Vector2.Zero, new Rg32(Vector2.One * -1234.0f).ToVector2()));
Assert.True(Equal(Vector2.One, new Rg32(Vector2.One * 1234.0f).ToVector2()));
// Test Ordering
float x = 0xb6dc;
float y = 0xA59f;
Assert.Equal(0xa59fb6dc, new Rg32(x / 0xffff, y / 0xffff).PackedValue);
x = 0.1f;
y = -0.3f;
Assert.Equal((uint)6554, new Rg32(x, y).PackedValue);
// Test ordering
byte[] rgb = new byte[3];
byte[] rgba = new byte[4];
byte[] bgr = new byte[3];
byte[] bgra = new byte[4];
new Rg32(x, y).ToBytes(rgb, 0, ComponentOrder.XYZ);
Assert.Equal(rgb, new byte[] { 25, 0, 0 });
new Rg32(x, y).ToBytes(rgba, 0, ComponentOrder.XYZW);
Assert.Equal(rgba, new byte[] { 25, 0, 0, 255 });
new Rg32(x, y).ToBytes(bgr, 0, ComponentOrder.ZYX);
Assert.Equal(bgr, new byte[] { 0, 0, 25 });
new Rg32(x, y).ToBytes(bgra, 0, ComponentOrder.ZYXW);
Assert.Equal(bgra, new byte[] { 0, 0, 25, 255 });
}
// Comparison helpers with small tolerance to allow for floating point rounding during computations.
public static bool Equal(float a, float b)
{

1
tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

@ -61,6 +61,7 @@ namespace ImageSharp.Tests
// Image<HalfSingle, ushort> image = file.CreateImage().To<HalfSingle, ushort>();
// Image<HalfVector2, uint> image = file.CreateImage().To<HalfVector2, uint>();
// Image<HalfVector4, ulong> image = file.CreateImage().To<HalfVector4, ulong>();
// Image<Rg32, uint> image = file.CreateImage().To<Rg32, uint>();
// TODO: Conversion between types who's vector ranges are different are not possible without scaling function, Make static version of known ones.
// Image<NormalizedByte2, ushort> image = file.CreateImage().To<NormalizedByte2, ushort>(v => (2F * v) - Vector4.One);

Loading…
Cancel
Save