Optimized Textures and Sampler

This commit is contained in:
DarthAffe committed 2021-02-23 01:38:53 +01:00
1 parent 34a2acc6c4
commit c9634f3913
11 files changed
+325 -52

No files matched your search

@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=brushes/@EntryIndexedValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=decorators/@EntryIndexedValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=groups_005Cextensions/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=groups_005Cextensions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=textures_005Cenums/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
@@ -0,0 +1,72 @@
using System;
using RGB.NET.Core;
using RGB.NET.Presets.Textures.Sampler;
namespace RGB.NET.Presets.Textures
{
public sealed class BytePixelTexture : PixelTexture<byte>
{
#region Properties & Fields
private readonly int _stride;
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
public BytePixelTexture(int with, int height, byte[] data, ColorFormat colorFormat = ColorFormat.RGB)
: this(with, height, data, new AverageByteSampler(), colorFormat)
{ }
public BytePixelTexture(int with, int height, byte[] data, ISampler<byte> sampler, ColorFormat colorFormat = ColorFormat.RGB)
: base(with, height, data, 3, sampler)
{
this._stride = with;
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}).");
}
#endregion
#region Methods
protected override Color GetColor(int x, int y)
{
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(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;
Span<byte> data = Data.AsSpan();
for (int i = 0; i < height; i++)
{
Span<byte> dataSlice = data.Slice((((y + i) * _stride) + x) * 3, width3);
Span<byte> destination = buffer.Slice(i * width3, width3);
dataSlice.CopyTo(destination);
}
}
#endregion
}
}
@@ -0,0 +1,8 @@
namespace RGB.NET.Presets.Textures
{
public enum ColorFormat
{
RGB,
BGR
}
}
@@ -0,0 +1,72 @@
using System;
using RGB.NET.Core;
using RGB.NET.Presets.Textures.Sampler;
namespace RGB.NET.Presets.Textures
{
public sealed class FloatPixelTexture : PixelTexture<float>
{
#region Properties & Fields
private readonly int _stride;
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
public FloatPixelTexture(int with, int height, float[] data, ColorFormat colorFormat = ColorFormat.RGB)
: this(with, height, data, new AverageFloatSampler(), colorFormat)
{ }
public FloatPixelTexture(int with, int height, float[] data, ISampler<float> sampler, ColorFormat colorFormat = ColorFormat.RGB)
: base(with, height, data, 3, sampler)
{
this._stride = with;
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}).");
}
#endregion
#region Methods
protected override Color GetColor(int x, int y)
{
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(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;
Span<float> data = Data.AsSpan();
for (int i = 0; i < height; i++)
{
Span<float> dataSlice = data.Slice((((y + i) * _stride) + x) * 3, width3);
Span<float> destination = buffer.Slice(i * width3, width3);
dataSlice.CopyTo(destination);
}
}
#endregion
}
}
@@ -0,0 +1,31 @@
using System;
using RGB.NET.Core;
namespace RGB.NET.Presets.Textures.Sampler
{
public class AverageByteSampler : ISampler<byte>
{
#region Methods
public Color SampleColor(SamplerInfo<byte> info)
{
int count = info.Width * info.Height;
if (count == 0) return Color.Transparent;
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];
}
float divisor = count * byte.MaxValue;
return new Color(r / divisor, g / divisor, b / divisor);
}
#endregion
}
}
@@ -0,0 +1,30 @@
using System;
using RGB.NET.Core;
namespace RGB.NET.Presets.Textures.Sampler
{
public class AverageFloatSampler : ISampler<float>
{
#region Methods
public Color SampleColor(SamplerInfo<float> info)
{
int count = info.Width * info.Height;
if (count == 0) return Color.Transparent;
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];
}
return new Color(r / count, g / count, b / count);
}
#endregion
}
}