mirror of
https://github.com/DarthAffe/RGB.NET.git
synced 2025-12-12 17:48:31 +00:00
(MAJOR) Improvied sampling performance by removing the need to copy region data to a buffer first
This commit is contained in:
parent
af3989aa73
commit
586734b44a
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace RGB.NET.Core;
|
||||
@ -12,12 +11,6 @@ namespace RGB.NET.Core;
|
||||
public abstract class PixelTexture<T> : ITexture
|
||||
where T : unmanaged
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private const int STACK_ALLOC_LIMIT = 1024;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly int _dataPerPixel;
|
||||
@ -85,31 +78,12 @@ public abstract class PixelTexture<T> : ITexture
|
||||
if ((width == 0) || (height == 0)) return Color.Transparent;
|
||||
if ((width == 1) && (height == 1)) return GetColor(GetPixelData(x, y));
|
||||
|
||||
int bufferSize = width * height * _dataPerPixel;
|
||||
if (bufferSize <= STACK_ALLOC_LIMIT)
|
||||
{
|
||||
Span<T> buffer = stackalloc T[bufferSize];
|
||||
GetRegionData(x, y, width, height, buffer);
|
||||
SamplerInfo<T> samplerInfo = new(x, y, width, height, _stride, _dataPerPixel, Data);
|
||||
|
||||
Span<T> pixelData = stackalloc T[_dataPerPixel];
|
||||
Sampler.Sample(new SamplerInfo<T>(width, height, buffer), pixelData);
|
||||
Span<T> pixelData = stackalloc T[_dataPerPixel];
|
||||
Sampler.Sample(samplerInfo, pixelData);
|
||||
|
||||
return GetColor(pixelData);
|
||||
}
|
||||
else
|
||||
{
|
||||
T[] rent = ArrayPool<T>.Shared.Rent(bufferSize);
|
||||
|
||||
Span<T> buffer = new Span<T>(rent)[..bufferSize];
|
||||
GetRegionData(x, y, width, height, buffer);
|
||||
|
||||
Span<T> pixelData = stackalloc T[_dataPerPixel];
|
||||
Sampler.Sample(new SamplerInfo<T>(width, height, buffer), pixelData);
|
||||
|
||||
ArrayPool<T>.Shared.Return(rent);
|
||||
|
||||
return GetColor(pixelData);
|
||||
}
|
||||
return GetColor(pixelData);
|
||||
}
|
||||
}
|
||||
|
||||
@ -152,27 +126,7 @@ public abstract class PixelTexture<T> : ITexture
|
||||
/// <param name="y">The y-location.</param>
|
||||
/// <returns>The pixel-data on the specified location.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected virtual ReadOnlySpan<T> GetPixelData(int x, int y) => Data.Slice((y * _stride) + x, _dataPerPixel);
|
||||
|
||||
/// <summary>
|
||||
/// Writes the pixel-data of the specified region to the passed buffer.
|
||||
/// </summary>
|
||||
/// <param name="x">The x-location of the region to get the data for.</param>
|
||||
/// <param name="y">The y-location of the region to get the data for.</param>
|
||||
/// <param name="width">The width of the region to get the data for.</param>
|
||||
/// <param name="height">The height of the region to get the data for.</param>
|
||||
/// <param name="buffer">The buffer to write the data to.</param>
|
||||
protected virtual void GetRegionData(int x, int y, int width, int height, in Span<T> buffer)
|
||||
{
|
||||
int dataWidth = width * _dataPerPixel;
|
||||
ReadOnlySpan<T> data = Data;
|
||||
for (int i = 0; i < height; i++)
|
||||
{
|
||||
ReadOnlySpan<T> dataSlice = data.Slice((((y + i) * _stride) + x) * _dataPerPixel, dataWidth);
|
||||
Span<T> destination = buffer.Slice(i * dataWidth, dataWidth);
|
||||
dataSlice.CopyTo(destination);
|
||||
}
|
||||
}
|
||||
private ReadOnlySpan<T> GetPixelData(int x, int y) => Data.Slice((y * _stride) + x, _dataPerPixel);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@ -225,6 +179,7 @@ public sealed class PixelTexture : PixelTexture<Color>
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override Color GetColor(in ReadOnlySpan<Color> pixel) => pixel[0];
|
||||
|
||||
#endregion
|
||||
|
||||
@ -30,20 +30,35 @@ public sealed class AverageColorSampler : ISampler<Color>
|
||||
|
||||
float a = 0, r = 0, g = 0, b = 0;
|
||||
|
||||
if (Vector.IsHardwareAccelerated && (info.Data.Length >= Vector<float>.Count))
|
||||
if (Vector.IsHardwareAccelerated && (info.Height > 1) && (info.Width >= ELEMENTS_PER_VECTOR))
|
||||
{
|
||||
int chunks = info.Data.Length / ELEMENTS_PER_VECTOR;
|
||||
int missingElements = info.Data.Length - (chunks * ELEMENTS_PER_VECTOR);
|
||||
int chunks = info.Width / ELEMENTS_PER_VECTOR;
|
||||
int missingElements = info.Width - (chunks * ELEMENTS_PER_VECTOR);
|
||||
|
||||
Vector<float> sum = Vector<float>.Zero;
|
||||
|
||||
fixed (Color* colorPtr = &MemoryMarshal.GetReference(info.Data))
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
Color* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
ReadOnlySpan<Color> data = info[y];
|
||||
|
||||
fixed (Color* colorPtr = &MemoryMarshal.GetReference(data))
|
||||
{
|
||||
sum = Vector.Add(sum, *(Vector<float>*)current);
|
||||
current += ELEMENTS_PER_VECTOR;
|
||||
Color* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
{
|
||||
sum = Vector.Add(sum, *(Vector<float>*)current);
|
||||
current += ELEMENTS_PER_VECTOR;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < missingElements; i++)
|
||||
{
|
||||
Color color = data[^(i + 1)];
|
||||
|
||||
a += color.A;
|
||||
r += color.R;
|
||||
g += color.G;
|
||||
b += color.B;
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,26 +69,17 @@ public sealed class AverageColorSampler : ISampler<Color>
|
||||
g += sum[i + 2];
|
||||
b += sum[i + 3];
|
||||
}
|
||||
|
||||
for (int i = 0; i < missingElements; i++)
|
||||
{
|
||||
Color color = info.Data[^(i + 1)];
|
||||
|
||||
a += color.A;
|
||||
r += color.R;
|
||||
g += color.G;
|
||||
b += color.B;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (Color color in info.Data)
|
||||
{
|
||||
a += color.A;
|
||||
r += color.R;
|
||||
g += color.G;
|
||||
b += color.B;
|
||||
}
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
foreach (Color color in info[y])
|
||||
{
|
||||
a += color.A;
|
||||
r += color.R;
|
||||
g += color.G;
|
||||
b += color.B;
|
||||
}
|
||||
}
|
||||
|
||||
pixelData[0] = new Color(a / count, r / count, g / count, b / count);
|
||||
|
||||
@ -10,20 +10,29 @@ public readonly ref struct SamplerInfo<T>
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly ReadOnlySpan<T> _data;
|
||||
private readonly int _x;
|
||||
private readonly int _y;
|
||||
private readonly int _stride;
|
||||
private readonly int _dataPerPixel;
|
||||
private readonly int _dataWidth;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the width of the region the data comes from.
|
||||
/// </summary>
|
||||
public int Width { get; }
|
||||
public readonly int Width;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the height of region the data comes from.
|
||||
/// </summary>
|
||||
public int Height { get; }
|
||||
public readonly int Height;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the data to sample.
|
||||
/// Gets the data for the requested row.
|
||||
/// </summary>
|
||||
public ReadOnlySpan<T> Data { get; }
|
||||
/// <param name="row">The row to get the data for.</param>
|
||||
/// <returns>A readonly span containing the data of the row.</returns>
|
||||
public ReadOnlySpan<T> this[int row] => _data.Slice((((_y + row) * _stride) + _x) * _dataPerPixel, _dataWidth);
|
||||
|
||||
#endregion
|
||||
|
||||
@ -35,11 +44,17 @@ public readonly ref struct SamplerInfo<T>
|
||||
/// <param name="width">The width of the region the data comes from.</param>
|
||||
/// <param name="height">The height of region the data comes from.</param>
|
||||
/// <param name="data">The data to sample.</param>
|
||||
public SamplerInfo(int width, int height, ReadOnlySpan<T> data)
|
||||
public SamplerInfo(int x, int y, int width, int height, int stride, int dataPerPixel, in ReadOnlySpan<T> data)
|
||||
{
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._data = data;
|
||||
this._stride = stride;
|
||||
this._dataPerPixel = dataPerPixel;
|
||||
this.Width = width;
|
||||
this.Height = height;
|
||||
this.Data = data;
|
||||
|
||||
_dataWidth = width * dataPerPixel;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using RGB.NET.Core;
|
||||
using RGB.NET.Presets.Textures.Sampler;
|
||||
|
||||
@ -59,6 +60,7 @@ public sealed class BytePixelTexture : PixelTexture<byte>
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override Color GetColor(in ReadOnlySpan<byte> pixel)
|
||||
{
|
||||
return ColorFormat switch
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using RGB.NET.Core;
|
||||
using RGB.NET.Presets.Textures.Sampler;
|
||||
|
||||
@ -59,6 +60,7 @@ public sealed class FloatPixelTexture : PixelTexture<float>
|
||||
#region Methods
|
||||
|
||||
/// <inheritdoc />
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected override Color GetColor(in ReadOnlySpan<float> pixel)
|
||||
{
|
||||
return ColorFormat switch
|
||||
|
||||
@ -24,41 +24,48 @@ public sealed class AverageByteSampler : ISampler<byte>
|
||||
int count = info.Width * info.Height;
|
||||
if (count == 0) return;
|
||||
|
||||
ReadOnlySpan<byte> data = info.Data;
|
||||
|
||||
int dataLength = pixelData.Length;
|
||||
Span<uint> sums = stackalloc uint[dataLength];
|
||||
|
||||
if (Vector.IsHardwareAccelerated && (data.Length >= Vector<byte>.Count) && (dataLength <= Vector<byte>.Count))
|
||||
int elementsPerVector = Vector<byte>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
if (Vector.IsHardwareAccelerated && (info.Height > 1) && (info.Width >= valuesPerVector) && (dataLength <= Vector<byte>.Count))
|
||||
{
|
||||
int elementsPerVector = Vector<byte>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
|
||||
int chunks = data.Length / valuesPerVector;
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int chunks = info.Width / elementsPerVector;
|
||||
|
||||
Vector<uint> sum1 = Vector<uint>.Zero;
|
||||
Vector<uint> sum2 = Vector<uint>.Zero;
|
||||
Vector<uint> sum3 = Vector<uint>.Zero;
|
||||
Vector<uint> sum4 = Vector<uint>.Zero;
|
||||
|
||||
fixed (byte* colorPtr = &MemoryMarshal.GetReference(data))
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
byte* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
ReadOnlySpan<byte> data = info[y];
|
||||
|
||||
fixed (byte* colorPtr = &MemoryMarshal.GetReference(data))
|
||||
{
|
||||
Vector<byte> bytes = *(Vector<byte>*)current;
|
||||
Vector.Widen(bytes, out Vector<ushort> short1, out Vector<ushort> short2);
|
||||
Vector.Widen(short1, out Vector<uint> int1, out Vector<uint> int2);
|
||||
Vector.Widen(short2, out Vector<uint> int3, out Vector<uint> int4);
|
||||
byte* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
{
|
||||
Vector<byte> bytes = *(Vector<byte>*)current;
|
||||
Vector.Widen(bytes, out Vector<ushort> short1, out Vector<ushort> short2);
|
||||
Vector.Widen(short1, out Vector<uint> int1, out Vector<uint> int2);
|
||||
Vector.Widen(short2, out Vector<uint> int3, out Vector<uint> int4);
|
||||
|
||||
sum1 = Vector.Add(sum1, int1);
|
||||
sum2 = Vector.Add(sum2, int2);
|
||||
sum3 = Vector.Add(sum3, int3);
|
||||
sum4 = Vector.Add(sum4, int4);
|
||||
sum1 = Vector.Add(sum1, int1);
|
||||
sum2 = Vector.Add(sum2, int2);
|
||||
sum3 = Vector.Add(sum3, int3);
|
||||
sum4 = Vector.Add(sum4, int4);
|
||||
|
||||
current += valuesPerVector;
|
||||
current += valuesPerVector;
|
||||
}
|
||||
}
|
||||
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
|
||||
int value = 0;
|
||||
@ -102,17 +109,16 @@ public sealed class AverageByteSampler : ISampler<byte>
|
||||
if (sumIndex >= dataLength)
|
||||
sumIndex = 0;
|
||||
}
|
||||
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
ReadOnlySpan<byte> data = info[y];
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
}
|
||||
}
|
||||
|
||||
float divisor = count * byte.MaxValue;
|
||||
|
||||
@ -18,45 +18,51 @@ public sealed class AverageFloatSampler : ISampler<float>
|
||||
int count = info.Width * info.Height;
|
||||
if (count == 0) return;
|
||||
|
||||
ReadOnlySpan<float> data = info.Data;
|
||||
|
||||
int dataLength = pixelData.Length;
|
||||
Span<float> sums = stackalloc float[dataLength];
|
||||
|
||||
if (Vector.IsHardwareAccelerated && (data.Length >= Vector<float>.Count) && (dataLength <= Vector<float>.Count))
|
||||
{
|
||||
int elementsPerVector = Vector<float>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
|
||||
int chunks = data.Length / valuesPerVector;
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int elementsPerVector = Vector<float>.Count / dataLength;
|
||||
int valuesPerVector = elementsPerVector * dataLength;
|
||||
if (Vector.IsHardwareAccelerated && (info.Height > 1) && (info.Width >= valuesPerVector) && (dataLength <= Vector<float>.Count))
|
||||
{
|
||||
int chunks = info.Width / elementsPerVector;
|
||||
|
||||
Vector<float> sum = Vector<float>.Zero;
|
||||
|
||||
fixed (float* colorPtr = &MemoryMarshal.GetReference(data))
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
float* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
ReadOnlySpan<float> data = info[y];
|
||||
|
||||
fixed (float* colorPtr = &MemoryMarshal.GetReference(data))
|
||||
{
|
||||
sum = Vector.Add(sum, *(Vector<float>*)current);
|
||||
current += valuesPerVector;
|
||||
float* current = colorPtr;
|
||||
for (int i = 0; i < chunks; i++)
|
||||
{
|
||||
sum = Vector.Add(sum, *(Vector<float>*)current);
|
||||
current += valuesPerVector;
|
||||
}
|
||||
}
|
||||
|
||||
int missingElements = data.Length - (chunks * valuesPerVector);
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
|
||||
for (int i = 0; i < valuesPerVector; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += sum[i + j];
|
||||
|
||||
int offset = chunks * valuesPerVector;
|
||||
for (int i = 0; i < missingElements; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[offset + i + j];
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
for (int y = 0; y < info.Height; y++)
|
||||
{
|
||||
ReadOnlySpan<float> data = info[y];
|
||||
for (int i = 0; i < data.Length; i += dataLength)
|
||||
for (int j = 0; j < sums.Length; j++)
|
||||
sums[j] += data[i + j];
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < pixelData.Length; i++)
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
using System;
|
||||
// ReSharper disable InconsistentNaming
|
||||
|
||||
using System;
|
||||
|
||||
namespace RGB.NET.Core.Tests.Helper;
|
||||
|
||||
@ -342,13 +344,4 @@ public static class SimplexNoise
|
||||
float v = h < 4 ? y : (h == 12) || (h == 14) ? x : z; // Fix repeats at h = 12 to 15
|
||||
return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -v : v);
|
||||
}
|
||||
|
||||
private static float Grad(int hash, float x, float y, float z, float t)
|
||||
{
|
||||
int h = hash & 31; // Convert low 5 bits of hash code into 32 simple
|
||||
float u = h < 24 ? x : y; // gradient directions, and compute dot product.
|
||||
float v = h < 16 ? y : z;
|
||||
float w = h < 8 ? z : t;
|
||||
return ((h & 1) != 0 ? -u : u) + ((h & 2) != 0 ? -v : v) + ((h & 4) != 0 ? -w : w);
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,11 +15,11 @@ public class AverageColorSamplerTest
|
||||
data.Fill(new Core.Color(1f, 1f, 1f, 1f));
|
||||
Core.Color[] result = new Core.Color[1];
|
||||
|
||||
SamplerInfo<Core.Color> info = new(2, 3, data[..6]);
|
||||
SamplerInfo<Core.Color> info = new(0, 0, 2, 3, 16, 1, data);
|
||||
new AverageColorSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Core.Color(1f, 1f, 1f, 1f), result[0]);
|
||||
|
||||
info = new SamplerInfo<Core.Color>(16, 16, data);
|
||||
info = new SamplerInfo<Core.Color>(0, 0, 16, 16, 16, 1, data);
|
||||
new AverageColorSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Core.Color(1f, 1f, 1f, 1f), result[0]);
|
||||
}
|
||||
@ -31,11 +31,11 @@ public class AverageColorSamplerTest
|
||||
data.Fill(new Core.Color(1f, 0f, 0f, 0f));
|
||||
Core.Color[] result = new Core.Color[1];
|
||||
|
||||
SamplerInfo<Core.Color> info = new(2, 3, data[..6]);
|
||||
SamplerInfo<Core.Color> info = new(0, 0, 2, 3, 16, 1, data);
|
||||
new AverageColorSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Core.Color(1f, 0f, 0f, 0f), result[0]);
|
||||
|
||||
info = new SamplerInfo<Core.Color>(16, 16, data);
|
||||
info = new SamplerInfo<Core.Color>(0, 0, 16, 16, 16, 1, data);
|
||||
new AverageColorSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Core.Color(1f, 0f, 0f, 0f), result[0]);
|
||||
}
|
||||
@ -48,11 +48,11 @@ public class AverageColorSamplerTest
|
||||
data[i] = (i % 2) == 0 ? new Core.Color(1f, 0f, 0f, 0f) : new Core.Color(1f, 1f, 1f, 1f);
|
||||
Core.Color[] result = new Core.Color[1];
|
||||
|
||||
SamplerInfo<Core.Color> info = new(2, 3, data[..6]);
|
||||
SamplerInfo<Core.Color> info = new(0, 0, 2, 3, 16, 1, data);
|
||||
new AverageColorSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Core.Color(1f, 0.5f, 0.5f, 0.5f), result[0]);
|
||||
|
||||
info = new SamplerInfo<Core.Color>(16, 16, data);
|
||||
info = new SamplerInfo<Core.Color>(0, 0, 16, 16, 16, 1, data);
|
||||
new AverageColorSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Core.Color(1f, 0.5f, 0.5f, 0.5f), result[0]);
|
||||
}
|
||||
@ -72,11 +72,11 @@ public class AverageColorSamplerTest
|
||||
};
|
||||
Core.Color[] result = new Core.Color[1];
|
||||
|
||||
SamplerInfo<Core.Color> info = new(2, 3, data[..6]);
|
||||
SamplerInfo<Core.Color> info = new(0, 0, 2, 3, 2, 1, data[..6]);
|
||||
new AverageColorSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Core.Color(0.5833333f, 0.5f, 0.291666657f, 0.25f), result[0]);
|
||||
|
||||
info = new SamplerInfo<Core.Color>(16, 16, data);
|
||||
info = new SamplerInfo<Core.Color>(0, 0, 16, 16, 16, 1, data);
|
||||
new AverageColorSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Core.Color(0.5019531f, 0.40234375f, 0.3486328f, 0.298828125f), result[0]);
|
||||
}
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using RGB.NET.Core.Tests.Helper;
|
||||
|
||||
@ -42,7 +41,6 @@ public class PixelTextureTest
|
||||
|
||||
Core.Color[] data = new Core.Color[SIZE * SIZE];
|
||||
SimplexNoise.Seed = 1872;
|
||||
Random random = new(1872);
|
||||
for (int y = 0; y < SIZE; y++)
|
||||
for (int x = 0; x < SIZE; x++)
|
||||
data[(y * SIZE) + x] = HSVColor.Create(SimplexNoise.CalcPixel2D(x, y, 1f / SIZE) * 360, 1, 1);
|
||||
|
||||
@ -27,11 +27,15 @@ public class AverageByteSamplerTest
|
||||
data[index++] = colorData[i].GetB();
|
||||
}
|
||||
|
||||
SamplerInfo<byte> info = new(2, 3, data[..(6 * 4)]);
|
||||
SamplerInfo<byte> info = new(0, 0, 2, 3, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<byte>(16, 16, data);
|
||||
info = new SamplerInfo<byte>(0, 0, 13, 13, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<byte>(0, 0, 16, 16, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
|
||||
}
|
||||
@ -53,11 +57,15 @@ public class AverageByteSamplerTest
|
||||
data[index++] = colorData[i].GetB();
|
||||
}
|
||||
|
||||
SamplerInfo<byte> info = new(2, 3, data[..(6 * 4)]);
|
||||
SamplerInfo<byte> info = new(0, 0, 2, 3, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<byte>(16, 16, data);
|
||||
info = new SamplerInfo<byte>(0, 0, 13, 13, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<byte>(0, 0, 16, 16, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
|
||||
}
|
||||
@ -80,11 +88,15 @@ public class AverageByteSamplerTest
|
||||
data[index++] = colorData[i].GetB();
|
||||
}
|
||||
|
||||
SamplerInfo<byte> info = new(2, 3, data[..(6 * 4)]);
|
||||
SamplerInfo<byte> info = new(0, 0, 2, 3, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0.5f, 0.5f, 0.5f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<byte>(16, 16, data);
|
||||
info = new SamplerInfo<byte>(0, 0, 13, 13, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, (6f / 13f).GetByteValueFromPercentage(), (6f / 13f).GetByteValueFromPercentage(), (6f / 13f).GetByteValueFromPercentage()), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<byte>(0, 0, 16, 16, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0.5f, 0.5f, 0.5f), new Color(result[0], result[1], result[2], result[3]));
|
||||
}
|
||||
@ -114,11 +126,11 @@ public class AverageByteSamplerTest
|
||||
data[index++] = colorData[i].GetB();
|
||||
}
|
||||
|
||||
SamplerInfo<byte> info = new(2, 3, data[..(6 * 4)]);
|
||||
SamplerInfo<byte> info = new(0, 0, 2, 3, 2, 4, data[..(6 * 4)]);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(149, 128, 74, 64), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<byte>(16, 16, data);
|
||||
info = new SamplerInfo<byte>(0, 0, 16, 16, 16, 4, data);
|
||||
new AverageByteSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(128, 103, 89, 76), new Color(result[0], result[1], result[2], result[3]));
|
||||
}
|
||||
|
||||
@ -27,11 +27,15 @@ public class AverageFloatSamplerTest
|
||||
data[index++] = colorData[i].B;
|
||||
}
|
||||
|
||||
SamplerInfo<float> info = new(2, 3, data[..(6 * 4)]);
|
||||
SamplerInfo<float> info = new(0, 0, 2, 3, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<float>(16, 16, data);
|
||||
info = new SamplerInfo<float>(0, 0, 13, 13, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<float>(0, 0, 16, 16, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 1f, 1f, 1f), new Color(result[0], result[1], result[2], result[3]));
|
||||
}
|
||||
@ -53,11 +57,15 @@ public class AverageFloatSamplerTest
|
||||
data[index++] = colorData[i].B;
|
||||
}
|
||||
|
||||
SamplerInfo<float> info = new(2, 3, data[..(6 * 4)]);
|
||||
SamplerInfo<float> info = new(0, 0, 2, 3, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<float>(16, 16, data);
|
||||
info = new SamplerInfo<float>(0, 0, 13, 13, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<float>(0, 0, 16, 16, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0f, 0f, 0f), new Color(result[0], result[1], result[2], result[3]));
|
||||
}
|
||||
@ -80,11 +88,15 @@ public class AverageFloatSamplerTest
|
||||
data[index++] = colorData[i].B;
|
||||
}
|
||||
|
||||
SamplerInfo<float> info = new(2, 3, data[..(6 * 4)]);
|
||||
SamplerInfo<float> info = new(0, 0, 2, 3, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0.5f, 0.5f, 0.5f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<float>(16, 16, data);
|
||||
info = new SamplerInfo<float>(0, 0, 13, 13, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 6f / 13f, 6f / 13f, 6f / 13f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<float>(0, 0, 16, 16, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(1f, 0.5f, 0.5f, 0.5f), new Color(result[0], result[1], result[2], result[3]));
|
||||
}
|
||||
@ -114,11 +126,11 @@ public class AverageFloatSamplerTest
|
||||
data[index++] = colorData[i].B;
|
||||
}
|
||||
|
||||
SamplerInfo<float> info = new(2, 3, data[..(6 * 4)]);
|
||||
SamplerInfo<float> info = new(0, 0, 2, 3, 2, 4, data[..(6 * 4)]);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(0.5833333f, 0.5f, 0.291666657f, 0.25f), new Color(result[0], result[1], result[2], result[3]));
|
||||
|
||||
info = new SamplerInfo<float>(16, 16, data);
|
||||
info = new SamplerInfo<float>(0, 0, 16, 16, 16, 4, data);
|
||||
new AverageFloatSampler().Sample(info, result);
|
||||
Assert.AreEqual(new Color(0.5019531f, 0.40234375f, 0.3486328f, 0.298828125f), new Color(result[0], result[1], result[2], result[3]));
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user