arithm.cpp 70.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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
/*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.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// 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*/

/* ////////////////////////////////////////////////////////////////////
//
//  Matrix arithmetic and logical operations: +, -, *, /, &, |, ^, ~, abs ...
//
// */

#include "precomp.hpp"

51 52 53 54
#ifdef HAVE_IPP
#include "ippversion.h"
#endif

55 56 57 58 59 60
namespace cv
{

#if CV_SSE2

enum { ARITHM_SIMD = CV_CPU_SSE2 };
61

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
template<class Op8> struct VBinOp8
{
    int operator()(const uchar* src1, const uchar* src2, uchar* dst, int len) const
    {
        int x = 0;
        for( ; x <= len - 32; x += 32 )
        {
            __m128i r0 = _mm_loadu_si128((const __m128i*)(src1 + x));
            __m128i r1 = _mm_loadu_si128((const __m128i*)(src1 + x + 16));
            r0 = op(r0,_mm_loadu_si128((const __m128i*)(src2 + x)));
            r1 = op(r1,_mm_loadu_si128((const __m128i*)(src2 + x + 16)));
            _mm_storeu_si128((__m128i*)(dst + x), r0);
            _mm_storeu_si128((__m128i*)(dst + x + 16), r1);
        }
        for( ; x <= len - 8; x += 8 )
        {
            __m128i r0 = _mm_loadl_epi64((const __m128i*)(src1 + x));
            r0 = op(r0,_mm_loadl_epi64((const __m128i*)(src2 + x)));
            _mm_storel_epi64((__m128i*)(dst + x), r0);
        }
        return x;
    }
    Op8 op;
};

template<typename T, class Op16> struct VBinOp16
{
    int operator()(const T* src1, const T* src2, T* dst, int len) const
    {
        int x = 0;
        for( ; x <= len - 16; x += 16 )
        {
            __m128i r0 = _mm_loadu_si128((const __m128i*)(src1 + x));
            __m128i r1 = _mm_loadu_si128((const __m128i*)(src1 + x + 8));
            r0 = op(r0,_mm_loadu_si128((const __m128i*)(src2 + x)));
            r1 = op(r1,_mm_loadu_si128((const __m128i*)(src2 + x + 8)));
            _mm_storeu_si128((__m128i*)(dst + x), r0);
            _mm_storeu_si128((__m128i*)(dst + x + 8), r1);
        }
        for( ; x <= len - 4; x += 4 )
        {
            __m128i r0 = _mm_loadl_epi64((const __m128i*)(src1 + x));
            r0 = op(r0,_mm_loadl_epi64((const __m128i*)(src2 + x)));
            _mm_storel_epi64((__m128i*)(dst + x), r0);
        }
        return x;
    }
    Op16 op;
};

template<class Op32f> struct VBinOp32f
{
    int operator()(const float* src1, const float* src2, float* dst, int len) const
    {
        int x = 0;
        if( (((size_t)src1|(size_t)src2|(size_t)dst)&15) == 0 )
            for( ; x <= len - 8; x += 8 )
            {
                __m128 r0 = _mm_load_ps(src1 + x);
                __m128 r1 = _mm_load_ps(src1 + x + 4);
                r0 = op(r0,_mm_load_ps(src2 + x));
                r1 = op(r1,_mm_load_ps(src2 + x + 4));
                _mm_store_ps(dst + x, r0);
                _mm_store_ps(dst + x + 4, r1);
            }
        else
            for( ; x <= len - 8; x += 8 )
            {
                __m128 r0 = _mm_loadu_ps(src1 + x);
                __m128 r1 = _mm_loadu_ps(src1 + x + 4);
                r0 = op(r0,_mm_loadu_ps(src2 + x));
                r1 = op(r1,_mm_loadu_ps(src2 + x + 4));
                _mm_storeu_ps(dst + x, r0);
                _mm_storeu_ps(dst + x + 4, r1);
            }
        return x;
    }
    Op32f op;
};

struct _VAdd8u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_adds_epu8(a,b); }};
struct _VSub8u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_subs_epu8(a,b); }};
struct _VMin8u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_min_epu8(a,b); }};
struct _VMax8u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_max_epu8(a,b); }};
struct _VCmpGT8u { __m128i operator()(const __m128i& a, const __m128i& b) const
{
    __m128i delta = _mm_set1_epi32(0x80808080);
    return _mm_cmpgt_epi8(_mm_xor_si128(a,delta),_mm_xor_si128(b,delta));
}};
struct _VCmpEQ8u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_cmpeq_epi8(a,b); }};
struct _VAbsDiff8u
{
    __m128i operator()(const __m128i& a, const __m128i& b) const
    { return _mm_add_epi8(_mm_subs_epu8(a,b),_mm_subs_epu8(b,a)); }
};
struct _VAdd16u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_adds_epu16(a,b); }};
struct _VSub16u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_subs_epu16(a,b); }};
struct _VMin16u
{
    __m128i operator()(const __m128i& a, const __m128i& b) const
    { return _mm_subs_epu16(a,_mm_subs_epu16(a,b)); }
};
struct _VMax16u
{
    __m128i operator()(const __m128i& a, const __m128i& b) const
    { return _mm_adds_epu16(_mm_subs_epu16(a,b),b); }
};
struct _VAbsDiff16u
{
    __m128i operator()(const __m128i& a, const __m128i& b) const
    { return _mm_add_epi16(_mm_subs_epu16(a,b),_mm_subs_epu16(b,a)); }
};
struct _VAdd16s { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_adds_epi16(a,b); }};
struct _VSub16s { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_subs_epi16(a,b); }};
struct _VMin16s { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_min_epi16(a,b); }};
struct _VMax16s { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_max_epi16(a,b); }};
struct _VAbsDiff16s
{
    __m128i operator()(const __m128i& a, const __m128i& b) const
    {
        __m128i M = _mm_max_epi16(a,b), m = _mm_min_epi16(a,b);
        return _mm_subs_epi16(M, m);
    }
};
struct _VAdd32f { __m128 operator()(const __m128& a, const __m128& b) const { return _mm_add_ps(a,b); }};
struct _VSub32f { __m128 operator()(const __m128& a, const __m128& b) const { return _mm_sub_ps(a,b); }};
struct _VMin32f { __m128 operator()(const __m128& a, const __m128& b) const { return _mm_min_ps(a,b); }};
struct _VMax32f { __m128 operator()(const __m128& a, const __m128& b) const { return _mm_max_ps(a,b); }};
static int CV_DECL_ALIGNED(16) v32f_absmask[] = { 0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff };
struct _VAbsDiff32f
{
    __m128 operator()(const __m128& a, const __m128& b) const
    {
        return _mm_and_ps(_mm_sub_ps(a,b), *(const __m128*)v32f_absmask);
    }
};

struct _VAnd8u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_and_si128(a,b); }};
200
struct _VOr8u  { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_or_si128(a,b); }};
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
struct _VXor8u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_xor_si128(a,b); }};

typedef VBinOp8<_VAdd8u> VAdd8u;
typedef VBinOp8<_VSub8u> VSub8u;
typedef VBinOp8<_VMin8u> VMin8u;
typedef VBinOp8<_VMax8u> VMax8u;
typedef VBinOp8<_VAbsDiff8u> VAbsDiff8u;
typedef VBinOp8<_VCmpEQ8u> VCmpEQ8u;
typedef VBinOp8<_VCmpGT8u> VCmpGT8u;

typedef VBinOp16<ushort, _VAdd16u> VAdd16u;
typedef VBinOp16<ushort, _VSub16u> VSub16u;
typedef VBinOp16<ushort, _VMin16u> VMin16u;
typedef VBinOp16<ushort, _VMax16u> VMax16u;
typedef VBinOp16<ushort, _VAbsDiff16u> VAbsDiff16u;

typedef VBinOp16<short, _VAdd16s> VAdd16s;
typedef VBinOp16<short, _VSub16s> VSub16s;
typedef VBinOp16<short, _VMin16s> VMin16s;
typedef VBinOp16<short, _VMax16s> VMax16s;
typedef VBinOp16<short, _VAbsDiff16s> VAbsDiff16s;

typedef VBinOp32f<_VAdd32f> VAdd32f;
typedef VBinOp32f<_VSub32f> VSub32f;
typedef VBinOp32f<_VMin32f> VMin32f;
typedef VBinOp32f<_VMax32f> VMax32f;
typedef VBinOp32f<_VAbsDiff32f> VAbsDiff32f;

typedef VBinOp8<_VAnd8u> VAnd8u;
typedef VBinOp8<_VOr8u> VOr8u;
typedef VBinOp8<_VXor8u> VXor8u;

#else

235 236
enum { ARITHM_SIMD = CV_CPU_NONE };

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
typedef NoVec VAdd8u;
typedef NoVec VSub8u;
typedef NoVec VMin8u;
typedef NoVec VMax8u;
typedef NoVec VAbsDiff8u;
typedef NoVec VCmpEQ8u;
typedef NoVec VCmpGT8u;

typedef NoVec VAdd16u;
typedef NoVec VSub16u;
typedef NoVec VMin16u;
typedef NoVec VMax16u;
typedef NoVec VAbsDiff16u;

typedef NoVec VAdd16s;
typedef NoVec VSub16s;
typedef NoVec VMin16s;
typedef NoVec VMax16s;
typedef NoVec VAbsDiff16s;

typedef NoVec VAdd32f;
typedef NoVec VSub32f;
typedef NoVec VMin32f;
typedef NoVec VMax32f;
typedef NoVec VAbsDiff32f;

typedef NoVec VAnd8u;
typedef NoVec VOr8u;
typedef NoVec VXor8u;

#endif

269 270

#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
271 272 273 274 275 276 277 278 279 280
struct IPPArithmInitializer
{
    IPPArithmInitializer(void)
    {
      IppStatus status = ippStaticInit();
    }
};

IPPArithmInitializer ippArithmInitializer;

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
struct ippAdd8u
{
    int operator()(const Ipp8u* src1, const Ipp8u* src2, Ipp8u* dst, int len) const
    {
        ippsAdd_8u_Sfs(src1,src2,dst,len,0);
        return len;
    }
};

struct ippAdd16u
{
    int operator()(const Ipp16u* src1, const Ipp16u* src2, Ipp16u* dst, int len) const
    {
        ippsAdd_16u_Sfs(src1,src2,dst,len,0);
        return len;
    }
};

struct ippAdd16s
{
    int operator()(const Ipp16s* src1, const Ipp16s* src2, Ipp16s* dst, int len) const
    {
        ippsAdd_16s_Sfs(src1,src2,dst,len,0);
        return len;
    }
};

