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.
53 CArray() : data( 0 ), bReleaseData( false ) {}
56 if( bReleaseData && data )
63 // create data array of size _num
64 void create( int _num = 0 )
82 operator T *() { return( data ); }
83 T *getData() { return( data ); }
84 operator T() { return( data[0] ); }
86 T operator=( T val ) { data[0] = val; return( val ); }
88 T &operator[]( int index ) { return( data[index] ); }
94 // prevent from copying
95 CArray( const CArray &arr );
96 CArray &operator=( const CArray &arr );
102 CImage() : header( 0 ), bReleaseData( false ) {}
109 cvReleaseData( header );
110 bReleaseData = false;
112 cvReleaseImageHeader( & header );
117 void createHeader( CvSize size, int depth, int channels )
120 header = cvCreateImageHeader( size, depth, channels );
124 void setData( void *data, int step )
127 assert( !bReleaseData );
128 cvSetData( header, data, step );
131 // Create own data. It will be automatically released
135 assert( !bReleaseData );
136 cvCreateData( header );
139 operator IplImage *() { return( header ); }
145 // prevent from copying
146 CImage( const CImage &arr );
147 CImage &operator=( const CImage &arr );
153 CStorage() { storage = cvCreateMemStorage(); }
154 ~CStorage() { cvReleaseMemStorage( &storage ); }
155 operator CvMemStorage *() { return( storage ); }
157 CvMemStorage *storage;
159 // prevent from copying
160 CStorage( const CStorage &obj );
161 CStorage &operator=( const CStorage &obj );
167 CStructElem() : structElem( 0 ) {}
172 cvReleaseStructuringElement( &structElem );
177 void create( int cols, int rows, int anchorX, int anchorY,
178 CvElementShape shape, int *values = 0 )
180 assert( !structElem );
181 structElem = cvCreateStructuringElementEx( cols, rows,
182 anchorX, anchorY, shape, values );
185 operator IplConvKernel *() { return( structElem ); }
187 IplConvKernel *structElem;
189 // prevent from copying
190 CStructElem( const CStructElem &obj );
191 CStructElem &operator=( const CStructElem &obj );
195 template< class T1, class T2 >
196 void castCopy( T1 *src, T2 *dst, int rowNum, int colNum,
197 int srcRowStep, int srcColStep,
198 int dstRowStep, int dstColStep )
200 for( int r = 0; r < rowNum; r++ )
202 for( int c = 0; c < colNum; c++ )
204 dst[r * dstRowStep + c * dstColStep] =
205 static_cast< T2 > ( src[r * srcRowStep + c * srcColStep] );
210 #define MXTYPE_DATAPTR( type, part1, ptrName, part2 ) \
213 case mxDOUBLE_CLASS: \
216 double *ptrName = (double *)part2; \
219 case mxSINGLE_CLASS: \
222 float *ptrName = (float *)part2; \
225 case mxINT32_CLASS: \
228 signed int *ptrName = (signed int *)part2; \
231 case mxUINT32_CLASS: \
234 unsigned int *ptrName = (unsigned int *)part2; \
237 case mxINT16_CLASS: \
240 signed short *ptrName = (signed short *)part2; \
243 case mxUINT16_CLASS: \
246 unsigned short *ptrName = (unsigned short *)part2; \
252 signed char *ptrName = (signed char *)part2; \
255 case mxUINT8_CLASS: \
258 unsigned char *ptrName = (unsigned char *)part2; \
262 throw_error( "Parameter should be numeric." ); \
266 // Execute callfun. If an error occured, add to the error
267 // #name and rethrow it.
268 #define _ENHANCE_ERROR( callfun, name ) \
275 std::ostringstream ostr; \
276 ostr << _e.get_error_messages() << " Parameter: " << #name; \
277 throw_error( ostr.str() ); \
280 #define _CREATE_MATRIX( n, rows, cols, class ) \
281 plhs[n] = mxCreateNumericMatrix( rows, cols, class, mxREAL );
283 #define _CREATE_VECTOR( name ) \
284 int _m = ( name##_vecType == any || name##_vecType == row ) ? \
286 int _n = ( name##_vecType == any || name##_vecType == row ) ? \
288 _CREATE_MATRIX( name##_n, _m, _n, name##_class );
290 #define _CREATE_IMAGE( name ) \
291 if( name##_channels == 3 ) \
293 const int ndim = 3; \
295 dims[0] = name##_size.width; \
296 dims[1] = name##_size.height; \
297 dims[2] = name##_channels; \
299 mxCreateNumericArray(ndim, dims, name##_class, mxREAL );\
303 _CREATE_MATRIX( name##_n, name##_size.width, name##_size.height, \
307 /* Macro for convert functions of certain type */
308 #define FUNC_CONVERT( type, \
309 getDecl, getParam, getAssign, \
310 putDecl, putAssign, putParam ) \
312 template< class T1 > \
313 void convert( T1 *src, type *dst, int num ) \
319 for( nelem = 0; nelem < num; nelem++ ) \
321 castCopy( src+nelem, getParam, 1, type##NUMFIELDS, 1, num, 1, 1 ); \
325 } /* for each elem in arr */ \
328 template< class T2 > \
329 void convert( type *src, T2 *dst, int num ) \
335 for( nelem = 0; nelem < num; nelem++ ) \
340 castCopy( putParam, dst+nelem, 1, type##NUMFIELDS, 1, 1, 1, num ); \
341 } /* for each elem in arr */ \
345 #define CvPointNUMFIELDS 2
346 FUNC_CONVERT( CvPoint,
352 (int *)(src + nelem) )
355 // Convert from 1-based to 0-based and swap coordinates
356 struct CvPoint0S : public CvPoint {};
357 #define CvPoint0SNUMFIELDS 2
358 FUNC_CONVERT( CvPoint0S,
361 _tmp = dst[nelem].x - 1; dst[nelem].x = dst[nelem].y - 1;
362 dst[nelem].y = _tmp;,
364 point.x = src[nelem].y + 1; point.y = src[nelem].x + 1;,
369 struct CvSizeS : public CvSize {};
370 #define CvSizeSNUMFIELDS 2
371 FUNC_CONVERT( CvSizeS,
374 _tmp = dst[nelem].width; dst[nelem].width = dst[nelem].height;
375 dst[nelem].height = _tmp;,
377 elem.width = src[nelem].height; elem.height = src[nelem].width;,
381 // Convert from 1-based to 0-based
382 struct CvSlice0 : public CvSlice {};
383 #define CvSlice0NUMFIELDS 2
384 FUNC_CONVERT( CvSlice0,
387 dst[nelem].startIndex = ( dst[nelem].startIndex > 0 ) ?
388 dst[nelem].startIndex - 1 : dst[nelem].startIndex;
389 dst[nelem].endIndex = ( dst[nelem].endIndex > 0 ) ?
390 dst[nelem].endIndex - 1 : dst[nelem].endIndex;,
392 slice.startIndex = src[nelem].startIndex +
393 ( src[nelem].startIndex >= 0 ) ? 1 : 0;
394 slice.endIndex = src[nelem].endIndex +
395 ( src[nelem].endIndex >= 0 ) ? 1 : 0;,
399 #define CvPoint2D32fNUMFIELDS 2
400 FUNC_CONVERT( CvPoint2D32f,
402 (float *)(dst+nelem),
406 (float *)(src + nelem) )
408 // type CvPoint2D32fS
410 struct CvPoint2D32fS : public CvPoint2D32f {};
411 #define CvPoint2D32fSNUMFIELDS 2
412 FUNC_CONVERT( CvPoint2D32fS,
414 (float *)(dst+nelem),
415 _tmp = dst[nelem].x; dst[nelem].x = dst[nelem].y;
416 dst[nelem].y = _tmp;,
418 point.x = src[nelem].y; point.y = src[nelem].x;,
422 #define CvRectNUMFIELDS 4
423 FUNC_CONVERT( CvRect,
432 // Convert from 1-based to 0-based and swap coordinates
433 struct CvRect0S : public CvRect {};
434 #define CvRect0SNUMFIELDS 4
435 FUNC_CONVERT( CvRect0S,
438 _tmp = dst[nelem].x - 1; dst[nelem].x = dst[nelem].y - 1;
440 _tmp = dst[nelem].width - 1;
441 dst[nelem].width = dst[nelem].height - 1;
442 dst[nelem].height = _tmp;,
444 elem.x = src[nelem].y + 1; elem.y = src[nelem].x + 1;
445 elem.width = src[nelem].height;
446 elem.height = src[nelem].width;,
449 // type CvTermCriteria
450 #define CvTermCriteriaNUMFIELDS 2
451 FUNC_CONVERT( CvTermCriteria,
455 dst[nelem].maxIter = 0;
456 dst[nelem].epsilon = 0.0f;
459 dst[nelem].type |= CV_TERMCRIT_ITER;
460 dst[nelem].maxIter = static_cast< int > ( elem[0] );
464 dst[nelem].type |= CV_TERMCRIT_EPS;
465 dst[nelem].epsilon = static_cast< float > ( elem[1] );
468 elem[0] = ( ( src[nelem].type & CV_TERMCRIT_ITER ) ?
469 static_cast< double > ( src[nelem].maxIter ) : 0.0 );
470 elem[1] = ( ( src[nelem].type & CV_TERMCRIT_EPS ) ?
471 static_cast< double > ( src[nelem].epsilon ) : 0.0 );,
474 #endif /* _TMACRO_H_ */