📄 cxmisc.h
字号:
}
CV_INLINE size_t cvAlign( size_t size, int align );
CV_INLINE size_t cvAlign( size_t size, int align )
{
assert( (align & (align-1)) == 0 );
return (size + align - 1) & ~(size_t)(align-1);
}
CV_INLINE CvSize cvGetMatSize( const CvMat* mat );
CV_INLINE CvSize cvGetMatSize( const CvMat* mat )
{
CvSize size = { mat->width, mat->height };
return size;
}
#define CV_DESCALE(x,n) (((x) + (1 << ((n)-1))) >> (n))
#define CV_FLT_TO_FIX(x,n) cvRound((x)*(1<<(n)))
/* This is a small engine for performing fast division of multiple numbers
by the same constant. Most compilers do it too if they know the divisor value
at compile-time. The algorithm was taken from Agner Fog's optimization guide
at http://www.agner.org/assem */
typedef struct CvFastDiv
{
unsigned delta, scale, divisor;
}
CvFastDiv;
#define CV_FAST_DIV_SHIFT 32
CV_INLINE CvFastDiv cvFastDiv( int divisor );
CV_INLINE CvFastDiv cvFastDiv( int divisor )
{
CvFastDiv fastdiv;
assert( divisor >= 1 );
uint64 temp = ((uint64)1 << CV_FAST_DIV_SHIFT)/divisor;
fastdiv.divisor = divisor;
fastdiv.delta = (unsigned)(((temp & 1) ^ 1) + divisor - 1);
fastdiv.scale = (unsigned)((temp + 1) >> 1);
return fastdiv;
}
#define CV_FAST_DIV( x, fastdiv ) \
((int)(((int64)((x)*2 + (int)(fastdiv).delta))*(int)(fastdiv).scale>>CV_FAST_DIV_SHIFT))
#define CV_FAST_UDIV( x, fastdiv ) \
((int)(((uint64)((x)*2 + (fastdiv).delta))*(fastdiv).scale>>CV_FAST_DIV_SHIFT))
#define CV_MEMCPY_CHAR( dst, src, len ) \
{ \
size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
char* _icv_memcpy_dst_ = (char*)(dst); \
const char* _icv_memcpy_src_ = (const char*)(src); \
\
for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ ) \
_icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_]; \
}
#define CV_MEMCPY_INT( dst, src, len ) \
{ \
size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
int* _icv_memcpy_dst_ = (int*)(dst); \
const int* _icv_memcpy_src_ = (const int*)(src); \
assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 && \
((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 ); \
\
for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++) \
_icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_]; \
}
#define CV_MEMCPY_AUTO( dst, src, len ) \
{ \
size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
char* _icv_memcpy_dst_ = (char*)(dst); \
const char* _icv_memcpy_src_ = (const char*)(src); \
if( (_icv_memcpy_len_ & (sizeof(int)-1)) == 0 ) \
{ \
assert( ((size_t)_icv_memcpy_src_&(sizeof(int)-1)) == 0 && \
((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 ); \
for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; \
_icv_memcpy_i_+=sizeof(int) ) \
{ \
*(int*)(_icv_memcpy_dst_+_icv_memcpy_i_) = \
*(const int*)(_icv_memcpy_src_+_icv_memcpy_i_); \
} \
} \
else \
{ \
for(_icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++)\
_icv_memcpy_dst_[_icv_memcpy_i_] = _icv_memcpy_src_[_icv_memcpy_i_]; \
} \
}
#define CV_ZERO_CHAR( dst, len ) \
{ \
size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
char* _icv_memcpy_dst_ = (char*)(dst); \
\
for( _icv_memcpy_i_ = 0; _icv_memcpy_i_ < _icv_memcpy_len_; _icv_memcpy_i_++ ) \
_icv_memcpy_dst_[_icv_memcpy_i_] = '\0'; \
}
#define CV_ZERO_INT( dst, len ) \
{ \
size_t _icv_memcpy_i_, _icv_memcpy_len_ = (len); \
int* _icv_memcpy_dst_ = (int*)(dst); \
assert( ((size_t)_icv_memcpy_dst_&(sizeof(int)-1)) == 0 ); \
\
for(_icv_memcpy_i_=0;_icv_memcpy_i_<_icv_memcpy_len_;_icv_memcpy_i_++) \
_icv_memcpy_dst_[_icv_memcpy_i_] = 0; \
}
/****************************************************************************************\
Generic implementation of QuickSort algorithm.
----------------------------------------------
Using this macro user can declare customized sort function that can be much faster
than built-in qsort function because of lower overhead on elements
comparison and exchange. The macro takes "cmp" macro which semantics is the same
as semantics of comparison function passed to qsort function.
Example:
Suppose that the task is to sort points by ascending of y coordinates and if
y's are equal x's should ascend.
The code is:
------------------------------------------------------------------------------
#define cmp_pts( pt1, pt2 ) \
((pt1).y - (pt2).y ? (pt1).y - (pt2).y : (pt1).x - (pt2).x)
[static] CV_IMPLEMENT_QSORT( icvSortPoints, CvPoint, cmp_pts )
------------------------------------------------------------------------------
After that the function "void icvSortPoints( CvPoint* array, size_t total, int aux );"
is available to user.
aux is an additional parameter, which can be used when comparing elements.
The current implementation was derived from *BSD system qsort():
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
\****************************************************************************************/
#define CV_IMPLEMENT_QSORT_EX( func_name, T, LT, user_data_type ) \
void func_name( T *array, size_t total, user_data_type aux ) \
{ \
long isort_thresh = 7; \
T t; \
int sp = 0; \
\
struct \
{ \
T *lb; \
T *ub; \
} \
stack[48]; \
\
aux = aux; \
\
if( total <= 1 ) \
return; \
\
stack[0].lb = array; \
stack[0].ub = array + (total - 1); \
\
while( sp >= 0 ) \
{ \
T* left = stack[sp].lb; \
T* right = stack[sp--].ub; \
\
for(;;) \
{ \
long i, n = right - left + 1, m; \
T* ptr; \
T* ptr2; \
\
if( n <= isort_thresh ) \
{ \
insert_sort: \
for( ptr = left + 1; ptr <= right; ptr++ ) \
{ \
for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--) \
CV_SWAP( ptr2[0], ptr2[-1], t ); \
} \
break; \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -