Imgcodecs.cs 9.9 KB

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