Browse Source

Add Byte4

pull/33/head
James Jackson-South 10 years ago
parent
commit
aeccdb49a1
  1. 17
      src/ImageSharp/Colors/Color.cs
  2. 178
      src/ImageSharp/Colors/PackedPixel/Byte4.cs
  3. 51
      tests/ImageSharp.Tests/Colors/PackedVectorTests.cs
  4. 1
      tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs

17
src/ImageSharp/Colors/Color.cs

@ -10,7 +10,7 @@ namespace ImageSharp
using System.Numerics;
/// <summary>
/// Packed vector type containing four 8-bit unsigned normalized values ranging from 0 to 255.
/// Packed pixel type containing four 8-bit unsigned normalized values ranging from 0 to 255.
/// The color components are stored in red, green, blue, and alpha order.
/// </summary>
/// <remarks>
@ -19,9 +19,24 @@ namespace ImageSharp
/// </remarks>
public partial struct Color : IPackedPixel<uint>, IEquatable<Color>
{
/// <summary>
/// The shift count for the red component
/// </summary>
private const int RedShift = 0;
/// <summary>
/// The shift count for the green component
/// </summary>
private const int GreenShift = 8;
/// <summary>
/// The shift count for the blue component
/// </summary>
private const int BlueShift = 16;
/// <summary>
/// The shift count for the alpha component
/// </summary>
private const int AlphaShift = 24;
/// <summary>

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

@ -0,0 +1,178 @@
// <copyright file="Byte4.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 four 8-bit unsigned integer values, ranging from 0 to 255.
/// </summary>
public struct Byte4 : IPackedPixel<uint>, IEquatable<Byte4>, IPackedVector
{
/// <summary>
/// Initializes a new instance of the <see cref="Byte4"/> struct.
/// </summary>
/// <param name="vector">
/// A vector containing the initial values for the components of the Byte4 structure.
/// </param>
public Byte4(Vector4 vector)
{
this.PackedValue = Pack(ref vector);
}
/// <summary>
/// Initializes a new instance of the <see cref="Byte4"/> struct.
/// </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>
public Byte4(float x, float y, float z, float w)
{
Vector4 vector = new Vector4(x, y, z, w);
this.PackedValue = Pack(ref vector);
}
/// <inheritdoc />
public uint PackedValue { get; set; }
/// <summary>
/// Compares two <see cref="Byte4"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Byte4"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="Byte4"/> 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 ==(Byte4 left, Byte4 right)
{
return left.PackedValue == right.PackedValue;
}
/// <summary>
/// Compares two <see cref="Byte4"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Byte4"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="Byte4"/> 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 !=(Byte4 left, Byte4 right)
{
return left.PackedValue != right.PackedValue;
}
/// <summary>
/// Sets the packed representation from a Vector4.
/// </summary>
/// <param name="vector">The vector to create the packed representation from.</param>
public void PackFromVector4(Vector4 vector)
{
this.PackedValue = Pack(ref vector);
}
/// <summary>
/// Expands the packed representation into a Vector4.
/// </summary>
/// <returns>The expanded vector.</returns>
public Vector4 ToVector4()
{
return new Vector4(
this.PackedValue & 0xFF,
(this.PackedValue >> 0x8) & 0xFF,
(this.PackedValue >> 0x10) & 0xFF,
(this.PackedValue >> 0x18) & 0xFF);
}
/// <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 Byte4) && this.Equals((Byte4)obj);
}
/// <inheritdoc />
public bool Equals(Byte4 other)
{
return this == other;
}
/// <inheritdoc />
public override int GetHashCode()
{
return this.PackedValue.GetHashCode();
}
/// <summary>
/// Returns a string representation of the current instance.
/// </summary>
/// <returns>String that represents the object.</returns>
public override string ToString()
{
return this.PackedValue.ToString("x8");
}
/// <summary>
/// Packs a vector into a uint.
/// </summary>
/// <param name="vector">The vector containing the values to pack.</param>
/// <returns>The <see cref="uint"/> containing the packed values.</returns>
private static uint Pack(ref Vector4 vector)
{
const float Max = 255F;
const float Min = 0F;
// Clamp the value between min and max values
uint byte4 = (uint)Math.Round(vector.X.Clamp(Min, Max)) & 0xFF;
uint byte3 = ((uint)Math.Round(vector.Y.Clamp(Min, Max)) & 0xFF) << 0x8;
uint byte2 = ((uint)Math.Round(vector.Z.Clamp(Min, Max)) & 0xFF) << 0x10;
uint byte1 = ((uint)Math.Round(vector.W.Clamp(Min, Max)) & 0xFF) << 0x18;
return byte4 | byte3 | byte2 | byte1;
}
}
}