struct ippAdd32s
{
    int operator()(const Ipp32s* src1, const Ipp32s* src2, Ipp32s* dst, int len) const
    {
        ippsAdd_32s_Sfs(src1,src2,dst,len,0);
        return len;
    }
};

struct ippAdd32f
{
    int operator()(const Ipp32f* src1, const Ipp32f* src2, Ipp32f* dst, int len) const
    {
        ippsAdd_32f(src1,src2,dst,len);
        return len;
    }
};

struct ippAdd64f
{
    int operator()(const Ipp64f* src1, const Ipp64f* src2, Ipp64f* dst, int len) const
    {
        ippsAdd_64f(src1,src2,dst,len);
        return len;
    }
};

335 336 337 338
struct ippSub8u
{
    int operator()(const Ipp8u* src1, const Ipp8u* src2, Ipp8u* dst, int len) const
    {
339
        ippsSub_8u_Sfs(src2,src1,dst,len,0);
340 341 342 343 344 345 346 347
        return len;
    }
};

struct ippSub16u
{
    int operator()(const Ipp16u* src1, const Ipp16u* src2, Ipp16u* dst, int len) const
    {
348
        ippsSub_16u_Sfs(src2,src1,dst,len,0);
349 350 351 352 353 354 355 356
        return len;
    }
};

struct ippSub16s
{
    int operator()(const Ipp16s* src1, const Ipp16s* src2, Ipp16s* dst, int len) const
    {
357
        ippsSub_16s_Sfs(src2,src1,dst,len,0);
358 359 360 361 362 363 364 365
        return len;
    }
};

struct ippSub32s
{
    int operator()(const Ipp32s* src1, const Ipp32s* src2, Ipp32s* dst, int len) const
    {
366
        ippsSub_32s_Sfs(src2,src1,dst,len,0);
367 368 369 370 371 372 373 374
        return len;
    }
};

struct ippSub32f
{
    int operator()(const Ipp32f* src1, const Ipp32f* src2, Ipp32f* dst, int len) const
    {
375
        ippsSub_32f(src2,src1,dst,len);
376 377 378 379 380 381 382 383
        return len;
    }
};

struct ippSub64f
{
    int operator()(const Ipp64f* src1, const Ipp64f* src2, Ipp64f* dst, int len) const
    {
384
        ippsSub_64f(src2,src1,dst,len);
385 386 387 388
        return len;
    }
};

389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
struct ippMax8u
{
    int operator()(const Ipp8u* src1, const Ipp8u* src2, Ipp8u* dst, int len) const
    {
        ippsMaxEvery_8u(src1,src2,dst,len);
        return len;
    }
};

struct ippMax16u
{
    int operator()(const Ipp16u* src1, const Ipp16u* src2, Ipp16u* dst, int len) const
    {
        ippsMaxEvery_16u(src1,src2,dst,len);
        return len;
    }
};

struct ippMax32f
{
    int operator()(const Ipp32f* src1, const Ipp32f* src2, Ipp32f* dst, int len) const
    {
        ippsMaxEvery_32f(src1,src2,dst,len);
        return len;
    }
};

struct ippMax64f
{
    int operator()(const Ipp64f* src1, const Ipp64f* src2, Ipp64f* dst, int len) const
    {
        ippsMaxEvery_64f(src1,src2,dst,len);
        return len;
    }
};

struct ippMin8u
{
    int operator()(const Ipp8u* src1, const Ipp8u* src2, Ipp8u* dst, int len) const
    {
        ippsMinEvery_8u(src1,src2,dst,len);
        return len;
    }
};

struct ippMin16u
{
    int operator()(const Ipp16u* src1, const Ipp16u* src2, Ipp16u* dst, int len) const
    {
        ippsMinEvery_16u(src1,src2,dst,len);
        return len;
    }
};

struct ippMin32f
{
    int operator()(const Ipp32f* src1, const Ipp32f* src2, Ipp32f* dst, int len) const
    {
        ippsMinEvery_32f(src1,src2,dst,len);
        return len;
    }
};

struct ippMin64f
{
    int operator()(const Ipp64f* src1, const Ipp64f* src2, Ipp64f* dst, int len) const
    {
        ippsMinEvery_64f(src1,src2,dst,len);
        return len;
    }
};

struct ippAbsDiff8u
{
    int operator()(const Ipp8u* src1, const Ipp8u* src2, Ipp8u* dst, int len) const
    {
        int step = len * sizeof(Ipp8u);
        IppiSize roi = { len, 1 };
        ippiAbsDiff_8u_C1R(src1,step,src2,step,dst,step,roi);
        return len;
    }
};

struct ippAbsDiff16u
{
    int operator()(const Ipp16u* src1, const Ipp16u* src2, Ipp16u* dst, int len) const
    {
        int step = len * sizeof(Ipp16u);
        IppiSize roi = { len, 1 };
        ippiAbsDiff_16u_C1R(src1,step,src2,step,dst,step,roi);
        return len;
    }
};

struct ippAbsDiff32f
{
    int operator()(const Ipp32f* src1, const Ipp32f* src2, Ipp32f* dst, int len) const
    {
        int step = len * sizeof(Ipp32f);
        IppiSize roi = { len, 1 };
        ippiAbsDiff_32f_C1R(src1,step,src2,step,dst,step,roi);
        return len;
    }
};

struct ippAnd8u
{
    int operator()(const Ipp8u* src1, const Ipp8u* src2, Ipp8u* dst, int len) const
    {
        ippsAnd_8u(src1,src2,dst,len);
        return len;
    }
};

struct ippOr8u
{
    int operator()(const Ipp8u* src1, const Ipp8u* src2, Ipp8u* dst, int len) const
    {
        ippsOr_8u(src1,src2,dst,len);
        return len;
    }
};

struct ippXor8u
{
    int operator()(const Ipp8u* src1, const Ipp8u* src2, Ipp8u* dst, int len) const
    {
        ippsXor_8u(src1,src2,dst,len);
        return len;
    }
};

#endif // defined(HAVE_IPP)
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
/****************************************************************************************\
*                                   logical operations                                   *
\****************************************************************************************/

template<typename T> struct AndOp
{
    typedef T type1;
    typedef T type2;
    typedef T rtype;
    T operator()( T a, T b ) const { return a & b; }
};

template<typename T> struct OrOp
{
    typedef T type1;
    typedef T type2;
    typedef T rtype;
    T operator()( T a, T b ) const { return a | b; }
};

template<typename T> struct XorOp
{
    typedef T type1;
    typedef T type2;
    typedef T rtype;
    T operator()( T a, T b ) const { return a ^ b; }
};

template<class OPB, class OPI, class OPV> static void
bitwiseOp_( const Mat& srcmat1, const Mat& srcmat2, Mat& dstmat )
{
    OPB opb; OPI opi; OPV opv;
    const uchar* src1 = srcmat1.data;
    const uchar* src2 = srcmat2.data;
    uchar* dst = dstmat.data;
    size_t step1 = srcmat1.step, step2 = srcmat2.step, step = dstmat.step;
    Size size = getContinuousSize( srcmat1, srcmat2, dstmat, (int)srcmat1.elemSize() );
    bool useSIMD = checkHardwareSupport(ARITHM_SIMD);

    for( ; size.height--; src1 += step1, src2 += step2, dst += step )
    {
        int i = useSIMD ? opv(src1, src2, dst, size.width) : 0;

        if( (((size_t)src1 | (size_t)src2 | (size_t)dst) & 3) == 0 )
        {
            for( ; i <= size.width - 16; i += 16 )
            {
                int t0 = opi(((const int*)(src1+i))[0], ((const int*)(src2+i))[0]);
                int t1 = opi(((const int*)(src1+i))[1], ((const int*)(src2+i))[1]);

                ((int*)(dst+i))[0] = t0;
                ((int*)(dst+i))[1] = t1;

                t0 = opi(((const int*)(src1+i))[2], ((const int*)(src2+i))[2]);
                t1 = opi(((const int*)(src1+i))[3], ((const int*)(src2+i))[3]);

                ((int*)(dst+i))[2] = t0;
                ((int*)(dst+i))[3] = t1;
            }

            for( ; i <= size.width - 4; i += 4 )
            {
                int t = opi(*(const int*)(src1+i), *(const int*)(src2+i));
                *(int*)(dst+i) = t;
            }
        }

        for( ; i < size.width; i++ )
            dst[i] = opb(src1[i], src2[i]);
    }
}


template<class OPB, class OPI, class OPV> static void
bitwiseSOp_( const Mat& srcmat, Mat& dstmat, const Scalar& _scalar )
{
    OPB opb; OPI opi; OPV opv;
    const uchar* src0 = srcmat.data;
    uchar* dst0 = dstmat.data;
    size_t step1 = srcmat.step, step = dstmat.step;
    Size size = getContinuousSize( srcmat, dstmat, (int)srcmat.elemSize() );
    const int delta = 96;
    uchar scalar[delta];
    scalarToRawData(_scalar, scalar, srcmat.type(), (int)(delta/srcmat.elemSize1()) );
    bool useSIMD = checkHardwareSupport(ARITHM_SIMD);

    for( ; size.height--; src0 += step1, dst0 += step )
    {
        const uchar* src = (const uchar*)src0;
        uchar* dst = dst0;
        int i, len = size.width;

        if( (((size_t)src|(size_t)dst) & 3) == 0 )
        {
            while( (len -= delta) >= 0 )
            {
                i = useSIMD ? opv(src, scalar, dst, delta) : 0;
                for( ; i < delta; i += 16 )
                {
                    int t0 = opi(((const int*)(src+i))[0], ((const int*)(scalar+i))[0]);
                    int t1 = opi(((const int*)(src+i))[1], ((const int*)(scalar+i))[1]);
                    ((int*)(dst+i))[0] = t0;
                    ((int*)(dst+i))[1] = t1;

                    t0 = opi(((const int*)(src+i))[2], ((const int*)(scalar+i))[2]);
                    t1 = opi(((const int*)(src+i))[3], ((const int*)(scalar+i))[3]);
                    ((int*)(dst+i))[2] = t0;
                    ((int*)(dst+i))[3] = t1;
                }
                src += delta;
                dst += delta;
            }
        }
        else
        {
            while( (len -= delta) >= 0 )
            {
                for( i = 0; i < delta; i += 4 )
                {
                    uchar t0 = opb(src[i], scalar[i]);
                    uchar t1 = opb(src[i+1], scalar[i+1]);
                    dst[i] = t0; dst[i+1] = t1;

                    t0 = opb(src[i+2], scalar[i+2]);
                    t1 = opb(src[i+3], scalar[i+3]);
                    dst[i+2] = t0; dst[i+3] = t1;
                }
                src += delta;
                dst += delta;
            }
        }

        for( len += delta, i = 0; i < len; i++ )
            dst[i] = opb(src[i],scalar[i]);
    }
}

