-
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
996 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Formats/Image.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Copyright (c) 2023 homuler | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
using System; | ||
using Unity.Collections; | ||
using Unity.Collections.LowLevel.Unsafe; | ||
|
||
namespace Mediapipe | ||
{ | ||
public class Image : MpResourceHandle | ||
{ | ||
public Image(IntPtr imagePtr, bool isOwner = true) : base(imagePtr, isOwner) { } | ||
|
||
public Image(ImageFormat.Types.Format format, int width, int height, int widthStep, IntPtr pixelData, ImageFrame.Deleter deleter) : base() | ||
{ | ||
UnsafeNativeMethods.mp_Image__ui_i_i_i_Pui8_PF(format, width, height, widthStep, pixelData, deleter, out var ptr).Assert(); | ||
this.ptr = ptr; | ||
} | ||
|
||
public unsafe Image(ImageFormat.Types.Format format, int width, int height, int widthStep, NativeArray<byte> pixelData, ImageFrame.Deleter deleter) | ||
: this(format, width, height, widthStep, (IntPtr)NativeArrayUnsafeUtility.GetUnsafeReadOnlyPtr(pixelData), deleter) | ||
{ } | ||
|
||
/// <summary> | ||
/// Initialize an <see cref="Image" />. | ||
/// </summary> | ||
/// <remarks> | ||
/// <paramref name="pixelData" /> won't be released if the instance is disposed of.<br /> | ||
/// It's useful when: | ||
/// <list type="bullet"> | ||
/// <item> | ||
/// <description>You can reuse the memory allocated to <paramref name="pixelData" />.</description> | ||
/// </item> | ||
/// <item> | ||
/// <description>You've not allocated the memory (e.g. <see cref="Texture2D.GetRawTextureData" />).</description> | ||
/// </item> | ||
/// </list> | ||
/// </remarks> | ||
public Image(ImageFormat.Types.Format format, int width, int height, int widthStep, NativeArray<byte> pixelData) | ||
: this(format, width, height, widthStep, pixelData, ImageFrame.VoidDeleter) | ||
{ } | ||
|
||
#if UNITY_EDITOR_LINUX || UNITY_STANDLONE_LINUX || UNITY_ANDROID | ||
public Image(uint target, uint name, int width, int height, GpuBufferFormat format, GlTextureBuffer.DeletionCallback callback, GlContext glContext) : base() | ||
{ | ||
UnsafeNativeMethods.mp_Image__ui_ui_i_i_ui_PF_PSgc(target, name, width, height, format, callback, glContext.sharedPtr, out var ptr).Assert(); | ||
this.ptr = ptr; | ||
} | ||
|
||
public Image(uint name, int width, int height, GpuBufferFormat format, GlTextureBuffer.DeletionCallback callback, GlContext glContext) : | ||
this(Gl.GL_TEXTURE_2D, name, width, height, format, callback, glContext) | ||
{ } | ||
#endif | ||
|
||
protected override void DeleteMpPtr() | ||
{ | ||
UnsafeNativeMethods.mp_Image__delete(ptr); | ||
} | ||
|
||
public int Width() | ||
{ | ||
var ret = SafeNativeMethods.mp_Image__width(mpPtr); | ||
|
||
GC.KeepAlive(this); | ||
return ret; | ||
} | ||
|
||
public int Height() | ||
{ | ||
var ret = SafeNativeMethods.mp_Image__height(mpPtr); | ||
|
||
GC.KeepAlive(this); | ||
return ret; | ||
} | ||
|
||
public int Channels() | ||
{ | ||
var ret = SafeNativeMethods.mp_Image__channels(mpPtr); | ||
|
||
GC.KeepAlive(this); | ||
return ret; | ||
} | ||
|
||
public int Step() | ||
{ | ||
var ret = SafeNativeMethods.mp_Image__step(mpPtr); | ||
|
||
GC.KeepAlive(this); | ||
return ret; | ||
} | ||
|
||
public bool UsesGpu() | ||
{ | ||
var ret = SafeNativeMethods.mp_Image__UsesGpu(mpPtr); | ||
|
||
GC.KeepAlive(this); | ||
return ret; | ||
} | ||
|
||
public ImageFormat.Types.Format ImageFormat() | ||
{ | ||
var ret = SafeNativeMethods.mp_Image__image_format(mpPtr); | ||
|
||
GC.KeepAlive(this); | ||
return ret; | ||
} | ||
|
||
public GpuBufferFormat Format() | ||
{ | ||
var ret = SafeNativeMethods.mp_Image__format(mpPtr); | ||
|
||
GC.KeepAlive(this); | ||
return ret; | ||
} | ||
|
||
public bool ConvertToCpu() | ||
{ | ||
UnsafeNativeMethods.mp_Image__ConvertToCpu(mpPtr, out var result).Assert(); | ||
|
||
GC.KeepAlive(this); | ||
return result; | ||
} | ||
|
||
public bool ConvertToGpu() | ||
{ | ||
UnsafeNativeMethods.mp_Image__ConvertToGpu(mpPtr, out var result).Assert(); | ||
|
||
GC.KeepAlive(this); | ||
return result; | ||
} | ||
} | ||
|
||
public class PixelWriteLock : MpResourceHandle | ||
{ | ||
public PixelWriteLock(Image image) : base() | ||
{ | ||
UnsafeNativeMethods.mp_PixelWriteLock__RI(image.mpPtr, out var ptr).Assert(); | ||
this.ptr = ptr; | ||
} | ||
|
||
protected override void DeleteMpPtr() | ||
{ | ||
UnsafeNativeMethods.mp_PixelWriteLock__delete(ptr); | ||
} | ||
|
||
public IntPtr Pixels() | ||
{ | ||
return SafeNativeMethods.mp_PixelWriteLock__Pixels(mpPtr); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Formats/Image.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/ImagePacket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) 2023 homuler | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
using System; | ||
|
||
namespace Mediapipe | ||
{ | ||
public class ImagePacket : Packet<Image> | ||
{ | ||
/// <summary> | ||
/// Creates an empty <see cref="ImagePacket" /> instance. | ||
/// </summary> | ||
public ImagePacket() : base(true) { } | ||
|
||
[UnityEngine.Scripting.Preserve] | ||
public ImagePacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { } | ||
|
||
public ImagePacket(Image image) : base() | ||
{ | ||
UnsafeNativeMethods.mp__MakeImagePacket__Pif(image.mpPtr, out var ptr).Assert(); | ||
image.Dispose(); // respect move semantics | ||
|
||
this.ptr = ptr; | ||
} | ||
|
||
public ImagePacket(Image image, Timestamp timestamp) : base() | ||
{ | ||
UnsafeNativeMethods.mp__MakeImagePacket_At__Pif_Rt(image.mpPtr, timestamp.mpPtr, out var ptr).Assert(); | ||
GC.KeepAlive(timestamp); | ||
image.Dispose(); // respect move semantics | ||
|
||
this.ptr = ptr; | ||
} | ||
|
||
public ImagePacket At(Timestamp timestamp) | ||
{ | ||
return At<ImagePacket>(timestamp); | ||
} | ||
|
||
public override Image Get() | ||
{ | ||
UnsafeNativeMethods.mp_Packet__GetImage(mpPtr, out var imagePtr).Assert(); | ||
|
||
GC.KeepAlive(this); | ||
return new Image(imagePtr, false); | ||
} | ||
|
||
public override Image Consume() | ||
{ | ||
UnsafeNativeMethods.mp_Packet__ConsumeImage(mpPtr, out var statusPtr, out var imagePtr).Assert(); | ||
|
||
GC.KeepAlive(this); | ||
AssertStatusOk(statusPtr); | ||
return new Image(imagePtr, true); | ||
} | ||
|
||
public override Status ValidateAsType() | ||
{ | ||
UnsafeNativeMethods.mp_Packet__ValidateAsImage(mpPtr, out var statusPtr).Assert(); | ||
|
||
GC.KeepAlive(this); | ||
return new Status(statusPtr); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Framework/Packet/ImagePacket.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
...ub.homuler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Image_Safe.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) 2021 homuler | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
using System; | ||
using System.Diagnostics.Contracts; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Mediapipe | ||
{ | ||
internal static partial class SafeNativeMethods | ||
{ | ||
[Pure, DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
public static extern int mp_Image__width(IntPtr image); | ||
|
||
[Pure, DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
public static extern int mp_Image__height(IntPtr image); | ||
|
||
[Pure, DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
public static extern int mp_Image__channels(IntPtr image); | ||
|
||
[Pure, DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
public static extern int mp_Image__step(IntPtr image); | ||
|
||
[Pure, DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
[return: MarshalAs(UnmanagedType.I1)] | ||
public static extern bool mp_Image__UsesGpu(IntPtr image); | ||
|
||
[Pure, DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
public static extern ImageFormat.Types.Format mp_Image__image_format(IntPtr image); | ||
|
||
[Pure, DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
public static extern GpuBufferFormat mp_Image__format(IntPtr image); | ||
|
||
#region PixelWriteLock | ||
[Pure, DllImport(MediaPipeLibrary, ExactSpelling = true)] | ||
[return: MarshalAs(UnmanagedType.I1)] | ||
public static extern IntPtr mp_PixelWriteLock__Pixels(IntPtr pixelWriteLock); | ||
#endregion | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...muler.mediapipe/Runtime/Scripts/PInvoke/NativeMethods/Framework/Format/Image_Safe.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.