copy.cpp 25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

/* ////////////////////////////////////////////////////////////////////
//
//  Mat basic operations: Copy, Set
//
// */

#include "precomp.hpp"

namespace cv
{

template<typename T> static void
55
copyMask_(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size)
56
{
57
    for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
58
    {
59 60
        const T* src = (const T*)_src;
        T* dst = (T*)_dst;
61
        int x = 0;
Andrey Kamaev's avatar
Andrey Kamaev committed
62
         #if CV_ENABLE_UNROLLED
63 64 65 66 67 68 69 70 71 72 73
        for( ; x <= size.width - 4; x += 4 )
        {
            if( mask[x] )
                dst[x] = src[x];
            if( mask[x+1] )
                dst[x+1] = src[x+1];
            if( mask[x+2] )
                dst[x+2] = src[x+2];
            if( mask[x+3] )
                dst[x+3] = src[x+3];
        }
Victoria Zhislina's avatar
Victoria Zhislina committed
74
        #endif
75 76 77 78 79 80
        for( ; x < size.width; x++ )
            if( mask[x] )
                dst[x] = src[x];
    }
}

Maria Dimashova's avatar
Maria Dimashova committed
81
template<> void
82 83 84 85 86 87 88 89
copyMask_<uchar>(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size)
{
    for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
    {
        const uchar* src = (const uchar*)_src;
        uchar* dst = (uchar*)_dst;
        int x = 0;
        #if CV_SSE4_2
90 91 92 93 94 95 96 97 98 99 100 101 102 103
        if(USE_SSE4_2)//
        {
            __m128i zero = _mm_setzero_si128 ();

             for( ; x <= size.width - 16; x += 16 )
             {
                 const __m128i rSrc = _mm_lddqu_si128((const __m128i*)(src+x));
                 __m128i _mask = _mm_lddqu_si128((const __m128i*)(mask+x));
                 __m128i rDst = _mm_lddqu_si128((__m128i*)(dst+x));
                 __m128i _negMask = _mm_cmpeq_epi8(_mask, zero);
                 rDst = _mm_blendv_epi8(rSrc, rDst, _negMask);
                 _mm_storeu_si128((__m128i*)(dst + x), rDst);
             }
        }
104 105 106 107 108 109 110
        #endif
        for( ; x < size.width; x++ )
            if( mask[x] )
                dst[x] = src[x];
    }
}

Maria Dimashova's avatar
Maria Dimashova committed
111
template<> void
112 113 114 115
copyMask_<ushort>(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size)
{
    for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
    {
116
        const ushort* src = (const ushort*)_src;
117 118 119
        ushort* dst = (ushort*)_dst;
        int x = 0;
        #if CV_SSE4_2
120 121 122 123 124 125 126 127 128 129 130 131 132 133
        if(USE_SSE4_2)//
        {
            __m128i zero = _mm_setzero_si128 ();
            for( ; x <= size.width - 8; x += 8 )
            {
                 const __m128i rSrc =_mm_lddqu_si128((const __m128i*)(src+x));
                 __m128i _mask = _mm_loadl_epi64((const __m128i*)(mask+x));
                 _mask = _mm_unpacklo_epi8(_mask, _mask);
                 __m128i rDst = _mm_lddqu_si128((const __m128i*)(dst+x));
                 __m128i _negMask = _mm_cmpeq_epi8(_mask, zero);
                 rDst = _mm_blendv_epi8(rSrc, rDst, _negMask);
                 _mm_storeu_si128((__m128i*)(dst + x), rDst);
             }
        }
134 135 136 137 138 139 140
        #endif
        for( ; x < size.width; x++ )
            if( mask[x] )
                dst[x] = src[x];
    }
}

141 142
static void
copyMaskGeneric(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size, void* _esz)
143
{
144 145
    size_t k, esz = *(size_t*)_esz;
    for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
146
    {
147 148 149 150 151 152 153 154 155 156
        const uchar* src = _src;
        uchar* dst = _dst;
        int x = 0;
        for( ; x < size.width; x++, src += esz, dst += esz )
        {
            if( !mask[x] )
                continue;
            for( k = 0; k < esz; k++ )
                dst[k] = src[k];
        }
157
    }
158
}
Andrey Kamaev's avatar
Andrey Kamaev committed
159 160


161
#define DEF_COPY_MASK(suffix, type) \
162 163 164 165 166
static void copyMask##suffix(const uchar* src, size_t sstep, const uchar* mask, size_t mstep, \
                             uchar* dst, size_t dstep, Size size, void*) \
{ \
    copyMask_<type>(src, sstep, mask, mstep, dst, dstep, size); \
}
Andrey Kamaev's avatar
Andrey Kamaev committed
167 168