661

V
Vadim Pisarevsky 已提交
662 663 664 665 666 667
static void
binaryOp( const Mat& src1, const Mat& src2, Mat& dst, BinaryFunc func, int dsttype=-1 )
{
    if( dsttype == -1 )
        dsttype = src1.type();
    CV_Assert( src1.type() == src2.type() && func != 0 );
668

V
Vadim Pisarevsky 已提交
669 670 671 672 673 674
    if( src1.dims > 2 || src2.dims > 2 )
    {
        dst.create(src1.dims, src1.size, dsttype);
        const Mat* arrays[] = { &src1, &src2, &dst, 0 };
        Mat planes[3];
        NAryMatIterator it(arrays, planes);
675

V
Vadim Pisarevsky 已提交
676 677 678 679
        for( int i = 0; i < it.nplanes; i++, ++it )
            func(it.planes[0], it.planes[1], it.planes[2]);
        return;
    }
680

V
Vadim Pisarevsky 已提交
681 682 683 684 685
    CV_Assert( src1.size() == src2.size() );
    dst.create( src1.size(), dsttype );
    func( src1, src2, dst );
}

686

687 688 689 690
static void
binaryMaskOp( const Mat& src1, const Mat& src2, Mat& dst,
              const Mat& mask, BinaryFunc func )
{
V
Vadim Pisarevsky 已提交
691
    CV_Assert( src1.type() == src2.type() && func != 0 );
692

V
Vadim Pisarevsky 已提交
693 694 695 696 697 698
    if( src1.dims > 2 || src2.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = { &src1, &src2, &dst, &mask, 0 };
        Mat planes[4];
        NAryMatIterator it(arrays, planes);
699

V
Vadim Pisarevsky 已提交
700 701 702 703 704 705 706 707 708 709
        if( !mask.data )
            for( int i = 0; i < it.nplanes; i++, ++it )
                func(it.planes[0], it.planes[1], it.planes[2]);
        else
            for( int i = 0; i < it.nplanes; i++, ++it )
                binaryMaskOp(it.planes[0], it.planes[1],
                             it.planes[2], it.planes[3],
                             func);
        return;
    }
710

V
Vadim Pisarevsky 已提交
711
    CV_Assert( src1.size() == src2.size() );
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 740 741 742 743
    dst.create( src1.size(), src1.type() );

    if( !mask.data )
        func(src1, src2, dst);
    else
    {
        AutoBuffer<uchar> buf;
        size_t esz = dst.elemSize(), buf_step = dst.cols*esz;
        CopyMaskFunc copym_func = getCopyMaskFunc((int)esz);
        int y, dy;

        CV_Assert(mask.type() == CV_8UC1 && mask.size() == dst.size());
        dy = std::min(std::max((int)(CV_MAX_LOCAL_SIZE/buf_step), 1), dst.rows);
        buf.allocate( buf_step*dy );

        for( y = 0; y < dst.rows; y += dy )
        {
            dy = std::min(dy, dst.rows - y);
            Mat dstpart = dst.rowRange(y, y + dy);
            Mat temp(dy, dst.cols, dst.type(), (uchar*)buf );
            func( src1.rowRange(y, y + dy), src2.rowRange(y, y + dy), temp );
            copym_func( temp, dstpart, mask.rowRange(y, y + dy) );
        }
    }
}


static void
binarySMaskOp( const Mat& src1, const Scalar& s, Mat& dst,
               const Mat& mask, BinarySFuncCn func )
{
    CV_Assert( func != 0 );
744

V
Vadim Pisarevsky 已提交
745 746 747 748 749 750
    if( src1.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = { &src1, &dst, &mask, 0 };
        Mat planes[3];
        NAryMatIterator it(arrays, planes);
751

V
Vadim Pisarevsky 已提交
752 753 754 755 756 757 758 759 760
        if( !mask.data )
            for( int i = 0; i < it.nplanes; i++, ++it )
                func(it.planes[0], it.planes[1], s);
        else
            for( int i = 0; i < it.nplanes; i++, ++it )
                binarySMaskOp(it.planes[0], s, it.planes[1],
                              it.planes[2], func);
        return;
    }
761

762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    dst.create( src1.size(), src1.type() );

    if( !mask.data )
        func(src1, dst, s);
    else
    {
        AutoBuffer<uchar> buf;
        size_t esz = dst.elemSize(), buf_step = dst.cols*esz;
        CopyMaskFunc copym_func = getCopyMaskFunc((int)esz);
        int y, dy;

        CV_Assert(mask.type() == CV_8UC1 && mask.size() == dst.size());
        dy = std::min(std::max((int)(CV_MAX_LOCAL_SIZE/buf_step), 1), dst.rows);
        buf.allocate( buf_step*dy );

        for( y = 0; y < dst.rows; y += dy )
        {
            dy = std::min(dy, dst.rows - y);
            Mat dstpart = dst.rowRange(y, y + dy);
            Mat temp(dy, dst.cols, dst.type(), (uchar*)buf);
            func( src1.rowRange(y, y + dy), temp, s );
            copym_func( temp, dstpart, mask.rowRange(y, y + dy) );
        }
    }
}


void bitwise_and(const Mat& a, const Mat& b, Mat& c, const Mat& mask)
{
791 792 793
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
    binaryMaskOp(a, b, c, mask, bitwiseOp_<AndOp<uchar>, AndOp<int>, ippAnd8u>);
#else
794
    binaryMaskOp(a, b, c, mask, bitwiseOp_<AndOp<uchar>, AndOp<int>, VAnd8u>);
795
#endif
796 797 798 799
}

void bitwise_or(const Mat& a, const Mat& b, Mat& c, const Mat& mask)
{
800 801 802
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
    binaryMaskOp(a, b, c, mask, bitwiseOp_<OrOp<uchar>, OrOp<int>, ippOr8u>);
#else
803
    binaryMaskOp(a, b, c, mask, bitwiseOp_<OrOp<uchar>, OrOp<int>, VOr8u>);
804
#endif
805 806 807 808
}

void bitwise_xor(const Mat& a, const Mat& b, Mat& c, const Mat& mask)
{
809 810 811
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
    binaryMaskOp(a, b, c, mask, bitwiseOp_<XorOp<uchar>, XorOp<int>, ippXor8u>);
#else
812
    binaryMaskOp(a, b, c, mask, bitwiseOp_<XorOp<uchar>, XorOp<int>, VXor8u>);
813
#endif
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
}

void bitwise_and(const Mat& a, const Scalar& s, Mat& c, const Mat& mask)
{
    binarySMaskOp(a, s, c, mask,
        bitwiseSOp_<AndOp<uchar>, AndOp<int>, VAnd8u>);
}

void bitwise_or(const Mat& a, const Scalar& s, Mat& c, const Mat& mask)
{
    binarySMaskOp(a, s, c, mask,
        bitwiseSOp_<OrOp<uchar>, OrOp<int>, VOr8u>);
}

void bitwise_xor(const Mat& a, const Scalar& s, Mat& c, const Mat& mask)
{
    binarySMaskOp(a, s, c, mask,
        bitwiseSOp_<XorOp<uchar>, XorOp<int>, VXor8u>);
}


void bitwise_not(const Mat& src, Mat& dst)
{
V
Vadim Pisarevsky 已提交
837 838 839 840 841 842
    if( src.dims > 2 )
    {
        dst.create(src.dims, src.size, src.type());
        const Mat* arrays[] = { &src, &dst, 0 };
        Mat planes[4];
        NAryMatIterator it(arrays, planes);
843

V
Vadim Pisarevsky 已提交
844 845 846 847
        for( int i = 0; i < it.nplanes; i++, ++it )
            bitwise_not(it.planes[0], it.planes[1]);
        return;
    }
848

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
    const uchar* sptr = src.data;
    dst.create( src.size(), src.type() );
    uchar* dptr = dst.data;
    Size size = getContinuousSize( src, dst, (int)src.elemSize() );

    for( ; size.height--; sptr += src.step, dptr += dst.step )
    {
        int i = 0;
        if( (((size_t)sptr | (size_t)dptr) & 3) == 0 )
        {
            for( ; i <= size.width - 16; i += 16 )
            {
                int t0 = ~((const int*)(sptr+i))[0];
                int t1 = ~((const int*)(sptr+i))[1];

                ((int*)(dptr+i))[0] = t0;
                ((int*)(dptr+i))[1] = t1;

                t0 = ~((const int*)(sptr+i))[2];
                t1 = ~((const int*)(sptr+i))[3];

                ((int*)(dptr+i))[2] = t0;
                ((int*)(dptr+i))[3] = t1;
            }

            for( ; i <= size.width - 4; i += 4 )
                *(int*)(dptr+i) = ~*(const int*)(sptr+i);
        }

        for( ; i < size.width; i++ )
        {
            dptr[i] = (uchar)(~sptr[i]);
        }
    }
}

/****************************************************************************************\
*                                      add/subtract                                      *
\****************************************************************************************/

template<> inline uchar OpAdd<uchar>::operator ()(uchar a, uchar b) const
{ return CV_FAST_CAST_8U(a + b); }
template<> inline uchar OpSub<uchar>::operator ()(uchar a, uchar b) const
{ return CV_FAST_CAST_8U(a - b); }

static BinaryFunc addTab[] =
{
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
    binaryOpC1_<OpAdd<uchar>,  ippAdd8u>,
    0,
    binaryOpC1_<OpAdd<ushort>, ippAdd16u>,
    binaryOpC1_<OpAdd<short>,  ippAdd16s>,
    binaryOpC1_<OpAdd<int>,    ippAdd32s>,
    binaryOpC1_<OpAdd<float>,  ippAdd32f>,
    binaryOpC1_<OpAdd<double>, ippAdd64f>,
    0
#else
    binaryOpC1_<OpAdd<uchar>,  VAdd8u>,
    0,
    binaryOpC1_<OpAdd<ushort>, VAdd16u>,
    binaryOpC1_<OpAdd<short>,  VAdd16s>,
    binaryOpC1_<OpAdd<int>,    NoVec>,
    binaryOpC1_<OpAdd<float>,  VAdd32f>,
    binaryOpC1_<OpAdd<double>, NoVec>,
    0
#endif
915 916 917 918
};

