Skip to content

Commit

Permalink
[Vision] Make P/Invokes have blittable signatures.
Browse files Browse the repository at this point in the history
Contributes towards xamarin#15684.
  • Loading branch information
rolfbjarne committed Dec 22, 2023
1 parent c1f25e6 commit 4a9987a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/Vision/VNUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ public static partial class VNUtils {
public static CGRect NormalizedIdentityRect { get; } = Dlfcn.GetCGRect (Libraries.Vision.Handle, "VNNormalizedIdentityRect");

[DllImport (Constants.VisionLibrary, EntryPoint = "VNNormalizedRectIsIdentityRect")]
[return: MarshalAs (UnmanagedType.U1)]
public static extern bool IsIdentityRect (CGRect rect);
static extern byte _IsIdentityRect (CGRect rect);

public static bool IsIdentityRect (CGRect rect)
{
return _IsIdentityRect (rect) != 0;
}

[DllImport (Constants.VisionLibrary, EntryPoint = "VNImagePointForNormalizedPoint")]
public static extern CGPoint GetImagePoint (CGPoint normalizedPoint, nuint imageWidth, nuint imageHeight);
Expand Down Expand Up @@ -120,25 +124,31 @@ public static partial class VNUtils {
public static extern CGRect GetNormalizedRect (CGRect imageRect, nuint imageWidth, nuint imageHeight, CGRect regionOfInterest);

[DllImport ("__Internal", EntryPoint = "xamarin_CGPoint__VNNormalizedFaceBoundingBoxPointForLandmarkPoint_Vector2_CGRect_nuint_nuint_string")]
static extern CGPoint VNNormalizedFaceBoundingBoxPointForLandmarkPoint (Vector2 faceLandmarkPoint, CGRect faceBoundingBox, nuint imageWidth, nuint imageHeight, out IntPtr error);
unsafe static extern CGPoint VNNormalizedFaceBoundingBoxPointForLandmarkPoint (Vector2 faceLandmarkPoint, CGRect faceBoundingBox, nuint imageWidth, nuint imageHeight, IntPtr* error);

public static CGPoint GetNormalizedFaceBoundingBoxPoint (Vector2 faceLandmarkPoint, CGRect faceBoundingBox, nuint imageWidth, nuint imageHeight)
{
IntPtr error;
var result = VNNormalizedFaceBoundingBoxPointForLandmarkPoint (faceLandmarkPoint, faceBoundingBox, imageWidth, imageHeight, out error);
CGPoint result;
unsafe {
result = VNNormalizedFaceBoundingBoxPointForLandmarkPoint (faceLandmarkPoint, faceBoundingBox, imageWidth, imageHeight, &error);
}
if (error != IntPtr.Zero)
throw new InvalidOperationException (Marshal.PtrToStringAuto (error));

return result;
}

[DllImport ("__Internal", EntryPoint = "xamarin_CGPoint__VNImagePointForFaceLandmarkPoint_Vector2_CGRect_nuint_nuint_string")]
static extern CGPoint VNImagePointForFaceLandmarkPoint (Vector2 faceLandmarkPoint, CGRect faceBoundingBox, nuint imageWidth, nuint imageHeight, out IntPtr error);
unsafe static extern CGPoint VNImagePointForFaceLandmarkPoint (Vector2 faceLandmarkPoint, CGRect faceBoundingBox, nuint imageWidth, nuint imageHeight, IntPtr* error);

public static CGPoint GetImagePoint (Vector2 faceLandmarkPoint, CGRect faceBoundingBox, nuint imageWidth, nuint imageHeight)
{
IntPtr error;
var result = VNImagePointForFaceLandmarkPoint (faceLandmarkPoint, faceBoundingBox, imageWidth, imageHeight, out error);
CGPoint result;
unsafe {
result = VNImagePointForFaceLandmarkPoint (faceLandmarkPoint, faceBoundingBox, imageWidth, imageHeight, &error);
}
if (error != IntPtr.Zero)
throw new InvalidOperationException (Marshal.PtrToStringAuto (error));

Expand Down
3 changes: 0 additions & 3 deletions tests/cecil-tests/BlittablePInvokes.KnownFailures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ public partial class BlittablePInvokes {
"CoreFoundation.CFMessagePortSendRequestStatus CoreFoundation.CFMessagePort::CFMessagePortSendRequest(System.IntPtr,System.Int32,System.IntPtr,System.Double,System.Double,System.IntPtr,System.IntPtr&)",
"CoreFoundation.CFRunLoopExitReason CoreFoundation.CFRunLoop::CFRunLoopRunInMode(System.IntPtr,System.Double,System.Boolean)",
"CoreGraphics.CGAffineTransform CoreGraphics.CGPDFPage::CGPDFPageGetDrawingTransform(System.IntPtr,CoreGraphics.CGPDFBox,CoreGraphics.CGRect,System.Int32,System.Boolean)",
"CoreGraphics.CGPoint Vision.VNUtils::VNImagePointForFaceLandmarkPoint(System.Numerics.Vector2,CoreGraphics.CGRect,System.UIntPtr,System.UIntPtr,System.IntPtr&)",
"CoreGraphics.CGPoint Vision.VNUtils::VNNormalizedFaceBoundingBoxPointForLandmarkPoint(System.Numerics.Vector2,CoreGraphics.CGRect,System.UIntPtr,System.UIntPtr,System.IntPtr&)",
"CoreGraphics.CGRect CoreMedia.CMFormatDescription::CMVideoFormatDescriptionGetCleanAperture(System.IntPtr,System.Boolean)",
"CoreGraphics.CGSize CoreMedia.CMFormatDescription::CMVideoFormatDescriptionGetPresentationDimensions(System.IntPtr,System.Boolean,System.Boolean)",
"CoreGraphics.CGSize CoreText.CTFramesetter::CTFramesetterSuggestFrameSizeWithConstraints(System.IntPtr,Foundation.NSRange,System.IntPtr,CoreGraphics.CGSize,Foundation.NSRange&)",
Expand Down Expand Up @@ -575,7 +573,6 @@ public partial class BlittablePInvokes {
"System.Boolean UIKit.UIFloatRange::UIFloatRangeIsInfinite(UIKit.UIFloatRange)",
"System.Boolean UIKit.UIVideo::UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(System.IntPtr)",
"System.Boolean VideoToolbox.VTDecompressionSession::VTIsHardwareDecodeSupported(CoreMedia.CMVideoCodecType)",
"System.Boolean Vision.VNUtils::IsIdentityRect(CoreGraphics.CGRect)",
"System.Byte Security.SecProtocolMetadata::sec_protocol_metadata_access_supported_signature_algorithms(System.IntPtr,ObjCRuntime.BlockLiteral*)",
"System.Byte* Network.NWEndpoint::nw_endpoint_get_signature(System.IntPtr,System.UIntPtr&)",
"System.Char CoreFoundation.CFString::CFStringGetCharacterAtIndex(System.IntPtr,System.IntPtr)",
Expand Down
24 changes: 24 additions & 0 deletions tests/monotouch-test/Vision/VNUtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
using Foundation;
using Vision;

#if NET
using Vector2 = global::System.Numerics.Vector2;
#else
using Vector2 = global::OpenTK.Vector2;
#endif

namespace MonoTouchFixtures.Vision {

[TestFixture]
Expand Down Expand Up @@ -54,6 +60,24 @@ public void GetNormalizedRectTest ()
var normalizedRect = VNUtils.GetImageRect (new CGRect (2, 34, 5, 67), 8, 90, new CGRect (123, 4, 56, 7));
Assert.That (normalizedRect, Is.Not.EqualTo (CGRect.Empty), "VNNormalizedRectForImageRectUsingRegionOfInterest is not empty");
}

[Test]
public void GetNormalizedFaceBoundingBoxPointTest ()
{
var normalizedPoint = VNUtils.GetNormalizedFaceBoundingBoxPoint (new Vector2 (9, 8), new CGRect (3, 14, 15, 92), 2, 11);
Assert.That (normalizedPoint, Is.Not.EqualTo (CGPoint.Empty), "VNNormalizedFaceBoundingBoxPointForLandmarkPoint is not empty");
}


[Test]
public void IsIdentityTest ()
{
Assert.True (VNUtils.IsIdentityRect (new CGRect (0, 0, 1, 1)), "Identity");
Assert.False (VNUtils.IsIdentityRect (new CGRect (0, 0, 2, 2)), "Not Identity A");
Assert.False (VNUtils.IsIdentityRect (new CGRect (1, 1, 1, 1)), "Not Identity B");
Assert.False (VNUtils.IsIdentityRect (new CGRect (1, 1, 0, 0)), "Not Identity C");
}

}
}
#endif

0 comments on commit 4a9987a

Please sign in to comment.