From 105fdea2d799151a524fc217fda3f1d4e5b27c1c Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Thu, 25 Feb 2021 00:30:09 +0100 Subject: [PATCH] Small optimizations --- RGB.NET.Core/RGBSurface.cs | 4 ++-- RGB.NET.Core/Rendering/Textures/PixelTexture.cs | 6 +++--- .../Rendering/Textures/Sampler/AverageColorSampler.cs | 2 +- RGB.NET.Core/Rendering/Textures/Sampler/ISampler.cs | 2 +- RGB.NET.Presets/Textures/BytePixelTexture.cs | 2 +- RGB.NET.Presets/Textures/FloatPixelTexture.cs | 2 +- RGB.NET.Presets/Textures/Sampler/AverageByteSampler.cs | 5 +++-- RGB.NET.Presets/Textures/Sampler/AverageFloatSampler.cs | 2 +- 8 files changed, 13 insertions(+), 12 deletions(-) diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index 58208a3..a6c346f 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -228,8 +228,8 @@ namespace RGB.NET.Core foreach ((RenderTarget renderTarget, Color c) in render) { Color color = c; - foreach (IColorCorrection colorCorrection in renderTarget.Led.Device.ColorCorrections) - colorCorrection.ApplyTo(ref color); + for (int i = 0; i < renderTarget.Led.Device.ColorCorrections.Count; i++) + renderTarget.Led.Device.ColorCorrections[i].ApplyTo(ref color); renderTarget.Led.Color = color; } diff --git a/RGB.NET.Core/Rendering/Textures/PixelTexture.cs b/RGB.NET.Core/Rendering/Textures/PixelTexture.cs index f193ec2..43a7828 100644 --- a/RGB.NET.Core/Rendering/Textures/PixelTexture.cs +++ b/RGB.NET.Core/Rendering/Textures/PixelTexture.cs @@ -91,12 +91,12 @@ namespace RGB.NET.Core #region Methods - protected abstract Color GetColor(ReadOnlySpan pixel); + protected abstract Color GetColor(in ReadOnlySpan pixel); [MethodImpl(MethodImplOptions.AggressiveInlining)] protected virtual ReadOnlySpan GetPixelData(int x, int y) => Data.Slice((y * _stride) + x, _dataPerPixel); - protected virtual void GetRegionData(int x, int y, int width, int height, Span buffer) + protected virtual void GetRegionData(int x, int y, int width, int height, in Span buffer) { int dataWidth = width * _dataPerPixel; ReadOnlySpan data = Data; @@ -139,7 +139,7 @@ namespace RGB.NET.Core #region Methods - protected override Color GetColor(ReadOnlySpan pixel) => pixel[0]; + protected override Color GetColor(in ReadOnlySpan pixel) => pixel[0]; #endregion } diff --git a/RGB.NET.Core/Rendering/Textures/Sampler/AverageColorSampler.cs b/RGB.NET.Core/Rendering/Textures/Sampler/AverageColorSampler.cs index 11c3dd2..4910dbc 100644 --- a/RGB.NET.Core/Rendering/Textures/Sampler/AverageColorSampler.cs +++ b/RGB.NET.Core/Rendering/Textures/Sampler/AverageColorSampler.cs @@ -6,7 +6,7 @@ namespace RGB.NET.Core { #region Methods - public void SampleColor(in SamplerInfo info, Span pixelData) + public void SampleColor(in SamplerInfo info, in Span pixelData) { int count = info.Width * info.Height; if (count == 0) return; diff --git a/RGB.NET.Core/Rendering/Textures/Sampler/ISampler.cs b/RGB.NET.Core/Rendering/Textures/Sampler/ISampler.cs index 2b76c5f..ce507bd 100644 --- a/RGB.NET.Core/Rendering/Textures/Sampler/ISampler.cs +++ b/RGB.NET.Core/Rendering/Textures/Sampler/ISampler.cs @@ -4,6 +4,6 @@ namespace RGB.NET.Core { public interface ISampler { - void SampleColor(in SamplerInfo info, Span pixelData); + void SampleColor(in SamplerInfo info, in Span pixelData); } } diff --git a/RGB.NET.Presets/Textures/BytePixelTexture.cs b/RGB.NET.Presets/Textures/BytePixelTexture.cs index 1e993b6..ab008b7 100644 --- a/RGB.NET.Presets/Textures/BytePixelTexture.cs +++ b/RGB.NET.Presets/Textures/BytePixelTexture.cs @@ -34,7 +34,7 @@ namespace RGB.NET.Presets.Textures #region Methods - protected override Color GetColor(ReadOnlySpan pixel) + protected override Color GetColor(in ReadOnlySpan pixel) { if (ColorFormat == ColorFormat.BGR) return new Color(pixel[2], pixel[1], pixel[0]); diff --git a/RGB.NET.Presets/Textures/FloatPixelTexture.cs b/RGB.NET.Presets/Textures/FloatPixelTexture.cs index 936cdd9..686f05c 100644 --- a/RGB.NET.Presets/Textures/FloatPixelTexture.cs +++ b/RGB.NET.Presets/Textures/FloatPixelTexture.cs @@ -34,7 +34,7 @@ namespace RGB.NET.Presets.Textures #region Methods - protected override Color GetColor(ReadOnlySpan pixel) + protected override Color GetColor(in ReadOnlySpan pixel) { if (ColorFormat == ColorFormat.BGR) return new Color(pixel[2], pixel[1], pixel[0]); diff --git a/RGB.NET.Presets/Textures/Sampler/AverageByteSampler.cs b/RGB.NET.Presets/Textures/Sampler/AverageByteSampler.cs index 8c91242..b2f3770 100644 --- a/RGB.NET.Presets/Textures/Sampler/AverageByteSampler.cs +++ b/RGB.NET.Presets/Textures/Sampler/AverageByteSampler.cs @@ -7,7 +7,7 @@ namespace RGB.NET.Presets.Textures.Sampler { #region Methods - public void SampleColor(in SamplerInfo info, Span pixelData) + public void SampleColor(in SamplerInfo info, in Span pixelData) { int count = info.Width * info.Height; if (count == 0) return; @@ -20,8 +20,9 @@ namespace RGB.NET.Presets.Textures.Sampler for (int j = 0; j < sums.Length; j++) sums[j] += data[i + j]; + float divisor = count * byte.MaxValue; for (int i = 0; i < pixelData.Length; i++) - pixelData[i] = (byte)MathF.Round(sums[i] / (float)count); + pixelData[i] = (sums[i] / divisor).GetByteValueFromPercentage(); } #endregion diff --git a/RGB.NET.Presets/Textures/Sampler/AverageFloatSampler.cs b/RGB.NET.Presets/Textures/Sampler/AverageFloatSampler.cs index a9584a3..12b0368 100644 --- a/RGB.NET.Presets/Textures/Sampler/AverageFloatSampler.cs +++ b/RGB.NET.Presets/Textures/Sampler/AverageFloatSampler.cs @@ -7,7 +7,7 @@ namespace RGB.NET.Presets.Textures.Sampler { #region Methods - public void SampleColor(in SamplerInfo info, Span pixelData) + public void SampleColor(in SamplerInfo info, in Span pixelData) { int count = info.Width * info.Height; if (count == 0) return;