static BinaryFunc subTab[] =
{
919 920 921 922 923 924 925 926 927 928
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
    binaryOpC1_<OpSub<uchar>,  ippSub8u>,
    0,
    binaryOpC1_<OpSub<ushort>, ippSub16u>,
    binaryOpC1_<OpSub<short>,  ippSub16s>,
    binaryOpC1_<OpSub<int>,    ippSub32s>,
    binaryOpC1_<OpSub<float>,  ippSub32f>,
    binaryOpC1_<OpSub<double>, ippSub64f>,
    0
#else
929 930 931 932 933 934 935 936
    binaryOpC1_<OpSub<uchar>,  VSub8u>,
    0,
    binaryOpC1_<OpSub<ushort>, VSub16u>,
    binaryOpC1_<OpSub<short>,  VSub16s>,
    binaryOpC1_<OpSub<int>,    NoVec>,
    binaryOpC1_<OpSub<float>,  VSub32f>,
    binaryOpC1_<OpSub<double>, NoVec>,
    0
937
#endif
938 939 940 941
};

void add( const Mat& src1, const Mat& src2, Mat& dst )
{
V
Vadim Pisarevsky 已提交
942
    int type = src1.type();
943
    BinaryFunc func = addTab[CV_MAT_DEPTH(type)];
V
Vadim Pisarevsky 已提交
944
    CV_Assert( type == src2.type() && func != 0 );
945

V
Vadim Pisarevsky 已提交
946 947 948 949 950 951
    if( src1.dims > 2 || src2.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = {&src1, &src2, &dst, 0};
        Mat planes[3];
        NAryMatIterator it(arrays, planes);
952

V
Vadim Pisarevsky 已提交
953 954 955 956
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2] );
        return;
    }
957

V
Vadim Pisarevsky 已提交
958 959
    Size size = src1.size();
    CV_Assert( size == src2.size() );
960 961 962 963 964 965
    dst.create( size, type );
    func(src1, src2, dst);
}

void subtract( const Mat& src1, const Mat& src2, Mat& dst )
{
V
Vadim Pisarevsky 已提交
966
    int type = src1.type();
967
    BinaryFunc func = subTab[CV_MAT_DEPTH(type)];
V
Vadim Pisarevsky 已提交
968
    CV_Assert( type == src2.type() && func != 0 );
969

V
Vadim Pisarevsky 已提交
970 971 972 973 974 975
    if( src1.dims > 2 || src2.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = {&src1, &src2, &dst, 0};
        Mat planes[3];
        NAryMatIterator it(arrays, planes);
976

V
Vadim Pisarevsky 已提交
977 978 979 980
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2] );
        return;
    }
981

V
Vadim Pisarevsky 已提交
982 983
    Size size = src1.size();
    CV_Assert( size == src2.size() );
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    dst.create( size, type );
    func(src1, src2, dst);
}

void subtract(const Mat& a, const Scalar& s, Mat& c, const Mat& mask)
{
    add(a, -s, c, mask);
}

void add(const Mat& src1, const Mat& src2, Mat& dst, const Mat& mask)
{
    binaryMaskOp(src1, src2, dst, mask, addTab[src1.depth()] );
}

void subtract(const Mat& src1, const Mat& src2, Mat& dst, const Mat& mask)
{
    binaryMaskOp(src1, src2, dst, mask, subTab[src1.depth()] );
}

void add(const Mat& src1, const Scalar& s, Mat& dst, const Mat& mask)
{
    static BinarySFuncCn addSTab[] =
    {
1007 1008
        binarySOpCn_<OpAdd<uchar, int, uchar> >,
        0,
1009 1010 1011 1012
        binarySOpCn_<OpAdd<ushort, int, ushort> >,
        binarySOpCn_<OpAdd<short, int, short> >,
        binarySOpCn_<OpAdd<int> >,
        binarySOpCn_<OpAdd<float> >,
1013 1014
        binarySOpCn_<OpAdd<double> >,
        0
1015 1016 1017 1018 1019 1020 1021 1022 1023
    };
    int depth = src1.depth();
    binarySMaskOp(src1, s, dst, mask, addSTab[depth]);
}

void subtract(const Scalar& s, const Mat& src1, Mat& dst, const Mat& mask)
{
    static BinarySFuncCn rsubSTab[] =
    {
1024 1025
        binarySOpCn_<OpRSub<uchar, int, uchar> >,
        0,
1026 1027 1028 1029
        binarySOpCn_<OpRSub<ushort, int, ushort> >,
        binarySOpCn_<OpRSub<short, int, short> >,
        binarySOpCn_<OpRSub<int> >,
        binarySOpCn_<OpRSub<float> >,
1030 1031
        binarySOpCn_<OpRSub<double> >,
        0
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
    };
    int depth = src1.depth();
    binarySMaskOp(src1, s, dst, mask, rsubSTab[depth]);
}

/****************************************************************************************\
*                                    multiply/divide                                     *
\****************************************************************************************/

template<typename T, typename WT> static void
mul_( const Mat& srcmat1, const Mat& srcmat2, Mat& dstmat, double _scale )
{
    const T* src1 = (const T*)srcmat1.data;
    const T* src2 = (const T*)srcmat2.data;
    T* dst = (T*)dstmat.data;
    size_t step1 = srcmat1.step/sizeof(src1[0]);
    size_t step2 = srcmat2.step/sizeof(src2[0]);
    size_t step = dstmat.step/sizeof(dst[0]);
    Size size = getContinuousSize( srcmat1, srcmat2, dstmat, dstmat.channels() );

    if( fabs(_scale - 1.) < DBL_EPSILON )
    {
        for( ; size.height--; src1+=step1, src2+=step2, dst+=step )
        {
            int i;
            for( i = 0; i <= size.width - 4; i += 4 )
            {
1059 1060 1061 1062 1063 1064
                T t0;
                T t1;
                t0 = saturate_cast<T>(src1[i  ] * src2[i  ]);
                t1 = saturate_cast<T>(src1[i+1] * src2[i+1]);
                dst[i  ] = t0;
                dst[i+1] = t1;
1065 1066 1067

                t0 = saturate_cast<T>(src1[i+2] * src2[i+2]);
                t1 = saturate_cast<T>(src1[i+3] * src2[i+3]);
1068 1069
                dst[i+2] = t0;
                dst[i+3] = t1;
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
            }

            for( ; i < size.width; i++ )
                dst[i] = saturate_cast<T>(src1[i] * src2[i]);
        }
    }
    else
    {
        WT scale = (WT)_scale;
        for( ; size.height--; src1+=step1, src2+=step2, dst+=step )
        {
            int i;
            for( i = 0; i <= size.width - 4; i += 4 )
            {
                T t0 = saturate_cast<T>(scale*(WT)src1[i]*src2[i]);
                T t1 = saturate_cast<T>(scale*(WT)src1[i+1]*src2[i+1]);
                dst[i] = t0; dst[i+1] = t1;

                t0 = saturate_cast<T>(scale*(WT)src1[i+2]*src2[i+2]);
                t1 = saturate_cast<T>(scale*(WT)src1[i+3]*src2[i+3]);
                dst[i+2] = t0; dst[i+3] = t1;
            }

            for( ; i < size.width; i++ )
                dst[i] = saturate_cast<T>(scale*(WT)src1[i]*src2[i]);
        }
    }
}

typedef void (*MulDivFunc)( const Mat& src1, const Mat& src2,
                            Mat& dst, double scale );

void multiply(const Mat& src1, const Mat& src2, Mat& dst, double scale)
{
    static MulDivFunc tab[] =
    {
1106 1107 1108 1109 1110 1111 1112 1113
        mul_<uchar, float>,
        0,
        mul_<ushort, float>,
        mul_<short, float>,
        mul_<int, double>,
        mul_<float, float>,
        mul_<double, double>,
        0
1114 1115 1116
    };

    MulDivFunc func = tab[src1.depth()];
V
Vadim Pisarevsky 已提交
1117
    CV_Assert( src1.type() == src2.type() && func != 0 );
1118

V
Vadim Pisarevsky 已提交
1119 1120 1121 1122 1123 1124
    if( src1.dims > 2 || src2.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = {&src1, &src2, &dst, 0};
        Mat planes[3];
        NAryMatIterator it(arrays, planes);
1125

V
Vadim Pisarevsky 已提交
1126 1127 1128 1129
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2], scale );
        return;
    }
1130

V
Vadim Pisarevsky 已提交
1131
    CV_Assert( src1.size() == src2.size() );
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
    dst.create( src1.size(), src1.type() );
    func( src1, src2, dst, scale );
}


template<typename T> static void
div_( const Mat& srcmat1, const Mat& srcmat2, Mat& dstmat, double scale )
{
    const T* src1 = (const T*)srcmat1.data;
    const T* src2 = (const T*)srcmat2.data;
    T* dst = (T*)dstmat.data;
    size_t step1 = srcmat1.step/sizeof(src1[0]);
    size_t step2 = srcmat2.step/sizeof(src2[0]);
    size_t step = dstmat.step/sizeof(dst[0]);
    Size size = getContinuousSize( srcmat1, srcmat2, dstmat, dstmat.channels() );

    for( ; size.height--; src1+=step1, src2+=step2, dst+=step )
    {
        int i = 0;
        for( ; i <= size.width - 4; i += 4 )
        {
            if( src2[i] != 0 && src2[i+1] != 0 && src2[i+2] != 0 && src2[i+3] != 0 )
            {
                double a = (double)src2[i] * src2[i+1];
                double b = (double)src2[i+2] * src2[i+3];
                double d = scale/(a * b);
                b *= d;
                a *= d;

                T z0 = saturate_cast<T>(src2[i+1] * src1[i] * b);
                T z1 = saturate_cast<T>(src2[i] * src1[i+1] * b);
                T z2 = saturate_cast<T>(src2[i+3] * src1[i+2] * a);
                T z3 = saturate_cast<T>(src2[i+2] * src1[i+3] * a);

                dst[i] = z0; dst[i+1] = z1;
                dst[i+2] = z2; dst[i+3] = z3;
            }
            else
            {
                T z0 = src2[i] != 0 ? saturate_cast<T>(src1[i]*scale/src2[i]) : 0;
                T z1 = src2[i+1] != 0 ? saturate_cast<T>(src1[i+1]*scale/src2[i+1]) : 0;
                T z2 = src2[i+2] != 0 ? saturate_cast<T>(src1[i+2]*scale/src2[i+2]) : 0;
                T z3 = src2[i+3] != 0 ? saturate_cast<T>(src1[i+3]*scale/src2[i+3]) : 0;

                dst[i] = z0; dst[i+1] = z1;
                dst[i+2] = z2; dst[i+3] = z3;
            }
        }

        for( ; i < size.width; i++ )
            dst[i] = src2[i] != 0 ? saturate_cast<T>(src1[i]*scale/src2[i]) : 0;
    }
}


