1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 /* ////////////////////////////////////////////////////////////////////
44 // CvMat, CvMatND, CvSparceMat and IplImage support functions
45 // (creation, deletion, copying, retrieving and setting elements etc.)
54 Cv_iplCreateImageHeader createHeader;
55 Cv_iplAllocateImageData allocateData;
56 Cv_iplDeallocate deallocate;
57 Cv_iplCreateROI createROI;
58 Cv_iplCloneImage cloneImage;
62 // Makes the library use native IPL image allocators
64 cvSetIPLAllocators( Cv_iplCreateImageHeader createHeader,
65 Cv_iplAllocateImageData allocateData,
66 Cv_iplDeallocate deallocate,
67 Cv_iplCreateROI createROI,
68 Cv_iplCloneImage cloneImage )
70 if( !createHeader || !allocateData || !deallocate || !createROI || !cloneImage )
72 if( createHeader || allocateData || deallocate || createROI || cloneImage )
73 CV_Error( CV_StsBadArg, "Either all the pointers should be null or "
74 "they all should be non-null" );
77 CvIPL.createHeader = createHeader;
78 CvIPL.allocateData = allocateData;
79 CvIPL.deallocate = deallocate;
80 CvIPL.createROI = createROI;
81 CvIPL.cloneImage = cloneImage;
85 /****************************************************************************************\
86 * CvMat creation and basic operations *
87 \****************************************************************************************/
89 // Creates CvMat and underlying data
91 cvCreateMat( int height, int width, int type )
93 CvMat* arr = cvCreateMatHeader( height, width, type );
100 static void icvCheckHuge( CvMat* arr )
102 if( (int64)arr->step*arr->rows > INT_MAX )
103 arr->type &= ~CV_MAT_CONT_FLAG;
106 // Creates CvMat header only
108 cvCreateMatHeader( int rows, int cols, int type )
110 type = CV_MAT_TYPE(type);
112 if( rows <= 0 || cols <= 0 )
113 CV_Error( CV_StsBadSize, "Non-positive width or height" );
115 int min_step = CV_ELEM_SIZE(type)*cols;
117 CV_Error( CV_StsUnsupportedFormat, "Invalid matrix type" );
119 CvMat* arr = (CvMat*)cvAlloc( sizeof(*arr));
121 arr->step = min_step;
122 arr->type = CV_MAT_MAGIC_VAL | type | CV_MAT_CONT_FLAG;
127 arr->hdr_refcount = 1;
134 // Initializes CvMat header, allocated by the user
136 cvInitMatHeader( CvMat* arr, int rows, int cols,
137 int type, void* data, int step )
140 CV_Error( CV_StsNullPtr, "" );
142 if( (unsigned)CV_MAT_DEPTH(type) > CV_DEPTH_MAX )
143 CV_Error( CV_BadNumChannels, "" );
145 if( rows <= 0 || cols <= 0 )
146 CV_Error( CV_StsBadSize, "Non-positive cols or rows" );
148 type = CV_MAT_TYPE( type );
149 arr->type = type | CV_MAT_MAGIC_VAL;
152 arr->data.ptr = (uchar*)data;
154 arr->hdr_refcount = 0;
156 int pix_size = CV_ELEM_SIZE(type);
157 int min_step = arr->cols*pix_size;
159 if( step != CV_AUTOSTEP && step != 0 )
161 if( step < min_step )
162 CV_Error( CV_BadStep, "" );
167 arr->step = min_step;
170 arr->type = CV_MAT_MAGIC_VAL | type |
171 (arr->rows == 1 || arr->step == min_step ? CV_MAT_CONT_FLAG : 0);
178 // Deallocates the CvMat structure and underlying data
180 cvReleaseMat( CvMat** array )
183 CV_Error( CV_HeaderIsNull, "" );
189 if( !CV_IS_MAT_HDR(arr) && !CV_IS_MATND_HDR(arr) )
190 CV_Error( CV_StsBadFlag, "" );
200 // Creates a copy of matrix
202 cvCloneMat( const CvMat* src )
204 if( !CV_IS_MAT_HDR( src ))
205 CV_Error( CV_StsBadArg, "Bad CvMat header" );
207 CvMat* dst = cvCreateMatHeader( src->rows, src->cols, src->type );
219 /****************************************************************************************\
220 * CvMatND creation and basic operations *
221 \****************************************************************************************/
224 cvInitMatNDHeader( CvMatND* mat, int dims, const int* sizes,
225 int type, void* data )
227 type = CV_MAT_TYPE(type);
228 int64 step = CV_ELEM_SIZE(type);
231 CV_Error( CV_StsNullPtr, "NULL matrix header pointer" );
234 CV_Error( CV_StsUnsupportedFormat, "invalid array data type" );
237 CV_Error( CV_StsNullPtr, "NULL <sizes> pointer" );
239 if( dims <= 0 || dims > CV_MAX_DIM )
240 CV_Error( CV_StsOutOfRange,
241 "non-positive or too large number of dimensions" );
243 for( int i = dims - 1; i >= 0; i-- )
246 CV_Error( CV_StsBadSize, "one of dimesion sizes is non-positive" );
247 mat->dim[i].size = sizes[i];
249 CV_Error( CV_StsOutOfRange, "The array is too big" );
250 mat->dim[i].step = (int)step;
254 mat->type = CV_MATND_MAGIC_VAL | (step <= INT_MAX ? CV_MAT_CONT_FLAG : 0) | type;
256 mat->data.ptr = (uchar*)data;
258 mat->hdr_refcount = 0;
263 // Creates CvMatND and underlying data
265 cvCreateMatND( int dims, const int* sizes, int type )
267 CvMatND* arr = cvCreateMatNDHeader( dims, sizes, type );
274 // Creates CvMatND header only
276 cvCreateMatNDHeader( int dims, const int* sizes, int type )
278 if( dims <= 0 || dims > CV_MAX_DIM )
279 CV_Error( CV_StsOutOfRange,
280 "non-positive or too large number of dimensions" );
282 CvMatND* arr = (CvMatND*)cvAlloc( sizeof(*arr) );
284 cvInitMatNDHeader( arr, dims, sizes, type, 0 );
285 arr->hdr_refcount = 1;
290 // Creates a copy of nD array
292 cvCloneMatND( const CvMatND* src )
294 if( !CV_IS_MATND_HDR( src ))
295 CV_Error( CV_StsBadArg, "Bad CvMatND header" );
297 int* sizes = (int*)cvStackAlloc( src->dims*sizeof(sizes[0]) );
299 for( int i = 0; i < src->dims; i++ )
300 sizes[i] = src->dim[i].size;
302 CvMatND* dst = cvCreateMatNDHeader( src->dims, sizes, src->type );
315 cvGetMatND( const CvArr* arr, CvMatND* matnd, int* coi )
323 CV_Error( CV_StsNullPtr, "NULL array pointer is passed" );
325 if( CV_IS_MATND_HDR(arr))
327 if( !((CvMatND*)arr)->data.ptr )
328 CV_Error( CV_StsNullPtr, "The matrix has NULL data pointer" );
330 result = (CvMatND*)arr;
334 CvMat stub, *mat = (CvMat*)arr;
336 if( CV_IS_IMAGE_HDR( mat ))
337 mat = cvGetMat( mat, &stub, coi );
339 if( !CV_IS_MAT_HDR( mat ))
340 CV_Error( CV_StsBadArg, "Unrecognized or unsupported array type" );
343 CV_Error( CV_StsNullPtr, "Input array has NULL data pointer" );
345 matnd->data.ptr = mat->data.ptr;
347 matnd->hdr_refcount = 0;
348 matnd->type = mat->type;
350 matnd->dim[0].size = mat->rows;
351 matnd->dim[0].step = mat->step;
352 matnd->dim[1].size = mat->cols;
353 matnd->dim[1].step = CV_ELEM_SIZE(mat->type);
361 // returns number of dimensions to iterate.
363 Checks whether <count> arrays have equal type, sizes (mask is optional array
364 that needs to have the same size, but 8uC1 or 8sC1 type).
365 Returns number of dimensions to iterate through:
366 0 means that all arrays are continuous,
367 1 means that all arrays are vectors of continuous arrays etc.
368 and the size of largest common continuous part of the arrays
371 cvInitNArrayIterator( int count, CvArr** arrs,
372 const CvArr* mask, CvMatND* stubs,
373 CvNArrayIterator* iterator, int flags )
376 int i, j, size, dim0 = -1;
380 if( count < 1 || count > CV_MAX_ARR )
381 CV_Error( CV_StsOutOfRange, "Incorrect number of arrays" );
383 if( !arrs || !stubs )
384 CV_Error( CV_StsNullPtr, "Some of required array pointers is NULL" );
387 CV_Error( CV_StsNullPtr, "Iterator pointer is NULL" );
389 for( i = 0; i <= count; i++ )
391 const CvArr* arr = i < count ? arrs[i] : mask;
397 CV_Error( CV_StsNullPtr, "Some of required array pointers is NULL" );
401 if( CV_IS_MATND( arr ))
406 hdr = cvGetMatND( arr, stubs + i, &coi );
408 CV_Error( CV_BadCOI, "COI set is not allowed here" );
411 iterator->hdr[i] = hdr;
415 if( hdr->dims != hdr0->dims )
416 CV_Error( CV_StsUnmatchedSizes,
417 "Number of dimensions is the same for all arrays" );
421 switch( flags & (CV_NO_DEPTH_CHECK|CV_NO_CN_CHECK))
424 if( !CV_ARE_TYPES_EQ( hdr, hdr0 ))
425 CV_Error( CV_StsUnmatchedFormats,
426 "Data type is not the same for all arrays" );
428 case CV_NO_DEPTH_CHECK:
429 if( !CV_ARE_CNS_EQ( hdr, hdr0 ))
430 CV_Error( CV_StsUnmatchedFormats,
431 "Number of channels is not the same for all arrays" );
434 if( !CV_ARE_CNS_EQ( hdr, hdr0 ))
435 CV_Error( CV_StsUnmatchedFormats,
436 "Depth is not the same for all arrays" );
442 if( !CV_IS_MASK_ARR( hdr ))
443 CV_Error( CV_StsBadMask, "Mask should have 8uC1 or 8sC1 data type" );
446 if( !(flags & CV_NO_SIZE_CHECK) )
448 for( j = 0; j < hdr->dims; j++ )
449 if( hdr->dim[j].size != hdr0->dim[j].size )
450 CV_Error( CV_StsUnmatchedSizes,
451 "Dimension sizes are the same for all arrays" );
457 step = CV_ELEM_SIZE(hdr->type);
458 for( j = hdr->dims - 1; j > dim0; j-- )
460 if( step != hdr->dim[j].step )
462 step *= hdr->dim[j].size;
465 if( j == dim0 && step > INT_MAX )
471 iterator->hdr[i] = (CvMatND*)hdr;
472 iterator->ptr[i] = (uchar*)hdr->data.ptr;
476 for( j = hdr0->dims - 1; j > dim0; j-- )
477 size *= hdr0->dim[j].size;
480 iterator->dims = dims;
481 iterator->count = count;
482 iterator->size = cvSize(size,1);
484 for( i = 0; i < dims; i++ )
485 iterator->stack[i] = hdr0->dim[i].size;
491 // returns zero value if iteration is finished, non-zero otherwise
492 CV_IMPL int cvNextNArraySlice( CvNArrayIterator* iterator )
494 assert( iterator != 0 );
495 int i, dims, size = 0;
497 for( dims = iterator->dims; dims > 0; dims-- )
499 for( i = 0; i < iterator->count; i++ )
500 iterator->ptr[i] += iterator->hdr[i]->dim[dims-1].step;
502 if( --iterator->stack[dims-1] > 0 )
505 size = iterator->hdr[0]->dim[dims-1].size;
507 for( i = 0; i < iterator->count; i++ )
508 iterator->ptr[i] -= (size_t)size*iterator->hdr[i]->dim[dims-1].step;
510 iterator->stack[dims-1] = size;
517 /****************************************************************************************\
518 * CvSparseMat creation and basic operations *
519 \****************************************************************************************/
522 // Creates CvMatND and underlying data
524 cvCreateSparseMat( int dims, const int* sizes, int type )
526 type = CV_MAT_TYPE( type );
527 int pix_size1 = CV_ELEM_SIZE1(type);
528 int pix_size = pix_size1*CV_MAT_CN(type);
530 CvMemStorage* storage;
533 CV_Error( CV_StsUnsupportedFormat, "invalid array data type" );
535 if( dims <= 0 || dims > CV_MAX_DIM_HEAP )
536 CV_Error( CV_StsOutOfRange, "bad number of dimensions" );
539 CV_Error( CV_StsNullPtr, "NULL <sizes> pointer" );
541 for( i = 0; i < dims; i++ )
544 CV_Error( CV_StsBadSize, "one of dimesion sizes is non-positive" );
547 CvSparseMat* arr = (CvSparseMat*)cvAlloc(sizeof(*arr)+MAX(0,dims-CV_MAX_DIM)*sizeof(arr->size[0]));
549 arr->type = CV_SPARSE_MAT_MAGIC_VAL | type;
552 arr->hdr_refcount = 1;
553 memcpy( arr->size, sizes, dims*sizeof(sizes[0]));
555 arr->valoffset = (int)cvAlign(sizeof(CvSparseNode), pix_size1);
556 arr->idxoffset = (int)cvAlign(arr->valoffset + pix_size, sizeof(int));
557 size = (int)cvAlign(arr->idxoffset + dims*sizeof(int), sizeof(CvSetElem));
559 storage = cvCreateMemStorage( CV_SPARSE_MAT_BLOCK );
560 arr->heap = cvCreateSet( 0, sizeof(CvSet), size, storage );
562 arr->hashsize = CV_SPARSE_HASH_SIZE0;
563 size = arr->hashsize*sizeof(arr->hashtable[0]);
565 arr->hashtable = (void**)cvAlloc( size );
566 memset( arr->hashtable, 0, size );
572 // Creates CvMatND and underlying data
574 cvReleaseSparseMat( CvSparseMat** array )
577 CV_Error( CV_HeaderIsNull, "" );
581 CvSparseMat* arr = *array;
583 if( !CV_IS_SPARSE_MAT_HDR(arr) )
584 CV_Error( CV_StsBadFlag, "" );
588 CvMemStorage* storage = arr->heap->storage;
589 cvReleaseMemStorage( &storage );
590 cvFree( &arr->hashtable );
596 // Creates CvMatND and underlying data
598 cvCloneSparseMat( const CvSparseMat* src )
600 if( !CV_IS_SPARSE_MAT_HDR(src) )
601 CV_Error( CV_StsBadArg, "Invalid sparse array header" );
603 CvSparseMat* dst = cvCreateSparseMat( src->dims, src->size, src->type );
610 cvInitSparseMatIterator( const CvSparseMat* mat, CvSparseMatIterator* iterator )
612 CvSparseNode* node = 0;
615 if( !CV_IS_SPARSE_MAT( mat ))
616 CV_Error( CV_StsBadArg, "Invalid sparse matrix header" );
619 CV_Error( CV_StsNullPtr, "NULL iterator pointer" );
621 iterator->mat = (CvSparseMat*)mat;
624 for( idx = 0; idx < mat->hashsize; idx++ )
625 if( mat->hashtable[idx] )
627 node = iterator->node = (CvSparseNode*)mat->hashtable[idx];
631 iterator->curidx = idx;
635 #define ICV_SPARSE_MAT_HASH_MULTIPLIER cv::SparseMat::HASH_SCALE
638 icvGetNodePtr( CvSparseMat* mat, const int* idx, int* _type,
639 int create_node, unsigned* precalc_hashval )
643 unsigned hashval = 0;
645 assert( CV_IS_SPARSE_MAT( mat ));
647 if( !precalc_hashval )
649 for( i = 0; i < mat->dims; i++ )
652 if( (unsigned)t >= (unsigned)mat->size[i] )
653 CV_Error( CV_StsOutOfRange, "One of indices is out of range" );
654 hashval = hashval*ICV_SPARSE_MAT_HASH_MULTIPLIER + t;
659 hashval = *precalc_hashval;
662 tabidx = hashval & (mat->hashsize - 1);
665 if( create_node >= -1 )
667 for( node = (CvSparseNode*)mat->hashtable[tabidx];
668 node != 0; node = node->next )
670 if( node->hashval == hashval )
672 int* nodeidx = CV_NODE_IDX(mat,node);
673 for( i = 0; i < mat->dims; i++ )
674 if( idx[i] != nodeidx[i] )
678 ptr = (uchar*)CV_NODE_VAL(mat,node);
685 if( !ptr && create_node )
687 if( mat->heap->active_count >= mat->hashsize*CV_SPARSE_HASH_RATIO )
690 int newsize = MAX( mat->hashsize*2, CV_SPARSE_HASH_SIZE0);
691 int newrawsize = newsize*sizeof(newtable[0]);
693 CvSparseMatIterator iterator;
694 assert( (newsize & (newsize - 1)) == 0 );
697 newtable = (void**)cvAlloc( newrawsize );
698 memset( newtable, 0, newrawsize );
700 node = cvInitSparseMatIterator( mat, &iterator );
703 CvSparseNode* next = cvGetNextSparseNode( &iterator );
704 int newidx = node->hashval & (newsize - 1);
705 node->next = (CvSparseNode*)newtable[newidx];
706 newtable[newidx] = node;
710 cvFree( &mat->hashtable );
711 mat->hashtable = newtable;
712 mat->hashsize = newsize;
713 tabidx = hashval & (newsize - 1);
716 node = (CvSparseNode*)cvSetNew( mat->heap );
717 node->hashval = hashval;
718 node->next = (CvSparseNode*)mat->hashtable[tabidx];
719 mat->hashtable[tabidx] = node;
720 CV_MEMCPY_INT( CV_NODE_IDX(mat,node), idx, mat->dims );
721 ptr = (uchar*)CV_NODE_VAL(mat,node);
722 if( create_node > 0 )
723 CV_ZERO_CHAR( ptr, CV_ELEM_SIZE(mat->type));
727 *_type = CV_MAT_TYPE(mat->type);
734 icvDeleteNode( CvSparseMat* mat, const int* idx, unsigned* precalc_hashval )
737 unsigned hashval = 0;
738 CvSparseNode *node, *prev = 0;
739 assert( CV_IS_SPARSE_MAT( mat ));
741 if( !precalc_hashval )
743 for( i = 0; i < mat->dims; i++ )
746 if( (unsigned)t >= (unsigned)mat->size[i] )
747 CV_Error( CV_StsOutOfRange, "One of indices is out of range" );
748 hashval = hashval*ICV_SPARSE_MAT_HASH_MULTIPLIER + t;
753 hashval = *precalc_hashval;
756 tabidx = hashval & (mat->hashsize - 1);
759 for( node = (CvSparseNode*)mat->hashtable[tabidx];
760 node != 0; prev = node, node = node->next )
762 if( node->hashval == hashval )
764 int* nodeidx = CV_NODE_IDX(mat,node);
765 for( i = 0; i < mat->dims; i++ )
766 if( idx[i] != nodeidx[i] )
776 prev->next = node->next;
778 mat->hashtable[tabidx] = node->next;
779 cvSetRemoveByPtr( mat->heap, node );
784 /****************************************************************************************\
785 * Common for multiple array types operations *
786 \****************************************************************************************/
788 // Allocates underlying array data
790 cvCreateData( CvArr* arr )
792 if( CV_IS_MAT_HDR( arr ))
794 size_t step, total_size;
795 CvMat* mat = (CvMat*)arr;
798 if( mat->data.ptr != 0 )
799 CV_Error( CV_StsError, "Data is already allocated" );
802 step = CV_ELEM_SIZE(mat->type)*mat->cols;
804 total_size = step*mat->rows + sizeof(int) + CV_MALLOC_ALIGN;
805 mat->refcount = (int*)cvAlloc( (size_t)total_size );
806 mat->data.ptr = (uchar*)cvAlignPtr( mat->refcount + 1, CV_MALLOC_ALIGN );
809 else if( CV_IS_IMAGE_HDR(arr))
811 IplImage* img = (IplImage*)arr;
813 if( img->imageData != 0 )
814 CV_Error( CV_StsError, "Data is already allocated" );
816 if( !CvIPL.allocateData )
818 img->imageData = img->imageDataOrigin =
819 (char*)cvAlloc( (size_t)img->imageSize );
823 int depth = img->depth;
824 int width = img->width;
826 if( img->depth == IPL_DEPTH_32F || img->nChannels == 64 )
828 img->width *= img->depth == IPL_DEPTH_32F ? sizeof(float) : sizeof(double);
829 img->depth = IPL_DEPTH_8U;
832 CvIPL.allocateData( img, 0, 0 );
838 else if( CV_IS_MATND_HDR( arr ))
840 CvMatND* mat = (CvMatND*)arr;
842 size_t total_size = CV_ELEM_SIZE(mat->type);
844 if( mat->data.ptr != 0 )
845 CV_Error( CV_StsError, "Data is already allocated" );
847 if( CV_IS_MAT_CONT( mat->type ))
849 total_size = (size_t)mat->dim[0].size*(mat->dim[0].step != 0 ?
850 mat->dim[0].step : total_size);
854 for( i = mat->dims - 1; i >= 0; i-- )
856 size_t size = (size_t)mat->dim[i].step*mat->dim[i].size;
858 if( total_size < size )
863 mat->refcount = (int*)cvAlloc( total_size +
864 sizeof(int) + CV_MALLOC_ALIGN );
865 mat->data.ptr = (uchar*)cvAlignPtr( mat->refcount + 1, CV_MALLOC_ALIGN );
869 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
873 // Assigns external data to array
875 cvSetData( CvArr* arr, void* data, int step )
877 int pix_size, min_step;
879 if( CV_IS_MAT_HDR(arr) || CV_IS_MATND_HDR(arr) )
880 cvReleaseData( arr );
882 if( CV_IS_MAT_HDR( arr ))
884 CvMat* mat = (CvMat*)arr;
886 int type = CV_MAT_TYPE(mat->type);
887 pix_size = CV_ELEM_SIZE(type);
888 min_step = mat->cols*pix_size;
890 if( step != CV_AUTOSTEP && step != 0 )
892 if( step < min_step && data != 0 )
893 CV_Error( CV_BadStep, "" );
897 mat->step = min_step;
899 mat->data.ptr = (uchar*)data;
900 mat->type = CV_MAT_MAGIC_VAL | type |
901 (mat->rows == 1 || mat->step == min_step ? CV_MAT_CONT_FLAG : 0);
904 else if( CV_IS_IMAGE_HDR( arr ))
906 IplImage* img = (IplImage*)arr;
908 pix_size = ((img->depth & 255) >> 3)*img->nChannels;
909 min_step = img->width*pix_size;
911 if( step != CV_AUTOSTEP && img->height > 1 )
913 if( step < min_step && data != 0 )
914 CV_Error( CV_BadStep, "" );
915 img->widthStep = step;
919 img->widthStep = min_step;
922 img->imageSize = img->widthStep * img->height;
923 img->imageData = img->imageDataOrigin = (char*)data;
925 if( (((int)(size_t)data | step) & 7) == 0 &&
926 cvAlign(img->width * pix_size, 8) == step )
931 else if( CV_IS_MATND_HDR( arr ))
933 CvMatND* mat = (CvMatND*)arr;
937 if( step != CV_AUTOSTEP )
938 CV_Error( CV_BadStep,
939 "For multidimensional array only CV_AUTOSTEP is allowed here" );
941 mat->data.ptr = (uchar*)data;
942 cur_step = CV_ELEM_SIZE(mat->type);
944 for( i = mat->dims - 1; i >= 0; i-- )
946 if( cur_step > INT_MAX )
947 CV_Error( CV_StsOutOfRange, "The array is too big" );
948 mat->dim[i].step = (int)cur_step;
949 cur_step *= mat->dim[i].size;
953 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
957 // Deallocates array's data
959 cvReleaseData( CvArr* arr )
961 if( CV_IS_MAT_HDR( arr ) || CV_IS_MATND_HDR( arr ))
963 CvMat* mat = (CvMat*)arr;
966 else if( CV_IS_IMAGE_HDR( arr ))
968 IplImage* img = (IplImage*)arr;
970 if( !CvIPL.deallocate )
972 char* ptr = img->imageDataOrigin;
973 img->imageData = img->imageDataOrigin = 0;
978 CvIPL.deallocate( img, IPL_IMAGE_DATA );
982 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
986 // Retrieves essential information about image ROI or CvMat data
988 cvGetRawData( const CvArr* arr, uchar** data, int* step, CvSize* roi_size )
990 if( CV_IS_MAT( arr ))
992 CvMat *mat = (CvMat*)arr;
998 *data = mat->data.ptr;
1001 *roi_size = cvGetMatSize( mat );
1003 else if( CV_IS_IMAGE( arr ))
1005 IplImage* img = (IplImage*)arr;
1008 *step = img->widthStep;
1011 *data = cvPtr2D( img, 0, 0 );
1017 *roi_size = cvSize( img->roi->width, img->roi->height );
1021 *roi_size = cvSize( img->width, img->height );
1025 else if( CV_IS_MATND( arr ))
1027 CvMatND* mat = (CvMatND*)arr;
1029 if( !CV_IS_MAT_CONT( mat->type ))
1030 CV_Error( CV_StsBadArg, "Only continuous nD arrays are supported here" );
1033 *data = mat->data.ptr;
1035 if( roi_size || step )
1037 int i, size1 = mat->dim[0].size, size2 = 1;
1040 for( i = 1; i < mat->dims; i++ )
1041 size1 *= mat->dim[i].size;
1043 size2 = mat->dim[1].size;
1047 roi_size->width = size2;
1048 roi_size->height = size1;
1052 *step = mat->dim[0].step;
1056 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
1061 cvGetElemType( const CvArr* arr )
1064 if( CV_IS_MAT_HDR(arr) || CV_IS_MATND_HDR(arr) || CV_IS_SPARSE_MAT_HDR(arr))
1065 type = CV_MAT_TYPE( ((CvMat*)arr)->type );
1066 else if( CV_IS_IMAGE(arr))
1068 IplImage* img = (IplImage*)arr;
1069 type = CV_MAKETYPE( IplToCvDepth(img->depth), img->nChannels );
1072 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
1078 // Returns a number of array dimensions
1080 cvGetDims( const CvArr* arr, int* sizes )
1083 if( CV_IS_MAT_HDR( arr ))
1085 CvMat* mat = (CvMat*)arr;
1090 sizes[0] = mat->rows;
1091 sizes[1] = mat->cols;
1094 else if( CV_IS_IMAGE( arr ))
1096 IplImage* img = (IplImage*)arr;
1101 sizes[0] = img->height;
1102 sizes[1] = img->width;
1105 else if( CV_IS_MATND_HDR( arr ))
1107 CvMatND* mat = (CvMatND*)arr;
1113 for( i = 0; i < dims; i++ )
1114 sizes[i] = mat->dim[i].size;
1117 else if( CV_IS_SPARSE_MAT_HDR( arr ))
1119 CvSparseMat* mat = (CvSparseMat*)arr;
1123 memcpy( sizes, mat->size, dims*sizeof(sizes[0]));
1126 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
1132 // Returns the size of particular array dimension
1134 cvGetDimSize( const CvArr* arr, int index )
1138 if( CV_IS_MAT( arr ))
1140 CvMat *mat = (CvMat*)arr;
1151 CV_Error( CV_StsOutOfRange, "bad dimension index" );
1154 else if( CV_IS_IMAGE( arr ))
1156 IplImage* img = (IplImage*)arr;
1161 size = !img->roi ? img->height : img->roi->height;
1164 size = !img->roi ? img->width : img->roi->width;
1167 CV_Error( CV_StsOutOfRange, "bad dimension index" );
1170 else if( CV_IS_MATND_HDR( arr ))
1172 CvMatND* mat = (CvMatND*)arr;
1174 if( (unsigned)index >= (unsigned)mat->dims )
1175 CV_Error( CV_StsOutOfRange, "bad dimension index" );
1177 size = mat->dim[index].size;
1179 else if( CV_IS_SPARSE_MAT_HDR( arr ))
1181 CvSparseMat* mat = (CvSparseMat*)arr;
1183 if( (unsigned)index >= (unsigned)mat->dims )
1184 CV_Error( CV_StsOutOfRange, "bad dimension index" );
1186 size = mat->size[index];
1189 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
1195 // Returns the size of CvMat or IplImage
1197 cvGetSize( const CvArr* arr )
1199 CvSize size = { 0, 0 };
1201 if( CV_IS_MAT_HDR( arr ))
1203 CvMat *mat = (CvMat*)arr;
1205 size.width = mat->cols;
1206 size.height = mat->rows;
1208 else if( CV_IS_IMAGE_HDR( arr ))
1210 IplImage* img = (IplImage*)arr;
1214 size.width = img->roi->width;
1215 size.height = img->roi->height;
1219 size.width = img->width;
1220 size.height = img->height;
1224 CV_Error( CV_StsBadArg, "Array should be CvMat or IplImage" );
1230 // Selects sub-array (no data is copied)
1232 cvGetSubRect( const CvArr* arr, CvMat* submat, CvRect rect )
1235 CvMat stub, *mat = (CvMat*)arr;
1237 if( !CV_IS_MAT( mat ))
1238 mat = cvGetMat( mat, &stub );
1241 CV_Error( CV_StsNullPtr, "" );
1243 if( (rect.x|rect.y|rect.width|rect.height) < 0 )
1244 CV_Error( CV_StsBadSize, "" );
1246 if( rect.x + rect.width > mat->cols ||
1247 rect.y + rect.height > mat->rows )
1248 CV_Error( CV_StsBadSize, "" );
1252 int* refcount = mat->refcount;
1257 cvDecRefData( submat );
1259 submat->data.ptr = mat->data.ptr + (size_t)rect.y*mat->step +
1260 rect.x*CV_ELEM_SIZE(mat->type);
1261 submat->step = mat->step;
1262 submat->type = (mat->type & (rect.width < mat->cols ? ~CV_MAT_CONT_FLAG : -1)) |
1263 (rect.height <= 1 ? CV_MAT_CONT_FLAG : 0);
1264 submat->rows = rect.height;
1265 submat->cols = rect.width;
1266 submat->refcount = 0;
1274 // Selects array's row span.
1276 cvGetRows( const CvArr* arr, CvMat* submat,
1277 int start_row, int end_row, int delta_row )
1280 CvMat stub, *mat = (CvMat*)arr;
1282 if( !CV_IS_MAT( mat ))
1283 mat = cvGetMat( mat, &stub );
1286 CV_Error( CV_StsNullPtr, "" );
1288 if( (unsigned)start_row >= (unsigned)mat->rows ||
1289 (unsigned)end_row > (unsigned)mat->rows || delta_row <= 0 )
1290 CV_Error( CV_StsOutOfRange, "" );
1294 int* refcount = mat->refcount;
1299 cvDecRefData( submat );
1301 if( delta_row == 1 )
1303 submat->rows = end_row - start_row;
1304 submat->step = mat->step;
1308 submat->rows = (end_row - start_row + delta_row - 1)/delta_row;
1309 submat->step = mat->step * delta_row;
1312 submat->cols = mat->cols;
1313 submat->step &= submat->rows > 1 ? -1 : 0;
1314 submat->data.ptr = mat->data.ptr + (size_t)start_row*mat->step;
1315 submat->type = (mat->type | (submat->rows == 1 ? CV_MAT_CONT_FLAG : 0)) &
1316 (delta_row != 1 && submat->rows > 1 ? ~CV_MAT_CONT_FLAG : -1);
1317 submat->refcount = 0;
1318 submat->hdr_refcount = 0;
1326 // Selects array's column span.
1328 cvGetCols( const CvArr* arr, CvMat* submat, int start_col, int end_col )
1331 CvMat stub, *mat = (CvMat*)arr;
1334 if( !CV_IS_MAT( mat ))
1335 mat = cvGetMat( mat, &stub );
1338 CV_Error( CV_StsNullPtr, "" );
1341 if( (unsigned)start_col >= (unsigned)cols ||
1342 (unsigned)end_col > (unsigned)cols )
1343 CV_Error( CV_StsOutOfRange, "" );
1347 int* refcount = mat->refcount;
1352 cvDecRefData( submat );
1354 submat->rows = mat->rows;
1355 submat->cols = end_col - start_col;
1356 submat->step = mat->step;
1357 submat->data.ptr = mat->data.ptr + (size_t)start_col*CV_ELEM_SIZE(mat->type);
1358 submat->type = mat->type & (submat->rows > 1 && submat->cols < cols ? ~CV_MAT_CONT_FLAG : -1);
1359 submat->refcount = 0;
1360 submat->hdr_refcount = 0;
1368 // Selects array diagonal
1370 cvGetDiag( const CvArr* arr, CvMat* submat, int diag )
1373 CvMat stub, *mat = (CvMat*)arr;
1376 if( !CV_IS_MAT( mat ))
1377 mat = cvGetMat( mat, &stub );
1380 CV_Error( CV_StsNullPtr, "" );
1382 pix_size = CV_ELEM_SIZE(mat->type);
1385 int* refcount = mat->refcount;
1390 cvDecRefData( submat );
1395 len = mat->cols - diag;
1398 CV_Error( CV_StsOutOfRange, "" );
1400 len = CV_IMIN( len, mat->rows );
1401 submat->data.ptr = mat->data.ptr + diag*pix_size;
1405 len = mat->rows + diag;
1408 CV_Error( CV_StsOutOfRange, "" );
1410 len = CV_IMIN( len, mat->cols );
1411 submat->data.ptr = mat->data.ptr - diag*mat->step;
1416 submat->step = mat->step + (submat->rows > 1 ? pix_size : 0);
1417 submat->type = mat->type;
1418 if( submat->rows > 1 )
1419 submat->type &= ~CV_MAT_CONT_FLAG;
1421 submat->type |= CV_MAT_CONT_FLAG;
1422 submat->refcount = 0;
1423 submat->hdr_refcount = 0;
1430 /****************************************************************************************\
1431 * Operations on CvScalar and accessing array elements *
1432 \****************************************************************************************/
1434 // Converts CvScalar to specified type
1436 cvScalarToRawData( const CvScalar* scalar, void* data, int type, int extend_to_12 )
1438 type = CV_MAT_TYPE(type);
1439 int cn = CV_MAT_CN( type );
1440 int depth = type & CV_MAT_DEPTH_MASK;
1442 assert( scalar && data );
1443 if( (unsigned)(cn - 1) >= 4 )
1444 CV_Error( CV_StsOutOfRange, "The number of channels must be 1, 2, 3 or 4" );
1451 int t = cvRound( scalar->val[cn] );
1452 ((uchar*)data)[cn] = CV_CAST_8U(t);
1458 int t = cvRound( scalar->val[cn] );
1459 ((char*)data)[cn] = CV_CAST_8S(t);
1465 int t = cvRound( scalar->val[cn] );
1466 ((ushort*)data)[cn] = CV_CAST_16U(t);
1472 int t = cvRound( scalar->val[cn] );
1473 ((short*)data)[cn] = CV_CAST_16S(t);
1478 ((int*)data)[cn] = cvRound( scalar->val[cn] );
1482 ((float*)data)[cn] = (float)(scalar->val[cn]);
1486 ((double*)data)[cn] = (double)(scalar->val[cn]);
1490 CV_Error( CV_BadDepth, "" );
1495 int pix_size = CV_ELEM_SIZE(type);
1496 int offset = CV_ELEM_SIZE1(depth)*12;
1501 CV_MEMCPY_AUTO( (char*)data + offset, data, pix_size );
1503 while( offset > pix_size );
1508 // Converts data of specified type to CvScalar
1510 cvRawDataToScalar( const void* data, int flags, CvScalar* scalar )
1512 int cn = CV_MAT_CN( flags );
1514 assert( scalar && data );
1516 if( (unsigned)(cn - 1) >= 4 )
1517 CV_Error( CV_StsOutOfRange, "The number of channels must be 1, 2, 3 or 4" );
1519 memset( scalar->val, 0, sizeof(scalar->val));
1521 switch( CV_MAT_DEPTH( flags ))
1525 scalar->val[cn] = CV_8TO32F(((uchar*)data)[cn]);
1529 scalar->val[cn] = CV_8TO32F(((char*)data)[cn]);
1533 scalar->val[cn] = ((ushort*)data)[cn];
1537 scalar->val[cn] = ((short*)data)[cn];
1541 scalar->val[cn] = ((int*)data)[cn];
1545 scalar->val[cn] = ((float*)data)[cn];
1549 scalar->val[cn] = ((double*)data)[cn];
1553 CV_Error( CV_BadDepth, "" );
1558 static double icvGetReal( const void* data, int type )
1563 return *(uchar*)data;
1565 return *(char*)data;
1567 return *(ushort*)data;
1569 return *(short*)data;
1573 return *(float*)data;
1575 return *(double*)data;
1582 static void icvSetReal( double value, const void* data, int type )
1586 int ivalue = cvRound(value);
1590 *(uchar*)data = CV_CAST_8U(ivalue);
1593 *(char*)data = CV_CAST_8S(ivalue);
1596 *(ushort*)data = CV_CAST_16U(ivalue);
1599 *(short*)data = CV_CAST_16S(ivalue);
1602 *(int*)data = CV_CAST_32S(ivalue);
1611 *(float*)data = (float)value;
1614 *(double*)data = value;
1621 // Returns pointer to specified element of array (linear index is used)
1623 cvPtr1D( const CvArr* arr, int idx, int* _type )
1626 if( CV_IS_MAT( arr ))
1628 CvMat* mat = (CvMat*)arr;
1630 int type = CV_MAT_TYPE(mat->type);
1631 int pix_size = CV_ELEM_SIZE(type);
1636 // the first part is mul-free sufficient check
1637 // that the index is within the matrix
1638 if( (unsigned)idx >= (unsigned)(mat->rows + mat->cols - 1) &&
1639 (unsigned)idx >= (unsigned)(mat->rows*mat->cols))
1640 CV_Error( CV_StsOutOfRange, "index is out of range" );
1642 if( CV_IS_MAT_CONT(mat->type))
1644 ptr = mat->data.ptr + (size_t)idx*pix_size;
1649 if( mat->cols == 1 )
1652 row = idx/mat->cols, col = idx - row*mat->cols;
1653 ptr = mat->data.ptr + (size_t)row*mat->step + col*pix_size;
1656 else if( CV_IS_IMAGE_HDR( arr ))
1658 IplImage* img = (IplImage*)arr;
1659 int width = !img->roi ? img->width : img->roi->width;
1660 int y = idx/width, x = idx - y*width;
1662 ptr = cvPtr2D( arr, y, x, _type );
1664 else if( CV_IS_MATND( arr ))
1666 CvMatND* mat = (CvMatND*)arr;
1667 int j, type = CV_MAT_TYPE(mat->type);
1668 size_t size = mat->dim[0].size;
1673 for( j = 1; j < mat->dims; j++ )
1674 size *= mat->dim[j].size;
1676 if((unsigned)idx >= (unsigned)size )
1677 CV_Error( CV_StsOutOfRange, "index is out of range" );
1679 if( CV_IS_MAT_CONT(mat->type))
1681 int pix_size = CV_ELEM_SIZE(type);
1682 ptr = mat->data.ptr + (size_t)idx*pix_size;
1686 ptr = mat->data.ptr;
1687 for( j = mat->dims - 1; j >= 0; j-- )
1689 int sz = mat->dim[j].size;
1693 ptr += (idx - t*sz)*mat->dim[j].step;
1699 else if( CV_IS_SPARSE_MAT( arr ))
1701 CvSparseMat* m = (CvSparseMat*)arr;
1703 ptr = icvGetNodePtr( (CvSparseMat*)arr, &idx, _type, 1, 0 );
1707 int* _idx = (int*)cvStackAlloc(n*sizeof(_idx[0]));
1709 for( i = n - 1; i >= 0; i-- )
1711 int t = idx / m->size[i];
1712 _idx[i] = idx - t*m->size[i];
1715 ptr = icvGetNodePtr( (CvSparseMat*)arr, _idx, _type, 1, 0 );
1720 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
1727 // Returns pointer to specified element of 2d array
1729 cvPtr2D( const CvArr* arr, int y, int x, int* _type )
1732 if( CV_IS_MAT( arr ))
1734 CvMat* mat = (CvMat*)arr;
1737 if( (unsigned)y >= (unsigned)(mat->rows) ||
1738 (unsigned)x >= (unsigned)(mat->cols) )
1739 CV_Error( CV_StsOutOfRange, "index is out of range" );
1741 type = CV_MAT_TYPE(mat->type);
1745 ptr = mat->data.ptr + (size_t)y*mat->step + x*CV_ELEM_SIZE(type);
1747 else if( CV_IS_IMAGE( arr ))
1749 IplImage* img = (IplImage*)arr;
1750 int pix_size = (img->depth & 255) >> 3;
1752 ptr = (uchar*)img->imageData;
1754 if( img->dataOrder == 0 )
1755 pix_size *= img->nChannels;
1759 width = img->roi->width;
1760 height = img->roi->height;
1762 ptr += img->roi->yOffset*img->widthStep +
1763 img->roi->xOffset*pix_size;
1765 if( img->dataOrder )
1767 int coi = img->roi->coi;
1769 CV_Error( CV_BadCOI,
1770 "COI must be non-null in case of planar images" );
1771 ptr += (coi - 1)*img->imageSize;
1777 height = img->height;
1780 if( (unsigned)y >= (unsigned)height ||
1781 (unsigned)x >= (unsigned)width )
1782 CV_Error( CV_StsOutOfRange, "index is out of range" );
1784 ptr += y*img->widthStep + x*pix_size;
1788 int type = IplToCvDepth(img->depth);
1789 if( type < 0 || (unsigned)(img->nChannels - 1) > 3 )
1790 CV_Error( CV_StsUnsupportedFormat, "" );
1792 *_type = CV_MAKETYPE( type, img->nChannels );
1795 else if( CV_IS_MATND( arr ))
1797 CvMatND* mat = (CvMatND*)arr;
1799 if( mat->dims != 2 ||
1800 (unsigned)y >= (unsigned)(mat->dim[0].size) ||
1801 (unsigned)x >= (unsigned)(mat->dim[1].size) )
1802 CV_Error( CV_StsOutOfRange, "index is out of range" );
1804 ptr = mat->data.ptr + (size_t)y*mat->dim[0].step + x*mat->dim[1].step;
1806 *_type = CV_MAT_TYPE(mat->type);
1808 else if( CV_IS_SPARSE_MAT( arr ))
1810 int idx[] = { y, x };
1811 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, _type, 1, 0 );
1815 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
1822 // Returns pointer to specified element of 3d array
1824 cvPtr3D( const CvArr* arr, int z, int y, int x, int* _type )
1827 if( CV_IS_MATND( arr ))
1829 CvMatND* mat = (CvMatND*)arr;
1831 if( mat->dims != 3 ||
1832 (unsigned)z >= (unsigned)(mat->dim[0].size) ||
1833 (unsigned)y >= (unsigned)(mat->dim[1].size) ||
1834 (unsigned)x >= (unsigned)(mat->dim[2].size) )
1835 CV_Error( CV_StsOutOfRange, "index is out of range" );
1837 ptr = mat->data.ptr + (size_t)z*mat->dim[0].step +
1838 (size_t)y*mat->dim[1].step + x*mat->dim[2].step;
1841 *_type = CV_MAT_TYPE(mat->type);
1843 else if( CV_IS_SPARSE_MAT( arr ))
1845 int idx[] = { z, y, x };
1846 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, _type, 1, 0 );
1850 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
1857 // Returns pointer to specified element of n-d array
1859 cvPtrND( const CvArr* arr, const int* idx, int* _type,
1860 int create_node, unsigned* precalc_hashval )
1864 CV_Error( CV_StsNullPtr, "NULL pointer to indices" );
1866 if( CV_IS_SPARSE_MAT( arr ))
1867 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx,
1868 _type, create_node, precalc_hashval );
1869 else if( CV_IS_MATND( arr ))
1871 CvMatND* mat = (CvMatND*)arr;
1873 ptr = mat->data.ptr;
1875 for( i = 0; i < mat->dims; i++ )
1877 if( (unsigned)idx[i] >= (unsigned)(mat->dim[i].size) )
1878 CV_Error( CV_StsOutOfRange, "index is out of range" );
1879 ptr += (size_t)idx[i]*mat->dim[i].step;
1883 *_type = CV_MAT_TYPE(mat->type);
1885 else if( CV_IS_MAT_HDR(arr) || CV_IS_IMAGE_HDR(arr) )
1886 ptr = cvPtr2D( arr, idx[0], idx[1], _type );
1888 CV_Error( CV_StsBadArg, "unrecognized or unsupported array type" );
1894 // Returns specifed element of n-D array given linear index
1896 cvGet1D( const CvArr* arr, int idx )
1898 CvScalar scalar = {{0,0,0,0}};
1902 if( CV_IS_MAT( arr ) && CV_IS_MAT_CONT( ((CvMat*)arr)->type ))
1904 CvMat* mat = (CvMat*)arr;
1906 type = CV_MAT_TYPE(mat->type);
1907 int pix_size = CV_ELEM_SIZE(type);
1909 // the first part is mul-free sufficient check
1910 // that the index is within the matrix
1911 if( (unsigned)idx >= (unsigned)(mat->rows + mat->cols - 1) &&
1912 (unsigned)idx >= (unsigned)(mat->rows*mat->cols))
1913 CV_Error( CV_StsOutOfRange, "index is out of range" );
1915 ptr = mat->data.ptr + (size_t)idx*pix_size;
1917 else if( !CV_IS_SPARSE_MAT( arr ) || ((CvSparseMat*)arr)->dims > 1 )
1918 ptr = cvPtr1D( arr, idx, &type );
1920 ptr = icvGetNodePtr( (CvSparseMat*)arr, &idx, &type, 0, 0 );
1923 cvRawDataToScalar( ptr, type, &scalar );
1929 // Returns specifed element of 2D array
1931 cvGet2D( const CvArr* arr, int y, int x )
1933 CvScalar scalar = {{0,0,0,0}};
1937 if( CV_IS_MAT( arr ))
1939 CvMat* mat = (CvMat*)arr;
1941 if( (unsigned)y >= (unsigned)(mat->rows) ||
1942 (unsigned)x >= (unsigned)(mat->cols) )
1943 CV_Error( CV_StsOutOfRange, "index is out of range" );
1945 type = CV_MAT_TYPE(mat->type);
1946 ptr = mat->data.ptr + (size_t)y*mat->step + x*CV_ELEM_SIZE(type);
1948 else if( !CV_IS_SPARSE_MAT( arr ))
1949 ptr = cvPtr2D( arr, y, x, &type );
1952 int idx[] = { y, x };
1953 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, 0, 0 );
1957 cvRawDataToScalar( ptr, type, &scalar );
1963 // Returns specifed element of 3D array
1965 cvGet3D( const CvArr* arr, int z, int y, int x )
1967 CvScalar scalar = {{0,0,0,0}};
1971 if( !CV_IS_SPARSE_MAT( arr ))
1972 ptr = cvPtr3D( arr, z, y, x, &type );
1975 int idx[] = { z, y, x };
1976 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, 0, 0 );
1980 cvRawDataToScalar( ptr, type, &scalar );
1985 // Returns specifed element of nD array
1987 cvGetND( const CvArr* arr, const int* idx )
1989 CvScalar scalar = {{0,0,0,0}};
1993 if( !CV_IS_SPARSE_MAT( arr ))
1994 ptr = cvPtrND( arr, idx, &type );
1996 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, 0, 0 );
1999 cvRawDataToScalar( ptr, type, &scalar );
2005 // Returns specifed element of n-D array given linear index
2007 cvGetReal1D( const CvArr* arr, int idx )
2013 if( CV_IS_MAT( arr ) && CV_IS_MAT_CONT( ((CvMat*)arr)->type ))
2015 CvMat* mat = (CvMat*)arr;
2017 type = CV_MAT_TYPE(mat->type);
2018 int pix_size = CV_ELEM_SIZE(type);
2020 // the first part is mul-free sufficient check
2021 // that the index is within the matrix
2022 if( (unsigned)idx >= (unsigned)(mat->rows + mat->cols - 1) &&
2023 (unsigned)idx >= (unsigned)(mat->rows*mat->cols))
2024 CV_Error( CV_StsOutOfRange, "index is out of range" );
2026 ptr = mat->data.ptr + (size_t)idx*pix_size;
2028 else if( !CV_IS_SPARSE_MAT( arr ) || ((CvSparseMat*)arr)->dims > 1 )
2029 ptr = cvPtr1D( arr, idx, &type );
2031 ptr = icvGetNodePtr( (CvSparseMat*)arr, &idx, &type, 0, 0 );
2035 if( CV_MAT_CN( type ) > 1 )
2036 CV_Error( CV_BadNumChannels, "cvGetReal* support only single-channel arrays" );
2038 value = icvGetReal( ptr, type );
2044 // Returns specifed element of 2D array
2046 cvGetReal2D( const CvArr* arr, int y, int x )
2052 if( CV_IS_MAT( arr ))
2054 CvMat* mat = (CvMat*)arr;
2056 if( (unsigned)y >= (unsigned)(mat->rows) ||
2057 (unsigned)x >= (unsigned)(mat->cols) )
2058 CV_Error( CV_StsOutOfRange, "index is out of range" );
2060 type = CV_MAT_TYPE(mat->type);
2061 ptr = mat->data.ptr + (size_t)y*mat->step + x*CV_ELEM_SIZE(type);
2063 else if( !CV_IS_SPARSE_MAT( arr ))
2064 ptr = cvPtr2D( arr, y, x, &type );
2067 int idx[] = { y, x };
2068 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, 0, 0 );
2073 if( CV_MAT_CN( type ) > 1 )
2074 CV_Error( CV_BadNumChannels, "cvGetReal* support only single-channel arrays" );
2076 value = icvGetReal( ptr, type );
2083 // Returns specifed element of 3D array
2085 cvGetReal3D( const CvArr* arr, int z, int y, int x )
2091 if( !CV_IS_SPARSE_MAT( arr ))
2092 ptr = cvPtr3D( arr, z, y, x, &type );
2095 int idx[] = { z, y, x };
2096 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, 0, 0 );
2101 if( CV_MAT_CN( type ) > 1 )
2102 CV_Error( CV_BadNumChannels, "cvGetReal* support only single-channel arrays" );
2104 value = icvGetReal( ptr, type );
2111 // Returns specifed element of nD array
2113 cvGetRealND( const CvArr* arr, const int* idx )
2119 if( !CV_IS_SPARSE_MAT( arr ))
2120 ptr = cvPtrND( arr, idx, &type );
2122 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, 0, 0 );
2126 if( CV_MAT_CN( type ) > 1 )
2127 CV_Error( CV_BadNumChannels, "cvGetReal* support only single-channel arrays" );
2129 value = icvGetReal( ptr, type );
2136 // Assigns new value to specifed element of nD array given linear index
2138 cvSet1D( CvArr* arr, int idx, CvScalar scalar )
2143 if( CV_IS_MAT( arr ) && CV_IS_MAT_CONT( ((CvMat*)arr)->type ))
2145 CvMat* mat = (CvMat*)arr;
2147 type = CV_MAT_TYPE(mat->type);
2148 int pix_size = CV_ELEM_SIZE(type);
2150 // the first part is mul-free sufficient check
2151 // that the index is within the matrix
2152 if( (unsigned)idx >= (unsigned)(mat->rows + mat->cols - 1) &&
2153 (unsigned)idx >= (unsigned)(mat->rows*mat->cols))
2154 CV_Error( CV_StsOutOfRange, "index is out of range" );
2156 ptr = mat->data.ptr + (size_t)idx*pix_size;
2158 else if( !CV_IS_SPARSE_MAT( arr ) || ((CvSparseMat*)arr)->dims > 1 )
2159 ptr = cvPtr1D( arr, idx, &type );
2161 ptr = icvGetNodePtr( (CvSparseMat*)arr, &idx, &type, -1, 0 );
2163 cvScalarToRawData( &scalar, ptr, type );
2167 // Assigns new value to specifed element of 2D array
2169 cvSet2D( CvArr* arr, int y, int x, CvScalar scalar )
2174 if( CV_IS_MAT( arr ))
2176 CvMat* mat = (CvMat*)arr;
2178 if( (unsigned)y >= (unsigned)(mat->rows) ||
2179 (unsigned)x >= (unsigned)(mat->cols) )
2180 CV_Error( CV_StsOutOfRange, "index is out of range" );
2182 type = CV_MAT_TYPE(mat->type);
2183 ptr = mat->data.ptr + (size_t)y*mat->step + x*CV_ELEM_SIZE(type);
2185 else if( !CV_IS_SPARSE_MAT( arr ))
2186 ptr = cvPtr2D( arr, y, x, &type );
2189 int idx[] = { y, x };
2190 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, -1, 0 );
2192 cvScalarToRawData( &scalar, ptr, type );
2196 // Assigns new value to specifed element of 3D array
2198 cvSet3D( CvArr* arr, int z, int y, int x, CvScalar scalar )
2203 if( !CV_IS_SPARSE_MAT( arr ))
2204 ptr = cvPtr3D( arr, z, y, x, &type );
2207 int idx[] = { z, y, x };
2208 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, -1, 0 );
2210 cvScalarToRawData( &scalar, ptr, type );
2214 // Assigns new value to specifed element of nD array
2216 cvSetND( CvArr* arr, const int* idx, CvScalar scalar )
2221 if( !CV_IS_SPARSE_MAT( arr ))
2222 ptr = cvPtrND( arr, idx, &type );
2224 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, -1, 0 );
2225 cvScalarToRawData( &scalar, ptr, type );
2230 cvSetReal1D( CvArr* arr, int idx, double value )
2235 if( CV_IS_MAT( arr ) && CV_IS_MAT_CONT( ((CvMat*)arr)->type ))
2237 CvMat* mat = (CvMat*)arr;
2239 type = CV_MAT_TYPE(mat->type);
2240 int pix_size = CV_ELEM_SIZE(type);
2242 // the first part is mul-free sufficient check
2243 // that the index is within the matrix
2244 if( (unsigned)idx >= (unsigned)(mat->rows + mat->cols - 1) &&
2245 (unsigned)idx >= (unsigned)(mat->rows*mat->cols))
2246 CV_Error( CV_StsOutOfRange, "index is out of range" );
2248 ptr = mat->data.ptr + (size_t)idx*pix_size;
2250 else if( !CV_IS_SPARSE_MAT( arr ) || ((CvSparseMat*)arr)->dims > 1 )
2251 ptr = cvPtr1D( arr, idx, &type );
2253 ptr = icvGetNodePtr( (CvSparseMat*)arr, &idx, &type, -1, 0 );
2255 if( CV_MAT_CN( type ) > 1 )
2256 CV_Error( CV_BadNumChannels, "cvSetReal* support only single-channel arrays" );
2259 icvSetReal( value, ptr, type );
2264 cvSetReal2D( CvArr* arr, int y, int x, double value )
2269 if( CV_IS_MAT( arr ))
2271 CvMat* mat = (CvMat*)arr;
2273 if( (unsigned)y >= (unsigned)(mat->rows) ||
2274 (unsigned)x >= (unsigned)(mat->cols) )
2275 CV_Error( CV_StsOutOfRange, "index is out of range" );
2277 type = CV_MAT_TYPE(mat->type);
2278 ptr = mat->data.ptr + (size_t)y*mat->step + x*CV_ELEM_SIZE(type);
2280 else if( !CV_IS_SPARSE_MAT( arr ))
2282 ptr = cvPtr2D( arr, y, x, &type );
2286 int idx[] = { y, x };
2287 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, -1, 0 );
2289 if( CV_MAT_CN( type ) > 1 )
2290 CV_Error( CV_BadNumChannels, "cvSetReal* support only single-channel arrays" );
2293 icvSetReal( value, ptr, type );
2298 cvSetReal3D( CvArr* arr, int z, int y, int x, double value )
2303 if( !CV_IS_SPARSE_MAT( arr ))
2304 ptr = cvPtr3D( arr, z, y, x, &type );
2307 int idx[] = { z, y, x };
2308 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, -1, 0 );
2310 if( CV_MAT_CN( type ) > 1 )
2311 CV_Error( CV_BadNumChannels, "cvSetReal* support only single-channel arrays" );
2314 icvSetReal( value, ptr, type );
2319 cvSetRealND( CvArr* arr, const int* idx, double value )
2324 if( !CV_IS_SPARSE_MAT( arr ))
2325 ptr = cvPtrND( arr, idx, &type );
2327 ptr = icvGetNodePtr( (CvSparseMat*)arr, idx, &type, -1, 0 );
2329 if( CV_MAT_CN( type ) > 1 )
2330 CV_Error( CV_BadNumChannels, "cvSetReal* support only single-channel arrays" );
2333 icvSetReal( value, ptr, type );
2338 cvClearND( CvArr* arr, const int* idx )
2340 if( !CV_IS_SPARSE_MAT( arr ))
2344 ptr = cvPtrND( arr, idx, &type );
2346 CV_ZERO_CHAR( ptr, CV_ELEM_SIZE(type) );
2349 icvDeleteNode( (CvSparseMat*)arr, idx, 0 );
2353 /****************************************************************************************\
2354 * Conversion to CvMat or IplImage *
2355 \****************************************************************************************/
2357 // convert array (CvMat or IplImage) to CvMat
2359 cvGetMat( const CvArr* array, CvMat* mat,
2360 int* pCOI, int allowND )
2363 CvMat* src = (CvMat*)array;
2367 CV_Error( CV_StsNullPtr, "NULL array pointer is passed" );
2369 if( CV_IS_MAT_HDR(src))
2371 if( !src->data.ptr )
2372 CV_Error( CV_StsNullPtr, "The matrix has NULL data pointer" );
2374 result = (CvMat*)src;
2376 else if( CV_IS_IMAGE_HDR(src) )
2378 const IplImage* img = (const IplImage*)src;
2381 if( img->imageData == 0 )
2382 CV_Error( CV_StsNullPtr, "The image has NULL data pointer" );
2384 depth = IplToCvDepth( img->depth );
2386 CV_Error( CV_BadDepth, "" );
2388 order = img->dataOrder & (img->nChannels > 1 ? -1 : 0);
2392 if( order == IPL_DATA_ORDER_PLANE )
2396 if( img->roi->coi == 0 )
2397 CV_Error( CV_StsBadFlag,
2398 "Images with planar data layout should be used with COI selected" );
2400 cvInitMatHeader( mat, img->roi->height,
2401 img->roi->width, type,
2402 img->imageData + (img->roi->coi-1)*img->imageSize +
2403 img->roi->yOffset*img->widthStep +
2404 img->roi->xOffset*CV_ELEM_SIZE(type),
2407 else /* pixel order */
2409 int type = CV_MAKETYPE( depth, img->nChannels );
2410 coi = img->roi->coi;
2412 if( img->nChannels > CV_CN_MAX )
2413 CV_Error( CV_BadNumChannels,
2414 "The image is interleaved and has over CV_CN_MAX channels" );
2416 cvInitMatHeader( mat, img->roi->height, img->roi->width,
2417 type, img->imageData +
2418 img->roi->yOffset*img->widthStep +
2419 img->roi->xOffset*CV_ELEM_SIZE(type),
2425 int type = CV_MAKETYPE( depth, img->nChannels );
2427 if( order != IPL_DATA_ORDER_PIXEL )
2428 CV_Error( CV_StsBadFlag, "Pixel order should be used with coi == 0" );
2430 cvInitMatHeader( mat, img->height, img->width, type,
2431 img->imageData, img->widthStep );
2436 else if( allowND && CV_IS_MATND_HDR(src) )
2438 CvMatND* matnd = (CvMatND*)src;
2440 int size1 = matnd->dim[0].size, size2 = 1;
2442 if( !src->data.ptr )
2443 CV_Error( CV_StsNullPtr, "Input array has NULL data pointer" );
2445 if( !CV_IS_MAT_CONT( matnd->type ))
2446 CV_Error( CV_StsBadArg, "Only continuous nD arrays are supported here" );
2448 if( matnd->dims > 2 )
2449 for( i = 1; i < matnd->dims; i++ )
2450 size2 *= matnd->dim[i].size;
2452 size2 = matnd->dims == 1 ? 1 : matnd->dim[1].size;
2455 mat->hdr_refcount = 0;
2456 mat->data.ptr = matnd->data.ptr;
2459 mat->type = CV_MAT_TYPE(matnd->type) | CV_MAT_MAGIC_VAL | CV_MAT_CONT_FLAG;
2460 mat->step = size2*CV_ELEM_SIZE(matnd->type);
2461 mat->step &= size1 > 1 ? -1 : 0;
2463 icvCheckHuge( mat );
2467 CV_Error( CV_StsBadFlag, "Unrecognized or unsupported array type" );
2477 cvReshapeMatND( const CvArr* arr,
2478 int sizeof_header, CvArr* _header,
2479 int new_cn, int new_dims, int* new_sizes )
2484 if( !arr || !_header )
2485 CV_Error( CV_StsNullPtr, "NULL pointer to array or destination header" );
2487 if( new_cn == 0 && new_dims == 0 )
2488 CV_Error( CV_StsBadArg, "None of array parameters is changed: dummy call?" );
2490 dims = cvGetDims( arr );
2497 else if( new_dims == 1 )
2503 if( new_dims <= 0 || new_dims > CV_MAX_DIM )
2504 CV_Error( CV_StsOutOfRange, "Non-positive or too large number of dimensions" );
2506 CV_Error( CV_StsNullPtr, "New dimension sizes are not specified" );
2511 CvMat* mat = (CvMat*)arr;
2512 CvMat* header = (CvMat*)_header;
2514 int hdr_refcount = 0;
2515 int total_width, new_rows, cn;
2517 if( sizeof_header != sizeof(CvMat))
2518 CV_Error( CV_StsBadArg, "The header should be CvMat" );
2522 refcount = mat->refcount;
2523 hdr_refcount = mat->hdr_refcount;
2525 else if( !CV_IS_MAT( mat ))
2526 mat = cvGetMat( mat, header, &coi, 1 );
2528 cn = CV_MAT_CN( mat->type );
2529 total_width = mat->cols * cn;
2535 new_rows = new_sizes[0];
2536 else if( new_dims == 1 )
2537 new_rows = total_width*mat->rows/new_cn;
2540 new_rows = mat->rows;
2541 if( new_cn > total_width )
2542 new_rows = mat->rows * total_width / new_cn;
2545 if( new_rows != mat->rows )
2547 int total_size = total_width * mat->rows;
2549 if( !CV_IS_MAT_CONT( mat->type ))
2550 CV_Error( CV_BadStep,
2551 "The matrix is not continuous so the number of rows can not be changed" );
2553 total_width = total_size / new_rows;
2555 if( total_width * new_rows != total_size )
2556 CV_Error( CV_StsBadArg, "The total number of matrix elements "
2557 "is not divisible by the new number of rows" );
2560 header->rows = new_rows;
2561 header->cols = total_width / new_cn;
2563 if( header->cols * new_cn != total_width ||
2564 (new_sizes && header->cols != new_sizes[1]) )
2565 CV_Error( CV_StsBadArg, "The total matrix width is not "
2566 "divisible by the new number of columns" );
2568 header->type = (mat->type & ~CV_MAT_TYPE_MASK) | CV_MAKETYPE(mat->type, new_cn);
2569 header->step = header->cols * CV_ELEM_SIZE(mat->type);
2570 header->step &= new_rows > 1 ? -1 : 0;
2571 header->refcount = refcount;
2572 header->hdr_refcount = hdr_refcount;
2576 CvMatND* header = (CvMatND*)_header;
2578 if( sizeof_header != sizeof(CvMatND))
2579 CV_Error( CV_StsBadSize, "The header should be CvMatND" );
2583 if( !CV_IS_MATND( arr ))
2584 CV_Error( CV_StsBadArg, "The source array must be CvMatND" );
2587 CvMatND* mat = (CvMatND*)arr;
2588 assert( new_cn > 0 );
2589 int last_dim_size = mat->dim[mat->dims-1].size*CV_MAT_CN(mat->type);
2590 int new_size = last_dim_size/new_cn;
2592 if( new_size*new_cn != last_dim_size )
2593 CV_Error( CV_StsBadArg,
2594 "The last dimension full size is not divisible by new number of channels");
2598 memcpy( header, mat, sizeof(*header));
2599 header->refcount = 0;
2600 header->hdr_refcount = 0;
2603 header->dim[header->dims-1].size = new_size;
2604 header->type = (header->type & ~CV_MAT_TYPE_MASK) | CV_MAKETYPE(header->type, new_cn);
2610 CvMatND* mat = (CvMatND*)arr;
2611 int i, size1, size2;
2615 CV_Error( CV_StsBadArg,
2616 "Simultaneous change of shape and number of channels is not supported. "
2617 "Do it by 2 separate calls" );
2619 if( !CV_IS_MATND( mat ))
2621 cvGetMatND( mat, &stub, &coi );
2625 if( CV_IS_MAT_CONT( mat->type ))
2626 CV_Error( CV_StsBadArg, "Non-continuous nD arrays are not supported" );
2628 size1 = mat->dim[0].size;
2629 for( i = 1; i < dims; i++ )
2630 size1 *= mat->dim[i].size;
2633 for( i = 0; i < new_dims; i++ )
2635 if( new_sizes[i] <= 0 )
2636 CV_Error( CV_StsBadSize,
2637 "One of new dimension sizes is non-positive" );
2638 size2 *= new_sizes[i];
2641 if( size1 != size2 )
2642 CV_Error( CV_StsBadSize,
2643 "Number of elements in the original and reshaped array is different" );
2647 header->refcount = 0;
2648 header->hdr_refcount = 0;
2651 header->dims = new_dims;
2652 header->type = mat->type;
2653 header->data.ptr = mat->data.ptr;
2654 step = CV_ELEM_SIZE(header->type);
2656 for( i = new_dims - 1; i >= 0; i-- )
2658 header->dim[i].size = new_sizes[i];
2659 header->dim[i].step = step;
2660 step *= new_sizes[i];
2666 CV_Error( CV_BadCOI, "COI is not supported by this operation" );
2674 cvReshape( const CvArr* array, CvMat* header,
2675 int new_cn, int new_rows )
2678 CvMat *mat = (CvMat*)array;
2679 int total_width, new_width;
2682 CV_Error( CV_StsNullPtr, "" );
2684 if( !CV_IS_MAT( mat ))
2687 mat = cvGetMat( mat, header, &coi, 1 );
2689 CV_Error( CV_BadCOI, "COI is not supported" );
2693 new_cn = CV_MAT_CN(mat->type);
2694 else if( (unsigned)(new_cn - 1) > 3 )
2695 CV_Error( CV_BadNumChannels, "" );
2699 int hdr_refcount = header->hdr_refcount;
2701 header->refcount = 0;
2702 header->hdr_refcount = hdr_refcount;
2705 total_width = mat->cols * CV_MAT_CN( mat->type );
2707 if( (new_cn > total_width || total_width % new_cn != 0) && new_rows == 0 )
2708 new_rows = mat->rows * total_width / new_cn;
2710 if( new_rows == 0 || new_rows == mat->rows )
2712 header->rows = mat->rows;
2713 header->step = mat->step;
2717 int total_size = total_width * mat->rows;
2718 if( !CV_IS_MAT_CONT( mat->type ))
2719 CV_Error( CV_BadStep,
2720 "The matrix is not continuous, thus its number of rows can not be changed" );
2722 if( (unsigned)new_rows > (unsigned)total_size )
2723 CV_Error( CV_StsOutOfRange, "Bad new number of rows" );
2725 total_width = total_size / new_rows;
2727 if( total_width * new_rows != total_size )
2728 CV_Error( CV_StsBadArg, "The total number of matrix elements "
2729 "is not divisible by the new number of rows" );
2731 header->rows = new_rows;
2732 header->step = total_width * CV_ELEM_SIZE1(mat->type);
2735 new_width = total_width / new_cn;
2737 if( new_width * new_cn != total_width )
2738 CV_Error( CV_BadNumChannels,
2739 "The total width is not divisible by the new number of channels" );
2741 header->cols = new_width;
2742 header->type = (mat->type & ~CV_MAT_TYPE_MASK) | CV_MAKETYPE(mat->type, new_cn);
2749 // convert array (CvMat or IplImage) to IplImage
2751 cvGetImage( const CvArr* array, IplImage* img )
2753 IplImage* result = 0;
2754 const IplImage* src = (const IplImage*)array;
2758 CV_Error( CV_StsNullPtr, "" );
2760 if( !CV_IS_IMAGE_HDR(src) )
2762 const CvMat* mat = (const CvMat*)src;
2764 if( !CV_IS_MAT_HDR(mat))
2765 CV_Error( CV_StsBadFlag, "" );
2767 if( mat->data.ptr == 0 )
2768 CV_Error( CV_StsNullPtr, "" );
2770 depth = cvIplDepth(mat->type);
2772 cvInitImageHeader( img, cvSize(mat->cols, mat->rows),
2773 depth, CV_MAT_CN(mat->type) );
2774 cvSetData( img, mat->data.ptr, mat->step );
2780 result = (IplImage*)src;
2787 /****************************************************************************************\
2788 * IplImage-specific functions *
2789 \****************************************************************************************/
2791 static IplROI* icvCreateROI( int coi, int xOffset, int yOffset, int width, int height )
2794 if( !CvIPL.createROI )
2796 roi = (IplROI*)cvAlloc( sizeof(*roi));
2799 roi->xOffset = xOffset;
2800 roi->yOffset = yOffset;
2802 roi->height = height;
2806 roi = CvIPL.createROI( coi, xOffset, yOffset, width, height );
2813 icvGetColorModel( int nchannels, const char** colorModel, const char** channelSeq )
2815 static const char* tab[][2] =
2824 *colorModel = *channelSeq = "";
2826 if( (unsigned)nchannels <= 3 )
2828 *colorModel = tab[nchannels][0];
2829 *channelSeq = tab[nchannels][1];
2834 // create IplImage header
2836 cvCreateImageHeader( CvSize size, int depth, int channels )
2840 if( !CvIPL.createHeader )
2842 img = (IplImage *)cvAlloc( sizeof( *img ));
2843 cvInitImageHeader( img, size, depth, channels, IPL_ORIGIN_TL,
2844 CV_DEFAULT_IMAGE_ROW_ALIGN );
2848 const char *colorModel, *channelSeq;
2850 icvGetColorModel( channels, &colorModel, &channelSeq );
2852 img = CvIPL.createHeader( channels, 0, depth, (char*)colorModel, (char*)channelSeq,
2853 IPL_DATA_ORDER_PIXEL, IPL_ORIGIN_TL,
2854 CV_DEFAULT_IMAGE_ROW_ALIGN,
2855 size.width, size.height, 0, 0, 0, 0 );
2862 // create IplImage header and allocate underlying data
2864 cvCreateImage( CvSize size, int depth, int channels )
2866 IplImage *img = cvCreateImageHeader( size, depth, channels );
2868 cvCreateData( img );
2874 // initalize IplImage header, allocated by the user
2876 cvInitImageHeader( IplImage * image, CvSize size, int depth,
2877 int channels, int origin, int align )
2879 const char *colorModel, *channelSeq;
2882 CV_Error( CV_HeaderIsNull, "null pointer to header" );
2884 memset( image, 0, sizeof( *image ));
2885 image->nSize = sizeof( *image );
2887 icvGetColorModel( channels, &colorModel, &channelSeq );
2888 strncpy( image->colorModel, colorModel, 4 );
2889 strncpy( image->channelSeq, channelSeq, 4 );
2891 if( size.width < 0 || size.height < 0 )
2892 CV_Error( CV_BadROISize, "Bad input roi" );
2894 if( (depth != (int)IPL_DEPTH_1U && depth != (int)IPL_DEPTH_8U &&
2895 depth != (int)IPL_DEPTH_8S && depth != (int)IPL_DEPTH_16U &&
2896 depth != (int)IPL_DEPTH_16S && depth != (int)IPL_DEPTH_32S &&
2897 depth != (int)IPL_DEPTH_32F && depth != (int)IPL_DEPTH_64F) ||
2899 CV_Error( CV_BadDepth, "Unsupported format" );
2900 if( origin != CV_ORIGIN_BL && origin != CV_ORIGIN_TL )
2901 CV_Error( CV_BadOrigin, "Bad input origin" );
2903 if( align != 4 && align != 8 )
2904 CV_Error( CV_BadAlign, "Bad input align" );
2906 image->width = size.width;
2907 image->height = size.height;
2911 image->roi->coi = 0;
2912 image->roi->xOffset = image->roi->yOffset = 0;
2913 image->roi->width = size.width;
2914 image->roi->height = size.height;
2917 image->nChannels = MAX( channels, 1 );
2918 image->depth = depth;
2919 image->align = align;
2920 image->widthStep = (((image->width * image->nChannels *
2921 (image->depth & ~IPL_DEPTH_SIGN) + 7)/8)+ align - 1) & (~(align - 1));
2922 image->origin = origin;
2923 image->imageSize = image->widthStep * image->height;
2930 cvReleaseImageHeader( IplImage** image )
2933 CV_Error( CV_StsNullPtr, "" );
2937 IplImage* img = *image;
2940 if( !CvIPL.deallocate )
2942 cvFree( &img->roi );
2947 CvIPL.deallocate( img, IPL_IMAGE_HEADER | IPL_IMAGE_ROI );
2954 cvReleaseImage( IplImage ** image )
2957 CV_Error( CV_StsNullPtr, "" );
2961 IplImage* img = *image;
2964 cvReleaseData( img );
2965 cvReleaseImageHeader( &img );
2971 cvSetImageROI( IplImage* image, CvRect rect )
2974 CV_Error( CV_HeaderIsNull, "" );
2976 if( rect.x > image->width || rect.y > image->height )
2977 CV_Error( CV_BadROISize, "" );
2979 if( rect.x + rect.width < 0 || rect.y + rect.height < 0 )
2980 CV_Error( CV_BadROISize, "" );
2984 rect.width += rect.x;
2990 rect.height += rect.y;
2994 if( rect.x + rect.width > image->width )
2995 rect.width = image->width - rect.x;
2997 if( rect.y + rect.height > image->height )
2998 rect.height = image->height - rect.y;
3002 image->roi->xOffset = rect.x;
3003 image->roi->yOffset = rect.y;
3004 image->roi->width = rect.width;
3005 image->roi->height = rect.height;
3008 image->roi = icvCreateROI( 0, rect.x, rect.y, rect.width, rect.height );
3013 cvResetImageROI( IplImage* image )
3016 CV_Error( CV_HeaderIsNull, "" );
3020 if( !CvIPL.deallocate )
3022 cvFree( &image->roi );
3026 CvIPL.deallocate( image, IPL_IMAGE_ROI );
3034 cvGetImageROI( const IplImage* img )
3036 CvRect rect = { 0, 0, 0, 0 };
3038 CV_Error( CV_StsNullPtr, "Null pointer to image" );
3041 rect = cvRect( img->roi->xOffset, img->roi->yOffset,
3042 img->roi->width, img->roi->height );
3044 rect = cvRect( 0, 0, img->width, img->height );
3051 cvSetImageCOI( IplImage* image, int coi )
3054 CV_Error( CV_HeaderIsNull, "" );
3056 if( (unsigned)coi > (unsigned)(image->nChannels) )
3057 CV_Error( CV_BadCOI, "" );
3059 if( image->roi || coi != 0 )
3063 image->roi->coi = coi;
3067 image->roi = icvCreateROI( coi, 0, 0, image->width, image->height );
3074 cvGetImageCOI( const IplImage* image )
3077 CV_Error( CV_HeaderIsNull, "" );
3079 return image->roi ? image->roi->coi : 0;
3084 cvCloneImage( const IplImage* src )
3088 if( !CV_IS_IMAGE_HDR( src ))
3089 CV_Error( CV_StsBadArg, "Bad image header" );
3091 if( !CvIPL.cloneImage )
3093 dst = (IplImage*)cvAlloc( sizeof(*dst));
3095 memcpy( dst, src, sizeof(*src));
3096 dst->imageData = dst->imageDataOrigin = 0;
3101 dst->roi = icvCreateROI( src->roi->coi, src->roi->xOffset,
3102 src->roi->yOffset, src->roi->width, src->roi->height );
3105 if( src->imageData )
3107 int size = src->imageSize;
3108 cvCreateData( dst );
3109 memcpy( dst->imageData, src->imageData, size );
3113 dst = CvIPL.cloneImage( src );
3119 /****************************************************************************************\
3120 * Additional operations on CvTermCriteria *
3121 \****************************************************************************************/
3123 CV_IMPL CvTermCriteria
3124 cvCheckTermCriteria( CvTermCriteria criteria, double default_eps,
3125 int default_max_iters )
3127 CvTermCriteria crit;
3129 crit.type = CV_TERMCRIT_ITER|CV_TERMCRIT_EPS;
3130 crit.max_iter = default_max_iters;
3131 crit.epsilon = (float)default_eps;
3133 if( (criteria.type & ~(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER)) != 0 )
3134 CV_Error( CV_StsBadArg,
3135 "Unknown type of term criteria" );
3137 if( (criteria.type & CV_TERMCRIT_ITER) != 0 )
3139 if( criteria.max_iter <= 0 )
3140 CV_Error( CV_StsBadArg,
3141 "Iterations flag is set and maximum number of iterations is <= 0" );
3142 crit.max_iter = criteria.max_iter;
3145 if( (criteria.type & CV_TERMCRIT_EPS) != 0 )
3147 if( criteria.epsilon < 0 )
3148 CV_Error( CV_StsBadArg, "Accuracy flag is set and epsilon is < 0" );
3150 crit.epsilon = criteria.epsilon;
3153 if( (criteria.type & (CV_TERMCRIT_EPS | CV_TERMCRIT_ITER)) == 0 )
3154 CV_Error( CV_StsBadArg,
3155 "Neither accuracy nor maximum iterations "
3156 "number flags are set in criteria type" );
3158 crit.epsilon = (float)MAX( 0, crit.epsilon );
3159 crit.max_iter = MAX( 1, crit.max_iter );