add possibility to get a reference to the raw data of PixelGrid

This commit is contained in:
Vinzenz Schroeter 2024-05-13 01:26:44 +02:00
parent 10e6138756
commit ea7061db7f
7 changed files with 75 additions and 13 deletions

View file

@ -192,6 +192,10 @@ namespace ServicePoint2.BindGen
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint sp2_pixel_grid_height(PixelGrid* @this);
/// <summary>Gets a reference to the data of the `PixelGrid` instance.</summary>
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_data_ref", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern byte* sp2_pixel_grid_data_ref(PixelGrid* @this);
}

View file

@ -1,3 +1,4 @@
using System.Text;
using ServicePoint2.BindGen;
namespace ServicePoint2;
@ -50,6 +51,36 @@ public sealed class ByteGrid : Sp2NativeInstance<BindGen.ByteGrid>
}
}
public string this[int y]
{
set
{
var width = Width;
ArgumentOutOfRangeException.ThrowIfGreaterThan(value.Length, width);
var x = 0;
for (; x < value.Length; x++)
this[x, y] = (byte)value[x];
for (; x < width; x++)
this[x, y] = 0;
}
get
{
var sb = new StringBuilder();
for (int x = 0; x < Width; x++)
{
var val = this[x, y];
if (val == 0)
break;
sb.Append((char)val);
}
return sb.ToString();
}
}
public void Fill(byte value)
{
unsafe

View file

@ -80,6 +80,18 @@ public sealed class PixelGrid : Sp2NativeInstance<BindGen.PixelGrid>
}
}
public Span<byte> Data
{
get
{
unsafe
{
var ptr = NativeMethods.sp2_pixel_grid_data_ref(Instance);
return new Span<byte>(ptr, Width * Height / 8);
}
}
}
private unsafe PixelGrid(BindGen.PixelGrid* instance) : base(instance)
{
}

View file

@ -7,4 +7,16 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup Condition="'$(Configuration)'=='Debug'">
<Content Include="..\..\target\debug\libservicepoint2.so" CopyToOutputDirectory="Always">
<Link>libservicepoint2.so</Link>
</Content>
</ItemGroup>
<ItemGroup Condition="'$(Configuration)'=='Release'">
<Content Include="..\..\target\release\libservicepoint2.so" CopyToOutputDirectory="Always">
<Link>libservicepoint2.so</Link>
</Content>
</ItemGroup>
</Project>