📄 cxarray.cpp
字号:
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's 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.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders 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 Intel Corporation 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.
//
//M*/
/* ////////////////////////////////////////////////////////////////////
//
// CvMat, CvMatND, CvSparceMat and IplImage support functions
// (creation, deletion, copying, retrieving and setting elements etc.)
//
// */
#include "_cxcore.h"
static struct
{
Cv_iplCreateImageHeader createHeader;
Cv_iplAllocateImageData allocateData;
Cv_iplDeallocate deallocate;
Cv_iplCreateROI createROI;
Cv_iplCloneImage cloneImage;
}
CvIPL;
// Makes the library use native IPL image allocators
CV_IMPL void
cvSetIPLAllocators( Cv_iplCreateImageHeader createHeader,
Cv_iplAllocateImageData allocateData,
Cv_iplDeallocate deallocate,
Cv_iplCreateROI createROI,
Cv_iplCloneImage cloneImage )
{
CV_FUNCNAME( "cvSetIPLAllocators" );
__BEGIN__;
if( !createHeader || !allocateData || !deallocate || !createROI || !cloneImage )
{
if( createHeader || allocateData || deallocate || createROI || cloneImage )
CV_ERROR( CV_StsBadArg, "Either all the pointers should be null or "
"they all should be non-null" );
}
CvIPL.createHeader = createHeader;
CvIPL.allocateData = allocateData;
CvIPL.deallocate = deallocate;
CvIPL.createROI = createROI;
CvIPL.cloneImage = cloneImage;
__END__;
}
/****************************************************************************************\
* CvMat creation and basic operations *
\****************************************************************************************/
// Creates CvMat and underlying data
CV_IMPL CvMat*
cvCreateMat( int height, int width, int type )
{
CvMat* arr = 0;
CV_FUNCNAME( "cvCreateMat" );
__BEGIN__;
CV_CALL( arr = cvCreateMatHeader( height, width, type ));
CV_CALL( cvCreateData( arr ));
__END__;
if( cvGetErrStatus() < 0 )
cvReleaseMat( &arr );
return arr;
}
static void icvCheckHuge( CvMat* arr )
{
if( (int64)arr->step*arr->rows > INT_MAX )
arr->type &= ~CV_MAT_CONT_FLAG;
}
// Creates CvMat header only
CV_IMPL CvMat*
cvCreateMatHeader( int rows, int cols, int type )
{
CvMat* arr = 0;
CV_FUNCNAME( "cvCreateMatHeader" );
__BEGIN__;
int min_step;
type = CV_MAT_TYPE(type);
if( rows <= 0 || cols <= 0 )
CV_ERROR( CV_StsBadSize, "Non-positive width or height" );
min_step = CV_ELEM_SIZE(type)*cols;
if( min_step <= 0 )
CV_ERROR( CV_StsUnsupportedFormat, "Invalid matrix type" );
CV_CALL( arr = (CvMat*)cvAlloc( sizeof(*arr)));
arr->step = rows == 1 ? 0 : cvAlign(min_step, CV_DEFAULT_MAT_ROW_ALIGN);
arr->type = CV_MAT_MAGIC_VAL | type |
(arr->step == 0 || arr->step == min_step ? CV_MAT_CONT_FLAG : 0);
arr->rows = rows;
arr->cols = cols;
arr->data.ptr = 0;
arr->refcount = 0;
arr->hdr_refcount = 1;
icvCheckHuge( arr );
__END__;
if( cvGetErrStatus() < 0 )
cvReleaseMat( &arr );
return arr;
}
// Initializes CvMat header, allocated by the user
CV_IMPL CvMat*
cvInitMatHeader( CvMat* arr, int rows, int cols,
int type, void* data, int step )
{
CV_FUNCNAME( "cvInitMatHeader" );
__BEGIN__;
int mask, pix_size, min_step;
if( !arr )
CV_ERROR_FROM_CODE( CV_StsNullPtr );
if( (unsigned)CV_MAT_DEPTH(type) > CV_DEPTH_MAX )
CV_ERROR_FROM_CODE( CV_BadNumChannels );
if( rows <= 0 || cols <= 0 )
CV_ERROR( CV_StsBadSize, "Non-positive cols or rows" );
type = CV_MAT_TYPE( type );
arr->type = type | CV_MAT_MAGIC_VAL;
arr->rows = rows;
arr->cols = cols;
arr->data.ptr = (uchar*)data;
arr->refcount = 0;
arr->hdr_refcount = 0;
mask = (arr->rows <= 1) - 1;
pix_size = CV_ELEM_SIZE(type);
min_step = arr->cols*pix_size & mask;
if( step != CV_AUTOSTEP && step != 0 )
{
if( step < min_step )
CV_ERROR_FROM_CODE( CV_BadStep );
arr->step = step & mask;
}
else
{
arr->step = min_step;
}
arr->type = CV_MAT_MAGIC_VAL | type |
(arr->step == min_step ? CV_MAT_CONT_FLAG : 0);
icvCheckHuge( arr );
__END__;
return arr;
}
// Deallocates the CvMat structure and underlying data
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_MAT_HDR(arr) && !CV_IS_MATND_HDR(arr) )
CV_ERROR_FROM_CODE( CV_StsBadFlag );
*array = 0;
cvDecRefData( arr );
cvFree( &arr );
}
__END__;
}
// Creates a copy of matrix
CV_IMPL CvMat*
cvCloneMat( const CvMat* src )
{
CvMat* dst = 0;
CV_FUNCNAME( "cvCloneMat" );
__BEGIN__;
if( !CV_IS_MAT_HDR( src ))
CV_ERROR( CV_StsBadArg, "Bad CvMat header" );
CV_CALL( dst = cvCreateMatHeader( src->rows, src->cols, src->type ));
if( src->data.ptr )
{
CV_CALL( cvCreateData( dst ));
CV_CALL( cvCopy( src, dst ));
}
__END__;
return dst;
}
/****************************************************************************************\
* CvMatND creation and basic operations *
\****************************************************************************************/
CV_IMPL CvMatND*
cvInitMatNDHeader( CvMatND* mat, int dims, const int* sizes,
int type, void* data )
{
CvMatND* result = 0;
CV_FUNCNAME( "cvInitMatNDHeader" );
__BEGIN__;
type = CV_MAT_TYPE(type);
int i;
int64 step = CV_ELEM_SIZE(type);
if( !mat )
CV_ERROR( CV_StsNullPtr, "NULL matrix header pointer" );
if( step == 0 )
CV_ERROR( CV_StsUnsupportedFormat, "invalid array data type" );
if( !sizes )
CV_ERROR( CV_StsNullPtr, "NULL <sizes> pointer" );
if( dims <= 0 || dims > CV_MAX_DIM )
CV_ERROR( CV_StsOutOfRange,
"non-positive or too large number of dimensions" );
for( i = dims - 1; i >= 0; i-- )
{
if( sizes[i] <= 0 )
CV_ERROR( CV_StsBadSize, "one of dimesion sizes is non-positive" );
mat->dim[i].size = sizes[i];
if( step > INT_MAX )
CV_ERROR( CV_StsOutOfRange, "The array is too big" );
mat->dim[i].step = (int)step;
step *= sizes[i];
}
mat->type = CV_MATND_MAGIC_VAL | (step <= INT_MAX ? CV_MAT_CONT_FLAG : 0) | type;
mat->dims = dims;
mat->data.ptr = (uchar*)data;
mat->refcount = 0;
mat->hdr_refcount = 0;
result = mat;
__END__;
if( cvGetErrStatus() < 0 && mat )
{
mat->type = 0;
mat->data.ptr = 0;
}
return result;
}
// Creates CvMatND and underlying data
CV_IMPL CvMatND*
cvCreateMatND( int dims, const int* sizes, int type )
{
CvMatND* arr = 0;
CV_FUNCNAME( "cvCreateMatND" );
__BEGIN__;
CV_CALL( arr = cvCreateMatNDHeader( dims, sizes, type ));
CV_CALL( cvCreateData( arr ));
__END__;
if( cvGetErrStatus() < 0 )
cvReleaseMatND( &arr );
return arr;
}
// Creates CvMatND header only
CV_IMPL CvMatND*
cvCreateMatNDHeader( int dims, const int* sizes, int type )
{
CvMatND* arr = 0;
CV_FUNCNAME( "cvCreateMatNDHeader" );
__BEGIN__;
if( dims <= 0 || dims > CV_MAX_DIM )
CV_ERROR( CV_StsOutOfRange,
"non-positive or too large number of dimensions" );
CV_CALL( arr = (CvMatND*)cvAlloc( sizeof(*arr) ));
CV_CALL( cvInitMatNDHeader( arr, dims, sizes, type, 0 ));
arr->hdr_refcount = 1;
__END__;
if( cvGetErrStatus() < 0 )
cvReleaseMatND( &arr );
return arr;
}
// Creates a copy of nD array
CV_IMPL CvMatND*
cvCloneMatND( const CvMatND* src )
{
CvMatND* dst = 0;
CV_FUNCNAME( "cvCloneMatND" );
__BEGIN__;
int i, *sizes;
if( !CV_IS_MATND_HDR( src ))
CV_ERROR( CV_StsBadArg, "Bad CvMatND header" );
sizes = (int*)alloca( src->dims*sizeof(sizes[0]) );
for( i = 0; i < src->dims; i++ )
sizes[i] = src->dim[i].size;
CV_CALL( dst = cvCreateMatNDHeader( src->dims, sizes, src->type ));
if( src->data.ptr )
{
CV_CALL( cvCreateData( dst ));
CV_CALL( cvCopy( src, dst ));
}
__END__;
return dst;
}
static CvMatND*
cvGetMatND( const CvArr* arr, CvMatND* matnd, int* coi )
{
CvMatND* result = 0;
CV_FUNCNAME( "cvGetMatND" );
__BEGIN__;
if( coi )
*coi = 0;
if( !matnd || !arr )
CV_ERROR( CV_StsNullPtr, "NULL array pointer is passed" );
if( CV_IS_MATND_HDR(arr))
{
if( !((CvMatND*)arr)->data.ptr )
CV_ERROR( CV_StsNullPtr, "The matrix has NULL data pointer" );
result = (CvMatND*)arr;
}
else
{
CvMat stub, *mat = (CvMat*)arr;
if( CV_IS_IMAGE_HDR( mat ))
CV_CALL( mat = cvGetMat( mat, &stub, coi ));
if( !CV_IS_MAT_HDR( mat ))
CV_ERROR( CV_StsBadArg, "Unrecognized or unsupported array type" );
if( !mat->data.ptr )
CV_ERROR( CV_StsNullPtr, "Input array has NULL data pointer" );
matnd->data.ptr = mat->data.ptr;
matnd->refcount = 0;
matnd->hdr_refcount = 0;
matnd->type = mat->type;
matnd->dims = 2;
matnd->dim[0].size = mat->rows;
matnd->dim[0].step = mat->step;
matnd->dim[1].size = mat->cols;
matnd->dim[1].step = CV_ELEM_SIZE(mat->type);
result = matnd;
}
__END__;
return result;
}
// returns number of dimensions to iterate.
/*
Checks whether <count> arrays have equal type, sizes (mask is optional array
that needs to have the same size, but 8uC1 or 8sC1 type).
Returns number of dimensions to iterate through:
0 means that all arrays are continuous,
1 means that all arrays are vectors of continuous arrays etc.
and the size of largest common continuous part of the arrays
*/
CV_IMPL int
cvInitNArrayIterator( int count, CvArr** arrs,
const CvArr* mask, CvMatND* stubs,
CvNArrayIterator* iterator, int flags )
{
int dims = -1;
CV_FUNCNAME( "cvInitArrayOp" );
__BEGIN__;
int i, j, size, dim0 = -1;
int64 step;
CvMatND* hdr0 = 0;
if( count < 1 || count > CV_MAX_ARR )
CV_ERROR( CV_StsOutOfRange, "Incorrect number of arrays" );
if( !arrs || !stubs )
CV_ERROR( CV_StsNullPtr, "Some of required array pointers is NULL" );
if( !iterator )
CV_ERROR( CV_StsNullPtr, "Iterator pointer is NULL" );
for( i = 0; i <= count; i++ )
{
const CvArr* arr = i < count ? arrs[i] : mask;
CvMatND* hdr;
if( !arr )
{
if( i < count )
CV_ERROR( CV_StsNullPtr, "Some of required array pointers is NULL" );
break;
}
if( CV_IS_MATND( arr ))
hdr = (CvMatND*)arr;
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -