From 62497fa18886e992897f6b510b09959edbd94a1f Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Wed, 24 Feb 2021 13:05:50 +0100 Subject: [PATCH] Updated to use a simpler texture --- src/Artemis.Core/Artemis.Core.csproj | 3 + src/Artemis.Core/RGB.NET/ArtemisSampler.cs | 32 ---------- src/Artemis.Core/RGB.NET/SKTexture.cs | 72 ++-------------------- 3 files changed, 9 insertions(+), 98 deletions(-) delete mode 100644 src/Artemis.Core/RGB.NET/ArtemisSampler.cs diff --git a/src/Artemis.Core/Artemis.Core.csproj b/src/Artemis.Core/Artemis.Core.csproj index cb65d532a..bcbc79521 100644 --- a/src/Artemis.Core/Artemis.Core.csproj +++ b/src/Artemis.Core/Artemis.Core.csproj @@ -70,6 +70,9 @@ ..\..\..\RGB.NET\bin\net5.0\RGB.NET.Layout.dll + + ..\..\..\RGB.NET\bin\net5.0\RGB.NET.Presets.dll + diff --git a/src/Artemis.Core/RGB.NET/ArtemisSampler.cs b/src/Artemis.Core/RGB.NET/ArtemisSampler.cs deleted file mode 100644 index 307dcc252..000000000 --- a/src/Artemis.Core/RGB.NET/ArtemisSampler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using RGB.NET.Core; - -namespace Artemis.Core -{ - public class ArtemisSampler : ISampler - { - #region Methods - - /// - public Color SampleColor(SamplerInfo info) - { - int count = info.Width * info.Height; - if (count == 0) return Color.Transparent; - - ReadOnlySpan data = info.Data; - - uint r = 0, g = 0, b = 0; - for (int i = 0; i < data.Length; i += 4) - { - 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 - } -} diff --git a/src/Artemis.Core/RGB.NET/SKTexture.cs b/src/Artemis.Core/RGB.NET/SKTexture.cs index 3be5905a2..367faab76 100644 --- a/src/Artemis.Core/RGB.NET/SKTexture.cs +++ b/src/Artemis.Core/RGB.NET/SKTexture.cs @@ -1,95 +1,35 @@ using System; -using System.Buffers; using RGB.NET.Core; +using RGB.NET.Presets.Textures.Sampler; using SkiaSharp; namespace Artemis.Core { - public sealed class SKTexture : ITexture + public sealed class SKTexture : PixelTexture { - #region Constants - - private const int STACK_ALLOC_LIMIT = 1024; - - #endregion - #region Properties & Fields private readonly SKBitmap _bitmap; - private readonly int _stride; - public SKBitmap Bitmap => _bitmap; - public Size Size { get; } - - public ISampler Sampler { get; set; } = new ArtemisSampler(); - - public Color this[in Point point] - { - get - { - int x = (Size.Width * point.X.Clamp(0, 1)).RoundToInt(); - int y = (Size.Height * point.Y.Clamp(0, 1)).RoundToInt(); - return _bitmap.GetPixel(x, y).ToRgbColor(); - } - } - - public Color this[in Rectangle rectangle] - { - get - { - int x = (Size.Width * rectangle.Location.X.Clamp(0, 1)).RoundToInt(); - int y = (Size.Height * rectangle.Location.Y.Clamp(0, 1)).RoundToInt(); - int width = (Size.Width * rectangle.Size.Width.Clamp(0, 1)).RoundToInt(); - int height = (Size.Height * rectangle.Size.Height.Clamp(0, 1)).RoundToInt(); - - int bufferSize = width * height * 4; - if (bufferSize <= STACK_ALLOC_LIMIT) - { - Span buffer = stackalloc byte[bufferSize]; - GetRegionData(x, y, width, height, buffer); - return Sampler.SampleColor(new SamplerInfo(width, height, buffer)); - } - else - { - byte[] rent = ArrayPool.Shared.Rent(bufferSize); - Span buffer = new Span(rent).Slice(0, bufferSize); - GetRegionData(x, y, width, height, buffer); - Color color = Sampler.SampleColor(new SamplerInfo(width, height, buffer)); - ArrayPool.Shared.Return(rent); - - return color; - } - } - } + protected override ReadOnlySpan Data => _bitmap.GetPixelSpan(); #endregion #region Constructors public SKTexture(SKBitmap bitmap) + : base(bitmap.Width, bitmap.Height, 4, new AverageByteSampler()) { this._bitmap = bitmap; - - Size = new Size(bitmap.Width, bitmap.Height); - _stride = bitmap.Width; } #endregion #region Methods - private void GetRegionData(int x, int y, int width, int height, in Span buffer) - { - int width4 = width * 4; - ReadOnlySpan data = _bitmap.GetPixelSpan(); - for (int i = 0; i < height; i++) - { - ReadOnlySpan dataSlice = data.Slice((((y + i) * _stride) + x) * 4, width4); - Span destination = buffer.Slice(i * width4, width4); - dataSlice.CopyTo(destination); - } - } + /// + protected override Color GetColor(ReadOnlySpan pixel) => new(pixel[0], pixel[1], pixel[2]); #endregion }