⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cxrand.cpp

📁 opencv库在TI DM6437上的移植,目前包括两个库cv.lib和cxcore.lib的工程
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            x = bx > 0 ? 0.8857913*(2.506628 - ax) : -0.8857913*(2.506628 - ax);
            
            if( y > v + 0.0506 )
                break;

            if( log(y) < .6931472 - .5*bx*bx )
            {
                x = bx;
                break;
            }

            if( log(1.8857913 - y) < .5718733-.5*x*x )
                break;

            do
            {
                v = ((int)temp)*4.656613e-10;
                x = -log(fabs(v))*.3989423;
                temp = ICV_RNG_NEXT(temp);
                y = -log(((unsigned)temp)*2.328306e-10);
                temp = ICV_RNG_NEXT(temp);
            }
            while( y+y < x*x );

            x = v > 0 ? 2.506628 + x : -2.506628 - x;
            break;
        }

        arr[i] = (float)x;
    }
    *state = temp;
    return CV_OK;
}


#define RAND_BUF_SIZE  96


#define ICV_IMPL_RANDN( flavor, arrtype, worktype, cast_macro1, cast_macro2 )   \
static CvStatus CV_STDCALL                                                      \
icvRandn_##flavor##_C1R( arrtype* arr, int step, CvSize size,                   \
                         uint64* state, const double* param )                   \
{                                                                               \
    float buffer[RAND_BUF_SIZE];                                                \
    step /= sizeof(arr[0]);                                                     \
                                                                                \
    for( ; size.height--; arr += step )                                         \
    {                                                                           \
        int i, j, len = RAND_BUF_SIZE;                                          \
                                                                                \
        for( i = 0; i < size.width; i += RAND_BUF_SIZE )                        \
        {                                                                       \
            int k = 3;                                                          \
            const double* p = param;                                            \
                                                                                \
            if( i + len > size.width )                                          \
                len = size.width - i;                                           \
                                                                                \
            icvRandn_0_1_32f_C1R( buffer, len, state );                         \
                                                                                \
            for( j = 0; j <= len - 4; j += 4 )                                  \
            {                                                                   \
                worktype f0, f1;                                                \
                                                                                \
                f0 = cast_macro1( buffer[j]*p[j+12] + p[j] );                   \
                f1 = cast_macro1( buffer[j+1]*p[j+13] + p[j+1] );               \
                arr[i+j] = cast_macro2(f0);                                     \
                arr[i+j+1] = cast_macro2(f1);                                   \
                                                                                \
                f0 = cast_macro1( buffer[j+2]*p[j+14] + p[j+2] );               \
                f1 = cast_macro1( buffer[j+3]*p[j+15] + p[j+3] );               \
                arr[i+j+2] = cast_macro2(f0);                                   \
                arr[i+j+3] = cast_macro2(f1);                                   \
                                                                                \
                if( --k == 0 )                                                  \
                {                                                               \
                    k = 3;                                                      \
                    p -= 12;                                                    \
                }                                                               \
            }                                                                   \
                                                                                \
            for( ; j < len; j++ )                                               \
            {                                                                   \
                worktype f0 = cast_macro1( buffer[j]*p[j+12] + p[j] );          \
                arr[i+j] = cast_macro2(f0);                                     \
            }                                                                   \
        }                                                                       \
    }                                                                           \
                                                                                \
    return CV_OK;                                                               \
}


ICV_IMPL_RAND_BITS( 8u, uchar, CV_CAST_8U )
ICV_IMPL_RAND_BITS( 16u, ushort, CV_CAST_16U )
ICV_IMPL_RAND_BITS( 16s, short, CV_CAST_16S )
ICV_IMPL_RAND_BITS( 32s, int, CV_CAST_32S )

ICV_IMPL_RAND( 8u, uchar, int, cvFloor, CV_CAST_8U )
ICV_IMPL_RAND( 16u, ushort, int, cvFloor, CV_CAST_16U )
ICV_IMPL_RAND( 16s, short, int, cvFloor, CV_CAST_16S )
ICV_IMPL_RAND( 32s, int, int, cvFloor, CV_CAST_32S )
ICV_IMPL_RAND( 32f, float, float, CV_CAST_32F, CV_NOP )

ICV_IMPL_RANDN( 8u, uchar, int, cvRound, CV_CAST_8U )
ICV_IMPL_RANDN( 16u, ushort, int, cvRound, CV_CAST_16U )
ICV_IMPL_RANDN( 16s, short, int, cvRound, CV_CAST_16S )
ICV_IMPL_RANDN( 32s, int, int, cvRound, CV_CAST_32S )
ICV_IMPL_RANDN( 32f, float, float, CV_CAST_32F, CV_NOP )
ICV_IMPL_RANDN( 64f, double, double, CV_CAST_64F, CV_NOP )