51
tests/ImageSharp.Tests/Colors/PackedVectorTests.cs

@ -200,6 +200,57 @@ namespace ImageSharp.Tests.Colors
Assert.Equal(bgra, new byte[] { 131, 0, 24, 0 });
}
[Fact]
public void Byte4()
{
// Test the limits.
Assert.Equal((uint)0x0, new Byte4(Vector4.Zero).PackedValue);
Assert.Equal(0xFFFFFFFF, new Byte4(Vector4.One * 255).PackedValue);
// Test ToVector4.
Assert.True(Equal(Vector4.One * 255, new Byte4(Vector4.One * 255).ToVector4()));
Assert.True(Equal(Vector4.Zero, new Byte4(Vector4.Zero).ToVector4()));
Assert.True(Equal(Vector4.UnitX * 255, new Byte4(Vector4.UnitX * 255).ToVector4()));
Assert.True(Equal(Vector4.UnitY * 255, new Byte4(Vector4.UnitY * 255).ToVector4()));
Assert.True(Equal(Vector4.UnitZ * 255, new Byte4(Vector4.UnitZ * 255).ToVector4()));
Assert.True(Equal(Vector4.UnitW * 255, new Byte4(Vector4.UnitW * 255).ToVector4()));
// Test clamping.
Assert.True(Equal(Vector4.Zero, new Byte4(Vector4.One * -1234.0f).ToVector4()));
Assert.True(Equal(Vector4.One * 255, new Byte4(Vector4.One * 1234.0f).ToVector4()));
// Test ordering
float x = 0x2d;
float y = 0x36;
float z = 0x7b;
float w = 0x1a;
Assert.Equal((uint)0x1a7b362d, new Byte4(x, y, z, w).PackedValue);
x = 127.5f;
y = -12.3f;
z = 0.5f;
w = -0.7f;
Assert.Equal((uint)128, new Byte4(x, y, z, w).PackedValue);
// Test ordering
byte[] rgb = new byte[3];
byte[] rgba = new byte[4];
byte[] bgr = new byte[3];
byte[] bgra = new byte[4];
new Byte4(x, y, z, w).ToBytes(rgb, 0, ComponentOrder.XYZ);
Assert.Equal(rgb, new byte[] { 128, 0, 0 });
new Byte4(x, y, z, w).ToBytes(rgba, 0, ComponentOrder.XYZW);
Assert.Equal(rgba, new byte[] { 128, 0, 0, 0 });
new Byte4(x, y, z, w).ToBytes(bgr, 0, ComponentOrder.ZYX);
Assert.Equal(bgr, new byte[] { 0, 0, 128 });
new Byte4(x, y, z, w).ToBytes(bgra, 0, ComponentOrder.ZYXW);
Assert.Equal(bgra, new byte[] { 0, 0, 128, 0 });
}
// 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

@ -55,6 +55,7 @@ namespace ImageSharp.Tests
// Image<Bgr565, ushort> image = file.CreateImage().To<Bgr565, ushort>();
// 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>();
using (FileStream output = File.OpenWrite($"{path}/{file.FileName}"))
{

Loading…
Cancel
Save