Simplified textures

This commit is contained in:
DarthAffe committed 2021-02-24 13:05:15 +01:00
1 parent 8ddf0bc734
commit cc7abb63f0
7 files changed
+85 -129

No files matched your search

+7 -32
View File
@@ -8,22 +8,11 @@ namespace RGB.NET.Presets.Textures
{
#region Properties & Fields
private readonly int _stride;
private readonly byte[] _data;
protected override ReadOnlySpan<byte> Data => _data;
public ColorFormat ColorFormat { get; }
public override Color this[in Rectangle rectangle]
{
get
{
Color color = base[rectangle];
if (ColorFormat == ColorFormat.BGR)
return new Color(color.A, color.B, color.G, color.R);
return color;
}
}
#endregion
#region Constructors
@@ -33,9 +22,9 @@ namespace RGB.NET.Presets.Textures
{ }
public BytePixelTexture(int with, int height, byte[] data, ISampler<byte> sampler, ColorFormat colorFormat = ColorFormat.RGB)
: base(with, height, data, 3, sampler)
: base(with, height, 3, sampler)
{
this._stride = with;
this._data = data;
this.ColorFormat = colorFormat;
if (Data.Length != ((with * height) * 3)) throw new ArgumentException($"Data-Length {Data.Length} differs from the given size {with}x{height} * 3 bytes ({with * height * 3}).");
@@ -45,26 +34,12 @@ namespace RGB.NET.Presets.Textures
#region Methods
protected override Color GetColor(int x, int y)
protected override Color GetColor(ReadOnlySpan<byte> pixel)
{
int offset = ((y * _stride) + x) * 3;
if (ColorFormat == ColorFormat.BGR)
return new Color(Data[offset + 2], Data[offset + 1], Data[offset + 1]);
return new Color(pixel[2], pixel[1], pixel[0]);
return new Color(Data[offset], Data[offset + 1], Data[offset + 2]);
}
protected override void GetRegionData(int x, int y, int width, int height, in Span<byte> buffer)
{
int width3 = width * 3;
ReadOnlySpan<byte> data = Data.AsSpan();
for (int i = 0; i < height; i++)
{
ReadOnlySpan<byte> dataSlice = data.Slice((((y + i) * _stride) + x) * 3, width3);
Span<byte> destination = buffer.Slice(i * width3, width3);
dataSlice.CopyTo(destination);
}
return new Color(pixel[0], pixel[1], pixel[2]);
}
#endregion
+7 -32
View File
@@ -8,22 +8,11 @@ namespace RGB.NET.Presets.Textures
{
#region Properties & Fields
private readonly int _stride;
private readonly float[] _data;
protected override ReadOnlySpan<float> Data => _data;
public ColorFormat ColorFormat { get; }
public override Color this[in Rectangle rectangle]
{
get
{
Color color = base[rectangle];
if (ColorFormat == ColorFormat.BGR)
return new Color(color.A, color.B, color.G, color.R);
return color;
}
}
#endregion
#region Constructors
@@ -33,9 +22,9 @@ namespace RGB.NET.Presets.Textures
{ }
public FloatPixelTexture(int with, int height, float[] data, ISampler<float> sampler, ColorFormat colorFormat = ColorFormat.RGB)
: base(with, height, data, 3, sampler)
: base(with, height, 3, sampler)
{
this._stride = with;
this._data = data;
this.ColorFormat = colorFormat;
if (Data.Length != ((with * height) * 3)) throw new ArgumentException($"Data-Length {Data.Length} differs from the given size {with}x{height} * 3 bytes ({with * height * 3}).");
@@ -45,26 +34,12 @@ namespace RGB.NET.Presets.Textures
#region Methods
protected override Color GetColor(int x, int y)
protected override Color GetColor(ReadOnlySpan<float> pixel)
{
int offset = ((y * _stride) + x) * 3;
if (ColorFormat == ColorFormat.BGR)
return new Color(Data[offset + 2], Data[offset + 1], Data[offset + 1]);
return new Color(pixel[2], pixel[1], pixel[0]);
return new Color(Data[offset], Data[offset + 1], Data[offset + 2]);
}
protected override void GetRegionData(int x, int y, int width, int height, in Span<float> buffer)
{
int width3 = width * 3;
ReadOnlySpan<float> data = Data.AsSpan();
for (int i = 0; i < height; i++)
{
ReadOnlySpan<float> dataSlice = data.Slice((((y + i) * _stride) + x) * 3, width3);
Span<float> destination = buffer.Slice(i * width3, width3);
dataSlice.CopyTo(destination);
}
return new Color(pixel[0], pixel[1], pixel[2]);
}
#endregion
@@ -7,23 +7,21 @@ namespace RGB.NET.Presets.Textures.Sampler
{
#region Methods
public Color SampleColor(SamplerInfo<byte> info)
public void SampleColor(in SamplerInfo<byte> info, Span<byte> pixelData)
{
int count = info.Width * info.Height;
if (count == 0) return Color.Transparent;
if (count == 0) return;
ReadOnlySpan<byte> data = info.Data;
uint r = 0, g = 0, b = 0;
for (int i = 0; i < data.Length; i += 3)
{
r += data[i];
g += data[i + 1];
b += data[i + 2];
}
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;
return new Color(r / divisor, g / divisor, b / divisor);
for (int i = 0; i < pixelData.Length; i++)
pixelData[i] = (byte)MathF.Round(sums[i] / (float)count);
}
#endregion
@@ -7,22 +7,21 @@ namespace RGB.NET.Presets.Textures.Sampler
{
#region Methods
public Color SampleColor(SamplerInfo<float> info)
public void SampleColor(in SamplerInfo<float> info, Span<float> pixelData)
{
int count = info.Width * info.Height;
if (count == 0) return Color.Transparent;
if (count == 0) return;
ReadOnlySpan<float> data = info.Data;
float r = 0, g = 0, b = 0;
for (int i = 0; i < data.Length; i += 3)
{
r += data[i];
g += data[i + 1];
b += data[i + 2];
}
int dataLength = pixelData.Length;
Span<float> sums = stackalloc float[dataLength];
for (int i = 0; i < data.Length; i += dataLength)
for (int j = 0; j < sums.Length; j++)
sums[j] += data[i + j];
return new Color(r / count, g / count, b / count);
for (int i = 0; i < pixelData.Length; i++)
pixelData[i] = sums[i] / count;
}
#endregion