static void icvInitRandTable( CvFuncTable* fastrng_tab,
                              CvFuncTable* rng_tab,
                              CvFuncTable* normal_tab )
{
    fastrng_tab->fn_2d[CV_8U] = (void*)icvRandBits_8u_C1R;
    fastrng_tab->fn_2d[CV_8S] = 0;
    fastrng_tab->fn_2d[CV_16U] = (void*)icvRandBits_16u_C1R;
    fastrng_tab->fn_2d[CV_16S] = (void*)icvRandBits_16s_C1R;
    fastrng_tab->fn_2d[CV_32S] = (void*)icvRandBits_32s_C1R;

    rng_tab->fn_2d[CV_8U] = (void*)icvRand_8u_C1R;
    rng_tab->fn_2d[CV_8S] = 0;
    rng_tab->fn_2d[CV_16U] = (void*)icvRand_16u_C1R;
    rng_tab->fn_2d[CV_16S] = (void*)icvRand_16s_C1R;
    rng_tab->fn_2d[CV_32S] = (void*)icvRand_32s_C1R;
    rng_tab->fn_2d[CV_32F] = (void*)icvRand_32f_C1R;
    rng_tab->fn_2d[CV_64F] = (void*)icvRand_64f_C1R;

    normal_tab->fn_2d[CV_8U] = (void*)icvRandn_8u_C1R;
    normal_tab->fn_2d[CV_8S] = 0;
    normal_tab->fn_2d[CV_16U] = (void*)icvRandn_16u_C1R;
    normal_tab->fn_2d[CV_16S] = (void*)icvRandn_16s_C1R;
    normal_tab->fn_2d[CV_32S] = (void*)icvRandn_32s_C1R;
    normal_tab->fn_2d[CV_32F] = (void*)icvRandn_32f_C1R;
    normal_tab->fn_2d[CV_64F] = (void*)icvRandn_64f_C1R;
}


CV_IMPL void
cvRandArr( CvRNG* rng, CvArr* arr, int disttype, CvScalar param1, CvScalar param2 )
{
    static CvFuncTable rng_tab[2], fastrng_tab;
    static int inittab = 0;

    CV_FUNCNAME( "cvRandArr" );

    __BEGIN__;

    int is_nd = 0;
    CvMat stub, *mat = (CvMat*)arr;
    int type, depth, channels;
    double dparam[2][12];
    int iparam[2][12];
    void* param = dparam;
    int i, fast_int_mode = 0;
    int mat_step = 0;
    CvSize size;
    CvFunc2D_1A2P func = 0;
    CvMatND stub_nd;
    CvNArrayIterator iterator_state, *iterator = 0;

    if( !inittab )
    {
        icvInitRandTable( &fastrng_tab, &rng_tab[CV_RAND_UNI],
                          &rng_tab[CV_RAND_NORMAL] );
        inittab = 1;
    }

    if( !rng )
        CV_ERROR( CV_StsNullPtr, "Null pointer to RNG state" );

    if( CV_IS_MATND(mat) )
    {
        iterator = &iterator_state;
        CV_CALL( cvInitNArrayIterator( 1, &arr, 0, &stub_nd, iterator ));
        type = CV_MAT_TYPE(iterator->hdr[0]->type);
        size = iterator->size;
        is_nd = 1;
    }
    else
    {
        if( !CV_IS_MAT(mat))
        {
            int coi = 0;
            CV_CALL( mat = cvGetMat( mat, &stub, &coi ));

            if( coi != 0 )
                CV_ERROR( CV_BadCOI, "COI is not supported" );
        }

        type = CV_MAT_TYPE( mat->type );
        size = cvGetMatSize( mat );
        mat_step = mat->step;

        if( mat->height > 1 && CV_IS_MAT_CONT( mat->type ))
        {
            size.width *= size.height;
            mat_step = CV_STUB_STEP;
            size.height = 1;
        }
    }

    depth = CV_MAT_DEPTH( type );
    channels = CV_MAT_CN( type );
    size.width *= channels;

    if( disttype == CV_RAND_UNI )
    {
        if( depth <= CV_32S )
        {
            for( i = 0, fast_int_mode = 1; i < channels; i++ )
            {
                int t0 = iparam[0][i] = cvCeil( param1.val[i] );
                int t1 = iparam[1][i] = cvFloor( param2.val[i] ) - t0;
                double diff = param1.val[i] - param2.val[i];

                fast_int_mode &= INT_MIN <= diff && diff <= INT_MAX && (t1 & (t1 - 1)) == 0;
            }
        }

        if( fast_int_mode )
        {
            for( i = 0; i < channels; i++ )
                iparam[1][i]--;
        
            for( ; i < 12; i++ )
            {
                int t0 = iparam[0][i - channels];
                int t1 = iparam[1][i - channels];

                iparam[0][i] = t0;
                iparam[1][i] = t1;
            }

            CV_GET_FUNC_PTR( func, (CvFunc2D_1A2P)(fastrng_tab.fn_2d[depth]));
            param = iparam;
        }
        else
        {
            for( i = 0; i < channels; i++ )
            {
                double t0 = param1.val[i];
                double t1 = param2.val[i];

                dparam[0][i] = t0 - (t1 - t0);
                dparam[1][i] = t1 - t0;
            }

            CV_GET_FUNC_PTR( func, (CvFunc2D_1A2P)(rng_tab[0].fn_2d[depth]));
        }
    }
    else if( disttype == CV_RAND_NORMAL )
    {
        for( i = 0; i < channels; i++ )
        {
            double t0 = param1.val[i];
            double t1 = param2.val[i];

            dparam[0][i] = t0;
            dparam[1][i] = t1;
        }

        CV_GET_FUNC_PTR( func, (CvFunc2D_1A2P)(rng_tab[1].fn_2d[depth]));
    }
    else
    {
        CV_ERROR( CV_StsBadArg, "Unknown distribution type" );
    }

    if( !fast_int_mode )
    {
        for( i = channels; i < 12; i++ )
        {
            double t0 = dparam[0][i - channels];
            double t1 = dparam[1][i - channels];

            dparam[0][i] = t0;
            dparam[1][i] = t1;
        }
    }

    if( !is_nd )
    {
        IPPI_CALL( func( mat->data.ptr, mat_step, size, rng, param ));
    }
    else
    {
        do
        {
            IPPI_CALL( func( iterator->ptr[0], CV_STUB_STEP, size, rng, param ));
        }
        while( cvNextNArraySlice( iterator ));
    }

    __END__;
}

/* End of file. */

⌨️ 快捷键说明

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