void divide(const Mat& src1, const Mat& src2, Mat& dst, double scale)
{
    static MulDivFunc tab[] =
    {
        div_<uchar>, 0, div_<ushort>, div_<short>,
        div_<int>, div_<float>, div_<double>, 0
    };

    MulDivFunc func = tab[src1.depth()];
    CV_Assert( src1.size() == src2.size() && src1.type() == src2.type() && func != 0 );
1197

V
Vadim Pisarevsky 已提交
1198 1199 1200 1201 1202 1203
    if( src1.dims > 2 || src2.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = {&src1, &src2, &dst, 0};
        Mat planes[3];
        NAryMatIterator it(arrays, planes);
1204

V
Vadim Pisarevsky 已提交
1205 1206 1207 1208
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2], scale );
        return;
    }
1209

V
Vadim Pisarevsky 已提交
1210
    CV_Assert( src1.size() == src2.size() );
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
    dst.create( src1.size(), src1.type() );
    func( src1, src2, dst, scale );
}

template<typename T> static void
recip_( double scale, const Mat& srcmat2, Mat& dstmat )
{
    const T* src2 = (const T*)srcmat2.data;
    T* dst = (T*)dstmat.data;
    size_t step2 = srcmat2.step/sizeof(src2[0]);
    size_t step = dstmat.step/sizeof(dst[0]);
    Size size = getContinuousSize( srcmat2, dstmat, dstmat.channels() );

    for( ; size.height--; src2+=step2, dst+=step )
    {
        int i = 0;
        for( ; i <= size.width - 4; i += 4 )
        {
            if( src2[i] != 0 && src2[i+1] != 0 && src2[i+2] != 0 && src2[i+3] != 0 )
            {
                double a = (double)src2[i] * src2[i+1];
                double b = (double)src2[i+2] * src2[i+3];
                double d = scale/(a * b);
                b *= d;
                a *= d;

                T z0 = saturate_cast<T>(src2[i+1] * b);
                T z1 = saturate_cast<T>(src2[i] * b);
                T z2 = saturate_cast<T>(src2[i+3] * a);
                T z3 = saturate_cast<T>(src2[i+2] * a);

                dst[i] = z0; dst[i+1] = z1;
                dst[i+2] = z2; dst[i+3] = z3;
            }
            else
            {
                T z0 = src2[i] != 0 ? saturate_cast<T>(scale/src2[i]) : 0;
                T z1 = src2[i+1] != 0 ? saturate_cast<T>(scale/src2[i+1]) : 0;
                T z2 = src2[i+2] != 0 ? saturate_cast<T>(scale/src2[i+2]) : 0;
                T z3 = src2[i+3] != 0 ? saturate_cast<T>(scale/src2[i+3]) : 0;

                dst[i] = z0; dst[i+1] = z1;
                dst[i+2] = z2; dst[i+3] = z3;
            }
        }

        for( ; i < size.width; i++ )
            dst[i] = src2[i] != 0 ? saturate_cast<T>(scale/src2[i]) : 0;
    }
}

typedef void (*RecipFunc)( double scale, const Mat& src, Mat& dst );

void divide(double scale, const Mat& src, Mat& dst)
{
    static RecipFunc tab[] =
    {
1268 1269 1270 1271 1272 1273 1274 1275
        recip_<uchar>,
        0,
        recip_<ushort>,
        recip_<short>,
        recip_<int>,
        recip_<float>,
        recip_<double>,
        0
1276 1277 1278 1279
    };

    RecipFunc func = tab[src.depth()];
    CV_Assert( func != 0 );
1280

V
Vadim Pisarevsky 已提交
1281 1282 1283 1284 1285 1286
    if( src.dims > 2 )
    {
        dst.create(src.dims, src.size, src.type());
        const Mat* arrays[] = {&src, &dst, 0};
        Mat planes[2];
        NAryMatIterator it(arrays, planes);
1287

V
Vadim Pisarevsky 已提交
1288 1289 1290 1291
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( scale, it.planes[0], it.planes[1] );
        return;
    }
1292

1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
    dst.create( src.size(), src.type() );
    func( scale, src, dst );
}

/****************************************************************************************\
*                                      addWeighted                                       *
\****************************************************************************************/

template<typename T, typename WT> static void
addWeighted_( const Mat& srcmat1, double _alpha, const Mat& srcmat2,
              double _beta, double _gamma, Mat& dstmat )
{
    const T* src1 = (const T*)srcmat1.data;
    const T* src2 = (const T*)srcmat2.data;
    T* dst = (T*)dstmat.data;
    size_t step1 = srcmat1.step/sizeof(src1[0]);
    size_t step2 = srcmat2.step/sizeof(src2[0]);
    size_t step = dstmat.step/sizeof(dst[0]);
    Size size = getContinuousSize( srcmat1, srcmat2, dstmat, dstmat.channels() );
    WT alpha = (WT)_alpha, beta = (WT)_beta, gamma = (WT)_gamma;

    for( ; size.height--; src1+=step1, src2+=step2, dst+=step )
    {
        int i = 0;
        for( ; i <= size.width - 4; i += 4 )
        {
            T t0 = saturate_cast<T>(src1[i]*alpha + src2[i]*beta + gamma);
            T t1 = saturate_cast<T>(src1[i+1]*alpha + src2[i+1]*beta + gamma);
            dst[i] = t0; dst[i+1] = t1;

            t0 = saturate_cast<T>(src1[i+2]*alpha + src2[i+2]*beta + gamma);
            t1 = saturate_cast<T>(src1[i+3]*alpha + src2[i+3]*beta + gamma);
            dst[i+2] = t0; dst[i+3] = t1;
        }

        for( ; i < size.width; i++ )
            dst[i] = saturate_cast<T>(src1[i]*alpha + src2[i]*beta + gamma);
    }
}


static void
addWeighted8u( const Mat& srcmat1, double alpha,
               const Mat& srcmat2, double beta,
               double gamma, Mat& dstmat )
{
    const int shift = 14;
    if( srcmat1.rows*srcmat1.cols*srcmat1.channels() <= 256 ||
        fabs(alpha) > 256 || fabs(beta) > 256 || fabs(gamma) > 256*256 )
    {
        addWeighted_<uchar, float>(srcmat1, alpha, srcmat2, beta, gamma, dstmat);
        return;
    }
    const uchar* src1 = srcmat1.data;
    const uchar* src2 = srcmat2.data;
    uchar* dst = dstmat.data;
    size_t step1 = srcmat1.step;
    size_t step2 = srcmat2.step;
    size_t step = dstmat.step;
    Size size = getContinuousSize( srcmat1, srcmat2, dstmat, dstmat.channels() );

    int tab1[256], tab2[256];
    double t = 0;
    int j, t0, t1, t2, t3;

    alpha *= 1 << shift;
    gamma = gamma*(1 << shift) + (1 << (shift - 1));
    beta *= 1 << shift;

    for( j = 0; j < 256; j++ )
    {
        tab1[j] = cvRound(t);
        tab2[j] = cvRound(gamma);
        t += alpha;
        gamma += beta;
    }

    t0 = (tab1[0] + tab2[0]) >> shift;
    t1 = (tab1[0] + tab2[255]) >> shift;
    t2 = (tab1[255] + tab2[0]) >> shift;
    t3 = (tab1[255] + tab2[255]) >> shift;

    if( (unsigned)(t0+256) < 768 && (unsigned)(t1+256) < 768 &&
        (unsigned)(t2+256) < 768 && (unsigned)(t3+256) < 768 )
    {
        // use faster table-based convertion back to 8u
        for( ; size.height--; src1 += step1, src2 += step2, dst += step )
        {
            int i;

            for( i = 0; i <= size.width - 4; i += 4 )
            {
                t0 = CV_FAST_CAST_8U((tab1[src1[i]] + tab2[src2[i]]) >> shift);
                t1 = CV_FAST_CAST_8U((tab1[src1[i+1]] + tab2[src2[i+1]]) >> shift);

                dst[i] = (uchar)t0;
                dst[i+1] = (uchar)t1;

                t0 = CV_FAST_CAST_8U((tab1[src1[i+2]] + tab2[src2[i+2]]) >> shift);
                t1 = CV_FAST_CAST_8U((tab1[src1[i+3]] + tab2[src2[i+3]]) >> shift);

                dst[i+2] = (uchar)t0;
                dst[i+3] = (uchar)t1;
            }

            for( ; i < size.width; i++ )
            {
                t0 = CV_FAST_CAST_8U((tab1[src1[i]] + tab2[src2[i]]) >> shift);
                dst[i] = (uchar)t0;
            }
        }
    }
    else
    {
        // use universal macro for convertion back to 8u
        for( ; size.height--; src1 += step1, src2 += step2, dst += step )
        {
            int i;

            for( i = 0; i <= size.width - 4; i += 4 )
            {
                t0 = (tab1[src1[i]] + tab2[src2[i]]) >> shift;
                t1 = (tab1[src1[i+1]] + tab2[src2[i+1]]) >> shift;

                dst[i] = CV_CAST_8U( t0 );
                dst[i+1] = CV_CAST_8U( t1 );

                t0 = (tab1[src1[i+2]] + tab2[src2[i+2]]) >> shift;
                t1 = (tab1[src1[i+3]] + tab2[src2[i+3]]) >> shift;

                dst[i+2] = CV_CAST_8U( t0 );
                dst[i+3] = CV_CAST_8U( t1 );
            }

            for( ; i < size.width; i++ )
            {
                t0 = (tab1[src1[i]] + tab2[src2[i]]) >> shift;
                dst[i] = CV_CAST_8U( t0 );
            }
        }
    }
}

typedef void (*AddWeightedFunc)( const Mat& src1, double alpha, const Mat& src2,
                                 double beta, double gamma, Mat& dst );

void addWeighted( const Mat& src1, double alpha, const Mat& src2,
                  double beta, double gamma, Mat& dst )
{
1442
    static AddWeightedFunc tab[] =
1443
    {
1444 1445 1446 1447 1448 1449 1450 1451
        addWeighted8u,
        0,
        addWeighted_<ushort, float>,
        addWeighted_<short, float>,
        addWeighted_<int, double>,
        addWeighted_<float, float>,
        addWeighted_<double, double>,
        0
1452 1453 1454
    };

    AddWeightedFunc func = tab[src1.depth()];
V
Vadim Pisarevsky 已提交
1455
    CV_Assert( src1.type() == src2.type() && func != 0 );
1456

V
Vadim Pisarevsky 已提交
1457 1458 1459 1460 1461 1462
    if( src1.dims > 2 || src2.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = {&src1, &src2, &dst, 0};
        Mat planes[3];
        NAryMatIterator it(arrays, planes);
1463

V
Vadim Pisarevsky 已提交
1464 1465 1466 1467
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], alpha, it.planes[1], beta, gamma, it.planes[2] );
        return;
    }
1468

