CollabVMDesktopClient/CollabVMClient/ViewModels/CollabVMScreenViewModel.cs

52 lines
1.8 KiB
C#
Raw Normal View History

2023-08-29 08:52:35 -04:00
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using SixLabors.ImageSharp.PixelFormats;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using Avalonia.Media;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
namespace CollabVMClient.ViewModels
{
internal partial class CollabVMScreenViewModel : ViewModelBase
{
public WriteableBitmap Framebuffer { get; set; }
[ObservableProperty] private string id;
[ObservableProperty] private int width;
[ObservableProperty] private int height;
public string Title => $"CollabVM - Screen ({id})";
public CollabVMScreenViewModel(string id)
{
this.Id = id;
Framebuffer = new WriteableBitmap(new Avalonia.PixelSize(1, 1), new Avalonia.Vector(1, 1));
}
public void SetSize(int width, int height)
{
this.Width = width;
this.Height = height;
Framebuffer = new WriteableBitmap(new Avalonia.PixelSize(width, height), new Avalonia.Vector(96.0f, 96.0f), PixelFormat.Rgba8888);
}
public void LoadRect(byte[] rect, int width, int height, int x, int y)
{
// writing this method made me suicidal
var l = Framebuffer.Lock();
var byteswritten = 0;
for (var i = 0; i < height; i++)
{
Marshal.Copy(rect, byteswritten, IntPtr.Add(l.Address, (int)(4 * ((y + i) * Framebuffer.Size.Width + x))), width * 4);
byteswritten += width * 4;
}
l.Dispose();
}
}
}