169 170 171 172 173 174 175 176 177 178
DEF_COPY_MASK(8u, uchar);
DEF_COPY_MASK(16u, ushort);
DEF_COPY_MASK(8uC3, Vec3b);
DEF_COPY_MASK(32s, int);
DEF_COPY_MASK(16uC3, Vec3s);
DEF_COPY_MASK(32sC2, Vec2i);
DEF_COPY_MASK(32sC3, Vec3i);
DEF_COPY_MASK(32sC4, Vec4i);
DEF_COPY_MASK(32sC6, Vec6i);
DEF_COPY_MASK(32sC8, Vec8i);
Andrey Kamaev's avatar
Andrey Kamaev committed
179

180
BinaryFunc copyMaskTab[] =
181 182
{
    0,
183 184 185 186
    copyMask8u,
    copyMask16u,
    copyMask8uC3,
    copyMask32s,
187
    0,
188
    copyMask16uC3,
189
    0,
190
    copyMask32sC2,
191
    0, 0, 0,
192
    copyMask32sC3,
193
    0, 0, 0,
194
    copyMask32sC4,
195
    0, 0, 0, 0, 0, 0, 0,
196
    copyMask32sC6,
197
    0, 0, 0, 0, 0, 0, 0,
198
    copyMask32sC8
199
};
Andrey Kamaev's avatar
Andrey Kamaev committed
200

201 202 203 204
BinaryFunc getCopyMaskFunc(size_t esz)
{
    return esz <= 32 && copyMaskTab[esz] ? copyMaskTab[esz] : copyMaskGeneric;
}
205 206

