Example #1
0
// initialize CvMat header, allocated by the user
CV_IMPL CvMat*
cvInitMatHeader( CvMat* arr, int height, int width,
                 int type, void* data, int step )
{
    CV_FUNCNAME( "cvInitMatHeader" );
    
    __BEGIN__;

    if( !arr )
        CV_ERROR_FROM_CODE( CV_StsNullPtr );

    if( (unsigned)(CV_ARR_CN(type) - 1) >= CV_CN_MAX )
        CV_ERROR_FROM_CODE( CV_BadDepth );

    if( (unsigned)CV_ARR_DEPTH(type) > CV_DEPTH_MAX )
        CV_ERROR_FROM_CODE( CV_BadNumChannels );

    if( height <= 0 || width <= 0 )
        CV_ERROR_FROM_CODE( CV_StsBadSize );
 
    arr->type = (type & CV_ARR_TYPE_MASK) | CV_ARR_MAGIC_VAL;
    arr->height = height;
    arr->width = width;

    CV_CALL( cvSetData( arr, data, step ));

    __END__;

    return arr;
}
Example #2
0
// get pointer to specified array element
CV_IMPL  uchar*
cvGetPtrAt( const CvArr* arr, int y, int x )
{
    uchar* ptr = 0;
    
    CV_FUNCNAME( "cvGetPtrAt" );

    __BEGIN__;

    if( CV_IS_ARR( arr ))
    {
        CvMat* mat = (CvMat*)arr;

        if( (unsigned)y >= (unsigned)(mat->height) ||
            (unsigned)x >= (unsigned)(mat->width) )
            CV_ERROR_FROM_CODE( CV_StsBadArg );

        ptr = mat->data.ptr + y*mat->step + x*icvPixSize[CV_ARR_TYPE(mat->type)];
    }
    else if( CV_IS_IMAGE( arr ))
    {
        IplImage* img = (IplImage*)arr;
        int pix_size = (img->depth & 255) >> 3;
        int width, height;
        ptr = (uchar*)img->imageData;

        if( img->dataOrder == 0 )
            pix_size *= img->nChannels;

        if( img->roi )
        {
            width = img->roi->width;
            height = img->roi->height;

            ptr += img->roi->yOffset*img->widthStep +
                   img->roi->xOffset*pix_size;

            if( img->dataOrder && img->roi->coi )
                ptr += (img->roi->coi - 1)*img->imageSize;
        }
        else
        {
            width = img->width;
            height = img->height;
        }

        if( (unsigned)y >= (unsigned)height ||
            (unsigned)x >= (unsigned)width )
            CV_ERROR_FROM_CODE( CV_StsBadArg );

        ptr += y*img->widthStep + x*pix_size;
    }

    __END__;

    return ptr;
}
Example #3
0
CV_IMPL void
cvRandSetRange( CvRandState * state, double lower, double upper, int index  )
{
    CV_FUNCNAME( "cvRandSetRange" );

    __BEGIN__;

    if( !state )
        CV_ERROR_FROM_CODE( CV_StsNullPtr );

    if( lower > upper )
        CV_ERROR( CV_StsOutOfRange,
        "lower boundary is greater than the upper one" );

    if( (unsigned)(index + 1) > 4 )
        CV_ERROR( CV_StsOutOfRange, "index is not in -1..3" );

    if( index < 0 )
    {
        state->param[0].val[0] = state->param[0].val[1] =
        state->param[0].val[2] = state->param[0].val[3] = lower;
        state->param[1].val[0] = state->param[1].val[1] = 
        state->param[1].val[2] = state->param[1].val[3] = upper;
    }
    else
    {
        state->param[0].val[index] = upper;
        state->param[1].val[index] = lower;
    }

    __END__;
}
Example #4
0
// deallocate CvMat header
CV_IMPL void
cvReleaseMatHeader( CvMat** array )
{
    CV_FUNCNAME( "cvReleaseMatHeader" );
    
    __BEGIN__;

    if( !array )
        CV_ERROR_FROM_CODE( CV_HeaderIsNull );

    if( *array )
    {
        CvMat* arr = *array;
        if( !_CV_IS_ARR( arr ))
            CV_ERROR_FROM_CODE( !arr ? CV_StsNullPtr : CV_StsBadFlag );

        *array = 0;
        cvFree( (void**)&arr );
    }

    __END__;
}
Example #5
0
// convert array (CvMat or IplImage) to IplImage
CV_IMPL IplImage*
cvGetImage( const CvArr* array, IplImage* img )
{
    IplImage* result = 0;
    const IplImage* src = (const IplImage*)array;
    
    CV_FUNCNAME( "cvGetImage" );

    __BEGIN__;

    if( !img )
        CV_ERROR_FROM_CODE( CV_StsNullPtr );

    if( !_CV_IS_IMAGE(src) )
    {
        const CvMat* mat = (const CvMat*)src;
        
        if( !_CV_IS_ARR(mat))
            CV_ERROR_FROM_CODE( CV_StsBadFlag );

        if( mat->data.ptr == 0 )
            CV_ERROR_FROM_CODE( CV_StsNullPtr );

        cvInitImageHeader( img, cvSize(mat->width, mat->height),
                           icvCvToIplDepth(CV_ARR_DEPTH(mat->type)),
                           CV_ARR_CN(mat->type) );
        cvSetData( img, mat->data.ptr, mat->step );

        result = img;
    }
    else
    {
        result = (IplImage*)src;
    }

    __END__;

    return result;
}
Example #6
0
CV_IMPL void
cvRandInit( CvRandState* state, double lower, double upper, int seed )
{
    CV_FUNCNAME( "cvRandInit" );

    __BEGIN__;

    if( !state )
        CV_ERROR_FROM_CODE( CV_StsNullPtr );

    state->state = (uint64)(seed ? seed : UINT_MAX);
    CV_CALL( cvRandSetRange( state, lower, upper ));

    __END__;
}
Example #7
0
// deallocate the CvMat structure and underlying date
CV_IMPL void
cvReleaseMat( CvMat** array )
{
    CV_FUNCNAME( "cvReleaseMat" );
    
    __BEGIN__;

    if( !array )
        CV_ERROR_FROM_CODE( CV_HeaderIsNull );

    if( *array )
    {
        CvMat* arr = *array;
        
        if( !_CV_IS_ARR( arr ))
            CV_ERROR_FROM_CODE( !arr ? CV_StsNullPtr : CV_StsBadFlag );

        *array = 0;
        CV_CALL( cvReleaseData( arr ));
        CV_CALL( cvReleaseMatHeader( &arr ));
    }

    __END__;
}
Example #8
0
// convert data of specified type to CvScalar
void
cvRawDataToScalar( void* data, int flags, CvScalar* scalar )
{
    CV_FUNCNAME( "cvRawDataToScalar" );
    
    __BEGIN__;

    int cn = CV_ARR_CN( flags );

    assert( scalar && data );
    assert( (unsigned)(cn - 1) < 4 );

    memset( scalar->val, 0, sizeof(scalar->val));

    switch( CV_ARR_DEPTH( flags ))
    {
    case CV_8U:
        while( cn-- )
            scalar->val[cn] = CV_8TO32F(((uchar*)data)[cn]);
        break;
    case CV_8S:
        while( cn-- )
            scalar->val[cn] = CV_8TO32F(((char*)data)[cn]);
        break;
    case CV_16S:
        while( cn-- )
            scalar->val[cn] = ((short*)data)[cn];
        break;
    case CV_32S:
        while( cn-- )
            scalar->val[cn] = ((int*)data)[cn];
        break;
    case CV_32F:
        while( cn-- )
            scalar->val[cn] = ((float*)data)[cn];
        break;
    case CV_64F:
        while( cn-- )
            scalar->val[cn] = ((double*)data)[cn];
        break;
    default:
        assert(0);
        CV_ERROR_FROM_CODE( CV_BadDepth );
    }

    __END__;
}
Example #9
0
CV_IMPL unsigned
cvRandNext( CvRandState * state )
{
    uint64 temp = 0;
    CV_FUNCNAME( "cvRandNext" );

    __BEGIN__;

    if( !state )
        CV_ERROR_FROM_CODE( CV_StsNullPtr );

    temp = state->state;
    temp = ICV_RNG_NEXT(temp);
    state->state = temp;

    __END__;

    return (unsigned)temp;
}
Example #10
0
// set CvMat flags
void
cvSetMatShapeFlags( CvMat* arr )
{
    CV_FUNCNAME( "cvSetMatShapeFlags" );
    
    __BEGIN__;

    int type, cont_flag;

    if( !_CV_IS_ARR( arr ))
        CV_ERROR_FROM_CODE( !arr ? CV_StsNullPtr : CV_StsBadFlag );

    type = CV_ARR_TYPE( arr->type );
    cont_flag = arr->step == 0 || arr->step == arr->width*icvPixSize[type];

    arr->type = type | (cont_flag << CV_ARR_CONT_FLAG_SHIFT) | CV_ARR_MAGIC_VAL;

    __END__;
}
Example #11
0
CV_IMPL void
cvAbsDiffS( const void* srcarr, void* dstarr, CvScalar scalar )
{
    static CvFuncTable adiffs_tab;
    static int inittab = 0;

    CV_FUNCNAME( "cvAbsDiffS" );

    __BEGIN__;

    int coi1 = 0, coi2 = 0;
    int type, sctype;
    CvMat srcstub, *src = (CvMat*)srcarr;
    CvMat dststub, *dst = (CvMat*)dstarr;
    int src_step = src->step;
    int dst_step = dst->step;
    double buf[12];
    CvSize size;

    if( !inittab )
    {
        icvInitAbsDiffCTable( &adiffs_tab );
        inittab = 1;
    }

    CV_CALL( src = cvGetMat( src, &srcstub, &coi1 ));
    CV_CALL( dst = cvGetMat( dst, &dststub, &coi2 ));

    if( coi1 != 0 || coi2 != 0 )
        CV_ERROR( CV_BadCOI, "" );

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

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

    sctype = type = CV_MAT_TYPE( src->type );
    if( CV_MAT_DEPTH(type) < CV_32S )
        sctype = (type & CV_MAT_CN_MASK) | CV_32SC1;
 
    size = icvGetMatSize( src );
    size.width *= CV_MAT_CN( type );

    src_step = src->step;
    dst_step = dst->step;

    if( CV_IS_MAT_CONT( src->type & dst->type ))
    {
        size.width *= size.height;
        size.height = 1;
        src_step = dst_step = CV_STUB_STEP;
    }

    CV_CALL( cvScalarToRawData( &scalar, buf, sctype, 1 ));

    {
        CvFunc2D_2A1P func = (CvFunc2D_2A1P)
            (adiffs_tab.fn_2d[CV_MAT_DEPTH(type)]);

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

        IPPI_CALL( func( src->data.ptr, src_step, dst->data.ptr,
                         dst_step, size, buf ));
    }

    __END__;
}
Example #12
0
CV_IMPL  void
cvAbsDiff( const void* srcarr1, const void* srcarr2, void* dstarr )
{
    static CvFuncTable adiff_tab;
    static int inittab = 0;

    CV_FUNCNAME( "cvAbsDiff" );

    __BEGIN__;

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

    if( !inittab )
    {
        icvInitAbsDiffTable( &adiff_tab );
        inittab = 1;
    }

    CV_CALL( src1 = cvGetMat( src1, &srcstub1, &coi1 ));
    CV_CALL( src2 = cvGetMat( src2, &srcstub2, &coi2 ));
    CV_CALL( dst = cvGetMat( dst, &dststub, &coi3 ));

    if( coi1 != 0 || coi2 != 0 || coi3 != 0 )
        CV_ERROR( CV_BadCOI, "" );

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

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

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

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

    size.width *= CV_MAT_CN( type );

    src1_step = src1->step;
    src2_step = src2->step;
    dst_step = dst->step;

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

    {
        CvFunc2D_3A func = (CvFunc2D_3A)
            (adiff_tab.fn_2d[CV_MAT_DEPTH(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__;
}
Example #13
0
CV_IMPL  void
cvAbsDiff( const void* srcarr1, const void* srcarr2, void* dstarr )
{

    CV_FUNCNAME( "cvAbsDiff" );

    __BEGIN__;

    int coi1 = 0, coi2 = 0, coi3 = 0;
    CvMat srcstub1, *src1 = (CvMat*)srcarr1;
    CvMat srcstub2, *src2 = (CvMat*)srcarr2;
    CvMat dststub,  *dst = (CvMat*)dstarr;
    CvSize size;
    int type, depth, pixel_size;

    CV_CALL( src1 = cvGetMat( src1, &srcstub1, &coi1 ));
    CV_CALL( src2 = cvGetMat( src2, &srcstub2, &coi2 ));
    CV_CALL( dst = cvGetMat( dst, &dststub, &coi3 ));

    if( coi1 != 0 || coi2 != 0 || coi3 != 0 )
        CV_ERROR( CV_BadCOI, "" );

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

    type = CV_MAT_TYPE(src1->type);
    depth = CV_MAT_DEPTH(type);

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

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

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

    size.width = src1->step * src1->height;
    size.height = 1;
	pixel_size = CV_DEPTH_BYTES[depth];

	if(depth == CV_8U)
	{
		int idx;
		unsigned char * p1;
		unsigned char * p2;
		unsigned char * pdst; 
		p1 = src1->data.ptr ; 
		p2 = src2->data.ptr; 
		pdst = dst->data.ptr; 
		
#ifdef _TMS320C6X
       	for (idx = 0; idx < size.width/pixel_size; idx+=4)
       	{
       		_amem4(pdst) = _subabs4(_amem4_const(p1), _amem4_const(p2) );
       		p1 += 4;
       		p2 += 4;
       		pdst += 4;
       	}	
#else
       	for (idx = 0; idx < size.width/pixel_size; idx+=1)
       	{
			(*pdst) = abs((*p1)-(*p2));
			pdst++;
			p1++;
			p2++;
       	}	
#endif
    }
	else if(depth == CV_32S)        
	{
		int idx;
		int * p1;
		int * p2;
		int * pdst; 
		p1 = src1->data.i; 
		p2 = src2->data.i; 
		pdst = dst->data.i;

       	for (idx = 0; idx < size.width/pixel_size; idx++)
       	{
#ifdef _TMS320C6X
       		*pdst = _abs(_ssub(*p1, *p2));
#else
       		*pdst = abs((*p1)-(*p2));
#endif
       		p1 += 1;
       		p2 += 1;
       		pdst += 1;
       	}	
    }
    else
    {
		CV_ERROR( CV_StsUnsupportedFormat, "unsupported matrix type." );
	}

    __END__;
}
Example #14
0
// convert array (CvMat or IplImage) to CvMat
CV_IMPL CvMat*
cvGetMat( const CvArr* array, CvMat* mat, int* pCOI )
{
    CvMat* result = 0;
    CvMat* src = (CvMat*)array;
    int coi = 0;
    
    CV_FUNCNAME( "cvGetMat" );

    __BEGIN__;

    if( !mat || !src )
        CV_ERROR_FROM_CODE( CV_StsNullPtr );

    if( !_CV_IS_ARR(src) )
    {
        const IplImage* img = (const IplImage*)src;
        int depth, order;

        if( img->nSize != sizeof(IplImage))
            CV_ERROR_FROM_CODE( CV_StsBadFlag );

        if( img->imageData == 0 )
            CV_ERROR_FROM_CODE( CV_StsNullPtr );

        if( img->nChannels > 4 )
            CV_ERROR_FROM_CODE( CV_BadNumChannels );

        depth = icvIplToCvDepth( img->depth );
        if( depth < 0 )
            CV_ERROR_FROM_CODE( CV_BadDepth );

        order = img->dataOrder & (img->nChannels > 1 ? -1 : 0);

        if( img->roi )
        {
            if( order == IPL_DATA_ORDER_PLANE )
            {
                int type = depth;

                if( img->roi->coi == 0 )
                    CV_ERROR( CV_StsBadFlag, "Planar order should be used with coi != 0" );

                CV_CALL( cvInitMatHeader( mat, img->roi->height,
                                   img->roi->width, type,
                                   img->imageData + (img->roi->coi-1)*img->imageSize +
                                   img->roi->yOffset*img->widthStep +
                                   img->roi->xOffset*icvPixSize[type],
                                   img->widthStep ));
            }
            else /* pixel order */
            {
                int type = depth + (img->nChannels - 1)*8;

                coi = img->roi->coi;

                CV_CALL( cvInitMatHeader( mat, img->roi->height,
                                   img->roi->width, type,
                                   img->imageData +
                                   img->roi->yOffset*img->widthStep +
                                   img->roi->xOffset*icvPixSize[type],
                                   img->widthStep ));
            }
        }
        else
        {
            int type = depth + (img->nChannels - 1)*8;

            if( order != IPL_DATA_ORDER_PIXEL )
                CV_ERROR( CV_StsBadFlag, "Pixel order should be used with coi == 0" );

            CV_CALL( cvInitMatHeader( mat, img->height,
                                      img->width, type,
                                      img->imageData, img->widthStep ));
        }

        result = mat;
    }
    else
    {
        if( !src->data.ptr )
            CV_ERROR_FROM_CODE( CV_StsNullPtr );
        
        result = (CvMat*)src;
    }

    __END__;

    if( pCOI )
        *pCOI = coi;

    return result;
}
Example #15
0
// convert CvScalar to specified type
void
cvScalarToRawData( CvScalar* scalar, int flags, void* data, int extend_to_12 )
{
    CV_FUNCNAME( "cvScalarToRawData" );
    
    __BEGIN__;

    int type = CV_ARR_TYPE( flags );
    int cn = CV_ARR_CN( type );
    int depth = type & CV_ARR_DEPTH_MASK;

    assert( scalar && data );
    assert( (unsigned)(cn - 1) < 4 );

    switch( depth )
    {
    case CV_8UC1:
        while( cn-- )
        {
            int t = cvRound( scalar->val[cn] );
            ((uchar*)data)[cn] = CV_CAST_8U(t);
        }
        break;
    case CV_8SC1:
        while( cn-- )
        {
            int t = cvRound( scalar->val[cn] );
            ((char*)data)[cn] = CV_CAST_8S(t);
        }
        break;
    case CV_16SC1:
        while( cn-- )
        {
            int t = cvRound( scalar->val[cn] );
            ((short*)data)[cn] = CV_CAST_16S(t);
        }
        break;
    case CV_32SC1:
        while( cn-- )
            ((int*)data)[cn] = cvRound( scalar->val[cn] );
        break;
    case CV_32FC1:
        while( cn-- )
            ((float*)data)[cn] = (float)(scalar->val[cn]);
        break;
    case CV_64FC1:
        while( cn-- )
            ((double*)data)[cn] = (double)(scalar->val[cn]);
        break;
    default:
        assert(0);
        CV_ERROR_FROM_CODE( CV_BadDepth );
    }

    if( extend_to_12 )
    {
        int pix_size = icvPixSize[type];
        int offset = icvPixSize[depth]*12;

        do
        {
            offset -= pix_size;
            memcpy( (char*)data + offset, data, pix_size );
        }
        while( offset > pix_size );
    }

    __END__;
}
Example #16
0
// set underlying CvMat or IplImage data
CV_IMPL void
cvSetData( CvArr* arr, void* data, int step )
{
    CV_FUNCNAME( "cvSetData" );

    __BEGIN__;

    int pix_size, min_step;

    if( _CV_IS_ARR( arr ))
    {
        CvMat* mat = (CvMat*)arr;

        pix_size = icvPixSize[CV_ARR_TYPE(mat->type)];
        min_step = mat->width*pix_size & ((mat->height <= 1) - 1);

        if( step != CV_AUTOSTEP )
        {
            if( step < min_step && data != 0 )
                CV_ERROR_FROM_CODE( CV_BadStep );
            mat->step = step & ((mat->height <= 1) - 1);
        }
        else
        {
            mat->step = min_step;
        }

        mat->data.ptr = (uchar*)data;

        CV_CALL( cvSetMatShapeFlags( mat ));
    }
    else if( _CV_IS_IMAGE( arr ))
    {
        IplImage* img = (IplImage*)arr;
        
        pix_size = icvGetBtPix( img );
        min_step = img->width*pix_size;

        if( step != CV_AUTOSTEP && img->height > 1 )
        {
            if( step < min_step && data != 0 )
                CV_ERROR_FROM_CODE( CV_BadStep );
            img->widthStep = step;
        }
        else
        {
            img->widthStep = min_step;
        }

        img->imageSize = img->widthStep * img->height;
        img->imageData = img->imageDataOrigin = (char*)data;

        if( (((int)(long)data | step) & 7) == 0 &&
            icvAlign(img->width * pix_size, 7) == step )
        {
            img->align = 8;
        }
        else
        {
            img->align = 4;
        }
    }
    else
    {
        CV_ERROR( CV_StsBadArg, "" );
    }

    __END__;
}