V
Vadim Pisarevsky 已提交
1469
    CV_Assert( src1.size() == src2.size() );
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
    dst.create( src1.size(), src1.type() );
    func( src1, alpha, src2, beta, gamma, dst );
}


/****************************************************************************************\
*                                      absdiff                                           *
\****************************************************************************************/

template<typename T> struct OpAbsDiff
{
    typedef T type1;
    typedef T type2;
    typedef T rtype;
    T operator()(T a, T b) { return (T)std::abs(a - b); }
};

template<> inline short OpAbsDiff<short>::operator ()(short a, short b)
{ return saturate_cast<short>(std::abs(a - b)); }

template<typename T, typename WT=T> struct OpAbsDiffS
{
    typedef T type1;
    typedef WT type2;
    typedef T rtype;
    T operator()(T a, WT b) { return saturate_cast<T>(std::abs(a - b)); }
};

void absdiff( const Mat& src1, const Mat& src2, Mat& dst )
{
    static BinaryFunc tab[] =
    {
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
        binaryOpC1_<OpAbsDiff<uchar>,ippAbsDiff8u>,
        0,
        binaryOpC1_<OpAbsDiff<ushort>,ippAbsDiff16u>,
        binaryOpC1_<OpAbsDiff<short>,VAbsDiff16s>,
        binaryOpC1_<OpAbsDiff<int>,NoVec>,
        binaryOpC1_<OpAbsDiff<float>,ippAbsDiff32f>,
        binaryOpC1_<OpAbsDiff<double>,NoVec>,
        0
#else
1512 1513
        binaryOpC1_<OpAbsDiff<uchar>,VAbsDiff8u>,
        0,
1514 1515 1516 1517
        binaryOpC1_<OpAbsDiff<ushort>,VAbsDiff16u>,
        binaryOpC1_<OpAbsDiff<short>,VAbsDiff16s>,
        binaryOpC1_<OpAbsDiff<int>,NoVec>,
        binaryOpC1_<OpAbsDiff<float>,VAbsDiff32f>,
1518 1519
        binaryOpC1_<OpAbsDiff<double>,NoVec>,
        0
1520
#endif
1521 1522
    };

V
Vadim Pisarevsky 已提交
1523
    binaryOp(src1, src2, dst, tab[src1.depth()]);
1524 1525 1526 1527 1528 1529 1530
}


void absdiff( const Mat& src1, const Scalar& s, Mat& dst )
{
    static BinarySFuncCn tab[] =
    {
1531 1532
        binarySOpCn_<OpAbsDiffS<uchar, int> >,
        0,
1533 1534 1535 1536
        binarySOpCn_<OpAbsDiffS<ushort, int> >,
        binarySOpCn_<OpAbsDiffS<short, int> >,
        binarySOpCn_<OpAbsDiffS<int> >,
        binarySOpCn_<OpAbsDiffS<float> >,
1537 1538
        binarySOpCn_<OpAbsDiffS<double> >,
        0
1539 1540 1541 1542
    };

    BinarySFuncCn func = tab[src1.depth()];
    CV_Assert(src1.channels() <= 4 && func != 0);
1543

V
Vadim Pisarevsky 已提交
1544 1545 1546 1547 1548 1549
    if( src1.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = {&src1, &dst, 0};
        Mat planes[3];
        NAryMatIterator it(arrays, planes);
1550

V
Vadim Pisarevsky 已提交
1551 1552 1553 1554 1555 1556
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], s );
        return;
    }

    dst.create(src1.size(), src1.type());
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
    func( src1, dst, s );
}

/****************************************************************************************\
*                                      inRange[S]                                        *
\****************************************************************************************/

template<typename T, typename WT> struct InRangeC1
{
    typedef T xtype;
    typedef WT btype;
    uchar operator()(xtype x, btype a, btype b) const
    { return (uchar)-(a <= x && x < b); }
};

template<typename T, typename WT> struct InRangeC2
{
    typedef Vec<T,2> xtype;
    typedef Vec<WT,2> btype;
    uchar operator()(const xtype& x, const btype& a, const btype& b) const
    {
        return (uchar)-(a[0] <= x[0] && x[0] < b[0] &&
                        a[1] <= x[1] && x[1] < b[1]);
    }
};

template<typename T, typename WT> struct InRangeC3
{
    typedef Vec<T,3> xtype;
    typedef Vec<WT,3> btype;
    uchar operator()(const xtype& x, const btype& a, const btype& b) const
    {
        return (uchar)-(a[0] <= x[0] && x[0] < b[0] &&
                        a[1] <= x[1] && x[1] < b[1] &&
                        a[2] <= x[2] && x[2] < b[2]);
    }
};

template<typename T, typename WT> struct InRangeC4
{
    typedef Vec<T,4> xtype;
    typedef Vec<WT,4> btype;
    uchar operator()(const xtype& x, const btype& a, const btype& b) const
    {
        return (uchar)-(a[0] <= x[0] && x[0] < b[0] &&
                        a[1] <= x[1] && x[1] < b[1] &&
                        a[2] <= x[2] && x[2] < b[2] &&
                        a[3] <= x[3] && x[3] < b[3]);
    }
};

template<class Op> static void
inRange_( const Mat& srcmat1, const Mat& srcmat2, const Mat& srcmat3, Mat& dstmat )
{
    Op op;
    uchar* dst = dstmat.data;
    size_t dstep = dstmat.step;
    Size size = getContinuousSize( srcmat1, srcmat2, srcmat3, dstmat );

    for( int y = 0; y < size.height; y++, dst += dstep )
    {
        const typename Op::xtype* src1 = (const typename Op::xtype*)(srcmat1.data + srcmat1.step*y);
        const typename Op::xtype* src2 = (const typename Op::xtype*)(srcmat2.data + srcmat2.step*y);
        const typename Op::xtype* src3 = (const typename Op::xtype*)(srcmat3.data + srcmat3.step*y);
        for( int x = 0; x < size.width; x++ )
            dst[x] = op( src1[x], src2[x], src3[x] );
    }
}

template<class Op> static void
inRangeS_( const Mat& srcmat1, const Scalar& _a, const Scalar& _b, Mat& dstmat )
{
    Op op;
    typedef typename Op::btype WT;
    typedef typename DataType<WT>::channel_type WT1;
    WT a, b;
    uchar* dst = dstmat.data;
    size_t dstep = dstmat.step;
    Size size = getContinuousSize( srcmat1, dstmat );
    int cn = srcmat1.channels();
1637 1638
    scalarToRawData(_a, &a, CV_MAKETYPE(DataType<WT>::depth, cn));
    scalarToRawData(_b, &b, CV_MAKETYPE(DataType<WT>::depth, cn));
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655

    for( int y = 0; y < size.height; y++, dst += dstep )
    {
        const typename Op::xtype* src1 = (const typename Op::xtype*)(srcmat1.data + srcmat1.step*y);
        for( int x = 0; x < size.width; x++ )
            dst[x] = op( src1[x], a, b );
    }
}

typedef void (*InRangeFunc)( const Mat& src1, const Mat& src2, const Mat& src3, Mat& dst );
typedef void (*InRangeSFunc)( const Mat& src1, const Scalar& a, const Scalar& b, Mat& dst );

void inRange(const Mat& src, const Mat& lowerb,
             const Mat& upperb, Mat& dst)
{
    static InRangeFunc tab[] =
    {
1656 1657
        inRange_<InRangeC1<uchar, uchar> >,
        0,
1658 1659 1660 1661
        inRange_<InRangeC1<ushort, ushort> >,
        inRange_<InRangeC1<short, short> >,
        inRange_<InRangeC1<int, int> >,
        inRange_<InRangeC1<float, float> >,
1662 1663
        inRange_<InRangeC1<double, double> >,
        0,
1664

1665 1666
        inRange_<InRangeC2<uchar, uchar> >,
        0,
1667 1668 1669 1670
        inRange_<InRangeC2<ushort, ushort> >,
        inRange_<InRangeC2<short, short> >,
        inRange_<InRangeC2<int, int> >,
        inRange_<InRangeC2<float, float> >,
1671 1672
        inRange_<InRangeC2<double, double> >,
        0,
1673

1674 1675
        inRange_<InRangeC3<uchar, uchar> >,
        0,
1676 1677 1678 1679
        inRange_<InRangeC3<ushort, ushort> >,
        inRange_<InRangeC3<short, short> >,
        inRange_<InRangeC3<int, int> >,
        inRange_<InRangeC3<float, float> >,
1680 1681
        inRange_<InRangeC3<double, double> >,
        0,
1682

1683 1684
        inRange_<InRangeC4<uchar, uchar> >,
        0,
1685 1686 1687 1688
        inRange_<InRangeC4<ushort, ushort> >,
        inRange_<InRangeC4<short, short> >,
        inRange_<InRangeC4<int, int> >,
        inRange_<InRangeC4<float, float> >,
1689 1690
        inRange_<InRangeC4<double, double> >,
        0
1691 1692
    };

V
Vadim Pisarevsky 已提交
1693
    CV_Assert( src.type() == lowerb.type() && src.type() == upperb.type() && src.channels() <= 4 );
1694 1695 1696 1697

    InRangeFunc func = tab[src.type()];
    CV_Assert( func != 0 );

V
Vadim Pisarevsky 已提交
1698 1699 1700 1701 1702 1703
    if( src.dims > 2 || lowerb.dims > 2 || upperb.dims > 2 )
    {
        dst.create(src.dims, src.size, CV_8U);
        const Mat* arrays[] = {&src, &lowerb, &upperb, &dst, 0};
        Mat planes[4];
        NAryMatIterator it(arrays, planes);
1704

V
Vadim Pisarevsky 已提交
1705 1706 1707 1708
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2], it.planes[3] );
        return;
    }
1709

V
Vadim Pisarevsky 已提交
1710
    CV_Assert( src.size() == lowerb.size() && src.size() == upperb.size() );
1711 1712 1713 1714 1715 1716 1717 1718 1719
    dst.create(src.size(), CV_8U);
    func( src, lowerb, upperb, dst );
}

