You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

123 lines
3.7 KiB

//----------------------------------------------------------------------------
// Copyright (C) 2004-2016 by EMGU Corporation. All rights reserved.
//----------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Emgu.CV.Structure;
using Emgu.Util;
namespace Emgu.CV
{
/// <summary>
/// An implementation of IInputArray intented to convert data to IInputArray
/// </summary>
public class ScalarArray : UnmanagedObject, IInputArray
{
static ScalarArray()
{
CvInvoke.CheckLibraryLoaded();
}
private enum DataType
{
Scalar,
Double
}
private DataType _dataType;
/// <summary>
/// Create an InputArray from MCvScalar
/// </summary>
/// <param name="scalar">The MCvScalar to be converted to InputArray</param>
public ScalarArray(MCvScalar scalar)
{
_ptr = cveScalarCreate(ref scalar);
_dataType = DataType.Scalar;
}
/// <summary>
/// Create an InputArray from a double value
/// </summary>
/// <param name="scalar">The double value to be converted to InputArray</param>
public ScalarArray(double scalar)
{
_ptr = Marshal.AllocHGlobal(sizeof(double));
_dataType = DataType.Double;
Marshal.Copy(new double[] { scalar }, 0, _ptr, 1);
}
/// <summary>
/// Convert double scalar to InputArray
/// </summary>
/// <param name="scalar">The double scalar</param>
/// <returns>The InputArray</returns>
public static explicit operator ScalarArray(double scalar)
{
return new ScalarArray(scalar);
}
/// <summary>
/// Convert MCvSalar to InputArray
/// </summary>
/// <param name="scalar">The MCvScalar</param>
/// <returns>The InputArray</returns>
public static explicit operator ScalarArray(MCvScalar scalar)
{
return new ScalarArray(scalar);
}
/// <summary>
/// Release all the unmanaged memory associated with this InputArray
/// </summary>
protected override void DisposeObject()
{
if (_ptr != IntPtr.Zero)
{
if (_dataType == DataType.Scalar)
cveScalarRelease(ref _ptr);
else if (_dataType == DataType.Double)
{
Marshal.FreeHGlobal(_ptr);
_ptr = IntPtr.Zero;
}
Debug.Assert(_ptr == IntPtr.Zero);
}
}
/// <summary>
/// The pointer to the input array
/// </summary>
public InputArray GetInputArray()
{
if (_dataType == DataType.Scalar)
return new InputArray(cveInputArrayFromScalar(_ptr), this);
else if (_dataType == DataType.Double)
{
return new InputArray(cveInputArrayFromDouble(_ptr), this);
}
else
{
throw new NotImplementedException("Not implemented");
}
}
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal extern static IntPtr cveScalarCreate(ref MCvScalar scalar);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal extern static void cveScalarRelease(ref IntPtr scalar);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal extern static IntPtr cveInputArrayFromScalar(IntPtr scalar);
[DllImport(CvInvoke.ExternLibrary, CallingConvention = CvInvoke.CvCallingConvention)]
internal extern static IntPtr cveInputArrayFromDouble(IntPtr scalar);
}
}