From 1ea2e2bee7deb1a9a8f6ac762b75c420554b8d3a Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 25 Feb 2021 19:58:40 +0100 Subject: [PATCH] Devices - Ported color correction code to RGB.NET --- .../Events/FrameRenderedEventArgs.cs | 2 +- .../Models/Surface/ArtemisDevice.cs | 7 +++- src/Artemis.Core/Models/Surface/ArtemisLed.cs | 11 +----- src/Artemis.Core/RGB.NET/SKTexture.cs | 37 ++++++++++++------- .../RGB.NET/ScaleColorCorrection.cs | 19 ++++++++++ .../Controls/DeviceVisualizerLed.cs | 9 ++--- 6 files changed, 55 insertions(+), 30 deletions(-) create mode 100644 src/Artemis.Core/RGB.NET/ScaleColorCorrection.cs diff --git a/src/Artemis.Core/Events/FrameRenderedEventArgs.cs b/src/Artemis.Core/Events/FrameRenderedEventArgs.cs index 110f7279c..65c0c18e9 100644 --- a/src/Artemis.Core/Events/FrameRenderedEventArgs.cs +++ b/src/Artemis.Core/Events/FrameRenderedEventArgs.cs @@ -15,7 +15,7 @@ namespace Artemis.Core } /// - /// Gets the bitmap brush used to render this frame + /// Gets the texture used to render this frame /// public SKTexture Texture { get; } diff --git a/src/Artemis.Core/Models/Surface/ArtemisDevice.cs b/src/Artemis.Core/Models/Surface/ArtemisDevice.cs index 7db6d2ff1..661130160 100644 --- a/src/Artemis.Core/Models/Surface/ArtemisDevice.cs +++ b/src/Artemis.Core/Models/Surface/ArtemisDevice.cs @@ -340,18 +340,20 @@ namespace Artemis.Core DeviceEntity.InputIdentifiers.Clear(); foreach (ArtemisDeviceInputIdentifier identifier in InputIdentifiers) + { DeviceEntity.InputIdentifiers.Add(new DeviceInputIdentifierEntity { InputProvider = identifier.InputProvider, Identifier = identifier.Identifier }); + } } internal void ApplyToRgbDevice() { RgbDevice.Rotation = DeviceEntity.Rotation; RgbDevice.Scale = DeviceEntity.Scale; - + // Workaround for device rotation not applying if (DeviceEntity.X == 0 && DeviceEntity.Y == 0) RgbDevice.Location = new Point(1, 1); @@ -361,6 +363,9 @@ namespace Artemis.Core foreach (DeviceInputIdentifierEntity identifierEntity in DeviceEntity.InputIdentifiers) InputIdentifiers.Add(new ArtemisDeviceInputIdentifier(identifierEntity.InputProvider, identifierEntity.Identifier)); + if (!RgbDevice.ColorCorrections.Any()) + RgbDevice.ColorCorrections.Add(new ScaleColorCorrection(this)); + CalculateRenderProperties(); OnDeviceUpdated(); } diff --git a/src/Artemis.Core/Models/Surface/ArtemisLed.cs b/src/Artemis.Core/Models/Surface/ArtemisLed.cs index eaf06b73a..473757f76 100644 --- a/src/Artemis.Core/Models/Surface/ArtemisLed.cs +++ b/src/Artemis.Core/Models/Surface/ArtemisLed.cs @@ -53,16 +53,7 @@ namespace Artemis.Core { return RgbLed.ToString(); } - - /// - /// Gets the color of this led, reverting the correction done to the parent device - /// - /// - public Color GetOriginalColor() - { - return RgbLed.Color.DivideRGB(Device.RedScale, Device.GreenScale, Device.BlueScale); - } - + internal void CalculateRectangles() { Rectangle = RgbLed.Boundary.ToSKRect(); diff --git a/src/Artemis.Core/RGB.NET/SKTexture.cs b/src/Artemis.Core/RGB.NET/SKTexture.cs index 04992f3e7..45d039203 100644 --- a/src/Artemis.Core/RGB.NET/SKTexture.cs +++ b/src/Artemis.Core/RGB.NET/SKTexture.cs @@ -5,23 +5,17 @@ using SkiaSharp; namespace Artemis.Core { + /// + /// Represents a SkiaSharp-based RGB.NET PixelTexture + /// public sealed class SKTexture : PixelTexture { - #region Properties & Fields - - private readonly SKBitmap _bitmap; - public SKBitmap Bitmap => _bitmap; - - protected override ReadOnlySpan Data => _bitmap.GetPixelSpan(); - - #endregion - #region Constructors - public SKTexture(SKBitmap bitmap) + internal SKTexture(SKBitmap bitmap) : base(bitmap.Width, bitmap.Height, 4, new AverageByteSampler()) { - this._bitmap = bitmap; + Bitmap = bitmap; } #endregion @@ -29,8 +23,25 @@ namespace Artemis.Core #region Methods /// - protected override Color GetColor(in ReadOnlySpan pixel) => new(pixel[0], pixel[1], pixel[2]); + protected override Color GetColor(in ReadOnlySpan pixel) + { + return new(pixel[0], pixel[1], pixel[2]); + } + + #endregion + + #region Properties & Fields + + /// + /// Gets the SKBitmap backing this texture + /// + public SKBitmap Bitmap { get; } + + /// + /// Gets the color data in RGB format + /// + protected override ReadOnlySpan Data => Bitmap.GetPixelSpan(); #endregion } -} +} \ No newline at end of file diff --git a/src/Artemis.Core/RGB.NET/ScaleColorCorrection.cs b/src/Artemis.Core/RGB.NET/ScaleColorCorrection.cs new file mode 100644 index 000000000..8cd7dd1f5 --- /dev/null +++ b/src/Artemis.Core/RGB.NET/ScaleColorCorrection.cs @@ -0,0 +1,19 @@ +using RGB.NET.Core; + +namespace Artemis.Core +{ + internal class ScaleColorCorrection : IColorCorrection + { + private readonly ArtemisDevice _device; + + public ScaleColorCorrection(ArtemisDevice device) + { + _device = device; + } + + public void ApplyTo(ref Color color) + { + color = color.MultiplyRGB(_device.RedScale, _device.GreenScale, _device.BlueScale); + } + } +} \ No newline at end of file diff --git a/src/Artemis.UI.Shared/Controls/DeviceVisualizerLed.cs b/src/Artemis.UI.Shared/Controls/DeviceVisualizerLed.cs index 498932b35..1e96549a9 100644 --- a/src/Artemis.UI.Shared/Controls/DeviceVisualizerLed.cs +++ b/src/Artemis.UI.Shared/Controls/DeviceVisualizerLed.cs @@ -27,7 +27,7 @@ namespace Artemis.UI.Shared if (Led.Layout?.Image != null && File.Exists(Led.Layout.Image.LocalPath)) LedImage = new BitmapImage(Led.Layout.Image); - + CreateLedGeometry(); } @@ -44,10 +44,9 @@ namespace Artemis.UI.Shared _renderColorBrush ??= new SolidColorBrush(); - RGB.NET.Core.Color originalColor = Led.GetOriginalColor(); - byte r = originalColor.GetR(); - byte g = originalColor.GetG(); - byte b = originalColor.GetB(); + byte r = Led.RgbLed.Color.GetR(); + byte g = Led.RgbLed.Color.GetG(); + byte b = Led.RgbLed.Color.GetB(); _renderColor.A = isDimmed ? 100 : 255; _renderColor.R = r;