floodfill.cpp 22.5 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9
/*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.
//
//
wester committed
10
//                        Intel License Agreement
wester committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, 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.
//
wester committed
26
//   * The name of Intel Corporation may not be used to endorse or promote products
wester committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
//     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*/

#include "precomp.hpp"

#if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 8)
# pragma GCC diagnostic ignored "-Warray-bounds"
#endif

wester committed
48
typedef struct CvFFillSegment
wester committed
49 50 51 52 53 54 55
{
    ushort y;
    ushort l;
    ushort r;
    ushort prevl;
    ushort prevr;
    short dir;
wester committed
56 57
}
CvFFillSegment;
wester committed
58

wester committed
59 60
#define UP 1
#define DOWN -1
wester committed
61 62 63 64 65 66 67 68 69 70 71

#define ICV_PUSH( Y, L, R, PREV_L, PREV_R, DIR )  \
{                                                 \
    tail->y = (ushort)(Y);                        \
    tail->l = (ushort)(L);                        \
    tail->r = (ushort)(R);                        \
    tail->prevl = (ushort)(PREV_L);               \
    tail->prevr = (ushort)(PREV_R);               \
    tail->dir = (short)(DIR);                     \
    if( ++tail == buffer_end )                    \
    {                                             \
wester committed
72
        buffer->resize(buffer->size() * 2);       \
wester committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        tail = &buffer->front() + (tail - head);  \
        head = &buffer->front();                  \
        buffer_end = head + buffer->size();       \
    }                                             \
}

#define ICV_POP( Y, L, R, PREV_L, PREV_R, DIR )   \
{                                                 \
    --tail;                                       \
    Y = tail->y;                                  \
    L = tail->l;                                  \
    R = tail->r;                                  \
    PREV_L = tail->prevl;                         \
    PREV_R = tail->prevr;                         \
    DIR = tail->dir;                              \
}

wester committed
90 91 92
/****************************************************************************************\
*              Simple Floodfill (repainting single-color connected component)            *
\****************************************************************************************/
wester committed
93 94 95

template<typename _Tp>
static void
wester committed
96 97 98
icvFloodFill_CnIR( uchar* pImage, int step, CvSize roi, CvPoint seed,
                   _Tp newVal, CvConnectedComp* region, int flags,
                   std::vector<CvFFillSegment>* buffer )
wester committed
99
{
wester committed
100
    _Tp* img = (_Tp*)(pImage + step * seed.y);
wester committed
101 102 103 104
    int i, L, R;
    int area = 0;
    int XMin, XMax, YMin = seed.y, YMax = seed.y;
    int _8_connectivity = (flags & 255) == 8;
wester committed
105
    CvFFillSegment* buffer_end = &buffer->front() + buffer->size(), *head = &buffer->front(), *tail = &buffer->front();
wester committed
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

    L = R = XMin = XMax = seed.x;

    _Tp val0 = img[L];
    img[L] = newVal;

    while( ++R < roi.width && img[R] == val0 )
        img[R] = newVal;

    while( --L >= 0 && img[L] == val0 )
        img[L] = newVal;

    XMax = --R;
    XMin = ++L;

    ICV_PUSH( seed.y, L, R, R + 1, R, UP );

    while( head != tail )
    {
        int k, YC, PL, PR, dir;
        ICV_POP( YC, L, R, PL, PR, dir );

        int data[][3] =
        {
            {-dir, L - _8_connectivity, R + _8_connectivity},
            {dir, L - _8_connectivity, PL - 1},
            {dir, PR + 1, R + _8_connectivity}
        };

        if( region )
        {
            area += R - L + 1;

            if( XMax < R ) XMax = R;
            if( XMin > L ) XMin = L;
            if( YMax < YC ) YMax = YC;
            if( YMin > YC ) YMin = YC;
        }

        for( k = 0; k < 3; k++ )
        {
            dir = data[k][0];
wester committed
148 149 150
            img = (_Tp*)(pImage + (YC + dir) * step);
            int left = data[k][1];
            int right = data[k][2];
wester committed
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

            if( (unsigned)(YC + dir) >= (unsigned)roi.height )
                continue;

            for( i = left; i <= right; i++ )
            {
                if( (unsigned)i < (unsigned)roi.width && img[i] == val0 )
                {
                    int j = i;
                    img[i] = newVal;
                    while( --j >= 0 && img[j] == val0 )
                        img[j] = newVal;

                    while( ++i < roi.width && img[i] == val0 )
                        img[i] = newVal;

                    ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
                }
            }
        }
    }

    if( region )
    {
        region->area = area;
        region->rect.x = XMin;
        region->rect.y = YMin;
        region->rect.width = XMax - XMin + 1;
        region->rect.height = YMax - YMin + 1;
wester committed
180
        region->value = cv::Scalar(newVal);
wester committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    }
}

