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 );
91 icvCopyReplicateBorder_8u_C1R_t icvCopyReplicateBorder_8u_C1R_p = 0;
92 icvCopyReplicateBorder_16s_C1R_t icvCopyReplicateBorder_16s_C1R_p = 0;
93 icvCopyReplicateBorder_8u_C3R_t icvCopyReplicateBorder_8u_C3R_p = 0;
94 icvCopyReplicateBorder_32s_C1R_t icvCopyReplicateBorder_32s_C1R_p = 0;
95 icvCopyReplicateBorder_16s_C3R_t icvCopyReplicateBorder_16s_C3R_p = 0;
96 icvCopyReplicateBorder_16s_C4R_t icvCopyReplicateBorder_16s_C4R_p = 0;
97 icvCopyReplicateBorder_32s_C3R_t icvCopyReplicateBorder_32s_C3R_p = 0;
98 icvCopyReplicateBorder_32s_C4R_t icvCopyReplicateBorder_32s_C4R_p = 0;
100 icvCopyReplicateBorder_8u_C1IR_t icvCopyReplicateBorder_8u_C1IR_p = 0;
101 icvCopyReplicateBorder_16s_C1IR_t icvCopyReplicateBorder_16s_C1IR_p = 0;
102 icvCopyReplicateBorder_8u_C3IR_t icvCopyReplicateBorder_8u_C3IR_p = 0;
103 icvCopyReplicateBorder_32s_C1IR_t icvCopyReplicateBorder_32s_C1IR_p = 0;
104 icvCopyReplicateBorder_16s_C3IR_t icvCopyReplicateBorder_16s_C3IR_p = 0;
105 icvCopyReplicateBorder_16s_C4IR_t icvCopyReplicateBorder_16s_C4IR_p = 0;
106 icvCopyReplicateBorder_32s_C3IR_t icvCopyReplicateBorder_32s_C3IR_p = 0;
107 icvCopyReplicateBorder_32s_C4IR_t icvCopyReplicateBorder_32s_C4IR_p = 0;
111 icvCopyReplicateBorder_8u( const uchar* src, int srcstep, CvSize srcroi,
112 uchar* dst, int dststep, CvSize dstroi,
113 int top, int left, int cn, const uchar* )
115 const int isz = (int)sizeof(int);
118 if( srcstep == dststep && dst + dststep*top + left*cn == src &&
119 icvCopyReplicateBorder_8u_C1IR_p )
121 CvCopyNonConstBorderFuncI ifunc =
122 cn == 1 ? icvCopyReplicateBorder_8u_C1IR_p :
123 cn == 2 ? icvCopyReplicateBorder_16s_C1IR_p :
124 cn == 3 ? icvCopyReplicateBorder_8u_C3IR_p :
125 cn == 4 ? icvCopyReplicateBorder_32s_C1IR_p :
126 cn == 6 ? icvCopyReplicateBorder_16s_C3IR_p :
127 cn == 8 ? icvCopyReplicateBorder_16s_C4IR_p :
128 cn == 12 ? icvCopyReplicateBorder_32s_C3IR_p :
129 cn == 16 ? icvCopyReplicateBorder_32s_C4IR_p : 0;
132 return ifunc( src, srcstep, srcroi, dstroi, top, left );
134 else if( icvCopyReplicateBorder_8u_C1R_p )
136 CvCopyNonConstBorderFunc func =
137 cn == 1 ? icvCopyReplicateBorder_8u_C1R_p :
138 cn == 2 ? icvCopyReplicateBorder_16s_C1R_p :
139 cn == 3 ? icvCopyReplicateBorder_8u_C3R_p :
140 cn == 4 ? icvCopyReplicateBorder_32s_C1R_p :
141 cn == 6 ? icvCopyReplicateBorder_16s_C3R_p :
142 cn == 8 ? icvCopyReplicateBorder_16s_C4R_p :
143 cn == 12 ? icvCopyReplicateBorder_32s_C3R_p :
144 cn == 16 ? icvCopyReplicateBorder_32s_C4R_p : 0;
147 return func( src, srcstep, srcroi, dst, dststep, dstroi, top, left );
150 if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
152 const int* isrc = (const int*)src;
153 int* idst = (int*)dst;
163 for( i = 0; i < dstroi.height; i++, idst += dststep )
165 if( idst + left != isrc )
166 for( j = 0; j < srcroi.width; j++ )
167 idst[j + left] = isrc[j];
168 for( j = left - 1; j >= 0; j-- )
169 idst[j] = idst[j + cn];
170 for( j = left+srcroi.width; j < dstroi.width; j++ )
171 idst[j] = idst[j - cn];
172 if( i >= top && i < top + srcroi.height - 1 )
182 for( i = 0; i < dstroi.height; i++, dst += dststep )
184 if( dst + left != src )
185 for( j = 0; j < srcroi.width; j++ )
186 dst[j + left] = src[j];
187 for( j = left - 1; j >= 0; j-- )
188 dst[j] = dst[j + cn];
189 for( j = left+srcroi.width; j < dstroi.width; j++ )
190 dst[j] = dst[j - cn];
191 if( i >= top && i < top + srcroi.height - 1 )
200 static CvStatus CV_STDCALL
201 icvCopyReflect101Border_8u( const uchar* src, int srcstep, CvSize srcroi,
202 uchar* dst, int dststep, CvSize dstroi,
203 int top, int left, int cn )
205 const int isz = (int)sizeof(int);
206 int i, j, k, t, dj, tab_size, int_mode = 0;
207 const int* isrc = (const int*)src;
208 int* idst = (int*)dst, *tab;
210 if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
223 tab_size = dstroi.width - srcroi.width;
224 tab = (int*)cvStackAlloc( tab_size*sizeof(tab[0]) );
226 if( srcroi.width == 1 )
228 for( k = 0; k < cn; k++ )
229 for( i = 0; i < tab_size; i += cn )
230 tab[i + k] = k + left;
235 for( i = left - cn; i >= 0; i -= cn )
237 for( k = 0; k < cn; k++ )
238 tab[i + k] = j + k + left;
239 if( (unsigned)(j += dj) >= (unsigned)srcroi.width )
243 j = srcroi.width - cn*2;
245 for( i = left; i < tab_size; i += cn )
247 for( k = 0; k < cn; k++ )
248 tab[i + k] = j + k + left;
249 if( (unsigned)(j += dj) >= (unsigned)srcroi.width )
257 for( i = 0; i < srcroi.height; i++, isrc += srcstep, idst += dststep )
259 if( idst + left != isrc )
260 for( j = 0; j < srcroi.width; j++ )
261 idst[j + left] = isrc[j];
262 for( j = 0; j < left; j++ )
267 for( ; j < tab_size; j++ )
270 idst[j + srcroi.width] = idst[k];
273 isrc -= srcroi.height*srcstep;
274 idst -= (top - srcroi.height)*dststep;
279 for( i = 0; i < srcroi.height; i++, src += srcstep, dst += dststep )
281 if( dst + left != src )
282 for( j = 0; j < srcroi.width; j++ )
283 dst[j + left] = src[j];
284 for( j = 0; j < left; j++ )
289 for( ; j < tab_size; j++ )
292 dst[j + srcroi.width] = dst[k];
295 src -= srcroi.height*srcstep;
296 dst -= (top - srcroi.height)*dststep;
299 for( t = 0; t < 2; t++ )
303 i1 = top-1, i2 = 0, di = -1, j = 1, dj = 1;
305 i1 = top+srcroi.height, i2=dstroi.height, di = 1, j = srcroi.height-2, dj = -1;
307 for( i = i1; i != i2; i += di )
311 const int* s = idst + i*dststep;
312 int* d = idst + (j+top)*dststep;
313 for( k = 0; k < dstroi.width; k++ )
318 const uchar* s = dst + i*dststep;
319 uchar* d = dst + (j+top)*dststep;
320 for( k = 0; k < dstroi.width; k++ )
324 if( (unsigned)(j += dj) >= (unsigned)srcroi.height )
333 static CvStatus CV_STDCALL
334 icvCopyConstBorder_8u( const uchar* src, int srcstep, CvSize srcroi,
335 uchar* dst, int dststep, CvSize dstroi,
336 int top, int left, int cn, const uchar* value )
338 const int isz = (int)sizeof(int);
340 if( (cn | srcstep | dststep | (size_t)src | (size_t)dst | (size_t)value) % isz == 0 )
342 const int* isrc = (const int*)src;
343 int* idst = (int*)dst;
344 const int* ivalue = (const int*)value;
355 for( j = 1; j < cn; j++ )
356 if( ivalue[j] != ivalue[0] )
362 if( dstroi.width <= 0 )
365 for( i = 0; i < dstroi.height; i++, idst += dststep )
367 if( i < top || i >= top + srcroi.height )
371 for( j = 0; j < dstroi.width; j++ )
376 for( j = 0; j < cn; j++ )
378 for( ; j < dstroi.width; j++ )
379 idst[j] = idst[j - cn];
386 for( j = 0; j < left; j++ )
388 for( j = srcroi.width + left; j < dstroi.width; j++ )
393 for( k = 0; k < cn; k++ )
395 for( j = 0; j < left; j += cn )
396 idst[j+k] = ivalue[k];
397 for( j = srcroi.width + left; j < dstroi.width; j += cn )
398 idst[j+k] = ivalue[k];
402 if( idst + left != isrc )
403 for( j = 0; j < srcroi.width; j++ )
404 idst[j + left] = isrc[j];
416 for( j = 1; j < cn; j++ )
417 if( value[j] != value[0] )
423 if( dstroi.width <= 0 )
426 for( i = 0; i < dstroi.height; i++, dst += dststep )
428 if( i < top || i >= top + srcroi.height )
432 for( j = 0; j < dstroi.width; j++ )
437 for( j = 0; j < cn; j++ )
439 for( ; j < dstroi.width; j++ )
440 dst[j] = dst[j - cn];
447 for( j = 0; j < left; j++ )
449 for( j = srcroi.width + left; j < dstroi.width; j++ )
454 for( k = 0; k < cn; k++ )
456 for( j = 0; j < left; j += cn )
458 for( j = srcroi.width + left; j < dstroi.width; j += cn )
463 if( dst + left != src )
464 for( j = 0; j < srcroi.width; j++ )
465 dst[j + left] = src[j];
475 cvCopyMakeBorder( const CvArr* srcarr, CvArr* dstarr, CvPoint offset,
476 int bordertype, CvScalar value )
478 CV_FUNCNAME( "cvCopyMakeBorder" );
482 CvMat srcstub, *src = (CvMat*)srcarr;
483 CvMat dststub, *dst = (CvMat*)dstarr;
484 CvSize srcsize, dstsize;
485 int srcstep, dststep;
488 if( !CV_IS_MAT(src) )
489 CV_CALL( src = cvGetMat( src, &srcstub ));
491 if( !CV_IS_MAT(dst) )
492 CV_CALL( dst = cvGetMat( dst, &dststub ));
494 if( offset.x < 0 || offset.y < 0 )
495 CV_ERROR( CV_StsOutOfRange, "Offset (left/top border width) is negative" );
497 if( src->rows + offset.y > dst->rows || src->cols + offset.x > dst->cols )
498 CV_ERROR( CV_StsBadSize, "Source array is too big or destination array is too small" );
500 if( !CV_ARE_TYPES_EQ( src, dst ))
501 CV_ERROR( CV_StsUnmatchedFormats, "" );
503 type = CV_MAT_TYPE(src->type);
504 pix_size = CV_ELEM_SIZE(type);
505 srcsize = cvGetMatSize(src);
506 dstsize = cvGetMatSize(dst);
510 srcstep = CV_STUB_STEP;
512 dststep = CV_STUB_STEP;
514 if( bordertype == IPL_BORDER_REPLICATE )
516 icvCopyReplicateBorder_8u( src->data.ptr, srcstep, srcsize,
517 dst->data.ptr, dststep, dstsize,
518 offset.y, offset.x, pix_size );
520 else if( bordertype == IPL_BORDER_REFLECT_101 )
522 icvCopyReflect101Border_8u( src->data.ptr, srcstep, srcsize,
523 dst->data.ptr, dststep, dstsize,
524 offset.y, offset.x, pix_size );
526 else if( bordertype == IPL_BORDER_CONSTANT )
529 cvScalarToRawData( &value, buf, src->type, 0 );
530 icvCopyConstBorder_8u( src->data.ptr, srcstep, srcsize,
531 dst->data.ptr, dststep, dstsize,
532 offset.y, offset.x, pix_size, (uchar*)buf );
535 CV_ERROR( CV_StsBadFlag, "Unknown/unsupported border type" );