From 0884e2d57132ab913afcb9dffa7d6d5bf00f6541 Mon Sep 17 00:00:00 2001 From: "Andrew B." Date: Tue, 12 Mar 2024 10:13:23 +0300 Subject: [PATCH] issue#48 Additional notes in comments and slightly better behaviour for image creation in case of OrbbecSDK K4A Wrapper. --- K4AdotNet/Sensor/Image.cs | 44 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/K4AdotNet/Sensor/Image.cs b/K4AdotNet/Sensor/Image.cs index 62360f7..0a62df3 100644 --- a/K4AdotNet/Sensor/Image.cs +++ b/K4AdotNet/Sensor/Image.cs @@ -50,8 +50,7 @@ public Image(ImageFormat format, int widthPixels, int heightPixels) /// Height of image in pixels. Must be positive. /// Image stride in bytes (the number of bytes per horizontal line of the image). Must be positive. /// - /// Don't use this method to create image with unknown/unspecified stride (like and ). - /// For such formats, size of image in bytes must be specified to create image: . + /// This version of image construction should be preferred in case of OrbbecSDK-K4A-Wrapper usage. /// /// /// or is equal to or less than zero @@ -87,6 +86,10 @@ public Image(ImageFormat format, int widthPixels, int heightPixels, int strideBy /// Height of image in pixels. Must be positive. /// Image stride in bytes (the number of bytes per horizontal line of the image). Must be non-negative. Zero value can be used for and . /// Size of image buffer in size. Non negative. Cannot be less than size calculated from image parameters. + /// + /// This version of image construction allocates memory buffer via + /// or if not set. + /// /// /// or is equal to or less than zero /// or is less than zero or is too small for specified @@ -107,6 +110,23 @@ public Image(ImageFormat format, int widthPixels, int heightPixels, int strideBy if (strideBytes > 0 && sizeBytes < format.ImageSizeBytes(strideBytes, heightPixels)) throw new ArgumentOutOfRangeException(nameof(sizeBytes)); +#if ORBBECSDK_K4A_WRAPPER + // OrbbecSDK K4A Wrapper has limited support of image creation from provided memory buffer. + // For this reason, trying to use "standard" image creation, if possible. + if (strideBytes == 0 && sizeBytes % heightPixels == 0) + strideBytes = sizeBytes / heightPixels; + if (strideBytes > 0 && strideBytes * heightPixels == sizeBytes) + { + var result = NativeApi.ImageCreate(format, widthPixels, heightPixels, strideBytes, out var imageHandle); + if (result == NativeCallResults.Result.Succeeded && !imageHandle.IsValid) + { + this.handle = imageHandle; + this.handle.Disposed += Handle_Disposed; + return; + } + } +#endif + // Gets current memory allocator Sdk.GetCustomMemoryAllocator(out var memoryAllocator, out var memoryDestroyCallback); // If not set, use HGlobal @@ -144,6 +164,8 @@ public Image(ImageFormat format, int widthPixels, int heightPixels, int strideBy /// For other formats use . /// /// points to pinned array for such images. + /// + /// OrbbecSDK-K4A-Wrapper has limited support of this functionality. /// /// /// or is equal to or less than zero @@ -166,6 +188,8 @@ public static Image CreateFromArray(T[] buffer, ImageFormat format, int width /// Image stride in bytes (the number of bytes per horizontal line of the image). Must be non-negative. Zero value can be used for and . /// Created image. Not . /// /// points to pinned array for such images. + /// + /// OrbbecSDK-K4A-Wrapper has limited support of this functionality. /// /// /// or is equal to or less than zero @@ -197,7 +221,14 @@ public static Image CreateFromArray(T[] buffer, ImageFormat format, int width pinnedArrayReleaseCallback, (IntPtr)bufferPin, out var handle); if (res != NativeCallResults.Result.Succeeded || !handle.IsValid) + { + bufferPin.Free(); +#if ORBBECSDK_K4A_WRAPPER + throw new NotSupportedException("OrbbecSDK-K4A-Wrapper has limited support of this functionality. Please, prefer image creation via constructors."); +#else throw new ArgumentException($"Cannot create image with format {format}, size {widthPixels}x{heightPixels} pixels, stride {strideBytes} bytes from buffer of size {sizeBytes} bytes."); +#endif + } return Create(handle)!; } @@ -273,14 +304,21 @@ public static unsafe Image CreateFromMemory(System.Buffers.IMemoryOwner me pinnedMemoryReleaseCallback, PinnedMemoryContext.Create(memoryOwner, memoryPin), out var handle); if (res != NativeCallResults.Result.Succeeded || !handle.IsValid) + { + memoryPin.Dispose(); +#if ORBBECSDK_K4A_WRAPPER + throw new NotSupportedException("OrbbecSDK-K4A-Wrapper has limited support of this functionality. Please, prefer image creation via constructors."); +#else throw new ArgumentException($"Cannot create image with format {format}, size {widthPixels}x{heightPixels} pixels, stride {strideBytes} bytes from memory of size {sizeBytes} bytes."); +#endif + } return Create(handle)!; } #endif - private void Handle_Disposed(object? sender, EventArgs e) + private void Handle_Disposed(object? sender, EventArgs e) { handle.Disposed -= Handle_Disposed; Disposed?.Invoke(this, EventArgs.Empty);