/****************************************************************************************\
*                                   Gradient Floodfill                                   *
\****************************************************************************************/

struct Diff8uC1
{
    Diff8uC1(uchar _lo, uchar _up) : lo(_lo), interval(_lo + _up) {}
    bool operator()(const uchar* a, const uchar* b) const
    { return (unsigned)(a[0] - b[0] + lo) <= interval; }
    unsigned lo, interval;
};

struct Diff8uC3
{
wester committed
198
    Diff8uC3(cv::Vec3b _lo, cv::Vec3b _up)
wester committed
199 200 201 202
    {
        for( int k = 0; k < 3; k++ )
            lo[k] = _lo[k], interval[k] = _lo[k] + _up[k];
    }
wester committed
203
    bool operator()(const cv::Vec3b* a, const cv::Vec3b* b) const
wester committed
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
    {
        return (unsigned)(a[0][0] - b[0][0] + lo[0]) <= interval[0] &&
               (unsigned)(a[0][1] - b[0][1] + lo[1]) <= interval[1] &&
               (unsigned)(a[0][2] - b[0][2] + lo[2]) <= interval[2];
    }
    unsigned lo[3], interval[3];
};

template<typename _Tp>
struct DiffC1
{
    DiffC1(_Tp _lo, _Tp _up) : lo(-_lo), up(_up) {}
    bool operator()(const _Tp* a, const _Tp* b) const
    {
        _Tp d = a[0] - b[0];
        return lo <= d && d <= up;
    }
    _Tp lo, up;
};

template<typename _Tp>
struct DiffC3
{
    DiffC3(_Tp _lo, _Tp _up) : lo(-_lo), up(_up) {}
    bool operator()(const _Tp* a, const _Tp* b) const
    {
        _Tp d = *a - *b;
        return lo[0] <= d[0] && d[0] <= up[0] &&
               lo[1] <= d[1] && d[1] <= up[1] &&
               lo[2] <= d[2] && d[2] <= up[2];
    }
    _Tp lo, up;
};

typedef DiffC1<int> Diff32sC1;
wester committed
239
typedef DiffC3<cv::Vec3i> Diff32sC3;
wester committed
240
typedef DiffC1<float> Diff32fC1;
wester committed
241 242 243 244 245 246 247 248 249
typedef DiffC3<cv::Vec3f> Diff32fC3;

static cv::Vec3i& operator += (cv::Vec3i& a, const cv::Vec3b& b)
{
    a[0] += b[0];
    a[1] += b[1];
    a[2] += b[2];
    return a;
}
wester committed
250

wester committed
251
template<typename _Tp, typename _WTp, class Diff>
wester committed
252
static void
wester committed
253 254 255 256
icvFloodFillGrad_CnIR( uchar* pImage, int step, uchar* pMask, int maskStep,
                       CvSize /*roi*/, CvPoint seed, _Tp newVal, Diff diff,
                       CvConnectedComp* region, int flags,
                       std::vector<CvFFillSegment>* buffer )
