Imgcodecs.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. 
  2. using OpenCVForUnity.CoreModule;
  3. using OpenCVForUnity.UtilsModule;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace OpenCVForUnity.ImgcodecsModule
  8. {
  9. // C++: class Imgcodecs
  10. //javadoc: Imgcodecs
  11. public class Imgcodecs
  12. {
  13. // C++: enum ImwritePAMFlags
  14. public const int IMWRITE_PAM_FORMAT_NULL = 0;
  15. public const int IMWRITE_PAM_FORMAT_BLACKANDWHITE = 1;
  16. public const int IMWRITE_PAM_FORMAT_GRAYSCALE = 2;
  17. public const int IMWRITE_PAM_FORMAT_GRAYSCALE_ALPHA = 3;
  18. public const int IMWRITE_PAM_FORMAT_RGB = 4;
  19. public const int IMWRITE_PAM_FORMAT_RGB_ALPHA = 5;
  20. // C++: enum ImwritePNGFlags
  21. public const int IMWRITE_PNG_STRATEGY_DEFAULT = 0;
  22. public const int IMWRITE_PNG_STRATEGY_FILTERED = 1;
  23. public const int IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY = 2;
  24. public const int IMWRITE_PNG_STRATEGY_RLE = 3;
  25. public const int IMWRITE_PNG_STRATEGY_FIXED = 4;
  26. // C++: enum ImwriteFlags
  27. public const int IMWRITE_JPEG_QUALITY = 1;
  28. public const int IMWRITE_JPEG_PROGRESSIVE = 2;
  29. public const int IMWRITE_JPEG_OPTIMIZE = 3;
  30. public const int IMWRITE_JPEG_RST_INTERVAL = 4;
  31. public const int IMWRITE_JPEG_LUMA_QUALITY = 5;
  32. public const int IMWRITE_JPEG_CHROMA_QUALITY = 6;
  33. public const int IMWRITE_PNG_COMPRESSION = 16;
  34. public const int IMWRITE_PNG_STRATEGY = 17;
  35. public const int IMWRITE_PNG_BILEVEL = 18;
  36. public const int IMWRITE_PXM_BINARY = 32;
  37. public const int IMWRITE_EXR_TYPE = (3 << 4) + 0;
  38. public const int IMWRITE_WEBP_QUALITY = 64;
  39. public const int IMWRITE_PAM_TUPLETYPE = 128;
  40. public const int IMWRITE_TIFF_RESUNIT = 256;
  41. public const int IMWRITE_TIFF_XDPI = 257;
  42. public const int IMWRITE_TIFF_YDPI = 258;
  43. // C++: enum ImwriteEXRTypeFlags
  44. public const int IMWRITE_EXR_TYPE_HALF = 1;
  45. public const int IMWRITE_EXR_TYPE_FLOAT = 2;
  46. // C++: enum ImreadModes
  47. public const int IMREAD_UNCHANGED = -1;
  48. public const int IMREAD_GRAYSCALE = 0;
  49. public const int IMREAD_COLOR = 1;
  50. public const int IMREAD_ANYDEPTH = 2;
  51. public const int IMREAD_ANYCOLOR = 4;
  52. public const int IMREAD_LOAD_GDAL = 8;
  53. public const int IMREAD_REDUCED_GRAYSCALE_2 = 16;
  54. public const int IMREAD_REDUCED_COLOR_2 = 17;
  55. public const int IMREAD_REDUCED_GRAYSCALE_4 = 32;
  56. public const int IMREAD_REDUCED_COLOR_4 = 33;
  57. public const int IMREAD_REDUCED_GRAYSCALE_8 = 64;
  58. public const int IMREAD_REDUCED_COLOR_8 = 65;
  59. public const int IMREAD_IGNORE_ORIENTATION = 128;
  60. //
  61. // C++: Mat cv::imdecode(Mat buf, int flags)
  62. //
  63. //javadoc: imdecode(buf, flags)
  64. public static Mat imdecode (Mat buf, int flags)
  65. {
  66. if (buf != null) buf.ThrowIfDisposed ();
  67. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  68. Mat retVal = new Mat(imgcodecs_Imgcodecs_imdecode_10(buf.nativeObj, flags));
  69. return retVal;
  70. #else
  71. return null;
  72. #endif
  73. }
  74. //
  75. // C++: Mat cv::imread(String filename, int flags = IMREAD_COLOR)
  76. //
  77. //javadoc: imread(filename, flags)
  78. public static Mat imread (string filename, int flags)
  79. {
  80. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  81. Mat retVal = new Mat(imgcodecs_Imgcodecs_imread_10(filename, flags));
  82. return retVal;
  83. #else
  84. return null;
  85. #endif
  86. }
  87. //javadoc: imread(filename)
  88. public static Mat imread (string filename)
  89. {
  90. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  91. Mat retVal = new Mat(imgcodecs_Imgcodecs_imread_11(filename));
  92. return retVal;
  93. #else
  94. return null;
  95. #endif
  96. }
  97. //
  98. // C++: bool cv::haveImageReader(String filename)
  99. //
  100. //javadoc: haveImageReader(filename)
  101. public static bool haveImageReader (string filename)
  102. {
  103. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  104. bool retVal = imgcodecs_Imgcodecs_haveImageReader_10(filename);
  105. return retVal;
  106. #else
  107. return false;
  108. #endif
  109. }
  110. //
  111. // C++: bool cv::haveImageWriter(String filename)
  112. //
  113. //javadoc: haveImageWriter(filename)
  114. public static bool haveImageWriter (string filename)
  115. {
  116. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  117. bool retVal = imgcodecs_Imgcodecs_haveImageWriter_10(filename);
  118. return retVal;
  119. #else
  120. return false;
  121. #endif
  122. }
  123. //
  124. // C++: bool cv::imencode(String ext, Mat img, vector_uchar& buf, vector_int _params = std::vector<int>())
  125. //
  126. //javadoc: imencode(ext, img, buf, _params)
  127. public static bool imencode (string ext, Mat img, MatOfByte buf, MatOfInt _params)
  128. {
  129. if (img != null) img.ThrowIfDisposed ();
  130. if (buf != null) buf.ThrowIfDisposed ();
  131. if (_params != null) _params.ThrowIfDisposed ();
  132. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  133. Mat buf_mat = buf;
  134. Mat _params_mat = _params;
  135. bool retVal = imgcodecs_Imgcodecs_imencode_10(ext, img.nativeObj, buf_mat.nativeObj, _params_mat.nativeObj);
  136. return retVal;
  137. #else
  138. return false;
  139. #endif
  140. }
  141. //javadoc: imencode(ext, img, buf)
  142. public static bool imencode (string ext, Mat img, MatOfByte buf)
  143. {
  144. if (img != null) img.ThrowIfDisposed ();
  145. if (buf != null) buf.ThrowIfDisposed ();
  146. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  147. Mat buf_mat = buf;
  148. bool retVal = imgcodecs_Imgcodecs_imencode_11(ext, img.nativeObj, buf_mat.nativeObj);
  149. return retVal;
  150. #else
  151. return false;
  152. #endif
  153. }
  154. //
  155. // C++: bool cv::imreadmulti(String filename, vector_Mat& mats, int flags = IMREAD_ANYCOLOR)
  156. //
  157. //javadoc: imreadmulti(filename, mats, flags)
  158. public static bool imreadmulti (string filename, List<Mat> mats, int flags)
  159. {
  160. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  161. Mat mats_mat = new Mat();
  162. bool retVal = imgcodecs_Imgcodecs_imreadmulti_10(filename, mats_mat.nativeObj, flags);
  163. Converters.Mat_to_vector_Mat(mats_mat, mats);
  164. mats_mat.release();
  165. return retVal;
  166. #else
  167. return false;
  168. #endif
  169. }
  170. //javadoc: imreadmulti(filename, mats)
  171. public static bool imreadmulti (string filename, List<Mat> mats)
  172. {
  173. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  174. Mat mats_mat = new Mat();
  175. bool retVal = imgcodecs_Imgcodecs_imreadmulti_11(filename, mats_mat.nativeObj);
  176. Converters.Mat_to_vector_Mat(mats_mat, mats);
  177. mats_mat.release();
  178. return retVal;
  179. #else
  180. return false;
  181. #endif
  182. }
  183. //
  184. // C++: bool cv::imwrite(String filename, Mat img, vector_int _params = std::vector<int>())
  185. //
  186. //javadoc: imwrite(filename, img, _params)
  187. public static bool imwrite (string filename, Mat img, MatOfInt _params)
  188. {
  189. if (img != null) img.ThrowIfDisposed ();
  190. if (_params != null) _params.ThrowIfDisposed ();
  191. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  192. Mat _params_mat = _params;
  193. bool retVal = imgcodecs_Imgcodecs_imwrite_10(filename, img.nativeObj, _params_mat.nativeObj);
  194. return retVal;
  195. #else
  196. return false;
  197. #endif
  198. }
  199. //javadoc: imwrite(filename, img)
  200. public static bool imwrite (string filename, Mat img)
  201. {
  202. if (img != null) img.ThrowIfDisposed ();
  203. #if ((UNITY_ANDROID || UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR) || UNITY_5 || UNITY_5_3_OR_NEWER
  204. bool retVal = imgcodecs_Imgcodecs_imwrite_11(filename, img.nativeObj);
  205. return retVal;
  206. #else
  207. return false;
  208. #endif
  209. }
  210. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  211. const string LIBNAME = "__Internal";
  212. #else
  213. const string LIBNAME = "opencvforunity";
  214. #endif
  215. // C++: Mat cv::imdecode(Mat buf, int flags)
  216. [DllImport (LIBNAME)]
  217. private static extern IntPtr imgcodecs_Imgcodecs_imdecode_10 (IntPtr buf_nativeObj, int flags);
  218. // C++: Mat cv::imread(String filename, int flags = IMREAD_COLOR)
  219. [DllImport (LIBNAME)]
  220. private static extern IntPtr imgcodecs_Imgcodecs_imread_10 (string filename, int flags);
  221. [DllImport (LIBNAME)]
  222. private static extern IntPtr imgcodecs_Imgcodecs_imread_11 (string filename);
  223. // C++: bool cv::haveImageReader(String filename)
  224. [DllImport (LIBNAME)]
  225. private static extern bool imgcodecs_Imgcodecs_haveImageReader_10 (string filename);
  226. // C++: bool cv::haveImageWriter(String filename)
  227. [DllImport (LIBNAME)]
  228. private static extern bool imgcodecs_Imgcodecs_haveImageWriter_10 (string filename);
  229. // C++: bool cv::imencode(String ext, Mat img, vector_uchar& buf, vector_int _params = std::vector<int>())
  230. [DllImport (LIBNAME)]
  231. private static extern bool imgcodecs_Imgcodecs_imencode_10 (string ext, IntPtr img_nativeObj, IntPtr buf_mat_nativeObj, IntPtr _params_mat_nativeObj);
  232. [DllImport (LIBNAME)]
  233. private static extern bool imgcodecs_Imgcodecs_imencode_11 (string ext, IntPtr img_nativeObj, IntPtr buf_mat_nativeObj);
  234. // C++: bool cv::imreadmulti(String filename, vector_Mat& mats, int flags = IMREAD_ANYCOLOR)
  235. [DllImport (LIBNAME)]
  236. private static extern bool imgcodecs_Imgcodecs_imreadmulti_10 (string filename, IntPtr mats_mat_nativeObj, int flags);
  237. [DllImport (LIBNAME)]
  238. private static extern bool imgcodecs_Imgcodecs_imreadmulti_11 (string filename, IntPtr mats_mat_nativeObj);
  239. // C++: bool cv::imwrite(String filename, Mat img, vector_int _params = std::vector<int>())
  240. [DllImport (LIBNAME)]
  241. private static extern bool imgcodecs_Imgcodecs_imwrite_10 (string filename, IntPtr img_nativeObj, IntPtr _params_mat_nativeObj);
  242. [DllImport (LIBNAME)]
  243. private static extern bool imgcodecs_Imgcodecs_imwrite_11 (string filename, IntPtr img_nativeObj);
  244. }
  245. }