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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
44 /****************************************************************************************\
45 * Find sum of pixels in the ROI *
46 \****************************************************************************************/
48 #define ICV_SUM_COI_CASE( __op__, len, cn ) \
49 for( ; x <= (len) - 4*(cn); x += 4*(cn) ) \
50 s0 += __op__(src[x]) + __op__(src[x+(cn)]) + \
51 __op__(src[x+(cn)*2]) + __op__(src[x+(cn)*3]);\
53 for( ; x < (len); x += (cn) ) \
57 #define ICV_SUM_CASE_C1( __op__, len ) \
58 ICV_SUM_COI_CASE( __op__, len, 1 )
61 #define ICV_SUM_CASE_C2( __op__, len ) \
62 for( ; x <= (len) - 8; x += 8 ) \
64 s0 += __op__(src[x]) + __op__(src[x+2]) + \
65 __op__(src[x+4]) + __op__(src[x+6]); \
66 s1 += __op__(src[x+1]) + __op__(src[x+3]) + \
67 __op__(src[x+5]) + __op__(src[x+7]); \
70 for( ; x < (len); x += 2 ) \
72 s0 += __op__(src[x]); \
73 s1 += __op__(src[x+1]); \
78 #define ICV_SUM_CASE_C3( __op__, len ) \
79 for( ; x <= (len) - 12; x += 12 ) \
81 s0 += __op__(src[x]) + __op__(src[x+3]) + \
82 __op__(src[x+6]) + __op__(src[x+9]); \
83 s1 += __op__(src[x+1]) + __op__(src[x+4]) + \
84 __op__(src[x+7]) + __op__(src[x+10]); \
85 s2 += __op__(src[x+2]) + __op__(src[x+5]) + \
86 __op__(src[x+8]) + __op__(src[x+11]); \
89 for( ; x < (len); x += 3 ) \
91 s0 += __op__(src[x]); \
92 s1 += __op__(src[x+1]); \
93 s2 += __op__(src[x+2]); \
97 #define ICV_SUM_CASE_C4( __op__, len ) \
98 for( ; x <= (len) - 16; x += 16 ) \
100 s0 += __op__(src[x]) + __op__(src[x+4]) + \
101 __op__(src[x+8]) + __op__(src[x+12]); \
102 s1 += __op__(src[x+1]) + __op__(src[x+5]) + \
103 __op__(src[x+9]) + __op__(src[x+13]); \
104 s2 += __op__(src[x+2]) + __op__(src[x+6]) + \
105 __op__(src[x+10]) + __op__(src[x+14]); \
106 s3 += __op__(src[x+3]) + __op__(src[x+7]) + \
107 __op__(src[x+11]) + __op__(src[x+15]); \
110 for( ; x < (len); x += 4 ) \
112 s0 += __op__(src[x]); \
113 s1 += __op__(src[x+1]); \
114 s2 += __op__(src[x+2]); \
115 s3 += __op__(src[x+3]); \
119 ////////////////////////////////////// entry macros //////////////////////////////////////
121 #define ICV_SUM_ENTRY_COMMON() \
122 step /= sizeof(src[0])
124 #define ICV_SUM_ENTRY_C1( sumtype ) \
126 ICV_SUM_ENTRY_COMMON()
128 #define ICV_SUM_ENTRY_C2( sumtype ) \
129 sumtype s0 = 0, s1 = 0; \
130 ICV_SUM_ENTRY_COMMON()
132 #define ICV_SUM_ENTRY_C3( sumtype ) \
133 sumtype s0 = 0, s1 = 0, s2 = 0; \
134 ICV_SUM_ENTRY_COMMON()
136 #define ICV_SUM_ENTRY_C4( sumtype ) \
137 sumtype s0 = 0, s1 = 0, s2 = 0, s3 = 0; \
138 ICV_SUM_ENTRY_COMMON()
141 #define ICV_SUM_ENTRY_BLOCK_COMMON( block_size ) \
142 int remaining = block_size; \
143 ICV_SUM_ENTRY_COMMON()
145 #define ICV_SUM_ENTRY_BLOCK_C1( sumtype, worktype, block_size ) \
148 ICV_SUM_ENTRY_BLOCK_COMMON( block_size )
150 #define ICV_SUM_ENTRY_BLOCK_C2( sumtype, worktype, block_size ) \
151 sumtype sum0 = 0, sum1 = 0; \
152 worktype s0 = 0, s1 = 0; \
153 ICV_SUM_ENTRY_BLOCK_COMMON( block_size )
155 #define ICV_SUM_ENTRY_BLOCK_C3( sumtype, worktype, block_size ) \
156 sumtype sum0 = 0, sum1 = 0, sum2 = 0; \
157 worktype s0 = 0, s1 = 0, s2 = 0; \
158 ICV_SUM_ENTRY_BLOCK_COMMON( block_size )
160 #define ICV_SUM_ENTRY_BLOCK_C4( sumtype, worktype, block_size ) \
161 sumtype sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0; \
162 worktype s0 = 0, s1 = 0, s2 = 0, s3 = 0; \
163 ICV_SUM_ENTRY_BLOCK_COMMON( block_size )
166 /////////////////////////////////////// exit macros //////////////////////////////////////
168 #define ICV_SUM_EXIT_C1( tmp, sumtype ) \
169 sum[0] = (sumtype)tmp##0
171 #define ICV_SUM_EXIT_C2( tmp, sumtype ) \
172 sum[0] = (sumtype)tmp##0; \
173 sum[1] = (sumtype)tmp##1;
175 #define ICV_SUM_EXIT_C3( tmp, sumtype ) \
176 sum[0] = (sumtype)tmp##0; \
177 sum[1] = (sumtype)tmp##1; \
178 sum[2] = (sumtype)tmp##2;
180 #define ICV_SUM_EXIT_C4( tmp, sumtype ) \
181 sum[0] = (sumtype)tmp##0; \
182 sum[1] = (sumtype)tmp##1; \
183 sum[2] = (sumtype)tmp##2; \
184 sum[3] = (sumtype)tmp##3;
186 #define ICV_SUM_EXIT_BLOCK_C1( sumtype ) \
188 ICV_SUM_EXIT_C1( sum, sumtype )
190 #define ICV_SUM_EXIT_BLOCK_C2( sumtype ) \
191 sum0 += s0; sum1 += s1; \
192 ICV_SUM_EXIT_C2( sum, sumtype )
194 #define ICV_SUM_EXIT_BLOCK_C3( sumtype ) \
195 sum0 += s0; sum1 += s1; \
197 ICV_SUM_EXIT_C3( sum, sumtype )
199 #define ICV_SUM_EXIT_BLOCK_C4( sumtype ) \
200 sum0 += s0; sum1 += s1; \
201 sum2 += s2; sum3 += s3; \
202 ICV_SUM_EXIT_C4( sum, sumtype )
204 ////////////////////////////////////// update macros /////////////////////////////////////
206 #define ICV_SUM_UPDATE_COMMON( block_size ) \
207 remaining = block_size
209 #define ICV_SUM_UPDATE_C1( block_size ) \
210 ICV_SUM_UPDATE_COMMON( block_size ); \
214 #define ICV_SUM_UPDATE_C2( block_size ) \
215 ICV_SUM_UPDATE_COMMON( block_size ); \
216 sum0 += s0; sum1 += s1; \
219 #define ICV_SUM_UPDATE_C3( block_size ) \
220 ICV_SUM_UPDATE_COMMON( block_size ); \
221 sum0 += s0; sum1 += s1; sum2 += s2; \
224 #define ICV_SUM_UPDATE_C4( block_size ) \
225 ICV_SUM_UPDATE_COMMON( block_size ); \
226 sum0 += s0; sum1 += s1; \
227 sum2 += s2; sum3 += s3; \
228 s0 = s1 = s2 = s3 = 0
231 #define ICV_DEF_SUM_NOHINT_BLOCK_FUNC_2D( name, flavor, cn, \
232 __op__, arrtype, sumtype_final, sumtype, worktype, block_size )\
233 IPCVAPI_IMPL(CvStatus, icv##name##_##flavor##_C##cn##R,( \
234 const arrtype* src, int step, CvSize size, \
235 sumtype_final* sum ), (src, step, size, sum) ) \
237 ICV_SUM_ENTRY_BLOCK_C##cn(sumtype,worktype,(block_size)*(cn)); \
240 for( ; size.height--; src += step ) \
243 while( x < size.width ) \
245 int limit = MIN( remaining, size.width - x ); \
246 remaining -= limit; \
248 ICV_SUM_CASE_C##cn( __op__, limit ); \
249 if( remaining == 0 ) \
251 ICV_SUM_UPDATE_C##cn( (block_size)*(cn) ); \
256 ICV_SUM_EXIT_BLOCK_C##cn( sumtype_final ); \
261 #define ICV_DEF_SUM_NOHINT_FUNC_2D( name, flavor, cn, \
262 __op__, arrtype, sumtype_final, sumtype, worktype, block_size )\
263 IPCVAPI_IMPL(CvStatus, icv##name##_##flavor##_C##cn##R,( \
264 const arrtype* src, int step, CvSize size, \
265 sumtype_final* sum ), (src, step, size, sum) ) \
267 ICV_SUM_ENTRY_C##cn( sumtype ); \
270 for( ; size.height--; src += step ) \
273 ICV_SUM_CASE_C##cn( __op__, size.width ); \
276 ICV_SUM_EXIT_C##cn( s, sumtype_final ); \
281 #define ICV_DEF_SUM_HINT_FUNC_2D( name, flavor, cn, \
282 __op__, arrtype, sumtype_final, sumtype, worktype, block_size )\
283 IPCVAPI_IMPL(CvStatus, icv##name##_##flavor##_C##cn##R,( \
284 const arrtype* src, int step, CvSize size, \
285 sumtype_final* sum, CvHintAlgorithm /*hint*/ ), \
286 (src, step, size, sum, cvAlgHintAccurate) ) \
288 ICV_SUM_ENTRY_C##cn( sumtype ); \
291 for( ; size.height--; src += step ) \
294 ICV_SUM_CASE_C##cn( __op__, size.width ); \
297 ICV_SUM_EXIT_C##cn( s, sumtype_final ); \
302 #define ICV_DEF_SUM_NOHINT_BLOCK_FUNC_2D_COI( name, flavor, \
303 __op__, arrtype, sumtype_final, sumtype, worktype, block_size )\
304 static CvStatus CV_STDCALL icv##name##_##flavor##_CnCR( \
305 const arrtype* src, int step, CvSize size, int cn, \
306 int coi, sumtype_final* sum ) \
308 ICV_SUM_ENTRY_BLOCK_C1(sumtype,worktype,(block_size)*(cn)); \
312 for( ; size.height--; src += step ) \
315 while( x < size.width ) \
317 int limit = MIN( remaining, size.width - x ); \
318 remaining -= limit; \
320 ICV_SUM_COI_CASE( __op__, limit, cn ); \
321 if( remaining == 0 ) \
323 ICV_SUM_UPDATE_C1( (block_size)*(cn) ); \
328 ICV_SUM_EXIT_BLOCK_C1( sumtype_final ); \
333 #define ICV_DEF_SUM_NOHINT_FUNC_2D_COI( name, flavor, \
334 __op__, arrtype, sumtype_final, sumtype, worktype, block_size )\
335 static CvStatus CV_STDCALL icv##name##_##flavor##_CnCR( \
336 const arrtype* src, int step, CvSize size, int cn, \
337 int coi, sumtype_final* sum ) \
339 ICV_SUM_ENTRY_C1( sumtype ); \
343 for( ; size.height--; src += step ) \
346 ICV_SUM_COI_CASE( __op__, size.width, cn ); \
349 ICV_SUM_EXIT_C1( s, sumtype_final ); \
354 #define ICV_DEF_SUM_ALL( name, flavor, __op__, arrtype, sumtype_final, sumtype, \
355 worktype, hintp_type, nohint_type, block_size ) \
356 ICV_DEF_SUM_##hintp_type##_FUNC_2D( name, flavor, 1, __op__, arrtype, \
357 sumtype_final, sumtype, worktype, block_size ) \
358 ICV_DEF_SUM_##hintp_type##_FUNC_2D( name, flavor, 2, __op__, arrtype, \
359 sumtype_final, sumtype, worktype, block_size ) \
360 ICV_DEF_SUM_##hintp_type##_FUNC_2D( name, flavor, 3, __op__, arrtype, \
361 sumtype_final, sumtype, worktype, block_size ) \
362 ICV_DEF_SUM_##hintp_type##_FUNC_2D( name, flavor, 4, __op__, arrtype, \
363 sumtype_final, sumtype, worktype, block_size ) \
364 ICV_DEF_SUM_##nohint_type##_FUNC_2D_COI( name, flavor, __op__, arrtype, \
365 sumtype_final, sumtype, worktype, block_size )
367 ICV_DEF_SUM_ALL( Sum, 8u, CV_NOP, uchar, double, int64, unsigned,
368 NOHINT_BLOCK, NOHINT_BLOCK, 1 << 24 )
369 ICV_DEF_SUM_ALL( Sum, 16u, CV_NOP, ushort, double, int64, unsigned,
370 NOHINT_BLOCK, NOHINT_BLOCK, 1 << 16 )
371 ICV_DEF_SUM_ALL( Sum, 16s, CV_NOP, short, double, int64, int,
372 NOHINT_BLOCK, NOHINT_BLOCK, 1 << 16 )
373 ICV_DEF_SUM_ALL( Sum, 32s, CV_NOP, int, double, double, double, NOHINT, NOHINT, 0 )
374 ICV_DEF_SUM_ALL( Sum, 32f, CV_NOP, float, double, double, double, HINT, NOHINT, 0 )
375 ICV_DEF_SUM_ALL( Sum, 64f, CV_NOP, double, double, double, double, NOHINT, NOHINT, 0 )
377 #define icvSum_8s_C1R 0
378 #define icvSum_8s_C2R 0
379 #define icvSum_8s_C3R 0
380 #define icvSum_8s_C4R 0
381 #define icvSum_8s_CnCR 0
383 CV_DEF_INIT_BIG_FUNC_TAB_2D( Sum, R )
384 CV_DEF_INIT_FUNC_TAB_2D( Sum, CnCR )
387 cvSum( const CvArr* arr )
389 static CvBigFuncTable sum_tab;
390 static CvFuncTable sumcoi_tab;
391 static int inittab = 0;
393 CvScalar sum = {{0,0,0,0}};
395 CV_FUNCNAME("cvSum");
402 CvMat stub, *mat = (CvMat*)arr;
406 icvInitSumRTable( &sum_tab );
407 icvInitSumCnCRTable( &sumcoi_tab );
411 if( !CV_IS_MAT(mat) )
413 if( CV_IS_MATND(mat) )
415 void* matnd = (void*)mat;
417 CvNArrayIterator iterator;
420 CV_CALL( cvInitNArrayIterator( 1, &matnd, 0, &nstub, &iterator ));
422 type = CV_MAT_TYPE(iterator.hdr[0]->type);
423 if( CV_MAT_CN(type) > 4 )
424 CV_ERROR( CV_StsOutOfRange, "The input array must have at most 4 channels" );
426 pass_hint = CV_MAT_DEPTH(type) == CV_32F;
430 CvFunc2D_1A1P func = (CvFunc2D_1A1P)(sum_tab.fn_2d[type]);
432 CV_ERROR( CV_StsUnsupportedFormat, "" );
436 CvScalar temp = {{0,0,0,0}};
437 IPPI_CALL( func( iterator.ptr[0], CV_STUB_STEP,
438 iterator.size, temp.val ));
439 sum.val[0] += temp.val[0];
440 sum.val[1] += temp.val[1];
441 sum.val[2] += temp.val[2];
442 sum.val[3] += temp.val[3];
444 while( cvNextNArraySlice( &iterator ));
448 CvFunc2D_1A1P1I func = (CvFunc2D_1A1P1I)(sum_tab.fn_2d[type]);
450 CV_ERROR( CV_StsUnsupportedFormat, "" );
454 CvScalar temp = {{0,0,0,0}};
455 IPPI_CALL( func( iterator.ptr[0], CV_STUB_STEP,
456 iterator.size, temp.val, cvAlgHintAccurate ));
457 sum.val[0] += temp.val[0];
458 sum.val[1] += temp.val[1];
459 sum.val[2] += temp.val[2];
460 sum.val[3] += temp.val[3];
462 while( cvNextNArraySlice( &iterator ));
467 CV_CALL( mat = cvGetMat( mat, &stub, &coi ));
470 type = CV_MAT_TYPE(mat->type);
471 size = cvGetMatSize( mat );
473 mat_step = mat->step;
475 if( CV_IS_MAT_CONT( mat->type ))
477 size.width *= size.height;
479 if( size.width <= CV_MAX_INLINE_MAT_OP_SIZE )
481 if( type == CV_32FC1 )
483 float* data = mat->data.fl;
487 sum.val[0] += data[size.width - 1];
489 while( --size.width );
494 if( type == CV_64FC1 )
496 double* data = mat->data.db;
500 sum.val[0] += data[size.width - 1];
502 while( --size.width );
508 mat_step = CV_STUB_STEP;
511 if( CV_MAT_CN(type) == 1 || coi == 0 )
513 int pass_hint = CV_MAT_DEPTH(type) == CV_32F;
515 if( CV_MAT_CN(type) > 4 )
516 CV_ERROR( CV_StsOutOfRange, "The input array must have at most 4 channels" );
520 CvFunc2D_1A1P func = (CvFunc2D_1A1P)(sum_tab.fn_2d[type]);
523 CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
525 IPPI_CALL( func( mat->data.ptr, mat_step, size, sum.val ));
529 CvFunc2D_1A1P1I func = (CvFunc2D_1A1P1I)(sum_tab.fn_2d[type]);
532 CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
534 IPPI_CALL( func( mat->data.ptr, mat_step, size, sum.val, cvAlgHintAccurate ));
539 CvFunc2DnC_1A1P func = (CvFunc2DnC_1A1P)(sumcoi_tab.fn_2d[CV_MAT_DEPTH(type)]);
542 CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
544 IPPI_CALL( func( mat->data.ptr, mat_step, size,
545 CV_MAT_CN(type), coi, sum.val ));
554 #define ICV_DEF_NONZERO_ALL( flavor, __op__, arrtype ) \
555 ICV_DEF_SUM_NOHINT_FUNC_2D( CountNonZero, flavor, 1, __op__, \
556 arrtype, int, int, int, 0 ) \
557 ICV_DEF_SUM_NOHINT_FUNC_2D_COI( CountNonZero, flavor, __op__, \
558 arrtype, int, int, int, 0 )
560 #undef CV_NONZERO_DBL
561 #define CV_NONZERO_DBL(x) (((x) & CV_BIG_INT(0x7fffffffffffffff)) != 0)
563 ICV_DEF_NONZERO_ALL( 8u, CV_NONZERO, uchar )
564 ICV_DEF_NONZERO_ALL( 16s, CV_NONZERO, ushort )
565 ICV_DEF_NONZERO_ALL( 32s, CV_NONZERO, int )
566 ICV_DEF_NONZERO_ALL( 32f, CV_NONZERO_FLT, int )
567 ICV_DEF_NONZERO_ALL( 64f, CV_NONZERO_DBL, int64 )
569 #define icvCountNonZero_8s_C1R icvCountNonZero_8u_C1R
570 #define icvCountNonZero_8s_CnCR icvCountNonZero_8u_CnCR
571 #define icvCountNonZero_16u_C1R icvCountNonZero_16s_C1R
572 #define icvCountNonZero_16u_CnCR icvCountNonZero_16s_CnCR
574 CV_DEF_INIT_FUNC_TAB_2D( CountNonZero, C1R )
575 CV_DEF_INIT_FUNC_TAB_2D( CountNonZero, CnCR )
578 cvCountNonZero( const CvArr* arr )
580 static CvFuncTable nz_tab;
581 static CvFuncTable nzcoi_tab;
582 static int inittab = 0;
586 CV_FUNCNAME("cvCountNonZero");
593 CvMat stub, *mat = (CvMat*)arr;
597 icvInitCountNonZeroC1RTable( &nz_tab );
598 icvInitCountNonZeroCnCRTable( &nzcoi_tab );
602 if( !CV_IS_MAT(mat) )
604 if( CV_IS_MATND(mat) )
606 void* matnd = (void*)arr;
608 CvNArrayIterator iterator;
611 CV_CALL( cvInitNArrayIterator( 1, &matnd, 0, &nstub, &iterator ));
613 type = CV_MAT_TYPE(iterator.hdr[0]->type);
615 if( CV_MAT_CN(type) != 1 )
616 CV_ERROR( CV_BadNumChannels,
617 "Only single-channel array are supported here" );
619 func = (CvFunc2D_1A1P)(nz_tab.fn_2d[CV_MAT_DEPTH(type)]);
621 CV_ERROR( CV_StsUnsupportedFormat, "" );
626 IPPI_CALL( func( iterator.ptr[0], CV_STUB_STEP,
627 iterator.size, &temp ));
630 while( cvNextNArraySlice( &iterator ));
634 CV_CALL( mat = cvGetMat( mat, &stub, &coi ));
637 type = CV_MAT_TYPE(mat->type);
638 size = cvGetMatSize( mat );
640 mat_step = mat->step;
642 if( CV_IS_MAT_CONT( mat->type ))
644 size.width *= size.height;
646 mat_step = CV_STUB_STEP;
649 if( CV_MAT_CN(type) == 1 || coi == 0 )
651 CvFunc2D_1A1P func = (CvFunc2D_1A1P)(nz_tab.fn_2d[CV_MAT_DEPTH(type)]);
653 if( CV_MAT_CN(type) != 1 )
654 CV_ERROR( CV_BadNumChannels,
655 "The function can handle only a single channel at a time (use COI)");
658 CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
660 IPPI_CALL( func( mat->data.ptr, mat_step, size, &count ));
664 CvFunc2DnC_1A1P func = (CvFunc2DnC_1A1P)(nzcoi_tab.fn_2d[CV_MAT_DEPTH(type)]);
667 CV_ERROR( CV_StsBadArg, cvUnsupportedFormat );
669 IPPI_CALL( func( mat->data.ptr, mat_step, size, CV_MAT_CN(type), coi, &count ));
678 /****************************************************************************************\
679 * Reduce Matrix to Vector *
680 \****************************************************************************************/
682 #define ICV_ACC_ROWS_FUNC( name, flavor, arrtype, acctype, \
683 __op__, load_macro ) \
684 static CvStatus CV_STDCALL \
685 icv##name##Rows_##flavor##_C1R( const arrtype* src, int srcstep,\
686 acctype* dst, CvSize size ) \
688 int i, width = size.width; \
689 srcstep /= sizeof(src[0]); \
691 for( i = 0; i < width; i++ ) \
692 dst[i] = load_macro(src[i]); \
694 for( ; --size.height; ) \
697 for( i = 0; i <= width - 4; i += 4 ) \
699 acctype s0 = load_macro(src[i]); \
700 acctype s1 = load_macro(src[i+1]); \
701 acctype a0 = dst[i], a1 = dst[i+1]; \
702 a0 = (acctype)__op__(a0,s0); a1 = (acctype)__op__(a1,s1); \
703 dst[i] = a0; dst[i+1] = a1; \
705 s0 = load_macro(src[i+2]); \
706 s1 = load_macro(src[i+3]); \
707 a0 = dst[i+2]; a1 = dst[i+3]; \
708 a0 = (acctype)__op__(a0,s0); a1 = (acctype)__op__(a1,s1); \
709 dst[i+2] = a0; dst[i+3] = a1; \
712 for( ; i < width; i++ ) \
714 acctype s0 = load_macro(src[i]), a0 = dst[i]; \
715 a0 = (acctype)__op__(a0,s0); \
724 #define ICV_ACC_COLS_FUNC_C1( name, flavor, arrtype, worktype, acctype, __op__ )\
725 static CvStatus CV_STDCALL \
726 icv##name##Cols_##flavor##_C1R( const arrtype* src, int srcstep, \
727 acctype* dst, int dststep, CvSize size )\
729 int i, width = size.width; \
730 srcstep /= sizeof(src[0]); \
731 dststep /= sizeof(dst[0]); \
733 for( ; size.height--; src += srcstep, dst += dststep ) \
736 dst[0] = (acctype)src[0]; \
739 worktype a0 = src[0], a1 = src[1]; \
740 for( i = 2; i <= width - 4; i += 4 ) \
742 worktype s0 = src[i], s1 = src[i+1]; \
743 a0 = __op__(a0, s0); \
744 a1 = __op__(a1, s1); \
745 s0 = src[i+2]; s1 = src[i+3]; \
746 a0 = __op__(a0, s0); \
747 a1 = __op__(a1, s1); \
750 for( ; i < width; i++ ) \
752 worktype s0 = src[i]; \
753 a0 = __op__(a0, s0); \
755 a0 = __op__(a0, a1); \
756 dst[0] = (acctype)a0; \
764 #define ICV_ACC_COLS_FUNC_C3( name, flavor, arrtype, worktype, acctype, __op__ ) \
765 static CvStatus CV_STDCALL \
766 icv##name##Cols_##flavor##_C3R( const arrtype* src, int srcstep, \
767 acctype* dst, int dststep, CvSize size )\
769 int i, width = size.width*3; \
770 srcstep /= sizeof(src[0]); \
771 dststep /= sizeof(dst[0]); \
773 for( ; size.height--; src += srcstep, dst += dststep ) \
775 worktype a0 = src[0], a1 = src[1], a2 = src[2]; \
776 for( i = 3; i < width; i += 3 ) \
778 worktype s0 = src[i], s1 = src[i+1], s2 = src[i+2]; \
779 a0 = __op__(a0, s0); \
780 a1 = __op__(a1, s1); \
781 a2 = __op__(a2, s2); \
784 dst[0] = (acctype)a0; \
785 dst[1] = (acctype)a1; \
786 dst[2] = (acctype)a2; \
793 #define ICV_ACC_COLS_FUNC_C4( name, flavor, arrtype, worktype, acctype, __op__ ) \
794 static CvStatus CV_STDCALL \
795 icv##name##Cols_##flavor##_C4R( const arrtype* src, int srcstep, \
796 acctype* dst, int dststep, CvSize size )\
798 int i, width = size.width*4; \
799 srcstep /= sizeof(src[0]); \
800 dststep /= sizeof(dst[0]); \
802 for( ; size.height--; src += srcstep, dst += dststep ) \
804 worktype a0 = src[0], a1 = src[1], a2 = src[2], a3 = src[3]; \
805 for( i = 4; i < width; i += 4 ) \
807 worktype s0 = src[i], s1 = src[i+1]; \
808 a0 = __op__(a0, s0); \
809 a1 = __op__(a1, s1); \
810 s0 = src[i+2]; s1 = src[i+3]; \
811 a2 = __op__(a2, s0); \
812 a3 = __op__(a3, s1); \
815 dst[0] = (acctype)a0; \
816 dst[1] = (acctype)a1; \
817 dst[2] = (acctype)a2; \
818 dst[3] = (acctype)a3; \
825 ICV_ACC_ROWS_FUNC( Sum, 8u32s, uchar, int, CV_ADD, CV_NOP )
826 ICV_ACC_ROWS_FUNC( Sum, 8u32f, uchar, float, CV_ADD, CV_8TO32F )
827 ICV_ACC_ROWS_FUNC( Sum, 16u32f, ushort, float, CV_ADD, CV_NOP )
828 ICV_ACC_ROWS_FUNC( Sum, 16u64f, ushort, double, CV_ADD, CV_NOP )
829 ICV_ACC_ROWS_FUNC( Sum, 16s32f, short, float, CV_ADD, CV_NOP )
830 ICV_ACC_ROWS_FUNC( Sum, 16s64f, short, double, CV_ADD, CV_NOP )
831 ICV_ACC_ROWS_FUNC( Sum, 32f, float, float, CV_ADD, CV_NOP )
832 ICV_ACC_ROWS_FUNC( Sum, 32f64f, float, double, CV_ADD, CV_NOP )
833 ICV_ACC_ROWS_FUNC( Sum, 64f, double, double, CV_ADD, CV_NOP )
835 ICV_ACC_ROWS_FUNC( Max, 8u, uchar, uchar, CV_MAX_8U, CV_NOP )
836 ICV_ACC_ROWS_FUNC( Max, 32f, float, float, MAX, CV_NOP )
837 ICV_ACC_ROWS_FUNC( Max, 64f, double, double, MAX, CV_NOP )
839 ICV_ACC_ROWS_FUNC( Min, 8u, uchar, uchar, CV_MIN_8U, CV_NOP )
840 ICV_ACC_ROWS_FUNC( Min, 32f, float, float, MIN, CV_NOP )
841 ICV_ACC_ROWS_FUNC( Min, 64f, double, double, MIN, CV_NOP )
843 ICV_ACC_COLS_FUNC_C1( Sum, 8u32s, uchar, int, int, CV_ADD )
844 ICV_ACC_COLS_FUNC_C1( Sum, 8u32f, uchar, int, float, CV_ADD )
845 ICV_ACC_COLS_FUNC_C1( Sum, 16u32f, ushort, float, float, CV_ADD )
846 ICV_ACC_COLS_FUNC_C1( Sum, 16u64f, ushort, double, double, CV_ADD )
847 ICV_ACC_COLS_FUNC_C1( Sum, 16s32f, short, float, float, CV_ADD )
848 ICV_ACC_COLS_FUNC_C1( Sum, 16s64f, short, double, double, CV_ADD )
850 ICV_ACC_COLS_FUNC_C1( Sum, 32f, float, float, float, CV_ADD )
851 ICV_ACC_COLS_FUNC_C1( Sum, 32f64f, float, double, double, CV_ADD )
852 ICV_ACC_COLS_FUNC_C1( Sum, 64f, double, double, double, CV_ADD )
853 ICV_ACC_COLS_FUNC_C3( Sum, 8u32s, uchar, int, int, CV_ADD )
854 ICV_ACC_COLS_FUNC_C3( Sum, 8u32f, uchar, int, float, CV_ADD )
855 ICV_ACC_COLS_FUNC_C3( Sum, 32f, float, float, float, CV_ADD )
856 ICV_ACC_COLS_FUNC_C3( Sum, 64f, double, double, double, CV_ADD )
857 ICV_ACC_COLS_FUNC_C4( Sum, 8u32s, uchar, int, int, CV_ADD )
858 ICV_ACC_COLS_FUNC_C4( Sum, 8u32f, uchar, int, float, CV_ADD )
859 ICV_ACC_COLS_FUNC_C4( Sum, 32f, float, float, float, CV_ADD )
860 ICV_ACC_COLS_FUNC_C4( Sum, 64f, double, double, double, CV_ADD )
862 ICV_ACC_COLS_FUNC_C1( Max, 8u, uchar, int, uchar, CV_MAX_8U )
863 ICV_ACC_COLS_FUNC_C1( Max, 32f, float, float, float, MAX )
864 ICV_ACC_COLS_FUNC_C1( Max, 64f, double, double, double, MAX )
866 ICV_ACC_COLS_FUNC_C1( Min, 8u, uchar, int, uchar, CV_MIN_8U )
867 ICV_ACC_COLS_FUNC_C1( Min, 32f, float, float, float, MIN )
868 ICV_ACC_COLS_FUNC_C1( Min, 64f, double, double, double, MIN )
870 typedef CvStatus (CV_STDCALL * CvReduceToRowFunc)
871 ( const void* src, int srcstep, void* dst, CvSize size );
873 typedef CvStatus (CV_STDCALL * CvReduceToColFunc)
874 ( const void* src, int srcstep, void* dst, int dststep, CvSize size );
878 cvReduce( const CvArr* srcarr, CvArr* dstarr, int dim, int op )
882 CV_FUNCNAME( "cvReduce" );
886 CvMat sstub, *src = (CvMat*)srcarr;
887 CvMat dstub, *dst = (CvMat*)dstarr, *dst0;
888 int sdepth, ddepth, cn, op0 = op;
891 if( !CV_IS_MAT(src) )
892 CV_CALL( src = cvGetMat( src, &sstub ));
894 if( !CV_IS_MAT(dst) )
895 CV_CALL( dst = cvGetMat( dst, &dstub ));
897 if( !CV_ARE_CNS_EQ(src, dst) )
898 CV_ERROR( CV_StsUnmatchedFormats, "Input and output arrays must have the same number of channels" );
900 sdepth = CV_MAT_DEPTH(src->type);
901 ddepth = CV_MAT_DEPTH(dst->type);
902 cn = CV_MAT_CN(src->type);
905 size = cvGetMatSize(src);
908 dim = src->rows > dst->rows ? 0 : src->cols > dst->cols ? 1 : dst->cols == 1;
911 CV_ERROR( CV_StsOutOfRange, "The reduced dimensionality index is out of range" );
913 if( dim == 0 && (dst->cols != src->cols || dst->rows != 1) ||
914 dim == 1 && (dst->rows != src->rows || dst->cols != 1) )
915 CV_ERROR( CV_StsBadSize, "The output array size is incorrect" );
917 if( op == CV_REDUCE_AVG )
919 int ttype = sdepth == CV_8U ? CV_MAKETYPE(CV_32S,cn) : dst->type;
920 if( ttype != dst->type )
921 CV_CALL( dst = temp = cvCreateMat( dst->rows, dst->cols, ttype ));
923 ddepth = CV_MAT_DEPTH(ttype);
926 if( op != CV_REDUCE_SUM && op != CV_REDUCE_MAX && op != CV_REDUCE_MIN )
927 CV_ERROR( CV_StsBadArg, "Unknown reduce operation index, must be one of CV_REDUCE_*" );
931 CvReduceToRowFunc rfunc =
932 op == CV_REDUCE_SUM ?
933 (sdepth == CV_8U && ddepth == CV_32S ? (CvReduceToRowFunc)icvSumRows_8u32s_C1R :
934 sdepth == CV_8U && ddepth == CV_32F ? (CvReduceToRowFunc)icvSumRows_8u32f_C1R :
935 sdepth == CV_16U && ddepth == CV_32F ? (CvReduceToRowFunc)icvSumRows_16u32f_C1R :
936 sdepth == CV_16U && ddepth == CV_64F ? (CvReduceToRowFunc)icvSumRows_16u64f_C1R :
937 sdepth == CV_16S && ddepth == CV_32F ? (CvReduceToRowFunc)icvSumRows_16s32f_C1R :
938 sdepth == CV_16S && ddepth == CV_64F ? (CvReduceToRowFunc)icvSumRows_16s64f_C1R :
939 sdepth == CV_32F && ddepth == CV_32F ? (CvReduceToRowFunc)icvSumRows_32f_C1R :
940 sdepth == CV_32F && ddepth == CV_64F ? (CvReduceToRowFunc)icvSumRows_32f64f_C1R :
941 sdepth == CV_64F && ddepth == CV_64F ? (CvReduceToRowFunc)icvSumRows_64f_C1R : 0) :
942 op == CV_REDUCE_MAX ?
943 (sdepth == CV_8U && ddepth == CV_8U ? (CvReduceToRowFunc)icvMaxRows_8u_C1R :
944 sdepth == CV_32F && ddepth == CV_32F ? (CvReduceToRowFunc)icvMaxRows_32f_C1R :
945 sdepth == CV_64F && ddepth == CV_64F ? (CvReduceToRowFunc)icvMaxRows_64f_C1R : 0) :
947 (sdepth == CV_8U && ddepth == CV_8U ? (CvReduceToRowFunc)icvMinRows_8u_C1R :
948 sdepth == CV_32F && ddepth == CV_32F ? (CvReduceToRowFunc)icvMinRows_32f_C1R :
949 sdepth == CV_64F && ddepth == CV_64F ? (CvReduceToRowFunc)icvMinRows_64f_C1R : 0);
952 CV_ERROR( CV_StsUnsupportedFormat,
953 "Unsupported combination of input and output array formats" );
956 IPPI_CALL( rfunc( src->data.ptr, src->step ? src->step : CV_STUB_STEP,
957 dst->data.ptr, size ));
961 CvReduceToColFunc cfunc =
962 op == CV_REDUCE_SUM ?
963 (sdepth == CV_8U && ddepth == CV_32S ?
964 (CvReduceToColFunc)(cn == 1 ? icvSumCols_8u32s_C1R :
965 cn == 3 ? icvSumCols_8u32s_C3R :
966 cn == 4 ? icvSumCols_8u32s_C4R : 0) :
967 sdepth == CV_8U && ddepth == CV_32F ?
968 (CvReduceToColFunc)(cn == 1 ? icvSumCols_8u32f_C1R :
969 cn == 3 ? icvSumCols_8u32f_C3R :
970 cn == 4 ? icvSumCols_8u32f_C4R : 0) :
971 sdepth == CV_16U && ddepth == CV_32F ?
972 (CvReduceToColFunc)(cn == 1 ? icvSumCols_16u32f_C1R : 0) :
973 sdepth == CV_16U && ddepth == CV_64F ?
974 (CvReduceToColFunc)(cn == 1 ? icvSumCols_16u64f_C1R : 0) :
975 sdepth == CV_16S && ddepth == CV_32F ?
976 (CvReduceToColFunc)(cn == 1 ? icvSumCols_16s32f_C1R : 0) :
977 sdepth == CV_16S && ddepth == CV_64F ?
978 (CvReduceToColFunc)(cn == 1 ? icvSumCols_16s64f_C1R : 0) :
979 sdepth == CV_32F && ddepth == CV_32F ?
980 (CvReduceToColFunc)(cn == 1 ? icvSumCols_32f_C1R :
981 cn == 3 ? icvSumCols_32f_C3R :
982 cn == 4 ? icvSumCols_32f_C4R : 0) :
983 sdepth == CV_32F && ddepth == CV_64F ?
984 (CvReduceToColFunc)(cn == 1 ? icvSumCols_32f64f_C1R : 0) :
985 sdepth == CV_64F && ddepth == CV_64F ?
986 (CvReduceToColFunc)(cn == 1 ? icvSumCols_64f_C1R :
987 cn == 3 ? icvSumCols_64f_C3R :
988 cn == 4 ? icvSumCols_64f_C4R : 0) : 0) :
989 op == CV_REDUCE_MAX && cn == 1 ?
990 (sdepth == CV_8U && ddepth == CV_8U ? (CvReduceToColFunc)icvMaxCols_8u_C1R :
991 sdepth == CV_32F && ddepth == CV_32F ? (CvReduceToColFunc)icvMaxCols_32f_C1R :
992 sdepth == CV_64F && ddepth == CV_64F ? (CvReduceToColFunc)icvMaxCols_64f_C1R : 0) :
993 op == CV_REDUCE_MIN && cn == 1 ?
994 (sdepth == CV_8U && ddepth == CV_8U ? (CvReduceToColFunc)icvMinCols_8u_C1R :
995 sdepth == CV_32F && ddepth == CV_32F ? (CvReduceToColFunc)icvMinCols_32f_C1R :
996 sdepth == CV_64F && ddepth == CV_64F ? (CvReduceToColFunc)icvMinCols_64f_C1R : 0) : 0;
999 CV_ERROR( CV_StsUnsupportedFormat,
1000 "Unsupported combination of input and output array formats" );
1002 IPPI_CALL( cfunc( src->data.ptr, src->step ? src->step : CV_STUB_STEP,
1003 dst->data.ptr, dst->step ? dst->step : CV_STUB_STEP, size ));
1006 if( op0 == CV_REDUCE_AVG )
1007 cvScale( dst, dst0, 1./(dim == 0 ? src->rows : src->cols) );
1012 cvReleaseMat( &temp );