1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08:31 +00:00
RGB.NET/RGB.NET.Presets/Textures/Sampler/AverageByteSampler.cs
2021-02-25 00:30:09 +01:00

31 lines
890 B
C#

using System;
using RGB.NET.Core;
namespace RGB.NET.Presets.Textures.Sampler
{
public class AverageByteSampler : ISampler<byte>
{
#region Methods
public void SampleColor(in SamplerInfo<byte> info, in Span<byte> pixelData)
{
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];
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;
for (int i = 0; i < pixelData.Length; i++)
pixelData[i] = (sums[i] / divisor).GetByteValueFromPercentage();
}
#endregion
}
}