void inRange(const Mat& src, const Scalar& lowerb,
             const Scalar& upperb, Mat& dst)
{
    static InRangeSFunc tab[] =
    {
1720 1721
        inRangeS_<InRangeC1<uchar, int> >,
        0,
1722 1723 1724 1725
        inRangeS_<InRangeC1<ushort, int> >,
        inRangeS_<InRangeC1<short, int> >,
        inRangeS_<InRangeC1<int, int> >,
        inRangeS_<InRangeC1<float, float> >,
1726 1727
        inRangeS_<InRangeC1<double, double> >,
        0,
1728

1729 1730
        inRangeS_<InRangeC2<uchar, int> >,
        0,
1731 1732 1733 1734
        inRangeS_<InRangeC2<ushort, int> >,
        inRangeS_<InRangeC2<short, int> >,
        inRangeS_<InRangeC2<int, int> >,
        inRangeS_<InRangeC2<float, float> >,
1735 1736
        inRangeS_<InRangeC2<double, double> >,
        0,
1737

1738 1739
        inRangeS_<InRangeC3<uchar, int> >,
        0,
1740 1741 1742 1743
        inRangeS_<InRangeC3<ushort, int> >,
        inRangeS_<InRangeC3<short, int> >,
        inRangeS_<InRangeC3<int, int> >,
        inRangeS_<InRangeC3<float, float> >,
1744 1745
        inRangeS_<InRangeC3<double, double> >,
        0,
1746

1747 1748
        inRangeS_<InRangeC4<uchar, int> >,
        0,
1749 1750 1751 1752
        inRangeS_<InRangeC4<ushort, int> >,
        inRangeS_<InRangeC4<short, int> >,
        inRangeS_<InRangeC4<int, int> >,
        inRangeS_<InRangeC4<float, float> >,
1753 1754
        inRangeS_<InRangeC4<double, double> >,
        0
1755 1756 1757 1758 1759 1760
    };

    CV_Assert( src.channels() <= 4 );

    InRangeSFunc func = tab[src.type()];
    CV_Assert( func != 0 );
1761

V
Vadim Pisarevsky 已提交
1762 1763 1764 1765 1766 1767
    if( src.dims > 2 )
    {
        dst.create(src.dims, src.size, CV_8U);
        const Mat* arrays[] = {&src, &dst, 0};
        Mat planes[2];
        NAryMatIterator it(arrays, planes);
1768

V
Vadim Pisarevsky 已提交
1769 1770 1771 1772
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], lowerb, upperb, it.planes[1] );
        return;
    }
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809

    dst.create(src.size(), CV_8U);
    func( src, lowerb, upperb, dst );
}

/****************************************************************************************\
*                                          compare                                       *
\****************************************************************************************/

template<typename T, typename WT=T> struct CmpEQ
{
    typedef T type1;
    typedef WT type2;
    typedef uchar rtype;
    uchar operator()(T a, WT b) const { return (uchar)-(a == b); }
};

template<typename T, typename WT=T> struct CmpGT
{
    typedef T type1;
    typedef WT type2;
    typedef uchar rtype;
    uchar operator()(T a, WT b) const { return (uchar)-(a > b); }
};

template<typename T, typename WT=T> struct CmpGE
{
    typedef T type1;
    typedef WT type2;
    typedef uchar rtype;
    uchar operator()(T a, WT b) const { return (uchar)-(a >= b); }
};

void compare( const Mat& src1, const Mat& src2, Mat& dst, int cmpOp )
{
    static BinaryFunc tab[][8] =
    {
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
        {
            binaryOpC1_<CmpGT<uchar>,VCmpGT8u>,
            0,
            binaryOpC1_<CmpGT<ushort>,NoVec>,
            binaryOpC1_<CmpGT<short>,NoVec>,
            binaryOpC1_<CmpGT<int>,NoVec>,
            binaryOpC1_<CmpGT<float>,NoVec>,
            binaryOpC1_<CmpGT<double>,NoVec>,
            0
        },

        {
            binaryOpC1_<CmpEQ<uchar>,VCmpEQ8u>,
            0,
            binaryOpC1_<CmpEQ<ushort>,NoVec>,
            binaryOpC1_<CmpEQ<ushort>,NoVec>, // same function as for ushort's
            binaryOpC1_<CmpEQ<int>,NoVec>,
            binaryOpC1_<CmpEQ<float>,NoVec>,
            binaryOpC1_<CmpEQ<double>,NoVec>,
            0
        },
1831 1832
    };

V
Vadim Pisarevsky 已提交
1833
    CV_Assert(src1.channels() == 1);
1834 1835 1836 1837 1838 1839 1840 1841 1842 1843

    int depth = src1.depth();
    const Mat *psrc1 = &src1, *psrc2 = &src2;
    bool invflag = false;

    switch( cmpOp )
    {
    case CMP_GT:
    case CMP_EQ:
        break;
1844

1845 1846 1847 1848
    case CMP_GE:
        std::swap( psrc1, psrc2 );
        invflag = true;
        break;
1849

1850 1851 1852
    case CMP_LT:
        std::swap( psrc1, psrc2 );
        break;
1853

1854 1855 1856
    case CMP_LE:
        invflag = true;
        break;
1857

1858 1859 1860 1861
    case CMP_NE:
        cmpOp = CMP_EQ;
        invflag = true;
        break;
1862

1863 1864 1865 1866 1867
    default:
        CV_Error(CV_StsBadArg, "Unknown comparison method");
    }

    BinaryFunc func = tab[cmpOp == CMP_EQ][depth];
V
Vadim Pisarevsky 已提交
1868
    binaryOp(*psrc1, *psrc2, dst, func, CV_8U);
1869 1870 1871 1872 1873 1874 1875 1876 1877
    if( invflag )
        bitwise_not(dst, dst);
}


void compare( const Mat& src1, double value, Mat& dst, int cmpOp )
{
    static BinarySFuncC1 tab[][8] =
    {
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909
        {
            binarySOpC1_<CmpEQ<uchar, int> >,
            0,
            binarySOpC1_<CmpEQ<ushort, int> >,
            binarySOpC1_<CmpEQ<short, int> >,
            binarySOpC1_<CmpEQ<int> >,
            binarySOpC1_<CmpEQ<float> >,
            binarySOpC1_<CmpEQ<double> >,
            0
        },

        {
            binarySOpC1_<CmpGT<uchar, int> >,
            0,
            binarySOpC1_<CmpGT<ushort, int> >,
            binarySOpC1_<CmpGT<short, int> >,
            binarySOpC1_<CmpGT<int> >,
            binarySOpC1_<CmpGT<float> >,
            binarySOpC1_<CmpGT<double> >,
            0
        },

        {
            binarySOpC1_<CmpGE<uchar, int> >,
            0,
            binarySOpC1_<CmpGE<ushort, int> >,
            binarySOpC1_<CmpGE<short, int> >,
            binarySOpC1_<CmpGE<int> >,
            binarySOpC1_<CmpGE<float> >,
            binarySOpC1_<CmpGE<double> >,
            0
        },
1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
    };

    int depth = src1.depth();
    bool invflag = false;

    switch( cmpOp )
    {
    case CMP_GT:
    case CMP_EQ:
    case CMP_GE:
        break;
1921

1922 1923 1924 1925
    case CMP_LT:
        invflag = true;
        cmpOp = CMP_GE;
        break;
1926

1927 1928 1929 1930
    case CMP_LE:
        invflag = true;
        cmpOp = CMP_GT;
        break;
1931

1932 1933 1934 1935
    case CMP_NE:
        invflag = true;
        cmpOp = CMP_EQ;
        break;
1936

1937 1938 1939 1940 1941 1942
    default:
        CV_Error(CV_StsBadArg, "Unknown comparison method");
    }

    BinarySFuncC1 func = tab[cmpOp == CMP_EQ ? 0 : cmpOp == CMP_GT ? 1 : 2][depth];
    CV_Assert( func != 0 );
1943

V
Vadim Pisarevsky 已提交
1944 1945
    if( src1.dims > 2 )
    {
1946
        dst.create(src1.dims, src1.size, CV_8UC(src1.channels()));
V
Vadim Pisarevsky 已提交
1947 1948 1949
        const Mat* arrays[] = {&src1, &dst, 0};
        Mat planes[2];
        NAryMatIterator it(arrays, planes);
1950

V
Vadim Pisarevsky 已提交
1951 1952 1953 1954 1955 1956 1957 1958
        for( int i = 0; i < it.nplanes; i++, ++it )
        {
            func( it.planes[0], it.planes[1], value );
            if( invflag )
                bitwise_not(it.planes[2], it.planes[2]);
        }
        return;
    }
1959

1960
    dst.create(src1.rows, src1.cols, CV_8UC(src1.channels()));
1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992
    func( src1, dst, value );
    if( invflag )
        bitwise_not(dst, dst);
}

/****************************************************************************************\
*                                       min/max                                          *
\****************************************************************************************/

template<typename T> struct MinOp
{
    typedef T type1;
    typedef T type2;
    typedef T rtype;
    T operator ()(T a, T b) const { return std::min(a, b); }
};

template<typename T> struct MaxOp
{
    typedef T type1;
    typedef T type2;
    typedef T rtype;
    T operator ()(T a, T b) const { return std::max(a, b); }
};

template<> inline uchar MinOp<uchar>::operator ()(uchar a, uchar b) const { return CV_MIN_8U(a, b); }
template<> inline uchar MaxOp<uchar>::operator ()(uchar a, uchar b) const { return CV_MAX_8U(a, b); }

void min( const Mat& src1, const Mat& src2, Mat& dst )
{
    static BinaryFunc tab[] =
    {
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
        binaryOpC1_<MinOp<uchar>,ippMin8u>,
        0,
        binaryOpC1_<MinOp<ushort>,ippMin16u>,
        binaryOpC1_<MinOp<short>,VMin16s>,
        binaryOpC1_<MinOp<int>,NoVec>,
        binaryOpC1_<MinOp<float>,ippMin32f>,
        binaryOpC1_<MinOp<double>,ippMin64f>,
        0
#else
2003 2004 2005 2006 2007 2008 2009 2010
        binaryOpC1_<MinOp<uchar>,VMin8u>,
        0,
        binaryOpC1_<MinOp<ushort>,VMin16u>,
        binaryOpC1_<MinOp<short>,VMin16s>,
        binaryOpC1_<MinOp<int>,NoVec>,
        binaryOpC1_<MinOp<float>,VMin32f>,
        binaryOpC1_<MinOp<double>,NoVec>,
        0
2011
#endif
2012 2013
    };

V
Vadim Pisarevsky 已提交
2014
    binaryOp(src1, src2, dst, tab[src1.depth()]);
2015 2016 2017 2018 2019 2020
}