/* dst = src */
207
void Mat::copyTo( OutputArray _dst ) const
208
{
209 210 211
    int dtype = _dst.type();
    if( _dst.fixedType() && dtype != type() )
    {
Vadim Pisarevsky's avatar
Vadim Pisarevsky committed
212
        CV_Assert( channels() == CV_MAT_CN(dtype) );
213 214 215
        convertTo( _dst, dtype );
        return;
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
216

217 218 219
    if( empty() )
    {
        _dst.release();
220
        return;
221
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
222

223
    if( dims <= 2 )
224
    {
225 226 227 228
        _dst.create( rows, cols, type() );
        Mat dst = _dst.getMat();
        if( data == dst.data )
            return;
Andrey Kamaev's avatar
Andrey Kamaev committed
229

230 231 232 233
        if( rows > 0 && cols > 0 )
        {
            const uchar* sptr = data;
            uchar* dptr = dst.data;
Andrey Kamaev's avatar
Andrey Kamaev committed
234

235 236
            // to handle the copying 1xn matrix => nx1 std vector.
            Size sz = size() == dst.size() ?
237 238 239
                getContinuousSize(*this, dst) :
                getContinuousSize(*this);
            size_t len = sz.width*elemSize();
Andrey Kamaev's avatar
Andrey Kamaev committed
240

241
            for( ; sz.height--; sptr += step, dptr += dst.step )
242
                memcpy( dptr, sptr, len );
243
        }
244 245
        return;
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
246

247 248 249 250
    _dst.create( dims, size, type() );
    Mat dst = _dst.getMat();
    if( data == dst.data )
        return;
Andrey Kamaev's avatar
Andrey Kamaev committed
251

252
    if( total() != 0 )
253
    {
254 255 256
        const Mat* arrays[] = { this, &dst };
        uchar* ptrs[2];
        NAryMatIterator it(arrays, ptrs, 2);
Andrey Kamaev's avatar
Andrey Kamaev committed
257 258
        size_t sz = it.size*elemSize();

259
        for( size_t i = 0; i < it.nplanes; i++, ++it )
Andrey Kamaev's avatar
Andrey Kamaev committed
260
            memcpy(ptrs[1], ptrs[0], sz);
261
    }
262 263
}

264
void Mat::copyTo( OutputArray _dst, InputArray _mask ) const
265
{
266
    Mat mask = _mask.getMat();
267 268
    if( !mask.data )
    {
269
        copyTo(_dst);
270 271
        return;
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
272

273 274 275
    int cn = channels(), mcn = mask.channels();
    CV_Assert( mask.depth() == CV_8U && (mcn == 1 || mcn == cn) );
    bool colorMask = mcn > 1;
Andrey Kamaev's avatar
Andrey Kamaev committed
276

277
    size_t esz = colorMask ? elemSize1() : elemSize();
278
    BinaryFunc copymask = getCopyMaskFunc(esz);
Andrey Kamaev's avatar
Andrey Kamaev committed
279

280 281 282
    uchar* data0 = _dst.getMat().data;
    _dst.create( dims, size, type() );
    Mat dst = _dst.getMat();
Andrey Kamaev's avatar
Andrey Kamaev committed
283

284 285
    if( dst.data != data0 ) // do not leave dst uninitialized
        dst = Scalar(0);
Andrey Kamaev's avatar
Andrey Kamaev committed
286

287
    if( dims <= 2 )
288
    {
289
        Size sz = getContinuousSize(*this, dst, mask, mcn);
290
        copymask(data, step, mask.data, mask.step, dst.data, dst.step, sz, &esz);
291 292
        return;
    }
Andrey Kamaev's avatar
Andrey Kamaev committed
293

294 295 296
    const Mat* arrays[] = { this, &dst, &mask, 0 };
    uchar* ptrs[3];
    NAryMatIterator it(arrays, ptrs);
297
    Size sz((int)(it.size*mcn), 1);
Andrey Kamaev's avatar
Andrey Kamaev committed
298

299 300
    for( size_t i = 0; i < it.nplanes; i++, ++it )
        copymask(ptrs[0], 0, ptrs[2], 0, ptrs[1], 0, sz, &esz);
301 302 303 304
}

Mat& Mat::operator = (const Scalar& s)
{
305
    const Mat* arrays[] = { this };
Andrey Kamaev's avatar
Andrey Kamaev committed
306 307 308
    uchar* dptr;
    NAryMatIterator it(arrays, &dptr, 1);
    size_t elsize = it.size*elemSize();
309
    const int64* is = (const int64*)&s.val[0];
Andrey Kamaev's avatar
Andrey Kamaev committed
310

311
    if( is[0] == 0 && is[1] == 0 && is[2] == 0 && is[3] == 0 )
312
    {
313
        for( size_t i = 0; i < it.nplanes; i++, ++it )
Andrey Kamaev's avatar
Andrey Kamaev committed
314
            memset( dptr, 0, elsize );
315 316 317
    }
    else
    {
318
        if( it.nplanes > 0 )
319
        {
320 321 322
            double scalar[12];
            scalarToRawData(s, scalar, type(), 12);
            size_t blockSize = 12*elemSize1();
Andrey Kamaev's avatar
Andrey Kamaev committed
323 324

            for( size_t j = 0; j < elsize; j += blockSize )
325
            {
Andrey Kamaev's avatar
Andrey Kamaev committed
326 327
                size_t sz = MIN(blockSize, elsize - j);
                memcpy( dptr + j, scalar, sz );
328 329
            }
        }
Andrey Kamaev's avatar
Andrey Kamaev committed
330

331
        for( size_t i = 1; i < it.nplanes; i++ )
332
        {
333
            ++it;
Andrey Kamaev's avatar
Andrey Kamaev committed
334
            memcpy( dptr, data, elsize );
335 336 337 338 339
        }
    }
    return *this;
}

Andrey Kamaev's avatar
Andrey Kamaev committed
340

341
Mat& Mat::setTo(InputArray _value, InputArray _mask)
342
{
343 344
    if( !data )
        return *this;
Andrey Kamaev's avatar
Andrey Kamaev committed
345

346
    Mat value = _value.getMat(), mask = _mask.getMat();
Andrey Kamaev's avatar
Andrey Kamaev committed
347

348 349
    CV_Assert( checkScalar(value, type(), _value.kind(), _InputArray::MAT ));
    CV_Assert( mask.empty() || mask.type() == CV_8U );
Andrey Kamaev's avatar
Andrey Kamaev committed
350

351 352
    size_t esz = elemSize();
    BinaryFunc copymask = getCopyMaskFunc(esz);
Andrey Kamaev's avatar
Andrey Kamaev committed
353

354 355 356
    const Mat* arrays[] = { this, !mask.empty() ? &mask : 0, 0 };
    uchar* ptrs[2]={0,0};
    NAryMatIterator it(arrays, ptrs);
Andrey Kamaev's avatar
Andrey Kamaev committed
357
    int totalsz = (int)it.size, blockSize0 = std::min(totalsz, (int)((BLOCK_SIZE + esz-1)/esz));
358 359 360
    AutoBuffer<uchar> _scbuf(blockSize0*esz + 32);
    uchar* scbuf = alignPtr((uchar*)_scbuf, (int)sizeof(double));
    convertAndUnrollScalar( value, type(), scbuf, blockSize0 );
Andrey Kamaev's avatar
Andrey Kamaev committed
361

362
    for( size_t i = 0; i < it.nplanes; i++, ++it )
363
    {
Andrey Kamaev's avatar
Andrey Kamaev committed
364
        for( int j = 0; j < totalsz; j += blockSize0 )
365
        {
Andrey Kamaev's avatar
Andrey Kamaev committed
366
            Size sz(std::min(blockSize0, totalsz - j), 1);
367 368 369 370 371 372 373 374 375 376
            size_t blockSize = sz.width*esz;
            if( ptrs[1] )
            {
                copymask(scbuf, 0, ptrs[1], 0, ptrs[0], 0, sz, &esz);
                ptrs[1] += sz.width;
            }
            else
                memcpy(ptrs[0], scbuf, blockSize);
            ptrs[0] += blockSize;
        }
377 378 379 380 381
    }
    return *this;
}


382 383
static void
flipHoriz( const uchar* src, size_t sstep, uchar* dst, size_t dstep, Size size, size_t esz )
384
{
385
    int i, j, limit = (int)(((size.width + 1)/2)*esz);
386 387
    AutoBuffer<int> _tab(size.width*esz);
    int* tab = _tab;
Andrey Kamaev's avatar
Andrey Kamaev committed
388

389 390
    for( i = 0; i < size.width; i++ )
        for( size_t k = 0; k < esz; k++ )
391
            tab[i*esz + k] = (int)((size.width - i - 1)*esz + k);
392

393
    for( ; size.height--; src += sstep, dst += dstep )
394
    {
395
        for( i = 0; i < limit; i++ )
396
        {
397 398 399
            j = tab[i];
            uchar t0 = src[i], t1 = src[j];
            dst[i] = t1; dst[j] = t0;
400 401 402 403 404
        }
    }
}

static void
405
flipVert( const uchar* src0, size_t sstep, uchar* dst0, size_t dstep, Size size, size_t esz )
406
{
407 408 409 410 411 412
    const uchar* src1 = src0 + (size.height - 1)*sstep;
    uchar* dst1 = dst0 + (size.height - 1)*dstep;
    size.width *= (int)esz;

    for( int y = 0; y < (size.height + 1)/2; y++, src0 += sstep, src1 -= sstep,
                                                  dst0 += dstep, dst1 -= dstep )
413 414
    {
        int i = 0;
415
        if( ((size_t)src0|(size_t)dst0|(size_t)src1|(size_t)dst1) % sizeof(int) == 0 )
416 417 418
        {
            for( ; i <= size.width - 16; i += 16 )
            {
419
                int t0 = ((int*)(src0 + i))[0];
420 421
                int t1 = ((int*)(src1 + i))[0];

422
                ((int*)(dst0 + i))[0] = t1;
423 424
                ((int*)(dst1 + i))[0] = t0;

425
                t0 = ((int*)(src0 + i))[1];
426 427
                t1 = ((int*)(src1 + i))[1];

428
                ((int*)(dst0 + i))[1] = t1;
429 430
                ((int*)(dst1 + i))[1] = t0;

431
                t0 = ((int*)(src0 + i))[2];
432 433
                t1 = ((int*)(src1 + i))[2];

434
                ((int*)(dst0 + i))[2] = t1;
435 436
                ((int*)(dst1 + i))[2] = t0;

437
                t0 = ((int*)(src0 + i))[3];
438 439
                t1 = ((int*)(src1 + i))[3];

440
                ((int*)(dst0 + i))[3] = t1;
441 442 443 444 445
                ((int*)(dst1 + i))[3] = t0;
            }

            for( ; i <= size.width - 4; i += 4 )
            {
446
                int t0 = ((int*)(src0 + i))[0];
447 448
                int t1 = ((int*)(src1 + i))[0];

449
                ((int*)(dst0 + i))[0] = t1;
450 451 452 453 454 455
                ((int*)(dst1 + i))[0] = t0;
            }
        }

        for( ; i < size.width; i++ )
        {
456
            uchar t0 = src0[i];
457 458
            uchar t1 = src1[i];

459
            dst0[i] = t1;
460 461 462 463 464
            dst1[i] = t0;
        }
    }
}

465
void flip( InputArray _src, OutputArray _dst, int flip_mode )
466
{
467
    Mat src = _src.getMat();
Andrey Kamaev's avatar
Andrey Kamaev committed
468

469
    CV_Assert( src.dims <= 2 );
470 471 472
    _dst.create( src.size(), src.type() );
    Mat dst = _dst.getMat();
    size_t esz = src.elemSize();
473

474 475
    if( flip_mode <= 0 )
        flipVert( src.data, src.step, dst.data, dst.step, src.size(), esz );
476
    else
477
        flipHoriz( src.data, src.step, dst.data, dst.step, src.size(), esz );
Andrey Kamaev's avatar
Andrey Kamaev committed
478

479 480
    if( flip_mode < 0 )
        flipHoriz( dst.data, dst.step, dst.data, dst.step, dst.size(), esz );
481 482 483
}


484
void repeat(InputArray _src, int ny, int nx, OutputArray _dst)
485
{
486
    Mat src = _src.getMat();
487
    CV_Assert( src.dims <= 2 );
Andrey Kamaev's avatar
Andrey Kamaev committed
488

489 490
    _dst.create(src.rows*ny, src.cols*nx, src.type());
    Mat dst = _dst.getMat();
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    Size ssize = src.size(), dsize = dst.size();
    int esz = (int)src.elemSize();
    int x, y;
    ssize.width *= esz; dsize.width *= esz;

    for( y = 0; y < ssize.height; y++ )
    {
        for( x = 0; x < dsize.width; x += ssize.width )
            memcpy( dst.data + y*dst.step + x, src.data + y*src.step, ssize.width );
    }

    for( ; y < dsize.height; y++ )
        memcpy( dst.data + y*dst.step, dst.data + (y - ssize.height)*dst.step, dsize.width );
}

506 507 508 509 510 511 512 513 514
Mat repeat(const Mat& src, int ny, int nx)
{
    if( nx == 1 && ny == 1 )
        return src;
    Mat dst;
    repeat(src, ny, nx, dst);
    return dst;
}

515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739

} // cv


/*
 Various border types, image boundaries are denoted with '|'

 * BORDER_REPLICATE:     aaaaaa|abcdefgh|hhhhhhh
 * BORDER_REFLECT:       fedcba|abcdefgh|hgfedcb
 * BORDER_REFLECT_101:   gfedcb|abcdefgh|gfedcba
 * BORDER_WRAP:          cdefgh|abcdefgh|abcdefg
 * BORDER_CONSTANT:      iiiiii|abcdefgh|iiiiiii  with some specified 'i'
 */
int cv::borderInterpolate( int p, int len, int borderType )
{
    if( (unsigned)p < (unsigned)len )
        ;
    else if( borderType == BORDER_REPLICATE )
        p = p < 0 ? 0 : len - 1;
    else if( borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101 )
    {
        int delta = borderType == BORDER_REFLECT_101;
        if( len == 1 )
            return 0;
        do
        {
            if( p < 0 )
                p = -p - 1 + delta;
            else
                p = len - 1 - (p - len) - delta;
        }
        while( (unsigned)p >= (unsigned)len );
    }
    else if( borderType == BORDER_WRAP )
    {
        if( p < 0 )
            p -= ((p-len+1)/len)*len;
        if( p >= len )
            p %= len;
    }
    else if( borderType == BORDER_CONSTANT )
        p = -1;
    else
        CV_Error( CV_StsBadArg, "Unknown/unsupported border type" );
    return p;
}

namespace
{

void copyMakeBorder_8u( const uchar* src, size_t srcstep, cv::Size srcroi,
                        uchar* dst, size_t dststep, cv::Size dstroi,
                        int top, int left, int cn, int borderType )
{
    const int isz = (int)sizeof(int);
    int i, j, k, elemSize = 1;
    bool intMode = false;

    if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
    {
        cn /= isz;
        elemSize = isz;
        intMode = true;
    }

    cv::AutoBuffer<int> _tab((dstroi.width - srcroi.width)*cn);
    int* tab = _tab;
    int right = dstroi.width - srcroi.width - left;
    int bottom = dstroi.height - srcroi.height - top;

    for( i = 0; i < left; i++ )
    {
        j = cv::borderInterpolate(i - left, srcroi.width, borderType)*cn;
        for( k = 0; k < cn; k++ )
            tab[i*cn + k] = j + k;
    }

    for( i = 0; i < right; i++ )
    {
        j = cv::borderInterpolate(srcroi.width + i, srcroi.width, borderType)*cn;
        for( k = 0; k < cn; k++ )
            tab[(i+left)*cn + k] = j + k;
    }

    srcroi.width *= cn;
    dstroi.width *= cn;
    left *= cn;
    right *= cn;

    uchar* dstInner = dst + dststep*top + left*elemSize;

    for( i = 0; i < srcroi.height; i++, dstInner += dststep, src += srcstep )
    {
        if( dstInner != src )
            memcpy(dstInner, src, srcroi.width*elemSize);

        if( intMode )
        {
            const int* isrc = (int*)src;
            int* idstInner = (int*)dstInner;
            for( j = 0; j < left; j++ )
                idstInner[j - left] = isrc[tab[j]];
            for( j = 0; j < right; j++ )
                idstInner[j + srcroi.width] = isrc[tab[j + left]];
        }
        else
        {
            for( j = 0; j < left; j++ )
                dstInner[j - left] = src[tab[j]];
            for( j = 0; j < right; j++ )
                dstInner[j + srcroi.width] = src[tab[j + left]];
        }
    }

    dstroi.width *= elemSize;
    dst += dststep*top;

    for( i = 0; i < top; i++ )
    {
        j = cv::borderInterpolate(i - top, srcroi.height, borderType);
        memcpy(dst + (i - top)*dststep, dst + j*dststep, dstroi.width);
    }

    for( i = 0; i < bottom; i++ )
    {
        j = cv::borderInterpolate(i + srcroi.height, srcroi.height, borderType);
        memcpy(dst + (i + srcroi.height)*dststep, dst + j*dststep, dstroi.width);
    }
}


void copyMakeConstBorder_8u( const uchar* src, size_t srcstep, cv::Size srcroi,
                             uchar* dst, size_t dststep, cv::Size dstroi,
                             int top, int left, int cn, const uchar* value )
{
    int i, j;
    cv::AutoBuffer<uchar> _constBuf(dstroi.width*cn);
    uchar* constBuf = _constBuf;
    int right = dstroi.width - srcroi.width - left;
    int bottom = dstroi.height - srcroi.height - top;

    for( i = 0; i < dstroi.width; i++ )
    {
        for( j = 0; j < cn; j++ )
            constBuf[i*cn + j] = value[j];
    }

    srcroi.width *= cn;
    dstroi.width *= cn;
    left *= cn;
    right *= cn;

    uchar* dstInner = dst + dststep*top + left;

    for( i = 0; i < srcroi.height; i++, dstInner += dststep, src += srcstep )
    {
        if( dstInner != src )
            memcpy( dstInner, src, srcroi.width );
        memcpy( dstInner - left, constBuf, left );
        memcpy( dstInner + srcroi.width, constBuf, right );
    }

    dst += dststep*top;

    for( i = 0; i < top; i++ )
        memcpy(dst + (i - top)*dststep, constBuf, dstroi.width);

    for( i = 0; i < bottom; i++ )
        memcpy(dst + (i + srcroi.height)*dststep, constBuf, dstroi.width);
}

}

void cv::copyMakeBorder( InputArray _src, OutputArray _dst, int top, int bottom,
                         int left, int right, int borderType, const Scalar& value )
{
    Mat src = _src.getMat();
    CV_Assert( top >= 0 && bottom >= 0 && left >= 0 && right >= 0 );

    if( src.isSubmatrix() && (borderType & BORDER_ISOLATED) == 0 )
    {
        Size wholeSize;
        Point ofs;
        src.locateROI(wholeSize, ofs);
        int dtop = std::min(ofs.y, top);
        int dbottom = std::min(wholeSize.height - src.rows - ofs.y, bottom);
        int dleft = std::min(ofs.x, left);
        int dright = std::min(wholeSize.width - src.cols - ofs.x, right);
        src.adjustROI(dtop, dbottom, dleft, dright);
        top -= dtop;
        left -= dleft;
        bottom -= dbottom;
        right -= dright;
    }

    _dst.create( src.rows + top + bottom, src.cols + left + right, src.type() );
    Mat dst = _dst.getMat();

    if(top == 0 && left == 0 && bottom == 0 && right == 0)
    {
        if(src.data != dst.data || src.step != dst.step)
            src.copyTo(dst);
        return;
    }

    borderType &= ~BORDER_ISOLATED;

    if( borderType != BORDER_CONSTANT )
        copyMakeBorder_8u( src.data, src.step, src.size(),
                           dst.data, dst.step, dst.size(),
                           top, left, (int)src.elemSize(), borderType );
    else
    {
        int cn = src.channels(), cn1 = cn;
        AutoBuffer<double> buf(cn);
        if( cn > 4 )
        {
            CV_Assert( value[0] == value[1] && value[0] == value[2] && value[0] == value[3] );
            cn1 = 1;
        }
        scalarToRawData(value, buf, CV_MAKETYPE(src.depth(), cn1), cn);
        copyMakeConstBorder_8u( src.data, src.step, src.size(),
                                dst.data, dst.step, dst.size(),
                                top, left, (int)src.elemSize(), (uchar*)(double*)buf );
    }
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
}

/* dst = src */
CV_IMPL void
cvCopy( const void* srcarr, void* dstarr, const void* maskarr )
{
    if( CV_IS_SPARSE_MAT(srcarr) && CV_IS_SPARSE_MAT(dstarr))
    {
        CV_Assert( maskarr == 0 );
        CvSparseMat* src1 = (CvSparseMat*)srcarr;
        CvSparseMat* dst1 = (CvSparseMat*)dstarr;
        CvSparseMatIterator iterator;
        CvSparseNode* node;

        dst1->dims = src1->dims;
        memcpy( dst1->size, src1->size, src1->dims*sizeof(src1->size[0]));
        dst1->valoffset = src1->valoffset;
        dst1->idxoffset = src1->idxoffset;
        cvClearSet( dst1->heap );

        if( src1->heap->active_count >= dst1->hashsize*CV_SPARSE_HASH_RATIO )
        {
            cvFree( &dst1->hashtable );
            dst1->hashsize = src1->hashsize;
            dst1->hashtable =
                (void**)cvAlloc( dst1->hashsize*sizeof(dst1->hashtable[0]));
        }

        memset( dst1->hashtable, 0, dst1->hashsize*sizeof(dst1->hashtable[0]));

        for( node = cvInitSparseMatIterator( src1, &iterator );
             node != 0; node = cvGetNextSparseNode( &iterator ))
        {
            CvSparseNode* node_copy = (CvSparseNode*)cvSetNew( dst1->heap );
            int tabidx = node->hashval & (dst1->hashsize - 1);
775
            memcpy( node_copy, node, dst1->heap->elem_size );
776 777 778 779 780 781
            node_copy->next = (CvSparseNode*)dst1->hashtable[tabidx];
            dst1->hashtable[tabidx] = node_copy;
        }
        return;
    }
    cv::Mat src = cv::cvarrToMat(srcarr, false, true, 1), dst = cv::cvarrToMat(dstarr, false, true, 1);
782
    CV_Assert( src.depth() == dst.depth() && src.size == dst.size );
Andrey Kamaev's avatar
Andrey Kamaev committed
783

784 785 786 787 788
    int coi1 = 0, coi2 = 0;
    if( CV_IS_IMAGE(srcarr) )
        coi1 = cvGetImageCOI((const IplImage*)srcarr);
    if( CV_IS_IMAGE(dstarr) )
        coi2 = cvGetImageCOI((const IplImage*)dstarr);
Andrey Kamaev's avatar
Andrey Kamaev committed
789

790 791 792 793
    if( coi1 || coi2 )
    {
        CV_Assert( (coi1 != 0 || src.channels() == 1) &&
            (coi2 != 0 || dst.channels() == 1) );
Andrey Kamaev's avatar
Andrey Kamaev committed
794

795 796 797 798 799 800
        int pair[] = { std::max(coi1-1, 0), std::max(coi2-1, 0) };
        cv::mixChannels( &src, 1, &dst, 1, pair, 1 );
        return;
    }
    else
        CV_Assert( src.channels() == dst.channels() );
Andrey Kamaev's avatar
Andrey Kamaev committed
801

802 803 804 805 806 807 808 809 810 811 812 813 814
    if( !maskarr )
        src.copyTo(dst);
    else
        src.copyTo(dst, cv::cvarrToMat(maskarr));
}

CV_IMPL void
cvSet( void* arr, CvScalar value, const void* maskarr )
{
    cv::Mat m = cv::cvarrToMat(arr);
    if( !maskarr )
        m = value;
    else
815
        m.setTo(cv::Scalar(value), cv::cvarrToMat(maskarr));
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
}

CV_IMPL void
cvSetZero( CvArr* arr )
{
    if( CV_IS_SPARSE_MAT(arr) )
    {
        CvSparseMat* mat1 = (CvSparseMat*)arr;
        cvClearSet( mat1->heap );
        if( mat1->hashtable )
            memset( mat1->hashtable, 0, mat1->hashsize*sizeof(mat1->hashtable[0]));
        return;
    }
    cv::Mat m = cv::cvarrToMat(arr);
    m = cv::Scalar(0);
}

CV_IMPL void
cvFlip( const CvArr* srcarr, CvArr* dstarr, int flip_mode )
{
    cv::Mat src = cv::cvarrToMat(srcarr);
    cv::Mat dst;
Andrey Kamaev's avatar
Andrey Kamaev committed
838

839 840 841 842
    if (!dstarr)
      dst = src;
    else
      dst = cv::cvarrToMat(dstarr);
Andrey Kamaev's avatar
Andrey Kamaev committed
843

844 845 846 847 848 849 850 851 852 853 854 855 856
    CV_Assert( src.type() == dst.type() && src.size() == dst.size() );
    cv::flip( src, dst, flip_mode );
}

CV_IMPL void
cvRepeat( const CvArr* srcarr, CvArr* dstarr )
{
    cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
    CV_Assert( src.type() == dst.type() &&
        dst.rows % src.rows == 0 && dst.cols % src.cols == 0 );
    cv::repeat(src, dst.rows/src.rows, dst.cols/src.cols, dst);
}

Maria Dimashova's avatar
Maria Dimashova committed
857
/* End of file. */