cvcmp.cpp.svn-base

来自「非结构化路识别」· SVN-BASE 代码 · 共 1,099 行 · 第 1/3 页

SVN-BASE
1,099
字号

    if( CV_IS_MAT_CONT( src1->type & dst->type ))
    {
        size.width *= size.height;
        src1_step = dst_step = CV_STUB_STEP;
        size.height = 1;
    }
    else
    {
        src1_step = src1->step;
        dst_step = dst->step;
    }

    func = (CvFunc2D_2A1P)(cmps_tab[cmp_op].fn_2d[type]);

    if( !func )
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    IPPI_CALL( func( src1->data.ptr, src1_step, dst->data.ptr,
                     dst_step, size, &buf ));

    if( invflag )
        IPPI_CALL( icvNot_8u_C1R( dst->data.ptr, dst_step,
                           dst->data.ptr, dst_step, size ));

    __END__;
}


/****************************************************************************************\
*                                       Min/Max                                          *
\****************************************************************************************/


#define ICV_DEF_MINMAX_CASE_C1( __op__, arrtype, worktype, _toggle_macro_ )\
for( x = 0; x <= size.width - 4; x += 4 )                               \
{                                                                       \
    worktype a0 = _toggle_macro_(src1[x]), b0 = _toggle_macro_(src2[x]);\
    worktype a1 = _toggle_macro_(src1[x+1]), b1 = _toggle_macro_(src2[x+1]);\
    a0 = __op__( a0, b0 );                                              \
    a1 = __op__( a1, b1 );                                              \
    dst[x] = (arrtype)_toggle_macro_(a0);                               \
    dst[x+1] = (arrtype)_toggle_macro_(a1);                             \
    a0 = _toggle_macro_(src1[x+2]), b0 = _toggle_macro_(src2[x+2]);     \
    a1 = _toggle_macro_(src1[x+3]), b1 = _toggle_macro_(src2[x+3]);     \
    a0 = __op__( a0, b0 );                                              \
    a1 = __op__( a1, b1 );                                              \
    a0 = _toggle_macro_(a0);                                            \
    a1 = _toggle_macro_(a1);                                            \
    dst[x+2] = (arrtype)_toggle_macro_(a0);                             \
    dst[x+3] = (arrtype)_toggle_macro_(a1);                             \
}                                                                       \
                                                                        \
for( ; x < size.width; x++ )                                            \
{                                                                       \
    worktype a0 = _toggle_macro_(src1[x]), b0 = _toggle_macro_(src2[x]);\
    a0 = __op__( a0, b0 );                                              \
    dst[x] = (arrtype)_toggle_macro_(a0);                               \
}


#define ICV_DEF_MINMAX_FUNC( __op__, name, flavor, arrtype,             \
                             worktype, _toggle_macro_ )                 \
IPCVAPI( CvStatus,                                                      \
icv##name##_##flavor##_C1R,( const arrtype* src1, int step1,            \
                             const arrtype* src2, int step2,            \
                             arrtype* dst, int step, CvSize size ));    \
IPCVAPI_IMPL( CvStatus,                                                 \
icv##name##_##flavor##_C1R,( const arrtype* src1, int step1,            \
                             const arrtype* src2, int step2,            \
                             arrtype* dst, int step, CvSize size ))     \
{                                                                       \
    for( ; size.height--; (char*&)src1 += step1, (char*&)src2 += step2, \
                          (char*&)dst += step )                         \
    {                                                                   \
        int x;                                                          \
        ICV_DEF_MINMAX_CASE_C1( __op__, arrtype,                        \
                                worktype, _toggle_macro_ )              \
    }                                                                   \
                                                                        \
    return CV_OK;                                                       \
}


#define ICV_DEF_MINMAX_CONST_CASE_C1( __op__, arrtype, worktype, _toggle_macro_ )\
for( x = 0; x <= size.width - 4; x += 4 )                       \
{                                                               \
    worktype a0 = _toggle_macro_(src1[x]);                      \
    worktype a1 = _toggle_macro_(src1[x+1]);                    \
    a0 = __op__( a0, scalar );                                  \
    a1 = __op__( a1, scalar );                                  \
    dst[x] = (arrtype)_toggle_macro_(a0);                       \
    dst[x+1] = (arrtype)_toggle_macro_(a1);                     \
    a0 = _toggle_macro_(src1[x+2]);                             \
    a1 = _toggle_macro_(src1[x+3]);                             \
    a0 = __op__( a0, scalar );                                  \
    a1 = __op__( a1, scalar );                                  \
    dst[x+2] = (arrtype)_toggle_macro_(a0);                     \
    dst[x+3] = (arrtype)_toggle_macro_(a1);                     \
}                                                               \
                                                                \
