1/*M///////////////////////////////////////////////////////////////////////////////////////
2//
3//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4//
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.
8//
9//
10//                        Intel License Agreement
11//                For Open Source Computer Vision Library
12//
13// Copyright (C) 2000, Intel Corporation, all rights reserved.
14// Third party copyrights are property of their respective owners.
15//
16// Redistribution and use in source and binary forms, with or without modification,
17// are permitted provided that the following conditions are met:
18//
19//   * Redistribution's of source code must retain the above copyright notice,
20//     this list of conditions and the following disclaimer.
21//
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.
25//
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.
28//
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.
39//
40//M*/
41
42#include "_cv.h"
43
44CV_IMPL CvSeq* cvPointSeqFromMat( int seq_kind, const CvArr* arr,
45                                  CvContour* contour_header, CvSeqBlock* block )
46{
47    CvSeq* contour = 0;
48
49    CV_FUNCNAME( "cvPointSeqFromMat" );
50
51    assert( arr != 0 && contour_header != 0 && block != 0 );
52
53    __BEGIN__;
54
55    int eltype;
56    CvMat* mat = (CvMat*)arr;
57
58    if( !CV_IS_MAT( mat ))
59        CV_ERROR( CV_StsBadArg, "Input array is not a valid matrix" );
60
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" );
66
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" );
71
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 ));
76
77    contour = (CvSeq*)contour_header;
78
79    __END__;
80
81    return contour;
82}
83
84
85typedef CvStatus (CV_STDCALL * CvCopyNonConstBorderFunc)(
86    const void*, int, CvSize, void*, int, CvSize, int, int );
87
88typedef CvStatus (CV_STDCALL * CvCopyNonConstBorderFuncI)(
89    const void*, int, CvSize, CvSize, int, int );
90
91icvCopyReplicateBorder_8u_C1R_t icvCopyReplicateBorder_8u_C1R_p = 0;
92icvCopyReplicateBorder_16s_C1R_t icvCopyReplicateBorder_16s_C1R_p = 0;
93icvCopyReplicateBorder_8u_C3R_t icvCopyReplicateBorder_8u_C3R_p = 0;
94icvCopyReplicateBorder_32s_C1R_t icvCopyReplicateBorder_32s_C1R_p = 0;
95icvCopyReplicateBorder_16s_C3R_t icvCopyReplicateBorder_16s_C3R_p = 0;
96icvCopyReplicateBorder_16s_C4R_t icvCopyReplicateBorder_16s_C4R_p = 0;
97icvCopyReplicateBorder_32s_C3R_t icvCopyReplicateBorder_32s_C3R_p = 0;
98icvCopyReplicateBorder_32s_C4R_t icvCopyReplicateBorder_32s_C4R_p = 0;
99
100icvCopyReplicateBorder_8u_C1IR_t icvCopyReplicateBorder_8u_C1IR_p = 0;
101icvCopyReplicateBorder_16s_C1IR_t icvCopyReplicateBorder_16s_C1IR_p = 0;
102icvCopyReplicateBorder_8u_C3IR_t icvCopyReplicateBorder_8u_C3IR_p = 0;
103icvCopyReplicateBorder_32s_C1IR_t icvCopyReplicateBorder_32s_C1IR_p = 0;
104icvCopyReplicateBorder_16s_C3IR_t icvCopyReplicateBorder_16s_C3IR_p = 0;
105icvCopyReplicateBorder_16s_C4IR_t icvCopyReplicateBorder_16s_C4IR_p = 0;
106icvCopyReplicateBorder_32s_C3IR_t icvCopyReplicateBorder_32s_C3IR_p = 0;
107icvCopyReplicateBorder_32s_C4IR_t icvCopyReplicateBorder_32s_C4IR_p = 0;
108
109
110CvStatus CV_STDCALL
111icvCopyReplicateBorder_8u( const uchar* src, int srcstep, CvSize srcroi,
112                           uchar* dst, int dststep, CvSize dstroi,
113                           int top, int left, int cn, const uchar* )
114{
115    const int isz = (int)sizeof(int);
116    int i, j;
117
118    if( srcstep == dststep && dst + dststep*top + left*cn == src &&
119        icvCopyReplicateBorder_8u_C1IR_p )
120    {
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;
130
131        if( ifunc )
132            return ifunc( src, srcstep, srcroi, dstroi, top, left );
133    }
134    else if( icvCopyReplicateBorder_8u_C1R_p )
135    {
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;
145
146        if( func )
147            return func( src, srcstep, srcroi, dst, dststep, dstroi, top, left );
148    }
149
150    if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
151    {
152        const int* isrc = (const int*)src;
153        int* idst = (int*)dst;
154
155        cn /= isz;
156        srcstep /= isz;
157        dststep /= isz;
158
159        srcroi.width *= cn;
160        dstroi.width *= cn;
161        left *= cn;
162
163        for( i = 0; i < dstroi.height; i++, idst += dststep )
164        {
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 )
173                isrc += srcstep;
174        }
175    }
176    else
177    {
178        srcroi.width *= cn;
179        dstroi.width *= cn;
180        left *= cn;
181
182        for( i = 0; i < dstroi.height; i++, dst += dststep )
183        {
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 )
192                src += srcstep;
193        }
194    }
195
196    return CV_OK;
197}
198
199
200static CvStatus CV_STDCALL
201icvCopyReflect101Border_8u( const uchar* src, int srcstep, CvSize srcroi,
202                            uchar* dst, int dststep, CvSize dstroi,
203                            int top, int left, int cn )
204{
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;
209
210    if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
211    {
212        cn /= isz;
213        srcstep /= isz;
214        dststep /= isz;
215
216        int_mode = 1;
217    }
218
219    srcroi.width *= cn;
220    dstroi.width *= cn;
221    left *= cn;
222
223    tab_size = dstroi.width - srcroi.width;
224    tab = (int*)cvStackAlloc( tab_size*sizeof(tab[0]) );
225
226    if( srcroi.width == 1 )
227    {
228        for( k = 0; k < cn; k++ )
229            for( i = 0; i < tab_size; i += cn )
230                tab[i + k] = k + left;
231    }
232    else
233    {
234        j = dj = cn;
235        for( i = left - cn; i >= 0; i -= cn )
236        {
237            for( k = 0; k < cn; k++ )
238                tab[i + k] = j + k + left;
239            if( (unsigned)(j += dj) >= (unsigned)srcroi.width )
240                j -= 2*dj, dj = -dj;
241        }
242
243        j = srcroi.width - cn*2;
244        dj = -cn;
245        for( i = left; i < tab_size; i += cn )
246        {
247            for( k = 0; k < cn; k++ )
248                tab[i + k] = j + k + left;
249            if( (unsigned)(j += dj) >= (unsigned)srcroi.width )
250                j -= 2*dj, dj = -dj;
251        }
252    }
253
254    if( int_mode )
255    {
256        idst += top*dststep;
257        for( i = 0; i < srcroi.height; i++, isrc += srcstep, idst += dststep )
258        {
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++ )
263            {
264                k = tab[j];
265                idst[j] = idst[k];
266            }
267            for( ; j < tab_size; j++ )
268            {
269                k = tab[j];
270                idst[j + srcroi.width] = idst[k];
271            }
272        }
273        isrc -= srcroi.height*srcstep;
274        idst -= (top - srcroi.height)*dststep;
275    }
276    else
277    {
278        dst += top*dststep;
279        for( i = 0; i < srcroi.height; i++, src += srcstep, dst += dststep )
280        {
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++ )
285            {
286                k = tab[j];
287                dst[j] = dst[k];
288            }
289            for( ; j < tab_size; j++ )
290            {
291                k = tab[j];
292                dst[j + srcroi.width] = dst[k];
293            }
294        }
295        src -= srcroi.height*srcstep;
296        dst -= (top - srcroi.height)*dststep;
297    }
298
299    for( t = 0; t < 2; t++ )
300    {
301        int i1, i2, di;
302        if( t == 0 )
303            i1 = top-1, i2 = 0, di = -1, j = 1, dj = 1;
304        else
305            i1 = top+srcroi.height, i2=dstroi.height, di = 1, j = srcroi.height-2, dj = -1;
306
307        for( i = i1; i != i2; i += di )
308        {
309            if( int_mode )
310            {
311                const int* s = idst + i*dststep;
312                int* d = idst + (j+top)*dststep;
313                for( k = 0; k < dstroi.width; k++ )
314                    d[k] = s[k];
315            }
316            else
317            {
318                const uchar* s = dst + i*dststep;
319                uchar* d = dst + (j+top)*dststep;
320                for( k = 0; k < dstroi.width; k++ )
321                    d[k] = s[k];
322            }
323
324            if( (unsigned)(j += dj) >= (unsigned)srcroi.height )
325                j -= 2*dj, dj = -dj;
326        }
327    }
328
329    return CV_OK;
330}
331
332
333static CvStatus CV_STDCALL
334icvCopyConstBorder_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 )
337{
338    const int isz = (int)sizeof(int);
339    int i, j, k;
340    if( (cn | srcstep | dststep | (size_t)src | (size_t)dst | (size_t)value) % isz == 0 )
341    {
342        const int* isrc = (const int*)src;
343        int* idst = (int*)dst;
344        const int* ivalue = (const int*)value;
345        int v0 = ivalue[0];
346
347        cn /= isz;
348        srcstep /= isz;
349        dststep /= isz;
350
351        srcroi.width *= cn;
352        dstroi.width *= cn;
353        left *= cn;
354
355        for( j = 1; j < cn; j++ )
356            if( ivalue[j] != ivalue[0] )
357                break;
358
359        if( j == cn )
360            cn = 1;
361
362        if( dstroi.width <= 0 )
363            return CV_OK;
364
365        for( i = 0; i < dstroi.height; i++, idst += dststep )
366        {
367            if( i < top || i >= top + srcroi.height )
368            {
369                if( cn == 1 )
370                {
371                    for( j = 0; j < dstroi.width; j++ )
372                        idst[j] = v0;
373                }
374                else
375                {
376                    for( j = 0; j < cn; j++ )
377                        idst[j] = ivalue[j];
378                    for( ; j < dstroi.width; j++ )
379                        idst[j] = idst[j - cn];
380                }
381                continue;
382            }
383
384            if( cn == 1 )
385            {
386                for( j = 0; j < left; j++ )
387                    idst[j] = v0;
388                for( j = srcroi.width + left; j < dstroi.width; j++ )
389                    idst[j] = v0;
390            }
391            else
392            {
393                for( k = 0; k < cn; k++ )
394                {
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];
399                }
400            }
401
402            if( idst + left != isrc )
403                for( j = 0; j < srcroi.width; j++ )
404                    idst[j + left] = isrc[j];
405            isrc += srcstep;
406        }
407    }
408    else
409    {
410        uchar v0 = value[0];
411
412        srcroi.width *= cn;
413        dstroi.width *= cn;
414        left *= cn;
415
416        for( j = 1; j < cn; j++ )
417            if( value[j] != value[0] )
418                break;
419
420        if( j == cn )
421            cn = 1;
422
423        if( dstroi.width <= 0 )
424            return CV_OK;
425
426        for( i = 0; i < dstroi.height; i++, dst += dststep )
427        {
428            if( i < top || i >= top + srcroi.height )
429            {
430                if( cn == 1 )
431                {
432                    for( j = 0; j < dstroi.width; j++ )
433                        dst[j] = v0;
434                }
435                else
436                {
437                    for( j = 0; j < cn; j++ )
438                        dst[j] = value[j];
439                    for( ; j < dstroi.width; j++ )
440                        dst[j] = dst[j - cn];
441                }
442                continue;
443            }
444
445            if( cn == 1 )
446            {
447                for( j = 0; j < left; j++ )
448                    dst[j] = v0;
449                for( j = srcroi.width + left; j < dstroi.width; j++ )
450                    dst[j] = v0;
451            }
452            else
453            {
454                for( k = 0; k < cn; k++ )
455                {
456                    for( j = 0; j < left; j += cn )
457                        dst[j+k] = value[k];
458                    for( j = srcroi.width + left; j < dstroi.width; j += cn )
459                        dst[j+k] = value[k];
460                }
461            }
462
463            if( dst + left != src )
464                for( j = 0; j < srcroi.width; j++ )
465                    dst[j + left] = src[j];
466            src += srcstep;
467        }
468    }
469
470    return CV_OK;
471}
472
473
474CV_IMPL void
475cvCopyMakeBorder( const CvArr* srcarr, CvArr* dstarr, CvPoint offset,
476                  int bordertype, CvScalar value )
477{
478    CV_FUNCNAME( "cvCopyMakeBorder" );
479
480    __BEGIN__;
481
482    CvMat srcstub, *src = (CvMat*)srcarr;
483    CvMat dststub, *dst = (CvMat*)dstarr;
484    CvSize srcsize, dstsize;
485    int srcstep, dststep;
486    int pix_size, type;
487
488    if( !CV_IS_MAT(src) )
489        CV_CALL( src = cvGetMat( src, &srcstub ));
490
491    if( !CV_IS_MAT(dst) )
492        CV_CALL( dst = cvGetMat( dst, &dststub ));
493
494    if( offset.x < 0 || offset.y < 0 )
495        CV_ERROR( CV_StsOutOfRange, "Offset (left/top border width) is negative" );
496
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" );
499
500    if( !CV_ARE_TYPES_EQ( src, dst ))
501        CV_ERROR( CV_StsUnmatchedFormats, "" );
502
503    type = CV_MAT_TYPE(src->type);
504    pix_size = CV_ELEM_SIZE(type);
505    srcsize = cvGetMatSize(src);
506    dstsize = cvGetMatSize(dst);
507    srcstep = src->step;
508    dststep = dst->step;
509    if( srcstep == 0 )
510        srcstep = CV_STUB_STEP;
511    if( dststep == 0 )
512        dststep = CV_STUB_STEP;
513
514    if( bordertype == IPL_BORDER_REPLICATE )
515    {
516        icvCopyReplicateBorder_8u( src->data.ptr, srcstep, srcsize,
517                                   dst->data.ptr, dststep, dstsize,
518                                   offset.y, offset.x, pix_size );
519    }
520    else if( bordertype == IPL_BORDER_REFLECT_101 )
521    {
522        icvCopyReflect101Border_8u( src->data.ptr, srcstep, srcsize,
523                                   dst->data.ptr, dststep, dstsize,
524                                   offset.y, offset.x, pix_size );
525    }
526    else if( bordertype == IPL_BORDER_CONSTANT )
527    {
528        double buf[4];
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 );
533    }
534    else
535        CV_ERROR( CV_StsBadFlag, "Unknown/unsupported border type" );
536
537    __END__;
538}
539
540/* End of file. */
541