Browse Source

Add HalfSingle

af/merge-core
James Jackson-South 10 years ago
parent
commit
7dcd46a678
  1. 4
      src/ImageSharp/Colors/PackedPixel/Alpha8.cs
  2. 2
      src/ImageSharp/Colors/PackedPixel/Bgra5551.cs
  3. 3
      src/ImageSharp/Colors/PackedPixel/Byte4.cs
  4. 160
      src/ImageSharp/Colors/PackedPixel/HalfSingle.cs
  5. 144
      src/ImageSharp/Colors/PackedPixel/HalfTypeHelper.cs
  6. 40
      tests/ImageSharp.Tests/Colors/PackedPixelTests.cs
  7. 1
      tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

4
src/ImageSharp/Colors/PackedPixel/Alpha8.cs

@ -16,9 +16,7 @@ namespace ImageSharp
/// <summary>
/// Initializes a new instance of the <see cref="Alpha8"/> struct.
/// </summary>
/// <param name="alpha">
/// The alpha component
/// </param>
/// <param name="alpha">The alpha component</param>
public Alpha8(float alpha)
{
this.PackedValue = Pack(alpha);

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

@ -11,7 +11,7 @@ namespace ImageSharp
/// <summary>
/// Packed pixel type containing unsigned normalized values ranging from 0 to 1. The x , y and z components use 5 bits, and the w component uses 1 bit.
/// </summary>
public struct Bgra5551 : IPackedPixel<ushort>, IEquatable<Bgra5551>, IPackedVector
public struct Bgra5551 : IPackedPixel<ushort>, IEquatable<Bgra5551>
{
/// <summary>
/// Initializes a new instance of the <see cref="Bgra5551"/> struct.

3
src/ImageSharp/Colors/PackedPixel/Byte4.cs

@ -3,7 +3,6 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
{
using System;
@ -12,7 +11,7 @@ namespace ImageSharp
/// <summary>
/// Packed pixel type containing four 8-bit unsigned integer values, ranging from 0 to 255.
/// </summary>
public struct Byte4 : IPackedPixel<uint>, IEquatable<Byte4>, IPackedVector
public struct Byte4 : IPackedPixel<uint>, IEquatable<Byte4>
{
/// <summary>
/// Initializes a new instance of the <see cref="Byte4"/> struct.

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

@ -0,0 +1,160 @@
// <copyright file="HalfSingle.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 a single 16 bit floating point value.
/// </summary>
public struct HalfSingle : IPackedPixel<ushort>, IEquatable<HalfSingle>
{
/// <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="HalfSingle"/> struct.
/// </summary>
/// <param name="single">The single component.</param>
public HalfSingle(float single)
{
this.PackedValue = HalfTypeHelper.Pack(single);
}
/// <inheritdoc />
public ushort PackedValue { get; set; }
/// <summary>
/// Compares two <see cref="HalfSingle"/> objects for equality.
/// </summary>
/// <param name="left">
/// The <see cref="HalfSingle"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="HalfSingle"/> 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 ==(HalfSingle left, HalfSingle right)
{
return left.PackedValue == right.PackedValue;
}
/// <summary>
/// Compares two <see cref="HalfSingle"/> objects for equality.
/// </summary>
/// <param name="left">
/// The <see cref="HalfSingle"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="HalfSingle"/> 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 !=(HalfSingle left, HalfSingle right)
{
return left.PackedValue != right.PackedValue;
}
/// <summary>
/// Expands the packed representation into a <see cref="float"/>.
/// </summary>
/// <returns>The <see cref="float"/>.</returns>
public float ToSingle()
{
return HalfTypeHelper.Unpack(this.PackedValue);
}
/// <inheritdoc />
public void PackFromVector4(Vector4 vector)
{
this.PackedValue = HalfTypeHelper.Pack(vector.X);
}
/// <inheritdoc />
public Vector4 ToVector4()
{
return new Vector4(this.ToSingle(), 0, 0, 1);
}
/// <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();
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 bool Equals(object obj)
{
return (obj is HalfSingle) && this.Equals((HalfSingle)obj);
}
/// <inheritdoc />
public bool Equals(HalfSingle other)
{
return this.PackedValue == other.PackedValue;
}
/// <inheritdoc />
public override string ToString()
{
return this.ToSingle().ToString();
}
/// <inheritdoc />
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
}
}

144
src/ImageSharp/Colors/PackedPixel/HalfTypeHelper.cs

@ -0,0 +1,144 @@
// <copyright file="HalfTypeHelper.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.Runtime.InteropServices;
/// <summary>
/// Helper methods for packing and unpacking floating point values
/// </summary>
internal class HalfTypeHelper
{
/// <summary>
/// Packs a <see cref="float"/> into an <see cref="ushort"/>
/// </summary>
/// <param name="value">The float to pack</param>
/// <returns>The <see cref="ushort"/></returns>
internal static ushort Pack(float value)
{
Uif uif = new Uif { F = value };
return Pack(uif.I);
}
/// <summary>
/// Packs an <see cref="int"/> into a <see cref="ushort"/>
/// </summary>
/// <param name="value">The integer to pack.</param>
/// <returns>The <see cref="ushort"/></returns>
internal static ushort Pack(int value)
{
int s = (value >> 16) & 0x00008000;
int e = ((value >> 23) & 0x000000ff) - (127 - 15);
int m = value & 0x007fffff;
if (e <= 0)
{
if (e < -10)
{
return (ushort)s;
}
m = m | 0x00800000;
int t = 14 - e;
int a = (1 << (t - 1)) - 1;
int b = (m >> t) & 1;
m = (m + a + b) >> t;
return (ushort)(s | m);
}
if (e == 0xff - (127 - 15))
{
if (m == 0)
{
return (ushort)(s | 0x7c00);
}
m >>= 13;
return (ushort)(s | 0x7c00 | m | ((m == 0) ? 1 : 0));
}
m = m + 0x00000fff + ((m >> 13) & 1);
if ((m & 0x00800000) != 0)
{
m = 0;
e += 1;
}
if (e > 30)
{
return (ushort)(s | 0x7c00);
}
return (ushort)(s | (e << 10) | (m >> 13));
}
/// <summary>
/// Unpacks a <see cref="ushort"/> into a <see cref="float"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The <see cref="float"/>.</returns>
internal static float Unpack(ushort value)
{
uint result;
uint mantissa = (uint)(value & 1023);
uint exponent = 0xfffffff2;
if ((value & -33792) == 0)
{
if (mantissa != 0)
{
while ((mantissa & 1024) == 0)
{
exponent--;
mantissa = mantissa << 1;
}
mantissa &= 0xfffffbff;
result = ((uint)((((uint)value & 0x8000) << 16) | ((exponent + 127) << 23))) | (mantissa << 13);
}
else
{
result = (uint)((value & 0x8000) << 16);
}
}
else
{
result = ((((uint)value & 0x8000) << 16) | ((((((uint)value >> 10) & 0x1f) - 15) + 127) << 23)) | (mantissa << 13);
}
Uif uif = new Uif { U = result };
return uif.F;
}
/// <summary>
/// Maps the position of number types in memory
/// </summary>
[StructLayout(LayoutKind.Explicit)]
private struct Uif
{
/// <summary>
/// The float.
/// </summary>
[FieldOffset(0)]
public float F;
/// <summary>
/// The integer.
/// </summary>
[FieldOffset(0)]
public int I;
/// <summary>
/// The unsigned integer.
/// </summary>
[FieldOffset(0)]
public uint U;
}
}
}

40
tests/ImageSharp.Tests/Colors/PackedVectorTests.cs → tests/ImageSharp.Tests/Colors/PackedPixelTests.cs

@ -1,4 +1,4 @@
// <copyright file="PackedVectorTests.cs" company="James Jackson-South">
// <copyright file="PackedPixelTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -11,9 +11,9 @@ namespace ImageSharp.Tests.Colors
using Xunit;
/// <summary>
/// The packed vector tests.
/// The packed pixel tests.
/// </summary>
public class PackedVectorTests
public class PackedPixelTests
{
[Fact]
public void Alpha8()
@ -251,6 +251,40 @@ namespace ImageSharp.Tests.Colors
Assert.Equal(bgra, new byte[] { 0, 0, 128, 0 });
}
[Fact]
public void HalfSingle()
{
// Test limits
Assert.Equal(15360, new HalfSingle(1F).PackedValue);
Assert.Equal(0, new HalfSingle(0F).PackedValue);
Assert.Equal(48128, new HalfSingle(-1F).PackedValue);
// Test values
Assert.Equal(11878, new HalfSingle(0.1F).PackedValue);
Assert.Equal(46285, new HalfSingle(-0.3F).PackedValue);
// Test ordering
float x = .5F;
Assert.True(Equal(new Vector4(x, 0, 0, 1), new HalfSingle(x).ToVector4()));
byte[] rgb = new byte[3];
byte[] rgba = new byte[4];
byte[] bgr = new byte[3];
byte[] bgra = new byte[4];
new HalfSingle(x).ToBytes(rgb, 0, ComponentOrder.XYZ);
Assert.Equal(rgb, new byte[] { 128, 0, 0 });
new HalfSingle(x).ToBytes(rgba, 0, ComponentOrder.XYZW);
Assert.Equal(rgba, new byte[] { 128, 0, 0, 255 });
new HalfSingle(x).ToBytes(bgr, 0, ComponentOrder.ZYX);
Assert.Equal(bgr, new byte[] { 0, 0, 128 });
new HalfSingle(x).ToBytes(bgra, 0, ComponentOrder.ZYXW);
Assert.Equal(bgra, new byte[] { 0, 0, 128, 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

@ -56,6 +56,7 @@ namespace ImageSharp.Tests
// Image<Bgra4444, ushort> image = file.CreateImage().To<Bgra4444, ushort>();
// Image<Bgra5551, ushort> image = file.CreateImage().To<Bgra5551, ushort>();
// Image<Byte4, uint> image = file.CreateImage().To<Byte4, uint>();
// Image<HalfSingle, ushort> image = file.CreateImage().To<HalfSingle, ushort>();
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
{

Loading…
Cancel
Save