Files
Speykious 3371a2e745 Replace slow Marshal operations with unsafe (#13)
* Remove this bool marshal

* Literally remove all bool marshals

* Remove CharSet.Unicode warning

* Remove most FunctionPtr marshals

* Remove `[return: MarshalAs(...)]`

* Replace all `IntPtr`s with `void*`s

* Replace all `void*.Zero`s with `null`s

* Make native method classes unsafe

* U N S A F E

* Replace `PtrToStructure` with pointer arithmetic

* Run `dotnet format`

* Temp ugly Activator fix

* Remove `.ToPointer()`

* Use `byte` to return a `bool`

* Backwards compatibility with `Activator`

Yeah that sucks :(

* Replace `Marshal.Copy` by pointer arithmetic

* Replace `PtrToStringAnsi()` with `new string()`

* Remove unnecessary usings

* Explicitly type string pointers as `sbyte*`

* It actually doesn't hang?

* `MediaPipeException` -> `MediapipeException`

* Simplify `for` loops

* Remove unnecessary `float*` cast

* Some more explicit types for `ImageFrame`

* Remove unnecessary cast

* Looks like we forgor a `using` 💀

* Create `SafeArrayCopy` helper method

* Document `SafeArrayCopy`
2022-01-28 10:53:30 +01:00

73 lines
2.2 KiB
C#

// Copyright (c) homuler and Vignette
// This file is part of MediaPipe.NET.
// MediaPipe.NET is licensed under the MIT License. See LICENSE for details.
using System;
using Mediapipe.Net.Framework.Port;
using Mediapipe.Net.Native;
using Mediapipe.Net.Util;
namespace Mediapipe.Net.Framework.Packet
{
public unsafe class FloatArrayPacket : Packet<float[]>
{
private int length = -1;
public int Length
{
get => length;
set
{
if (length >= 0)
throw new InvalidOperationException("Length is already set and cannot be changed");
length = value;
}
}
public FloatArrayPacket() : base() { }
public FloatArrayPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }
public FloatArrayPacket(float[] value) : base()
{
UnsafeNativeMethods.mp__MakeFloatArrayPacket__Pf_i(value, value.Length, out var ptr).Assert();
Ptr = ptr;
Length = value.Length;
}
public FloatArrayPacket(float[] value, Timestamp timestamp) : base()
{
UnsafeNativeMethods.mp__MakeFloatArrayPacket_At__Pf_i_Rt(value, value.Length, timestamp.MpPtr, out var ptr).Assert();
GC.KeepAlive(timestamp);
Ptr = ptr;
Length = value.Length;
}
public override float[] Get()
{
if (Length < 0)
throw new InvalidOperationException("The array's length is unknown, set Length first");
return UnsafeUtil.SafeArrayCopy(GetArrayPtr(), Length);
}
public float* GetArrayPtr()
{
UnsafeNativeMethods.mp_Packet__GetFloatArray(MpPtr, out float* array).Assert();
GC.KeepAlive(this);
return array;
}
public override StatusOr<float[]> Consume() => throw new NotSupportedException();
public override Status ValidateAsType()
{
UnsafeNativeMethods.mp_Packet__ValidateAsFloatArray(MpPtr, out var statusPtr).Assert();
GC.KeepAlive(this);
return new Status(statusPtr);
}
}
}