for( ; x < size.width; x++ )                                    \
{                                                               \
    worktype a0 = _toggle_macro_(src1[x]);                      \
    a0 = __op__( a0, scalar );                                  \
    dst[x] = (arrtype)_toggle_macro_(a0);                       \
}


#define ICV_DEF_MINMAX_CONST_FUNC( __op__, name, flavor, arrtype,       \
                                   worktype, _toggle_macro_)            \
IPCVAPI( CvStatus,                                                      \
icv##name##C_##flavor##_C1R,( const arrtype* src1, int step1,           \
                             arrtype* dst, int step,                    \
                             CvSize size, worktype* pScalar ));         \
IPCVAPI_IMPL( CvStatus,                                                 \
icv##name##C_##flavor##_C1R,( const arrtype* src1, int step1,           \
                             arrtype* dst, int step,                    \
                             CvSize size, worktype* pScalar ))          \
{                                                                       \
    worktype scalar = _toggle_macro_(*pScalar);                         \
    for( ; size.height--; (char*&)src1 += step1, (char*&)dst += step )  \
    {                                                                   \
        int x;                                                          \
        ICV_DEF_MINMAX_CONST_CASE_C1( __op__, arrtype,                  \
                                      worktype, _toggle_macro_ )        \
    }                                                                   \
                                                                        \
    return CV_OK;                                                       \
}


#define ICV_DEF_MINMAX_ALL( flavor, arrtype, worktype,                             \
                            _toggle_macro_, _min_op_, _max_op_ )                   \
ICV_DEF_MINMAX_FUNC( _min_op_, Min, flavor, arrtype, worktype, _toggle_macro_ )    \
ICV_DEF_MINMAX_FUNC( _max_op_, Max, flavor, arrtype, worktype, _toggle_macro_ )    \
ICV_DEF_MINMAX_CONST_FUNC(_min_op_, Min, flavor, arrtype, worktype, _toggle_macro_)\
ICV_DEF_MINMAX_CONST_FUNC(_max_op_, Max, flavor, arrtype, worktype, _toggle_macro_)


ICV_DEF_MINMAX_ALL( 8u, uchar, int, CV_NOP, CV_IMIN, CV_IMAX )
ICV_DEF_MINMAX_ALL( 16s, short, int, CV_NOP, CV_IMIN, CV_IMAX )
ICV_DEF_MINMAX_ALL( 32s, int, int, CV_NOP, CV_IMIN, CV_IMAX )
ICV_DEF_MINMAX_ALL( 32f, int, int, CV_TOGGLE_FLT, CV_IMIN, CV_IMAX )
ICV_DEF_MINMAX_ALL( 64f, double, double, CV_NOP, CV_MIN, CV_MAX )

#define icvMin_8s_C1R     0
#define icvMax_8s_C1R     0
#define icvMinC_8s_C1R    0
#define icvMaxC_8s_C1R    0

CV_DEF_INIT_FUNC_TAB_2D( Min, C1R )
CV_DEF_INIT_FUNC_TAB_2D( Max, C1R )
CV_DEF_INIT_FUNC_TAB_2D( MinC, C1R )
CV_DEF_INIT_FUNC_TAB_2D( MaxC, C1R )

/*********************************** cvMin & cvMax **************************************/

static void
icvMinMax( const void* srcarr1, const void* srcarr2,
           void* dstarr, int is_max )
{
    static CvFuncTable minmax_tab[2];
    static int inittab = 0;

    CV_FUNCNAME( "icvMinMax" );

    __BEGIN__;

    int type, coi = 0;
    int src1_step, src2_step, dst_step;
    CvMat srcstub1, *src1 = (CvMat*)srcarr1;
    CvMat srcstub2, *src2 = (CvMat*)srcarr2;
    CvMat dststub,  *dst = (CvMat*)dstarr;
    CvSize size;
    CvFunc2D_3A func;

    if( !inittab )
    {
        icvInitMinC1RTable( &minmax_tab[0] );
        icvInitMaxC1RTable( &minmax_tab[1] );
        inittab = 1;
    }

    if( !CV_IS_MAT(src1) )
    {
        CV_CALL( src1 = cvGetMat( src1, &srcstub1, &coi ));
        if( coi != 0 )
            CV_ERROR( CV_BadCOI, "" );
    }

    if( !CV_IS_MAT(src2) )
    {
        CV_CALL( src2 = cvGetMat( src2, &srcstub2, &coi ));
        if( coi != 0 )
            CV_ERROR( CV_BadCOI, "" );
    }

    if( !CV_IS_MAT(dst) )
    {
        CV_CALL( dst = cvGetMat( dst, &dststub, &coi ));
        if( coi != 0 )
            CV_ERROR( CV_BadCOI, "" );
    }

    if( !CV_ARE_TYPES_EQ( src1, src2 ) ||
        !CV_ARE_TYPES_EQ( src1, dst ))
        CV_ERROR_FROM_CODE( CV_StsUnmatchedFormats );

    if( CV_MAT_CN( src1->type ) != 1 )
        CV_ERROR( CV_StsUnsupportedFormat, "Input arrays must be single-channel");

    if( !CV_ARE_SIZES_EQ( src1, src2 ) ||
        !CV_ARE_SIZES_EQ( src1, dst ))
        CV_ERROR_FROM_CODE( CV_StsUnmatchedSizes );

    type = CV_MAT_TYPE(src1->type);
    size = icvGetMatSize( src1 );

    if( CV_IS_MAT_CONT( src1->type & src2->type & dst->type ))
    {
        size.width *= size.height;
        src1_step = src2_step = dst_step = CV_STUB_STEP;
        size.height = 1;
    }
    else
    {
        src1_step = src1->step;
        src2_step = src2->step;
        dst_step = dst->step;
    }

    func = (CvFunc2D_3A)(minmax_tab[is_max != 0].fn_2d[type]);

    if( !func )
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    IPPI_CALL( func( src1->data.ptr, src1_step, src2->data.ptr, src2_step,
                     dst->data.ptr, dst_step, size ));

    __END__;
}


