1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Optimized Textures and Sampler

This commit is contained in:
Darth Affe 2021-02-23 01:38:53 +01:00
parent 34a2acc6c4
commit c9634f3913
11 changed files with 325 additions and 52 deletions

View File

@ -1,18 +1,28 @@
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
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
protected ISampler Sampler { get; set; }
private readonly int _dataPerColor;
protected ISampler<T> Sampler { get; set; }
protected T[] Data { get; set; }
public Size Size { get; }
public Color this[in Point point]
public virtual Color this[in Point point]
{
get
{
@ -24,7 +34,7 @@ namespace RGB.NET.Core
}
}
public Color this[in Rectangle rectangle]
public virtual Color this[in Rectangle rectangle]
{
get
{
@ -35,7 +45,23 @@ namespace RGB.NET.Core
int width = (int)Math.Round(Size.Width * rectangle.Size.Width.Clamp(0, 1));
int height = (int)Math.Round(Size.Height * rectangle.Size.Height.Clamp(0, 1));
return Sampler.SampleColor(x, y, width, height, GetColor);
int bufferSize = width * height * _dataPerColor;
if (bufferSize <= STACK_ALLOC_LIMIT)
{
Span<T> buffer = stackalloc T[bufferSize];
GetRegionData(x, y, width, height, buffer);
return Sampler.SampleColor(new SamplerInfo<T>(width, height, buffer));
}
else
{
T[] rent = ArrayPool<T>.Shared.Rent(bufferSize);
Span<T> buffer = new Span<T>(rent).Slice(0, bufferSize);
GetRegionData(x, y, width, height, buffer);
Color color = Sampler.SampleColor(new SamplerInfo<T>(width, height, buffer));
ArrayPool<T>.Shared.Return(rent);
return color;
}
}
}
@ -43,9 +69,10 @@ namespace RGB.NET.Core
#region Constructors
public PixelTexture(int with, int height, T[] data, ISampler sampler)
public PixelTexture(int with, int height, T[] data, int dataPerColor, ISampler<T> sampler)
{
this.Data = data;
this._dataPerColor = dataPerColor;
this.Sampler = sampler;
Size = new Size(with, height);
@ -55,8 +82,8 @@ namespace RGB.NET.Core
#region Methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected abstract Color GetColor(int x, int y);
protected abstract void GetRegionData(int x, int y, int width, int height, in Span<T> buffer);
#endregion
}
@ -72,11 +99,11 @@ namespace RGB.NET.Core
#region Constructors
public PixelTexture(int with, int height, Color[] data)
: this(with, height, data, new AverageSampler())
: this(with, height, data, new AverageColorSampler())
{ }
public PixelTexture(int with, int height, Color[] data, ISampler sampler)
: base(with, height, data, sampler)
public PixelTexture(int with, int height, Color[] data, ISampler<Color> sampler)
: base(with, height, data, 1, sampler)
{
this._stride = with;
@ -87,8 +114,20 @@ namespace RGB.NET.Core
#region Methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected override Color GetColor(int x, int y) => Data[(y * _stride) + x];
protected override void GetRegionData(int x, int y, int width, int height, in Span<Color> buffer)
{
Span<Color> data = Data.AsSpan();
for (int i = 0; i < height; i++)
{
Span<Color> dataSlice = data.Slice(((y + i) * _stride) + x, width);
Span<Color> destination = buffer.Slice(i * width, width);
dataSlice.CopyTo(destination);
}
}
#endregion
}
}

View File

@ -0,0 +1,34 @@
namespace RGB.NET.Core
{
public class AverageColorSampler : ISampler<Color>
{
#region Properties & Fields
public bool SampleAlpha { get; set; }
#endregion
#region Methods
public Color SampleColor(SamplerInfo<Color> info)
{
int count = info.Width * info.Height;
if (count == 0) return Color.Transparent;
float a = 0, r = 0, g = 0, b = 0;
foreach (Color color in info.Data)
{
a += color.A;
r += color.R;
g += color.G;
b += color.B;
}
return SampleAlpha
? new Color(a / count, r / count, g / count, b / count)
: new Color(r / count, g / count, b / count);
}
#endregion
}
}

View File

@ -1,38 +0,0 @@
namespace RGB.NET.Core
{
public class AverageSampler : ISampler
{
#region Properties & Fields
public bool SampleAlpha { get; set; }
#endregion
#region Methods
public Color SampleColor(int x, int y, int width, int height, GetColor getColor)
{
int maxX = x + width;
int maxY = y + height;
int count = width * height;
if (count == 0) return Color.Transparent;
float a = 0, r = 0, g = 0, b = 0;
for (int yPos = y; yPos < maxY; yPos++)
for (int xPos = x; xPos < maxX; xPos++)
{
Color color = getColor(x, y);
a += color.A;
r += color.R;
g += color.G;
b += color.B;
}
return SampleAlpha
? new Color(a / count, r / count, g / count, b / count)
: new Color(r / count, g / count, b / count);
}
#endregion
}
}

View File

@ -1,9 +1,7 @@
namespace RGB.NET.Core
{
public delegate Color GetColor(int x, int y);
public interface ISampler
public interface ISampler<T>
{
Color SampleColor(int x, int y, int width, int height, GetColor getColorFunc);
Color SampleColor(SamplerInfo<T> info);
}
}

View File

@ -0,0 +1,26 @@
using System;
namespace RGB.NET.Core
{
public readonly ref struct SamplerInfo<T>
{
#region Properties & Fields
public int Width { get; }
public int Height { get; }
public ReadOnlySpan<T> Data { get; }
#endregion
#region Constructors
public SamplerInfo(int width, int height, ReadOnlySpan<T> data)
{
this.Width = width;
this.Height = height;
this.Data = data;
}
#endregion
}
}

View File

@ -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>

View File

@ -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
}
}

View File

@ -0,0 +1,8 @@
namespace RGB.NET.Presets.Textures
{
public enum ColorFormat
{
RGB,
BGR
}
}

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -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
}
}