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