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"
48 /****************************************************************************************\
50 \****************************************************************************************/
52 template<typename T> static void
53 split_( const T* src, T** dst, int len, int cn )
55 int k = cn % 4 ? cn % 4 : 4;
60 for( i = j = 0; i < len; i++, j += cn )
65 T *dst0 = dst[0], *dst1 = dst[1];
66 for( i = j = 0; i < len; i++, j += cn )
74 T *dst0 = dst[0], *dst1 = dst[1], *dst2 = dst[2];
75 for( i = j = 0; i < len; i++, j += cn )
84 T *dst0 = dst[0], *dst1 = dst[1], *dst2 = dst[2], *dst3 = dst[3];
85 for( i = j = 0; i < len; i++, j += cn )
87 dst0[i] = src[j]; dst1[i] = src[j+1];
88 dst2[i] = src[j+2]; dst3[i] = src[j+3];
92 for( ; k < cn; k += 4 )
94 T *dst0 = dst[k], *dst1 = dst[k+1], *dst2 = dst[k+2], *dst3 = dst[k+3];
95 for( i = 0, j = k; i < len; i++, j += cn )
97 dst0[i] = src[j]; dst1[i] = src[j+1];
98 dst2[i] = src[j+2]; dst3[i] = src[j+3];
103 template<typename T> static void
104 merge_( const T** src, T* dst, int len, int cn )
106 int k = cn % 4 ? cn % 4 : 4;
110 const T* src0 = src[0];
111 for( i = j = 0; i < len; i++, j += cn )
116 const T *src0 = src[0], *src1 = src[1];
117 for( i = j = 0; i < len; i++, j += cn )
125 const T *src0 = src[0], *src1 = src[1], *src2 = src[2];
126 for( i = j = 0; i < len; i++, j += cn )
135 const T *src0 = src[0], *src1 = src[1], *src2 = src[2], *src3 = src[3];
136 for( i = j = 0; i < len; i++, j += cn )
138 dst[j] = src0[i]; dst[j+1] = src1[i];
139 dst[j+2] = src2[i]; dst[j+3] = src3[i];
143 for( ; k < cn; k += 4 )
145 const T *src0 = src[k], *src1 = src[k+1], *src2 = src[k+2], *src3 = src[k+3];
146 for( i = 0, j = k; i < len; i++, j += cn )
148 dst[j] = src0[i]; dst[j+1] = src1[i];
149 dst[j+2] = src2[i]; dst[j+3] = src3[i];
154 static void split8u(const uchar* src, uchar** dst, int len, int cn )
156 split_(src, dst, len, cn);
159 static void split16u(const ushort* src, ushort** dst, int len, int cn )
161 split_(src, dst, len, cn);
164 static void split32s(const int* src, int** dst, int len, int cn )
166 split_(src, dst, len, cn);
169 static void split64s(const int64* src, int64** dst, int len, int cn )
171 split_(src, dst, len, cn);
174 static void merge8u(const uchar** src, uchar* dst, int len, int cn )
176 merge_(src, dst, len, cn);
179 static void merge16u(const ushort** src, ushort* dst, int len, int cn )
181 merge_(src, dst, len, cn);
184 static void merge32s(const int** src, int* dst, int len, int cn )
186 merge_(src, dst, len, cn);
189 static void merge64s(const int64** src, int64* dst, int len, int cn )
191 merge_(src, dst, len, cn);
194 typedef void (*SplitFunc)(const uchar* src, uchar** dst, int len, int cn);
195 typedef void (*MergeFunc)(const uchar** src, uchar* dst, int len, int cn);
197 static SplitFunc getSplitFunc(int depth)
199 static SplitFunc splitTab[] =
201 (SplitFunc)GET_OPTIMIZED(split8u), (SplitFunc)GET_OPTIMIZED(split8u), (SplitFunc)GET_OPTIMIZED(split16u), (SplitFunc)GET_OPTIMIZED(split16u),
202 (SplitFunc)GET_OPTIMIZED(split32s), (SplitFunc)GET_OPTIMIZED(split32s), (SplitFunc)GET_OPTIMIZED(split64s), 0
205 return splitTab[depth];
208 static MergeFunc getMergeFunc(int depth)
210 static MergeFunc mergeTab[] =
212 (MergeFunc)GET_OPTIMIZED(merge8u), (MergeFunc)GET_OPTIMIZED(merge8u), (MergeFunc)GET_OPTIMIZED(merge16u), (MergeFunc)GET_OPTIMIZED(merge16u),
213 (MergeFunc)GET_OPTIMIZED(merge32s), (MergeFunc)GET_OPTIMIZED(merge32s), (MergeFunc)GET_OPTIMIZED(merge64s), 0
216 return mergeTab[depth];
221 void cv::split(const Mat& src, Mat* mv)
223 int k, depth = src.depth(), cn = src.channels();
230 SplitFunc func = getSplitFunc(depth);
231 CV_Assert( func != 0 );
233 int esz = (int)src.elemSize(), esz1 = (int)src.elemSize1();
234 int blocksize0 = (BLOCK_SIZE + esz-1)/esz;
235 AutoBuffer<uchar> _buf((cn+1)*(sizeof(Mat*) + sizeof(uchar*)) + 16);
236 const Mat** arrays = (const Mat**)(uchar*)_buf;
237 uchar** ptrs = (uchar**)alignPtr(arrays + cn + 1, 16);
240 for( k = 0; k < cn; k++ )
242 mv[k].create(src.dims, src.size, depth);
243 arrays[k+1] = &mv[k];
246 NAryMatIterator it(arrays, ptrs, cn+1);
247 int total = (int)it.size, blocksize = cn <= 4 ? total : std::min(total, blocksize0);
249 for( size_t i = 0; i < it.nplanes; i++, ++it )
251 for( int j = 0; j < total; j += blocksize )
253 int bsz = std::min(total - j, blocksize);
254 func( ptrs[0], &ptrs[1], bsz, cn );
256 if( j + blocksize < total )
259 for( k = 0; k < cn; k++ )
260 ptrs[k+1] += bsz*esz1;
266 void cv::split(InputArray _m, OutputArrayOfArrays _mv)
274 CV_Assert( !_mv.fixedType() || CV_MAT_TYPE(_mv.flags) == m.depth() );
275 _mv.create(m.channels(), 1, m.depth());
276 Mat* dst = &_mv.getMatRef(0);
280 void cv::split(const Mat& src, vector<Mat>& mv)
282 split(_InputArray(src), _OutputArray(mv));
285 void cv::merge(const Mat* mv, size_t n, OutputArray _dst)
287 CV_Assert( mv && n > 0 );
289 int depth = mv[0].depth();
294 for( i = 0; i < n; i++ )
296 CV_Assert(mv[i].size == mv[0].size && mv[i].depth() == depth);
297 allch1 = allch1 && mv[i].channels() == 1;
298 cn += mv[i].channels();
301 CV_Assert( 0 < cn && cn <= CV_CN_MAX );
302 _dst.create(mv[0].dims, mv[0].size, CV_MAKETYPE(depth, cn));
303 Mat dst = _dst.getMat();
313 AutoBuffer<int> pairs(cn*2);
316 for( i = 0, j = 0; i < n; i++, j += ni )
318 ni = mv[i].channels();
319 for( k = 0; k < ni; k++ )
321 pairs[(j+k)*2] = j + k;
322 pairs[(j+k)*2+1] = j + k;
325 mixChannels( mv, n, &dst, 1, &pairs[0], cn );
329 size_t esz = dst.elemSize(), esz1 = dst.elemSize1();
330 int blocksize0 = (int)((BLOCK_SIZE + esz-1)/esz);
331 AutoBuffer<uchar> _buf((cn+1)*(sizeof(Mat*) + sizeof(uchar*)) + 16);
332 const Mat** arrays = (const Mat**)(uchar*)_buf;
333 uchar** ptrs = (uchar**)alignPtr(arrays + cn + 1, 16);
336 for( k = 0; k < cn; k++ )
337 arrays[k+1] = &mv[k];
339 NAryMatIterator it(arrays, ptrs, cn+1);
340 int total = (int)it.size, blocksize = cn <= 4 ? total : std::min(total, blocksize0);
341 MergeFunc func = getMergeFunc(depth);
343 for( i = 0; i < it.nplanes; i++, ++it )
345 for( int j = 0; j < total; j += blocksize )
347 int bsz = std::min(total - j, blocksize);
348 func( (const uchar**)&ptrs[1], ptrs[0], bsz, cn );
350 if( j + blocksize < total )
353 for( int t = 0; t < cn; t++ )
354 ptrs[t+1] += bsz*esz1;
360 void cv::merge(InputArrayOfArrays _mv, OutputArray _dst)
363 _mv.getMatVector(mv);
364 merge(!mv.empty() ? &mv[0] : 0, mv.size(), _dst);
367 void cv::merge(const vector<Mat>& _mv, OutputArray _dst)
369 merge(_InputArray(_mv), _dst);
372 /****************************************************************************************\
373 * Generalized split/merge: mixing channels *
374 \****************************************************************************************/
379 template<typename T> static void
380 mixChannels_( const T** src, const int* sdelta,
381 T** dst, const int* ddelta,
382 int len, int npairs )
385 for( k = 0; k < npairs; k++ )
389 int ds = sdelta[k], dd = ddelta[k];
392 for( i = 0; i <= len - 2; i += 2, s += ds*2, d += dd*2 )
394 T t0 = s[0], t1 = s[ds];
395 d[0] = t0; d[dd] = t1;
402 for( i = 0; i <= len - 2; i += 2, d += dd*2 )
411 static void mixChannels8u( const uchar** src, const int* sdelta,
412 uchar** dst, const int* ddelta,
413 int len, int npairs )
415 mixChannels_(src, sdelta, dst, ddelta, len, npairs);
418 static void mixChannels16u( const ushort** src, const int* sdelta,
419 ushort** dst, const int* ddelta,
420 int len, int npairs )
422 mixChannels_(src, sdelta, dst, ddelta, len, npairs);
425 static void mixChannels32s( const int** src, const int* sdelta,
426 int** dst, const int* ddelta,
427 int len, int npairs )
429 mixChannels_(src, sdelta, dst, ddelta, len, npairs);
432 static void mixChannels64s( const int64** src, const int* sdelta,
433 int64** dst, const int* ddelta,
434 int len, int npairs )
436 mixChannels_(src, sdelta, dst, ddelta, len, npairs);
439 typedef void (*MixChannelsFunc)( const uchar** src, const int* sdelta,
440 uchar** dst, const int* ddelta, int len, int npairs );
442 static MixChannelsFunc getMixchFunc(int depth)
444 static MixChannelsFunc mixchTab[] =
446 (MixChannelsFunc)mixChannels8u, (MixChannelsFunc)mixChannels8u, (MixChannelsFunc)mixChannels16u,
447 (MixChannelsFunc)mixChannels16u, (MixChannelsFunc)mixChannels32s, (MixChannelsFunc)mixChannels32s,
448 (MixChannelsFunc)mixChannels64s, 0
451 return mixchTab[depth];
456 void cv::mixChannels( const Mat* src, size_t nsrcs, Mat* dst, size_t ndsts, const int* fromTo, size_t npairs )
460 CV_Assert( src && nsrcs > 0 && dst && ndsts > 0 && fromTo && npairs > 0 );
462 size_t i, j, k, esz1 = dst[0].elemSize1();
463 int depth = dst[0].depth();
465 AutoBuffer<uchar> buf((nsrcs + ndsts + 1)*(sizeof(Mat*) + sizeof(uchar*)) + npairs*(sizeof(uchar*)*2 + sizeof(int)*6));
466 const Mat** arrays = (const Mat**)(uchar*)buf;
467 uchar** ptrs = (uchar**)(arrays + nsrcs + ndsts);
468 const uchar** srcs = (const uchar**)(ptrs + nsrcs + ndsts + 1);
469 uchar** dsts = (uchar**)(srcs + npairs);
470 int* tab = (int*)(dsts + npairs);
471 int *sdelta = (int*)(tab + npairs*4), *ddelta = sdelta + npairs;
473 for( i = 0; i < nsrcs; i++ )
475 for( i = 0; i < ndsts; i++ )
476 arrays[i + nsrcs] = &dst[i];
477 ptrs[nsrcs + ndsts] = 0;
479 for( i = 0; i < npairs; i++ )
481 int i0 = fromTo[i*2], i1 = fromTo[i*2+1];
484 for( j = 0; j < nsrcs; i0 -= src[j].channels(), j++ )
485 if( i0 < src[j].channels() )
487 CV_Assert(j < nsrcs && src[j].depth() == depth);
488 tab[i*4] = (int)j; tab[i*4+1] = (int)(i0*esz1);
489 sdelta[i] = src[j].channels();
493 tab[i*4] = (int)(nsrcs + ndsts); tab[i*4+1] = 0;
497 for( j = 0; j < ndsts; i1 -= dst[j].channels(), j++ )
498 if( i1 < dst[j].channels() )
500 CV_Assert(i1 >= 0 && j < ndsts && dst[j].depth() == depth);
501 tab[i*4+2] = (int)(j + nsrcs); tab[i*4+3] = (int)(i1*esz1);
502 ddelta[i] = dst[j].channels();
505 NAryMatIterator it(arrays, ptrs, (int)(nsrcs + ndsts));
506 int total = (int)it.size, blocksize = std::min(total, (int)((BLOCK_SIZE + esz1-1)/esz1));
507 MixChannelsFunc func = getMixchFunc(depth);
509 for( i = 0; i < it.nplanes; i++, ++it )
511 for( k = 0; k < npairs; k++ )
513 srcs[k] = ptrs[tab[k*4]] + tab[k*4+1];
514 dsts[k] = ptrs[tab[k*4+2]] + tab[k*4+3];
517 for( int t = 0; t < total; t += blocksize )
519 int bsz = std::min(total - t, blocksize);
520 func( srcs, sdelta, dsts, ddelta, bsz, (int)npairs );
522 if( t + blocksize < total )
523 for( k = 0; k < npairs; k++ )
525 srcs[k] += blocksize*sdelta[k]*esz1;
526 dsts[k] += blocksize*ddelta[k]*esz1;
533 void cv::mixChannels(const vector<Mat>& src, vector<Mat>& dst,
534 const int* fromTo, size_t npairs)
536 mixChannels(!src.empty() ? &src[0] : 0, src.size(),
537 !dst.empty() ? &dst[0] : 0, dst.size(), fromTo, npairs);
540 void cv::mixChannels(InputArrayOfArrays src, InputArrayOfArrays dst,
541 const vector<int>& fromTo)
545 bool src_is_mat = src.kind() != _InputArray::STD_VECTOR_MAT &&
546 src.kind() != _InputArray::STD_VECTOR_VECTOR;
547 bool dst_is_mat = dst.kind() != _InputArray::STD_VECTOR_MAT &&
548 dst.kind() != _InputArray::STD_VECTOR_VECTOR;
550 int nsrc = src_is_mat ? 1 : (int)src.total();
551 int ndst = dst_is_mat ? 1 : (int)dst.total();
553 CV_Assert(fromTo.size()%2 == 0 && nsrc > 0 && ndst > 0);
554 cv::AutoBuffer<Mat> _buf(nsrc + ndst);
556 for( i = 0; i < nsrc; i++ )
557 buf[i] = src.getMat(src_is_mat ? -1 : i);
558 for( i = 0; i < ndst; i++ )
559 buf[nsrc + i] = dst.getMat(dst_is_mat ? -1 : i);
560 mixChannels(&buf[0], nsrc, &buf[nsrc], ndst, &fromTo[0], fromTo.size()/2);
563 void cv::extractChannel(InputArray _src, OutputArray _dst, int coi)
565 Mat src = _src.getMat();
566 CV_Assert( 0 <= coi && coi < src.channels() );
567 _dst.create(src.dims, &src.size[0], src.depth());
568 Mat dst = _dst.getMat();
569 int ch[] = { coi, 0 };
570 mixChannels(&src, 1, &dst, 1, ch, 1);
573 void cv::insertChannel(InputArray _src, InputOutputArray _dst, int coi)
575 Mat src = _src.getMat(), dst = _dst.getMat();
576 CV_Assert( src.size == dst.size && src.depth() == dst.depth() );
577 CV_Assert( 0 <= coi && coi < dst.channels() && src.channels() == 1 );
578 int ch[] = { 0, coi };
579 mixChannels(&src, 1, &dst, 1, ch, 1);
582 /****************************************************************************************\
583 * convertScale[Abs] *
584 \****************************************************************************************/
589 template<typename T, typename DT, typename WT> static void
590 cvtScaleAbs_( const T* src, size_t sstep,
591 DT* dst, size_t dstep, Size size,
594 sstep /= sizeof(src[0]);
595 dstep /= sizeof(dst[0]);
597 for( ; size.height--; src += sstep, dst += dstep )
600 #if CV_ENABLE_UNROLLED
601 for( ; x <= size.width - 4; x += 4 )
604 t0 = saturate_cast<DT>(std::abs(src[x]*scale + shift));
605 t1 = saturate_cast<DT>(std::abs(src[x+1]*scale + shift));
606 dst[x] = t0; dst[x+1] = t1;
607 t0 = saturate_cast<DT>(std::abs(src[x+2]*scale + shift));
608 t1 = saturate_cast<DT>(std::abs(src[x+3]*scale + shift));
609 dst[x+2] = t0; dst[x+3] = t1;
612 for( ; x < size.width; x++ )
613 dst[x] = saturate_cast<DT>(std::abs(src[x]*scale + shift));
618 template<typename T, typename DT, typename WT> static void
619 cvtScale_( const T* src, size_t sstep,
620 DT* dst, size_t dstep, Size size,
623 sstep /= sizeof(src[0]);
624 dstep /= sizeof(dst[0]);
626 for( ; size.height--; src += sstep, dst += dstep )
629 #if CV_ENABLE_UNROLLED
630 for( ; x <= size.width - 4; x += 4 )
633 t0 = saturate_cast<DT>(src[x]*scale + shift);
634 t1 = saturate_cast<DT>(src[x+1]*scale + shift);
635 dst[x] = t0; dst[x+1] = t1;
636 t0 = saturate_cast<DT>(src[x+2]*scale + shift);
637 t1 = saturate_cast<DT>(src[x+3]*scale + shift);
638 dst[x+2] = t0; dst[x+3] = t1;
642 for( ; x < size.width; x++ )
643 dst[x] = saturate_cast<DT>(src[x]*scale + shift);
647 //vz optimized template specialization
649 cvtScale_<short, short, float>( const short* src, size_t sstep,
650 short* dst, size_t dstep, Size size,
651 float scale, float shift )
653 sstep /= sizeof(src[0]);
654 dstep /= sizeof(dst[0]);
656 for( ; size.height--; src += sstep, dst += dstep )
662 __m128 scale128 = _mm_set1_ps (scale);
663 __m128 shift128 = _mm_set1_ps (shift);
664 for(; x <= size.width - 8; x += 8 )
666 __m128i r0 = _mm_loadl_epi64((const __m128i*)(src + x));
667 __m128i r1 = _mm_loadl_epi64((const __m128i*)(src + x + 4));
668 __m128 rf0 =_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(r0, r0), 16));
669 __m128 rf1 =_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(r1, r1), 16));
670 rf0 = _mm_add_ps(_mm_mul_ps(rf0, scale128), shift128);
671 rf1 = _mm_add_ps(_mm_mul_ps(rf1, scale128), shift128);
672 r0 = _mm_cvtps_epi32(rf0);
673 r1 = _mm_cvtps_epi32(rf1);
674 r0 = _mm_packs_epi32(r0, r1);
675 _mm_storeu_si128((__m128i*)(dst + x), r0);
680 for(; x < size.width; x++ )
681 dst[x] = saturate_cast<short>(src[x]*scale + shift);
686 cvtScale_<short, int, float>( const short* src, size_t sstep,
687 int* dst, size_t dstep, Size size,
688 float scale, float shift )
690 sstep /= sizeof(src[0]);
691 dstep /= sizeof(dst[0]);
693 for( ; size.height--; src += sstep, dst += dstep )
700 __m128 scale128 = _mm_set1_ps (scale);
701 __m128 shift128 = _mm_set1_ps (shift);
702 for(; x <= size.width - 8; x += 8 )
704 __m128i r0 = _mm_loadl_epi64((const __m128i*)(src + x));
705 __m128i r1 = _mm_loadl_epi64((const __m128i*)(src + x + 4));
706 __m128 rf0 =_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(r0, r0), 16));
707 __m128 rf1 =_mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(r1, r1), 16));
708 rf0 = _mm_add_ps(_mm_mul_ps(rf0, scale128), shift128);
709 rf1 = _mm_add_ps(_mm_mul_ps(rf1, scale128), shift128);
710 r0 = _mm_cvtps_epi32(rf0);
711 r1 = _mm_cvtps_epi32(rf1);
713 _mm_storeu_si128((__m128i*)(dst + x), r0);
714 _mm_storeu_si128((__m128i*)(dst + x + 4), r1);
719 //We will wait Haswell
722 if(USE_AVX)//2X - bad variant
724 ////TODO:AVX implementation (optimization?) required
725 __m256 scale256 = _mm256_set1_ps (scale);
726 __m256 shift256 = _mm256_set1_ps (shift);
727 for(; x <= size.width - 8; x += 8 )
729 __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)));
730 __m256 r0 = _mm256_add_ps( _mm256_mul_ps(_mm256_cvtepi32_ps (buf), scale256), shift256);
731 __m256i res = _mm256_cvtps_epi32(r0);
732 _mm256_storeu_si256 ((__m256i*)(dst+x), res);
737 for(; x < size.width; x++ )
738 dst[x] = saturate_cast<int>(src[x]*scale + shift);
742 template<typename T, typename DT> static void
743 cvt_( const T* src, size_t sstep,
744 DT* dst, size_t dstep, Size size )
746 sstep /= sizeof(src[0]);
747 dstep /= sizeof(dst[0]);
749 for( ; size.height--; src += sstep, dst += dstep )
752 #if CV_ENABLE_UNROLLED
753 for( ; x <= size.width - 4; x += 4 )
756 t0 = saturate_cast<DT>(src[x]);
757 t1 = saturate_cast<DT>(src[x+1]);
758 dst[x] = t0; dst[x+1] = t1;
759 t0 = saturate_cast<DT>(src[x+2]);
760 t1 = saturate_cast<DT>(src[x+3]);
761 dst[x+2] = t0; dst[x+3] = t1;
764 for( ; x < size.width; x++ )
765 dst[x] = saturate_cast<DT>(src[x]);
769 //vz optimized template specialization, test Core_ConvertScale/ElemWiseTest
771 cvt_<float, short>( const float* src, size_t sstep,
772 short* dst, size_t dstep, Size size )
774 sstep /= sizeof(src[0]);
775 dstep /= sizeof(dst[0]);
777 for( ; size.height--; src += sstep, dst += dstep )
782 for( ; x <= size.width - 8; x += 8 )
784 __m128 src128 = _mm_loadu_ps (src + x);
785 __m128i src_int128 = _mm_cvtps_epi32 (src128);
787 src128 = _mm_loadu_ps (src + x + 4);
788 __m128i src1_int128 = _mm_cvtps_epi32 (src128);
790 src1_int128 = _mm_packs_epi32(src_int128, src1_int128);
791 _mm_storeu_si128((__m128i*)(dst + x),src1_int128);
795 for( ; x < size.width; x++ )
796 dst[x] = saturate_cast<short>(src[x]);
802 template<typename T> static void
803 cpy_( const T* src, size_t sstep, T* dst, size_t dstep, Size size )
805 sstep /= sizeof(src[0]);
806 dstep /= sizeof(dst[0]);
808 for( ; size.height--; src += sstep, dst += dstep )
809 memcpy(dst, src, size.width*sizeof(src[0]));
812 #define DEF_CVT_SCALE_ABS_FUNC(suffix, tfunc, stype, dtype, wtype) \
813 static void cvtScaleAbs##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
814 dtype* dst, size_t dstep, Size size, double* scale) \
816 tfunc(src, sstep, dst, dstep, size, (wtype)scale[0], (wtype)scale[1]); \
819 #define DEF_CVT_SCALE_FUNC(suffix, stype, dtype, wtype) \
820 static void cvtScale##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
821 dtype* dst, size_t dstep, Size size, double* scale) \
823 cvtScale_(src, sstep, dst, dstep, size, (wtype)scale[0], (wtype)scale[1]); \
827 #define DEF_CVT_FUNC(suffix, stype, dtype) \
828 static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
829 dtype* dst, size_t dstep, Size size, double*) \
831 cvt_(src, sstep, dst, dstep, size); \
834 #define DEF_CPY_FUNC(suffix, stype) \
835 static void cvt##suffix( const stype* src, size_t sstep, const uchar*, size_t, \
836 stype* dst, size_t dstep, Size size, double*) \
838 cpy_(src, sstep, dst, dstep, size); \
842 DEF_CVT_SCALE_ABS_FUNC(8u, cvtScaleAbs_, uchar, uchar, float)
843 DEF_CVT_SCALE_ABS_FUNC(8s8u, cvtScaleAbs_, schar, uchar, float)
844 DEF_CVT_SCALE_ABS_FUNC(16u8u, cvtScaleAbs_, ushort, uchar, float)
845 DEF_CVT_SCALE_ABS_FUNC(16s8u, cvtScaleAbs_, short, uchar, float)
846 DEF_CVT_SCALE_ABS_FUNC(32s8u, cvtScaleAbs_, int, uchar, float)
847 DEF_CVT_SCALE_ABS_FUNC(32f8u, cvtScaleAbs_, float, uchar, float)
848 DEF_CVT_SCALE_ABS_FUNC(64f8u, cvtScaleAbs_, double, uchar, float)
850 DEF_CVT_SCALE_FUNC(8u, uchar, uchar, float)
851 DEF_CVT_SCALE_FUNC(8s8u, schar, uchar, float)
852 DEF_CVT_SCALE_FUNC(16u8u, ushort, uchar, float)
853 DEF_CVT_SCALE_FUNC(16s8u, short, uchar, float)
854 DEF_CVT_SCALE_FUNC(32s8u, int, uchar, float)
855 DEF_CVT_SCALE_FUNC(32f8u, float, uchar, float)
856 DEF_CVT_SCALE_FUNC(64f8u, double, uchar, float)
858 DEF_CVT_SCALE_FUNC(8u8s, uchar, schar, float)
859 DEF_CVT_SCALE_FUNC(8s, schar, schar, float)
860 DEF_CVT_SCALE_FUNC(16u8s, ushort, schar, float)
861 DEF_CVT_SCALE_FUNC(16s8s, short, schar, float)
862 DEF_CVT_SCALE_FUNC(32s8s, int, schar, float)
863 DEF_CVT_SCALE_FUNC(32f8s, float, schar, float)
864 DEF_CVT_SCALE_FUNC(64f8s, double, schar, float)
866 DEF_CVT_SCALE_FUNC(8u16u, uchar, ushort, float)
867 DEF_CVT_SCALE_FUNC(8s16u, schar, ushort, float)
868 DEF_CVT_SCALE_FUNC(16u, ushort, ushort, float)
869 DEF_CVT_SCALE_FUNC(16s16u, short, ushort, float)
870 DEF_CVT_SCALE_FUNC(32s16u, int, ushort, float)
871 DEF_CVT_SCALE_FUNC(32f16u, float, ushort, float)
872 DEF_CVT_SCALE_FUNC(64f16u, double, ushort, float)
874 DEF_CVT_SCALE_FUNC(8u16s, uchar, short, float)
875 DEF_CVT_SCALE_FUNC(8s16s, schar, short, float)
876 DEF_CVT_SCALE_FUNC(16u16s, ushort, short, float)
877 DEF_CVT_SCALE_FUNC(16s, short, short, float)
878 DEF_CVT_SCALE_FUNC(32s16s, int, short, float)
879 DEF_CVT_SCALE_FUNC(32f16s, float, short, float)
880 DEF_CVT_SCALE_FUNC(64f16s, double, short, float)
882 DEF_CVT_SCALE_FUNC(8u32s, uchar, int, float)
883 DEF_CVT_SCALE_FUNC(8s32s, schar, int, float)
884 DEF_CVT_SCALE_FUNC(16u32s, ushort, int, float)
885 DEF_CVT_SCALE_FUNC(16s32s, short, int, float)
886 DEF_CVT_SCALE_FUNC(32s, int, int, double)
887 DEF_CVT_SCALE_FUNC(32f32s, float, int, float)
888 DEF_CVT_SCALE_FUNC(64f32s, double, int, double)
890 DEF_CVT_SCALE_FUNC(8u32f, uchar, float, float)
891 DEF_CVT_SCALE_FUNC(8s32f, schar, float, float)
892 DEF_CVT_SCALE_FUNC(16u32f, ushort, float, float)
893 DEF_CVT_SCALE_FUNC(16s32f, short, float, float)
894 DEF_CVT_SCALE_FUNC(32s32f, int, float, double)
895 DEF_CVT_SCALE_FUNC(32f, float, float, float)
896 DEF_CVT_SCALE_FUNC(64f32f, double, float, double)
898 DEF_CVT_SCALE_FUNC(8u64f, uchar, double, double)
899 DEF_CVT_SCALE_FUNC(8s64f, schar, double, double)
900 DEF_CVT_SCALE_FUNC(16u64f, ushort, double, double)
901 DEF_CVT_SCALE_FUNC(16s64f, short, double, double)
902 DEF_CVT_SCALE_FUNC(32s64f, int, double, double)
903 DEF_CVT_SCALE_FUNC(32f64f, float, double, double)
904 DEF_CVT_SCALE_FUNC(64f, double, double, double)
906 DEF_CPY_FUNC(8u, uchar)
907 DEF_CVT_FUNC(8s8u, schar, uchar)
908 DEF_CVT_FUNC(16u8u, ushort, uchar)
909 DEF_CVT_FUNC(16s8u, short, uchar)
910 DEF_CVT_FUNC(32s8u, int, uchar)
911 DEF_CVT_FUNC(32f8u, float, uchar)
912 DEF_CVT_FUNC(64f8u, double, uchar)
914 DEF_CVT_FUNC(8u8s, uchar, schar)
915 DEF_CVT_FUNC(16u8s, ushort, schar)
916 DEF_CVT_FUNC(16s8s, short, schar)
917 DEF_CVT_FUNC(32s8s, int, schar)
918 DEF_CVT_FUNC(32f8s, float, schar)
919 DEF_CVT_FUNC(64f8s, double, schar)
921 DEF_CVT_FUNC(8u16u, uchar, ushort)
922 DEF_CVT_FUNC(8s16u, schar, ushort)
923 DEF_CPY_FUNC(16u, ushort)
924 DEF_CVT_FUNC(16s16u, short, ushort)
925 DEF_CVT_FUNC(32s16u, int, ushort)
926 DEF_CVT_FUNC(32f16u, float, ushort)
927 DEF_CVT_FUNC(64f16u, double, ushort)
929 DEF_CVT_FUNC(8u16s, uchar, short)
930 DEF_CVT_FUNC(8s16s, schar, short)
931 DEF_CVT_FUNC(16u16s, ushort, short)
932 DEF_CVT_FUNC(32s16s, int, short)
933 DEF_CVT_FUNC(32f16s, float, short)
934 DEF_CVT_FUNC(64f16s, double, short)
936 DEF_CVT_FUNC(8u32s, uchar, int)
937 DEF_CVT_FUNC(8s32s, schar, int)
938 DEF_CVT_FUNC(16u32s, ushort, int)
939 DEF_CVT_FUNC(16s32s, short, int)
940 DEF_CPY_FUNC(32s, int)
941 DEF_CVT_FUNC(32f32s, float, int)
942 DEF_CVT_FUNC(64f32s, double, int)
944 DEF_CVT_FUNC(8u32f, uchar, float)
945 DEF_CVT_FUNC(8s32f, schar, float)
946 DEF_CVT_FUNC(16u32f, ushort, float)
947 DEF_CVT_FUNC(16s32f, short, float)
948 DEF_CVT_FUNC(32s32f, int, float)
949 DEF_CVT_FUNC(64f32f, double, float)
951 DEF_CVT_FUNC(8u64f, uchar, double)
952 DEF_CVT_FUNC(8s64f, schar, double)
953 DEF_CVT_FUNC(16u64f, ushort, double)
954 DEF_CVT_FUNC(16s64f, short, double)
955 DEF_CVT_FUNC(32s64f, int, double)
956 DEF_CVT_FUNC(32f64f, float, double)
957 DEF_CPY_FUNC(64s, int64)
959 static BinaryFunc getCvtScaleAbsFunc(int depth)
961 static BinaryFunc cvtScaleAbsTab[] =
963 (BinaryFunc)cvtScaleAbs8u, (BinaryFunc)cvtScaleAbs8s8u, (BinaryFunc)cvtScaleAbs16u8u,
964 (BinaryFunc)cvtScaleAbs16s8u, (BinaryFunc)cvtScaleAbs32s8u, (BinaryFunc)cvtScaleAbs32f8u,
965 (BinaryFunc)cvtScaleAbs64f8u, 0
968 return cvtScaleAbsTab[depth];
971 BinaryFunc getConvertFunc(int sdepth, int ddepth)
973 static BinaryFunc cvtTab[][8] =
976 (BinaryFunc)(cvt8u), (BinaryFunc)GET_OPTIMIZED(cvt8s8u), (BinaryFunc)GET_OPTIMIZED(cvt16u8u),
977 (BinaryFunc)GET_OPTIMIZED(cvt16s8u), (BinaryFunc)GET_OPTIMIZED(cvt32s8u), (BinaryFunc)GET_OPTIMIZED(cvt32f8u),
978 (BinaryFunc)GET_OPTIMIZED(cvt64f8u), 0
981 (BinaryFunc)GET_OPTIMIZED(cvt8u8s), (BinaryFunc)cvt8u, (BinaryFunc)GET_OPTIMIZED(cvt16u8s),
982 (BinaryFunc)GET_OPTIMIZED(cvt16s8s), (BinaryFunc)GET_OPTIMIZED(cvt32s8s), (BinaryFunc)GET_OPTIMIZED(cvt32f8s),
983 (BinaryFunc)GET_OPTIMIZED(cvt64f8s), 0
986 (BinaryFunc)GET_OPTIMIZED(cvt8u16u), (BinaryFunc)GET_OPTIMIZED(cvt8s16u), (BinaryFunc)cvt16u,
987 (BinaryFunc)GET_OPTIMIZED(cvt16s16u), (BinaryFunc)GET_OPTIMIZED(cvt32s16u), (BinaryFunc)GET_OPTIMIZED(cvt32f16u),
988 (BinaryFunc)GET_OPTIMIZED(cvt64f16u), 0
991 (BinaryFunc)GET_OPTIMIZED(cvt8u16s), (BinaryFunc)GET_OPTIMIZED(cvt8s16s), (BinaryFunc)GET_OPTIMIZED(cvt16u16s),
992 (BinaryFunc)cvt16u, (BinaryFunc)GET_OPTIMIZED(cvt32s16s), (BinaryFunc)GET_OPTIMIZED(cvt32f16s),
993 (BinaryFunc)GET_OPTIMIZED(cvt64f16s), 0
996 (BinaryFunc)GET_OPTIMIZED(cvt8u32s), (BinaryFunc)GET_OPTIMIZED(cvt8s32s), (BinaryFunc)GET_OPTIMIZED(cvt16u32s),
997 (BinaryFunc)GET_OPTIMIZED(cvt16s32s), (BinaryFunc)cvt32s, (BinaryFunc)GET_OPTIMIZED(cvt32f32s),
998 (BinaryFunc)GET_OPTIMIZED(cvt64f32s), 0
1001 (BinaryFunc)GET_OPTIMIZED(cvt8u32f), (BinaryFunc)GET_OPTIMIZED(cvt8s32f), (BinaryFunc)GET_OPTIMIZED(cvt16u32f),
1002 (BinaryFunc)GET_OPTIMIZED(cvt16s32f), (BinaryFunc)GET_OPTIMIZED(cvt32s32f), (BinaryFunc)cvt32s,
1003 (BinaryFunc)GET_OPTIMIZED(cvt64f32f), 0
1006 (BinaryFunc)GET_OPTIMIZED(cvt8u64f), (BinaryFunc)GET_OPTIMIZED(cvt8s64f), (BinaryFunc)GET_OPTIMIZED(cvt16u64f),
1007 (BinaryFunc)GET_OPTIMIZED(cvt16s64f), (BinaryFunc)GET_OPTIMIZED(cvt32s64f), (BinaryFunc)GET_OPTIMIZED(cvt32f64f),
1008 (BinaryFunc)(cvt64s), 0
1011 0, 0, 0, 0, 0, 0, 0, 0
1015 return cvtTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)];
1018 BinaryFunc getConvertScaleFunc(int sdepth, int ddepth)
1020 static BinaryFunc cvtScaleTab[][8] =
1023 (BinaryFunc)GET_OPTIMIZED(cvtScale8u), (BinaryFunc)GET_OPTIMIZED(cvtScale8s8u), (BinaryFunc)GET_OPTIMIZED(cvtScale16u8u),
1024 (BinaryFunc)GET_OPTIMIZED(cvtScale16s8u), (BinaryFunc)GET_OPTIMIZED(cvtScale32s8u), (BinaryFunc)GET_OPTIMIZED(cvtScale32f8u),
1025 (BinaryFunc)cvtScale64f8u, 0
1028 (BinaryFunc)GET_OPTIMIZED(cvtScale8u8s), (BinaryFunc)GET_OPTIMIZED(cvtScale8s), (BinaryFunc)GET_OPTIMIZED(cvtScale16u8s),
1029 (BinaryFunc)GET_OPTIMIZED(cvtScale16s8s), (BinaryFunc)GET_OPTIMIZED(cvtScale32s8s), (BinaryFunc)GET_OPTIMIZED(cvtScale32f8s),
1030 (BinaryFunc)cvtScale64f8s, 0
1033 (BinaryFunc)GET_OPTIMIZED(cvtScale8u16u), (BinaryFunc)GET_OPTIMIZED(cvtScale8s16u), (BinaryFunc)GET_OPTIMIZED(cvtScale16u),
1034 (BinaryFunc)GET_OPTIMIZED(cvtScale16s16u), (BinaryFunc)GET_OPTIMIZED(cvtScale32s16u), (BinaryFunc)GET_OPTIMIZED(cvtScale32f16u),
1035 (BinaryFunc)cvtScale64f16u, 0
1038 (BinaryFunc)GET_OPTIMIZED(cvtScale8u16s), (BinaryFunc)GET_OPTIMIZED(cvtScale8s16s), (BinaryFunc)GET_OPTIMIZED(cvtScale16u16s),
1039 (BinaryFunc)GET_OPTIMIZED(cvtScale16s), (BinaryFunc)GET_OPTIMIZED(cvtScale32s16s), (BinaryFunc)GET_OPTIMIZED(cvtScale32f16s),
1040 (BinaryFunc)cvtScale64f16s, 0
1043 (BinaryFunc)GET_OPTIMIZED(cvtScale8u32s), (BinaryFunc)GET_OPTIMIZED(cvtScale8s32s), (BinaryFunc)GET_OPTIMIZED(cvtScale16u32s),
1044 (BinaryFunc)GET_OPTIMIZED(cvtScale16s32s), (BinaryFunc)GET_OPTIMIZED(cvtScale32s), (BinaryFunc)GET_OPTIMIZED(cvtScale32f32s),
1045 (BinaryFunc)cvtScale64f32s, 0
1048 (BinaryFunc)GET_OPTIMIZED(cvtScale8u32f), (BinaryFunc)GET_OPTIMIZED(cvtScale8s32f), (BinaryFunc)GET_OPTIMIZED(cvtScale16u32f),
1049 (BinaryFunc)GET_OPTIMIZED(cvtScale16s32f), (BinaryFunc)GET_OPTIMIZED(cvtScale32s32f), (BinaryFunc)GET_OPTIMIZED(cvtScale32f),
1050 (BinaryFunc)cvtScale64f32f, 0
1053 (BinaryFunc)cvtScale8u64f, (BinaryFunc)cvtScale8s64f, (BinaryFunc)cvtScale16u64f,
1054 (BinaryFunc)cvtScale16s64f, (BinaryFunc)cvtScale32s64f, (BinaryFunc)cvtScale32f64f,
1055 (BinaryFunc)cvtScale64f, 0
1058 0, 0, 0, 0, 0, 0, 0, 0
1062 return cvtScaleTab[CV_MAT_DEPTH(ddepth)][CV_MAT_DEPTH(sdepth)];
1067 void cv::convertScaleAbs( InputArray _src, OutputArray _dst, double alpha, double beta )
1069 Mat src = _src.getMat();
1070 int cn = src.channels();
1071 double scale[] = {alpha, beta};
1072 _dst.create( src.dims, src.size, CV_8UC(cn) );
1073 Mat dst = _dst.getMat();
1074 BinaryFunc func = getCvtScaleAbsFunc(src.depth());
1075 CV_Assert( func != 0 );
1079 Size sz = getContinuousSize(src, dst, cn);
1080 func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale );
1084 const Mat* arrays[] = {&src, &dst, 0};
1086 NAryMatIterator it(arrays, ptrs);
1087 Size sz((int)it.size*cn, 1);
1089 for( size_t i = 0; i < it.nplanes; i++, ++it )
1090 func( ptrs[0], 0, 0, 0, ptrs[1], 0, sz, scale );
1094 void cv::Mat::convertTo(OutputArray _dst, int _type, double alpha, double beta) const
1096 bool noScale = fabs(alpha-1) < DBL_EPSILON && fabs(beta) < DBL_EPSILON;
1099 _type = _dst.fixedType() ? _dst.type() : type();
1101 _type = CV_MAKETYPE(CV_MAT_DEPTH(_type), channels());
1103 int sdepth = depth(), ddepth = CV_MAT_DEPTH(_type);
1104 if( sdepth == ddepth && noScale )
1112 BinaryFunc func = noScale ? getConvertFunc(sdepth, ddepth) : getConvertScaleFunc(sdepth, ddepth);
1113 double scale[] = {alpha, beta};
1114 int cn = channels();
1115 CV_Assert( func != 0 );
1119 _dst.create( size(), _type );
1120 Mat dst = _dst.getMat();
1121 Size sz = getContinuousSize(src, dst, cn);
1122 func( src.data, src.step, 0, 0, dst.data, dst.step, sz, scale );
1126 _dst.create( dims, size, _type );
1127 Mat dst = _dst.getMat();
1128 const Mat* arrays[] = {&src, &dst, 0};
1130 NAryMatIterator it(arrays, ptrs);
1131 Size sz((int)(it.size*cn), 1);
1133 for( size_t i = 0; i < it.nplanes; i++, ++it )
1134 func(ptrs[0], 0, 0, 0, ptrs[1], 0, sz, scale);
1138 /****************************************************************************************\
1140 \****************************************************************************************/
1145 template<typename T> static void
1146 LUT8u_( const uchar* src, const T* lut, T* dst, int len, int cn, int lutcn )
1150 for( int i = 0; i < len*cn; i++ )
1151 dst[i] = lut[src[i]];
1155 for( int i = 0; i < len*cn; i += cn )
1156 for( int k = 0; k < cn; k++ )
1157 dst[i+k] = lut[src[i+k]*cn+k];
1161 static void LUT8u_8u( const uchar* src, const uchar* lut, uchar* dst, int len, int cn, int lutcn )
1163 LUT8u_( src, lut, dst, len, cn, lutcn );
1166 static void LUT8u_8s( const uchar* src, const schar* lut, schar* dst, int len, int cn, int lutcn )
1168 LUT8u_( src, lut, dst, len, cn, lutcn );
1171 static void LUT8u_16u( const uchar* src, const ushort* lut, ushort* dst, int len, int cn, int lutcn )
1173 LUT8u_( src, lut, dst, len, cn, lutcn );
1176 static void LUT8u_16s( const uchar* src, const short* lut, short* dst, int len, int cn, int lutcn )
1178 LUT8u_( src, lut, dst, len, cn, lutcn );
1181 static void LUT8u_32s( const uchar* src, const int* lut, int* dst, int len, int cn, int lutcn )
1183 LUT8u_( src, lut, dst, len, cn, lutcn );
1186 static void LUT8u_32f( const uchar* src, const float* lut, float* dst, int len, int cn, int lutcn )
1188 LUT8u_( src, lut, dst, len, cn, lutcn );
1191 static void LUT8u_64f( const uchar* src, const double* lut, double* dst, int len, int cn, int lutcn )
1193 LUT8u_( src, lut, dst, len, cn, lutcn );
1196 typedef void (*LUTFunc)( const uchar* src, const uchar* lut, uchar* dst, int len, int cn, int lutcn );
1198 static LUTFunc lutTab[] =
1200 (LUTFunc)LUT8u_8u, (LUTFunc)LUT8u_8s, (LUTFunc)LUT8u_16u, (LUTFunc)LUT8u_16s,
1201 (LUTFunc)LUT8u_32s, (LUTFunc)LUT8u_32f, (LUTFunc)LUT8u_64f, 0
1206 void cv::LUT( InputArray _src, InputArray _lut, OutputArray _dst, int interpolation )
1208 Mat src = _src.getMat(), lut = _lut.getMat();
1209 CV_Assert( interpolation == 0 );
1210 int cn = src.channels();
1211 int lutcn = lut.channels();
1213 CV_Assert( (lutcn == cn || lutcn == 1) &&
1214 lut.total() == 256 && lut.isContinuous() &&
1215 (src.depth() == CV_8U || src.depth() == CV_8S) );
1216 _dst.create( src.dims, src.size, CV_MAKETYPE(lut.depth(), cn));
1217 Mat dst = _dst.getMat();
1219 LUTFunc func = lutTab[lut.depth()];
1220 CV_Assert( func != 0 );
1222 const Mat* arrays[] = {&src, &dst, 0};
1224 NAryMatIterator it(arrays, ptrs);
1225 int len = (int)it.size;
1227 for( size_t i = 0; i < it.nplanes; i++, ++it )
1228 func(ptrs[0], lut.data, ptrs[1], len, cn, lutcn);
1232 void cv::normalize( InputArray _src, OutputArray _dst, double a, double b,
1233 int norm_type, int rtype, InputArray _mask )
1235 Mat src = _src.getMat(), mask = _mask.getMat();
1237 double scale = 1, shift = 0;
1238 if( norm_type == CV_MINMAX )
1240 double smin = 0, smax = 0;
1241 double dmin = MIN( a, b ), dmax = MAX( a, b );
1242 minMaxLoc( _src, &smin, &smax, 0, 0, mask );
1243 scale = (dmax - dmin)*(smax - smin > DBL_EPSILON ? 1./(smax - smin) : 0);
1244 shift = dmin - smin*scale;
1246 else if( norm_type == CV_L2 || norm_type == CV_L1 || norm_type == CV_C )
1248 scale = norm( src, norm_type, mask );
1249 scale = scale > DBL_EPSILON ? a/scale : 0.;
1253 CV_Error( CV_StsBadArg, "Unknown/unsupported norm type" );
1256 rtype = _dst.fixedType() ? _dst.depth() : src.depth();
1258 _dst.create(src.dims, src.size, CV_MAKETYPE(rtype, src.channels()));
1259 Mat dst = _dst.getMat();
1262 src.convertTo( dst, rtype, scale, shift );
1266 src.convertTo( temp, rtype, scale, shift );
1267 temp.copyTo( dst, mask );
1272 cvSplit( const void* srcarr, void* dstarr0, void* dstarr1, void* dstarr2, void* dstarr3 )
1274 void* dptrs[] = { dstarr0, dstarr1, dstarr2, dstarr3 };
1275 cv::Mat src = cv::cvarrToMat(srcarr);
1277 for( i = 0; i < 4; i++ )
1278 nz += dptrs[i] != 0;
1279 CV_Assert( nz > 0 );
1280 cv::vector<cv::Mat> dvec(nz);
1281 cv::vector<int> pairs(nz*2);
1283 for( i = j = 0; i < 4; i++ )
1287 dvec[j] = cv::cvarrToMat(dptrs[i]);
1288 CV_Assert( dvec[j].size() == src.size() );
1289 CV_Assert( dvec[j].depth() == src.depth() );
1290 CV_Assert( dvec[j].channels() == 1 );
1291 CV_Assert( i < src.channels() );
1297 if( nz == src.channels() )
1298 cv::split( src, dvec );
1301 cv::mixChannels( &src, 1, &dvec[0], nz, &pairs[0], nz );
1307 cvMerge( const void* srcarr0, const void* srcarr1, const void* srcarr2,
1308 const void* srcarr3, void* dstarr )
1310 const void* sptrs[] = { srcarr0, srcarr1, srcarr2, srcarr3 };
1311 cv::Mat dst = cv::cvarrToMat(dstarr);
1313 for( i = 0; i < 4; i++ )
1314 nz += sptrs[i] != 0;
1315 CV_Assert( nz > 0 );
1316 cv::vector<cv::Mat> svec(nz);
1317 cv::vector<int> pairs(nz*2);
1319 for( i = j = 0; i < 4; i++ )
1323 svec[j] = cv::cvarrToMat(sptrs[i]);
1324 CV_Assert( svec[j].size == dst.size &&
1325 svec[j].depth() == dst.depth() &&
1326 svec[j].channels() == 1 && i < dst.channels() );
1333 if( nz == dst.channels() )
1334 cv::merge( svec, dst );
1337 cv::mixChannels( &svec[0], nz, &dst, 1, &pairs[0], nz );
1343 cvMixChannels( const CvArr** src, int src_count,
1344 CvArr** dst, int dst_count,
1345 const int* from_to, int pair_count )
1347 cv::AutoBuffer<cv::Mat, 32> buf(src_count + dst_count);
1350 for( i = 0; i < src_count; i++ )
1351 buf[i] = cv::cvarrToMat(src[i]);
1352 for( i = 0; i < dst_count; i++ )
1353 buf[i+src_count] = cv::cvarrToMat(dst[i]);
1354 cv::mixChannels(&buf[0], src_count, &buf[src_count], dst_count, from_to, pair_count);
1358 cvConvertScaleAbs( const void* srcarr, void* dstarr,
1359 double scale, double shift )
1361 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
1362 CV_Assert( src.size == dst.size && dst.type() == CV_8UC(src.channels()));
1363 cv::convertScaleAbs( src, dst, scale, shift );
1367 cvConvertScale( const void* srcarr, void* dstarr,
1368 double scale, double shift )
1370 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
1372 CV_Assert( src.size == dst.size && src.channels() == dst.channels() );
1373 src.convertTo(dst, dst.type(), scale, shift);
1376 CV_IMPL void cvLUT( const void* srcarr, void* dstarr, const void* lutarr )
1378 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), lut = cv::cvarrToMat(lutarr);
1380 CV_Assert( dst.size() == src.size() && dst.type() == CV_MAKETYPE(lut.depth(), src.channels()) );
1381 cv::LUT( src, lut, dst );
1384 CV_IMPL void cvNormalize( const CvArr* srcarr, CvArr* dstarr,
1385 double a, double b, int norm_type, const CvArr* maskarr )
1387 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), mask;
1389 mask = cv::cvarrToMat(maskarr);
1390 CV_Assert( dst.size() == src.size() && src.channels() == dst.channels() );
1391 cv::normalize( src, dst, a, b, norm_type, dst.type(), mask );