wester committed
257 258
{
    _Tp* img = (_Tp*)(pImage + step*seed.y);
wester committed
259
    uchar* mask = (pMask += maskStep + 1) + maskStep*seed.y;
wester committed
260 261
    int i, L, R;
    int area = 0;
wester committed
262
    _WTp sum = _WTp((typename cv::DataType<_Tp>::channel_type)0);
wester committed
263 264
    int XMin, XMax, YMin = seed.y, YMax = seed.y;
    int _8_connectivity = (flags & 255) == 8;
wester committed
265 266 267 268
    int fixedRange = flags & CV_FLOODFILL_FIXED_RANGE;
    int fillImage = (flags & CV_FLOODFILL_MASK_ONLY) == 0;
    uchar newMaskVal = (uchar)(flags & 0xff00 ? flags >> 8 : 1);
    CvFFillSegment* buffer_end = &buffer->front() + buffer->size(), *head = &buffer->front(), *tail = &buffer->front();
wester committed
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

    L = R = seed.x;
    if( mask[L] )
        return;

    mask[L] = newMaskVal;
    _Tp val0 = img[L];

    if( fixedRange )
    {
        while( !mask[R + 1] && diff( img + (R+1), &val0 ))
            mask[++R] = newMaskVal;

        while( !mask[L - 1] && diff( img + (L-1), &val0 ))
            mask[--L] = newMaskVal;
    }
    else
    {
        while( !mask[R + 1] && diff( img + (R+1), img + R ))
            mask[++R] = newMaskVal;

        while( !mask[L - 1] && diff( img + (L-1), img + L ))
            mask[--L] = newMaskVal;
    }

    XMax = R;
    XMin = L;

    ICV_PUSH( seed.y, L, R, R + 1, R, UP );

    while( head != tail )
    {
        int k, YC, PL, PR, dir;
        ICV_POP( YC, L, R, PL, PR, dir );

        int data[][3] =
        {
            {-dir, L - _8_connectivity, R + _8_connectivity},
            {dir, L - _8_connectivity, PL - 1},
            {dir, PR + 1, R + _8_connectivity}
        };

        unsigned length = (unsigned)(R-L);

        if( region )
        {
            area += (int)length + 1;

            if( XMax < R ) XMax = R;
            if( XMin > L ) XMin = L;
            if( YMax < YC ) YMax = YC;
            if( YMin > YC ) YMin = YC;
        }

        for( k = 0; k < 3; k++ )
        {
            dir = data[k][0];
            img = (_Tp*)(pImage + (YC + dir) * step);
            _Tp* img1 = (_Tp*)(pImage + YC * step);
wester committed
328
            mask = pMask + (YC + dir) * maskStep;
wester committed
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
            int left = data[k][1];
            int right = data[k][2];

            if( fixedRange )
                for( i = left; i <= right; i++ )
                {
                    if( !mask[i] && diff( img + i, &val0 ))
                    {
                        int j = i;
                        mask[i] = newMaskVal;
                        while( !mask[--j] && diff( img + j, &val0 ))
                            mask[j] = newMaskVal;

                        while( !mask[++i] && diff( img + i, &val0 ))
                            mask[i] = newMaskVal;

                        ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
                    }
                }
            else if( !_8_connectivity )
                for( i = left; i <= right; i++ )
                {
                    if( !mask[i] && diff( img + i, img1 + i ))
                    {
                        int j = i;
                        mask[i] = newMaskVal;
                        while( !mask[--j] && diff( img + j, img + (j+1) ))
                            mask[j] = newMaskVal;

                        while( !mask[++i] &&
wester committed
359
                               (diff( img + i, img + (i-1) ) ||
wester committed
360 361 362 363 364 365 366 367 368 369 370 371 372
                               (diff( img + i, img1 + i) && i <= R)))
                            mask[i] = newMaskVal;

                        ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
                    }
                }
            else
                for( i = left; i <= right; i++ )
                {
                    int idx;
                    _Tp val;

                    if( !mask[i] &&
wester committed
373 374 375
                        (((val = img[i],
                        (unsigned)(idx = i-L-1) <= length) &&
                        diff( &val, img1 + (i-1))) ||
wester committed
376
                        ((unsigned)(++idx) <= length &&
wester committed
377
                        diff( &val, img1 + i )) ||
wester committed
378
                        ((unsigned)(++idx) <= length &&
wester committed
379
                        diff( &val, img1 + (i+1) ))))
wester committed
380 381 382 383 384 385 386
                    {
                        int j = i;
                        mask[i] = newMaskVal;
                        while( !mask[--j] && diff( img + j, img + (j+1) ))
                            mask[j] = newMaskVal;

                        while( !mask[++i] &&
wester committed
387 388
                               ((val = img[i],
                               diff( &val, img + (i-1) )) ||
wester committed
389
                               (((unsigned)(idx = i-L-1) <= length &&
wester committed
390
                               diff( &val, img1 + (i-1) ))) ||
wester committed
391
                               ((unsigned)(++idx) <= length &&
wester committed
392
                               diff( &val, img1 + i )) ||
wester committed
393
                               ((unsigned)(++idx) <= length &&
wester committed
394
                               diff( &val, img1 + (i+1) ))))
wester committed
395 396 397 398 399 400 401 402 403 404 405
                            mask[i] = newMaskVal;

                        ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
                    }
                }
        }

        img = (_Tp*)(pImage + YC * step);
        if( fillImage )
            for( i = L; i <= R; i++ )
                img[i] = newVal;
wester committed
406 407 408
        else if( region )
            for( i = L; i <= R; i++ )
                sum += img[i];
wester committed
409 410 411 412 413 414 415 416 417
    }

    if( region )
    {
        region->area = area;
        region->rect.x = XMin;
        region->rect.y = YMin;
        region->rect.width = XMax - XMin + 1;
        region->rect.height = YMax - YMin + 1;
wester committed
418 419 420 421 422 423 424 425

        if( fillImage )
            region->value = cv::Scalar(newVal);
        else
        {
            double iarea = area ? 1./area : 0;
            region->value = cv::Scalar(sum*iarea);
        }
wester committed
426 427 428 429 430 431 432 433
    }
}


