Skip to content

Commit

Permalink
feat: implement Image (#957)
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Jul 17, 2023
1 parent dd4c50e commit 889e8ca
Show file tree
Hide file tree
Showing 20 changed files with 996 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

namespace Mediapipe.Unity
Expand All @@ -15,7 +14,7 @@ public class StartSceneController : MonoBehaviour
{
private const string _TAG = nameof(Bootstrap);

[SerializeField] private Image _screen;
[SerializeField] private UnityEngine.UI.Image _screen;
[SerializeField] private GameObject _consolePrefab;

private IEnumerator Start()
Expand Down
2 changes: 1 addition & 1 deletion Assets/MediaPipeUnity/Samples/UI/Scripts/SolutionMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private Button GetButtonInRow(Transform row, int index)

private void HideButton(Button button)
{
var image = button.gameObject.GetComponent<Image>();
var image = button.gameObject.GetComponent<UnityEngine.UI.Image>();
image.color = new Color(0, 0, 0, 0);
button.transform.GetComponentInChildren<Text>().text = null;
}
Expand Down
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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected override void DeleteMpPtr()
}

[AOT.MonoPInvokeCallback(typeof(Deleter))]
private static void VoidDeleter(IntPtr _) { }
internal static void VoidDeleter(IntPtr _) { }

/// <returns>
/// The number of channels for a <paramref name="format" />.
Expand Down
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);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 889e8ca

Please sign in to comment.