cvconvert.cpp.svn-base
来自「非结构化路识别」· SVN-BASE 代码 · 共 1,330 行 · 第 1/5 页
SVN-BASE
1,330 行
size = icvGetMatSize( src );
src_step = src->step;
dst_step = dst->step;
if( CV_IS_MAT_CONT( src->type & dst->type ))
{
size.width *= size.height;
src_step = dst_step = CV_STUB_STEP;
size.height = 1;
}
IPPI_CALL( icvCvtScaleAbsTo_8u_C1R( src->data.ptr, src_step,
(uchar*)(dst->data.ptr), dst_step,
size, scale, shift, CV_MAT_TYPE(src->type)));
__END__;
}
/****************************************************************************************\
* cvLUT_Transform *
\****************************************************************************************/
#define ICV_DEF_LUT_FUNC( flavor, dsttype ) \
static CvStatus CV_STDCALL \
icvLUT_Transform_##flavor##_C1R( const void* srcptr, int srcstep, \
void* dstptr, int dststep, CvSize size, \
const void* lutptr ) \
{ \
const uchar* src = (const uchar*)srcptr; \
dsttype* dst = (dsttype*)dstptr; \
const dsttype* lut = (const dsttype*)lutptr; \
\
for( ; size.height--; src += srcstep, \
(char*&)dst += dststep ) \
{ \
int i; \
\
for( i = 0; i <= size.width - 4; i += 4 ) \
{ \
dsttype t0 = lut[src[i]]; \
dsttype t1 = lut[src[i+1]]; \
\
dst[i] = t0; \
dst[i+1] = t1; \
\
t0 = lut[src[i+2]]; \
t1 = lut[src[i+3]]; \
\
dst[i+2] = t0; \
dst[i+3] = t1; \
} \
\
for( ; i < size.width; i++ ) \
{ \
dsttype t0 = lut[src[i]]; \
dst[i] = t0; \
} \
} \
\
return CV_OK; \
}
ICV_DEF_LUT_FUNC( 8u, uchar )
ICV_DEF_LUT_FUNC( 16s, ushort )
ICV_DEF_LUT_FUNC( 32s, int )
ICV_DEF_LUT_FUNC( 64f, int64 )
#define icvLUT_Transform_8s_C1R icvLUT_Transform_8u_C1R
#define icvLUT_Transform_32f_C1R icvLUT_Transform_32s_C1R
CV_DEF_INIT_FUNC_TAB_2D( LUT_Transform, C1R )
CV_IMPL void
cvLUT( const void* srcarr, void* dstarr, const void* lutarr )
{
static CvFuncTable lut_tab;
static int inittab = 0;
CV_FUNCNAME( "cvLUT" );
__BEGIN__;
int coi1 = 0, coi2 = 0;
CvMat srcstub, *src = (CvMat*)srcarr;
CvMat dststub, *dst = (CvMat*)dstarr;
CvMat lutstub, *lut = (CvMat*)lutarr;
uchar* lut_data;
uchar shuffled_lut[256*sizeof(double)];
CvFunc2D_2A1P func = 0;
CvSize size;
int src_step, dst_step;
if( !inittab )
{
icvInitLUT_TransformC1RTable( &lut_tab );
inittab = 1;
}
CV_CALL( src = cvGetMat( src, &srcstub, &coi1 ));
CV_CALL( dst = cvGetMat( dst, &dststub, &coi2 ));
CV_CALL( lut = cvGetMat( lut, &lutstub ));
if( coi1 != 0 || coi2 != 0 )
CV_ERROR( CV_BadCOI, "" );
if( !CV_ARE_SIZES_EQ( src, dst ))
CV_ERROR( CV_StsUnmatchedSizes, "" );
if( !CV_ARE_CNS_EQ( src, dst ))
CV_ERROR( CV_StsUnmatchedFormats, "" );
if( CV_MAT_DEPTH( src->type ) > CV_8S )
CV_ERROR( CV_StsUnsupportedFormat, "" );
if( !CV_IS_MAT_CONT(lut->type) || CV_MAT_CN(lut->type) != 1 ||
!CV_ARE_DEPTHS_EQ( dst, lut ) || lut->width*lut->height != 256 )
CV_ERROR( CV_StsBadArg, "The LUT must be continuous, single-channel array \n"
"with 256 elements of the same type as destination" );
size = icvGetMatSize( src );
size.width *= CV_MAT_CN( src->type );
src_step = src->step;
dst_step = dst->step;
if( CV_IS_MAT_CONT( src->type & dst->type ))
{
size.width *= size.height;
src_step = dst_step = CV_STUB_STEP;
size.height = 1;
}
lut_data = lut->data.ptr;
if( CV_MAT_DEPTH( src->type ) == CV_8S )
{
int half_size = icvPixSize[CV_MAT_TYPE(lut->type)]*128;
// shuffle lut
memcpy( shuffled_lut, lut_data + half_size, half_size );
memcpy( shuffled_lut + half_size, lut_data, half_size );
lut_data = shuffled_lut;
}
func = (CvFunc2D_2A1P)(lut_tab.fn_2d[CV_MAT_DEPTH(src->type)]);
if( !func )
CV_ERROR( CV_StsUnsupportedFormat, "" );
IPPI_CALL( func( src->data.ptr, src_step, dst->data.ptr,
dst_step, size, lut_data));
__END__;
}
/****************************************************************************************\
* cvConvertScale *
\****************************************************************************************/
#define ICV_DEF_CVT_SCALE_CASE( worktype, cast_macro1, \
scale_macro, cast_macro2, src, a, b ) \
\
{ \
for( ; size.height--; (char*&)(src) += srcstep, \
(char*&)(dst) += dststep ) \
{ \
for( i = 0; i <= size.width - 4; i += 4 ) \
{ \
worktype t0 = scale_macro((a)*cast_macro1((src)[i])+(b)); \
worktype t1 = scale_macro((a)*cast_macro1((src)[i+1])+(b)); \
\
dst[i] = cast_macro2(t0); \
dst[i+1] = cast_macro2(t1); \
\
t0 = scale_macro((a)*cast_macro1((src)[i+2]) + (b)); \
t1 = scale_macro((a)*cast_macro1((src)[i+3]) + (b)); \
\
dst[i+2] = cast_macro2(t0); \
dst[i+3] = cast_macro2(t1); \
} \
\
for( ; i < size.width; i++ ) \
{ \
worktype t0 = scale_macro((a)*cast_macro1((src)[i]) + (b)); \
dst[i] = cast_macro2(t0); \
} \
} \
}
#define ICV_DEF_CVT_SCALE_FUNC_INT( flavor, dsttype, cast_macro ) \
static CvStatus CV_STDCALL \
icvCvtScaleTo_##flavor##_C1R( const char* src, int srcstep, \
dsttype* dst, int dststep, \
CvSize size, double scale, double shift, \
int param ) \
{ \
int i, srctype = param; \
dsttype lut[256]; \
\
switch( CV_MAT_DEPTH(srctype) ) \
{ \
case CV_8U: \
if( size.width*size.height >= 256 ) \
{ \
for( i = 0; i < 256; i++ ) \
{ \
int t = cvRound( i*scale + shift ); \
lut[i] = cast_macro(t); \
} \
\
icvLUT_Transform_##flavor##_C1R( src, srcstep, dst, \
dststep, size, lut ); \
} \
else if( fabs( scale ) <= 128. && \
fabs( shift ) <= (INT_MAX*0.5)/(1 << ICV_FIX_SHIFT)) \
{ \
int iscale = cvRound(scale*(1 << ICV_FIX_SHIFT)); \
int ishift = cvRound(shift*(1 << ICV_FIX_SHIFT)); \
\
ICV_DEF_CVT_SCALE_CASE( int, CV_NOP, ICV_SCALE, cast_macro, \
(uchar*&)src, iscale, ishift ); \
} \
else \
{ \
ICV_DEF_CVT_SCALE_CASE( int, CV_NOP, cvRound, cast_macro, \
(uchar*&)src, scale, shift ); \
} \
break; \
case CV_8S: \
if( size.width*size.height >= 256 ) \
{ \
for( i = 0; i < 256; i++ ) \
{ \
int t = cvRound( (char)i*scale + shift ); \
lut[i] = CV_CAST_8U(t); \
} \
\
icvLUT_Transform_##flavor##_C1R( src, srcstep, dst, \
dststep, size, lut ); \
} \
else if( fabs( scale ) <= 128. && \
fabs( shift ) <= (INT_MAX*0.5)/(1 << ICV_FIX_SHIFT)) \
{ \
int iscale = cvRound(scale*(1 << ICV_FIX_SHIFT)); \
int ishift = cvRound(shift*(1 << ICV_FIX_SHIFT)); \
\
ICV_DEF_CVT_SCALE_CASE( int, CV_NOP, ICV_SCALE, cast_macro, \
(char*&)src, iscale, ishift ); \
} \
else \
{ \
ICV_DEF_CVT_SCALE_CASE( int, CV_NOP, cvRound, cast_macro, \
(char*&)src, scale, shift ); \
} \
break; \
case CV_16S: \
if( fabs( scale ) <= 1. && \
fabs( shift ) <= (INT_MAX*0.5)/(1 << ICV_FIX_SHIFT)) \
{ \
int iscale = cvRound(scale*(1 << ICV_FIX_SHIFT)); \
int ishift = cvRound(shift*(1 << ICV_FIX_SHIFT)); \
\
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?