📷 A modern, cross-platform, 2D Graphics library for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

133 lines
4.9 KiB

// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System.Numerics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Tests.PixelFormats;
[Trait("Category", "PixelFormats")]
public class NormalizedShort2Tests
{
[Fact]
public void NormalizedShort2_PackedValue()
{
Assert.Equal(0xE6672CCC, new NormalizedShort2(0.35f, -0.2f).PackedValue);
Assert.Equal(3650751693, new NormalizedShort2(0.1f, -0.3f).PackedValue);
Assert.Equal(0x0U, new NormalizedShort2(Vector2.Zero).PackedValue);
Assert.Equal(0x7FFF7FFFU, new NormalizedShort2(Vector2.One).PackedValue);
Assert.Equal(0x80018001, new NormalizedShort2(-Vector2.One).PackedValue);
// TODO: I don't think this can ever pass since the bytes are already truncated.
// Assert.Equal(3650751693, n.PackedValue);
}
[Fact]
public void NormalizedShort2_ToVector2()
{
Assert.Equal(Vector2.One, new NormalizedShort2(Vector2.One).ToVector2());
Assert.Equal(Vector2.Zero, new NormalizedShort2(Vector2.Zero).ToVector2());
Assert.Equal(-Vector2.One, new NormalizedShort2(-Vector2.One).ToVector2());
Assert.Equal(Vector2.One, new NormalizedShort2(Vector2.One * 1234.0f).ToVector2());
Assert.Equal(-Vector2.One, new NormalizedShort2(Vector2.One * -1234.0f).ToVector2());
}
[Fact]
public void NormalizedShort2_MinimumStorageCodeDecodesAsNegativeOne()
{
NormalizedShort2 pixel = new() { PackedValue = 0x80008000 };
Vector4 expectedNative = new(-1F, -1F, 0F, 1F);
Vector4 expectedScaled = new(0F, 0F, 0F, 1F);
Assert.Equal(expectedNative, pixel.ToVector4());
Assert.Equal(expectedScaled, pixel.ToScaledVector4());
NormalizedShort2[] source = new NormalizedShort2[17];
Vector4[] native = new Vector4[source.Length];
Vector4[] scaled = new Vector4[source.Length];
Array.Fill(source, pixel);
PixelOperations<NormalizedShort2>.Instance.ToVector4(Configuration.Default, source, native);
PixelOperations<NormalizedShort2>.Instance.ToVector4(Configuration.Default, source, scaled, PixelConversionModifiers.Scale);
for (int i = 0; i < source.Length; i++)
{
Assert.Equal(expectedNative, native[i]);
Assert.Equal(expectedScaled, scaled[i]);
}
Vector4[] destructiveSource = new Vector4[source.Length];
Array.Fill(destructiveSource, expectedScaled);
NormalizedShort2[] actualPixels = new NormalizedShort2[source.Length];
PixelOperations<NormalizedShort2>.Instance.FromVector4Destructive(Configuration.Default, destructiveSource, actualPixels, PixelConversionModifiers.Scale);
Assert.All(actualPixels, actual => Assert.Equal(0x80018001U, actual.PackedValue));
}
[Fact]
public void NormalizedShort2_ToVector4()
{
Assert.Equal(new Vector4(1, 1, 0, 1), new NormalizedShort2(Vector2.One).ToVector4());
Assert.Equal(new Vector4(0, 0, 0, 1), new NormalizedShort2(Vector2.Zero).ToVector4());
}
[Fact]
public void NormalizedShort2_ToScaledVector4()
{
// arrange
NormalizedShort2 short2 = new(-Vector2.One);
// act
Vector4 actual = short2.ToScaledVector4();
// assert
Assert.Equal(0, actual.X);
Assert.Equal(0, actual.Y);
Assert.Equal(0, actual.Z);
Assert.Equal(1, actual.W);
}
[Fact]
public void NormalizedShort2_FromScaledVector4()
{
// arrange
Vector4 scaled = new NormalizedShort2(-Vector2.One).ToScaledVector4();
const uint expected = 0x80018001;
// act
NormalizedShort2 short2 = NormalizedShort2.FromScaledVector4(scaled);
uint actual = short2.PackedValue;
// assert
Assert.Equal(expected, actual);
}
[Fact]
public void NormalizedShort2_FromBgra5551()
{
// arrange
Vector4 expected = new(1, 1, 0, 1);
// act
NormalizedShort2 normalizedShort2 = NormalizedShort2.FromBgra5551(new Bgra5551(1f, 1f, 1f, 1f));
// assert
Assert.Equal(expected, normalizedShort2.ToVector4());
}
[Fact]
public void NormalizedShort2_PixelInformation()
{
PixelTypeInfo info = NormalizedShort2.GetPixelTypeInfo();
Assert.Equal(Unsafe.SizeOf<NormalizedShort2>() * 8, info.BitsPerPixel);
Assert.Equal(PixelAlphaRepresentation.None, info.AlphaRepresentation);
Assert.Equal(PixelColorType.Red | PixelColorType.Green, info.ColorType);
PixelComponentInfo componentInfo = info.ComponentInfo.Value;
Assert.Equal(2, componentInfo.ComponentCount);
Assert.Equal(0, componentInfo.Padding);
Assert.Equal(16, componentInfo.GetComponentPrecision(0));
Assert.Equal(16, componentInfo.GetComponentPrecision(1));
Assert.Equal(16, componentInfo.GetMaximumComponentPrecision());
}
}