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

@ -12,16 +12,4 @@
<ProjectReference Include="..\..\servicepoint2-binding-cs\ServicePoint2\ServicePoint2.csproj"/> <ProjectReference Include="..\..\servicepoint2-binding-cs\ServicePoint2\ServicePoint2.csproj"/>
</ItemGroup> </ItemGroup>
<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> </Project>

View file

@ -192,6 +192,10 @@ namespace ServicePoint2.BindGen
[DllImport(__DllName, EntryPoint = "sp2_pixel_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] [DllImport(__DllName, EntryPoint = "sp2_pixel_grid_height", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
public static extern nuint sp2_pixel_grid_height(PixelGrid* @this); 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; using ServicePoint2.BindGen;
namespace ServicePoint2; 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) public void Fill(byte value)
{ {
unsafe 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) private unsafe PixelGrid(BindGen.PixelGrid* instance) : base(instance)
{ {
} }

View file

@ -7,4 +7,16 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </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> </Project>

View file

@ -75,6 +75,10 @@ impl BitVec {
self.data.len() * 8 self.data.len() * 8
} }
pub fn data_ref(&self) -> &[u8] {
&*self.data
}
fn get_indexes(&self, index: usize) -> (usize, u8) { fn get_indexes(&self, index: usize) -> (usize, u8) {
let byte_index = index / 8; let byte_index = index / 8;
let bit_in_byte_index = 7 - index % 8; let bit_in_byte_index = 7 - index % 8;
@ -160,7 +164,7 @@ pub mod c_api {
/// Gets the length of the `BitVec` in bits. /// Gets the length of the `BitVec` in bits.
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn sp2_bit_vec_len(this: *const BitVec) -> usize{ pub unsafe extern "C" fn sp2_bit_vec_len(this: *const BitVec) -> usize {
(*this).len() (*this).len()
} }
} }

View file

@ -76,6 +76,10 @@ impl PixelGrid {
pub fn fill(&mut self, value: bool) { pub fn fill(&mut self, value: bool) {
self.bit_vec.fill(value); self.bit_vec.fill(value);
} }
pub fn data_ref(&self) -> &[u8] {
self.bit_vec.data_ref()
}
} }
impl Into<Vec<u8>> for PixelGrid { impl Into<Vec<u8>> for PixelGrid {
@ -148,4 +152,11 @@ pub mod c_api
pub unsafe extern "C" fn sp2_pixel_grid_height(this: *const PixelGrid) -> usize { pub unsafe extern "C" fn sp2_pixel_grid_height(this: *const PixelGrid) -> usize {
(*this).height (*this).height
} }
/// Gets a reference to the data of the `PixelGrid` instance.
#[no_mangle]
pub unsafe extern "C" fn sp2_pixel_grid_data_ref(this: *const PixelGrid) -> *const u8 {
// TODO: also return length
(*this).data_ref().as_ptr_range().start
}
} }