1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Updated to use a simpler texture

This commit is contained in:
Darth Affe 2021-02-24 13:05:50 +01:00
parent e58c0f20bb
commit 62497fa188
3 changed files with 9 additions and 98 deletions

View File

@ -70,6 +70,9 @@
<Reference Include="RGB.NET.Layout">
<HintPath>..\..\..\RGB.NET\bin\net5.0\RGB.NET.Layout.dll</HintPath>
</Reference>
<Reference Include="RGB.NET.Presets">
<HintPath>..\..\..\RGB.NET\bin\net5.0\RGB.NET.Presets.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Update="Resources\intro-profile.json">

View File

@ -1,32 +0,0 @@
using System;
using RGB.NET.Core;
namespace Artemis.Core
{
public class ArtemisSampler : ISampler<byte>
{
#region Methods
/// <inheritdoc />
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 += 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
}
}

View File

@ -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<byte>
{
#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<byte> 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<byte> buffer = stackalloc byte[bufferSize];
GetRegionData(x, y, width, height, buffer);
return Sampler.SampleColor(new SamplerInfo<byte>(width, height, buffer));
}
else
{
byte[] rent = ArrayPool<byte>.Shared.Rent(bufferSize);
Span<byte> buffer = new Span<byte>(rent).Slice(0, bufferSize);
GetRegionData(x, y, width, height, buffer);
Color color = Sampler.SampleColor(new SamplerInfo<byte>(width, height, buffer));
ArrayPool<byte>.Shared.Return(rent);
return color;
}
}
}
protected override ReadOnlySpan<byte> 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<byte> buffer)
{
int width4 = width * 4;
ReadOnlySpan<byte> data = _bitmap.GetPixelSpan();
for (int i = 0; i < height; i++)
{
ReadOnlySpan<byte> dataSlice = data.Slice((((y + i) * _stride) + x) * 4, width4);
Span<byte> destination = buffer.Slice(i * width4, width4);
dataSlice.CopyTo(destination);
}
}
/// <inheritdoc />
protected override Color GetColor(ReadOnlySpan<byte> pixel) => new(pixel[0], pixel[1], pixel[2]);
#endregion
}