void max( const Mat& src1, const Mat& src2, Mat& dst )
{
    static BinaryFunc tab[] =
    {
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030
#if defined (HAVE_IPP) && (IPP_VERSION_MAJOR >= 7)
        binaryOpC1_<MaxOp<uchar>,ippMax8u>,
        0,
        binaryOpC1_<MaxOp<ushort>,ippMax16u>,
        binaryOpC1_<MaxOp<short>,VMax16s>,
        binaryOpC1_<MaxOp<int>,NoVec>,
        binaryOpC1_<MaxOp<float>,ippMax32f>,
        binaryOpC1_<MaxOp<double>,ippMax64f>,
        0
#else
2031 2032 2033 2034 2035 2036 2037 2038
        binaryOpC1_<MaxOp<uchar>,VMax8u>,
        0,
        binaryOpC1_<MaxOp<ushort>,VMax16u>,
        binaryOpC1_<MaxOp<short>,VMax16s>,
        binaryOpC1_<MaxOp<int>,NoVec>,
        binaryOpC1_<MaxOp<float>,VMax32f>,
        binaryOpC1_<MaxOp<double>,NoVec>,
        0
2039
#endif
2040 2041
    };

V
Vadim Pisarevsky 已提交
2042
    binaryOp(src1, src2, dst, tab[src1.depth()]);
2043 2044 2045 2046 2047 2048
}

void min( const Mat& src1, double value, Mat& dst )
{
    static BinarySFuncC1 tab[] =
    {
2049 2050
        binarySOpC1_<MinOp<uchar> >,
        0,
2051 2052 2053 2054
        binarySOpC1_<MinOp<ushort> >,
        binarySOpC1_<MinOp<short> >,
        binarySOpC1_<MinOp<int> >,
        binarySOpC1_<MinOp<float> >,
2055 2056
        binarySOpC1_<MinOp<double> >,
        0
2057 2058 2059 2060
    };

    BinarySFuncC1 func = tab[src1.depth()];
    CV_Assert(func != 0);
2061

V
Vadim Pisarevsky 已提交
2062 2063 2064 2065 2066 2067
    if( src1.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = {&src1, &dst, 0};
        Mat planes[2];
        NAryMatIterator it(arrays, planes);
2068

V
Vadim Pisarevsky 已提交
2069 2070 2071 2072
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], value );
        return;
    }
2073

2074
    dst.create(src1.size(), src1.type());
2075

2076 2077 2078 2079 2080 2081 2082
    return func( src1, dst, value );
}

void max( const Mat& src1, double value, Mat& dst )
{
    static BinarySFuncC1 tab[] =
    {
2083 2084
        binarySOpC1_<MaxOp<uchar> >,
        0,
2085 2086 2087 2088
        binarySOpC1_<MaxOp<ushort> >,
        binarySOpC1_<MaxOp<short> >,
        binarySOpC1_<MaxOp<int> >,
        binarySOpC1_<MaxOp<float> >,
2089 2090
        binarySOpC1_<MaxOp<double> >,
        0
2091 2092 2093 2094
    };

    BinarySFuncC1 func = tab[src1.depth()];
    CV_Assert(func != 0);
2095

V
Vadim Pisarevsky 已提交
2096 2097 2098 2099 2100 2101
    if( src1.dims > 2 )
    {
        dst.create(src1.dims, src1.size, src1.type());
        const Mat* arrays[] = {&src1, &dst, 0};
        Mat planes[2];
        NAryMatIterator it(arrays, planes);
2102

V
Vadim Pisarevsky 已提交
2103 2104 2105 2106
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], value );
        return;
    }
2107

2108
    dst.create(src1.size(), src1.type());
2109

2110 2111 2112
    return func( src1, dst, value );
}

2113
} // namespace cv
2114 2115 2116 2117 2118 2119 2120 2121 2122

/****************************************************************************************\
*                                Earlier API: cvAdd etc.                                 *
\****************************************************************************************/

CV_IMPL void
cvNot( const CvArr* srcarr, CvArr* dstarr )
{
    cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
2123
    CV_Assert( src.size == dst.size && src.type() == dst.type() );
2124 2125 2126 2127 2128 2129 2130 2131 2132
    cv::bitwise_not( src, dst );
}


CV_IMPL void
cvAnd( const CvArr* srcarr1, const CvArr* srcarr2, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), src2 = cv::cvarrToMat(srcarr2),
        dst = cv::cvarrToMat(dstarr), mask;
2133
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2134 2135 2136 2137 2138
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::bitwise_and( src1, src2, dst, mask );
}

2139

2140 2141 2142 2143 2144
CV_IMPL void
cvOr( const CvArr* srcarr1, const CvArr* srcarr2, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), src2 = cv::cvarrToMat(srcarr2),
        dst = cv::cvarrToMat(dstarr), mask;
2145
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::bitwise_or( src1, src2, dst, mask );
}


CV_IMPL void
cvXor( const CvArr* srcarr1, const CvArr* srcarr2, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), src2 = cv::cvarrToMat(srcarr2),
        dst = cv::cvarrToMat(dstarr), mask;
2157
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::bitwise_xor( src1, src2, dst, mask );
}


CV_IMPL void
cvAndS( const CvArr* srcarr, CvScalar s, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), mask;
2168
    CV_Assert( src.size == dst.size && src.type() == dst.type() );
2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::bitwise_and( src, s, dst, mask );
}


CV_IMPL void
cvOrS( const CvArr* srcarr, CvScalar s, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), mask;
2179
    CV_Assert( src.size == dst.size && src.type() == dst.type() );
2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::bitwise_or( src, s, dst, mask );
}


CV_IMPL void
cvXorS( const CvArr* srcarr, CvScalar s, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr), mask;
2190
    CV_Assert( src.size == dst.size && src.type() == dst.type() );
2191 2192 2193 2194 2195
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::bitwise_xor( src, s, dst, mask );
}

2196

2197 2198 2199 2200
CV_IMPL void cvAdd( const CvArr* srcarr1, const CvArr* srcarr2, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), src2 = cv::cvarrToMat(srcarr2),
        dst = cv::cvarrToMat(dstarr), mask;
2201
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2202 2203 2204 2205 2206
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::add( src1, src2, dst, mask );
}

2207

2208 2209 2210 2211
CV_IMPL void cvSub( const CvArr* srcarr1, const CvArr* srcarr2, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), src2 = cv::cvarrToMat(srcarr2),
        dst = cv::cvarrToMat(dstarr), mask;
2212
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2213 2214 2215 2216 2217
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::subtract( src1, src2, dst, mask );
}

2218

2219 2220 2221 2222
CV_IMPL void cvAddS( const CvArr* srcarr1, CvScalar value, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1),
        dst = cv::cvarrToMat(dstarr), mask;
2223
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2224 2225 2226 2227 2228
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::add( src1, value, dst, mask );
}

2229

2230 2231 2232 2233
CV_IMPL void cvSubRS( const CvArr* srcarr1, CvScalar value, CvArr* dstarr, const CvArr* maskarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1),
        dst = cv::cvarrToMat(dstarr), mask;
2234
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2235 2236 2237 2238 2239
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::subtract( value, src1, dst, mask );
}

2240

2241 2242 2243 2244 2245
CV_IMPL void cvMul( const CvArr* srcarr1, const CvArr* srcarr2,
                    CvArr* dstarr, double scale )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), src2 = cv::cvarrToMat(srcarr2),
        dst = cv::cvarrToMat(dstarr);
2246
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2247 2248 2249
    cv::multiply( src1, src2, dst, scale );
}

2250

2251 2252 2253 2254 2255
CV_IMPL void cvDiv( const CvArr* srcarr1, const CvArr* srcarr2,
                    CvArr* dstarr, double scale )
{
    cv::Mat src2 = cv::cvarrToMat(srcarr2),
        dst = cv::cvarrToMat(dstarr), mask;
2256
    CV_Assert( src2.size == dst.size && src2.type() == dst.type() );
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271

    if( srcarr1 )
        cv::divide( cv::cvarrToMat(srcarr1), src2, dst, scale );
    else
        cv::divide( scale, src2, dst );
}


CV_IMPL void
cvAddWeighted( const CvArr* srcarr1, double alpha,
               const CvArr* srcarr2, double beta,
               double gamma, CvArr* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), src2 = cv::cvarrToMat(srcarr2),
        dst = cv::cvarrToMat(dstarr);
2272
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2273 2274 2275 2276 2277 2278 2279 2280
    cv::addWeighted( src1, alpha, src2, beta, gamma, dst );
}


CV_IMPL  void
cvAbsDiff( const CvArr* srcarr1, const CvArr* srcarr2, CvArr* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2281
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2282 2283 2284 2285 2286 2287 2288 2289 2290

    cv::absdiff( src1, cv::cvarrToMat(srcarr2), dst );
}


CV_IMPL void
cvAbsDiffS( const CvArr* srcarr1, CvArr* dstarr, CvScalar scalar )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2291
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2292 2293 2294 2295

    cv::absdiff( src1, scalar, dst );
}

2296

2297 2298 2299 2300 2301
CV_IMPL void
cvInRange( const void* srcarr1, const void* srcarr2,
           const void* srcarr3, void* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2302
    CV_Assert( src1.size == dst.size && dst.type() == CV_8U );
2303 2304 2305 2306

    cv::inRange( src1, cv::cvarrToMat(srcarr2), cv::cvarrToMat(srcarr3), dst );
}

2307

2308 2309 2310 2311
CV_IMPL void
cvInRangeS( const void* srcarr1, CvScalar lowerb, CvScalar upperb, void* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2312
    CV_Assert( src1.size == dst.size && dst.type() == CV_8U );
2313 2314 2315 2316 2317 2318 2319 2320 2321

    cv::inRange( src1, lowerb, upperb, dst );
}


CV_IMPL void
cvCmp( const void* srcarr1, const void* srcarr2, void* dstarr, int cmp_op )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2322
    CV_Assert( src1.size == dst.size && dst.type() == CV_8U );
2323 2324 2325 2326 2327 2328 2329 2330 2331

    cv::compare( src1, cv::cvarrToMat(srcarr2), dst, cmp_op );
}


CV_IMPL void
cvCmpS( const void* srcarr1, double value, void* dstarr, int cmp_op )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2332
    CV_Assert( src1.size == dst.size && dst.type() == CV_8U );
2333 2334 2335 2336 2337 2338 2339 2340 2341

    cv::compare( src1, value, dst, cmp_op );
}


CV_IMPL void
cvMin( const void* srcarr1, const void* srcarr2, void* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2342
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2343 2344 2345 2346 2347 2348 2349 2350 2351

    cv::min( src1, cv::cvarrToMat(srcarr2), dst );
}


CV_IMPL void
cvMax( const void* srcarr1, const void* srcarr2, void* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2352
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2353 2354 2355 2356

    cv::max( src1, cv::cvarrToMat(srcarr2), dst );
}

2357

2358 2359 2360 2361
CV_IMPL void
cvMinS( const void* srcarr1, double value, void* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2362
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2363 2364 2365 2366 2367 2368 2369 2370 2371

    cv::min( src1, value, dst );
}


CV_IMPL void
cvMaxS( const void* srcarr1, double value, void* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
2372
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
2373 2374 2375 2376 2377 2378

    cv::max( src1, value, dst );
}


/* End of file. */