/****************************************************************************************\
*                                    External Functions                                  *
\****************************************************************************************/

wester committed
434 435 436 437 438 439 440 441 442 443 444 445 446
typedef  void (*CvFloodFillFunc)(
               void* img, int step, CvSize size, CvPoint seed, void* newval,
               CvConnectedComp* comp, int flags, void* buffer, int cn );

typedef  void (*CvFloodFillGradFunc)(
               void* img, int step, uchar* mask, int maskStep, CvSize size,
               CvPoint seed, void* newval, void* d_lw, void* d_up, void* ccomp,
               int flags, void* buffer, int cn );

CV_IMPL void
cvFloodFill( CvArr* arr, CvPoint seed_point,
             CvScalar newVal, CvScalar lo_diff, CvScalar up_diff,
             CvConnectedComp* comp, int flags, CvArr* maskarr )
wester committed
447
{
wester committed
448 449
    cv::Ptr<CvMat> tempMask;
    std::vector<CvFFillSegment> buffer;
wester committed
450

wester committed
451 452
    if( comp )
        memset( comp, 0, sizeof(*comp) );
wester committed
453

wester committed
454 455
    int i, type, depth, cn, is_simple;
    int buffer_size, connectivity = flags & 255;
wester committed
456 457 458 459 460 461 462 463
    union {
        uchar b[4];
        int i[4];
        float f[4];
        double _[4];
    } nv_buf;
    nv_buf._[0] = nv_buf._[1] = nv_buf._[2] = nv_buf._[3] = 0;

wester committed
464 465 466 467
    struct { cv::Vec3b b; cv::Vec3i i; cv::Vec3f f; } ld_buf, ud_buf;
    CvMat stub, *img = cvGetMat(arr, &stub);
    CvMat maskstub, *mask = (CvMat*)maskarr;
    CvSize size;
wester committed
468

wester committed
469 470 471
    type = CV_MAT_TYPE( img->type );
    depth = CV_MAT_DEPTH(type);
    cn = CV_MAT_CN(type);
wester committed
472 473 474 475

    if ( (cn != 1) && (cn != 3) )
    {
        CV_Error( CV_StsBadArg, "Number of channels in input image must be 1 or 3" );
wester committed
476
        return;
wester committed
477 478 479 480 481 482 483
    }

    if( connectivity == 0 )
        connectivity = 4;
    else if( connectivity != 4 && connectivity != 8 )
        CV_Error( CV_StsBadFlag, "Connectivity must be 4, 0(=4) or 8" );

wester committed
484
    is_simple = mask == 0 && (flags & CV_FLOODFILL_MASK_ONLY) == 0;
wester committed
485 486 487

    for( i = 0; i < cn; i++ )
    {
wester committed
488
        if( lo_diff.val[i] < 0 || up_diff.val[i] < 0 )
wester committed
489
            CV_Error( CV_StsBadArg, "lo_diff and up_diff must be non-negative" );
wester committed
490
        is_simple &= fabs(lo_diff.val[i]) < DBL_EPSILON && fabs(up_diff.val[i]) < DBL_EPSILON;
wester committed
491 492
    }

wester committed
493 494 495 496
    size = cvGetMatSize( img );

    if( (unsigned)seed_point.x >= (unsigned)size.width ||
        (unsigned)seed_point.y >= (unsigned)size.height )
wester committed
497 498
        CV_Error( CV_StsOutOfRange, "Seed point is outside of image" );

wester committed
499 500
    cvScalarToRawData( &newVal, &nv_buf, type, 0 );
    buffer_size = MAX( size.width, size.height ) * 2;
wester committed
501 502 503 504
    buffer.resize( buffer_size );

    if( is_simple )
    {
wester committed
505 506
        int elem_size = CV_ELEM_SIZE(type);
        const uchar* seed_ptr = img->data.ptr + img->step*seed_point.y + elem_size*seed_point.x;
wester committed
507

wester committed
508 509
        for(i = 0; i < elem_size; i++)
            if (seed_ptr[i] != nv_buf.b[i])
wester committed
510 511
                break;

wester committed
512
        if (i != elem_size)
wester committed
513 514
        {
            if( type == CV_8UC1 )
wester committed
515 516
                icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, nv_buf.b[0],
                                  comp, flags, &buffer);
wester committed
517
            else if( type == CV_8UC3 )
wester committed
518 519
                icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, cv::Vec3b(nv_buf.b),
                                  comp, flags, &buffer);
wester committed
520
            else if( type == CV_32SC1 )
wester committed
521 522
                icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, nv_buf.i[0],
                                  comp, flags, &buffer);
