Skip to content

Commit

Permalink
feat: implement ImageVectorPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Sep 2, 2023
1 parent b3e86f9 commit 96a8235
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// 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 System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Mediapipe
{
public class ImageVectorPacket : Packet<List<Image>>
{
[StructLayout(LayoutKind.Sequential)]
internal readonly struct ImageVector
{
private readonly IntPtr _data;
private readonly int _size;

public void Dispose()
{
UnsafeNativeMethods.mp_api_ImageArray__delete(_data);
}

public List<Image> Copy()
{
var images = new List<Image>(_size);

unsafe
{
var imagePtr = (IntPtr*)_data;

for (var i = 0; i < _size; i++)
{
var image = new Image(*imagePtr++, true);
images.Add(image);
}
}

return images;
}
}

/// <summary>
/// Creates an empty <see cref="ImageVectorPacket" /> instance.
/// </summary>
public ImageVectorPacket() : base(true) { }

[UnityEngine.Scripting.Preserve]
public ImageVectorPacket(IntPtr ptr, bool isOwner = true) : base(ptr, isOwner) { }

public ImageVectorPacket At(Timestamp timestamp)
{
return At<ImageVectorPacket>(timestamp);
}

public override List<Image> Get()
{
UnsafeNativeMethods.mp_Packet__GetImageVector(mpPtr, out var imageVector).Assert();
GC.KeepAlive(this);

var images = imageVector.Copy();
imageVector.Dispose();

return images;
}

public override List<Image> Consume()
{
throw new NotSupportedException();
}
}
}

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 @@ -54,8 +54,14 @@ public static extern MpReturnCode mp_Image__ui_ui_i_i_ui_PF_PSgc(
[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetImage(IntPtr packet, out IntPtr image);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__GetImageVector(IntPtr packet, out ImageVectorPacket.ImageVector imageVector);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_Packet__ValidateAsImage(IntPtr packet, out IntPtr status);

[DllImport(MediaPipeLibrary, ExactSpelling = true)]
public static extern MpReturnCode mp_api_ImageArray__delete(IntPtr array);
#endregion
}
}
19 changes: 19 additions & 0 deletions mediapipe_api/framework/formats/image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,28 @@ MpReturnCode mp_Packet__GetImage(mediapipe::Packet* packet, const mediapipe::Ima
return mp_Packet__Get(packet, value_out);
}

MpReturnCode mp_Packet__GetImageVector(mediapipe::Packet* packet, mp_api::StructArray<mediapipe::Image*>* value_out) {
TRY_ALL
auto vec = packet->Get<std::vector<mediapipe::Image>>();
auto size = vec.size();
auto data = new mediapipe::Image*[size];

for (auto i = 0; i < size; ++i) {
data[i] = new mediapipe::Image(std::move(vec[i]));
}
value_out->data = data;
value_out->size = static_cast<int>(size);
RETURN_CODE(MpReturnCode::Success);
CATCH_ALL
}

MpReturnCode mp_Packet__ValidateAsImage(mediapipe::Packet* packet, absl::Status** status_out) {
TRY
*status_out = new absl::Status{packet->ValidateAsType<mediapipe::Image>()};
RETURN_CODE(MpReturnCode::Success);
CATCH_EXCEPTION
}

void mp_api_ImageArray__delete(mediapipe::Image** image_array) {
delete[] image_array;
}
3 changes: 3 additions & 0 deletions mediapipe_api/framework/formats/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ MP_CAPI(MpReturnCode) mp__MakeImagePacket__Pif(mediapipe::Image* image, mediapip
MP_CAPI(MpReturnCode) mp__MakeImagePacket_At__Pif_Rt(mediapipe::Image* image, mediapipe::Timestamp* timestamp, mediapipe::Packet** packet_out);
MP_CAPI(MpReturnCode) mp_Packet__ConsumeImage(mediapipe::Packet* packet, absl::Status **status_out, mediapipe::Image** value_out);
MP_CAPI(MpReturnCode) mp_Packet__GetImage(mediapipe::Packet* packet, const mediapipe::Image** value_out);
MP_CAPI(MpReturnCode) mp_Packet__GetImageVector(mediapipe::Packet* packet, mp_api::StructArray<mediapipe::Image*>* value_out);
MP_CAPI(MpReturnCode) mp_Packet__ValidateAsImage(mediapipe::Packet* packet, absl::Status** status_out);

MP_CAPI(void) mp_api_ImageArray__delete(mediapipe::Image** image_array);

} // extern "C"

#endif // MEDIAPIPE_API_FRAMEWORK_FORMATS_IMAGE_H_

0 comments on commit 96a8235

Please sign in to comment.