Add OpenCV source code
[platform/upstream/opencv.git] / modules / imgproc / src / floodfill.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
22 //   * Redistribution's in binary form must reproduce the above copyright notice,
23 //     this list of conditions and the following disclaimer in the documentation
24 //     and/or other materials provided with the distribution.
25 //
26 //   * The name of Intel Corporation may not be used to endorse or promote products
27 //     derived from this software without specific prior written permission.
28 //
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 #if defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 8)
45 # pragma GCC diagnostic ignored "-Warray-bounds"
46 #endif
47
48 typedef struct CvFFillSegment
49 {
50     ushort y;
51     ushort l;
52     ushort r;
53     ushort prevl;
54     ushort prevr;
55     short dir;
56 }
57 CvFFillSegment;
58
59 #define UP 1
60 #define DOWN -1
61
62 #define ICV_PUSH( Y, L, R, PREV_L, PREV_R, DIR )  \
63 {                                                 \
64     tail->y = (ushort)(Y);                        \
65     tail->l = (ushort)(L);                        \
66     tail->r = (ushort)(R);                        \
67     tail->prevl = (ushort)(PREV_L);               \
68     tail->prevr = (ushort)(PREV_R);               \
69     tail->dir = (short)(DIR);                     \
70     if( ++tail == buffer_end )                    \
71     {                                             \
72         buffer->resize(buffer->size() * 2);       \
73         tail = &buffer->front() + (tail - head);  \
74         head = &buffer->front();                  \
75         buffer_end = head + buffer->size();       \
76     }                                             \
77 }
78
79 #define ICV_POP( Y, L, R, PREV_L, PREV_R, DIR )   \
80 {                                                 \
81     --tail;                                       \
82     Y = tail->y;                                  \
83     L = tail->l;                                  \
84     R = tail->r;                                  \
85     PREV_L = tail->prevl;                         \
86     PREV_R = tail->prevr;                         \
87     DIR = tail->dir;                              \
88 }
89
90 /****************************************************************************************\
91 *              Simple Floodfill (repainting single-color connected component)            *
92 \****************************************************************************************/
93
94 template<typename _Tp>
95 static void
96 icvFloodFill_CnIR( uchar* pImage, int step, CvSize roi, CvPoint seed,
97                    _Tp newVal, CvConnectedComp* region, int flags,
98                    std::vector<CvFFillSegment>* buffer )
99 {
100     _Tp* img = (_Tp*)(pImage + step * seed.y);
101     int i, L, R;
102     int area = 0;
103     int XMin, XMax, YMin = seed.y, YMax = seed.y;
104     int _8_connectivity = (flags & 255) == 8;
105     CvFFillSegment* buffer_end = &buffer->front() + buffer->size(), *head = &buffer->front(), *tail = &buffer->front();
106
107     L = R = XMin = XMax = seed.x;
108
109     _Tp val0 = img[L];
110     img[L] = newVal;
111
112     while( ++R < roi.width && img[R] == val0 )
113         img[R] = newVal;
114
115     while( --L >= 0 && img[L] == val0 )
116         img[L] = newVal;
117
118     XMax = --R;
119     XMin = ++L;
120
121     ICV_PUSH( seed.y, L, R, R + 1, R, UP );
122
123     while( head != tail )
124     {
125         int k, YC, PL, PR, dir;
126         ICV_POP( YC, L, R, PL, PR, dir );
127
128         int data[][3] =
129         {
130             {-dir, L - _8_connectivity, R + _8_connectivity},
131             {dir, L - _8_connectivity, PL - 1},
132             {dir, PR + 1, R + _8_connectivity}
133         };
134
135         if( region )
136         {
137             area += R - L + 1;
138
139             if( XMax < R ) XMax = R;
140             if( XMin > L ) XMin = L;
141             if( YMax < YC ) YMax = YC;
142             if( YMin > YC ) YMin = YC;
143         }
144
145         for( k = 0; k < 3; k++ )
146         {
147             dir = data[k][0];
148             img = (_Tp*)(pImage + (YC + dir) * step);
149             int left = data[k][1];
150             int right = data[k][2];
151
152             if( (unsigned)(YC + dir) >= (unsigned)roi.height )
153                 continue;
154
155             for( i = left; i <= right; i++ )
156             {
157                 if( (unsigned)i < (unsigned)roi.width && img[i] == val0 )
158                 {
159                     int j = i;
160                     img[i] = newVal;
161                     while( --j >= 0 && img[j] == val0 )
162                         img[j] = newVal;
163
164                     while( ++i < roi.width && img[i] == val0 )
165                         img[i] = newVal;
166
167                     ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
168                 }
169             }
170         }
171     }
172
173     if( region )
174     {
175         region->area = area;
176         region->rect.x = XMin;
177         region->rect.y = YMin;
178         region->rect.width = XMax - XMin + 1;
179         region->rect.height = YMax - YMin + 1;
180         region->value = cv::Scalar(newVal);
181     }
182 }
183
184 /****************************************************************************************\
185 *                                   Gradient Floodfill                                   *
186 \****************************************************************************************/
187
188 struct Diff8uC1
189 {
190     Diff8uC1(uchar _lo, uchar _up) : lo(_lo), interval(_lo + _up) {}
191     bool operator()(const uchar* a, const uchar* b) const
192     { return (unsigned)(a[0] - b[0] + lo) <= interval; }
193     unsigned lo, interval;
194 };
195
196 struct Diff8uC3
197 {
198     Diff8uC3(cv::Vec3b _lo, cv::Vec3b _up)
199     {
200         for( int k = 0; k < 3; k++ )
201             lo[k] = _lo[k], interval[k] = _lo[k] + _up[k];
202     }
203     bool operator()(const cv::Vec3b* a, const cv::Vec3b* b) const
204     {
205         return (unsigned)(a[0][0] - b[0][0] + lo[0]) <= interval[0] &&
206                (unsigned)(a[0][1] - b[0][1] + lo[1]) <= interval[1] &&
207                (unsigned)(a[0][2] - b[0][2] + lo[2]) <= interval[2];
208     }
209     unsigned lo[3], interval[3];
210 };
211
212 template<typename _Tp>
213 struct DiffC1
214 {
215     DiffC1(_Tp _lo, _Tp _up) : lo(-_lo), up(_up) {}
216     bool operator()(const _Tp* a, const _Tp* b) const
217     {
218         _Tp d = a[0] - b[0];
219         return lo <= d && d <= up;
220     }
221     _Tp lo, up;
222 };
223
224 template<typename _Tp>
225 struct DiffC3
226 {
227     DiffC3(_Tp _lo, _Tp _up) : lo(-_lo), up(_up) {}
228     bool operator()(const _Tp* a, const _Tp* b) const
229     {
230         _Tp d = *a - *b;
231         return lo[0] <= d[0] && d[0] <= up[0] &&
232                lo[1] <= d[1] && d[1] <= up[1] &&
233                lo[2] <= d[2] && d[2] <= up[2];
234     }
235     _Tp lo, up;
236 };
237
238 typedef DiffC1<int> Diff32sC1;
239 typedef DiffC3<cv::Vec3i> Diff32sC3;
240 typedef DiffC1<float> Diff32fC1;
241 typedef DiffC3<cv::Vec3f> Diff32fC3;
242
243 static cv::Vec3i& operator += (cv::Vec3i& a, const cv::Vec3b& b)
244 {
245     a[0] += b[0];
246     a[1] += b[1];
247     a[2] += b[2];
248     return a;
249 }
250
251 template<typename _Tp, typename _WTp, class Diff>
252 static void
253 icvFloodFillGrad_CnIR( uchar* pImage, int step, uchar* pMask, int maskStep,
254                        CvSize /*roi*/, CvPoint seed, _Tp newVal, Diff diff,
255                        CvConnectedComp* region, int flags,
256                        std::vector<CvFFillSegment>* buffer )
257 {
258     _Tp* img = (_Tp*)(pImage + step*seed.y);
259     uchar* mask = (pMask += maskStep + 1) + maskStep*seed.y;
260     int i, L, R;
261     int area = 0;
262     _WTp sum = _WTp((typename cv::DataType<_Tp>::channel_type)0);
263     int XMin, XMax, YMin = seed.y, YMax = seed.y;
264     int _8_connectivity = (flags & 255) == 8;
265     int fixedRange = flags & CV_FLOODFILL_FIXED_RANGE;
266     int fillImage = (flags & CV_FLOODFILL_MASK_ONLY) == 0;
267     uchar newMaskVal = (uchar)(flags & 0xff00 ? flags >> 8 : 1);
268     CvFFillSegment* buffer_end = &buffer->front() + buffer->size(), *head = &buffer->front(), *tail = &buffer->front();
269
270     L = R = seed.x;
271     if( mask[L] )
272         return;
273
274     mask[L] = newMaskVal;
275     _Tp val0 = img[L];
276
277     if( fixedRange )
278     {
279         while( !mask[R + 1] && diff( img + (R+1), &val0 ))
280             mask[++R] = newMaskVal;
281
282         while( !mask[L - 1] && diff( img + (L-1), &val0 ))
283             mask[--L] = newMaskVal;
284     }
285     else
286     {
287         while( !mask[R + 1] && diff( img + (R+1), img + R ))
288             mask[++R] = newMaskVal;
289
290         while( !mask[L - 1] && diff( img + (L-1), img + L ))
291             mask[--L] = newMaskVal;
292     }
293
294     XMax = R;
295     XMin = L;
296
297     ICV_PUSH( seed.y, L, R, R + 1, R, UP );
298
299     while( head != tail )
300     {
301         int k, YC, PL, PR, dir;
302         ICV_POP( YC, L, R, PL, PR, dir );
303
304         int data[][3] =
305         {
306             {-dir, L - _8_connectivity, R + _8_connectivity},
307             {dir, L - _8_connectivity, PL - 1},
308             {dir, PR + 1, R + _8_connectivity}
309         };
310
311         unsigned length = (unsigned)(R-L);
312
313         if( region )
314         {
315             area += (int)length + 1;
316
317             if( XMax < R ) XMax = R;
318             if( XMin > L ) XMin = L;
319             if( YMax < YC ) YMax = YC;
320             if( YMin > YC ) YMin = YC;
321         }
322
323         for( k = 0; k < 3; k++ )
324         {
325             dir = data[k][0];
326             img = (_Tp*)(pImage + (YC + dir) * step);
327             _Tp* img1 = (_Tp*)(pImage + YC * step);
328             mask = pMask + (YC + dir) * maskStep;
329             int left = data[k][1];
330             int right = data[k][2];
331
332             if( fixedRange )
333                 for( i = left; i <= right; i++ )
334                 {
335                     if( !mask[i] && diff( img + i, &val0 ))
336                     {
337                         int j = i;
338                         mask[i] = newMaskVal;
339                         while( !mask[--j] && diff( img + j, &val0 ))
340                             mask[j] = newMaskVal;
341
342                         while( !mask[++i] && diff( img + i, &val0 ))
343                             mask[i] = newMaskVal;
344
345                         ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
346                     }
347                 }
348             else if( !_8_connectivity )
349                 for( i = left; i <= right; i++ )
350                 {
351                     if( !mask[i] && diff( img + i, img1 + i ))
352                     {
353                         int j = i;
354                         mask[i] = newMaskVal;
355                         while( !mask[--j] && diff( img + j, img + (j+1) ))
356                             mask[j] = newMaskVal;
357
358                         while( !mask[++i] &&
359                                (diff( img + i, img + (i-1) ) ||
360                                (diff( img + i, img1 + i) && i <= R)))
361                             mask[i] = newMaskVal;
362
363                         ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
364                     }
365                 }
366             else
367                 for( i = left; i <= right; i++ )
368                 {
369                     int idx;
370                     _Tp val;
371
372                     if( !mask[i] &&
373                         (((val = img[i],
374                         (unsigned)(idx = i-L-1) <= length) &&
375                         diff( &val, img1 + (i-1))) ||
376                         ((unsigned)(++idx) <= length &&
377                         diff( &val, img1 + i )) ||
378                         ((unsigned)(++idx) <= length &&
379                         diff( &val, img1 + (i+1) ))))
380                     {
381                         int j = i;
382                         mask[i] = newMaskVal;
383                         while( !mask[--j] && diff( img + j, img + (j+1) ))
384                             mask[j] = newMaskVal;
385
386                         while( !mask[++i] &&
387                                ((val = img[i],
388                                diff( &val, img + (i-1) )) ||
389                                (((unsigned)(idx = i-L-1) <= length &&
390                                diff( &val, img1 + (i-1) ))) ||
391                                ((unsigned)(++idx) <= length &&
392                                diff( &val, img1 + i )) ||
393                                ((unsigned)(++idx) <= length &&
394                                diff( &val, img1 + (i+1) ))))
395                             mask[i] = newMaskVal;
396
397                         ICV_PUSH( YC + dir, j+1, i-1, L, R, -dir );
398                     }
399                 }
400         }
401
402         img = (_Tp*)(pImage + YC * step);
403         if( fillImage )
404             for( i = L; i <= R; i++ )
405                 img[i] = newVal;
406         else if( region )
407             for( i = L; i <= R; i++ )
408                 sum += img[i];
409     }
410
411     if( region )
412     {
413         region->area = area;
414         region->rect.x = XMin;
415         region->rect.y = YMin;
416         region->rect.width = XMax - XMin + 1;
417         region->rect.height = YMax - YMin + 1;
418
419         if( fillImage )
420             region->value = cv::Scalar(newVal);
421         else
422         {
423             double iarea = area ? 1./area : 0;
424             region->value = cv::Scalar(sum*iarea);
425         }
426     }
427 }
428
429
430 /****************************************************************************************\
431 *                                    External Functions                                  *
432 \****************************************************************************************/
433
434 typedef  void (*CvFloodFillFunc)(
435                void* img, int step, CvSize size, CvPoint seed, void* newval,
436                CvConnectedComp* comp, int flags, void* buffer, int cn );
437
438 typedef  void (*CvFloodFillGradFunc)(
439                void* img, int step, uchar* mask, int maskStep, CvSize size,
440                CvPoint seed, void* newval, void* d_lw, void* d_up, void* ccomp,
441                int flags, void* buffer, int cn );
442
443 CV_IMPL void
444 cvFloodFill( CvArr* arr, CvPoint seed_point,
445              CvScalar newVal, CvScalar lo_diff, CvScalar up_diff,
446              CvConnectedComp* comp, int flags, CvArr* maskarr )
447 {
448     cv::Ptr<CvMat> tempMask;
449     std::vector<CvFFillSegment> buffer;
450
451     if( comp )
452         memset( comp, 0, sizeof(*comp) );
453
454     int i, type, depth, cn, is_simple;
455     int buffer_size, connectivity = flags & 255;
456     union {
457         uchar b[4];
458         int i[4];
459         float f[4];
460         double _[4];
461     } nv_buf;
462     nv_buf._[0] = nv_buf._[1] = nv_buf._[2] = nv_buf._[3] = 0;
463
464     struct { cv::Vec3b b; cv::Vec3i i; cv::Vec3f f; } ld_buf, ud_buf;
465     CvMat stub, *img = cvGetMat(arr, &stub);
466     CvMat maskstub, *mask = (CvMat*)maskarr;
467     CvSize size;
468
469     type = CV_MAT_TYPE( img->type );
470     depth = CV_MAT_DEPTH(type);
471     cn = CV_MAT_CN(type);
472
473     if( connectivity == 0 )
474         connectivity = 4;
475     else if( connectivity != 4 && connectivity != 8 )
476         CV_Error( CV_StsBadFlag, "Connectivity must be 4, 0(=4) or 8" );
477
478     is_simple = mask == 0 && (flags & CV_FLOODFILL_MASK_ONLY) == 0;
479
480     for( i = 0; i < cn; i++ )
481     {
482         if( lo_diff.val[i] < 0 || up_diff.val[i] < 0 )
483             CV_Error( CV_StsBadArg, "lo_diff and up_diff must be non-negative" );
484         is_simple &= fabs(lo_diff.val[i]) < DBL_EPSILON && fabs(up_diff.val[i]) < DBL_EPSILON;
485     }
486
487     size = cvGetMatSize( img );
488
489     if( (unsigned)seed_point.x >= (unsigned)size.width ||
490         (unsigned)seed_point.y >= (unsigned)size.height )
491         CV_Error( CV_StsOutOfRange, "Seed point is outside of image" );
492
493     cvScalarToRawData( &newVal, &nv_buf, type, 0 );
494     buffer_size = MAX( size.width, size.height ) * 2;
495     buffer.resize( buffer_size );
496
497     if( is_simple )
498     {
499         int elem_size = CV_ELEM_SIZE(type);
500         const uchar* seed_ptr = img->data.ptr + img->step*seed_point.y + elem_size*seed_point.x;
501
502         for(i = 0; i < elem_size; i++)
503             if (seed_ptr[i] != nv_buf.b[i])
504                 break;
505
506         if (i != elem_size)
507         {
508             if( type == CV_8UC1 )
509                 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, nv_buf.b[0],
510                                   comp, flags, &buffer);
511             else if( type == CV_8UC3 )
512                 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, cv::Vec3b(nv_buf.b),
513                                   comp, flags, &buffer);
514             else if( type == CV_32SC1 )
515                 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, nv_buf.i[0],
516                                   comp, flags, &buffer);
517             else if( type == CV_32FC1 )
518                 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, nv_buf.f[0],
519                                   comp, flags, &buffer);
520             else if( type == CV_32SC3 )
521                 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, cv::Vec3i(nv_buf.i),
522                                   comp, flags, &buffer);
523             else if( type == CV_32FC3 )
524                 icvFloodFill_CnIR(img->data.ptr, img->step, size, seed_point, cv::Vec3f(nv_buf.f),
525                                   comp, flags, &buffer);
526             else
527                 CV_Error( CV_StsUnsupportedFormat, "" );
528             return;
529         }
530     }
531
532     if( !mask )
533     {
534         /* created mask will be 8-byte aligned */
535         tempMask = cvCreateMat( size.height + 2, (size.width + 9) & -8, CV_8UC1 );
536         mask = tempMask;
537     }
538     else
539     {
540         mask = cvGetMat( mask, &maskstub );
541         if( !CV_IS_MASK_ARR( mask ))
542             CV_Error( CV_StsBadMask, "" );
543
544         if( mask->width != size.width + 2 || mask->height != size.height + 2 )
545             CV_Error( CV_StsUnmatchedSizes, "mask must be 2 pixel wider "
546                                    "and 2 pixel taller than filled image" );
547     }
548
549     int width = tempMask ? mask->step : size.width + 2;
550     uchar* mask_row = mask->data.ptr + mask->step;
551     memset( mask_row - mask->step, 1, width );
552
553     for( i = 1; i <= size.height; i++, mask_row += mask->step )
554     {
555         if( tempMask )
556             memset( mask_row, 0, width );
557         mask_row[0] = mask_row[size.width+1] = (uchar)1;
558     }
559     memset( mask_row, 1, width );
560
561     if( depth == CV_8U )
562         for( i = 0; i < cn; i++ )
563         {
564             int t = cvFloor(lo_diff.val[i]);
565             ld_buf.b[i] = CV_CAST_8U(t);
566             t = cvFloor(up_diff.val[i]);
567             ud_buf.b[i] = CV_CAST_8U(t);
568         }
569     else if( depth == CV_32S )
570         for( i = 0; i < cn; i++ )
571         {
572             int t = cvFloor(lo_diff.val[i]);
573             ld_buf.i[i] = t;
574             t = cvFloor(up_diff.val[i]);
575             ud_buf.i[i] = t;
576         }
577     else if( depth == CV_32F )
578         for( i = 0; i < cn; i++ )
579         {
580             ld_buf.f[i] = (float)lo_diff.val[i];
581             ud_buf.f[i] = (float)up_diff.val[i];
582         }
583     else
584         CV_Error( CV_StsUnsupportedFormat, "" );
585
586     if( type == CV_8UC1 )
587         icvFloodFillGrad_CnIR<uchar, int, Diff8uC1>(
588                               img->data.ptr, img->step, mask->data.ptr, mask->step,
589                               size, seed_point, nv_buf.b[0],
590                               Diff8uC1(ld_buf.b[0], ud_buf.b[0]),
591                               comp, flags, &buffer);
592     else if( type == CV_8UC3 )
593         icvFloodFillGrad_CnIR<cv::Vec3b, cv::Vec3i, Diff8uC3>(
594                               img->data.ptr, img->step, mask->data.ptr, mask->step,
595                               size, seed_point, cv::Vec3b(nv_buf.b),
596                               Diff8uC3(ld_buf.b, ud_buf.b),
597                               comp, flags, &buffer);
598     else if( type == CV_32SC1 )
599         icvFloodFillGrad_CnIR<int, int, Diff32sC1>(
600                               img->data.ptr, img->step, mask->data.ptr, mask->step,
601                               size, seed_point, nv_buf.i[0],
602                               Diff32sC1(ld_buf.i[0], ud_buf.i[0]),
603                               comp, flags, &buffer);
604     else if( type == CV_32SC3 )
605         icvFloodFillGrad_CnIR<cv::Vec3i, cv::Vec3i, Diff32sC3>(
606                               img->data.ptr, img->step, mask->data.ptr, mask->step,
607                               size, seed_point, cv::Vec3i(nv_buf.i),
608                               Diff32sC3(ld_buf.i, ud_buf.i),
609                               comp, flags, &buffer);
610     else if( type == CV_32FC1 )
611         icvFloodFillGrad_CnIR<float, float, Diff32fC1>(
612                               img->data.ptr, img->step, mask->data.ptr, mask->step,
613                               size, seed_point, nv_buf.f[0],
614                               Diff32fC1(ld_buf.f[0], ud_buf.f[0]),
615                               comp, flags, &buffer);
616     else if( type == CV_32FC3 )
617         icvFloodFillGrad_CnIR<cv::Vec3f, cv::Vec3f, Diff32fC3>(
618                               img->data.ptr, img->step, mask->data.ptr, mask->step,
619                               size, seed_point, cv::Vec3f(nv_buf.f),
620                               Diff32fC3(ld_buf.f, ud_buf.f),
621                               comp, flags, &buffer);
622     else
623         CV_Error(CV_StsUnsupportedFormat, "");
624 }
625
626
627 int cv::floodFill( InputOutputArray _image, Point seedPoint,
628                    Scalar newVal, Rect* rect,
629                    Scalar loDiff, Scalar upDiff, int flags )
630 {
631     CvConnectedComp ccomp;
632     CvMat c_image = _image.getMat();
633     cvFloodFill(&c_image, seedPoint, newVal, loDiff, upDiff, &ccomp, flags, 0);
634     if( rect )
635         *rect = ccomp.rect;
636     return cvRound(ccomp.area);
637 }
638
639 int cv::floodFill( InputOutputArray _image, InputOutputArray _mask,
640                    Point seedPoint, Scalar newVal, Rect* rect,
641                    Scalar loDiff, Scalar upDiff, int flags )
642 {
643     CvConnectedComp ccomp;
644     CvMat c_image = _image.getMat(), c_mask = _mask.getMat();
645     cvFloodFill(&c_image, seedPoint, newVal, loDiff, upDiff, &ccomp, flags, c_mask.data.ptr ? &c_mask : 0);
646     if( rect )
647         *rect = ccomp.rect;
648     return cvRound(ccomp.area);
649 }
650
651 /* End of file. */