using System; using System.Buffers; using System.Runtime.CompilerServices; namespace RGB.NET.Core; /// /// /// Represents a texture made of pixels (like a common image). /// /// The type of the pixels. public abstract class PixelTexture : ITexture where T : unmanaged { #region Constants private const int STACK_ALLOC_LIMIT = 1024; #endregion #region Properties & Fields private readonly int _dataPerPixel; private readonly int _stride; /// /// Gets or sets the sampler used to get the color of a region. /// public ISampler Sampler { get; set; } /// public Size Size { get; } /// /// Gets the underlying pixel data. /// protected abstract ReadOnlySpan Data { get; } /// public virtual Color this[in Point point] { get { if (Data.Length == 0) return Color.Transparent; int x = (int)MathF.Round((Size.Width - 1) * point.X.Clamp(0, 1)); int y = (int)MathF.Round((Size.Height - 1) * point.Y.Clamp(0, 1)); return GetColor(GetPixelData(x, y)); } } /// public virtual Color this[in Rectangle rectangle] { get { if (Data.Length == 0) return Color.Transparent; int x = (int)MathF.Round((Size.Width - 1) * rectangle.Location.X.Clamp(0, 1)); int y = (int)MathF.Round((Size.Height - 1) * rectangle.Location.Y.Clamp(0, 1)); int width = (int)MathF.Round(Size.Width * rectangle.Size.Width.Clamp(0, 1)); int height = (int)MathF.Round(Size.Height * rectangle.Size.Height.Clamp(0, 1)); if ((width == 0) && (rectangle.Size.Width > 0)) width = 1; if ((height == 0) && (rectangle.Size.Height > 0)) height = 1; return this[x, y, width, height]; } } /// /// Gets the sampled color inside the specified region. /// /// The x-location of the region. /// The y-location of the region. /// The with of the region. /// The height of the region. /// The sampled color. public virtual Color this[int x, int y, int width, int height] { get { if (Data.Length == 0) return Color.Transparent; if ((width == 0) || (height == 0)) return Color.Transparent; if ((width == 1) && (height == 1)) return GetColor(GetPixelData(x, y)); int bufferSize = width * height * _dataPerPixel; if (bufferSize <= STACK_ALLOC_LIMIT) { Span buffer = stackalloc T[bufferSize]; GetRegionData(x, y, width, height, buffer); Span pixelData = stackalloc T[_dataPerPixel]; Sampler.Sample(new SamplerInfo(width, height, buffer), pixelData); return GetColor(pixelData); } else { T[] rent = ArrayPool.Shared.Rent(bufferSize); Span buffer = new Span(rent)[..bufferSize]; GetRegionData(x, y, width, height, buffer); Span pixelData = stackalloc T[_dataPerPixel]; Sampler.Sample(new SamplerInfo(width, height, buffer), pixelData); ArrayPool.Shared.Return(rent); return GetColor(pixelData); } } } #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The width of the texture. /// The height of the texture. /// The amount of data-entries per pixel. /// The sampler used to get the color of a region. /// The stride of the data or -1 if the width should be used. public PixelTexture(int with, int height, int dataPerPixel, ISampler sampler, int stride = -1) { this._stride = stride == -1 ? with : stride; this._dataPerPixel = dataPerPixel; this.Sampler = sampler; Size = new Size(with, height); } #endregion #region Methods /// /// Converts the pixel-data to a color. /// /// The pixel-data to convert. /// The color represented by the specified pixel-data. protected abstract Color GetColor(in ReadOnlySpan pixel); /// /// Gets the pixel-data at the specified location. /// /// The x-location. /// The y-location. /// The pixel-data on the specified location. [MethodImpl(MethodImplOptions.AggressiveInlining)] protected virtual ReadOnlySpan GetPixelData(int x, int y) => Data.Slice((y * _stride) + x, _dataPerPixel); /// /// Writes the pixel-data of the specified region to the passed buffer. /// /// The x-location of the region to get the data for. /// The y-location of the region to get the data for. /// The width of the region to get the data for. /// The height of the region to get the data for. /// The buffer to write the data to. protected virtual void GetRegionData(int x, int y, int width, int height, in Span buffer) { int dataWidth = width * _dataPerPixel; ReadOnlySpan data = Data; for (int i = 0; i < height; i++) { ReadOnlySpan dataSlice = data.Slice((((y + i) * _stride) + x) * _dataPerPixel, dataWidth); Span destination = buffer.Slice(i * dataWidth, dataWidth); dataSlice.CopyTo(destination); } } #endregion } /// /// /// Represents a texture made of color-pixels. /// public sealed class PixelTexture : PixelTexture { #region Properties & Fields private readonly Color[] _data; /// protected override ReadOnlySpan Data => _data; #endregion #region Constructors /// /// Initializes a new instance of the class. /// A is used. /// /// The width of the texture. /// The height of the texture. /// The pixel-data of the texture. public PixelTexture(int with, int height, Color[] data) : this(with, height, data, new AverageColorSampler()) { } /// /// Initializes a new instance of the class. /// /// The width of the texture. /// The height of the texture. /// The pixel-data of the texture. /// The sampler used to get the color of a region. public PixelTexture(int with, int height, Color[] data, ISampler sampler) : base(with, height, 1, sampler) { this._data = data; if (Data.Length != (with * height)) throw new ArgumentException($"Data-Length {Data.Length} differs from the specified size {with}x{height} ({with * height})."); } #endregion #region Methods /// protected override Color GetColor(in ReadOnlySpan pixel) => pixel[0]; #endregion }