1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "precomp.hpp"
44 #include "opencl_kernels.hpp"
49 /****************************************************************************************\
51 \****************************************************************************************/
54 template<typename T> struct VSplit2;
55 template<typename T> struct VSplit3;
56 template<typename T> struct VSplit4;
58 #define SPLIT2_KERNEL_TEMPLATE(name, data_type, reg_type, load_func, store_func) \
60 struct name<data_type>{ \
61 void operator()(const data_type* src, data_type* dst0, data_type* dst1){ \
62 reg_type r = load_func(src); \
63 store_func(dst0, r.val[0]); \
64 store_func(dst1, r.val[1]); \
68 #define SPLIT3_KERNEL_TEMPLATE(name, data_type, reg_type, load_func, store_func) \
70 struct name<data_type>{ \
71 void operator()(const data_type* src, data_type* dst0, data_type* dst1, \
73 reg_type r = load_func(src); \
74 store_func(dst0, r.val[0]); \
75 store_func(dst1, r.val[1]); \
76 store_func(dst2, r.val[2]); \
80 #define SPLIT4_KERNEL_TEMPLATE(name, data_type, reg_type, load_func, store_func) \
82 struct name<data_type>{ \
83 void operator()(const data_type* src, data_type* dst0, data_type* dst1, \
84 data_type* dst2, data_type* dst3){ \
85 reg_type r = load_func(src); \
86 store_func(dst0, r.val[0]); \
87 store_func(dst1, r.val[1]); \
88 store_func(dst2, r.val[2]); \
89 store_func(dst3, r.val[3]); \
93 SPLIT2_KERNEL_TEMPLATE(VSplit2, uchar , uint8x16x2_t, vld2q_u8 , vst1q_u8 );
94 SPLIT2_KERNEL_TEMPLATE(VSplit2, schar , int8x16x2_t, vld2q_s8 , vst1q_s8 );
95 SPLIT2_KERNEL_TEMPLATE(VSplit2, ushort, uint16x8x2_t, vld2q_u16, vst1q_u16);
96 SPLIT2_KERNEL_TEMPLATE(VSplit2, short , int16x8x2_t, vld2q_s16, vst1q_s16);
97 SPLIT2_KERNEL_TEMPLATE(VSplit2, int , int32x4x2_t, vld2q_s32, vst1q_s32);
98 SPLIT2_KERNEL_TEMPLATE(VSplit2, float , float32x4x2_t, vld2q_f32, vst1q_f32);
99 SPLIT2_KERNEL_TEMPLATE(VSplit2, int64 , int64x1x2_t, vld2_s64 , vst1_s64 );
101 SPLIT3_KERNEL_TEMPLATE(VSplit3, uchar , uint8x16x3_t, vld3q_u8 , vst1q_u8 );
102 SPLIT3_KERNEL_TEMPLATE(VSplit3, schar , int8x16x3_t, vld3q_s8 , vst1q_s8 );
103 SPLIT3_KERNEL_TEMPLATE(VSplit3, ushort, uint16x8x3_t, vld3q_u16, vst1q_u16);
104 SPLIT3_KERNEL_TEMPLATE(VSplit3, short , int16x8x3_t, vld3q_s16, vst1q_s16);
105 SPLIT3_KERNEL_TEMPLATE(VSplit3, int , int32x4x3_t, vld3q_s32, vst1q_s32);
106 SPLIT3_KERNEL_TEMPLATE(VSplit3, float , float32x4x3_t, vld3q_f32, vst1q_f32);
107 SPLIT3_KERNEL_TEMPLATE(VSplit3, int64 , int64x1x3_t, vld3_s64 , vst1_s64 );
109 SPLIT4_KERNEL_TEMPLATE(VSplit4, uchar , uint8x16x4_t, vld4q_u8 , vst1q_u8 );
110 SPLIT4_KERNEL_TEMPLATE(VSplit4, schar , int8x16x4_t, vld4q_s8 , vst1q_s8 );
111 SPLIT4_KERNEL_TEMPLATE(VSplit4, ushort, uint16x8x4_t, vld4q_u16, vst1q_u16);
112 SPLIT4_KERNEL_TEMPLATE(VSplit4, short , int16x8x4_t, vld4q_s16, vst1q_s16);
113 SPLIT4_KERNEL_TEMPLATE(VSplit4, int , int32x4x4_t, vld4q_s32, vst1q_s32);
114 SPLIT4_KERNEL_TEMPLATE(VSplit4, float , float32x4x4_t, vld4q_f32, vst1q_f32);
115 SPLIT4_KERNEL_TEMPLATE(VSplit4, int64 , int64x1x4_t, vld4_s64 , vst1_s64 );
118 template<typename T> static void
119 split_( const T* src, T** dst, int len, int cn )
121 int k = cn % 4 ? cn % 4 : 4;
129 memcpy(dst0, src, len * sizeof(T));
133 for( i = 0, j = 0 ; i < len; i++, j += cn )
139 T *dst0 = dst[0], *dst1 = dst[1];
145 int inc_i = (sizeof(T) == 8)? 1: 16/sizeof(T);
146 int inc_j = 2 * inc_i;
149 for( ; i < len - inc_i; i += inc_i, j += inc_j)
150 vsplit(src + j, dst0 + i, dst1 + i);
153 for( ; i < len; i++, j += cn )
161 T *dst0 = dst[0], *dst1 = dst[1], *dst2 = dst[2];
167 int inc_i = (sizeof(T) == 8)? 1: 16/sizeof(T);
168 int inc_j = 3 * inc_i;
171 for( ; i < len - inc_i; i += inc_i, j += inc_j)
172 vsplit(src + j, dst0 + i, dst1 + i, dst2 + i);
175 for( ; i < len; i++, j += cn )
184 T *dst0 = dst[0], *dst1 = dst[1], *dst2 = dst[2], *dst3 = dst[3];
190 int inc_i = (sizeof(T) == 8)? 1: 16/sizeof(T);
191 int inc_j = 4 * inc_i;
194 for( ; i < len - inc_i; i += inc_i, j += inc_j)
195 vsplit(src + j, dst0 + i, dst1 + i, dst2 + i, dst3 + i);
198 for( ; i < len; i++, j += cn )
200 dst0[i] = src[j]; dst1[i] = src[j+1];
201 dst2[i] = src[j+2]; dst3[i] = src[j+3];
205 for( ; k < cn; k += 4 )
207 T *dst0 = dst[k], *dst1 = dst[k+1], *dst2 = dst[k+2], *dst3 = dst[k+3];
208 for( i = 0, j = k; i < len; i++, j += cn )
210 dst0[i] = src[j]; dst1[i] = src[j+1];
211 dst2[i] = src[j+2]; dst3[i] = src[j+3];
218 template<typename T> struct VMerge2;
219 template<typename T> struct VMerge3;
220 template<typename T> struct VMerge4;
222 #define MERGE2_KERNEL_TEMPLATE(name, data_type, reg_type, load_func, store_func) \
224 struct name<data_type>{ \
225 void operator()(const data_type* src0, const data_type* src1, \
228 r.val[0] = load_func(src0); \
229 r.val[1] = load_func(src1); \
230 store_func(dst, r); \
234 #define MERGE3_KERNEL_TEMPLATE(name, data_type, reg_type, load_func, store_func) \
236 struct name<data_type>{ \
237 void operator()(const data_type* src0, const data_type* src1, \
238 const data_type* src2, data_type* dst){ \
240 r.val[0] = load_func(src0); \
241 r.val[1] = load_func(src1); \
242 r.val[2] = load_func(src2); \
243 store_func(dst, r); \
247 #define MERGE4_KERNEL_TEMPLATE(name, data_type, reg_type, load_func, store_func) \
249 struct name<data_type>{ \
250 void operator()(const data_type* src0, const data_type* src1, \
251 const data_type* src2, const data_type* src3, \
254 r.val[0] = load_func(src0); \
255 r.val[1] = load_func(src1); \
256 r.val[2] = load_func(src2); \
257 r.val[3] = load_func(src3); \
258 store_func(dst, r); \
262 MERGE2_KERNEL_TEMPLATE(VMerge2, uchar , uint8x16x2_t, vld1q_u8 , vst2q_u8 );
263 MERGE2_KERNEL_TEMPLATE(VMerge2, schar , int8x16x2_t, vld1q_s8 , vst2q_s8 );
264 MERGE2_KERNEL_TEMPLATE(VMerge2, ushort, uint16x8x2_t, vld1q_u16, vst2q_u16);
265 MERGE2_KERNEL_TEMPLATE(VMerge2, short , int16x8x2_t, vld1q_s16, vst2q_s16);
266 MERGE2_KERNEL_TEMPLATE(VMerge2, int , int32x4x2_t, vld1q_s32, vst2q_s32);
267 MERGE2_KERNEL_TEMPLATE(VMerge2, float , float32x4x2_t, vld1q_f32, vst2q_f32);
268 MERGE2_KERNEL_TEMPLATE(VMerge2, int64 , int64x1x2_t, vld1_s64 , vst2_s64 );
270 MERGE3_KERNEL_TEMPLATE(VMerge3, uchar , uint8x16x3_t, vld1q_u8 , vst3q_u8 );
271 MERGE3_KERNEL_TEMPLATE(VMerge3, schar , int8x16x3_t, vld1q_s8 , vst3q_s8 );
272 MERGE3_KERNEL_TEMPLATE(VMerge3, ushort, uint16x8x3_t, vld1q_u16, vst3q_u16);
273 MERGE3_KERNEL_TEMPLATE(VMerge3, short , int16x8x3_t, vld1q_s16, vst3q_s16);
274 MERGE3_KERNEL_TEMPLATE(VMerge3, int , int32x4x3_t, vld1q_s32, vst3q_s32);
275 MERGE3_KERNEL_TEMPLATE(VMerge3, float , float32x4x3_t, vld1q_f32, vst3q_f32);
276 MERGE3_KERNEL_TEMPLATE(VMerge3, int64 , int64x1x3_t, vld1_s64 , vst3_s64 );
278 MERGE4_KERNEL_TEMPLATE(VMerge4, uchar , uint8x16x4_t, vld1q_u8 , vst4q_u8 );
279 MERGE4_KERNEL_TEMPLATE(VMerge4, schar , int8x16x4_t, vld1q_s8 , vst4q_s8 );
280 MERGE4_KERNEL_TEMPLATE(VMerge4, ushort, uint16x8x4_t, vld1q_u16, vst4q_u16);
281 MERGE4_KERNEL_TEMPLATE(VMerge4, short , int16x8x4_t, vld1q_s16, vst4q_s16);
282 MERGE4_KERNEL_TEMPLATE(VMerge4, int , int32x4x4_t, vld1q_s32, vst4q_s32);
283 MERGE4_KERNEL_TEMPLATE(VMerge4, float , float32x4x4_t, vld1q_f32, vst4q_f32);
284 MERGE4_KERNEL_TEMPLATE(VMerge4, int64 , int64x1x4_t, vld1_s64 , vst4_s64 );
287 template<typename T> static void
288 merge_( const T** src, T* dst, int len, int cn )
290 int k = cn % 4 ? cn % 4 : 4;
294 const T* src0 = src[0];
295 for( i = j = 0; i < len; i++, j += cn )
300 const T *src0 = src[0], *src1 = src[1];
305 int inc_i = (sizeof(T) == 8)? 1: 16/sizeof(T);
306 int inc_j = 2 * inc_i;
309 for( ; i < len - inc_i; i += inc_i, j += inc_j)
310 vmerge(src0 + i, src1 + i, dst + j);
313 for( ; i < len; i++, j += cn )
321 const T *src0 = src[0], *src1 = src[1], *src2 = src[2];
326 int inc_i = (sizeof(T) == 8)? 1: 16/sizeof(T);
327 int inc_j = 3 * inc_i;
330 for( ; i < len - inc_i; i += inc_i, j += inc_j)
331 vmerge(src0 + i, src1 + i, src2 + i, dst + j);
334 for( ; i < len; i++, j += cn )
343 const T *src0 = src[0], *src1 = src[1], *src2 = src[2], *src3 = src[3];
348 int inc_i = (sizeof(T) == 8)? 1: 16/sizeof(T);
349 int inc_j = 4 * inc_i;
352 for( ; i < len - inc_i; i += inc_i, j += inc_j)
353 vmerge(src0 + i, src1 + i, src2 + i, src3 + i, dst + j);
356 for( ; i < len; i++, j += cn )
358 dst[j] = src0[i]; dst[j+1] = src1[i];
359 dst[j+2] = src2[i]; dst[j+3] = src3[i];
363 for( ; k < cn; k += 4 )
365 const T *src0 = src[k], *src1 = src[k+1], *src2 = src[k+2], *src3 = src[k+3];
366 for( i = 0, j = k; i < len; i++, j += cn )
368 dst[j] = src0[i]; dst[j+1] = src1[i];
369 dst[j+2] = src2[i]; dst[j+3] = src3[i];
374 static void split8u(const uchar* src, uchar** dst, int len, int cn )
376 split_(src, dst, len, cn);
379 static void split16u(const ushort* src, ushort** dst, int len, int cn )
381 split_(src, dst, len, cn);
384 static void split32s(const int* src, int** dst, int len, int cn )
386 split_(src, dst, len, cn);
389 static void split64s(const int64* src, int64** dst, int len, int cn )
391 split_(src, dst, len, cn);
394 static void merge8u(const uchar** src, uchar* dst, int len, int cn )
396 merge_(src, dst, len, cn);
399 static void merge16u(const ushort** src, ushort* dst, int len, int cn )
401 merge_(src, dst, len, cn);
404 static void merge32s(const int** src, int* dst, int len, int cn )
406 merge_(src, dst, len, cn);
409 static void merge64s(const int64** src, int64* dst, int len, int cn )
411 merge_(src, dst, len, cn);
414 typedef void (*SplitFunc)(const uchar* src, uchar** dst, int len, int cn);
415 typedef void (*MergeFunc)(const uchar** src, uchar* dst, int len, int cn);
417 static SplitFunc getSplitFunc(int depth)
419 static SplitFunc splitTab[] =
421 (SplitFunc)GET_OPTIMIZED(split8u), (SplitFunc)GET_OPTIMIZED(split8u), (SplitFunc)GET_OPTIMIZED(split16u), (SplitFunc)GET_OPTIMIZED(split16u),
422 (SplitFunc)GET_OPTIMIZED(split32s), (SplitFunc)GET_OPTIMIZED(split32s), (SplitFunc)GET_OPTIMIZED(split64s), 0
425 return splitTab[depth];
428 static MergeFunc getMergeFunc(int depth)
430 static MergeFunc mergeTab[] =
432 (MergeFunc)GET_OPTIMIZED(merge8u), (MergeFunc)GET_OPTIMIZED(merge8u), (MergeFunc)GET_OPTIMIZED(merge16u), (MergeFunc)GET_OPTIMIZED(merge16u),
433 (MergeFunc)GET_OPTIMIZED(merge32s), (MergeFunc)GET_OPTIMIZED(merge32s), (MergeFunc)GET_OPTIMIZED(merge64s), 0
436 return mergeTab[depth];
441 void cv::split(const Mat& src, Mat* mv)
443 int k, depth = src.depth(), cn = src.channels();
450 SplitFunc func = getSplitFunc(depth);
451 CV_Assert( func != 0 );
453 int esz = (int)src.elemSize(), esz1 = (int)src.elemSize1();
454 int blocksize0 = (BLOCK_SIZE + esz-1)/esz;
455 AutoBuffer<uchar> _buf((cn+1)*(sizeof(Mat*) + sizeof(uchar*)) + 16);
456 const Mat** arrays = (const Mat**)(uchar*)_buf;
457 uchar** ptrs = (uchar**)alignPtr(arrays + cn + 1, 16);
460 for( k = 0; k < cn; k++ )
462 mv[k].create(src.dims, src.size, depth);
463 arrays[k+1] = &mv[k];
466 NAryMatIterator it(arrays, ptrs, cn+1);
467 int total = (int)it.size, blocksize = cn <= 4 ? total : std::min(total, blocksize0);
469 for( size_t i = 0; i < it.nplanes; i++, ++it )
471 for( int j = 0; j < total; j += blocksize )
473 int bsz = std::min(total - j, blocksize);
474 func( ptrs[0], &ptrs[1], bsz, cn );
476 if( j + blocksize < total )
479 for( k = 0; k < cn; k++ )
480 ptrs[k+1] += bsz*esz1;
490 static bool ocl_split( InputArray _m, OutputArrayOfArrays _mv )
492 int type = _m.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
493 rowsPerWI = ocl::Device::getDefault().isIntel() ? 4 : 1;
495 String dstargs, processelem, indexdecl;
496 for (int i = 0; i < cn; ++i)
498 dstargs += format("DECLARE_DST_PARAM(%d)", i);
499 indexdecl += format("DECLARE_INDEX(%d)", i);
500 processelem += format("PROCESS_ELEM(%d)", i);
503 ocl::Kernel k("split", ocl::core::split_merge_oclsrc,
504 format("-D T=%s -D OP_SPLIT -D cn=%d -D DECLARE_DST_PARAMS=%s"
505 " -D PROCESS_ELEMS_N=%s -D DECLARE_INDEX_N=%s",
506 ocl::memopTypeToStr(depth), cn, dstargs.c_str(),
507 processelem.c_str(), indexdecl.c_str()));
511 Size size = _m.size();
512 _mv.create(cn, 1, depth);
513 for (int i = 0; i < cn; ++i)
514 _mv.create(size, depth, i);
516 std::vector<UMat> dst;
517 _mv.getUMatVector(dst);
519 int argidx = k.set(0, ocl::KernelArg::ReadOnly(_m.getUMat()));
520 for (int i = 0; i < cn; ++i)
521 argidx = k.set(argidx, ocl::KernelArg::WriteOnlyNoSize(dst[i]));
522 k.set(argidx, rowsPerWI);
524 size_t globalsize[2] = { size.width, (size.height + rowsPerWI - 1) / rowsPerWI };
525 return k.run(2, globalsize, NULL, false);
532 void cv::split(InputArray _m, OutputArrayOfArrays _mv)
534 CV_OCL_RUN(_m.dims() <= 2 && _mv.isUMatVector(),
544 CV_Assert( !_mv.fixedType() || _mv.empty() || _mv.type() == m.depth() );
546 Size size = m.size();
547 int depth = m.depth(), cn = m.channels();
548 _mv.create(cn, 1, depth);
549 for (int i = 0; i < cn; ++i)
550 _mv.create(size, depth, i);
552 std::vector<Mat> dst;
553 _mv.getMatVector(dst);
558 void cv::merge(const Mat* mv, size_t n, OutputArray _dst)
560 CV_Assert( mv && n > 0 );
562 int depth = mv[0].depth();
567 for( i = 0; i < n; i++ )
569 CV_Assert(mv[i].size == mv[0].size && mv[i].depth() == depth);
570 allch1 = allch1 && mv[i].channels() == 1;
571 cn += mv[i].channels();
574 CV_Assert( 0 < cn && cn <= CV_CN_MAX );
575 _dst.create(mv[0].dims, mv[0].size, CV_MAKETYPE(depth, cn));
576 Mat dst = _dst.getMat();
586 AutoBuffer<int> pairs(cn*2);
589 for( i = 0, j = 0; i < n; i++, j += ni )
591 ni = mv[i].channels();
592 for( k = 0; k < ni; k++ )
594 pairs[(j+k)*2] = j + k;
595 pairs[(j+k)*2+1] = j + k;
598 mixChannels( mv, n, &dst, 1, &pairs[0], cn );
602 size_t esz = dst.elemSize(), esz1 = dst.elemSize1();
603 int blocksize0 = (int)((BLOCK_SIZE + esz-1)/esz);
604 AutoBuffer<uchar> _buf((cn+1)*(sizeof(Mat*) + sizeof(uchar*)) + 16);
605 const Mat** arrays = (const Mat**)(uchar*)_buf;
606 uchar** ptrs = (uchar**)alignPtr(arrays + cn + 1, 16);
609 for( k = 0; k < cn; k++ )
610 arrays[k+1] = &mv[k];
612 NAryMatIterator it(arrays, ptrs, cn+1);
613 int total = (int)it.size, blocksize = cn <= 4 ? total : std::min(total, blocksize0);
614 MergeFunc func = getMergeFunc(depth);
616 for( i = 0; i < it.nplanes; i++, ++it )
618 for( int j = 0; j < total; j += blocksize )
620 int bsz = std::min(total - j, blocksize);
621 func( (const uchar**)&ptrs[1], ptrs[0], bsz, cn );
623 if( j + blocksize < total )
626 for( int t = 0; t < cn; t++ )
627 ptrs[t+1] += bsz*esz1;
637 static bool ocl_merge( InputArrayOfArrays _mv, OutputArray _dst )
639 std::vector<UMat> src, ksrc;
640 _mv.getUMatVector(src);
641 CV_Assert(!src.empty());
643 int type = src[0].type(), depth = CV_MAT_DEPTH(type),
644 rowsPerWI = ocl::Device::getDefault().isIntel() ? 4 : 1;
645 Size size = src[0].size();
647 for (size_t i = 0, srcsize = src.size(); i < srcsize; ++i)
649 int itype = src[i].type(), icn = CV_MAT_CN(itype), idepth = CV_MAT_DEPTH(itype),
650 esz1 = CV_ELEM_SIZE1(idepth);
654 CV_Assert(size == src[i].size() && depth == idepth);
656 for (int cn = 0; cn < icn; ++cn)
659 tsrc.offset += cn * esz1;
660 ksrc.push_back(tsrc);
663 int dcn = (int)ksrc.size();
665 String srcargs, processelem, cndecl, indexdecl;
666 for (int i = 0; i < dcn; ++i)
668 srcargs += format("DECLARE_SRC_PARAM(%d)", i);
669 processelem += format("PROCESS_ELEM(%d)", i);
670 indexdecl += format("DECLARE_INDEX(%d)", i);
671 cndecl += format(" -D scn%d=%d", i, ksrc[i].channels());
674 ocl::Kernel k("merge", ocl::core::split_merge_oclsrc,
675 format("-D OP_MERGE -D cn=%d -D T=%s -D DECLARE_SRC_PARAMS_N=%s"
676 " -D DECLARE_INDEX_N=%s -D PROCESS_ELEMS_N=%s%s",
677 dcn, ocl::memopTypeToStr(depth), srcargs.c_str(),
678 indexdecl.c_str(), processelem.c_str(), cndecl.c_str()));
682 _dst.create(size, CV_MAKE_TYPE(depth, dcn));
683 UMat dst = _dst.getUMat();
686 for (int i = 0; i < dcn; ++i)
687 argidx = k.set(argidx, ocl::KernelArg::ReadOnlyNoSize(ksrc[i]));
688 argidx = k.set(argidx, ocl::KernelArg::WriteOnly(dst));
689 k.set(argidx, rowsPerWI);
691 size_t globalsize[2] = { dst.cols, (dst.rows + rowsPerWI - 1) / rowsPerWI };
692 return k.run(2, globalsize, NULL, false);
699 void cv::merge(InputArrayOfArrays _mv, OutputArray _dst)
701 CV_OCL_RUN(_mv.isUMatVector() && _dst.isUMat(),
702 ocl_merge(_mv, _dst))
705 _mv.getMatVector(mv);
706 merge(!mv.empty() ? &mv[0] : 0, mv.size(), _dst);
709 /****************************************************************************************\
710 * Generalized split/merge: mixing channels *
711 \****************************************************************************************/
716 template<typename T> static void
717 mixChannels_( const T** src, const int* sdelta,
718 T** dst, const int* ddelta,
719 int len, int npairs )
722 for( k = 0; k < npairs; k++ )
726 int ds = sdelta[k], dd = ddelta[k];
729 for( i = 0; i <= len - 2; i += 2, s += ds*2, d += dd*2 )
731 T t0 = s[0], t1 = s[ds];
732 d[0] = t0; d[dd] = t1;
739 for( i = 0; i <= len - 2; i += 2, d += dd*2 )
748 static void mixChannels8u( const uchar** src, const int* sdelta,
749 uchar** dst, const int* ddelta,
750 int len, int npairs )
752 mixChannels_(src, sdelta, dst, ddelta, len, npairs);
755 static void mixChannels16u( const ushort** src, const int* sdelta,
756 ushort** dst, const int* ddelta,
757 int len, int npairs )
759 mixChannels_(src, sdelta, dst, ddelta, len, npairs);
762 static void mixChannels32s( const int** src, const int* sdelta,
763 int** dst, const int* ddelta,
764 int len, int npairs )
766 mixChannels_(src, sdelta, dst, ddelta, len, npairs);
769 static void mixChannels64s( const int64** src, const int* sdelta,
770 int64** dst, const int* ddelta,
771 int len, int npairs )
773 mixChannels_(src, sdelta, dst, ddelta, len, npairs);
776 typedef void (*MixChannelsFunc)( const uchar** src, const int* sdelta,
777 uchar** dst, const int* ddelta, int len, int npairs );
779 static MixChannelsFunc getMixchFunc(int depth)
781 static MixChannelsFunc mixchTab[] =
783 (MixChannelsFunc)mixChannels8u, (MixChannelsFunc)mixChannels8u, (MixChannelsFunc)mixChannels16u,
784 (MixChannelsFunc)mixChannels16u, (MixChannelsFunc)mixChannels32s, (MixChannelsFunc)mixChannels32s,
785 (MixChannelsFunc)mixChannels64s, 0
788 return mixchTab[depth];
793 void cv::mixChannels( const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, const int* fromTo, size_t npairs )
797 CV_Assert( src && nsrcs > 0 && dst && ndsts > 0 && fromTo && npairs > 0 );
799 size_t i, j, k, esz1 = dst[0].elemSize1();
800 int depth = dst[0].depth();
802 AutoBuffer<uchar> buf((nsrcs + ndsts + 1)*(sizeof(Mat*) + sizeof(uchar*)) + npairs*(sizeof(uchar*)*2 + sizeof(int)*6));
803 const Mat** arrays = (const Mat**)(uchar*)buf;
804 uchar** ptrs = (uchar**)(arrays + nsrcs + ndsts);
805 const uchar** srcs = (const uchar**)(ptrs + nsrcs + ndsts + 1);
806 uchar** dsts = (uchar**)(srcs + npairs);
807 int* tab = (int*)(dsts + npairs);
808 int *sdelta = (int*)(tab + npairs*4), *ddelta = sdelta + npairs;
810 for( i = 0; i < nsrcs; i++ )
812 for( i = 0; i < ndsts; i++ )
813 arrays[i + nsrcs] = &dst[i];
814 ptrs[nsrcs + ndsts] = 0;
816 for( i = 0; i < npairs; i++ )
818 int i0 = fromTo[i*2], i1 = fromTo[i*2+1];
821 for( j = 0; j < nsrcs; i0 -= src[j].channels(), j++ )
822 if( i0 < src[j].channels() )
824 CV_Assert(j < nsrcs && src[j].depth() == depth);
825 tab[i*4] = (int)j; tab[i*4+1] = (int)(i0*esz1);
826 sdelta[i] = src[j].channels();
830 tab[i*4] = (int)(nsrcs + ndsts); tab[i*4+1] = 0;
834 for( j = 0; j < ndsts; i1 -= dst[j].channels(), j++ )
835 if( i1 < dst[j].channels() )
837 CV_Assert(i1 >= 0 && j < ndsts && dst[j].depth() == depth);
838 tab[i*4+2] = (int)(j + nsrcs); tab[i*4+3] = (int)(i1*esz1);
839 ddelta[i] = dst[j].channels();
842 NAryMatIterator it(arrays, ptrs, (int)(nsrcs + ndsts));
843 int total = (int)it.size, blocksize = std::min(total, (int)((BLOCK_SIZE + esz1-1)/esz1));
844 MixChannelsFunc func = getMixchFunc(depth);
846 for( i = 0; i < it.nplanes; i++, ++it )
848 for( k = 0; k < npairs; k++ )
850 srcs[k] = ptrs[tab[k*4]] + tab[k*4+1];
851 dsts[k] = ptrs[tab[k*4+2]] + tab[k*4+3];
854 for( int t = 0; t < total; t += blocksize )
856 int bsz = std::min(total - t, blocksize);
857 func( srcs, sdelta, dsts, ddelta, bsz, (int)npairs );
859 if( t + blocksize < total )
860 for( k = 0; k < npairs; k++ )
862 srcs[k] += blocksize*sdelta[k]*esz1;
863 dsts[k] += blocksize*ddelta[k]*esz1;
873 static void getUMatIndex(const std::vector<UMat> & um, int cn, int & idx, int & cnidx)
875 int totalChannels = 0;
876 for (size_t i = 0, size = um.size(); i < size; ++i)
878 int ccn = um[i].channels();
879 totalChannels += ccn;
881 if (totalChannels == cn)
887 else if (totalChannels > cn)
890 cnidx = i == 0 ? cn : (cn - totalChannels + ccn);
898 static bool ocl_mixChannels(InputArrayOfArrays _src, InputOutputArrayOfArrays _dst,
899 const int* fromTo, size_t npairs)
901 std::vector<UMat> src, dst;
902 _src.getUMatVector(src);
903 _dst.getUMatVector(dst);
905 size_t nsrc = src.size(), ndst = dst.size();
906 CV_Assert(nsrc > 0 && ndst > 0);
908 Size size = src[0].size();
909 int depth = src[0].depth(), esz = CV_ELEM_SIZE(depth),
910 rowsPerWI = ocl::Device::getDefault().isIntel() ? 4 : 1;
912 for (size_t i = 1, ssize = src.size(); i < ssize; ++i)
913 CV_Assert(src[i].size() == size && src[i].depth() == depth);
914 for (size_t i = 0, dsize = dst.size(); i < dsize; ++i)
915 CV_Assert(dst[i].size() == size && dst[i].depth() == depth);
917 String declsrc, decldst, declproc, declcn, indexdecl;
918 std::vector<UMat> srcargs(npairs), dstargs(npairs);
920 for (size_t i = 0; i < npairs; ++i)
922 int scn = fromTo[i<<1], dcn = fromTo[(i<<1) + 1];
923 int src_idx, src_cnidx, dst_idx, dst_cnidx;
925 getUMatIndex(src, scn, src_idx, src_cnidx);
926 getUMatIndex(dst, dcn, dst_idx, dst_cnidx);
928 CV_Assert(dst_idx >= 0 && src_idx >= 0);
930 srcargs[i] = src[src_idx];
931 srcargs[i].offset += src_cnidx * esz;
933 dstargs[i] = dst[dst_idx];
934 dstargs[i].offset += dst_cnidx * esz;
936 declsrc += format("DECLARE_INPUT_MAT(%d)", i);
937 decldst += format("DECLARE_OUTPUT_MAT(%d)", i);
938 indexdecl += format("DECLARE_INDEX(%d)", i);
939 declproc += format("PROCESS_ELEM(%d)", i);
940 declcn += format(" -D scn%d=%d -D dcn%d=%d", i, src[src_idx].channels(), i, dst[dst_idx].channels());
943 ocl::Kernel k("mixChannels", ocl::core::mixchannels_oclsrc,
944 format("-D T=%s -D DECLARE_INPUT_MAT_N=%s -D DECLARE_OUTPUT_MAT_N=%s"
945 " -D PROCESS_ELEM_N=%s -D DECLARE_INDEX_N=%s%s",
946 ocl::memopTypeToStr(depth), declsrc.c_str(), decldst.c_str(),
947 declproc.c_str(), indexdecl.c_str(), declcn.c_str()));
952 for (size_t i = 0; i < npairs; ++i)
953 argindex = k.set(argindex, ocl::KernelArg::ReadOnlyNoSize(srcargs[i]));
954 for (size_t i = 0; i < npairs; ++i)
955 argindex = k.set(argindex, ocl::KernelArg::WriteOnlyNoSize(dstargs[i]));
956 argindex = k.set(argindex, size.height);
957 argindex = k.set(argindex, size.width);
958 k.set(argindex, rowsPerWI);
960 size_t globalsize[2] = { size.width, (size.height + rowsPerWI - 1) / rowsPerWI };
961 return k.run(2, globalsize, NULL, false);
968 void cv::mixChannels(InputArrayOfArrays src, InputOutputArrayOfArrays dst,
969 const int* fromTo, size_t npairs)
971 if (npairs == 0 || fromTo == NULL)
974 CV_OCL_RUN(dst.isUMatVector(),
975 ocl_mixChannels(src, dst, fromTo, npairs))
977 bool src_is_mat = src.kind() != _InputArray::STD_VECTOR_MAT &&
978 src.kind() != _InputArray::STD_VECTOR_VECTOR &&
979 src.kind() != _InputArray::STD_VECTOR_UMAT;
980 bool dst_is_mat = dst.kind() != _InputArray::STD_VECTOR_MAT &&
981 dst.kind() != _InputArray::STD_VECTOR_VECTOR &&
982 dst.kind() != _InputArray::STD_VECTOR_UMAT;
984 int nsrc = src_is_mat ? 1 : (int)src.total();
985 int ndst = dst_is_mat ? 1 : (int)dst.total();
987 CV_Assert(nsrc > 0 && ndst > 0);
988 cv::AutoBuffer<Mat> _buf(nsrc + ndst);
990 for( i = 0; i < nsrc; i++ )
991 buf[i] = src.getMat(src_is_mat ? -1 : i);
992 for( i = 0; i < ndst; i++ )
993 buf[nsrc + i] = dst.getMat(dst_is_mat ? -1 : i);
994 mixChannels(&buf[0], nsrc, &buf[nsrc], ndst, fromTo, npairs);
997 void cv::mixChannels(InputArrayOfArrays src, InputOutputArrayOfArrays dst,
998 const std::vector<int>& fromTo)
1003 CV_OCL_RUN(dst.isUMatVector(),
1004 ocl_mixChannels(src, dst, &fromTo[0], fromTo.size()>>1))
1006 bool src_is_mat = src.kind() != _InputArray::STD_VECTOR_MAT &&
1007 src.kind() != _InputArray::STD_VECTOR_VECTOR &&
1008 src.kind() != _InputArray::STD_VECTOR_UMAT;
1009 bool dst_is_mat = dst.kind() != _InputArray::STD_VECTOR_MAT &&
1010 dst.kind() != _InputArray::STD_VECTOR_VECTOR &&
1011 dst.kind() != _InputArray::STD_VECTOR_UMAT;
1013 int nsrc = src_is_mat ? 1 : (int)src.total();
1014 int ndst = dst_is_mat ? 1 : (int)dst.total();
1016 CV_Assert(fromTo.size()%2 == 0 && nsrc > 0 && ndst > 0);
1017 cv::AutoBuffer<Mat> _buf(nsrc + ndst);
1019 for( i = 0; i < nsrc; i++ )
1020 buf[i] = src.getMat(src_is_mat ? -1 : i);
1021 for( i = 0; i < ndst; i++ )
1022 buf[nsrc + i] = dst.getMat(dst_is_mat ? -1 : i);
1023 mixChannels(&buf[0], nsrc, &buf[nsrc], ndst, &fromTo[0], fromTo.size()/2);
1026 void cv::extractChannel(InputArray _src, OutputArray _dst, int coi)
1028 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
1029 CV_Assert( 0 <= coi && coi < cn );
1030 int ch[] = { coi, 0 };
1032 if (ocl::useOpenCL() && _src.dims() <= 2 && _dst.isUMat())
1034 UMat src = _src.getUMat();
1035 _dst.create(src.dims, &src.size[0], depth);
1036 UMat dst = _dst.getUMat();
1037 mixChannels(std::vector<UMat>(1, src), std::vector<UMat>(1, dst), ch, 1);
1041 Mat src = _src.getMat();
1042 _dst.create(src.dims, &src.size[0], depth);
1043 Mat dst = _dst.getMat();
1044 mixChannels(&src, 1, &dst, 1, ch, 1);
1047 void cv::insertChannel(InputArray _src, InputOutputArray _dst, int coi)
1049 int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), scn = CV_MAT_CN(stype);
1050 int dtype = _dst.type(), ddepth = CV_MAT_DEPTH(dtype), dcn = CV_MAT_CN(dtype);
1051 CV_Assert( _src.sameSize(_dst) && sdepth == ddepth );
1052 CV_Assert( 0 <= coi && coi < dcn && scn == 1 );
1054 int ch[] = { 0, coi };
1055 if (ocl::useOpenCL() && _src.dims() <= 2 && _dst.isUMat())
1057 UMat src = _src.getUMat(), dst = _dst.getUMat();
1058 mixChannels(std::vector<UMat>(1, src), std::vector<UMat>(1, dst), ch, 1);
1062 Mat src = _src.getMat(), dst = _dst.getMat();
1063 mixChannels(&src, 1, &dst, 1, ch, 1);
1066 /****************************************************************************************\
1067 * convertScale[Abs] *
1068 \****************************************************************************************/
1073 template<typename T, typename DT, typename WT>
1074 struct cvtScaleAbs_SSE2
1076 int operator () (const T *, DT *, int, WT, WT) const
1085 struct cvtScaleAbs_SSE2<uchar, uchar, float>
1087 int operator () (const uchar * src, uchar * dst, int width,
1088 float scale, float shift) const
1094 __m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift),
1095 v_zero_f = _mm_setzero_ps();
1096 __m128i v_zero_i = _mm_setzero_si128();
1098 for ( ; x <= width - 16; x += 16)
1100 __m128i v_src = _mm_loadu_si128((const __m128i *)(src + x));
1101 __m128i v_src12 = _mm_unpacklo_epi8(v_src, v_zero_i), v_src_34 = _mm_unpackhi_epi8(v_src, v_zero_i);
1102 __m128 v_dst1 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(v_src12, v_zero_i)), v_scale), v_shift);
1103 v_dst1 = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst1), v_dst1);
1104 __m128 v_dst2 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(v_src12, v_zero_i)), v_scale), v_shift);
1105 v_dst2 = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst2), v_dst2);
1106 __m128 v_dst3 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(v_src_34, v_zero_i)), v_scale), v_shift);
1107 v_dst3 = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst3), v_dst3);
1108 __m128 v_dst4 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(v_src_34, v_zero_i)), v_scale), v_shift);
1109 v_dst4 = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst4), v_dst4);
1111 __m128i v_dst_i = _mm_packus_epi16(_mm_packs_epi32(_mm_cvtps_epi32(v_dst1), _mm_cvtps_epi32(v_dst2)),
1112 _mm_packs_epi32(_mm_cvtps_epi32(v_dst3), _mm_cvtps_epi32(v_dst4)));
1113 _mm_storeu_si128((__m128i *)(dst + x), v_dst_i);
1122 struct cvtScaleAbs_SSE2<ushort, uchar, float>
1124 int operator () (const ushort * src, uchar * dst, int width,
1125 float scale, float shift) const
1131 __m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift),
1132 v_zero_f = _mm_setzero_ps();
1133 __m128i v_zero_i = _mm_setzero_si128();
1135 for ( ; x <= width - 8; x += 8)
1137 __m128i v_src = _mm_loadu_si128((const __m128i *)(src + x));
1138 __m128 v_dst1 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpacklo_epi16(v_src, v_zero_i)), v_scale), v_shift);
1139 v_dst1 = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst1), v_dst1);
1140 __m128 v_dst2 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_unpackhi_epi16(v_src, v_zero_i)), v_scale), v_shift);
1141 v_dst2 = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst2), v_dst2);
1143 __m128i v_dst_i = _mm_packus_epi16(_mm_packs_epi32(_mm_cvtps_epi32(v_dst1), _mm_cvtps_epi32(v_dst2)), v_zero_i);
1144 _mm_storel_epi64((__m128i *)(dst + x), v_dst_i);
1153 struct cvtScaleAbs_SSE2<short, uchar, float>
1155 int operator () (const short * src, uchar * dst, int width,
1156 float scale, float shift) const
1162 __m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift),
1163 v_zero_f = _mm_setzero_ps();
1164 __m128i v_zero_i = _mm_setzero_si128();
1166 for ( ; x <= width - 8; x += 8)
1168 __m128i v_src = _mm_loadu_si128((const __m128i *)(src + x));
1169 __m128 v_dst1 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(v_src, v_src), 16)), v_scale), v_shift);
1170 v_dst1 = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst1), v_dst1);
1171 __m128 v_dst2 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpackhi_epi16(v_src, v_src), 16)), v_scale), v_shift);
1172 v_dst2 = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst2), v_dst2);
1174 __m128i v_dst_i = _mm_packus_epi16(_mm_packs_epi32(_mm_cvtps_epi32(v_dst1), _mm_cvtps_epi32(v_dst2)), v_zero_i);
1175 _mm_storel_epi64((__m128i *)(dst + x), v_dst_i);
1184 struct cvtScaleAbs_SSE2<int, uchar, float>
1186 int operator () (const int * src, uchar * dst, int width,
1187 float scale, float shift) const
1193 __m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift),
1194 v_zero_f = _mm_setzero_ps();
1195 __m128i v_zero_i = _mm_setzero_si128();
1197 for ( ; x <= width - 8; x += 4)
1199 __m128i v_src = _mm_loadu_si128((const __m128i *)(src + x));
1200 __m128 v_dst1 = _mm_add_ps(_mm_mul_ps(_mm_cvtepi32_ps(v_src), v_scale), v_shift);
1201 v_dst1 = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst1), v_dst1);
1203 __m128i v_dst_i = _mm_packus_epi16(_mm_packs_epi32(_mm_cvtps_epi32(v_dst1), v_zero_i), v_zero_i);
1204 _mm_storel_epi64((__m128i *)(dst + x), v_dst_i);
1213 struct cvtScaleAbs_SSE2<float, uchar, float>
1215 int operator () (const float * src, uchar * dst, int width,
1216 float scale, float shift) const
1222 __m128 v_scale = _mm_set1_ps(scale), v_shift = _mm_set1_ps(shift),
1223 v_zero_f = _mm_setzero_ps();
1224 __m128i v_zero_i = _mm_setzero_si128();
1226 for ( ; x <= width - 8; x += 4)
1228 __m128 v_dst = _mm_add_ps(_mm_mul_ps(_mm_loadu_ps(src + x), v_scale), v_shift);
1229 v_dst = _mm_max_ps(_mm_sub_ps(v_zero_f, v_dst), v_dst);
1231 __m128i v_dst_i = _mm_packs_epi32(_mm_cvtps_epi32(v_dst), v_zero_i);
1232 _mm_storel_epi64((__m128i *)(dst + x), _mm_packus_epi16(v_dst_i, v_zero_i));
1242 template<typename T, typename DT, typename WT> static void
1243 cvtScaleAbs_( const T* src, size_t sstep,
1244 DT* dst, size_t dstep, Size size,
1245 WT scale, WT shift )
1247 sstep /= sizeof(src[0]);
1248 dstep /= sizeof(dst[0]);
1249 cvtScaleAbs_SSE2<T, DT, WT> vop;
1251 for( ; size.height--; src += sstep, dst += dstep )
1253 int x = vop(src, dst, size.width, scale, shift);
1255 #if CV_ENABLE_UNROLLED
1256 for( ; x <= size.width - 4; x += 4 )
1259 t0 = saturate_cast<DT>(std::abs(src[x]*scale + shift));
1260 t1 = saturate_cast<DT>(std::abs(src[x+1]*scale + shift));
1261 dst[x] = t0; dst[x+1] = t1;
1262 t0 = saturate_cast<DT>(std::abs(src[x+2]*scale + shift));
1263 t1 = saturate_cast<DT>(std::abs(src[x+3]*scale + shift));
1264 dst[x+2] = t0; dst[x+3] = t1;
1267 for( ; x < size.width; x++ )
1268 dst[x] = saturate_cast<DT>(std::abs(src[x]*scale + shift));
1272 template<typename T, typename DT, typename WT> static void
1273 cvtScale_( const T* src, size_t sstep,
1274 DT* dst, size_t dstep, Size size,
1275 WT scale, WT shift )
1277 sstep /= sizeof(src[0]);
1278 dstep /= sizeof(dst[0]);
1280 for( ; size.height--; src += sstep, dst += dstep )
1283 #if CV_ENABLE_UNROLLED
1284 for( ; x <= size.width - 4; x += 4 )
1287 t0 = saturate_cast<DT>(src[x]*scale + shift);
1288 t1 = saturate_cast<DT>(src[x+1]*scale + shift);
1289 dst[x] = t0; dst[x+1] = t1;
1290 t0 = saturate_cast<DT>(src[x+2]*scale + shift);
1291 t1 = saturate_cast<DT>(src[x+3]*scale + shift);
1292 dst[x+2] = t0; dst[x+3] = t1;
1296 for( ; x < size.width; x++ )
1297 dst[x] = saturate_cast<DT>(src[x]*scale + shift);
1301 //vz optimized template specialization
1303 cvtScale_<short, short, float>( const short* src, size_t sstep,
1304 short* dst, size_t dstep, Size size,
1305 float scale, float shift )
1307 sstep /= sizeof(src[0]);
1308 dstep /= sizeof(dst[0]);
1310 for( ; size.height--; src += sstep, dst += dstep )
1316 __m128 scale128 = _mm_set1_ps (scale);
1317 __m128 shift128 = _mm_set1_ps (shift);
1318 for(; x <= size.width - 8; x += 8 )
1320 __m128i r0 = _mm_loadl_epi64((const __m128i*)(src + x));
1321 __m128i r1 = _mm_loadl_epi64((const __m128i*)(src + x + 4));
1322 __m128 rf0 =_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(r0, r0), 16));
1323 __m128 rf1 =_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(r1, r1), 16));
1324 rf0 = _mm_add_ps(_mm_mul_ps(rf0, scale128), shift128);
1325 rf1 = _mm_add_ps(_mm_mul_ps(rf1, scale128), shift128);
1326 r0 = _mm_cvtps_epi32(rf0);
1327 r1 = _mm_cvtps_epi32(rf1);
1328 r0 = _mm_packs_epi32(r0, r1);
1329 _mm_storeu_si128((__m128i*)(dst + x), r0);
1334 for(; x < size.width; x++ )
1335 dst[x] = saturate_cast<short>(src[x]*scale + shift);
1340 cvtScale_<short, int, float>( const short* src, size_t sstep,
1341 int* dst, size_t dstep, Size size,
1342 float scale, float shift )
1344 sstep /= sizeof(src[0]);
1345 dstep /= sizeof(dst[0]);
1347 for( ; size.height--; src += sstep, dst += dstep )
1354 __m128 scale128 = _mm_set1_ps (scale);
1355 __m128 shift128 = _mm_set1_ps (shift);
1356 for(; x <= size.width - 8; x += 8 )
1358 __m128i r0 = _mm_loadl_epi64((const __m128i*)(src + x));
1359 __m128i r1 = _mm_loadl_epi64((const __m128i*)(src + x + 4));
1360 __m128 rf0 =_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(r0, r0), 16));
1361 __m128 rf1 =_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(r1, r1), 16));
1362 rf0 = _mm_add_ps(_mm_mul_ps(rf0, scale128), shift128);
1363 rf1 = _mm_add_ps(_mm_mul_ps(rf1, scale128), shift128);
1364 r0 = _mm_cvtps_epi32(rf0);
1365 r1 = _mm_cvtps_epi32(rf1);
1367 _mm_storeu_si128((__m128i*)(dst + x), r0);
1368 _mm_storeu_si128((__m128i*)(dst + x + 4), r1);
1373 //We will wait Haswell
1376 if(USE_AVX)//2X - bad variant
1378 ////TODO:AVX implementation (optimization?) required
1379 __m256 scale256 = _mm256_set1_ps (scale);
1380 __m256 shift256 = _mm256_set1_ps (shift);
1381 for(; x <= size.width - 8; x += 8 )
1383 __m256i buf = _mm256_set_epi32((int)(*(src+x+7)),(int)(*(src+x+6)),(int)(*(src+x+5)),(int)(*(src+x+4)),(int)(*(src+x+3)),(int)(*(src+x+2)),(int)(*(src+x+1)),(int)(*(src+x)));
1384 __m256 r0 = _mm256_add_ps( _mm256_mul_ps(_mm256_cvtepi32_ps (buf), scale256), shift256);
1385 __m256i res = _mm256_cvtps_epi32(r0);
1386 _mm256_storeu_si256 ((__m256i*)(dst+x), res);
1391 for(; x < size.width; x++ )
1392 dst[x] = saturate_cast<int>(src[x]*scale + shift);
1396 template<typename T, typename DT> static void
1397 cvt_( const T* src, size_t sstep,
1398 DT* dst, size_t dstep, Size size )
1400 sstep /= sizeof(src[0]);
1401 dstep /= sizeof(dst[0]);
1403 for( ; size.height--; src += sstep, dst += dstep )
1406 #if CV_ENABLE_UNROLLED
1407 for( ; x <= size.width - 4; x += 4 )
1410 t0 = saturate_cast<DT>(src[x]);
1411 t1 = saturate_cast<DT>(src[x+1]);
1412 dst[x] = t0; dst[x+1] = t1;
1413 t0 = saturate_cast<DT>(src[x+2]);
1414 t1 = saturate_cast<DT>(src[x+3]);
1415 dst[x+2] = t0; dst[x+3] = t1;
1418 for( ; x < size.width; x++ )
1419 dst[x] = saturate_cast<DT>(src[x]);
1423 //vz optimized template specialization, test Core_ConvertScale/ElemWiseTest
1425 cvt_<float, short>( const float* src, size_t sstep,
1426 short* dst, size_t dstep, Size size )
1428 sstep /= sizeof(src[0]);
1429 dstep /= sizeof(dst[0]);
1431 for( ; size.height--; src += sstep, dst += dstep )
1436 for( ; x <= size.width - 8; x += 8 )
1438 __m128 src128 = _mm_loadu_ps (src + x);
1439 __m128i src_int128 = _mm_cvtps_epi32 (src128);
1441 src128 = _mm_loadu_ps (src + x + 4);
1442 __m128i src1_int128 = _mm_cvtps_epi32 (src128);
1444 src1_int128 = _mm_packs_epi32(src_int128, src1_int128);
1445 _mm_storeu_si128((__m128i*)(dst + x),src1_int128);
1449 for( ; x < size.width; x++ )
1450 dst[x] = saturate_cast<short>(src[x]);
1456 template<typename T> static void
1457 cpy_( const T* src, size_t sstep, T* dst, size_t dstep, Size size )
1459 sstep /= sizeof(src[0]);
1460 dstep /= sizeof(dst[0]);
1462 for( ; size.height--; src += sstep, dst += dstep )
1463 memcpy(dst, src, size.width*sizeof(src[0]));
1466 #define DEF_CVT_SCALE_ABS_FUNC(suffix, tfunc, stype, dtype, wtype) \
1467 static void cvtScaleAbs##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
1468 dtype* dst, size_t dstep, Size size, double* scale) \
1470 tfunc(src, sstep, dst, dstep, size, (wtype)scale[0], (wtype)scale[1]); \
1473 #define DEF_CVT_SCALE_FUNC(suffix, stype, dtype, wtype) \
1474 static void cvtScale##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
1475 dtype* dst, size_t dstep, Size size, double* scale) \
1477 cvtScale_(src, sstep, dst, dstep, size, (wtype)scale[0], (wtype)scale[1]); \
1480 #if defined(HAVE_IPP)
1481 #define DEF_CVT_FUNC_F(suffix, stype, dtype, ippFavor) \
1482 static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
1483 dtype* dst, size_t dstep, Size size, double*) \
1487 if (ippiConvert_##ippFavor(src, (int)sstep, dst, (int)dstep, ippiSize(size.width, size.height)) >= 0) \
1489 setIppErrorStatus(); \
1491 cvt_(src, sstep, dst, dstep, size); \
1494 #define DEF_CVT_FUNC_F2(suffix, stype, dtype, ippFavor) \
1495 static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
1496 dtype* dst, size_t dstep, Size size, double*) \
1500 if (ippiConvert_##ippFavor(src, (int)sstep, dst, (int)dstep, ippiSize(size.width, size.height), ippRndFinancial, 0) >= 0) \
1502 setIppErrorStatus(); \
1504 cvt_(src, sstep, dst, dstep, size); \
1507 #define DEF_CVT_FUNC_F(suffix, stype, dtype, ippFavor) \
1508 static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
1509 dtype* dst, size_t dstep, Size size, double*) \
1511 cvt_(src, sstep, dst, dstep, size); \
1513 #define DEF_CVT_FUNC_F2 DEF_CVT_FUNC_F
1516 #define DEF_CVT_FUNC(suffix, stype, dtype) \
1517 static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
1518 dtype* dst, size_t dstep, Size size, double*) \
1520 cvt_(src, sstep, dst, dstep, size); \
1523 #define DEF_CPY_FUNC(suffix, stype) \
1524 static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
1525 stype* dst, size_t dstep, Size size, double*) \
1527 cpy_(src, sstep, dst, dstep, size); \
1531 DEF_CVT_SCALE_ABS_FUNC(8u, cvtScaleAbs_, uchar, uchar, float)
1532 DEF_CVT_SCALE_ABS_FUNC(8s8u, cvtScaleAbs_, schar, uchar, float)
1533 DEF_CVT_SCALE_ABS_FUNC(16u8u, cvtScaleAbs_, ushort, uchar, float)
1534 DEF_CVT_SCALE_ABS_FUNC(16s8u, cvtScaleAbs_, short, uchar, float)
1535 DEF_CVT_SCALE_ABS_FUNC(32s8u, cvtScaleAbs_, int, uchar, float)
1536 DEF_CVT_SCALE_ABS_FUNC(32f8u, cvtScaleAbs_, float, uchar, float)
1537 DEF_CVT_SCALE_ABS_FUNC(64f8u, cvtScaleAbs_, double, uchar, float)
1539 DEF_CVT_SCALE_FUNC(8u, uchar, uchar, float)
1540 DEF_CVT_SCALE_FUNC(8s8u, schar, uchar, float)
1541 DEF_CVT_SCALE_FUNC(16u8u, ushort, uchar, float)
1542 DEF_CVT_SCALE_FUNC(16s8u, short, uchar, float)
1543 DEF_CVT_SCALE_FUNC(32s8u, int, uchar, float)
1544 DEF_CVT_SCALE_FUNC(32f8u, float, uchar, float)
1545 DEF_CVT_SCALE_FUNC(64f8u, double, uchar, float)
1547 DEF_CVT_SCALE_FUNC(8u8s, uchar, schar, float)
1548 DEF_CVT_SCALE_FUNC(8s, schar, schar, float)
1549 DEF_CVT_SCALE_FUNC(16u8s, ushort, schar, float)
1550 DEF_CVT_SCALE_FUNC(16s8s, short, schar, float)
1551 DEF_CVT_SCALE_FUNC(32s8s, int, schar, float)
1552 DEF_CVT_SCALE_FUNC(32f8s, float, schar, float)
1553 DEF_CVT_SCALE_FUNC(64f8s, double, schar, float)
1555 DEF_CVT_SCALE_FUNC(8u16u, uchar, ushort, float)
1556 DEF_CVT_SCALE_FUNC(8s16u, schar, ushort, float)
1557 DEF_CVT_SCALE_FUNC(16u, ushort, ushort, float)
1558 DEF_CVT_SCALE_FUNC(16s16u, short, ushort, float)
1559 DEF_CVT_SCALE_FUNC(32s16u, int, ushort, float)
1560 DEF_CVT_SCALE_FUNC(32f16u, float, ushort, float)
1561 DEF_CVT_SCALE_FUNC(64f16u, double, ushort, float)
1563 DEF_CVT_SCALE_FUNC(8u16s, uchar, short, float)
1564 DEF_CVT_SCALE_FUNC(8s16s, schar, short, float)
1565 DEF_CVT_SCALE_FUNC(16u16s, ushort, short, float)
1566 DEF_CVT_SCALE_FUNC(16s, short, short, float)
1567 DEF_CVT_SCALE_FUNC(32s16s, int, short, float)
1568 DEF_CVT_SCALE_FUNC(32f16s, float, short, float)
1569 DEF_CVT_SCALE_FUNC(64f16s, double, short, float)
1571 DEF_CVT_SCALE_FUNC(8u32s, uchar, int, float)
1572 DEF_CVT_SCALE_FUNC(8s32s, schar, int, float)
1573 DEF_CVT_SCALE_FUNC(16u32s, ushort, int, float)
1574 DEF_CVT_SCALE_FUNC(16s32s, short, int, float)
1575 DEF_CVT_SCALE_FUNC(32s, int, int, double)
1576 DEF_CVT_SCALE_FUNC(32f32s, float, int, float)
1577 DEF_CVT_SCALE_FUNC(64f32s, double, int, double)
1579 DEF_CVT_SCALE_FUNC(8u32f, uchar, float, float)
1580 DEF_CVT_SCALE_FUNC(8s32f, schar, float, float)
1581 DEF_CVT_SCALE_FUNC(16u32f, ushort, float, float)
1582 DEF_CVT_SCALE_FUNC(16s32f, short, float, float)
1583 DEF_CVT_SCALE_FUNC(32s32f, int, float, double)
1584 DEF_CVT_SCALE_FUNC(32f, float, float, float)
1585 DEF_CVT_SCALE_FUNC(64f32f, double, float, double)
1587 DEF_CVT_SCALE_FUNC(8u64f, uchar, double, double)
1588 DEF_CVT_SCALE_FUNC(8s64f, schar, double, double)
1589 DEF_CVT_SCALE_FUNC(16u64f, ushort, double, double)
1590 DEF_CVT_SCALE_FUNC(16s64f, short, double, double)
1591 DEF_CVT_SCALE_FUNC(32s64f, int, double, double)
1592 DEF_CVT_SCALE_FUNC(32f64f, float, double, double)
1593 DEF_CVT_SCALE_FUNC(64f, double, double, double)
1595 DEF_CPY_FUNC(8u, uchar)
1596 DEF_CVT_FUNC_F(8s8u, schar, uchar, 8s8u_C1Rs)
1597 DEF_CVT_FUNC_F(16u8u, ushort, uchar, 16u8u_C1R)
1598 DEF_CVT_FUNC_F(16s8u, short, uchar, 16s8u_C1R)
1599 DEF_CVT_FUNC_F(32s8u, int, uchar, 32s8u_C1R)
1600 DEF_CVT_FUNC_F2(32f8u, float, uchar, 32f8u_C1RSfs)
1601 DEF_CVT_FUNC(64f8u, double, uchar)
1603 DEF_CVT_FUNC_F2(8u8s, uchar, schar, 8u8s_C1RSfs)
1604 DEF_CVT_FUNC_F2(16u8s, ushort, schar, 16u8s_C1RSfs)
1605 DEF_CVT_FUNC_F2(16s8s, short, schar, 16s8s_C1RSfs)
1606 DEF_CVT_FUNC_F(32s8s, int, schar, 32s8s_C1R)
1607 DEF_CVT_FUNC_F2(32f8s, float, schar, 32f8s_C1RSfs)
1608 DEF_CVT_FUNC(64f8s, double, schar)
1610 DEF_CVT_FUNC_F(8u16u, uchar, ushort, 8u16u_C1R)
1611 DEF_CVT_FUNC_F(8s16u, schar, ushort, 8s16u_C1Rs)
1612 DEF_CPY_FUNC(16u, ushort)
1613 DEF_CVT_FUNC_F(16s16u, short, ushort, 16s16u_C1Rs)
1614 DEF_CVT_FUNC_F2(32s16u, int, ushort, 32s16u_C1RSfs)
1615 DEF_CVT_FUNC_F2(32f16u, float, ushort, 32f16u_C1RSfs)
1616 DEF_CVT_FUNC(64f16u, double, ushort)
1618 DEF_CVT_FUNC_F(8u16s, uchar, short, 8u16s_C1R)
1619 DEF_CVT_FUNC_F(8s16s, schar, short, 8s16s_C1R)
1620 DEF_CVT_FUNC_F2(16u16s, ushort, short, 16u16s_C1RSfs)
1621 DEF_CVT_FUNC_F2(32s16s, int, short, 32s16s_C1RSfs)
1622 DEF_CVT_FUNC_F2(32f16s, float, short, 32f16s_C1RSfs)
1623 DEF_CVT_FUNC(64f16s, double, short)
1625 DEF_CVT_FUNC_F(8u32s, uchar, int, 8u32s_C1R)
1626 DEF_CVT_FUNC_F(8s32s, schar, int, 8s32s_C1R)
1627 DEF_CVT_FUNC_F(16u32s, ushort, int, 16u32s_C1R)
1628 DEF_CVT_FUNC_F(16s32s, short, int, 16s32s_C1R)
1629 DEF_CPY_FUNC(32s, int)
1630 DEF_CVT_FUNC_F2(32f32s, float, int, 32f32s_C1RSfs)
1631 DEF_CVT_FUNC(64f32s, double, int)
1633 DEF_CVT_FUNC_F(8u32f, uchar, float, 8u32f_C1R)
1634 DEF_CVT_FUNC_F(8s32f, schar, float, 8s32f_C1R)
1635 DEF_CVT_FUNC_F(16u32f, ushort, float, 16u32f_C1R)
1636 DEF_CVT_FUNC_F(16s32f, short, float, 16s32f_C1R)
1637 DEF_CVT_FUNC_F(32s32f, int, float, 32s32f_C1R)
1638 DEF_CVT_FUNC(64f32f, double, float)
1640 DEF_CVT_FUNC(8u64f, uchar, double)
1641 DEF_CVT_FUNC(8s64f, schar, double)
1642 DEF_CVT_FUNC(16u64f, ushort, double)
1643 DEF_CVT_FUNC(16s64f, short, double)
1644 DEF_CVT_FUNC(32s64f, int, double)
1645 DEF_CVT_FUNC(32f64f, float, double)
1646 DEF_CPY_FUNC(64s, int64)
1648 static BinaryFunc getCvtScaleAbsFunc(int depth)
1650 static BinaryFunc cvtScaleAbsTab[] =
1652 (BinaryFunc)cvtScaleAbs8u, (BinaryFunc)cvtScaleAbs8s8u, (BinaryFunc)cvtScaleAbs16u8u,
1653 (BinaryFunc)cvtScaleAbs16s8u, (BinaryFunc)cvtScaleAbs32s8u, (BinaryFunc)cvtScaleAbs32f8u,
1654 (BinaryFunc)cvtScaleAbs64f8u, 0
1657 return cvtScaleAbsTab[depth];
1660 BinaryFunc getConvertFunc(int sdepth, int ddepth)
1662 static BinaryFunc cvtTab[][8] =
1665 (BinaryFunc)(cvt8u), (BinaryFunc)GET_OPTIMIZED(cvt8s8u), (BinaryFunc)GET_OPTIMIZED(cvt16u8u),
1666 (BinaryFunc)GET_OPTIMIZED(cvt16s8u), (BinaryFunc)GET_OPTIMIZED(cvt32s8u), (BinaryFunc)GET_OPTIMIZED(cvt32f8u),
1667 (BinaryFunc)GET_OPTIMIZED(cvt64f8u), 0
1670 (BinaryFunc)GET_OPTIMIZED(cvt8u8s), (BinaryFunc)cvt8u, (BinaryFunc)GET_OPTIMIZED(cvt16u8s),
1671 (BinaryFunc)GET_OPTIMIZED(cvt16s8s), (BinaryFunc)GET_OPTIMIZED(cvt32s8s), (BinaryFunc)GET_OPTIMIZED(cvt32f8s),
1672 (BinaryFunc)GET_OPTIMIZED(cvt64f8s), 0
1675 (BinaryFunc)GET_OPTIMIZED(cvt8u16u), (BinaryFunc)GET_OPTIMIZED(cvt8s16u), (BinaryFunc)cvt16u,
1676 (BinaryFunc)GET_OPTIMIZED(cvt16s16u), (BinaryFunc)GET_OPTIMIZED(cvt32s16u), (BinaryFunc)GET_OPTIMIZED(cvt32f16u),
1677 (BinaryFunc)GET_OPTIMIZED(cvt64f16u), 0
1680 (BinaryFunc)GET_OPTIMIZED(cvt8u16s), (BinaryFunc)GET_OPTIMIZED(cvt8s16s), (BinaryFunc)GET_OPTIMIZED(cvt16u16s),
1681 (BinaryFunc)cvt16u, (BinaryFunc)GET_OPTIMIZED(cvt32s16s), (BinaryFunc)GET_OPTIMIZED(cvt32f16s),
1682 (BinaryFunc)GET_OPTIMIZED(cvt64f16s), 0
1685 (BinaryFunc)GET_OPTIMIZED(cvt8u32s), (BinaryFunc)GET_OPTIMIZED(cvt8s32s), (BinaryFunc)GET_OPTIMIZED(cvt16u32s),
1686 (BinaryFunc)GET_OPTIMIZED(cvt16s32s), (BinaryFunc)cvt32s, (BinaryFunc)GET_OPTIMIZED(cvt32f32s),
1687 (BinaryFunc)GET_OPTIMIZED(cvt64f32s), 0
1690 (BinaryFunc)GET_OPTIMIZED(cvt8u32f), (BinaryFunc)GET_OPTIMIZED(cvt8s32f), (BinaryFunc)GET_OPTIMIZED(cvt16u32f),
1691 (BinaryFunc)GET_OPTIMIZED(cvt16s32f), (BinaryFunc)GET_OPTIMIZED(cvt32s32f), (BinaryFunc)cvt32s,
1692 (BinaryFunc)GET_OPTIMIZED(cvt64f32f), 0
1695 (BinaryFunc)GET_OPTIMIZED(cvt8u64f), (BinaryFunc)GET_OPTIMIZED(cvt8s64f), (BinaryFunc)GET_OPTIMIZED(cvt16u64f),
1696 (BinaryFunc)GET_OPTIMIZED(cvt16s64f), (BinaryFunc)GET_OPTIMIZED(cvt32s64f), (BinaryFunc)GET_OPTIMIZED(cvt32f64f),
1697 (BinaryFunc)(cvt64s), 0
1700 0, 0, 0, 0, 0, 0, 0, 0
1704 return cvtTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)];
1707 static BinaryFunc getConvertScaleFunc(int sdepth, int ddepth)
1709 static BinaryFunc cvtScaleTab[][8] =
1712 (BinaryFunc)GET_OPTIMIZED(cvtScale8u), (BinaryFunc)GET_OPTIMIZED(cvtScale8s8u), (BinaryFunc)GET_OPTIMIZED(cvtScale16u8u),
1713 (BinaryFunc)GET_OPTIMIZED(cvtScale16s8u), (BinaryFunc)GET_OPTIMIZED(cvtScale32s8u), (BinaryFunc)GET_OPTIMIZED(cvtScale32f8u),
1714 (BinaryFunc)cvtScale64f8u, 0
1717 (BinaryFunc)GET_OPTIMIZED(cvtScale8u8s), (BinaryFunc)GET_OPTIMIZED(cvtScale8s), (BinaryFunc)GET_OPTIMIZED(cvtScale16u8s),
1718 (BinaryFunc)GET_OPTIMIZED(cvtScale16s8s), (BinaryFunc)GET_OPTIMIZED(cvtScale32s8s), (BinaryFunc)GET_OPTIMIZED(cvtScale32f8s),
1719 (BinaryFunc)cvtScale64f8s, 0
1722 (BinaryFunc)GET_OPTIMIZED(cvtScale8u16u), (BinaryFunc)GET_OPTIMIZED(cvtScale8s16u), (BinaryFunc)GET_OPTIMIZED(cvtScale16u),
1723 (BinaryFunc)GET_OPTIMIZED(cvtScale16s16u), (BinaryFunc)GET_OPTIMIZED(cvtScale32s16u), (BinaryFunc)GET_OPTIMIZED(cvtScale32f16u),
1724 (BinaryFunc)cvtScale64f16u, 0
1727 (BinaryFunc)GET_OPTIMIZED(cvtScale8u16s), (BinaryFunc)GET_OPTIMIZED(cvtScale8s16s), (BinaryFunc)GET_OPTIMIZED(cvtScale16u16s),
1728 (BinaryFunc)GET_OPTIMIZED(cvtScale16s), (BinaryFunc)GET_OPTIMIZED(cvtScale32s16s), (BinaryFunc)GET_OPTIMIZED(cvtScale32f16s),
1729 (BinaryFunc)cvtScale64f16s, 0
1732 (BinaryFunc)GET_OPTIMIZED(cvtScale8u32s), (BinaryFunc)GET_OPTIMIZED(cvtScale8s32s), (BinaryFunc)GET_OPTIMIZED(cvtScale16u32s),
1733 (BinaryFunc)GET_OPTIMIZED(cvtScale16s32s), (BinaryFunc)GET_OPTIMIZED(cvtScale32s), (BinaryFunc)GET_OPTIMIZED(cvtScale32f32s),
1734 (BinaryFunc)cvtScale64f32s, 0
1737 (BinaryFunc)GET_OPTIMIZED(cvtScale8u32f), (BinaryFunc)GET_OPTIMIZED(cvtScale8s32f), (BinaryFunc)GET_OPTIMIZED(cvtScale16u32f),
1738 (BinaryFunc)GET_OPTIMIZED(cvtScale16s32f), (BinaryFunc)GET_OPTIMIZED(cvtScale32s32f), (BinaryFunc)GET_OPTIMIZED(cvtScale32f),
1739 (BinaryFunc)cvtScale64f32f, 0
1742 (BinaryFunc)cvtScale8u64f, (BinaryFunc)cvtScale8s64f, (BinaryFunc)cvtScale16u64f,
1743 (BinaryFunc)cvtScale16s64f, (BinaryFunc)cvtScale32s64f, (BinaryFunc)cvtScale32f64f,
1744 (BinaryFunc)cvtScale64f, 0
1747 0, 0, 0, 0, 0, 0, 0, 0
1751 return cvtScaleTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)];
1756 static bool ocl_convertScaleAbs( InputArray _src, OutputArray _dst, double alpha, double beta )
1758 const ocl::Device & d = ocl::Device::getDefault();
1759 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
1760 kercn = ocl::predictOptimalVectorWidth(_src, _dst), rowsPerWI = d.isIntel() ? 4 : 1;
1761 bool doubleSupport = d.doubleFPConfig() > 0;
1763 if (depth == CV_32F || depth == CV_64F)
1767 int wdepth = std::max(depth, CV_32F);
1768 ocl::Kernel k("KF", ocl::core::arithm_oclsrc,
1769 format("-D OP_CONVERT_SCALE_ABS -D UNARY_OP -D dstT=%s -D srcT1=%s"
1770 " -D workT=%s -D wdepth=%d -D convertToWT1=%s -D convertToDT=%s"
1771 " -D workT1=%s -D rowsPerWI=%d%s",
1772 ocl::typeToStr(CV_8UC(kercn)),
1773 ocl::typeToStr(CV_MAKE_TYPE(depth, kercn)),
1774 ocl::typeToStr(CV_MAKE_TYPE(wdepth, kercn)), wdepth,
1775 ocl::convertTypeStr(depth, wdepth, kercn, cvt[0]),
1776 ocl::convertTypeStr(wdepth, CV_8U, kercn, cvt[1]),
1777 ocl::typeToStr(wdepth), rowsPerWI,
1778 doubleSupport ? " -D DOUBLE_SUPPORT" : ""));
1782 UMat src = _src.getUMat();
1783 _dst.create(src.size(), CV_8UC(cn));
1784 UMat dst = _dst.getUMat();
1786 ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
1787 dstarg = ocl::KernelArg::WriteOnly(dst, cn, kercn);
1789 if (wdepth == CV_32F)
1790 k.args(srcarg, dstarg, (float)alpha, (float)beta);
1791 else if (wdepth == CV_64F)
1792 k.args(srcarg, dstarg, alpha, beta);
1794 size_t globalsize[2] = { src.cols * cn / kercn, (src.rows + rowsPerWI - 1) / rowsPerWI };
1795 return k.run(2, globalsize, NULL, false);
1802 void cv::convertScaleAbs( InputArray _src, OutputArray _dst, double alpha, double beta )
1804 CV_OCL_RUN(_src.dims() <= 2 && _dst.isUMat(),
1805 ocl_convertScaleAbs(_src, _dst, alpha, beta))
1807 Mat src = _src.getMat();
1808 int cn = src.channels();
1809 double scale[] = {alpha, beta};
1810 _dst.create( src.dims, src.size, CV_8UC(cn) );
1811 Mat dst = _dst.getMat();
1812 BinaryFunc func = getCvtScaleAbsFunc(src.depth());
1813 CV_Assert( func != 0 );
1817 Size sz = getContinuousSize(src, dst, cn);
1818 func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale );
1822 const Mat* arrays[] = {&src, &dst, 0};
1824 NAryMatIterator it(arrays, ptrs);
1825 Size sz((int)it.size*cn, 1);
1827 for( size_t i = 0; i < it.nplanes; i++, ++it )
1828 func( ptrs[0], 0, 0, 0, ptrs[1], 0, sz, scale );
1832 void cv::Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const
1834 bool noScale = fabs(alpha-1) < DBL_EPSILON && fabs(beta) < DBL_EPSILON;
1837 _type = _dst.fixedType() ? _dst.type() : type();
1839 _type = CV_MAKETYPE(CV_MAT_DEPTH(_type), channels());
1841 int sdepth = depth(), ddepth = CV_MAT_DEPTH(_type);
1842 if( sdepth == ddepth && noScale )
1850 BinaryFunc func = noScale ? getConvertFunc(sdepth, ddepth) : getConvertScaleFunc(sdepth, ddepth);
1851 double scale[] = {alpha, beta};
1852 int cn = channels();
1853 CV_Assert( func != 0 );
1857 _dst.create( size(), _type );
1858 Mat dst = _dst.getMat();
1859 Size sz = getContinuousSize(src, dst, cn);
1860 func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale );
1864 _dst.create( dims, size, _type );
1865 Mat dst = _dst.getMat();
1866 const Mat* arrays[] = {&src, &dst, 0};
1868 NAryMatIterator it(arrays, ptrs);
1869 Size sz((int)(it.size*cn), 1);
1871 for( size_t i = 0; i < it.nplanes; i++, ++it )
1872 func(ptrs[0], 1, 0, 0, ptrs[1], 1, sz, scale);
1876 /****************************************************************************************\
1878 \****************************************************************************************/
1883 template<typename T> static void
1884 LUT8u_( const uchar* src, const T* lut, T* dst, int len, int cn, int lutcn )
1888 for( int i = 0; i < len*cn; i++ )
1889 dst[i] = lut[src[i]];
1893 for( int i = 0; i < len*cn; i += cn )
1894 for( int k = 0; k < cn; k++ )
1895 dst[i+k] = lut[src[i+k]*cn+k];
1899 static void LUT8u_8u( const uchar* src, const uchar* lut, uchar* dst, int len, int cn, int lutcn )
1901 LUT8u_( src, lut, dst, len, cn, lutcn );
1904 static void LUT8u_8s( const uchar* src, const schar* lut, schar* dst, int len, int cn, int lutcn )
1906 LUT8u_( src, lut, dst, len, cn, lutcn );
1909 static void LUT8u_16u( const uchar* src, const ushort* lut, ushort* dst, int len, int cn, int lutcn )
1911 LUT8u_( src, lut, dst, len, cn, lutcn );
1914 static void LUT8u_16s( const uchar* src, const short* lut, short* dst, int len, int cn, int lutcn )
1916 LUT8u_( src, lut, dst, len, cn, lutcn );
1919 static void LUT8u_32s( const uchar* src, const int* lut, int* dst, int len, int cn, int lutcn )
1921 LUT8u_( src, lut, dst, len, cn, lutcn );
1924 static void LUT8u_32f( const uchar* src, const float* lut, float* dst, int len, int cn, int lutcn )
1926 LUT8u_( src, lut, dst, len, cn, lutcn );
1929 static void LUT8u_64f( const uchar* src, const double* lut, double* dst, int len, int cn, int lutcn )
1931 LUT8u_( src, lut, dst, len, cn, lutcn );
1934 typedef void (*LUTFunc)( const uchar* src, const uchar* lut, uchar* dst, int len, int cn, int lutcn );
1936 static LUTFunc lutTab[] =
1938 (LUTFunc)LUT8u_8u, (LUTFunc)LUT8u_8s, (LUTFunc)LUT8u_16u, (LUTFunc)LUT8u_16s,
1939 (LUTFunc)LUT8u_32s, (LUTFunc)LUT8u_32f, (LUTFunc)LUT8u_64f, 0
1944 static bool ocl_LUT(InputArray _src, InputArray _lut, OutputArray _dst)
1946 int lcn = _lut.channels(), dcn = _src.channels(), ddepth = _lut.depth();
1948 UMat src = _src.getUMat(), lut = _lut.getUMat();
1949 _dst.create(src.size(), CV_MAKETYPE(ddepth, dcn));
1950 UMat dst = _dst.getUMat();
1951 int kercn = lcn == 1 ? std::min(4, ocl::predictOptimalVectorWidth(_dst)) : dcn;
1953 ocl::Kernel k("LUT", ocl::core::lut_oclsrc,
1954 format("-D dcn=%d -D lcn=%d -D srcT=%s -D dstT=%s", kercn, lcn,
1955 ocl::typeToStr(src.depth()), ocl::memopTypeToStr(ddepth)));
1959 k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::ReadOnlyNoSize(lut),
1960 ocl::KernelArg::WriteOnly(dst, dcn, kercn));
1962 size_t globalSize[2] = { dst.cols * dcn / kercn, (dst.rows + 3) / 4 };
1963 return k.run(2, globalSize, NULL, false);
1968 #if defined(HAVE_IPP)
1971 #if 0 // there are no performance benefits (PR #2653)
1972 class IppLUTParallelBody_LUTC1 : public ParallelLoopBody
1980 typedef IppStatus (*IppFn)(const Ipp8u* pSrc, int srcStep, void* pDst, int dstStep,
1981 IppiSize roiSize, const void* pTable, int nBitSize);
1986 IppLUTParallelBody_LUTC1(const Mat& src, const Mat& lut, Mat& dst, bool* _ok)
1987 : ok(_ok), src_(src), lut_(lut), dst_(dst)
1989 width = dst.cols * dst.channels();
1991 size_t elemSize1 = CV_ELEM_SIZE1(dst.depth());
1994 elemSize1 == 1 ? (IppFn)ippiLUTPalette_8u_C1R :
1995 elemSize1 == 4 ? (IppFn)ippiLUTPalette_8u32u_C1R :
2001 void operator()( const cv::Range& range ) const
2006 const int row0 = range.start;
2007 const int row1 = range.end;
2009 Mat src = src_.rowRange(row0, row1);
2010 Mat dst = dst_.rowRange(row0, row1);
2012 IppiSize sz = { width, dst.rows };
2014 CV_DbgAssert(fn != NULL);
2015 if (fn(src.data, (int)src.step[0], dst.data, (int)dst.step[0], sz, lut_.data, 8) < 0)
2017 setIppErrorStatus();
2022 IppLUTParallelBody_LUTC1(const IppLUTParallelBody_LUTC1&);
2023 IppLUTParallelBody_LUTC1& operator=(const IppLUTParallelBody_LUTC1&);
2027 class IppLUTParallelBody_LUTCN : public ParallelLoopBody
2040 IppLUTParallelBody_LUTCN(const Mat& src, const Mat& lut, Mat& dst, bool* _ok)
2041 : ok(_ok), src_(src), lut_(lut), dst_(dst), lutBuffer(NULL)
2043 lutcn = lut.channels();
2044 IppiSize sz256 = {256, 1};
2046 size_t elemSize1 = dst.elemSize1();
2047 CV_DbgAssert(elemSize1 == 1);
2048 lutBuffer = (uchar*)ippMalloc(256 * (int)elemSize1 * 4);
2049 lutTable[0] = lutBuffer + 0;
2050 lutTable[1] = lutBuffer + 1 * 256 * elemSize1;
2051 lutTable[2] = lutBuffer + 2 * 256 * elemSize1;
2052 lutTable[3] = lutBuffer + 3 * 256 * elemSize1;
2054 CV_DbgAssert(lutcn == 3 || lutcn == 4);
2057 IppStatus status = ippiCopy_8u_C3P3R(lut.data, (int)lut.step[0], lutTable, (int)lut.step[0], sz256);
2060 setIppErrorStatus();
2064 else if (lutcn == 4)
2066 IppStatus status = ippiCopy_8u_C4P4R(lut.data, (int)lut.step[0], lutTable, (int)lut.step[0], sz256);
2069 setIppErrorStatus();
2077 ~IppLUTParallelBody_LUTCN()
2079 if (lutBuffer != NULL)
2085 void operator()( const cv::Range& range ) const
2090 const int row0 = range.start;
2091 const int row1 = range.end;
2093 Mat src = src_.rowRange(row0, row1);
2094 Mat dst = dst_.rowRange(row0, row1);
2098 if (ippiLUTPalette_8u_C3R(
2099 src.data, (int)src.step[0], dst.data, (int)dst.step[0],
2100 ippiSize(dst.size()), lutTable, 8) >= 0)
2103 else if (lutcn == 4)
2105 if (ippiLUTPalette_8u_C4R(
2106 src.data, (int)src.step[0], dst.data, (int)dst.step[0],
2107 ippiSize(dst.size()), lutTable, 8) >= 0)
2110 setIppErrorStatus();
2114 IppLUTParallelBody_LUTCN(const IppLUTParallelBody_LUTCN&);
2115 IppLUTParallelBody_LUTCN& operator=(const IppLUTParallelBody_LUTCN&);
2120 class LUTParallelBody : public ParallelLoopBody
2130 LUTParallelBody(const Mat& src, const Mat& lut, Mat& dst, bool* _ok)
2131 : ok(_ok), src_(src), lut_(lut), dst_(dst)
2133 func = lutTab[lut.depth()];
2134 *ok = (func != NULL);
2137 void operator()( const cv::Range& range ) const
2141 const int row0 = range.start;
2142 const int row1 = range.end;
2144 Mat src = src_.rowRange(row0, row1);
2145 Mat dst = dst_.rowRange(row0, row1);
2147 int cn = src.channels();
2148 int lutcn = lut_.channels();
2150 const Mat* arrays[] = {&src, &dst, 0};
2152 NAryMatIterator it(arrays, ptrs);
2153 int len = (int)it.size;
2155 for( size_t i = 0; i < it.nplanes; i++, ++it )
2156 func(ptrs[0], lut_.data, ptrs[1], len, cn, lutcn);
2159 LUTParallelBody(const LUTParallelBody&);
2160 LUTParallelBody& operator=(const LUTParallelBody&);
2165 void cv::LUT( InputArray _src, InputArray _lut, OutputArray _dst )
2167 int cn = _src.channels(), depth = _src.depth();
2168 int lutcn = _lut.channels();
2170 CV_Assert( (lutcn == cn || lutcn == 1) &&
2171 _lut.total() == 256 && _lut.isContinuous() &&
2172 (depth == CV_8U || depth == CV_8S) );
2174 CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2,
2175 ocl_LUT(_src, _lut, _dst))
2177 Mat src = _src.getMat(), lut = _lut.getMat();
2178 _dst.create(src.dims, src.size, CV_MAKETYPE(_lut.depth(), cn));
2179 Mat dst = _dst.getMat();
2181 if (_src.dims() <= 2)
2184 Ptr<ParallelLoopBody> body;
2185 #if defined(HAVE_IPP)
2186 size_t elemSize1 = CV_ELEM_SIZE1(dst.depth());
2187 #if 0 // there are no performance benefits (PR #2653)
2190 ParallelLoopBody* p = new ipp::IppLUTParallelBody_LUTC1(src, lut, dst, &ok);
2195 if ((lutcn == 3 || lutcn == 4) && elemSize1 == 1)
2197 ParallelLoopBody* p = new ipp::IppLUTParallelBody_LUTCN(src, lut, dst, &ok);
2201 if (body == NULL || ok == false)
2204 ParallelLoopBody* p = new LUTParallelBody(src, lut, dst, &ok);
2207 if (body != NULL && ok)
2209 Range all(0, dst.rows);
2210 if (dst.total()>>18)
2211 parallel_for_(all, *body, (double)std::max((size_t)1, dst.total()>>16));
2219 LUTFunc func = lutTab[lut.depth()];
2220 CV_Assert( func != 0 );
2222 const Mat* arrays[] = {&src, &dst, 0};
2224 NAryMatIterator it(arrays, ptrs);
2225 int len = (int)it.size;
2227 for( size_t i = 0; i < it.nplanes; i++, ++it )
2228 func(ptrs[0], lut.data, ptrs[1], len, cn, lutcn);
2235 static bool ocl_normalize( InputArray _src, InputOutputArray _dst, InputArray _mask, int dtype,
2236 double scale, double delta )
2238 UMat src = _src.getUMat();
2241 src.convertTo( _dst, dtype, scale, delta );
2242 else if (src.channels() <= 4)
2244 const ocl::Device & dev = ocl::Device::getDefault();
2246 int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype),
2247 ddepth = CV_MAT_DEPTH(dtype), wdepth = std::max(CV_32F, std::max(sdepth, ddepth)),
2248 rowsPerWI = dev.isIntel() ? 4 : 1;
2250 float fscale = static_cast<float>(scale), fdelta = static_cast<float>(delta);
2251 bool haveScale = std::fabs(scale - 1) > DBL_EPSILON,
2252 haveZeroScale = !(std::fabs(scale) > DBL_EPSILON),
2253 haveDelta = std::fabs(delta) > DBL_EPSILON,
2254 doubleSupport = dev.doubleFPConfig() > 0;
2256 if (!haveScale && !haveDelta && stype == dtype)
2258 _src.copyTo(_dst, _mask);
2263 _dst.setTo(Scalar(delta), _mask);
2267 if ((sdepth == CV_64F || ddepth == CV_64F) && !doubleSupport)
2271 String opts = format("-D srcT=%s -D dstT=%s -D convertToWT=%s -D cn=%d -D rowsPerWI=%d"
2272 " -D convertToDT=%s -D workT=%s%s%s%s -D srcT1=%s -D dstT1=%s",
2273 ocl::typeToStr(stype), ocl::typeToStr(dtype),
2274 ocl::convertTypeStr(sdepth, wdepth, cn, cvt[0]), cn,
2275 rowsPerWI, ocl::convertTypeStr(wdepth, ddepth, cn, cvt[1]),
2276 ocl::typeToStr(CV_MAKE_TYPE(wdepth, cn)),
2277 doubleSupport ? " -D DOUBLE_SUPPORT" : "",
2278 haveScale ? " -D HAVE_SCALE" : "",
2279 haveDelta ? " -D HAVE_DELTA" : "",
2280 ocl::typeToStr(sdepth), ocl::typeToStr(ddepth));
2282 ocl::Kernel k("normalizek", ocl::core::normalize_oclsrc, opts);
2286 UMat mask = _mask.getUMat(), dst = _dst.getUMat();
2288 ocl::KernelArg srcarg = ocl::KernelArg::ReadOnlyNoSize(src),
2289 maskarg = ocl::KernelArg::ReadOnlyNoSize(mask),
2290 dstarg = ocl::KernelArg::ReadWrite(dst);
2295 k.args(srcarg, maskarg, dstarg, fscale, fdelta);
2297 k.args(srcarg, maskarg, dstarg, fscale);
2302 k.args(srcarg, maskarg, dstarg, fdelta);
2304 k.args(srcarg, maskarg, dstarg);
2307 size_t globalsize[2] = { src.cols, (src.rows + rowsPerWI - 1) / rowsPerWI };
2308 return k.run(2, globalsize, NULL, false);
2313 src.convertTo( temp, dtype, scale, delta );
2314 temp.copyTo( _dst, _mask );
2324 void cv::normalize( InputArray _src, InputOutputArray _dst, double a, double b,
2325 int norm_type, int rtype, InputArray _mask )
2327 double scale = 1, shift = 0;
2328 if( norm_type == CV_MINMAX )
2330 double smin = 0, smax = 0;
2331 double dmin = MIN( a, b ), dmax = MAX( a, b );
2332 minMaxLoc( _src, &smin, &smax, 0, 0, _mask );
2333 scale = (dmax - dmin)*(smax - smin > DBL_EPSILON ? 1./(smax - smin) : 0);
2334 shift = dmin - smin*scale;
2336 else if( norm_type == CV_L2 || norm_type == CV_L1 || norm_type == CV_C )
2338 scale = norm( _src, norm_type, _mask );
2339 scale = scale > DBL_EPSILON ? a/scale : 0.;
2343 CV_Error( CV_StsBadArg, "Unknown/unsupported norm type" );
2345 int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
2347 rtype = _dst.fixedType() ? _dst.depth() : depth;
2348 _dst.createSameSize(_src, CV_MAKETYPE(rtype, cn));
2350 CV_OCL_RUN(_dst.isUMat(),
2351 ocl_normalize(_src, _dst, _mask, rtype, scale, shift))
2353 Mat src = _src.getMat(), dst = _dst.getMat();
2355 src.convertTo( dst, rtype, scale, shift );
2359 src.convertTo( temp, rtype, scale, shift );
2360 temp.copyTo( dst, _mask );
2365 cvSplit( const void* srcarr, void* dstarr0, void* dstarr1, void* dstarr2, void* dstarr3 )
2367 void* dptrs[] = { dstarr0, dstarr1, dstarr2, dstarr3 };
2368 cv::Mat src = cv::cvarrToMat(srcarr);
2370 for( i = 0; i < 4; i++ )
2371 nz += dptrs[i] != 0;
2372 CV_Assert( nz > 0 );
2373 std::vector<cv::Mat> dvec(nz);
2374 std::vector<int> pairs(nz*2);
2376 for( i = j = 0; i < 4; i++ )
2380 dvec[j] = cv::cvarrToMat(dptrs[i]);
2381 CV_Assert( dvec[j].size() == src.size() );
2382 CV_Assert( dvec[j].depth() == src.depth() );
2383 CV_Assert( dvec[j].channels() == 1 );
2384 CV_Assert( i < src.channels() );
2390 if( nz == src.channels() )
2391 cv::split( src, dvec );
2394 cv::mixChannels( &src, 1, &dvec[0], nz, &pairs[0], nz );
2400 cvMerge( const void* srcarr0, const void* srcarr1, const void* srcarr2,
2401 const void* srcarr3, void* dstarr )
2403 const void* sptrs[] = { srcarr0, srcarr1, srcarr2, srcarr3 };
2404 cv::Mat dst = cv::cvarrToMat(dstarr);
2406 for( i = 0; i < 4; i++ )
2407 nz += sptrs[i] != 0;
2408 CV_Assert( nz > 0 );
2409 std::vector<cv::Mat> svec(nz);
2410 std::vector<int> pairs(nz*2);
2412 for( i = j = 0; i < 4; i++ )
2416 svec[j] = cv::cvarrToMat(sptrs[i]);
2417 CV_Assert( svec[j].size == dst.size &&
2418 svec[j].depth() == dst.depth() &&
2419 svec[j].channels() == 1 && i < dst.channels() );
2426 if( nz == dst.channels() )
2427 cv::merge( svec, dst );
2430 cv::mixChannels( &svec[0], nz, &dst, 1, &pairs[0], nz );
2436 cvMixChannels( const CvArr** src, int src_count,
2437 CvArr** dst, int dst_count,
2438 const int* from_to, int pair_count )
2440 cv::AutoBuffer<cv::Mat> buf(src_count + dst_count);
2443 for( i = 0; i < src_count; i++ )
2444 buf[i] = cv::cvarrToMat(src[i]);
2445 for( i = 0; i < dst_count; i++ )
2446 buf[i+src_count] = cv::cvarrToMat(dst[i]);
2447 cv::mixChannels(&buf[0], src_count, &buf[src_count], dst_count, from_to, pair_count);
2451 cvConvertScaleAbs( const void* srcarr, void* dstarr,
2452 double scale, double shift )
2454 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
2455 CV_Assert( src.size == dst.size && dst.type() == CV_8UC(src.channels()));
2456 cv::convertScaleAbs( src, dst, scale, shift );
2460 cvConvertScale( const void* srcarr, void* dstarr,
2461 double scale, double shift )
2463 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
2465 CV_Assert( src.size == dst.size && src.channels() == dst.channels() );
2466 src.convertTo(dst, dst.type(), scale, shift);
2469 CV_IMPL void cvLUT( const void* srcarr, void* dstarr, const void* lutarr )
2471 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), lut = cv::cvarrToMat(lutarr);
2473 CV_Assert( dst.size() == src.size() && dst.type() == CV_MAKETYPE(lut.depth(), src.channels()) );
2474 cv::LUT( src, lut, dst );
2477 CV_IMPL void cvNormalize( const CvArr* srcarr, CvArr* dstarr,
2478 double a, double b, int norm_type, const CvArr* maskarr )
2480 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), mask;
2482 mask = cv::cvarrToMat(maskarr);
2483 CV_Assert( dst.size() == src.size() && src.channels() == dst.channels() );
2484 cv::normalize( src, dst, a, b, norm_type, dst.type(), mask );