openvx_hal.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. #include "openvx_hal.hpp"
  2. #include "opencv2/imgproc/hal/interface.h"
  3. #define IVX_HIDE_INFO_WARNINGS
  4. #include "ivx.hpp"
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <cfloat>
  9. #include <climits>
  10. #include <cmath>
  11. #include <cstring>
  12. //==================================================================================================
  13. // utility
  14. // ...
  15. #if 0
  16. #include <cstdio>
  17. #define PRINT(...) printf(__VA_ARGS__)
  18. #define PRINT_HALERR_MSG(type) PRINT("OpenVX HAL impl "#type" error: %s\n", e.what())
  19. #else
  20. #define PRINT(...)
  21. #define PRINT_HALERR_MSG(type) (void)e
  22. #endif
  23. #if __cplusplus >= 201103L
  24. #include <chrono>
  25. struct Tick
  26. {
  27. typedef std::chrono::time_point<std::chrono::steady_clock> point_t;
  28. point_t start;
  29. point_t point;
  30. Tick()
  31. {
  32. start = std::chrono::steady_clock::now();
  33. point = std::chrono::steady_clock::now();
  34. }
  35. inline int one()
  36. {
  37. point_t old = point;
  38. point = std::chrono::steady_clock::now();
  39. return std::chrono::duration_cast<std::chrono::microseconds>(point - old).count();
  40. }
  41. inline int total()
  42. {
  43. return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start).count();
  44. }
  45. };
  46. #endif
  47. inline ivx::Context& getOpenVXHALContext()
  48. {
  49. #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
  50. //CXX11
  51. static thread_local ivx::Context instance = ivx::Context::create();
  52. #else //__cplusplus >= 201103L || _MSC_VER >= 1800
  53. //CXX98
  54. #ifdef _WIN32
  55. static __declspec(thread) ivx::Context instance = ivx::Context::create();
  56. #else
  57. static __thread ivx::Context instance = ivx::Context::create();
  58. #endif
  59. #endif
  60. return instance;
  61. }
  62. inline bool dimTooBig(int size)
  63. {
  64. static vx_uint16 current_vendor = getOpenVXHALContext().vendorID();
  65. if (current_vendor == VX_ID_KHRONOS || current_vendor == VX_ID_DEFAULT)
  66. {
  67. //OpenVX use uint32_t for image addressing
  68. return ((unsigned)size > (UINT_MAX / VX_SCALE_UNITY));
  69. }
  70. else
  71. return false;
  72. }
  73. //OpenVX calls have essential overhead so it make sense to skip them for small images
  74. template <int kernel_id> inline bool skipSmallImages(int w, int h) { return w*h < 7680 * 4320; }
  75. template <> inline bool skipSmallImages<VX_KERNEL_MULTIPLY>(int w, int h) { return w*h < 640 * 480; }
  76. template <> inline bool skipSmallImages<VX_KERNEL_COLOR_CONVERT>(int w, int h) { return w*h < 2048 * 1536; }
  77. template <> inline bool skipSmallImages<VX_KERNEL_INTEGRAL_IMAGE>(int w, int h) { return w*h < 640 * 480; }
  78. template <> inline bool skipSmallImages<VX_KERNEL_WARP_AFFINE>(int w, int h) { return w*h < 1280 * 720; }
  79. template <> inline bool skipSmallImages<VX_KERNEL_WARP_PERSPECTIVE>(int w, int h) { return w*h < 320 * 240; }
  80. template <> inline bool skipSmallImages<VX_KERNEL_CUSTOM_CONVOLUTION>(int w, int h) { return w*h < 320 * 240; }
  81. inline void setConstantBorder(ivx::border_t &border, vx_uint8 val)
  82. {
  83. border.mode = VX_BORDER_CONSTANT;
  84. #if VX_VERSION > VX_VERSION_1_0
  85. border.constant_value.U8 = val;
  86. #else
  87. border.constant_value = val;
  88. #endif
  89. }
  90. inline void refineStep(int w, int h, int imgType, size_t& step)
  91. {
  92. if (h == 1)
  93. step = w * ((imgType == VX_DF_IMAGE_RGBX ||
  94. imgType == VX_DF_IMAGE_U32 || imgType == VX_DF_IMAGE_S32) ? 4 :
  95. imgType == VX_DF_IMAGE_RGB ? 3 :
  96. (imgType == VX_DF_IMAGE_U16 || imgType == VX_DF_IMAGE_S16 ||
  97. imgType == VX_DF_IMAGE_UYVY || imgType == VX_DF_IMAGE_YUYV) ? 2 : 1);
  98. }
  99. //==================================================================================================
  100. // ivx::Image wrapped to simplify call to swapHandle prior to release
  101. // TODO update ivx::Image to handle swapHandle prior to release on the own
  102. class vxImage: public ivx::Image
  103. {
  104. public:
  105. vxImage(const ivx::Image &_img) : ivx::Image(_img) {}
  106. ~vxImage()
  107. {
  108. #if VX_VERSION > VX_VERSION_1_0
  109. swapHandle();
  110. #endif
  111. }
  112. };
  113. //==================================================================================================
  114. // real code starts here
  115. // ...
  116. #define OVX_BINARY_OP(hal_func, ovx_call, kernel_id) \
  117. template <typename T> \
  118. int ovx_hal_##hal_func(const T *a, size_t astep, const T *b, size_t bstep, T *c, size_t cstep, int w, int h) \
  119. { \
  120. if(skipSmallImages<kernel_id>(w, h)) \
  121. return CV_HAL_ERROR_NOT_IMPLEMENTED; \
  122. if(dimTooBig(w) || dimTooBig(h)) \
  123. return CV_HAL_ERROR_NOT_IMPLEMENTED; \
  124. refineStep(w, h, ivx::TypeToEnum<T>::imgType, astep); \
  125. refineStep(w, h, ivx::TypeToEnum<T>::imgType, bstep); \
  126. refineStep(w, h, ivx::TypeToEnum<T>::imgType, cstep); \
  127. try \
  128. { \
  129. ivx::Context ctx = getOpenVXHALContext(); \
  130. vxImage \
  131. ia = ivx::Image::createFromHandle(ctx, ivx::TypeToEnum<T>::imgType, \
  132. ivx::Image::createAddressing(w, h, sizeof(T), (vx_int32)(astep)), (void*)a), \
  133. ib = ivx::Image::createFromHandle(ctx, ivx::TypeToEnum<T>::imgType, \
  134. ivx::Image::createAddressing(w, h, sizeof(T), (vx_int32)(bstep)), (void*)b), \
  135. ic = ivx::Image::createFromHandle(ctx, ivx::TypeToEnum<T>::imgType, \
  136. ivx::Image::createAddressing(w, h, sizeof(T), (vx_int32)(cstep)), (void*)c); \
  137. ovx_call \
  138. } \
  139. catch (ivx::RuntimeError & e) \
  140. { \
  141. PRINT_HALERR_MSG(runtime); \
  142. return CV_HAL_ERROR_UNKNOWN; \
  143. } \
  144. catch (ivx::WrapperError & e) \
  145. { \
  146. PRINT_HALERR_MSG(wrapper); \
  147. return CV_HAL_ERROR_UNKNOWN; \
  148. } \
  149. return CV_HAL_ERROR_OK; \
  150. }
  151. OVX_BINARY_OP(add, { ivx::IVX_CHECK_STATUS(vxuAdd(ctx, ia, ib, VX_CONVERT_POLICY_SATURATE, ic)); }, VX_KERNEL_ADD)
  152. OVX_BINARY_OP(sub, { ivx::IVX_CHECK_STATUS(vxuSubtract(ctx, ia, ib, VX_CONVERT_POLICY_SATURATE, ic)); }, VX_KERNEL_SUBTRACT)
  153. OVX_BINARY_OP(absdiff, { ivx::IVX_CHECK_STATUS(vxuAbsDiff(ctx, ia, ib, ic)); }, VX_KERNEL_ABSDIFF)
  154. OVX_BINARY_OP(and, { ivx::IVX_CHECK_STATUS(vxuAnd(ctx, ia, ib, ic)); }, VX_KERNEL_AND)
  155. OVX_BINARY_OP(or , { ivx::IVX_CHECK_STATUS(vxuOr(ctx, ia, ib, ic)); }, VX_KERNEL_OR)
  156. OVX_BINARY_OP(xor, { ivx::IVX_CHECK_STATUS(vxuXor(ctx, ia, ib, ic)); }, VX_KERNEL_XOR)
  157. template <typename T>
  158. int ovx_hal_mul(const T *a, size_t astep, const T *b, size_t bstep, T *c, size_t cstep, int w, int h, double scale)
  159. {
  160. if(scale == 1.0 || sizeof(T) > 1 ?
  161. skipSmallImages<VX_KERNEL_ADD>(w, h) : /*actually it could be any kernel with generic minimum size*/
  162. skipSmallImages<VX_KERNEL_MULTIPLY>(w, h) )
  163. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  164. if (dimTooBig(w) || dimTooBig(h))
  165. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  166. refineStep(w, h, ivx::TypeToEnum<T>::imgType, astep);
  167. refineStep(w, h, ivx::TypeToEnum<T>::imgType, bstep);
  168. refineStep(w, h, ivx::TypeToEnum<T>::imgType, cstep);
  169. #ifdef _WIN32
  170. const float MAGIC_SCALE = 0x0.01010102p0;
  171. #else
  172. const float MAGIC_SCALE = 0x1.010102p-8;
  173. #endif
  174. try
  175. {
  176. int rounding_policy = VX_ROUND_POLICY_TO_ZERO;
  177. float fscale = (float)scale;
  178. if (fabs(fscale - MAGIC_SCALE) > FLT_EPSILON)
  179. {
  180. int exp = 0;
  181. double significand = frexp(fscale, &exp);
  182. if ((significand != 0.5) || (exp > 1) || (exp < -14))
  183. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  184. }
  185. else
  186. {
  187. fscale = MAGIC_SCALE;
  188. rounding_policy = VX_ROUND_POLICY_TO_NEAREST_EVEN;// That's the only rounding that MUST be supported for 1/255 scale
  189. }
  190. ivx::Context ctx = getOpenVXHALContext();
  191. vxImage
  192. ia = ivx::Image::createFromHandle(ctx, ivx::TypeToEnum<T>::imgType,
  193. ivx::Image::createAddressing(w, h, sizeof(T), (vx_int32)(astep)), (void*)a),
  194. ib = ivx::Image::createFromHandle(ctx, ivx::TypeToEnum<T>::imgType,
  195. ivx::Image::createAddressing(w, h, sizeof(T), (vx_int32)(bstep)), (void*)b),
  196. ic = ivx::Image::createFromHandle(ctx, ivx::TypeToEnum<T>::imgType,
  197. ivx::Image::createAddressing(w, h, sizeof(T), (vx_int32)(cstep)), (void*)c);
  198. ivx::IVX_CHECK_STATUS(vxuMultiply(ctx, ia, ib, fscale, VX_CONVERT_POLICY_SATURATE, rounding_policy, ic));
  199. }
  200. catch (ivx::RuntimeError & e)
  201. {
  202. PRINT_HALERR_MSG(runtime);
  203. return CV_HAL_ERROR_UNKNOWN;
  204. }
  205. catch (ivx::WrapperError & e)
  206. {
  207. PRINT_HALERR_MSG(wrapper);
  208. return CV_HAL_ERROR_UNKNOWN;
  209. }
  210. return CV_HAL_ERROR_OK;
  211. }
  212. template int ovx_hal_add<uchar>(const uchar *a, size_t astep, const uchar *b, size_t bstep, uchar *c, size_t cstep, int w, int h);
  213. template int ovx_hal_add<short>(const short *a, size_t astep, const short *b, size_t bstep, short *c, size_t cstep, int w, int h);
  214. template int ovx_hal_sub<uchar>(const uchar *a, size_t astep, const uchar *b, size_t bstep, uchar *c, size_t cstep, int w, int h);
  215. template int ovx_hal_sub<short>(const short *a, size_t astep, const short *b, size_t bstep, short *c, size_t cstep, int w, int h);
  216. template int ovx_hal_absdiff<uchar>(const uchar *a, size_t astep, const uchar *b, size_t bstep, uchar *c, size_t cstep, int w, int h);
  217. template int ovx_hal_absdiff<short>(const short *a, size_t astep, const short *b, size_t bstep, short *c, size_t cstep, int w, int h);
  218. template int ovx_hal_and<uchar>(const uchar *a, size_t astep, const uchar *b, size_t bstep, uchar *c, size_t cstep, int w, int h);
  219. template int ovx_hal_or<uchar>(const uchar *a, size_t astep, const uchar *b, size_t bstep, uchar *c, size_t cstep, int w, int h);
  220. template int ovx_hal_xor<uchar>(const uchar *a, size_t astep, const uchar *b, size_t bstep, uchar *c, size_t cstep, int w, int h);
  221. template int ovx_hal_mul<uchar>(const uchar *a, size_t astep, const uchar *b, size_t bstep, uchar *c, size_t cstep, int w, int h, double scale);
  222. template int ovx_hal_mul<short>(const short *a, size_t astep, const short *b, size_t bstep, short *c, size_t cstep, int w, int h, double scale);
  223. int ovx_hal_not(const uchar *a, size_t astep, uchar *c, size_t cstep, int w, int h)
  224. {
  225. if (skipSmallImages<VX_KERNEL_NOT>(w, h))
  226. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  227. if (dimTooBig(w) || dimTooBig(h))
  228. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  229. refineStep(w, h, VX_DF_IMAGE_U8, astep);
  230. refineStep(w, h, VX_DF_IMAGE_U8, cstep);
  231. try
  232. {
  233. ivx::Context ctx = getOpenVXHALContext();
  234. vxImage
  235. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  236. ivx::Image::createAddressing(w, h, 1, (vx_int32)(astep)), (void*)a),
  237. ic = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  238. ivx::Image::createAddressing(w, h, 1, (vx_int32)(cstep)), (void*)c);
  239. ivx::IVX_CHECK_STATUS(vxuNot(ctx, ia, ic));
  240. }
  241. catch (ivx::RuntimeError & e)
  242. {
  243. PRINT_HALERR_MSG(runtime);
  244. return CV_HAL_ERROR_UNKNOWN;
  245. }
  246. catch (ivx::WrapperError & e)
  247. {
  248. PRINT_HALERR_MSG(wrapper);
  249. return CV_HAL_ERROR_UNKNOWN;
  250. }
  251. return CV_HAL_ERROR_OK;
  252. }
  253. int ovx_hal_merge8u(const uchar **src_data, uchar *dst_data, int len, int cn)
  254. {
  255. if (skipSmallImages<VX_KERNEL_CHANNEL_COMBINE>(len, 1))
  256. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  257. if (dimTooBig(len))
  258. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  259. if (cn != 3 && cn != 4)
  260. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  261. try
  262. {
  263. ivx::Context ctx = getOpenVXHALContext();
  264. vxImage
  265. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  266. ivx::Image::createAddressing(len, 1, 1, (vx_int32)(len)), (void*)src_data[0]),
  267. ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  268. ivx::Image::createAddressing(len, 1, 1, (vx_int32)(len)), (void*)src_data[1]),
  269. ic = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  270. ivx::Image::createAddressing(len, 1, 1, (vx_int32)(len)), (void*)src_data[2]),
  271. id = ivx::Image::createFromHandle(ctx, cn == 4 ? VX_DF_IMAGE_RGBX : VX_DF_IMAGE_RGB,
  272. ivx::Image::createAddressing(len, 1, cn, (vx_int32)(len*cn)), (void*)dst_data);
  273. ivx::IVX_CHECK_STATUS(vxuChannelCombine(ctx, ia, ib, ic,
  274. cn == 4 ? (vx_image)(ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  275. ivx::Image::createAddressing(len, 1, 1, (vx_int32)(len)), (void*)src_data[3])) : NULL,
  276. id));
  277. }
  278. catch (ivx::RuntimeError & e)
  279. {
  280. PRINT_HALERR_MSG(runtime);
  281. return CV_HAL_ERROR_UNKNOWN;
  282. }
  283. catch (ivx::WrapperError & e)
  284. {
  285. PRINT_HALERR_MSG(wrapper);
  286. return CV_HAL_ERROR_UNKNOWN;
  287. }
  288. return CV_HAL_ERROR_OK;
  289. }
  290. int ovx_hal_resize(int atype, const uchar *a, size_t astep, int aw, int ah, uchar *b, size_t bstep, int bw, int bh, double inv_scale_x, double inv_scale_y, int interpolation)
  291. {
  292. if (skipSmallImages<VX_KERNEL_SCALE_IMAGE>(aw, ah))
  293. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  294. if (dimTooBig(aw) || dimTooBig(ah) || dimTooBig(bw) || dimTooBig(bh))
  295. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  296. refineStep(aw, ah, VX_DF_IMAGE_U8, astep);
  297. refineStep(bw, bh, VX_DF_IMAGE_U8, bstep);
  298. try
  299. {
  300. ivx::Context ctx = getOpenVXHALContext();
  301. vxImage
  302. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  303. ivx::Image::createAddressing(aw, ah, 1, (vx_int32)(astep)), (void*)a),
  304. ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  305. ivx::Image::createAddressing(bw, bh, 1, (vx_int32)(bstep)), (void*)b);
  306. if (!((atype == CV_8UC1 || atype == CV_8SC1) &&
  307. inv_scale_x > 0 && inv_scale_y > 0 &&
  308. (bw - 0.5) / inv_scale_x - 0.5 < aw && (bh - 0.5) / inv_scale_y - 0.5 < ah &&
  309. (bw + 0.5) / inv_scale_x + 0.5 >= aw && (bh + 0.5) / inv_scale_y + 0.5 >= ah &&
  310. std::abs(bw / inv_scale_x - aw) < 0.1 && std::abs(bh / inv_scale_y - ah) < 0.1))
  311. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  312. int mode;
  313. if (interpolation == CV_HAL_INTER_LINEAR)
  314. {
  315. mode = VX_INTERPOLATION_BILINEAR;
  316. if (inv_scale_x > 1 || inv_scale_y > 1)
  317. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  318. }
  319. else if (interpolation == CV_HAL_INTER_AREA)
  320. return CV_HAL_ERROR_NOT_IMPLEMENTED; //mode = VX_INTERPOLATION_AREA;
  321. else if (interpolation == CV_HAL_INTER_NEAREST)
  322. return CV_HAL_ERROR_NOT_IMPLEMENTED; //mode = VX_INTERPOLATION_NEAREST_NEIGHBOR;
  323. else
  324. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  325. ivx::IVX_CHECK_STATUS(vxuScaleImage(ctx, ia, ib, mode));
  326. }
  327. catch (ivx::RuntimeError & e)
  328. {
  329. PRINT_HALERR_MSG(runtime);
  330. return CV_HAL_ERROR_UNKNOWN;
  331. }
  332. catch (ivx::WrapperError & e)
  333. {
  334. PRINT_HALERR_MSG(wrapper);
  335. return CV_HAL_ERROR_UNKNOWN;
  336. }
  337. return CV_HAL_ERROR_OK;
  338. }
  339. int ovx_hal_warpAffine(int atype, const uchar *a, size_t astep, int aw, int ah, uchar *b, size_t bstep, int bw, int bh, const double M[6], int interpolation, int borderType, const double borderValue[4])
  340. {
  341. if (skipSmallImages<VX_KERNEL_WARP_AFFINE>(aw, ah))
  342. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  343. if (dimTooBig(aw) || dimTooBig(ah) || dimTooBig(bw) || dimTooBig(bh))
  344. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  345. refineStep(aw, ah, VX_DF_IMAGE_U8, astep);
  346. refineStep(bw, bh, VX_DF_IMAGE_U8, bstep);
  347. try
  348. {
  349. ivx::Context ctx = getOpenVXHALContext();
  350. vxImage
  351. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  352. ivx::Image::createAddressing(aw, ah, 1, (vx_int32)(astep)), (void*)a),
  353. ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  354. ivx::Image::createAddressing(bw, bh, 1, (vx_int32)(bstep)), (void*)b);
  355. if (!(atype == CV_8UC1 || atype == CV_8SC1))
  356. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  357. if(borderType != CV_HAL_BORDER_CONSTANT) // Neither 1.0 nor 1.1 OpenVX support BORDER_REPLICATE for warpings
  358. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  359. int mode;
  360. if (interpolation == CV_HAL_INTER_LINEAR)
  361. mode = VX_INTERPOLATION_BILINEAR;
  362. //AREA interpolation is unsupported
  363. //else if (interpolation == CV_HAL_INTER_AREA)
  364. // mode = VX_INTERPOLATION_AREA;
  365. else if (interpolation == CV_HAL_INTER_NEAREST)
  366. mode = VX_INTERPOLATION_NEAREST_NEIGHBOR;
  367. else
  368. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  369. std::vector<float> data;
  370. data.reserve(6);
  371. for (int j = 0; j < 3; ++j)
  372. for (int i = 0; i < 2; ++i)
  373. data.push_back((float)(M[i * 3 + j]));
  374. ivx::Matrix mtx = ivx::Matrix::create(ctx, VX_TYPE_FLOAT32, 2, 3);
  375. mtx.copyFrom(data);
  376. //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
  377. //since OpenVX standart says nothing about thread-safety for now
  378. ivx::border_t prevBorder = ctx.immediateBorder();
  379. ctx.setImmediateBorder(VX_BORDER_CONSTANT, (vx_uint8)borderValue[0]);
  380. ivx::IVX_CHECK_STATUS(vxuWarpAffine(ctx, ia, mtx, mode, ib));
  381. ctx.setImmediateBorder(prevBorder);
  382. }
  383. catch (ivx::RuntimeError & e)
  384. {
  385. PRINT_HALERR_MSG(runtime);
  386. return CV_HAL_ERROR_UNKNOWN;
  387. }
  388. catch (ivx::WrapperError & e)
  389. {
  390. PRINT_HALERR_MSG(wrapper);
  391. return CV_HAL_ERROR_UNKNOWN;
  392. }
  393. return CV_HAL_ERROR_OK;
  394. }
  395. int ovx_hal_warpPerspective(int atype, const uchar *a, size_t astep, int aw, int ah, uchar *b, size_t bstep, int bw, int bh, const double M[9], int interpolation, int borderType, const double borderValue[4])
  396. {
  397. if (skipSmallImages<VX_KERNEL_WARP_PERSPECTIVE>(aw, ah))
  398. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  399. if (dimTooBig(aw) || dimTooBig(ah) || dimTooBig(bw) || dimTooBig(bh))
  400. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  401. refineStep(aw, ah, VX_DF_IMAGE_U8, astep);
  402. refineStep(bw, bh, VX_DF_IMAGE_U8, bstep);
  403. try
  404. {
  405. ivx::Context ctx = getOpenVXHALContext();
  406. vxImage
  407. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  408. ivx::Image::createAddressing(aw, ah, 1, (vx_int32)(astep)), (void*)a),
  409. ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  410. ivx::Image::createAddressing(bw, bh, 1, (vx_int32)(bstep)), (void*)b);
  411. if (!(atype == CV_8UC1 || atype == CV_8SC1))
  412. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  413. if (borderType != CV_HAL_BORDER_CONSTANT) // Neither 1.0 nor 1.1 OpenVX support BORDER_REPLICATE for warpings
  414. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  415. int mode;
  416. if (interpolation == CV_HAL_INTER_LINEAR)
  417. mode = VX_INTERPOLATION_BILINEAR;
  418. //AREA interpolation is unsupported
  419. //else if (interpolation == CV_HAL_INTER_AREA)
  420. // mode = VX_INTERPOLATION_AREA;
  421. else if (interpolation == CV_HAL_INTER_NEAREST)
  422. mode = VX_INTERPOLATION_NEAREST_NEIGHBOR;
  423. else
  424. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  425. std::vector<float> data;
  426. data.reserve(9);
  427. for (int j = 0; j < 3; ++j)
  428. for (int i = 0; i < 3; ++i)
  429. data.push_back((float)(M[i * 3 + j]));
  430. ivx::Matrix mtx = ivx::Matrix::create(ctx, VX_TYPE_FLOAT32, 3, 3);
  431. mtx.copyFrom(data);
  432. //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
  433. //since OpenVX standart says nothing about thread-safety for now
  434. ivx::border_t prevBorder = ctx.immediateBorder();
  435. ctx.setImmediateBorder(VX_BORDER_CONSTANT, (vx_uint8)borderValue[0]);
  436. ivx::IVX_CHECK_STATUS(vxuWarpPerspective(ctx, ia, mtx, mode, ib));
  437. ctx.setImmediateBorder(prevBorder);
  438. }
  439. catch (ivx::RuntimeError & e)
  440. {
  441. PRINT_HALERR_MSG(runtime);
  442. return CV_HAL_ERROR_UNKNOWN;
  443. }
  444. catch (ivx::WrapperError & e)
  445. {
  446. PRINT_HALERR_MSG(wrapper);
  447. return CV_HAL_ERROR_UNKNOWN;
  448. }
  449. return CV_HAL_ERROR_OK;
  450. }
  451. struct cvhalFilter2D;
  452. struct FilterCtx
  453. {
  454. ivx::Convolution cnv;
  455. int dst_type;
  456. ivx::border_t border;
  457. FilterCtx(ivx::Context &ctx, const std::vector<short> data, int w, int h, int _dst_type, ivx::border_t & _border) :
  458. cnv(ivx::Convolution::create(ctx, w, h)), dst_type(_dst_type), border(_border) {
  459. cnv.copyFrom(data);
  460. }
  461. };
  462. int ovx_hal_filterInit(cvhalFilter2D **filter_context, uchar *kernel_data, size_t kernel_step, int kernel_type, int kernel_width, int kernel_height,
  463. int, int, int src_type, int dst_type, int borderType, double delta, int anchor_x, int anchor_y, bool allowSubmatrix, bool allowInplace)
  464. {
  465. if (!filter_context || !kernel_data || allowSubmatrix || allowInplace || delta != 0 ||
  466. src_type != CV_8UC1 || (dst_type != CV_8UC1 && dst_type != CV_16SC1) ||
  467. kernel_width % 2 == 0 || kernel_height % 2 == 0 || anchor_x != kernel_width / 2 || anchor_y != kernel_height / 2)
  468. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  469. ivx::border_t border;
  470. switch (borderType)
  471. {
  472. case CV_HAL_BORDER_CONSTANT:
  473. setConstantBorder(border, 0);
  474. break;
  475. case CV_HAL_BORDER_REPLICATE:
  476. border.mode = VX_BORDER_REPLICATE;
  477. break;
  478. default:
  479. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  480. }
  481. ivx::Context ctx = getOpenVXHALContext();
  482. std::vector<short> data;
  483. data.reserve(kernel_width*kernel_height);
  484. switch (kernel_type)
  485. {
  486. case CV_8UC1:
  487. for (int j = 0; j < kernel_height; ++j)
  488. {
  489. uchar * row = (uchar*)(kernel_data + kernel_step*j);
  490. for (int i = 0; i < kernel_width; ++i)
  491. data.push_back(row[i]);
  492. }
  493. break;
  494. case CV_8SC1:
  495. for (int j = 0; j < kernel_height; ++j)
  496. {
  497. schar * row = (schar*)(kernel_data + kernel_step*j);
  498. for (int i = 0; i < kernel_width; ++i)
  499. data.push_back(row[i]);
  500. }
  501. break;
  502. case CV_16SC1:
  503. for (int j = 0; j < kernel_height; ++j)
  504. {
  505. short * row = (short*)(kernel_data + kernel_step*j);
  506. for (int i = 0; i < kernel_width; ++i)
  507. data.push_back(row[i]);
  508. }
  509. break;
  510. default:
  511. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  512. }
  513. FilterCtx* cnv = new FilterCtx(ctx, data, kernel_width, kernel_height, dst_type, border);
  514. if (!cnv)
  515. return CV_HAL_ERROR_UNKNOWN;
  516. *filter_context = (cvhalFilter2D*)(cnv);
  517. return CV_HAL_ERROR_OK;
  518. }
  519. int ovx_hal_filterFree(cvhalFilter2D *filter_context)
  520. {
  521. if (filter_context)
  522. {
  523. delete (FilterCtx*)filter_context;
  524. return CV_HAL_ERROR_OK;
  525. }
  526. else
  527. {
  528. return CV_HAL_ERROR_UNKNOWN;
  529. }
  530. }
  531. int ovx_hal_filter(cvhalFilter2D *filter_context, uchar *a, size_t astep, uchar *b, size_t bstep, int w, int h, int, int, int, int)
  532. {
  533. if (skipSmallImages<VX_KERNEL_CUSTOM_CONVOLUTION>(w, h))
  534. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  535. if (dimTooBig(w) || dimTooBig(h))
  536. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  537. try
  538. {
  539. FilterCtx* cnv = (FilterCtx*)filter_context;
  540. if (!cnv)
  541. throw ivx::WrapperError("Bad HAL context");
  542. refineStep(w, h, VX_DF_IMAGE_U8, astep);
  543. refineStep(w, h, cnv->dst_type == CV_16SC1 ? VX_DF_IMAGE_S16 : VX_DF_IMAGE_U8, bstep);
  544. ivx::Context ctx = getOpenVXHALContext();
  545. vxImage
  546. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  547. ivx::Image::createAddressing(w, h, 1, (vx_int32)(astep)), (void*)a),
  548. ib = ivx::Image::createFromHandle(ctx, cnv->dst_type == CV_16SC1 ? VX_DF_IMAGE_S16 : VX_DF_IMAGE_U8,
  549. ivx::Image::createAddressing(w, h, cnv->dst_type == CV_16SC1 ? 2 : 1, (vx_int32)(bstep)), (void*)b);
  550. //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
  551. //since OpenVX standart says nothing about thread-safety for now
  552. ivx::border_t prevBorder = ctx.immediateBorder();
  553. ctx.setImmediateBorder(cnv->border);
  554. ivx::IVX_CHECK_STATUS(vxuConvolve(ctx, ia, cnv->cnv, ib));
  555. ctx.setImmediateBorder(prevBorder);
  556. }
  557. catch (ivx::RuntimeError & e)
  558. {
  559. PRINT_HALERR_MSG(runtime);
  560. return CV_HAL_ERROR_UNKNOWN;
  561. }
  562. catch (ivx::WrapperError & e)
  563. {
  564. PRINT_HALERR_MSG(wrapper);
  565. return CV_HAL_ERROR_UNKNOWN;
  566. }
  567. return CV_HAL_ERROR_OK;
  568. }
  569. int ovx_hal_sepFilterInit(cvhalFilter2D **filter_context, int src_type, int dst_type,
  570. int kernel_type, uchar *kernelx_data, int kernelx_length, uchar *kernely_data, int kernely_length,
  571. int anchor_x, int anchor_y, double delta, int borderType)
  572. {
  573. if (!filter_context || !kernelx_data || !kernely_data || delta != 0 ||
  574. src_type != CV_8UC1 || (dst_type != CV_8UC1 && dst_type != CV_16SC1) ||
  575. kernelx_length != 3 || kernely_length != 3 || anchor_x != 1 || anchor_y != 1)
  576. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  577. ivx::border_t border;
  578. switch (borderType)
  579. {
  580. case CV_HAL_BORDER_CONSTANT:
  581. setConstantBorder(border, 0);
  582. break;
  583. case CV_HAL_BORDER_REPLICATE:
  584. border.mode = VX_BORDER_REPLICATE;
  585. break;
  586. default:
  587. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  588. }
  589. ivx::Context ctx = getOpenVXHALContext();
  590. //At the moment OpenVX doesn't support separable filters natively so combine kernels to generic convolution
  591. std::vector<short> data;
  592. data.reserve(kernelx_length*kernely_length);
  593. switch (kernel_type)
  594. {
  595. case CV_8UC1:
  596. for (int j = 0; j < kernely_length; ++j)
  597. for (int i = 0; i < kernelx_length; ++i)
  598. data.push_back((short)(kernely_data[j]) * kernelx_data[i]);
  599. break;
  600. case CV_8SC1:
  601. for (int j = 0; j < kernely_length; ++j)
  602. for (int i = 0; i < kernelx_length; ++i)
  603. data.push_back((short)(((schar*)kernely_data)[j]) * ((schar*)kernelx_data)[i]);
  604. break;
  605. default:
  606. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  607. }
  608. FilterCtx* cnv = new FilterCtx(ctx, data, kernelx_length, kernely_length, dst_type, border);
  609. if (!cnv)
  610. return CV_HAL_ERROR_UNKNOWN;
  611. *filter_context = (cvhalFilter2D*)(cnv);
  612. return CV_HAL_ERROR_OK;
  613. }
  614. #if VX_VERSION > VX_VERSION_1_0
  615. struct MorphCtx
  616. {
  617. ivx::Matrix mask;
  618. int operation;
  619. ivx::border_t border;
  620. MorphCtx(ivx::Context &ctx, const std::vector<vx_uint8> data, int w, int h, int _operation, ivx::border_t & _border) :
  621. mask(ivx::Matrix::create(ctx, ivx::TypeToEnum<vx_uint8>::value, w, h)), operation(_operation), border(_border) {
  622. mask.copyFrom(data);
  623. }
  624. };
  625. int ovx_hal_morphInit(cvhalFilter2D **filter_context, int operation, int src_type, int dst_type, int, int,
  626. int kernel_type, uchar *kernel_data, size_t kernel_step, int kernel_width, int kernel_height, int anchor_x, int anchor_y,
  627. int borderType, const double borderValue[4], int iterations, bool allowSubmatrix, bool allowInplace)
  628. {
  629. if (!filter_context || !kernel_data || allowSubmatrix || allowInplace || iterations != 1 ||
  630. src_type != CV_8UC1 || dst_type != CV_8UC1 ||
  631. kernel_width % 2 == 0 || kernel_height % 2 == 0 || anchor_x != kernel_width / 2 || anchor_y != kernel_height / 2)
  632. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  633. ivx::border_t border;
  634. switch (borderType)
  635. {
  636. case CV_HAL_BORDER_CONSTANT:
  637. if (borderValue[0] == DBL_MAX && borderValue[1] == DBL_MAX && borderValue[2] == DBL_MAX && borderValue[3] == DBL_MAX)
  638. {
  639. if (operation == CV_HAL_MORPH_ERODE)
  640. setConstantBorder(border, UCHAR_MAX);
  641. else
  642. setConstantBorder(border, 0);
  643. }
  644. else
  645. {
  646. int rounded = (int)round(borderValue[0]);
  647. setConstantBorder(border, (vx_uint8)((unsigned)rounded <= UCHAR_MAX ? rounded : rounded > 0 ? UCHAR_MAX : 0));
  648. }
  649. break;
  650. case CV_HAL_BORDER_REPLICATE:
  651. border.mode = VX_BORDER_REPLICATE;
  652. break;
  653. default:
  654. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  655. }
  656. ivx::Context ctx = getOpenVXHALContext();
  657. vx_size maxKernelDim = ctx.nonlinearMaxDimension();
  658. if ((vx_size)kernel_width > maxKernelDim || (vx_size)kernel_height > maxKernelDim)
  659. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  660. std::vector<vx_uint8> kernel_mat;
  661. kernel_mat.reserve(kernel_width * kernel_height);
  662. switch (CV_MAT_DEPTH(kernel_type))
  663. {
  664. case CV_8U:
  665. case CV_8S:
  666. for (int j = 0; j < kernel_height; ++j)
  667. {
  668. uchar * kernel_row = kernel_data + j * kernel_step;
  669. for (int i = 0; i < kernel_width; ++i)
  670. kernel_mat.push_back(kernel_row[i] ? 255 : 0);
  671. }
  672. break;
  673. case CV_16U:
  674. case CV_16S:
  675. for (int j = 0; j < kernel_height; ++j)
  676. {
  677. short * kernel_row = (short*)(kernel_data + j * kernel_step);
  678. for (int i = 0; i < kernel_width; ++i)
  679. kernel_mat.push_back(kernel_row[i] ? 255 : 0);
  680. }
  681. break;
  682. case CV_32S:
  683. for (int j = 0; j < kernel_height; ++j)
  684. {
  685. int * kernel_row = (int*)(kernel_data + j * kernel_step);
  686. for (int i = 0; i < kernel_width; ++i)
  687. kernel_mat.push_back(kernel_row[i] ? 255 : 0);
  688. }
  689. break;
  690. case CV_32F:
  691. for (int j = 0; j < kernel_height; ++j)
  692. {
  693. float * kernel_row = (float*)(kernel_data + j * kernel_step);
  694. for (int i = 0; i < kernel_width; ++i)
  695. kernel_mat.push_back(kernel_row[i] ? 255 : 0);
  696. }
  697. break;
  698. case CV_64F:
  699. for (int j = 0; j < kernel_height; ++j)
  700. {
  701. double * kernel_row = (double*)(kernel_data + j * kernel_step);
  702. for (int i = 0; i < kernel_width; ++i)
  703. kernel_mat.push_back(kernel_row[i] ? 255 : 0);
  704. }
  705. break;
  706. default:
  707. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  708. }
  709. MorphCtx* mat;
  710. switch (operation)
  711. {
  712. case CV_HAL_MORPH_ERODE:
  713. mat = new MorphCtx(ctx, kernel_mat, kernel_width, kernel_height, VX_NONLINEAR_FILTER_MIN, border);
  714. break;
  715. case CV_HAL_MORPH_DILATE:
  716. mat = new MorphCtx(ctx, kernel_mat, kernel_width, kernel_height, VX_NONLINEAR_FILTER_MAX, border);
  717. break;
  718. default:
  719. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  720. }
  721. if (!mat)
  722. return CV_HAL_ERROR_UNKNOWN;
  723. *filter_context = (cvhalFilter2D*)(mat);
  724. return CV_HAL_ERROR_OK;
  725. }
  726. int ovx_hal_morphFree(cvhalFilter2D *filter_context)
  727. {
  728. if (filter_context)
  729. {
  730. delete (MorphCtx*)filter_context;
  731. return CV_HAL_ERROR_OK;
  732. }
  733. else
  734. {
  735. return CV_HAL_ERROR_UNKNOWN;
  736. }
  737. }
  738. int ovx_hal_morph(cvhalFilter2D *filter_context, uchar *a, size_t astep, uchar *b, size_t bstep, int w, int h, int, int, int, int, int, int, int, int)
  739. {
  740. if (skipSmallImages<VX_KERNEL_DILATE_3x3>(w, h))//Actually it make sense to separate checks if implementations of dilation and erosion have different performance gain
  741. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  742. if (dimTooBig(w) || dimTooBig(h))
  743. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  744. refineStep(w, h, VX_DF_IMAGE_U8, astep);
  745. refineStep(w, h, VX_DF_IMAGE_U8, bstep);
  746. try
  747. {
  748. MorphCtx* mat = (MorphCtx*)filter_context;
  749. if (!mat)
  750. throw ivx::WrapperError("Bad HAL context");
  751. ivx::Context ctx = getOpenVXHALContext();
  752. vxImage
  753. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  754. ivx::Image::createAddressing(w, h, 1, (vx_int32)(astep)), (void*)a),
  755. ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  756. ivx::Image::createAddressing(w, h, 1, (vx_int32)(bstep)), (void*)b);
  757. //ATTENTION: VX_CONTEXT_IMMEDIATE_BORDER attribute change could lead to strange issues in multi-threaded environments
  758. //since OpenVX standart says nothing about thread-safety for now
  759. ivx::border_t prevBorder = ctx.immediateBorder();
  760. ctx.setImmediateBorder(mat->border);
  761. ivx::IVX_CHECK_STATUS(vxuNonLinearFilter(ctx, mat->operation, ia, mat->mask, ib));
  762. ctx.setImmediateBorder(prevBorder);
  763. }
  764. catch (ivx::RuntimeError & e)
  765. {
  766. PRINT_HALERR_MSG(runtime);
  767. return CV_HAL_ERROR_UNKNOWN;
  768. }
  769. catch (ivx::WrapperError & e)
  770. {
  771. PRINT_HALERR_MSG(wrapper);
  772. return CV_HAL_ERROR_UNKNOWN;
  773. }
  774. return CV_HAL_ERROR_OK;
  775. }
  776. #endif // 1.0 guard
  777. int ovx_hal_cvtBGRtoBGR(const uchar * a, size_t astep, uchar * b, size_t bstep, int w, int h, int depth, int acn, int bcn, bool swapBlue)
  778. {
  779. if (skipSmallImages<VX_KERNEL_COLOR_CONVERT>(w, h))
  780. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  781. if (dimTooBig(w) || dimTooBig(h))
  782. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  783. if (depth != CV_8U || swapBlue || acn == bcn || (acn != 3 && acn != 4) || (bcn != 3 && bcn != 4))
  784. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  785. if (w & 1 || h & 1) // It's strange but sample implementation unable to convert odd sized images
  786. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  787. refineStep(w, h, acn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX, astep);
  788. refineStep(w, h, bcn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX, bstep);
  789. try
  790. {
  791. ivx::Context ctx = getOpenVXHALContext();
  792. vxImage
  793. ia = ivx::Image::createFromHandle(ctx, acn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX,
  794. ivx::Image::createAddressing(w, h, acn, (vx_int32)astep), (void*)a),
  795. ib = ivx::Image::createFromHandle(ctx, bcn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX,
  796. ivx::Image::createAddressing(w, h, bcn, (vx_int32)bstep), b);
  797. ivx::IVX_CHECK_STATUS(vxuColorConvert(ctx, ia, ib));
  798. }
  799. catch (ivx::RuntimeError & e)
  800. {
  801. PRINT_HALERR_MSG(runtime);
  802. return CV_HAL_ERROR_UNKNOWN;
  803. }
  804. catch (ivx::WrapperError & e)
  805. {
  806. PRINT_HALERR_MSG(wrapper);
  807. return CV_HAL_ERROR_UNKNOWN;
  808. }
  809. return CV_HAL_ERROR_OK;
  810. }
  811. int ovx_hal_cvtGraytoBGR(const uchar * a, size_t astep, uchar * b, size_t bstep, int w, int h, int depth, int bcn)
  812. {
  813. if (skipSmallImages<VX_KERNEL_CHANNEL_COMBINE>(w, h))
  814. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  815. if (dimTooBig(w) || dimTooBig(h))
  816. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  817. if (depth != CV_8U || (bcn != 3 && bcn != 4))
  818. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  819. refineStep(w, h, VX_DF_IMAGE_U8, astep);
  820. refineStep(w, h, bcn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX, bstep);
  821. try
  822. {
  823. ivx::Context ctx = getOpenVXHALContext();
  824. ivx::Image
  825. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  826. ivx::Image::createAddressing(w, h, 1, (vx_int32)astep), const_cast<uchar*>(a)),
  827. ib = ivx::Image::createFromHandle(ctx, bcn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX,
  828. ivx::Image::createAddressing(w, h, bcn, (vx_int32)bstep), b);
  829. ivx::IVX_CHECK_STATUS(vxuChannelCombine(ctx, ia, ia, ia,
  830. bcn == 4 ? (vx_image)(ivx::Image::createUniform(ctx, w, h, VX_DF_IMAGE_U8, vx_uint8(255))) : NULL,
  831. ib));
  832. }
  833. catch (ivx::RuntimeError & e)
  834. {
  835. PRINT_HALERR_MSG(runtime);
  836. return CV_HAL_ERROR_UNKNOWN;
  837. }
  838. catch (ivx::WrapperError & e)
  839. {
  840. PRINT_HALERR_MSG(wrapper);
  841. return CV_HAL_ERROR_UNKNOWN;
  842. }
  843. return CV_HAL_ERROR_OK;
  844. }
  845. int ovx_hal_cvtTwoPlaneYUVtoBGR(const uchar * a, size_t astep, uchar * b, size_t bstep, int w, int h, int bcn, bool swapBlue, int uIdx)
  846. {
  847. return ovx_hal_cvtTwoPlaneYUVtoBGREx(a, astep, a + h * astep, astep, b, bstep, w, h, bcn, swapBlue, uIdx);
  848. }
  849. int ovx_hal_cvtTwoPlaneYUVtoBGREx(const uchar * a, size_t astep, const uchar * b, size_t bstep, uchar * c, size_t cstep, int w, int h, int bcn, bool swapBlue, int uIdx)
  850. {
  851. if (skipSmallImages<VX_KERNEL_COLOR_CONVERT>(w, h))
  852. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  853. if (dimTooBig(w) || dimTooBig(h))
  854. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  855. if (!swapBlue || (bcn != 3 && bcn != 4))
  856. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  857. if (w & 1 || h & 1) // It's not described in spec but sample implementation unable to convert odd sized images
  858. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  859. try
  860. {
  861. ivx::Context ctx = getOpenVXHALContext();
  862. std::vector<vx_imagepatch_addressing_t> addr;
  863. std::vector<void *> ptrs;
  864. addr.push_back(ivx::Image::createAddressing(w, h, 1, (vx_int32)astep));
  865. ptrs.push_back((void*)a);
  866. addr.push_back(ivx::Image::createAddressing(w / 2, h / 2, 2, (vx_int32)bstep));
  867. ptrs.push_back((void*)b);
  868. vxImage
  869. ia = ivx::Image::createFromHandle(ctx, uIdx ? VX_DF_IMAGE_NV21 : VX_DF_IMAGE_NV12, addr, ptrs);
  870. if (ia.range() == VX_CHANNEL_RANGE_FULL)
  871. return CV_HAL_ERROR_NOT_IMPLEMENTED; // OpenCV store NV12/NV21 as RANGE_RESTRICTED while OpenVX expect RANGE_FULL
  872. vxImage
  873. ib = ivx::Image::createFromHandle(ctx, bcn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX,
  874. ivx::Image::createAddressing(w, h, bcn, (vx_int32)cstep), c);
  875. ivx::IVX_CHECK_STATUS(vxuColorConvert(ctx, ia, ib));
  876. }
  877. catch (ivx::RuntimeError & e)
  878. {
  879. PRINT_HALERR_MSG(runtime);
  880. return CV_HAL_ERROR_UNKNOWN;
  881. }
  882. catch (ivx::WrapperError & e)
  883. {
  884. PRINT_HALERR_MSG(wrapper);
  885. return CV_HAL_ERROR_UNKNOWN;
  886. }
  887. return CV_HAL_ERROR_OK;
  888. }
  889. int ovx_hal_cvtThreePlaneYUVtoBGR(const uchar * a, size_t astep, uchar * b, size_t bstep, int w, int h, int bcn, bool swapBlue, int uIdx)
  890. {
  891. if (skipSmallImages<VX_KERNEL_COLOR_CONVERT>(w, h))
  892. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  893. if (dimTooBig(w) || dimTooBig(h))
  894. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  895. if (!swapBlue || (bcn != 3 && bcn != 4) || uIdx || (size_t)w / 2 != astep - (size_t)w / 2)
  896. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  897. if (w & 1 || h & 1) // It's not described in spec but sample implementation unable to convert odd sized images
  898. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  899. refineStep(w, h, VX_DF_IMAGE_IYUV, astep);
  900. refineStep(w, h, bcn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX, bstep);
  901. try
  902. {
  903. ivx::Context ctx = getOpenVXHALContext();
  904. std::vector<vx_imagepatch_addressing_t> addr;
  905. std::vector<void *> ptrs;
  906. addr.push_back(ivx::Image::createAddressing(w, h, 1, (vx_int32)astep));
  907. ptrs.push_back((void*)a);
  908. addr.push_back(ivx::Image::createAddressing(w / 2, h / 2, 1, w / 2));
  909. ptrs.push_back((void*)(a + h * astep));
  910. if (addr[1].dim_x != (astep - addr[1].dim_x))
  911. throw ivx::WrapperError("UV planes use variable stride");
  912. addr.push_back(ivx::Image::createAddressing(w / 2, h / 2, 1, w / 2));
  913. ptrs.push_back((void*)(a + h * astep + addr[1].dim_y * addr[1].stride_y));
  914. vxImage
  915. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_IYUV, addr, ptrs);
  916. if (ia.range() == VX_CHANNEL_RANGE_FULL)
  917. return CV_HAL_ERROR_NOT_IMPLEMENTED; // OpenCV store NV12/NV21 as RANGE_RESTRICTED while OpenVX expect RANGE_FULL
  918. vxImage
  919. ib = ivx::Image::createFromHandle(ctx, bcn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX,
  920. ivx::Image::createAddressing(w, h, bcn, (vx_int32)bstep), b);
  921. ivx::IVX_CHECK_STATUS(vxuColorConvert(ctx, ia, ib));
  922. }
  923. catch (ivx::RuntimeError & e)
  924. {
  925. PRINT_HALERR_MSG(runtime);
  926. return CV_HAL_ERROR_UNKNOWN;
  927. }
  928. catch (ivx::WrapperError & e)
  929. {
  930. PRINT_HALERR_MSG(wrapper);
  931. return CV_HAL_ERROR_UNKNOWN;
  932. }
  933. return CV_HAL_ERROR_OK;
  934. }
  935. int ovx_hal_cvtBGRtoThreePlaneYUV(const uchar * a, size_t astep, uchar * b, size_t bstep, int w, int h, int acn, bool swapBlue, int uIdx)
  936. {
  937. if (skipSmallImages<VX_KERNEL_COLOR_CONVERT>(w, h))
  938. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  939. if (dimTooBig(w) || dimTooBig(h))
  940. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  941. if (!swapBlue || (acn != 3 && acn != 4) || uIdx || (size_t)w / 2 != bstep - (size_t)w / 2)
  942. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  943. if (w & 1 || h & 1) // It's not described in spec but sample implementation unable to convert odd sized images
  944. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  945. refineStep(w, h, acn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX, astep);
  946. refineStep(w, h, VX_DF_IMAGE_IYUV, bstep);
  947. try
  948. {
  949. ivx::Context ctx = getOpenVXHALContext();
  950. vxImage
  951. ia = ivx::Image::createFromHandle(ctx, acn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX,
  952. ivx::Image::createAddressing(w, h, acn, (vx_int32)astep), (void*)a);
  953. std::vector<vx_imagepatch_addressing_t> addr;
  954. std::vector<void *> ptrs;
  955. addr.push_back(ivx::Image::createAddressing(w, h, 1, (vx_int32)bstep));
  956. ptrs.push_back((void*)b);
  957. addr.push_back(ivx::Image::createAddressing(w / 2, h / 2, 1, w / 2));
  958. ptrs.push_back((void*)(b + h * bstep));
  959. if (addr[1].dim_x != (bstep - addr[1].dim_x))
  960. throw ivx::WrapperError("UV planes use variable stride");
  961. addr.push_back(ivx::Image::createAddressing(w / 2, h / 2, 1, w / 2));
  962. ptrs.push_back((void*)(b + h * bstep + addr[1].dim_y * addr[1].stride_y));
  963. vxImage
  964. ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_IYUV, addr, ptrs);
  965. ivx::IVX_CHECK_STATUS(vxuColorConvert(ctx, ia, ib));
  966. }
  967. catch (ivx::RuntimeError & e)
  968. {
  969. PRINT_HALERR_MSG(runtime);
  970. return CV_HAL_ERROR_UNKNOWN;
  971. }
  972. catch (ivx::WrapperError & e)
  973. {
  974. PRINT_HALERR_MSG(wrapper);
  975. return CV_HAL_ERROR_UNKNOWN;
  976. }
  977. return CV_HAL_ERROR_OK;
  978. }
  979. int ovx_hal_cvtOnePlaneYUVtoBGR(const uchar * a, size_t astep, uchar * b, size_t bstep, int w, int h, int bcn, bool swapBlue, int uIdx, int ycn)
  980. {
  981. if (skipSmallImages<VX_KERNEL_COLOR_CONVERT>(w, h))
  982. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  983. if (dimTooBig(w) || dimTooBig(h))
  984. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  985. if (!swapBlue || (bcn != 3 && bcn != 4) || uIdx)
  986. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  987. if (w & 1) // It's not described in spec but sample implementation unable to convert odd sized images
  988. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  989. refineStep(w, h, ycn ? VX_DF_IMAGE_UYVY : VX_DF_IMAGE_YUYV, astep);
  990. refineStep(w, h, bcn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX, bstep);
  991. try
  992. {
  993. ivx::Context ctx = getOpenVXHALContext();
  994. vxImage
  995. ia = ivx::Image::createFromHandle(ctx, ycn ? VX_DF_IMAGE_UYVY : VX_DF_IMAGE_YUYV,
  996. ivx::Image::createAddressing(w, h, 2, (vx_int32)astep), (void*)a);
  997. if (ia.range() == VX_CHANNEL_RANGE_FULL)
  998. return CV_HAL_ERROR_NOT_IMPLEMENTED; // OpenCV store NV12/NV21 as RANGE_RESTRICTED while OpenVX expect RANGE_FULL
  999. vxImage
  1000. ib = ivx::Image::createFromHandle(ctx, bcn == 3 ? VX_DF_IMAGE_RGB : VX_DF_IMAGE_RGBX,
  1001. ivx::Image::createAddressing(w, h, bcn, (vx_int32)bstep), b);
  1002. ivx::IVX_CHECK_STATUS(vxuColorConvert(ctx, ia, ib));
  1003. }
  1004. catch (ivx::RuntimeError & e)
  1005. {
  1006. PRINT_HALERR_MSG(runtime);
  1007. return CV_HAL_ERROR_UNKNOWN;
  1008. }
  1009. catch (ivx::WrapperError & e)
  1010. {
  1011. PRINT_HALERR_MSG(wrapper);
  1012. return CV_HAL_ERROR_UNKNOWN;
  1013. }
  1014. return CV_HAL_ERROR_OK;
  1015. }
  1016. int ovx_hal_integral(int depth, int sdepth, int, const uchar * a, size_t astep, uchar * b, size_t bstep, uchar * c, size_t, uchar * d, size_t, int w, int h, int cn)
  1017. {
  1018. if (skipSmallImages<VX_KERNEL_INTEGRAL_IMAGE>(w, h))
  1019. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  1020. if (depth != CV_8U || sdepth != CV_32S || c != NULL || d != NULL || cn != 1)
  1021. return CV_HAL_ERROR_NOT_IMPLEMENTED;
  1022. refineStep(w, h, VX_DF_IMAGE_U8, astep);
  1023. try
  1024. {
  1025. ivx::Context ctx = getOpenVXHALContext();
  1026. ivx::Image
  1027. ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
  1028. ivx::Image::createAddressing(w, h, 1, (vx_int32)astep), const_cast<uchar*>(a)),
  1029. ib = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U32,
  1030. ivx::Image::createAddressing(w, h, 4, (vx_int32)bstep), (unsigned int *)(b + bstep + sizeof(unsigned int)));
  1031. ivx::IVX_CHECK_STATUS(vxuIntegralImage(ctx, ia, ib));
  1032. std::memset(b, 0, (w + 1) * sizeof(unsigned int));
  1033. b += bstep;
  1034. for (int i = 0; i < h; i++, b += bstep)
  1035. {
  1036. *((unsigned int*)b) = 0;
  1037. }
  1038. }
  1039. catch (ivx::RuntimeError & e)
  1040. {
  1041. PRINT_HALERR_MSG(runtime);
  1042. return CV_HAL_ERROR_UNKNOWN;
  1043. }
  1044. catch (ivx::WrapperError & e)
  1045. {
  1046. PRINT_HALERR_MSG(wrapper);
  1047. return CV_HAL_ERROR_UNKNOWN;
  1048. }
  1049. return CV_HAL_ERROR_OK;
  1050. }