arithm.cpp 62.3 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 51 52 53 54 55 56 57 58 59 60 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 200 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 235 236 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 269 270 271 272 273 274 275 276 277 278 279 280 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 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
/*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"

namespace cv
{

#if CV_SSE2

enum { ARITHM_SIMD = CV_CPU_SSE2 };
    
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); }};
struct _VOr8u { __m128i operator()(const __m128i& a, const __m128i& b) const { return _mm_or_si128(a,b); }};
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

enum { ARITHM_SIMD = CV_CPU_NONE };    
    
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

/****************************************************************************************\
*                                   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]);
    }
}

V
Vadim Pisarevsky 已提交
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
    
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 );
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func(it.planes[0], it.planes[1], it.planes[2]);
        return;
    }
    
    CV_Assert( src1.size() == src2.size() );
    dst.create( src1.size(), dsttype );
    func( src1, src2, dst );
}

    
428 429 430 431
static void
binaryMaskOp( const Mat& src1, const Mat& src2, Mat& dst,
              const Mat& mask, BinaryFunc func )
{
V
Vadim Pisarevsky 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    CV_Assert( src1.type() == src2.type() && func != 0 );
    
    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);
        
        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;
    }
    
    CV_Assert( src1.size() == src2.size() );
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
    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 );
V
Vadim Pisarevsky 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
    
    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);
        
        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;
    }
    
503 504 505 506 507 508 509 510 511 512 513 514 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
    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)
{
    binaryMaskOp(a, b, c, mask, bitwiseOp_<AndOp<uchar>, AndOp<int>, VAnd8u>);
}

void bitwise_or(const Mat& a, const Mat& b, Mat& c, const Mat& mask)
{
    binaryMaskOp(a, b, c, mask, bitwiseOp_<OrOp<uchar>, OrOp<int>, VOr8u>);
}

void bitwise_xor(const Mat& a, const Mat& b, Mat& c, const Mat& mask)
{
    binaryMaskOp(a, b, c, mask, bitwiseOp_<XorOp<uchar>, XorOp<int>, VXor8u>);
}

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 已提交
566 567 568 569 570 571 572 573 574 575 576 577
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            bitwise_not(it.planes[0], it.planes[1]);
        return;
    }
    
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
    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[] =
{
    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
};

static BinaryFunc subTab[] =
{
    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
};

void add( const Mat& src1, const Mat& src2, Mat& dst )
{
V
Vadim Pisarevsky 已提交
645
    int type = src1.type();
646
    BinaryFunc func = addTab[CV_MAT_DEPTH(type)];
V
Vadim Pisarevsky 已提交
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
    CV_Assert( type == src2.type() && func != 0 );
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2] );
        return;
    }
    
    Size size = src1.size();
    CV_Assert( size == src2.size() );
663 664 665 666 667 668
    dst.create( size, type );
    func(src1, src2, dst);
}

void subtract( const Mat& src1, const Mat& src2, Mat& dst )
{
V
Vadim Pisarevsky 已提交
669
    int type = src1.type();
670
    BinaryFunc func = subTab[CV_MAT_DEPTH(type)];
V
Vadim Pisarevsky 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    CV_Assert( type == src2.type() && func != 0 );
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2] );
        return;
    }
    
    Size size = src1.size();
    CV_Assert( size == src2.size() );
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 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 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
    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[] =
    {
        binarySOpCn_<OpAdd<uchar, int, uchar> >, 0,
        binarySOpCn_<OpAdd<ushort, int, ushort> >,
        binarySOpCn_<OpAdd<short, int, short> >,
        binarySOpCn_<OpAdd<int> >,
        binarySOpCn_<OpAdd<float> >,
        binarySOpCn_<OpAdd<double> >, 0
    };
    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[] =
    {
        binarySOpCn_<OpRSub<uchar, int, uchar> >, 0,
        binarySOpCn_<OpRSub<ushort, int, ushort> >,
        binarySOpCn_<OpRSub<short, int, short> >,
        binarySOpCn_<OpRSub<int> >,
        binarySOpCn_<OpRSub<float> >,
        binarySOpCn_<OpRSub<double> >, 0
    };
    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 )
            {
                T t0 = saturate_cast<T>(src1[i] * src2[i]);
                T t1 = saturate_cast<T>(src1[i+1] * src2[i+1]);
                dst[i] = t0; dst[i+1] = t1;

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

            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[] =
    {
        mul_<uchar, float>, 0, mul_<ushort, float>, mul_<short, float>,
        mul_<int, double>, mul_<float, float>, mul_<double, double>, 0
    };

    MulDivFunc func = tab[src1.depth()];
V
Vadim Pisarevsky 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
    CV_Assert( src1.type() == src2.type() && func != 0 );
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2], scale );
        return;
    }
    
    CV_Assert( src1.size() == src2.size() );
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 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
    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 );
V
Vadim Pisarevsky 已提交
886 887 888 889 890 891 892 893 894 895 896 897 898 899
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2], scale );
        return;
    }
    
    CV_Assert( src1.size() == src2.size() );
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    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[] =
    {
        recip_<uchar>, 0, recip_<ushort>, recip_<short>,
        recip_<int>, recip_<float>, recip_<double>, 0
    };

    RecipFunc func = tab[src.depth()];
    CV_Assert( func != 0 );
V
Vadim Pisarevsky 已提交
963 964 965 966 967 968 969 970 971 972 973 974 975
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( scale, it.planes[0], it.planes[1] );
        return;
    }
    
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 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 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 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 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
    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 )
{
    static AddWeightedFunc tab[]=
    {
        addWeighted8u, 0, addWeighted_<ushort, float>, addWeighted_<short, float>,
        addWeighted_<int, double>, addWeighted_<float, float>, addWeighted_<double, double>, 0
    };

    AddWeightedFunc func = tab[src1.depth()];
V
Vadim Pisarevsky 已提交
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
    CV_Assert( src1.type() == src2.type() && func != 0 );
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], alpha, it.planes[1], beta, gamma, it.planes[2] );
        return;
    }
    
    CV_Assert( src1.size() == src2.size() );
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
    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[] =
    {
        binaryOpC1_<OpAbsDiff<uchar>,VAbsDiff8u>, 0,
        binaryOpC1_<OpAbsDiff<ushort>,VAbsDiff16u>,
        binaryOpC1_<OpAbsDiff<short>,VAbsDiff16s>,
        binaryOpC1_<OpAbsDiff<int>,NoVec>,
        binaryOpC1_<OpAbsDiff<float>,VAbsDiff32f>,
        binaryOpC1_<OpAbsDiff<double>,NoVec>, 0
    };

V
Vadim Pisarevsky 已提交
1187
    binaryOp(src1, src2, dst, tab[src1.depth()]);
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
}


void absdiff( const Mat& src1, const Scalar& s, Mat& dst )
{
    static BinarySFuncCn tab[] =
    {
        binarySOpCn_<OpAbsDiffS<uchar, int> >, 0,
        binarySOpCn_<OpAbsDiffS<ushort, int> >,
        binarySOpCn_<OpAbsDiffS<short, int> >,
        binarySOpCn_<OpAbsDiffS<int> >,
        binarySOpCn_<OpAbsDiffS<float> >,
        binarySOpCn_<OpAbsDiffS<double> >, 0
    };

    BinarySFuncCn func = tab[src1.depth()];
    CV_Assert(src1.channels() <= 4 && func != 0);
V
Vadim Pisarevsky 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], s );
        return;
    }

    dst.create(src1.size(), src1.type());
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 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 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
    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();
    _a.convertTo((WT1*)&a, cn);
    _b.convertTo((WT1*)&b, cn);

    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[] =
    {
        inRange_<InRangeC1<uchar, uchar> >, 0,
        inRange_<InRangeC1<ushort, ushort> >,
        inRange_<InRangeC1<short, short> >,
        inRange_<InRangeC1<int, int> >,
        inRange_<InRangeC1<float, float> >,
        inRange_<InRangeC1<double, double> >, 0,

        inRange_<InRangeC2<uchar, uchar> >, 0,
        inRange_<InRangeC2<ushort, ushort> >,
        inRange_<InRangeC2<short, short> >,
        inRange_<InRangeC2<int, int> >,
        inRange_<InRangeC2<float, float> >,
        inRange_<InRangeC2<double, double> >, 0,

        inRange_<InRangeC3<uchar, uchar> >, 0,
        inRange_<InRangeC3<ushort, ushort> >,
        inRange_<InRangeC3<short, short> >,
        inRange_<InRangeC3<int, int> >,
        inRange_<InRangeC3<float, float> >,
        inRange_<InRangeC3<double, double> >, 0,

        inRange_<InRangeC4<uchar, uchar> >, 0,
        inRange_<InRangeC4<ushort, ushort> >,
        inRange_<InRangeC4<short, short> >,
        inRange_<InRangeC4<int, int> >,
        inRange_<InRangeC4<float, float> >,
        inRange_<InRangeC4<double, double> >, 0
    };

V
Vadim Pisarevsky 已提交
1347
    CV_Assert( src.type() == lowerb.type() && src.type() == upperb.type() && src.channels() <= 4 );
1348 1349 1350 1351

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

V
Vadim Pisarevsky 已提交
1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], it.planes[2], it.planes[3] );
        return;
    }
    
    CV_Assert( src.size() == lowerb.size() && src.size() == upperb.size() );
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
    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[] =
    {
        inRangeS_<InRangeC1<uchar, int> >, 0,
        inRangeS_<InRangeC1<ushort, int> >,
        inRangeS_<InRangeC1<short, int> >,
        inRangeS_<InRangeC1<int, int> >,
        inRangeS_<InRangeC1<float, float> >,
        inRangeS_<InRangeC1<double, double> >, 0,

        inRangeS_<InRangeC2<uchar, int> >, 0,
        inRangeS_<InRangeC2<ushort, int> >,
        inRangeS_<InRangeC2<short, int> >,
        inRangeS_<InRangeC2<int, int> >,
        inRangeS_<InRangeC2<float, float> >,
        inRangeS_<InRangeC2<double, double> >, 0,

        inRangeS_<InRangeC3<uchar, int> >, 0,
        inRangeS_<InRangeC3<ushort, int> >,
        inRangeS_<InRangeC3<short, int> >,
        inRangeS_<InRangeC3<int, int> >,
        inRangeS_<InRangeC3<float, float> >,
        inRangeS_<InRangeC3<double, double> >, 0,

        inRangeS_<InRangeC4<uchar, int> >, 0,
        inRangeS_<InRangeC4<ushort, int> >,
        inRangeS_<InRangeC4<short, int> >,
        inRangeS_<InRangeC4<int, int> >,
        inRangeS_<InRangeC4<float, float> >,
        inRangeS_<InRangeC4<double, double> >, 0
    };

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

    InRangeSFunc func = tab[src.type()];
    CV_Assert( func != 0 );
V
Vadim Pisarevsky 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], lowerb, upperb, it.planes[1] );
        return;
    }
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470

    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] =
    {
        {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},
    };

V
Vadim Pisarevsky 已提交
1471
    CV_Assert(src1.channels() == 1);
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

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

    switch( cmpOp )
    {
    case CMP_GT:
    case CMP_EQ:
        break;
    case CMP_GE:
        std::swap( psrc1, psrc2 );
        invflag = true;
        break;
    case CMP_LT:
        std::swap( psrc1, psrc2 );
        break;
    case CMP_LE:
        invflag = true;
        break;
    case CMP_NE:
        cmpOp = CMP_EQ;
        invflag = true;
        break;
    default:
        CV_Error(CV_StsBadArg, "Unknown comparison method");
    }

    BinaryFunc func = tab[cmpOp == CMP_EQ][depth];
V
Vadim Pisarevsky 已提交
1501
    binaryOp(*psrc1, *psrc2, dst, func, CV_8U);
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    if( invflag )
        bitwise_not(dst, dst);
}


void compare( const Mat& src1, double value, Mat& dst, int cmpOp )
{
    static BinarySFuncC1 tab[][8] =
    {
        {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},
    };

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

    switch( cmpOp )
    {
    case CMP_GT:
    case CMP_EQ:
    case CMP_GE:
        break;
    case CMP_LT:
        invflag = true;
        cmpOp = CMP_GE;
        break;
    case CMP_LE:
        invflag = true;
        cmpOp = CMP_GT;
        break;
    case CMP_NE:
        invflag = true;
        cmpOp = CMP_EQ;
        break;
    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 );
V
Vadim Pisarevsky 已提交
1560 1561 1562
    
    if( src1.dims > 2 )
    {
1563
        dst.create(src1.dims, src1.size, CV_8UC(src1.channels()));
V
Vadim Pisarevsky 已提交
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
        const Mat* arrays[] = {&src1, &dst, 0};
        Mat planes[2];
        NAryMatIterator it(arrays, planes);
        
        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;
    }
    
1577
    dst.create(src1.rows, src1.cols, CV_8UC(src1.channels()));
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
    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[] =
    {
        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
    };

V
Vadim Pisarevsky 已提交
1615
    binaryOp(src1, src2, dst, tab[src1.depth()]);
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
}

void max( const Mat& src1, const Mat& src2, Mat& dst )
{
    static BinaryFunc tab[] =
    {
        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
    };

V
Vadim Pisarevsky 已提交
1627
    binaryOp(src1, src2, dst, tab[src1.depth()]);
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
}

void min( const Mat& src1, double value, Mat& dst )
{
    static BinarySFuncC1 tab[] =
    {
        binarySOpC1_<MinOp<uchar> >, 0,
        binarySOpC1_<MinOp<ushort> >,
        binarySOpC1_<MinOp<short> >,
        binarySOpC1_<MinOp<int> >,
        binarySOpC1_<MinOp<float> >,
        binarySOpC1_<MinOp<double> >, 0
    };

    BinarySFuncC1 func = tab[src1.depth()];
    CV_Assert(func != 0);
V
Vadim Pisarevsky 已提交
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], value );
        return;
    }
    
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674
    dst.create(src1.size(), src1.type());
    return func( src1, dst, value );
}

void max( const Mat& src1, double value, Mat& dst )
{
    static BinarySFuncC1 tab[] =
    {
        binarySOpC1_<MaxOp<uchar> >, 0,
        binarySOpC1_<MaxOp<ushort> >,
        binarySOpC1_<MaxOp<short> >,
        binarySOpC1_<MaxOp<int> >,
        binarySOpC1_<MaxOp<float> >,
        binarySOpC1_<MaxOp<double> >, 0
    };

    BinarySFuncC1 func = tab[src1.depth()];
    CV_Assert(func != 0);
V
Vadim Pisarevsky 已提交
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
    
    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);
        
        for( int i = 0; i < it.nplanes; i++, ++it )
            func( it.planes[0], it.planes[1], value );
        return;
    }
    
1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701
    dst.create(src1.size(), src1.type());
    return func( src1, dst, value );
}

}

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

CV_IMPL void
cvNot( const CvArr* srcarr, CvArr* dstarr )
{
    cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
1702
    CV_Assert( src.size == dst.size && src.type() == dst.type() );
1703 1704 1705 1706 1707 1708 1709 1710 1711
    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;
1712
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::bitwise_and( src1, src2, dst, mask );
}

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;
1723
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734
    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;
1735
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1736 1737 1738 1739 1740 1741 1742 1743 1744 1745
    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;
1746
    CV_Assert( src.size == dst.size && src.type() == dst.type() );
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
    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;
1757
    CV_Assert( src.size == dst.size && src.type() == dst.type() );
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767
    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;
1768
    CV_Assert( src.size == dst.size && src.type() == dst.type() );
1769 1770 1771 1772 1773 1774 1775 1776 1777
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::bitwise_xor( src, s, dst, mask );
}

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;
1778
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1779 1780 1781 1782 1783 1784 1785 1786 1787
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::add( src1, src2, dst, mask );
}

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;
1788
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1789 1790 1791 1792 1793 1794 1795 1796 1797
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::subtract( src1, src2, dst, mask );
}

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;
1798
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1799 1800 1801 1802 1803 1804 1805 1806 1807
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::add( src1, value, dst, mask );
}

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;
1808
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
    if( maskarr )
        mask = cv::cvarrToMat(maskarr);
    cv::subtract( value, src1, dst, mask );
}

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);
1819
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1820 1821 1822 1823 1824 1825 1826 1827
    cv::multiply( src1, src2, dst, scale );
}

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;
1828
    CV_Assert( src2.size == dst.size && src2.type() == dst.type() );
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843

    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);
1844
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1845 1846 1847 1848 1849 1850 1851 1852
    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);
1853
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1854 1855 1856 1857 1858 1859 1860 1861 1862

    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);
1863
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1864 1865 1866 1867 1868 1869 1870 1871 1872

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

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);
1873
    CV_Assert( src1.size == dst.size && dst.type() == CV_8U );
1874 1875 1876 1877 1878 1879 1880 1881

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

CV_IMPL void
cvInRangeS( const void* srcarr1, CvScalar lowerb, CvScalar upperb, void* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
1882
    CV_Assert( src1.size == dst.size && dst.type() == CV_8U );
1883 1884 1885 1886 1887 1888 1889 1890 1891

    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);
1892
    CV_Assert( src1.size == dst.size && dst.type() == CV_8U );
1893 1894 1895 1896 1897 1898 1899 1900 1901

    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);
1902
    CV_Assert( src1.size == dst.size && dst.type() == CV_8U );
1903 1904 1905 1906 1907 1908 1909 1910 1911

    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);
1912
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1913 1914 1915 1916 1917 1918 1919 1920 1921

    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);
1922
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1923 1924 1925 1926 1927 1928 1929 1930

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

CV_IMPL void
cvMinS( const void* srcarr1, double value, void* dstarr )
{
    cv::Mat src1 = cv::cvarrToMat(srcarr1), dst = cv::cvarrToMat(dstarr);
1931
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1932 1933 1934 1935 1936 1937 1938 1939 1940

    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);
1941
    CV_Assert( src1.size == dst.size && src1.type() == dst.type() );
1942 1943 1944 1945 1946 1947

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


/* End of file. */