This commit is contained in:
Robert committed 2019-12-02 17:37:33 +01:00
1 parent f8037feb81
commit 0e3afef6ca
16 files changed
+172 -166

No files matched your search

-57
View File
@@ -1,57 +0,0 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace Artemis.Core.RGB.NET
{
public class DirectBitmap : IDisposable
{
public DirectBitmap(int width, int height)
{
Width = width;
Height = height;
Bits = new int[width * height];
BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
}
public Bitmap Bitmap { get; }
public int[] Bits { get; }
public bool Disposed { get; private set; }
public int Height { get; }
public int Width { get; }
protected GCHandle BitsHandle { get; }
public void Dispose()
{
if (Disposed) return;
Disposed = true;
Bitmap.Dispose();
BitsHandle.Free();
}
public void SetPixel(int x, int y, Color colour)
{
var index = x + y * Width;
var col = colour.ToArgb();
Bits[index] = col;
}
public Color GetPixel(int x, int y)
{
var index = x + y * Width;
if (index >= 0 && index - 1 <= Bits.Length)
{
var col = Bits[index];
var result = Color.FromArgb(col);
return result;
}
return Color.Black;
}
}
}
+20 -35
View File
@@ -1,16 +1,13 @@
using System;
using System.Drawing;
using System.Linq;
using RGB.NET.Core;
using Color = RGB.NET.Core.Color;
using Rectangle = RGB.NET.Core.Rectangle;
using SkiaSharp;
namespace Artemis.Core.RGB.NET
{
public class GraphicsDecorator : AbstractDecorator, IBrushDecorator, IDisposable
{
private readonly double _scale;
private DirectBitmap _bitmap;
public GraphicsDecorator(ILedGroup ledGroup, double scale)
{
@@ -18,27 +15,27 @@ namespace Artemis.Core.RGB.NET
var leds = ledGroup.GetLeds().ToList();
if (!leds.Any())
_bitmap = null;
else
{
var width = Math.Min(leds.Max(l => l.AbsoluteLedRectangle.Location.X + l.AbsoluteLedRectangle.Size.Width) * scale, 4096);
var height = Math.Min(leds.Max(l => l.AbsoluteLedRectangle.Location.Y + l.AbsoluteLedRectangle.Size.Height) * scale, 4096);
return;
_bitmap = new DirectBitmap((int) width, (int) height);
}
var width = Math.Min(leds.Max(l => l.AbsoluteLedRectangle.Location.X + l.AbsoluteLedRectangle.Size.Width) * scale, 4096);
var height = Math.Min(leds.Max(l => l.AbsoluteLedRectangle.Location.Y + l.AbsoluteLedRectangle.Size.Height) * scale, 4096);
Bitmap = new SKBitmap(new SKImageInfo(RoundToInt(width), RoundToInt(height)));
Canvas = new SKCanvas(Bitmap);
}
public SKBitmap Bitmap { get; private set; }
public SKCanvas Canvas { get; private set; }
public Color ManipulateColor(Rectangle rectangle, BrushRenderTarget renderTarget, Color color)
{
if (Bitmap == null)
return new Color(0, 0, 0);
var x = renderTarget.Led.AbsoluteLedRectangle.Center.X * _scale;
var y = renderTarget.Led.AbsoluteLedRectangle.Center.Y * _scale;
if (_bitmap != null && _bitmap.Width - 1 >= x && _bitmap.Height - 1 >= y)
{
var pixel = _bitmap.GetPixel((int) x, (int) y);
return new Color(pixel.A, pixel.R, pixel.G, pixel.B);
}
return new Color(0, 0, 0);
var pixel = Bitmap.GetPixel(RoundToInt(x), RoundToInt(y));
return new Color(pixel.Alpha, pixel.Red, pixel.Green, pixel.Blue);
}
public override void OnDetached(IDecoratable decoratable)
@@ -48,27 +45,15 @@ namespace Artemis.Core.RGB.NET
public void Dispose()
{
_bitmap?.Dispose();
_bitmap = null;
Bitmap?.Dispose();
Canvas?.Dispose();
Bitmap = null;
Canvas = null;
}
public Graphics GetGraphics()
private int RoundToInt(double number)
{
try
{
return _bitmap == null ? null : Graphics.FromImage(_bitmap.Bitmap);
}
catch (AccessViolationException)
{
// ignored
}
return null;
}
public Bitmap GetBitmap()
{
return _bitmap?.Bitmap;
return (int) Math.Round(number, MidpointRounding.AwayFromZero);
}
}
}