CV_IMPL void
cvMin( const void* srcarr1, const void* srcarr2, void* dstarr )
{
    icvMinMax( srcarr1, srcarr2, dstarr, 0 );
}


CV_IMPL void
cvMax( const void* srcarr1, const void* srcarr2, void* dstarr )
{
    icvMinMax( srcarr1, srcarr2, dstarr, 1 );
}


/********************************* cvMinS / cvMaxS **************************************/

static void
icvMinMaxS( const void* srcarr, double value, void* dstarr, int is_max )
{
    static CvFuncTable minmaxs_tab[2];
    static int inittab = 0;

    CV_FUNCNAME( "icvMinMaxS" );

    __BEGIN__;

    int type, coi = 0;
    int src1_step, dst_step;
    CvMat srcstub1, *src1 = (CvMat*)srcarr;
    CvMat dststub,  *dst = (CvMat*)dstarr;
    CvSize size;
    CvFunc2D_2A1P func;
    union
    {
        int i;
        float f;
        double d;
    }
    buf;

    if( !inittab )
    {
        icvInitMinCC1RTable( &minmaxs_tab[0] );
        icvInitMaxCC1RTable( &minmaxs_tab[1] );
        inittab = 1;
    }

    if( !CV_IS_MAT(src1) )
    {
        CV_CALL( src1 = cvGetMat( src1, &srcstub1, &coi ));
        if( coi != 0 )
            CV_ERROR( CV_BadCOI, "" );
    }

    if( !CV_IS_MAT(dst) )
    {
        CV_CALL( dst = cvGetMat( dst, &dststub, &coi ));
        if( coi != 0 )
            CV_ERROR( CV_BadCOI, "" );
    }

    if( !CV_ARE_TYPES_EQ( src1, dst ))
        CV_ERROR_FROM_CODE( CV_StsUnmatchedFormats );

    if( CV_MAT_CN( src1->type ) != 1 )
        CV_ERROR( CV_StsUnsupportedFormat, "Input array must be single-channel");

    if( !CV_ARE_SIZES_EQ( src1, dst ))
        CV_ERROR_FROM_CODE( CV_StsUnmatchedSizes );

    type = CV_MAT_TYPE(src1->type);
    
    if( CV_MAT_DEPTH(type) <= CV_32S )
        buf.i = cvRound(value);
    else if( CV_MAT_DEPTH(type) == CV_32F )
        buf.f = (float)value;
    else
        buf.d = value;

    size = icvGetMatSize( src1 );

    if( CV_IS_MAT_CONT( src1->type & dst->type ))
    {
        size.width *= size.height;
        src1_step = dst_step = CV_STUB_STEP;
        size.height = 1;
    }
    else
    {
        src1_step = src1->step;
        dst_step = dst->step;
    }

    func = (CvFunc2D_2A1P)(minmaxs_tab[is_max].fn_2d[type]);

    if( !func )
        CV_ERROR( CV_StsUnsupportedFormat, "" );

    IPPI_CALL( func( src1->data.ptr, src1_step, dst->data.ptr,
                     dst_step, size, &buf ));

    __END__;
}


CV_IMPL void
cvMinS( const void* srcarr, double value, void* dstarr )
{
    icvMinMaxS( srcarr, value, dstarr, 0 );
}


CV_IMPL void
cvMaxS( const void* srcarr, double value, void* dstarr )
{
    icvMinMaxS( srcarr, value, dstarr, 1 );
}


/* End of file. */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?