ZEDMat.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. /// <summary>
  6. /// This file holds the ZEDMat class along with low-level structures used for passing data between
  7. /// the C# ZEDMat and its equivalent in the SDK.
  8. /// </summary>
  9. namespace sl
  10. {
  11. /// <summary>
  12. /// Represents a 2D vector of uchars for use on both the CPU and GPU.
  13. /// </summary>
  14. [StructLayout(LayoutKind.Sequential)]
  15. public struct char2
  16. {
  17. public byte r;
  18. public byte g;
  19. }
  20. /// <summary>
  21. /// Represents a 3D vector of uchars for use on both the CPU and GPU.
  22. /// </summary>
  23. [StructLayout(LayoutKind.Sequential)]
  24. public struct char3
  25. {
  26. public byte r;
  27. public byte g;
  28. public byte b;
  29. }
  30. /// <summary>
  31. /// Represents a 4D vector of uchars for use on both the CPU and GPU.
  32. /// </summary>
  33. [StructLayout(LayoutKind.Sequential)]
  34. public struct char4
  35. {
  36. [MarshalAs(UnmanagedType.U1)]
  37. public byte r;
  38. [MarshalAs(UnmanagedType.U1)]
  39. public byte g;
  40. [MarshalAs(UnmanagedType.U1)]
  41. public byte b;
  42. [MarshalAs(UnmanagedType.U1)]
  43. public byte a;
  44. }
  45. /// <summary>
  46. /// Represents a 2D vector of floats for use on both the CPU and GPU.
  47. /// </summary>
  48. [StructLayout(LayoutKind.Sequential)]
  49. public struct float2
  50. {
  51. public float r;
  52. public float g;
  53. }
  54. /// <summary>
  55. /// Represents a 3D vector of floats for use on both the CPU and GPU.
  56. /// </summary>
  57. [StructLayout(LayoutKind.Sequential)]
  58. public struct float3
  59. {
  60. public float r;
  61. public float g;
  62. public float b;
  63. }
  64. /// <summary>
  65. /// Represents a 4D vector of floats for use on both the CPU and GPU.
  66. /// </summary>
  67. [StructLayout(LayoutKind.Sequential)]
  68. public struct float4
  69. {
  70. public float r;
  71. public float g;
  72. public float b;
  73. public float a;
  74. }
  75. /// <summary>
  76. /// Mirrors the sl::Mat class used in the ZED C++ SDK to store images.
  77. /// Can be used to retrieve individual images from GPU or CPU memory: see ZEDCamera.RetrieveImage()
  78. /// and ZEDCamera.RetrieveMeasure().
  79. /// </summary><remarks>
  80. /// For more information on the Mat class it mirrors, see:
  81. /// https://www.stereolabs.com/developers/documentation/API/v2.5.1/classsl_1_1Mat.html
  82. /// </remarks>
  83. public class ZEDMat
  84. {
  85. /// <summary>
  86. /// Type of mat, indicating the data type and the number of channels it holds.
  87. /// Proper mat type depends on the image type. See sl.VIEW and sl.MEASURE (in ZEDCommon.cs)
  88. /// </summary>
  89. public enum MAT_TYPE
  90. {
  91. /// <summary>
  92. /// Float, one channel. Used for depth and disparity Measure-type textures.
  93. /// </summary>
  94. MAT_32F_C1,
  95. /// <summary>
  96. /// Float, two channels.
  97. /// </summary>
  98. MAT_32F_C2,
  99. /// <summary>
  100. /// Float, three channels.
  101. /// </summary>
  102. MAT_32F_C3, /*!< float 3 channels.*/
  103. /// <summary>
  104. /// Float, four channels. Used for normals and XYZ (point cloud) measure-type textures
  105. /// </summary>
  106. MAT_32F_C4,
  107. /// <summary>
  108. /// Unsigned char, one channel. Used for greyscale image-type textures like depth and confidence displays.
  109. /// </summary>
  110. MAT_8U_C1,
  111. /// <summary>
  112. /// Unsigned char, two channels.
  113. /// </summary>
  114. MAT_8U_C2,
  115. /// <summary>
  116. /// Unsigned char, three channels.
  117. /// </summary>
  118. MAT_8U_C3,
  119. /// <summary>
  120. /// Unsigned char, four channels. Used for color images, like the main RGB image from each sensor.
  121. /// </summary>
  122. MAT_8U_C4,
  123. /// <summary>
  124. /// Unsigned short 1 channel.
  125. /// </summary>
  126. MAT_16U_C1
  127. };
  128. /// <summary>
  129. /// Categories for copying data within or between the CPU (processor) memory and GPU (graphics card) memory.
  130. /// </summary>
  131. public enum COPY_TYPE
  132. {
  133. /// <summary>
  134. /// Copies data from one place in CPU memory to another.
  135. /// </summary>
  136. COPY_TYPE_CPU_CPU, /*!< copy data from CPU to CPU.*/
  137. /// <summary>
  138. /// Copies data from CPU memory to GPU memory.
  139. /// </summary>
  140. COPY_TYPE_CPU_GPU, /*!< copy data from CPU to GPU.*/
  141. /// <summary>
  142. /// Copies data from one place in GPU memory to another.
  143. /// </summary>
  144. COPY_TYPE_GPU_GPU, /*!< copy data from GPU to GPU.*/
  145. /// <summary>
  146. /// Copies data from GPU memory to CPU memory.
  147. /// </summary>
  148. COPY_TYPE_GPU_CPU /*!< copy data from GPU to CPU.*/
  149. };
  150. /// <summary>
  151. /// Which memory to store an image/mat: CPU/processor memory or GPU (graphics card) memory.
  152. /// </summary>
  153. public enum MEM
  154. {
  155. /// <summary>
  156. /// Store on memory accessible by the CPU.
  157. /// </summary>
  158. MEM_CPU = 0,
  159. /// <summary>
  160. /// Store on memory accessible by the GPU.
  161. /// </summary>
  162. MEM_GPU = 1 ,
  163. LAST = 2
  164. };
  165. #region DLL Calls
  166. const string nameDll = sl.ZEDCommon.NameDLL;
  167. [DllImport(nameDll, EntryPoint = "sl_mat_create_new")]
  168. private static extern IntPtr dllz_mat_create_new(int width, int height, int type, int mem);
  169. [DllImport(nameDll, EntryPoint = "sl_mat_create_new_empty")]
  170. private static extern IntPtr dllz_mat_create_new_empty();
  171. [DllImport(nameDll, EntryPoint = "sl_mat_is_init")]
  172. private static extern bool dllz_mat_is_init(System.IntPtr ptr);
  173. [DllImport(nameDll, EntryPoint = "sl_mat_free")]
  174. private static extern bool dllz_mat_free(System.IntPtr ptr, int type);
  175. [DllImport(nameDll, EntryPoint = "sl_mat_get_infos")]
  176. private static extern bool dllz_mat_get_infos(System.IntPtr ptr, byte[] buffer);
  177. [DllImport(nameDll, EntryPoint = "sl_mat_get_value_float")]
  178. private static extern int dllz_mat_get_value_float(System.IntPtr ptr, int x, int y, out float value, int mem);
  179. [DllImport(nameDll, EntryPoint = "sl_mat_get_value_float2")]
  180. private static extern int dllz_mat_get_value_float2(System.IntPtr ptr, int x, int y, out float2 value, int mem);
  181. [DllImport(nameDll, EntryPoint = "sl_mat_get_value_float3")]
  182. private static extern int dllz_mat_get_value_float3(System.IntPtr ptr, int x, int y, out float3 value, int mem);
  183. [DllImport(nameDll, EntryPoint = "sl_mat_get_value_float4")]
  184. private static extern int dllz_mat_get_value_float4(System.IntPtr ptr, int x, int y, out float4 value, int mem);
  185. [DllImport(nameDll, EntryPoint = "sl_mat_get_value_uchar")]
  186. private static extern int dllz_mat_get_value_uchar(System.IntPtr ptr, int x, int y, out byte value, int mem);
  187. [DllImport(nameDll, EntryPoint = "sl_mat_get_value_uchar2")]
  188. private static extern int dllz_mat_get_value_uchar2(System.IntPtr ptr, int x, int y, out char2 value, int mem);
  189. [DllImport(nameDll, EntryPoint = "sl_mat_get_value_uchar3")]
  190. private static extern int dllz_mat_get_value_uchar3(System.IntPtr ptr, int x, int y, out char3 value, int mem);
  191. [DllImport(nameDll, EntryPoint = "sl_mat_get_value_uchar4")]
  192. private static extern int dllz_mat_get_value_uchar4(System.IntPtr ptr, int x, int y, out char4 value, int mem);
  193. [DllImport(nameDll, EntryPoint = "sl_mat_set_value_float")]
  194. private static extern int dllz_mat_set_value_float(System.IntPtr ptr, int x, int y, ref float value, int mem);
  195. [DllImport(nameDll, EntryPoint = "sl_mat_set_value_float2")]
  196. private static extern int dllz_mat_set_value_float2(System.IntPtr ptr, int x, int y, ref float2 value, int mem);
  197. [DllImport(nameDll, EntryPoint = "sl_mat_set_value_float3")]
  198. private static extern int dllz_mat_set_value_float3(System.IntPtr ptr, int x, int y, ref float3 value, int mem);
  199. [DllImport(nameDll, EntryPoint = "sl_mat_set_value_float4")]
  200. private static extern int dllz_mat_set_value_float4(System.IntPtr ptr, int x, int y, ref float4 value, int mem);
  201. [DllImport(nameDll, EntryPoint = "sl_mat_set_value_uchar")]
  202. private static extern int dllz_mat_set_value_uchar(System.IntPtr ptr, int x, int y, ref byte value, int mem);
  203. [DllImport(nameDll, EntryPoint = "sl_mat_set_value_uchar2")]
  204. private static extern int dllz_mat_set_value_uchar2(System.IntPtr ptr, int x, int y, ref char2 value, int mem);
  205. [DllImport(nameDll, EntryPoint = "sl_mat_set_value_uchar3")]
  206. private static extern int dllz_mat_set_value_uchar3(System.IntPtr ptr, int x, int y, ref char3 value, int mem);
  207. [DllImport(nameDll, EntryPoint = "sl_mat_set_value_uchar4")]
  208. private static extern int dllz_mat_set_value_uchar4(System.IntPtr ptr, int x, int y, ref char4 value, int mem);
  209. [DllImport(nameDll, EntryPoint = "sl_mat_set_to_float")]
  210. private static extern int dllz_mat_set_to_float(System.IntPtr ptr, ref float value, int mem);
  211. [DllImport(nameDll, EntryPoint = "sl_mat_set_to_float2")]
  212. private static extern int dllz_mat_set_to_float2(System.IntPtr ptr, ref float2 value, int mem);
  213. [DllImport(nameDll, EntryPoint = "sl_mat_set_to_float3")]
  214. private static extern int dllz_mat_set_to_float3(System.IntPtr ptr, ref float3 value, int mem);
  215. [DllImport(nameDll, EntryPoint = "sl_mat_set_to_float4")]
  216. private static extern int dllz_mat_set_to_float4(System.IntPtr ptr, ref float4 value, int mem);
  217. [DllImport(nameDll, EntryPoint = "sl_mat_set_to_uchar")]
  218. private static extern int dllz_mat_set_to_uchar(System.IntPtr ptr, ref byte value, int mem);
  219. [DllImport(nameDll, EntryPoint = "sl_mat_set_to_uchar2")]
  220. private static extern int dllz_mat_set_to_uchar2(System.IntPtr ptr, ref char2 value, int mem);
  221. [DllImport(nameDll, EntryPoint = "sl_mat_set_to_uchar3")]
  222. private static extern int dllz_mat_set_to_uchar3(System.IntPtr ptr, ref char3 value, int mem);
  223. [DllImport(nameDll, EntryPoint = "sl_mat_set_to_uchar4")]
  224. private static extern int dllz_mat_set_to_uchar4(System.IntPtr ptr, ref char4 value, int mem);
  225. [DllImport(nameDll, EntryPoint = "sl_mat_update_cpu_from_gpu")]
  226. private static extern int dllz_mat_update_cpu_from_gpu(System.IntPtr ptr);
  227. [DllImport(nameDll, EntryPoint = "sl_mat_update_gpu_from_cpu")]
  228. private static extern int dllz_mat_update_gpu_from_cpu(System.IntPtr ptr);
  229. [DllImport(nameDll, EntryPoint = "sl_mat_read")]
  230. private static extern int dllz_mat_read(System.IntPtr ptr, string filePath);
  231. [DllImport(nameDll, EntryPoint = "sl_mat_write")]
  232. private static extern int dllz_mat_write(System.IntPtr ptr, string filePath,int compression_level);
  233. [DllImport(nameDll, EntryPoint = "sl_mat_copy_to")]
  234. private static extern int dllz_mat_copy_to(System.IntPtr ptr, System.IntPtr dest, int cpyType);
  235. [DllImport(nameDll, EntryPoint = "sl_mat_get_width")]
  236. private static extern int dllz_mat_get_width(System.IntPtr ptr);
  237. [DllImport(nameDll, EntryPoint = "sl_mat_get_height")]
  238. private static extern int dllz_mat_get_height(System.IntPtr ptr);
  239. [DllImport(nameDll, EntryPoint = "sl_mat_get_channels")]
  240. private static extern int dllz_mat_get_channels(System.IntPtr ptr);
  241. [DllImport(nameDll, EntryPoint = "sl_mat_get_memory_type")]
  242. private static extern int dllz_mat_get_memory_type(System.IntPtr ptr);
  243. [DllImport(nameDll, EntryPoint = "sl_mat_get_pixel_bytes")]
  244. private static extern int dllz_mat_get_pixel_bytes(System.IntPtr ptr);
  245. [DllImport(nameDll, EntryPoint = "sl_mat_get_step")]
  246. private static extern int dllz_mat_get_step(System.IntPtr ptr);
  247. [DllImport(nameDll, EntryPoint = "sl_mat_get_step_bytes")]
  248. private static extern int dllz_mat_get_step_bytes(System.IntPtr ptr);
  249. [DllImport(nameDll, EntryPoint = "sl_mat_get_width_bytes")]
  250. private static extern int dllz_mat_get_width_bytes(System.IntPtr ptr);
  251. [DllImport(nameDll, EntryPoint = "sl_mat_is_memory_owner")]
  252. private static extern bool dllz_mat_is_memory_owner(System.IntPtr ptr);
  253. [DllImport(nameDll, EntryPoint = "sl_mat_get_resolution")]
  254. private static extern sl.Resolution dllz_mat_get_resolution(System.IntPtr ptr);
  255. [DllImport(nameDll, EntryPoint = "sl_mat_alloc")]
  256. private static extern void dllz_mat_alloc(System.IntPtr ptr, int width, int height, int type, int mem);
  257. [DllImport(nameDll, EntryPoint = "sl_mat_set_from")]
  258. private static extern int dllz_mat_set_from(System.IntPtr ptr, System.IntPtr source, int copyType);
  259. [DllImport(nameDll, EntryPoint = "sl_mat_get_ptr")]
  260. private static extern System.IntPtr dllz_mat_get_ptr(System.IntPtr ptr, int mem);
  261. [DllImport(nameDll, EntryPoint = "sl_mat_clone")]
  262. private static extern void dllz_mat_clone(System.IntPtr ptr, System.IntPtr ptrSource);
  263. #endregion
  264. /// <summary>
  265. /// Returns the internal ptr of a Mat.
  266. /// </summary>
  267. private System.IntPtr _matInternalPtr;
  268. /// <summary>
  269. /// Returns the internal ptr of a Mat.
  270. /// </summary>
  271. public IntPtr MatPtr
  272. {
  273. get { return _matInternalPtr; }
  274. }
  275. /// <summary>
  276. /// Creates an empty Mat.
  277. /// </summary>
  278. public ZEDMat()
  279. {
  280. _matInternalPtr = IntPtr.Zero;
  281. }
  282. /// <summary>
  283. /// Creates a mat from an existing internal ptr.
  284. /// </summary>
  285. /// <param name="ptr">IntPtr to create the material with.</param>
  286. public ZEDMat(System.IntPtr ptr)
  287. {
  288. if(ptr == IntPtr.Zero)
  289. {
  290. throw new Exception("ZED Mat not initialized.");
  291. }
  292. _matInternalPtr = ptr;
  293. }
  294. /// <summary>
  295. /// Creates a Mat with a given resolution.
  296. /// </summary>
  297. /// <param name="resolution">Resolution for the new Mat.</param>
  298. /// <param name="type">Data type and number of channels the Mat will hold.
  299. /// Depends on texture type: see sl.VIEW and sl.MEASURE in ZEDCommon.cs.</param>
  300. /// <param name="mem">Whether Mat should exist on CPU or GPU memory.
  301. /// Choose depending on where you'll need to access it from.</param>
  302. public void Create(sl.Resolution resolution, MAT_TYPE type, MEM mem = MEM.MEM_CPU)
  303. {
  304. _matInternalPtr = dllz_mat_create_new((int)resolution.width, (int)resolution.height, (int)(type), (int)(mem));
  305. }
  306. /// <summary>
  307. /// Creates a Mat with a given width and height.
  308. /// </summary>
  309. /// <param name="width">Width of the new Mat.</param>
  310. /// <param name="height">Height of the new Mat.</param>
  311. /// <param name="type">Data type and number of channels the Mat will hold.
  312. /// Depends on texture type: see sl.VIEW and sl.MEASURE in ZEDCommon.cs.</param>
  313. /// <param name="mem">Whether Mat should exist on CPU or GPU memory.
  314. /// Choose depending on where you'll need to access it from.</param>
  315. public void Create(uint width, uint height, MAT_TYPE type, MEM mem = MEM.MEM_CPU)
  316. {
  317. _matInternalPtr = dllz_mat_create_new((int)width, (int)height, (int)(type), (int)(mem));
  318. }
  319. /// <summary>
  320. /// True if the Mat has been initialized.
  321. /// </summary>
  322. /// <returns></returns>
  323. public bool IsInit()
  324. {
  325. return dllz_mat_is_init(_matInternalPtr);
  326. }
  327. /// <summary>
  328. /// Frees the memory of the Mat.
  329. /// </summary>
  330. /// <param name="mem">Whether the Mat is on CPU or GPU memory.</param>
  331. public void Free(MEM mem = MEM.LAST)
  332. {
  333. dllz_mat_free(_matInternalPtr, (int)mem);
  334. _matInternalPtr = IntPtr.Zero;
  335. }
  336. /// <summary>
  337. /// Copies data from the GPU to the CPU, if possible.
  338. /// </summary>
  339. /// <returns></returns>
  340. public sl.ERROR_CODE UpdateCPUFromGPU()
  341. {
  342. return (sl.ERROR_CODE)dllz_mat_update_cpu_from_gpu(_matInternalPtr);
  343. }
  344. /// <summary>
  345. /// Copies data from the CPU to the GPU, if possible.
  346. /// </summary>
  347. /// <returns></returns>
  348. public sl.ERROR_CODE UpdateGPUFromCPU()
  349. {
  350. return (sl.ERROR_CODE)dllz_mat_update_gpu_from_cpu(_matInternalPtr);
  351. }
  352. /// <summary>
  353. /// Returns information about the Mat.
  354. /// </summary>
  355. /// <returns>String providing Mat information.</returns>
  356. public string GetInfos()
  357. {
  358. byte[] buf = new byte[300];
  359. dllz_mat_get_infos(_matInternalPtr, buf);
  360. return System.Text.Encoding.ASCII.GetString(buf);
  361. }
  362. /// <summary>
  363. /// Copies data from this Mat to another Mat (deep copy).
  364. /// </summary>
  365. /// <param name="dest">Mat that the data will be copied to.</param>
  366. /// <param name="copyType">The To and From memory types.</param>
  367. /// <returns>Error code indicating if the copy was successful, or why it wasn't.</returns>
  368. public sl.ERROR_CODE CopyTo(sl.ZEDMat dest, sl.ZEDMat.COPY_TYPE copyType = COPY_TYPE.COPY_TYPE_CPU_CPU)
  369. {
  370. return (sl.ERROR_CODE)dllz_mat_copy_to(_matInternalPtr, dest._matInternalPtr, (int)(copyType));
  371. }
  372. /// <summary>
  373. /// Reads an image from a file. Supports .png and .jpeg. Only works if Mat has access to MEM_CPU.
  374. /// </summary>
  375. /// <param name="filePath">File path, including file name and extension.</param>
  376. /// <returns>Error code indicating if the read was successful, or why it wasn't.</returns>
  377. public sl.ERROR_CODE Read(string filePath)
  378. {
  379. return (sl.ERROR_CODE)dllz_mat_read(_matInternalPtr, filePath);
  380. }
  381. /// <summary>
  382. /// Writes the Mat into a file as an image. Only works if Mat has access to MEM_CPU.
  383. /// </summary>
  384. /// <param name="filePath">File path, including file name and extension.</param>
  385. /// <param name="compression_level"> Compression level used. Highest value means highest compression (smaller size). Range : [0 - 100].</param>
  386. /// <returns>Error code indicating if the write was successful, or why it wasn't.</returns>
  387. public sl.ERROR_CODE Write(string filePath,int compressionLevel = -1)
  388. {
  389. return (sl.ERROR_CODE)dllz_mat_write(_matInternalPtr, filePath, compressionLevel);
  390. }
  391. /// <summary>
  392. /// Returns the width of the matrix.
  393. /// </summary>
  394. /// <returns></returns>
  395. public int GetWidth()
  396. {
  397. return dllz_mat_get_width(_matInternalPtr);
  398. }
  399. /// <summary>
  400. /// Returns the height of the matrix.
  401. /// </summary>
  402. /// <returns></returns>
  403. public int GetHeight()
  404. {
  405. return dllz_mat_get_height(_matInternalPtr);
  406. }
  407. /// <summary>
  408. /// Returns the number of values/channels stored in each pixel.
  409. /// </summary>
  410. /// <returns>Number of values/channels.</returns>
  411. public int GetChannels()
  412. {
  413. return dllz_mat_get_channels(_matInternalPtr);
  414. }
  415. /// <summary>
  416. /// Returns the size in bytes of one pixel.
  417. /// </summary>
  418. /// <returns>Size in bytes.</returns>
  419. public int GetPixelBytes()
  420. {
  421. return dllz_mat_get_pixel_bytes(_matInternalPtr);
  422. }
  423. /// <summary>
  424. /// Returns the memory 'step' in number/length of elements - how many values make up each row of pixels.
  425. /// </summary>
  426. /// <returns>Step length.</returns>
  427. public int GetStep()
  428. {
  429. return dllz_mat_get_step(_matInternalPtr);
  430. }
  431. /// <summary>
  432. /// Returns the memory 'step' in bytes - how many bytes make up each row of pixels.
  433. /// </summary>
  434. /// <returns></returns>
  435. public int GetStepBytes()
  436. {
  437. return dllz_mat_get_step_bytes(_matInternalPtr);
  438. }
  439. /// <summary>
  440. /// Returns the size of each row in bytes.
  441. /// </summary>
  442. /// <returns></returns>
  443. public int GetWidthBytes()
  444. {
  445. return dllz_mat_get_width_bytes(_matInternalPtr);
  446. }
  447. /// <summary>
  448. /// Returns the type of memory (CPU and/or GPU).
  449. /// </summary>
  450. /// <returns></returns>
  451. public MEM GetMemoryType()
  452. {
  453. return (MEM)dllz_mat_get_memory_type(_matInternalPtr);
  454. }
  455. /// <summary>
  456. /// Returns whether the Mat is the owner of the memory it's accessing.
  457. /// </summary>
  458. /// <returns></returns>
  459. public bool IsMemoryOwner()
  460. {
  461. return dllz_mat_is_memory_owner(_matInternalPtr);
  462. }
  463. /// <summary>
  464. /// Returns the resolution of the image that this Mat holds.
  465. /// </summary>
  466. /// <returns></returns>
  467. public sl.Resolution GetResolution()
  468. {
  469. return dllz_mat_get_resolution(_matInternalPtr);
  470. }
  471. /// <summary>
  472. /// Allocates memory for the Mat.
  473. /// </summary>
  474. /// <param name="width">Width of the image/matrix in pixels.</param>
  475. /// <param name="height">Height of the image/matrix in pixels.</param>
  476. /// <param name="matType">Type of matrix (data type and channels; see sl.MAT_TYPE)</param>
  477. /// <param name="mem">Where the buffer will be stored - CPU memory or GPU memory.</param>
  478. public void Alloc(uint width, uint height, MAT_TYPE matType, MEM mem = MEM.MEM_CPU)
  479. {
  480. dllz_mat_alloc(_matInternalPtr, (int)width, (int)height, (int)matType, (int)mem);
  481. }
  482. /// <summary>
  483. /// Allocates memory for the Mat.
  484. /// </summary>
  485. /// <param name="resolution">Size of the image/matrix in pixels.</param>
  486. /// <param name="matType">Type of matrix (data type and channels; see sl.MAT_TYPE)</param>
  487. /// <param name="mem">Where the buffer will be stored - CPU memory or GPU memory.</param>
  488. public void Alloc(sl.Resolution resolution, MAT_TYPE matType, MEM mem = MEM.MEM_CPU)
  489. {
  490. dllz_mat_alloc(_matInternalPtr, (int)resolution.width, (int)resolution.height, (int)matType, (int)mem);
  491. }
  492. /// <summary>
  493. /// Copies data from another Mat into this one(deep copy).
  494. /// </summary>
  495. /// <param name="src">Source Mat from which to copy.</param>
  496. /// <param name="copyType">The To and From memory types.</param>
  497. /// <returns>ERROR_CODE (as an int) indicating if the copy was successful, or why it wasn't.</returns>
  498. public int SetFrom(ZEDMat src, COPY_TYPE copyType = COPY_TYPE.COPY_TYPE_CPU_CPU)
  499. {
  500. return dllz_mat_set_from(_matInternalPtr, src._matInternalPtr, (int)copyType);
  501. }
  502. public System.IntPtr GetPtr(MEM mem = MEM.MEM_CPU)
  503. {
  504. return dllz_mat_get_ptr(_matInternalPtr, (int)mem);
  505. }
  506. /// <summary>
  507. /// Duplicates a Mat by copying all its data into a new one (deep copy).
  508. /// </summary>
  509. /// <param name="source"></param>
  510. public void Clone(ZEDMat source)
  511. {
  512. dllz_mat_clone(_matInternalPtr, source._matInternalPtr);
  513. }
  514. /************ GET VALUES *********************/
  515. //Cannot send values by template due to a covariant issue with an out needed.
  516. /// <summary>
  517. /// Returns the value of a specific point in the matrix. (MAT_32F_C1)
  518. /// </summary>
  519. /// <param name="x">Row the point is in.</param>
  520. /// <param name="y">Column the point is in.</param>
  521. /// <param name="value">Gets filled with the current value.</param>
  522. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  523. /// <returns>Error code indicating if the get was successful, or why it wasn't.</returns>
  524. public sl.ERROR_CODE GetValue(int x, int y, out float value, sl.ZEDMat.MEM mem)
  525. {
  526. return (sl.ERROR_CODE)(dllz_mat_get_value_float(_matInternalPtr, x, y, out value, (int)(mem)));
  527. }
  528. /// <summary>
  529. /// Returns the value of a specific point in the matrix. (MAT_32F_C2)
  530. /// </summary>
  531. /// <param name="x">Row the point is in.</param>
  532. /// <param name="y">Column the point is in.</param>
  533. /// <param name="value">Gets filled with the current value.</param>
  534. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  535. /// <returns>Error code indicating if the get was successful, or why it wasn't.</returns>
  536. public sl.ERROR_CODE GetValue(int x, int y, out float2 value, sl.ZEDMat.MEM mem)
  537. {
  538. return (sl.ERROR_CODE)(dllz_mat_get_value_float2(_matInternalPtr, x, y, out value, (int)(mem)));
  539. }
  540. /// <summary>
  541. /// Returns the value of a specific point in the matrix. (MAT_32F_C3)
  542. /// </summary>
  543. /// <param name="x">Row the point is in.</param>
  544. /// <param name="y">Column the point is in.</param>
  545. /// <param name="value">Gets filled with the current value.</param>
  546. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  547. /// <returns>Error code indicating if the get was successful, or why it wasn't.</returns>
  548. public sl.ERROR_CODE GetValue(int x, int y, out float3 value, sl.ZEDMat.MEM mem)
  549. {
  550. return (sl.ERROR_CODE)(dllz_mat_get_value_float3(_matInternalPtr, x, y, out value, (int)(mem)));
  551. }
  552. /// <summary>
  553. /// Returns the value of a specific point in the matrix. (MAT_32F_C4)
  554. /// </summary>
  555. /// <param name="x">Row the point is in.</param>
  556. /// <param name="y">Column the point is in.</param>
  557. /// <param name="value">Gets filled with the current value.</param>
  558. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  559. /// <returns>Error code indicating if the get was successful, or why it wasn't.</returns>
  560. public sl.ERROR_CODE GetValue(int x, int y, out float4 value, sl.ZEDMat.MEM mem)
  561. {
  562. return (sl.ERROR_CODE)(dllz_mat_get_value_float4(_matInternalPtr, x, y, out value, (int)(mem)));
  563. }
  564. /// <summary>
  565. /// Returns the value of a specific point in the matrix. (MAT_TYPE_8U_C1)
  566. /// </summary>
  567. /// <param name="x">Row the point is in.</param>
  568. /// <param name="y">Column the point is in.</param>
  569. /// <param name="value">Gets filled with the current value.</param>
  570. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  571. /// <returns>Error code indicating if the get was successful, or why it wasn't.</returns>
  572. public sl.ERROR_CODE GetValue(int x, int y, out byte value, sl.ZEDMat.MEM mem)
  573. {
  574. return (sl.ERROR_CODE)(dllz_mat_get_value_uchar(_matInternalPtr, x, y, out value, (int)(mem)));
  575. }
  576. /// <summary>
  577. /// Returns the value of a specific point in the matrix. (MAT_TYPE_8U_C2)
  578. /// </summary>
  579. /// <param name="x">Row the point is in.</param>
  580. /// <param name="y">Column the point is in.</param>
  581. /// <param name="value">Gets filled with the current value.</param>
  582. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  583. /// <returns>Error code indicating if the get was successful, or why it wasn't.</returns>
  584. public sl.ERROR_CODE GetValue(int x, int y, out char2 value, sl.ZEDMat.MEM mem)
  585. {
  586. return (sl.ERROR_CODE)(dllz_mat_get_value_uchar2(_matInternalPtr, x, y, out value, (int)(mem)));
  587. }
  588. /// <summary>
  589. /// Returns the value of a specific point in the matrix. (MAT_TYPE_8U_C3)
  590. /// </summary>
  591. /// <param name="x">Row the point is in.</param>
  592. /// <param name="y">Column the point is in.</param>
  593. /// <param name="value">Gets filled with the current value.</param>
  594. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  595. /// <returns>Error code indicating if the get was successful, or why it wasn't.</returns>
  596. public sl.ERROR_CODE GetValue(int x, int y, out char3 value, sl.ZEDMat.MEM mem)
  597. {
  598. return (sl.ERROR_CODE)(dllz_mat_get_value_uchar3(_matInternalPtr, x, y, out value, (int)(mem)));
  599. }
  600. /// <summary>
  601. /// Returns the value of a specific point in the matrix. (MAT_TYPE_8U_C4)
  602. /// </summary>
  603. /// <param name="x">Row the point is in.</param>
  604. /// <param name="y">Column the point is in.</param>
  605. /// <param name="value">Gets filled with the current value.</param>
  606. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  607. /// <returns>Error code indicating if the get was successful, or why it wasn't.</returns>
  608. public sl.ERROR_CODE GetValue(int x, int y, out char4 value, sl.ZEDMat.MEM mem)
  609. {
  610. return (sl.ERROR_CODE)(dllz_mat_get_value_uchar4(_matInternalPtr, x, y, out value, (int)(mem)));
  611. }
  612. /***************************************************************************************/
  613. /************ SET VALUES *********************/
  614. //Cannot send values by template due to a covariant issue with an out needed.
  615. /// <summary>
  616. /// Sets a value to a specific point in the matrix. (MAT_32F_C1)
  617. /// </summary>
  618. /// <param name="x">Row the point is in.</param>
  619. /// <param name="y">Column the point is in.</param>
  620. /// <param name="value">Value to which the point will be set.</param>
  621. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  622. /// <returns>Error code indicating if the set was successful, or why it wasn't.</returns>
  623. public sl.ERROR_CODE SetValue(int x, int y, ref float value, sl.ZEDMat.MEM mem)
  624. {
  625. return (sl.ERROR_CODE)(dllz_mat_set_value_float(_matInternalPtr, x, y, ref value, (int)(mem)));
  626. }
  627. /// <summary>
  628. /// Sets a value to a specific point in the matrix. (MAT_32F_C2)
  629. /// </summary>
  630. /// <param name="x">Row the point is in.</param>
  631. /// <param name="y">Column the point is in.</param>
  632. /// <param name="value">Value to which the point will be set.</param>
  633. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  634. /// <returns>Error code indicating if the set was successful, or why it wasn't.</returns>
  635. public sl.ERROR_CODE SetValue(int x, int y, ref float2 value, sl.ZEDMat.MEM mem)
  636. {
  637. return (sl.ERROR_CODE)(dllz_mat_set_value_float2(_matInternalPtr, x, y, ref value, (int)(mem)));
  638. }
  639. /// <summary>
  640. /// Sets a value to a specific point in the matrix. (MAT_32F_C3)
  641. /// </summary>
  642. /// <param name="x">Row the point is in.</param>
  643. /// <param name="y">Column the point is in.</param>
  644. /// <param name="value">Value to which the point will be set.</param>
  645. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  646. /// <returns>Error code indicating if the set was successful, or why it wasn't.</returns>
  647. public sl.ERROR_CODE SetValue(int x, int y, ref float3 value, sl.ZEDMat.MEM mem)
  648. {
  649. return (sl.ERROR_CODE)(dllz_mat_set_value_float3(_matInternalPtr, x, y, ref value, (int)(mem)));
  650. }
  651. /// <summary>
  652. /// Sets a value to a specific point in the matrix. (MAT_32F_C4)
  653. /// </summary>
  654. /// <param name="x">Row the point is in.</param>
  655. /// <param name="y">Column the point is in.</param>
  656. /// <param name="value">Value to which the point will be set.</param>
  657. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  658. /// <returns>Error code indicating if the set was successful, or why it wasn't.</returns>
  659. public sl.ERROR_CODE SetValue(int x, int y, float4 value, sl.ZEDMat.MEM mem)
  660. {
  661. return (sl.ERROR_CODE)(dllz_mat_set_value_float4(_matInternalPtr, x, y, ref value, (int)(mem)));
  662. }
  663. /// <summary>
  664. /// Sets a value to a specific point in the matrix. (MAT_TYPE_8U_C1)
  665. /// </summary>
  666. /// <param name="x">Row the point is in.</param>
  667. /// <param name="y">Column the point is in.</param>
  668. /// <param name="value">Value to which the point will be set.</param>
  669. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  670. /// <returns>Error code indicating if the set was successful, or why it wasn't.</returns>
  671. public sl.ERROR_CODE SetValue(int x, int y, ref byte value, sl.ZEDMat.MEM mem)
  672. {
  673. return (sl.ERROR_CODE)(dllz_mat_set_value_uchar(_matInternalPtr, x, y, ref value, (int)(mem)));
  674. }
  675. /// <summary>
  676. /// Sets a value to a specific point in the matrix. (MAT_TYPE_8U_C2)
  677. /// </summary>
  678. /// <param name="x">Row the point is in.</param>
  679. /// <param name="y">Column the point is in.</param>
  680. /// <param name="value">Value to which the point will be set.</param>
  681. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  682. /// <returns>Error code indicating if the set was successful, or why it wasn't.</returns>
  683. public sl.ERROR_CODE SetValue(int x, int y, ref char2 value, sl.ZEDMat.MEM mem)
  684. {
  685. return (sl.ERROR_CODE)(dllz_mat_set_value_uchar2(_matInternalPtr, x, y, ref value, (int)(mem)));
  686. }
  687. /// <summary>
  688. /// Sets a value to a specific point in the matrix. (MAT_TYPE_8U_C3)
  689. /// </summary>
  690. /// <param name="x">Row the point is in.</param>
  691. /// <param name="y">Column the point is in.</param>
  692. /// <param name="value">Value to which the point will be set.</param>
  693. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  694. /// <returns>Error code indicating if the set was successful, or why it wasn't.</returns>
  695. public sl.ERROR_CODE SetValue(int x, int y, ref char3 value, sl.ZEDMat.MEM mem)
  696. {
  697. return (sl.ERROR_CODE)(dllz_mat_set_value_uchar3(_matInternalPtr, x, y, ref value, (int)(mem)));
  698. }
  699. /// <summary>
  700. /// Sets a value to a specific point in the matrix. (MAT_TYPE_8U_C4)
  701. /// </summary>
  702. /// <param name="x">Row the point is in.</param>
  703. /// <param name="y">Column the point is in.</param>
  704. /// <param name="value">Value to which the point will be set.</param>
  705. /// <param name="mem">Whether point is on CPU memory or GPU memory.</param>
  706. /// <returns>Error code indicating if the set was successful, or why it wasn't.</returns>
  707. public sl.ERROR_CODE SetValue(int x, int y, ref char4 value, sl.ZEDMat.MEM mem)
  708. {
  709. return (sl.ERROR_CODE)(dllz_mat_set_value_uchar4(_matInternalPtr, x, y, ref value, (int)(mem)));
  710. }
  711. /***************************************************************************************/
  712. /************ SET TO *********************/
  713. //Cannot send values by template due to a covariant issue with an out needed.
  714. /// <summary>
  715. /// Fills the entire Mat with the given value. (MAT_32F_C1)
  716. /// </summary>
  717. /// <param name="value">Value with which to fill the Mat.</param>
  718. /// <param name="mem">Which buffer to fill - CPU or GPU memory.</param>
  719. /// <returns>Whether the set was successful, or why it wasn't.</returns>
  720. public sl.ERROR_CODE SetTo(ref float value, sl.ZEDMat.MEM mem)
  721. {
  722. return (sl.ERROR_CODE)(dllz_mat_set_to_float(_matInternalPtr, ref value, (int)(mem)));
  723. }
  724. /// <summary>
  725. /// Fills the entire Mat with the given value. (MAT_32F_C2)
  726. /// </summary>
  727. /// <param name="value">Value with which to fill the Mat.</param>
  728. /// <param name="mem">Which buffer to fill - CPU or GPU memory.</param>
  729. /// <returns>Whether the set was successful, or why it wasn't.</returns>
  730. public sl.ERROR_CODE SetTo(ref float2 value, sl.ZEDMat.MEM mem)
  731. {
  732. return (sl.ERROR_CODE)(dllz_mat_set_to_float2(_matInternalPtr, ref value, (int)(mem)));
  733. }
  734. /// <summary>
  735. /// Fills the entire Mat with the given value. (MAT_32F_C3)
  736. /// </summary>
  737. /// <param name="value">Value with which to fill the Mat.</param>
  738. /// <param name="mem">Which buffer to fill - CPU or GPU memory.</param>
  739. /// <returns>Whether the set was successful, or why it wasn't.</returns>
  740. public sl.ERROR_CODE SetTo(ref float3 value, sl.ZEDMat.MEM mem)
  741. {
  742. return (sl.ERROR_CODE)(dllz_mat_set_to_float3(_matInternalPtr, ref value, (int)(mem)));
  743. }
  744. /// <summary>
  745. /// Fills the entire Mat with the given value. (MAT_32F_C4)
  746. /// </summary>
  747. /// <param name="value">Value with which to fill the Mat.</param>
  748. /// <param name="mem">Which buffer to fill - CPU or GPU memory.</param>
  749. /// <returns>Whether the set was successful, or why it wasn't.</returns>
  750. public sl.ERROR_CODE SetTo(ref float4 value, sl.ZEDMat.MEM mem)
  751. {
  752. return (sl.ERROR_CODE)(dllz_mat_set_to_float4(_matInternalPtr, ref value, (int)(mem)));
  753. }
  754. /// <summary>
  755. /// Fills the entire Mat with the given value. (MAT_TYPE_8U_C1)
  756. /// </summary>
  757. /// <param name="value">Value with which to fill the Mat.</param>
  758. /// <param name="mem">Which buffer to fill - CPU or GPU memory.</param>
  759. /// <returns>Whether the set was successful, or why it wasn't.</returns>
  760. public sl.ERROR_CODE SetTo(ref byte value, sl.ZEDMat.MEM mem)
  761. {
  762. return (sl.ERROR_CODE)(dllz_mat_set_to_uchar(_matInternalPtr, ref value, (int)(mem)));
  763. }
  764. /// <summary>
  765. /// Fills the entire Mat with the given value. (MAT_TYPE_8U_C2)
  766. /// </summary>
  767. /// <param name="value">Value with which to fill the Mat.</param>
  768. /// <param name="mem">Which buffer to fill - CPU or GPU memory.</param>
  769. /// <returns>Whether the set was successful, or why it wasn't.</returns>
  770. public sl.ERROR_CODE SetTo(ref char2 value, sl.ZEDMat.MEM mem)
  771. {
  772. return (sl.ERROR_CODE)(dllz_mat_set_to_uchar2(_matInternalPtr, ref value, (int)(mem)));
  773. }
  774. /// <summary>
  775. /// Fills the entire Mat with the given value. (MAT_TYPE_8U_C3)
  776. /// </summary>
  777. /// <param name="value">Value with which to fill the Mat.</param>
  778. /// <param name="mem">Which buffer to fill - CPU or GPU memory.</param>
  779. /// <returns>Whether the set was successful, or why it wasn't.</returns>
  780. public sl.ERROR_CODE SetTo(ref char3 value, sl.ZEDMat.MEM mem)
  781. {
  782. return (sl.ERROR_CODE)(dllz_mat_set_to_uchar3(_matInternalPtr, ref value, (int)(mem)));
  783. }
  784. /// <summary>
  785. /// Fills the entire Mat with the given value. (MAT_TYPE_8U_C4)
  786. /// </summary>
  787. /// <param name="value">Value with which to fill the Mat.</param>
  788. /// <param name="mem">Which buffer to fill - CPU or GPU memory.</param>
  789. /// <returns>Whether the set was successful, or why it wasn't.</returns>
  790. public sl.ERROR_CODE SetTo( ref char4 value, sl.ZEDMat.MEM mem)
  791. {
  792. return (sl.ERROR_CODE)(dllz_mat_set_to_uchar4(_matInternalPtr, ref value, (int)(mem)));
  793. }
  794. /***************************************************************************************/
  795. }
  796. }