wester committed
523
            else if( type == CV_32FC1 )
wester committed
524 525
                icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, nv_buf.f[0],
                                  comp, flags, &buffer);
wester committed
526
            else if( type == CV_32SC3 )
wester committed
527 528
                icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, cv::Vec3i(nv_buf.i),
                                  comp, flags, &buffer);
wester committed
529
            else if( type == CV_32FC3 )
wester committed
530 531
                icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, cv::Vec3f(nv_buf.f),
                                  comp, flags, &buffer);
wester committed
532 533
            else
                CV_Error( CV_StsUnsupportedFormat, "" );
wester committed
534
            return;
wester committed
535 536 537
        }
    }

wester committed
538
    if( !mask )
wester committed
539
    {
wester committed
540 541
        /* created mask will be 8-byte aligned */
        tempMask = cvCreateMat( size.height + 2, (size.width + 9) & -8, CV_8UC1 );
wester committed
542 543 544 545
        mask = tempMask;
    }
    else
    {
wester committed
546 547 548 549 550 551 552
        mask = cvGetMat( mask, &maskstub );
        if( !CV_IS_MASK_ARR( mask ))
            CV_Error( CV_StsBadMask, "" );

        if( mask->width != size.width + 2 || mask->height != size.height + 2 )
            CV_Error( CV_StsUnmatchedSizes, "mask must be 2 pixel wider "
                                   "and 2 pixel taller than filled image" );
wester committed
553 554
    }

wester committed
555 556 557
    int width = tempMask ? mask->step : size.width + 2;
    uchar* mask_row = mask->data.ptr + mask->step;
    memset( mask_row - mask->step, 1, width );
wester committed
558

wester committed
559
    for( i = 1; i <= size.height; i++, mask_row += mask->step )
wester committed
560
    {
wester committed
561 562 563
        if( tempMask )
            memset( mask_row, 0, width );
        mask_row[0] = mask_row[size.width+1] = (uchar)1;
wester committed
564
    }
wester committed
565
    memset( mask_row, 1, width );
wester committed
566 567 568 569

    if( depth == CV_8U )
        for( i = 0; i < cn; i++ )
        {
wester committed
570 571 572 573
            int t = cvFloor(lo_diff.val[i]);
            ld_buf.b[i] = CV_CAST_8U(t);
            t = cvFloor(up_diff.val[i]);
            ud_buf.b[i] = CV_CAST_8U(t);
wester committed
574 575 576 577
        }
    else if( depth == CV_32S )
        for( i = 0; i < cn; i++ )
        {
wester committed
578 579 580 581
            int t = cvFloor(lo_diff.val[i]);
            ld_buf.i[i] = t;
            t = cvFloor(up_diff.val[i]);
            ud_buf.i[i] = t;
wester committed
582 583 584 585
        }
    else if( depth == CV_32F )
        for( i = 0; i < cn; i++ )
        {
wester committed
586 587
            ld_buf.f[i] = (float)lo_diff.val[i];
            ud_buf.f[i] = (float)up_diff.val[i];
wester committed
588 589 590 591 592
        }
    else
        CV_Error( CV_StsUnsupportedFormat, "" );

    if( type == CV_8UC1 )
