using System;
using System.Collections.Generic;
namespace MMDeploy
{
///
/// Output of Restorer.
///
public struct RestorerOutput
{
///
/// Height.
///
public int Height;
///
/// Width.
///
public int Width;
///
/// Raw data.
///
public byte[] Data;
///
/// Initializes a new instance of the struct.
///
/// height.
/// width.
/// data.
public RestorerOutput(int height, int width, byte[] data)
{
Height = height;
Width = width;
Data = new byte[height * width * 3];
Array.Copy(data, Data, data.Length);
}
internal unsafe RestorerOutput(Mat* result)
{
Height = result->Height;
Width = result->Width;
Data = new byte[Height * Width * 3];
int nbytes = Height * Width * 3;
fixed (byte* data = this.Data)
{
Buffer.MemoryCopy(result->Data, data, nbytes, nbytes);
}
}
}
///
/// Restorer.
///
public class Restorer : DisposableObject
{
///
/// Initializes a new instance of the class.
///
/// model path.
/// device name.
/// device id.
public Restorer(string modelPath, string deviceName, int deviceId)
{
ThrowException(NativeMethods.mmdeploy_restorer_create_by_path(modelPath, deviceName, deviceId, out _handle));
}
///
/// Get information of each image in a batch.
///
/// input mats.
/// Results of each input mat.
public List Apply(Mat[] mats)
{
List output = new List();
unsafe
{
Mat* results = null;
fixed (Mat* _mats = mats)
{
ThrowException(NativeMethods.mmdeploy_restorer_apply(_handle, _mats, mats.Length, &results));
}
FormatResult(mats.Length, results, ref output, out var total);
ReleaseResult(results, total);
}
return output;
}
private unsafe void FormatResult(int matCount, Mat* results, ref List output, out int total)
{
total = 0;
for (int i = 0; i < matCount; i++)
{
output.Add(new RestorerOutput(results));
results++;
total++;
}
}
private unsafe void ReleaseResult(Mat* results, int count)
{
NativeMethods.mmdeploy_restorer_release_result(results, count);
}
///
protected override void ReleaseHandle()
{
NativeMethods.mmdeploy_restorer_destroy(_handle);
}
}
}