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 // temporarily remove it from build
45 #if 0 && ((_MSC_VER>=1200) || defined __BORLANDC__)
47 double CvMAT::get( const uchar* ptr, int type, int coi )
50 assert( (unsigned)coi < (unsigned)CV_MAT_CN(type) );
52 switch( CV_MAT_DEPTH(type) )
55 t = ((uchar*)ptr)[coi];
58 t = ((char*)ptr)[coi];
61 t = ((short*)ptr)[coi];
67 t = ((float*)ptr)[coi];
70 t = ((double*)ptr)[coi];
77 void CvMAT::set( uchar* ptr, int type, int coi, double d )
80 assert( (unsigned)coi < (unsigned)CV_MAT_CN(type) );
82 switch( CV_MAT_DEPTH(type))
86 ((uchar*)ptr)[coi] = CV_CAST_8U(i);
90 ((char*)ptr)[coi] = CV_CAST_8S(i);
94 ((short*)ptr)[coi] = CV_CAST_16S(i);
98 ((int*)ptr)[coi] = CV_CAST_32S(i);
101 ((float*)ptr)[coi] = (float)d;
104 ((double*)ptr)[coi] = d;
110 void CvMAT::set( uchar* ptr, int type, int coi, int i )
112 assert( (unsigned)coi < (unsigned)CV_MAT_CN(type) );
114 switch( CV_MAT_DEPTH(type))
117 ((uchar*)ptr)[coi] = CV_CAST_8U(i);
120 ((char*)ptr)[coi] = CV_CAST_8S(i);
123 ((short*)ptr)[coi] = CV_CAST_16S(i);
126 ((int*)ptr)[coi] = i;
129 ((float*)ptr)[coi] = (float)i;
132 ((double*)ptr)[coi] = (double)i;
138 void CvMAT::set( uchar* ptr, int type, double d )
140 int i, cn = CV_MAT_CN(type);
142 switch( CV_MAT_DEPTH(type))
146 ((uchar*)ptr)[0] = CV_CAST_8U(i);
148 while( --i ) ((uchar*)ptr)[i] = 0;
152 ((char*)ptr)[0] = CV_CAST_8S(i);
154 while( --i ) ((char*)ptr)[i] = 0;
158 ((short*)ptr)[0] = CV_CAST_16S(i);
160 while( --i ) ((short*)ptr)[i] = 0;
166 while( --i ) ((int*)ptr)[i] = 0;
169 ((float*)ptr)[0] = (float)d;
171 while( --i ) ((float*)ptr)[i] = 0;
174 ((double*)ptr)[0] = d;
176 while( --i ) ((double*)ptr)[i] = 0;
182 void CvMAT::set( uchar* ptr, int type, int i )
184 int cn = CV_MAT_CN(type);
186 switch( CV_MAT_DEPTH(type))
189 ((uchar*)ptr)[0] = CV_CAST_8U(i);
191 while( --i ) ((uchar*)ptr)[i] = 0;
194 ((char*)ptr)[0] = CV_CAST_8S(i);
196 while( --i ) ((char*)ptr)[i] = 0;
199 ((short*)ptr)[0] = CV_CAST_16S(i);
201 while( --i ) ((short*)ptr)[i] = 0;
206 while( --i ) ((int*)ptr)[i] = 0;
209 ((float*)ptr)[0] = (float)i;
211 while( --i ) ((float*)ptr)[i] = 0;
214 ((double*)ptr)[0] = (double)i;
216 while( --i ) ((double*)ptr)[i] = 0;
222 CvMAT::CvMAT( const _CvMAT_T_& mat_t )
231 CvMAT::CvMAT( const _CvMAT_ADD_& mat_add )
240 CvMAT::CvMAT( const _CvMAT_ADD_EX_& mat_add )
249 CvMAT::CvMAT( const _CvMAT_SCALE_& scale_mat )
258 CvMAT::CvMAT( const _CvMAT_SCALE_SHIFT_& scale_shift_mat )
263 *this = scale_shift_mat;
267 CvMAT::CvMAT( const _CvMAT_MUL_& mmul )
276 CvMAT::CvMAT( const _CvMAT_MUL_ADD_& mmuladd )
285 CvMAT::CvMAT( const _CvMAT_INV_& inv_mat )
294 CvMAT::CvMAT( const _CvMAT_NOT_& not_mat )
303 CvMAT::CvMAT( const _CvMAT_UN_LOGIC_& mat_logic )
312 CvMAT::CvMAT( const _CvMAT_LOGIC_& mat_logic )
321 CvMAT::CvMAT( const _CvMAT_COPY_& mat_copy )
323 CvMAT* src = (CvMAT*)mat_copy.a;
324 create( src->height, src->width, src->type );
329 CvMAT::CvMAT( const _CvMAT_CVT_& mat_cvt )
338 CvMAT::CvMAT( const _CvMAT_DOT_OP_& dot_op )
347 CvMAT::CvMAT( const _CvMAT_SOLVE_& solve_mat )
356 CvMAT::CvMAT( const _CvMAT_CMP_& cmp_mat )
365 /****************************************************************************************\
366 * CvMAT::operator = *
367 \****************************************************************************************/
369 CvMAT& CvMAT::operator = ( const _CvMAT_T_& mat_t )
371 CvMAT* src = (CvMAT*)&mat_t.a;
374 create( src->width, src->height, src->type );
377 cvTranspose( src, this );
382 CvMAT& CvMAT::operator = ( const _CvMAT_ADD_& mat_add )
384 CvMAT* a = mat_add.a;
385 CvMAT* b = mat_add.b;
389 create( a->height, a->width, a->type );
392 if( mat_add.beta == 1 )
398 if( mat_add.beta == -1 )
404 if( CV_MAT_DEPTH(a->type) >= CV_32F && CV_MAT_CN(a->type) <= 2 )
405 cvScaleAdd( b, cvScalar(mat_add.beta), a, this );
407 cvAddWeighted( a, 1, b, mat_add.beta, 0, this );
412 CvMAT& CvMAT::operator = ( const _CvMAT_ADD_EX_& mat_add )
414 CvMAT* a = mat_add.a;
415 CvMAT* b = mat_add.b;
419 create( a->height, a->width, a->type );
422 cvAddWeighted( a, mat_add.alpha, b, mat_add.beta, mat_add.gamma, this );
427 CvMAT& CvMAT::operator = ( const _CvMAT_SCALE_& scale_mat )
429 CvMAT* src = scale_mat.a;
433 create( src->height, src->width, src->type );
436 cvConvertScale( src, this, scale_mat.alpha, 0 );
441 CvMAT& CvMAT::operator = ( const _CvMAT_SCALE_SHIFT_& scale_shift_mat )
443 CvMAT* src = scale_shift_mat.a;
447 create( src->height, src->width, src->type );
450 cvConvertScale( src, this, scale_shift_mat.alpha, scale_shift_mat.beta );
455 CvMAT& CvMAT::operator = ( const _CvMAT_MUL_& mmul )
459 int t_a = mmul.t_ab & 1;
460 int t_b = (mmul.t_ab & 2) != 0;
461 int m = (&(a->rows))[t_a];
462 int n = (&(b->rows))[t_b ^ 1];
463 /* this(m x n) = (a^o1(t))(m x l) * (b^o2(t))(l x n) */
467 create( m, n, a->type );
470 if( mmul.alpha == 1 )
474 cvMatMulAdd( a, b, 0, this );
478 if( a->data.ptr == b->data.ptr && mmul.t_ab < 3 &&
479 a->rows == b->rows && a->cols == b->cols &&
480 a->data.ptr != data.ptr )
482 cvMulTransposed( a, this, mmul.t_ab & 1 );
487 cvGEMM( a, b, mmul.alpha, 0, 0, this, mmul.t_ab );
492 CvMAT& CvMAT::operator = ( const _CvMAT_MUL_ADD_& mmuladd )
494 CvMAT* a = mmuladd.a;
495 CvMAT* b = mmuladd.b;
496 CvMAT* c = mmuladd.c;
497 int t_a = mmuladd.t_abc & 1;
498 int t_b = (mmuladd.t_abc & 2) != 0;
499 int m = (&(a->rows))[t_a];
500 int n = (&(b->rows))[t_b ^ 1];
501 /* this(m x n) = (a^o1(t))(m x l) * (b^o2(t))(l x n) */
505 create( m, n, a->type );
508 if( mmuladd.t_abc == 0 && mmuladd.alpha == 1 && mmuladd.beta == 1 )
509 cvMatMulAdd( a, b, c, this );
511 cvGEMM( a, b, mmuladd.alpha, c, mmuladd.beta, this, mmuladd.t_abc );
516 CvMAT& CvMAT::operator = ( const _CvMAT_INV_& inv_mat )
518 CvMAT* src = (CvMAT*)&inv_mat.a;
522 create( src->height, src->width, src->type );
525 if( inv_mat.method == 0 )
526 cvInvert( src, this );
528 cvPseudoInv( src, this );
533 CvMAT& CvMAT::operator = ( const _CvMAT_NOT_& not_mat )
535 CvMAT* src = not_mat.a;
539 create( src->height, src->width, src->type );
547 CvMAT& CvMAT::operator = ( const _CvMAT_LOGIC_& mat_logic )
549 CvMAT* a = mat_logic.a;
550 CvMAT* b = mat_logic.b;
551 int flags = mat_logic.flags;
552 _CvMAT_LOGIC_::Op op = mat_logic.op;
556 create( a->height, a->width, a->type );
561 case _CvMAT_LOGIC_::AND:
565 else if( flags == 3 )
570 else if( flags == 1 )
572 if( data.ptr == b->data.ptr )
575 cvOr( this, a, this );
581 cvAnd( this, b, this );
586 if( data.ptr == a->data.ptr )
589 cvOr( this, b, this );
595 cvAnd( this, a, this );
600 case _CvMAT_LOGIC_::OR:
604 else if( flags == 3 )
609 else if( flags == 1 )
611 if( data.ptr == b->data.ptr )
614 cvAnd( this, a, this );
620 cvOr( this, b, this );
625 if( data.ptr == a->data.ptr )
628 cvAnd( this, b, this );
634 cvOr( this, a, this );
639 case _CvMAT_LOGIC_::XOR:
642 if( flags == 1 || flags == 2 )
651 CvMAT& CvMAT::operator = ( const _CvMAT_UN_LOGIC_& mat_logic )
653 CvMAT* a = mat_logic.a;
654 CvScalar scalar = cvScalarAll( mat_logic.alpha );
655 int flags = mat_logic.flags;
656 _CvMAT_LOGIC_::Op op = mat_logic.op;
660 create( a->height, a->width, a->type );
665 case _CvMAT_LOGIC_::AND:
668 cvAndS( a, scalar, this );
672 cvAndS( this, scalar, this );
676 case _CvMAT_LOGIC_::OR:
679 cvOrS( a, scalar, this );
683 cvOrS( this, scalar, this );
687 case _CvMAT_LOGIC_::XOR:
690 cvXorS( a, scalar, this );
692 cvXorS( a, ~scalar, this );
700 CvMAT& CvMAT::operator = ( const _CvMAT_COPY_& mat_copy )
702 CvMAT* src = (CvMAT*)mat_copy.a;
706 create( src->height, src->width, src->type );
716 CvMAT& CvMAT::operator = ( const _CvMAT_CVT_& mat_cvt )
718 CvMAT* src = (CvMAT*)&mat_cvt.a;
722 int depth = mat_cvt.newdepth;
723 create( src->height, src->width, depth < 0 ? src->type :
724 CV_MAT_CN(src->type)|CV_MAT_DEPTH(depth));
727 cvCvtScale( src, this, mat_cvt.scale, mat_cvt.shift );
732 CvMAT& CvMAT::operator = ( const _CvMAT_DOT_OP_& dot_op )
734 CvMAT* a = (CvMAT*)&(dot_op.a);
739 create( a->height, a->width, a->type );
745 cvMul( a, b, this, dot_op.alpha );
749 cvDiv( a, b, this, dot_op.alpha );
751 cvDiv( 0, a, this, dot_op.alpha );
757 cvMinS( a, dot_op.alpha, this );
763 cvMaxS( a, dot_op.alpha, this );
767 cvAbsDiff( a, b, this );
769 cvAbsDiffS( a, this, cvScalar(dot_op.alpha) );
779 CvMAT& CvMAT::operator = ( const _CvMAT_SOLVE_& solve_mat )
781 CvMAT* a = (CvMAT*)(solve_mat.a);
782 CvMAT* b = (CvMAT*)(solve_mat.b);
786 create( a->height, b->width, a->type );
789 if( solve_mat.method == 0 )
790 cvSolve( a, b, this );
794 cvInitMatHeader( &temp, a->cols, a->rows, a->type );
795 cvCreateData( &temp );
797 cvPseudoInv( a, &temp );
798 cvMatMul( &temp, b, this );
805 CvMAT& CvMAT::operator = ( const _CvMAT_CMP_& mat_cmp )
807 CvMAT* a = mat_cmp.a;
808 CvMAT* b = mat_cmp.b;
812 create( a->height, a->width, CV_8UC1 );
816 cvCmp( a, b, this, mat_cmp.cmp_op );
818 cvCmpS( a, mat_cmp.alpha, this, mat_cmp.cmp_op );
823 /****************************************************************************************\
824 * CvMAT I/O operations *
825 \****************************************************************************************/
827 void CvMAT::write( const char* name, FILE* f, const char* fmt )
829 int i, j, w = width * CV_MAT_CN(type);
830 FILE* out = f ? f : stdout;
833 fprintf( stdout, "%s(%d x %d) =\n\t", name, rows, cols );
835 for( i = 0; i < rows; i++ )
837 switch( CV_MAT_DEPTH(type))
839 case CV_8U: if( !fmt )
841 for( j = 0; j < w; j++ )
842 fprintf( out, fmt, ((uchar*)(data.ptr + i*step))[j] );
844 case CV_8S: if( !fmt )
846 for( j = 0; j < w; j++ )
847 fprintf( out, fmt, ((char*)(data.ptr + i*step))[j] );
849 case CV_16S: if( !fmt )
851 for( j = 0; j < w; j++ )
852 fprintf( out, fmt, ((short*)(data.ptr + i*step))[j] );
854 case CV_32S: if( !fmt )
856 for( j = 0; j < w; j++ )
857 fprintf( out, fmt, ((int*)(data.ptr + i*step))[j] );
859 case CV_32F: if( !fmt )
861 for( j = 0; j < w; j++ )
862 fprintf( out, fmt, ((float*)(data.ptr + i*step))[j] );
864 case CV_64F: if( !fmt )
866 for( j = 0; j < w; j++ )
867 fprintf( out, fmt, ((double*)(data.ptr + i*step))[j] );
870 fprintf( out, "\n%s", i < rows - 1 ? "\t" : "" );
872 fprintf( out, "\n" );
875 #endif /* _MSC_VER || __BORLANDC__ */