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

60 lines
1.9 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.Format;
using Mediapipe.Net.Framework.Port;
using Mediapipe.Net.Native;
namespace Mediapipe.Net.Framework.Packet
{
public unsafe class ImageFramePacket : Packet<ImageFrame>
{
public ImageFramePacket() : base() { }
public ImageFramePacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }
public ImageFramePacket(ImageFrame imageFrame) : base()
{
UnsafeNativeMethods.mp__MakeImageFramePacket__Pif(imageFrame.MpPtr, out var ptr).Assert();
imageFrame.Dispose(); // respect move semantics
Ptr = ptr;
}
public ImageFramePacket(ImageFrame imageFrame, Timestamp timestamp) : base()
{
UnsafeNativeMethods.mp__MakeImageFramePacket_At__Pif_Rt(imageFrame.MpPtr, timestamp.MpPtr, out var ptr).Assert();
GC.KeepAlive(timestamp);
imageFrame.Dispose(); // respect move semantics
Ptr = ptr;
}
public override ImageFrame Get()
{
UnsafeNativeMethods.mp_Packet__GetImageFrame(MpPtr, out var imageFramePtr).Assert();
GC.KeepAlive(this);
return new ImageFrame(imageFramePtr, false);
}
public override StatusOr<ImageFrame> Consume()
{
UnsafeNativeMethods.mp_Packet__ConsumeImageFrame(MpPtr, out var statusOrImageFramePtr).Assert();
GC.KeepAlive(this);
return new StatusOrImageFrame(statusOrImageFramePtr);
}
public override Status ValidateAsType()
{
UnsafeNativeMethods.mp_Packet__ValidateAsImageFrame(MpPtr, out var statusPtr).Assert();
GC.KeepAlive(this);
return new Status(statusPtr);
}
}
}