// Copyright (c) Six Labors and contributors. // Licensed under the Apache License, Version 2.0. using System; using System.Numerics; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.PixelFormats; using Xunit; using Xunit.Abstractions; namespace SixLabors.ImageSharp.Tests.PixelFormats { public partial class PixelOperationsTests { public class Rgba32 : PixelOperationsTests { public Rgba32(ITestOutputHelper output) : base(output) { } // For 4.6 test runner MemberData does not work without redeclaring the public field in the derived test class: public static new TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; [Fact] public void IsSpecialImplementation() { Assert.IsType(PixelOperations.Instance); } [Fact] public void ToVector4SimdAligned() { ImageSharp.PixelFormats.Rgba32[] source = CreatePixelTestData(64); Vector4[] expected = CreateExpectedVector4Data(source); TestOperation( source, expected, (s, d) => ImageSharp.PixelFormats.Rgba32.PixelOperations.ToVector4SimdAligned(s, d.Span, 64) ); } // [Fact] // Profiling benchmark - enable manually! #pragma warning disable xUnit1013 // Public method should be marked as test public void Benchmark_ToVector4() #pragma warning restore xUnit1013 // Public method should be marked as test { int times = 200000; int count = 1024; using (IBuffer source = Configuration.Default.MemoryManager.Allocate(count)) using (IBuffer dest = Configuration.Default.MemoryManager.Allocate(count)) { this.Measure( times, () => { PixelOperations.Instance.ToVector4(source.Span, dest.Span, count); }); } } } public class Argb32 : PixelOperationsTests { // For 4.6 test runner MemberData does not work without redeclaring the public field in the derived test class: public Argb32(ITestOutputHelper output) : base(output) { } public static new TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; } [Theory] [WithBlankImages(1, 1, PixelTypes.All)] public void GetGlobalInstance(TestImageProvider dummy) where TPixel : struct, IPixel { Assert.NotNull(PixelOperations.Instance); } } public abstract class PixelOperationsTests : MeasureFixture where TPixel : struct, IPixel { protected PixelOperationsTests(ITestOutputHelper output) : base(output) { } public static TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; private static PixelOperations Operations => PixelOperations.Instance; internal static TPixel[] CreateExpectedPixelData(Vector4[] source) { TPixel[] expected = new TPixel[source.Length]; for (int i = 0; i < expected.Length; i++) { expected[i].PackFromVector4(source[i]); } return expected; } [Theory] [MemberData(nameof(ArraySizesData))] public void PackFromVector4(int count) { Vector4[] source = CreateVector4TestData(count); TPixel[] expected = CreateExpectedPixelData(source); TestOperation( source, expected, (s, d) => Operations.PackFromVector4(s, d.Span, count) ); } internal static Vector4[] CreateExpectedVector4Data(TPixel[] source) { Vector4[] expected = new Vector4[source.Length]; for (int i = 0; i < expected.Length; i++) { expected[i] = source[i].ToVector4(); } return expected; } [Theory] [MemberData(nameof(ArraySizesData))] public void ToVector4(int count) { TPixel[] source = CreatePixelTestData(count); Vector4[] expected = CreateExpectedVector4Data(source); TestOperation( source, expected, (s, d) => Operations.ToVector4(s, d.Span, count) ); } [Theory] [MemberData(nameof(ArraySizesData))] public void PackFromXyzBytes(int count) { byte[] source = CreateByteTestData(count * 3); TPixel[] expected = new TPixel[count]; for (int i = 0; i < count; i++) { int i3 = i * 3; expected[i].PackFromRgba32(new Rgba32(source[i3 + 0], source[i3 + 1], source[i3 + 2], 255)); } TestOperation( source, expected, (s, d) => Operations.PackFromRgb24Bytes(s, d.Span, count) ); } [Theory] [MemberData(nameof(ArraySizesData))] public void ToXyzBytes(int count) { TPixel[] source = CreatePixelTestData(count); byte[] expected = new byte[count * 3]; var rgb = default(Rgb24); for (int i = 0; i < count; i++) { int i3 = i * 3; source[i].ToRgb24(ref rgb); expected[i3] = rgb.R; expected[i3 + 1] = rgb.G; expected[i3 + 2] = rgb.B; } TestOperation( source, expected, (s, d) => Operations.ToRgb24Bytes(s, d.Span, count) ); } [Theory] [MemberData(nameof(ArraySizesData))] public void PackFromXyzwBytes(int count) { byte[] source = CreateByteTestData(count * 4); TPixel[] expected = new TPixel[count]; for (int i = 0; i < count; i++) { int i4 = i * 4; expected[i].PackFromRgba32(new Rgba32(source[i4 + 0], source[i4 + 1], source[i4 + 2], source[i4 + 3])); } TestOperation( source, expected, (s, d) => Operations.PackFromRgba32Bytes(s, d.Span, count) ); } [Theory] [MemberData(nameof(ArraySizesData))] public void ToXyzwBytes(int count) { TPixel[] source = CreatePixelTestData(count); byte[] expected = new byte[count * 4]; var rgba = default(Rgba32); for (int i = 0; i < count; i++) { int i4 = i * 4; source[i].ToRgba32(ref rgba); expected[i4] = rgba.R; expected[i4 + 1] = rgba.G; expected[i4 + 2] = rgba.B; expected[i4 + 3] = rgba.A; } TestOperation( source, expected, (s, d) => Operations.ToRgba32Bytes(s, d.Span, count) ); } [Theory] [MemberData(nameof(ArraySizesData))] public void PackFromZyxBytes(int count) { byte[] source = CreateByteTestData(count * 3); TPixel[] expected = new TPixel[count]; for (int i = 0; i < count; i++) { int i3 = i * 3; expected[i].PackFromRgba32(new Rgba32(source[i3 + 2], source[i3 + 1], source[i3 + 0], 255)); } TestOperation( source, expected, (s, d) => Operations.PackFromBgr24Bytes(s, d.Span, count) ); } [Theory] [MemberData(nameof(ArraySizesData))] public void ToZyxBytes(int count) { TPixel[] source = CreatePixelTestData(count); byte[] expected = new byte[count * 3]; var bgr = default(Bgr24); for (int i = 0; i < count; i++) { int i3 = i * 3; source[i].ToBgr24(ref bgr); expected[i3] = bgr.B; expected[i3 + 1] = bgr.G; expected[i3 + 2] = bgr.R; } TestOperation( source, expected, (s, d) => Operations.ToBgr24Bytes(s, d.Span, count) ); } [Theory] [MemberData(nameof(ArraySizesData))] public void PackFromZyxwBytes(int count) { byte[] source = CreateByteTestData(count * 4); TPixel[] expected = new TPixel[count]; for (int i = 0; i < count; i++) { int i4 = i * 4; expected[i].PackFromRgba32(new Rgba32(source[i4 + 2], source[i4 + 1], source[i4 + 0], source[i4 + 3])); } TestOperation( source, expected, (s, d) => Operations.PackFromBgra32Bytes(s, d.Span, count) ); } [Theory] [MemberData(nameof(ArraySizesData))] public void ToZyxwBytes(int count) { TPixel[] source = CreatePixelTestData(count); byte[] expected = new byte[count * 4]; var bgra = default(Bgra32); for (int i = 0; i < count; i++) { int i4 = i * 4; source[i].ToBgra32(ref bgra); expected[i4] = bgra.B; expected[i4 + 1] = bgra.G; expected[i4 + 2] = bgra.R; expected[i4 + 3] = bgra.A; } TestOperation( source, expected, (s, d) => Operations.ToBgra32Bytes(s, d.Span, count) ); } private class TestBuffers : IDisposable where TSource : struct where TDest : struct { public TSource[] SourceBuffer { get; } public IBuffer ActualDestBuffer { get; } public TDest[] ExpectedDestBuffer { get; } public TestBuffers(TSource[] source, TDest[] expectedDest) { this.SourceBuffer = source; this.ExpectedDestBuffer = expectedDest; this.ActualDestBuffer = Configuration.Default.MemoryManager.Allocate(expectedDest.Length); } public void Dispose() { this.ActualDestBuffer.Dispose(); } private const float Tolerance = 0.0001f; public void Verify() { int count = this.ExpectedDestBuffer.Length; if (typeof(TDest) == typeof(Vector4)) { Span expected = this.ExpectedDestBuffer.AsSpan().NonPortableCast(); Span actual = this.ActualDestBuffer.Span.NonPortableCast(); for (int i = 0; i < count; i++) { // ReSharper disable PossibleNullReferenceException Assert.Equal(expected[i], actual[i], new ApproximateFloatComparer(0.001f)); // ReSharper restore PossibleNullReferenceException } } else { Span expected = this.ExpectedDestBuffer.AsSpan(); Span actual = this.ActualDestBuffer.Span; for (int i = 0; i < count; i++) { Assert.Equal(expected[i], actual[i]); } } } } internal static void TestOperation( TSource[] source, TDest[] expected, Action> action) where TSource : struct where TDest : struct { using (var buffers = new TestBuffers(source, expected)) { action(buffers.SourceBuffer, buffers.ActualDestBuffer); buffers.Verify(); } } internal static Vector4[] CreateVector4TestData(int length) { Vector4[] result = new Vector4[length]; var rnd = new Random(42); // Deterministic random values for (int i = 0; i < result.Length; i++) { result[i] = GetVector(rnd); } return result; } internal static TPixel[] CreatePixelTestData(int length) { TPixel[] result = new TPixel[length]; var rnd = new Random(42); // Deterministic random values for (int i = 0; i < result.Length; i++) { Vector4 v = GetVector(rnd); result[i].PackFromVector4(v); } return result; } internal static byte[] CreateByteTestData(int length) { byte[] result = new byte[length]; var rnd = new Random(42); // Deterministic random values for (int i = 0; i < result.Length; i++) { result[i] = (byte)rnd.Next(255); } return result; } internal static Vector4 GetVector(Random rnd) { return new Vector4( (float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble() ); } } }