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.
44 CV_IMPL CvSeq* cvPointSeqFromMat( int seq_kind, const CvArr* arr,
45 CvContour* contour_header, CvSeqBlock* block )
49 CV_FUNCNAME( "cvPointSeqFromMat" );
51 assert( arr != 0 && contour_header != 0 && block != 0 );
56 CvMat* mat = (CvMat*)arr;
58 if( !CV_IS_MAT( mat ))
59 CV_ERROR( CV_StsBadArg, "Input array is not a valid matrix" );
61 eltype = CV_MAT_TYPE( mat->type );
62 if( eltype != CV_32SC2 && eltype != CV_32FC2 )
63 CV_ERROR( CV_StsUnsupportedFormat,
64 "The matrix can not be converted to point sequence because of "
65 "inappropriate element type" );
67 if( (mat->width != 1 && mat->height != 1) || !CV_IS_MAT_CONT(mat->type))
68 CV_ERROR( CV_StsBadArg,
69 "The matrix converted to point sequence must be "
70 "1-dimensional and continuous" );
72 CV_CALL( cvMakeSeqHeaderForArray(
73 (seq_kind & (CV_SEQ_KIND_MASK|CV_SEQ_FLAG_CLOSED)) | eltype,
74 sizeof(CvContour), CV_ELEM_SIZE(eltype), mat->data.ptr,
75 mat->width*mat->height, (CvSeq*)contour_header, block ));
77 contour = (CvSeq*)contour_header;
85 typedef CvStatus (CV_STDCALL * CvCopyNonConstBorderFunc)(
86 const void*, int, CvSize, void*, int, CvSize, int, int );
88 typedef CvStatus (CV_STDCALL * CvCopyNonConstBorderFuncI)(
89 const void*, int, CvSize, CvSize, int, int );
92 icvCopyReplicateBorder_8u( const uchar* src, int srcstep, CvSize srcroi,
93 uchar* dst, int dststep, CvSize dstroi,
94 int top, int left, int cn, const uchar* )
96 const int isz = (int)sizeof(int);
99 if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
101 const int* isrc = (const int*)src;
102 int* idst = (int*)dst;
112 for( i = 0; i < dstroi.height; i++, idst += dststep )
114 if( idst + left != isrc )
115 memcpy( idst + left, isrc, srcroi.width*sizeof(idst[0]) );
116 for( j = left - 1; j >= 0; j-- )
117 idst[j] = idst[j + cn];
118 for( j = left+srcroi.width; j < dstroi.width; j++ )
119 idst[j] = idst[j - cn];
120 if( i >= top && i < top + srcroi.height - 1 )
130 for( i = 0; i < dstroi.height; i++, dst += dststep )
132 if( dst + left != src )
133 memcpy( dst + left, src, srcroi.width );
134 for( j = left - 1; j >= 0; j-- )
135 dst[j] = dst[j + cn];
136 for( j = left+srcroi.width; j < dstroi.width; j++ )
137 dst[j] = dst[j - cn];
138 if( i >= top && i < top + srcroi.height - 1 )
147 static CvStatus CV_STDCALL
148 icvCopyReflect101Border_8u( const uchar* src, int srcstep, CvSize srcroi,
149 uchar* dst, int dststep, CvSize dstroi,
150 int top, int left, int cn )
152 const int isz = (int)sizeof(int);
153 int i, j, k, t, dj, tab_size, int_mode = 0;
154 const int* isrc = (const int*)src;
155 int* idst = (int*)dst, *tab;
157 if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
170 tab_size = dstroi.width - srcroi.width;
171 tab = (int*)cvStackAlloc( tab_size*sizeof(tab[0]) );
173 if( srcroi.width == 1 )
175 for( k = 0; k < cn; k++ )
176 for( i = 0; i < tab_size; i += cn )
177 tab[i + k] = k + left;
182 for( i = left - cn; i >= 0; i -= cn )
184 for( k = 0; k < cn; k++ )
185 tab[i + k] = j + k + left;
186 if( (unsigned)(j += dj) >= (unsigned)srcroi.width )
190 j = srcroi.width - cn*2;
192 for( i = left; i < tab_size; i += cn )
194 for( k = 0; k < cn; k++ )
195 tab[i + k] = j + k + left;
196 if( (unsigned)(j += dj) >= (unsigned)srcroi.width )
204 for( i = 0; i < srcroi.height; i++, isrc += srcstep, idst += dststep )
206 if( idst + left != isrc )
207 memcpy( idst + left, isrc, srcroi.width*sizeof(idst[0]) );
208 for( j = 0; j < left; j++ )
213 for( ; j < tab_size; j++ )
216 idst[j + srcroi.width] = idst[k];
219 isrc -= srcroi.height*srcstep;
220 idst -= (top + srcroi.height)*dststep;
225 for( i = 0; i < srcroi.height; i++, src += srcstep, dst += dststep )
227 if( dst + left != src )
228 memcpy( dst + left, src, srcroi.width );
229 for( j = 0; j < left; j++ )
234 for( ; j < tab_size; j++ )
237 dst[j + srcroi.width] = dst[k];
240 src -= srcroi.height*srcstep;
241 dst -= (top + srcroi.height)*dststep;
244 for( t = 0; t < 2; t++ )
248 i1 = top-1, i2 = 0, di = -1, j = 1, dj = 1;
250 i1 = top+srcroi.height, i2=dstroi.height, di = 1, j = srcroi.height-2, dj = -1;
252 for( i = i1; i != i2; i += di )
256 const int* s = idst + i*dststep;
257 int* d = idst + (j+top)*dststep;
258 for( k = 0; k < dstroi.width; k++ )
263 const uchar* s = dst + i*dststep;
264 uchar* d = dst + (j+top)*dststep;
265 for( k = 0; k < dstroi.width; k++ )
269 if( (unsigned)(j += dj) >= (unsigned)srcroi.height )
278 static CvStatus CV_STDCALL
279 icvCopyConstBorder_8u( const uchar* src, int srcstep, CvSize srcroi,
280 uchar* dst, int dststep, CvSize dstroi,
281 int top, int left, int cn, const uchar* value )
283 const int isz = (int)sizeof(int);
285 if( (cn | srcstep | dststep | (size_t)src | (size_t)dst | (size_t)value) % isz == 0 )
287 const int* isrc = (const int*)src;
288 int* idst = (int*)dst;
289 const int* ivalue = (const int*)value;
300 for( j = 1; j < cn; j++ )
301 if( ivalue[j] != ivalue[0] )
307 if( dstroi.width <= 0 )
310 for( i = 0; i < dstroi.height; i++, idst += dststep )
312 if( i < top || i >= top + srcroi.height )
316 for( j = 0; j < dstroi.width; j++ )
321 for( j = 0; j < cn; j++ )
323 for( ; j < dstroi.width; j++ )
324 idst[j] = idst[j - cn];
331 for( j = 0; j < left; j++ )
333 for( j = srcroi.width + left; j < dstroi.width; j++ )
338 for( k = 0; k < cn; k++ )
340 for( j = 0; j < left; j += cn )
341 idst[j+k] = ivalue[k];
342 for( j = srcroi.width + left; j < dstroi.width; j += cn )
343 idst[j+k] = ivalue[k];
347 if( idst + left != isrc )
348 for( j = 0; j < srcroi.width; j++ )
349 idst[j + left] = isrc[j];
361 for( j = 1; j < cn; j++ )
362 if( value[j] != value[0] )
368 if( dstroi.width <= 0 )
371 for( i = 0; i < dstroi.height; i++, dst += dststep )
373 if( i < top || i >= top + srcroi.height )
377 for( j = 0; j < dstroi.width; j++ )
382 for( j = 0; j < cn; j++ )
384 for( ; j < dstroi.width; j++ )
385 dst[j] = dst[j - cn];
392 for( j = 0; j < left; j++ )
394 for( j = srcroi.width + left; j < dstroi.width; j++ )
399 for( k = 0; k < cn; k++ )
401 for( j = 0; j < left; j += cn )
403 for( j = srcroi.width + left; j < dstroi.width; j += cn )
408 if( dst + left != src )
409 for( j = 0; j < srcroi.width; j++ )
410 dst[j + left] = src[j];
420 cvCopyMakeBorder( const CvArr* srcarr, CvArr* dstarr, CvPoint offset,
421 int bordertype, CvScalar value )
423 CV_FUNCNAME( "cvCopyMakeBorder" );
427 CvMat srcstub, *src = (CvMat*)srcarr;
428 CvMat dststub, *dst = (CvMat*)dstarr;
429 CvSize srcsize, dstsize;
430 int srcstep, dststep;
433 if( !CV_IS_MAT(src) )
434 CV_CALL( src = cvGetMat( src, &srcstub ));
436 if( !CV_IS_MAT(dst) )
437 CV_CALL( dst = cvGetMat( dst, &dststub ));
439 if( offset.x < 0 || offset.y < 0 )
440 CV_ERROR( CV_StsOutOfRange, "Offset (left/top border width) is negative" );
442 if( src->rows + offset.y > dst->rows || src->cols + offset.x > dst->cols )
443 CV_ERROR( CV_StsBadSize, "Source array is too big or destination array is too small" );
445 if( !CV_ARE_TYPES_EQ( src, dst ))
446 CV_ERROR( CV_StsUnmatchedFormats, "" );
448 type = CV_MAT_TYPE(src->type);
449 pix_size = CV_ELEM_SIZE(type);
450 srcsize = cvGetMatSize(src);
451 dstsize = cvGetMatSize(dst);
455 srcstep = CV_STUB_STEP;
457 dststep = CV_STUB_STEP;
459 if( bordertype == IPL_BORDER_REPLICATE )
461 icvCopyReplicateBorder_8u( src->data.ptr, srcstep, srcsize,
462 dst->data.ptr, dststep, dstsize,
463 offset.y, offset.x, pix_size );
465 else if( bordertype == IPL_BORDER_REFLECT_101 )
467 icvCopyReflect101Border_8u( src->data.ptr, srcstep, srcsize,
468 dst->data.ptr, dststep, dstsize,
469 offset.y, offset.x, pix_size );
471 else if( bordertype == IPL_BORDER_CONSTANT )
474 cvScalarToRawData( &value, buf, src->type, 0 );
475 icvCopyConstBorder_8u( src->data.ptr, srcstep, srcsize,
476 dst->data.ptr, dststep, dstsize,
477 offset.y, offset.x, pix_size, (uchar*)buf );
480 CV_ERROR( CV_StsBadFlag, "Unknown/unsupported border type" );
488 void copyMakeBorder( const Mat& src, Mat& dst, int top, int bottom,
489 int left, int right, int borderType, const Scalar& value )
491 dst.create( src.rows + top + bottom, src.cols + left + right, src.type() );
492 CvMat _src = src, _dst = dst;
493 cvCopyMakeBorder( &_src, &_dst, Point(left, top), borderType, value );