wester committed
593 594 595 596 597
        icvFloodFillGrad_CnIR<uchar, int, Diff8uC1>(
                              img->data.ptr, img->step, mask->data.ptr, mask->step,
                              size, seed_point, nv_buf.b[0],
                              Diff8uC1(ld_buf.b[0], ud_buf.b[0]),
                              comp, flags, &buffer);
wester committed
598
    else if( type == CV_8UC3 )
wester committed
599 600 601 602 603
        icvFloodFillGrad_CnIR<cv::Vec3b, cv::Vec3i, Diff8uC3>(
                              img->data.ptr, img->step, mask->data.ptr, mask->step,
                              size, seed_point, cv::Vec3b(nv_buf.b),
                              Diff8uC3(ld_buf.b, ud_buf.b),
                              comp, flags, &buffer);
wester committed
604
    else if( type == CV_32SC1 )
wester committed
605 606 607 608 609
        icvFloodFillGrad_CnIR<int, int, Diff32sC1>(
                              img->data.ptr, img->step, mask->data.ptr, mask->step,
                              size, seed_point, nv_buf.i[0],
                              Diff32sC1(ld_buf.i[0], ud_buf.i[0]),
                              comp, flags, &buffer);
wester committed
610
    else if( type == CV_32SC3 )
wester committed
611 612 613 614 615
        icvFloodFillGrad_CnIR<cv::Vec3i, cv::Vec3i, Diff32sC3>(
                              img->data.ptr, img->step, mask->data.ptr, mask->step,
                              size, seed_point, cv::Vec3i(nv_buf.i),
                              Diff32sC3(ld_buf.i, ud_buf.i),
                              comp, flags, &buffer);
wester committed
616
    else if( type == CV_32FC1 )
wester committed
617 618 619 620 621
        icvFloodFillGrad_CnIR<float, float, Diff32fC1>(
                              img->data.ptr, img->step, mask->data.ptr, mask->step,
                              size, seed_point, nv_buf.f[0],
                              Diff32fC1(ld_buf.f[0], ud_buf.f[0]),
                              comp, flags, &buffer);
wester committed
622
    else if( type == CV_32FC3 )
wester committed
623 624 625 626 627
        icvFloodFillGrad_CnIR<cv::Vec3f, cv::Vec3f, Diff32fC3>(
                              img->data.ptr, img->step, mask->data.ptr, mask->step,
                              size, seed_point, cv::Vec3f(nv_buf.f),
                              Diff32fC3(ld_buf.f, ud_buf.f),
                              comp, flags, &buffer);
wester committed
628 629 630 631 632 633
    else
        CV_Error(CV_StsUnsupportedFormat, "");
}


int cv::floodFill( InputOutputArray _image, Point seedPoint,
wester committed
634 635
                   Scalar newVal, Rect* rect,
                   Scalar loDiff, Scalar upDiff, int flags )
wester committed
636
{
wester committed
637 638 639 640 641 642
    CvConnectedComp ccomp;
    CvMat c_image = _image.getMat();
    cvFloodFill(&c_image, seedPoint, newVal, loDiff, upDiff, &ccomp, flags, 0);
    if( rect )
        *rect = ccomp.rect;
    return cvRound(ccomp.area);
wester committed
643 644
}

wester committed
645 646 647
int cv::floodFill( InputOutputArray _image, InputOutputArray _mask,
                   Point seedPoint, Scalar newVal, Rect* rect,
                   Scalar loDiff, Scalar upDiff, int flags )
wester committed
648
{
wester committed
649 650 651 652 653 654
    CvConnectedComp ccomp;
    CvMat c_image = _image.getMat(), c_mask = _mask.getMat();
    cvFloodFill(&c_image, seedPoint, newVal, loDiff, upDiff, &ccomp, flags, c_mask.data.ptr ? &c_mask : 0);
    if( rect )
        *rect = ccomp.rect;
    return cvRound(ccomp.area);
wester committed
655 656 657
}

/* End of file. */