CLAHE Python bindings
[profile/ivi/opencv.git] / modules / imgproc / src / distransform.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 #include "precomp.hpp"
42
43 #define ICV_DIST_SHIFT  16
44 #define ICV_INIT_DIST0  (INT_MAX >> 2)
45
46 static CvStatus
47 icvInitTopBottom( int* temp, int tempstep, CvSize size, int border )
48 {
49     int i, j;
50     for( i = 0; i < border; i++ )
51     {
52         int* ttop = (int*)(temp + i*tempstep);
53         int* tbottom = (int*)(temp + (size.height + border*2 - i - 1)*tempstep);
54
55         for( j = 0; j < size.width + border*2; j++ )
56         {
57             ttop[j] = ICV_INIT_DIST0;
58             tbottom[j] = ICV_INIT_DIST0;
59         }
60     }
61
62     return CV_OK;
63 }
64
65
66 static CvStatus CV_STDCALL
67 icvDistanceTransform_3x3_C1R( const uchar* src, int srcstep, int* temp,
68         int step, float* dist, int dststep, CvSize size, const float* metrics )
69 {
70     const int BORDER = 1;
71     int i, j;
72     const int HV_DIST = CV_FLT_TO_FIX( metrics[0], ICV_DIST_SHIFT );
73     const int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], ICV_DIST_SHIFT );
74     const float scale = 1.f/(1 << ICV_DIST_SHIFT);
75
76     srcstep /= sizeof(src[0]);
77     step /= sizeof(temp[0]);
78     dststep /= sizeof(dist[0]);
79
80     icvInitTopBottom( temp, step, size, BORDER );
81
82     // forward pass
83     for( i = 0; i < size.height; i++ )
84     {
85         const uchar* s = src + i*srcstep;
86         int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
87
88         for( j = 0; j < BORDER; j++ )
89             tmp[-j-1] = tmp[size.width + j] = ICV_INIT_DIST0;
90
91         for( j = 0; j < size.width; j++ )
92         {
93             if( !s[j] )
94                 tmp[j] = 0;
95             else
96             {
97                 int t0 = tmp[j-step-1] + DIAG_DIST;
98                 int t = tmp[j-step] + HV_DIST;
99                 if( t0 > t ) t0 = t;
100                 t = tmp[j-step+1] + DIAG_DIST;
101                 if( t0 > t ) t0 = t;
102                 t = tmp[j-1] + HV_DIST;
103                 if( t0 > t ) t0 = t;
104                 tmp[j] = t0;
105             }
106         }
107     }
108
109     // backward pass
110     for( i = size.height - 1; i >= 0; i-- )
111     {
112         float* d = (float*)(dist + i*dststep);
113         int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
114
115         for( j = size.width - 1; j >= 0; j-- )
116         {
117             int t0 = tmp[j];
118             if( t0 > HV_DIST )
119             {
120                 int t = tmp[j+step+1] + DIAG_DIST;
121                 if( t0 > t ) t0 = t;
122                 t = tmp[j+step] + HV_DIST;
123                 if( t0 > t ) t0 = t;
124                 t = tmp[j+step-1] + DIAG_DIST;
125                 if( t0 > t ) t0 = t;
126                 t = tmp[j+1] + HV_DIST;
127                 if( t0 > t ) t0 = t;
128                 tmp[j] = t0;
129             }
130             d[j] = (float)(t0 * scale);
131         }
132     }
133
134     return CV_OK;
135 }
136
137
138 static CvStatus CV_STDCALL
139 icvDistanceTransform_5x5_C1R( const uchar* src, int srcstep, int* temp,
140         int step, float* dist, int dststep, CvSize size, const float* metrics )
141 {
142     const int BORDER = 2;
143     int i, j;
144     const int HV_DIST = CV_FLT_TO_FIX( metrics[0], ICV_DIST_SHIFT );
145     const int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], ICV_DIST_SHIFT );
146     const int LONG_DIST = CV_FLT_TO_FIX( metrics[2], ICV_DIST_SHIFT );
147     const float scale = 1.f/(1 << ICV_DIST_SHIFT);
148
149     srcstep /= sizeof(src[0]);
150     step /= sizeof(temp[0]);
151     dststep /= sizeof(dist[0]);
152
153     icvInitTopBottom( temp, step, size, BORDER );
154
155     // forward pass
156     for( i = 0; i < size.height; i++ )
157     {
158         const uchar* s = src + i*srcstep;
159         int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
160
161         for( j = 0; j < BORDER; j++ )
162             tmp[-j-1] = tmp[size.width + j] = ICV_INIT_DIST0;
163
164         for( j = 0; j < size.width; j++ )
165         {
166             if( !s[j] )
167                 tmp[j] = 0;
168             else
169             {
170                 int t0 = tmp[j-step*2-1] + LONG_DIST;
171                 int t = tmp[j-step*2+1] + LONG_DIST;
172                 if( t0 > t ) t0 = t;
173                 t = tmp[j-step-2] + LONG_DIST;
174                 if( t0 > t ) t0 = t;
175                 t = tmp[j-step-1] + DIAG_DIST;
176                 if( t0 > t ) t0 = t;
177                 t = tmp[j-step] + HV_DIST;
178                 if( t0 > t ) t0 = t;
179                 t = tmp[j-step+1] + DIAG_DIST;
180                 if( t0 > t ) t0 = t;
181                 t = tmp[j-step+2] + LONG_DIST;
182                 if( t0 > t ) t0 = t;
183                 t = tmp[j-1] + HV_DIST;
184                 if( t0 > t ) t0 = t;
185                 tmp[j] = t0;
186             }
187         }
188     }
189
190     // backward pass
191     for( i = size.height - 1; i >= 0; i-- )
192     {
193         float* d = (float*)(dist + i*dststep);
194         int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
195
196         for( j = size.width - 1; j >= 0; j-- )
197         {
198             int t0 = tmp[j];
199             if( t0 > HV_DIST )
200             {
201                 int t = tmp[j+step*2+1] + LONG_DIST;
202                 if( t0 > t ) t0 = t;
203                 t = tmp[j+step*2-1] + LONG_DIST;
204                 if( t0 > t ) t0 = t;
205                 t = tmp[j+step+2] + LONG_DIST;
206                 if( t0 > t ) t0 = t;
207                 t = tmp[j+step+1] + DIAG_DIST;
208                 if( t0 > t ) t0 = t;
209                 t = tmp[j+step] + HV_DIST;
210                 if( t0 > t ) t0 = t;
211                 t = tmp[j+step-1] + DIAG_DIST;
212                 if( t0 > t ) t0 = t;
213                 t = tmp[j+step-2] + LONG_DIST;
214                 if( t0 > t ) t0 = t;
215                 t = tmp[j+1] + HV_DIST;
216                 if( t0 > t ) t0 = t;
217                 tmp[j] = t0;
218             }
219             d[j] = (float)(t0 * scale);
220         }
221     }
222
223     return CV_OK;
224 }
225
226
227 static CvStatus CV_STDCALL
228 icvDistanceTransformEx_5x5_C1R( const uchar* src, int srcstep, int* temp,
229                 int step, float* dist, int dststep, int* labels, int lstep,
230                 CvSize size, const float* metrics )
231 {
232     const int BORDER = 2;
233
234     int i, j;
235     const int HV_DIST = CV_FLT_TO_FIX( metrics[0], ICV_DIST_SHIFT );
236     const int DIAG_DIST = CV_FLT_TO_FIX( metrics[1], ICV_DIST_SHIFT );
237     const int LONG_DIST = CV_FLT_TO_FIX( metrics[2], ICV_DIST_SHIFT );
238     const float scale = 1.f/(1 << ICV_DIST_SHIFT);
239
240     srcstep /= sizeof(src[0]);
241     step /= sizeof(temp[0]);
242     dststep /= sizeof(dist[0]);
243     lstep /= sizeof(labels[0]);
244
245     icvInitTopBottom( temp, step, size, BORDER );
246
247     // forward pass
248     for( i = 0; i < size.height; i++ )
249     {
250         const uchar* s = src + i*srcstep;
251         int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
252         int* lls = (int*)(labels + i*lstep);
253
254         for( j = 0; j < BORDER; j++ )
255             tmp[-j-1] = tmp[size.width + j] = ICV_INIT_DIST0;
256
257         for( j = 0; j < size.width; j++ )
258         {
259             if( !s[j] )
260             {
261                 tmp[j] = 0;
262                 //assert( lls[j] != 0 );
263             }
264             else
265             {
266                 int t0 = ICV_INIT_DIST0, t;
267                 int l0 = 0;
268
269                 t = tmp[j-step*2-1] + LONG_DIST;
270                 if( t0 > t )
271                 {
272                     t0 = t;
273                     l0 = lls[j-lstep*2-1];
274                 }
275                 t = tmp[j-step*2+1] + LONG_DIST;
276                 if( t0 > t )
277                 {
278                     t0 = t;
279                     l0 = lls[j-lstep*2+1];
280                 }
281                 t = tmp[j-step-2] + LONG_DIST;
282                 if( t0 > t )
283                 {
284                     t0 = t;
285                     l0 = lls[j-lstep-2];
286                 }
287                 t = tmp[j-step-1] + DIAG_DIST;
288                 if( t0 > t )
289                 {
290                     t0 = t;
291                     l0 = lls[j-lstep-1];
292                 }
293                 t = tmp[j-step] + HV_DIST;
294                 if( t0 > t )
295                 {
296                     t0 = t;
297                     l0 = lls[j-lstep];
298                 }
299                 t = tmp[j-step+1] + DIAG_DIST;
300                 if( t0 > t )
301                 {
302                     t0 = t;
303                     l0 = lls[j-lstep+1];
304                 }
305                 t = tmp[j-step+2] + LONG_DIST;
306                 if( t0 > t )
307                 {
308                     t0 = t;
309                     l0 = lls[j-lstep+2];
310                 }
311                 t = tmp[j-1] + HV_DIST;
312                 if( t0 > t )
313                 {
314                     t0 = t;
315                     l0 = lls[j-1];
316                 }
317
318                 tmp[j] = t0;
319                 lls[j] = l0;
320             }
321         }
322     }
323
324     // backward pass
325     for( i = size.height - 1; i >= 0; i-- )
326     {
327         float* d = (float*)(dist + i*dststep);
328         int* tmp = (int*)(temp + (i+BORDER)*step) + BORDER;
329         int* lls = (int*)(labels + i*lstep);
330
331         for( j = size.width - 1; j >= 0; j-- )
332         {
333             int t0 = tmp[j];
334             int l0 = lls[j];
335             if( t0 > HV_DIST )
336             {
337                 int t = tmp[j+step*2+1] + LONG_DIST;
338                 if( t0 > t )
339                 {
340                     t0 = t;
341                     l0 = lls[j+lstep*2+1];
342                 }
343                 t = tmp[j+step*2-1] + LONG_DIST;
344                 if( t0 > t )
345                 {
346                     t0 = t;
347                     l0 = lls[j+lstep*2-1];
348                 }
349                 t = tmp[j+step+2] + LONG_DIST;
350                 if( t0 > t )
351                 {
352                     t0 = t;
353                     l0 = lls[j+lstep+2];
354                 }
355                 t = tmp[j+step+1] + DIAG_DIST;
356                 if( t0 > t )
357                 {
358                     t0 = t;
359                     l0 = lls[j+lstep+1];
360                 }
361                 t = tmp[j+step] + HV_DIST;
362                 if( t0 > t )
363                 {
364                     t0 = t;
365                     l0 = lls[j+lstep];
366                 }
367                 t = tmp[j+step-1] + DIAG_DIST;
368                 if( t0 > t )
369                 {
370                     t0 = t;
371                     l0 = lls[j+lstep-1];
372                 }
373                 t = tmp[j+step-2] + LONG_DIST;
374                 if( t0 > t )
375                 {
376                     t0 = t;
377                     l0 = lls[j+lstep-2];
378                 }
379                 t = tmp[j+1] + HV_DIST;
380                 if( t0 > t )
381                 {
382                     t0 = t;
383                     l0 = lls[j+1];
384                 }
385                 tmp[j] = t0;
386                 lls[j] = l0;
387             }
388             d[j] = (float)(t0 * scale);
389         }
390     }
391
392     return CV_OK;
393 }
394
395
396 static CvStatus
397 icvGetDistanceTransformMask( int maskType, float *metrics )
398 {
399     if( !metrics )
400         return CV_NULLPTR_ERR;
401
402     switch (maskType)
403     {
404     case 30:
405         metrics[0] = 1.0f;
406         metrics[1] = 1.0f;
407         break;
408
409     case 31:
410         metrics[0] = 1.0f;
411         metrics[1] = 2.0f;
412         break;
413
414     case 32:
415         metrics[0] = 0.955f;
416         metrics[1] = 1.3693f;
417         break;
418
419     case 50:
420         metrics[0] = 1.0f;
421         metrics[1] = 1.0f;
422         metrics[2] = 2.0f;
423         break;
424
425     case 51:
426         metrics[0] = 1.0f;
427         metrics[1] = 2.0f;
428         metrics[2] = 3.0f;
429         break;
430
431     case 52:
432         metrics[0] = 1.0f;
433         metrics[1] = 1.4f;
434         metrics[2] = 2.1969f;
435         break;
436     default:
437         return CV_BADRANGE_ERR;
438     }
439
440     return CV_OK;
441 }
442
443 namespace cv
444 {
445
446 struct DTColumnInvoker : ParallelLoopBody
447 {
448     DTColumnInvoker( const CvMat* _src, CvMat* _dst, const int* _sat_tab, const float* _sqr_tab)
449     {
450         src = _src;
451         dst = _dst;
452         sat_tab = _sat_tab + src->rows*2 + 1;
453         sqr_tab = _sqr_tab;
454     }
455
456     void operator()( const Range& range ) const
457     {
458         int i, i1 = range.start, i2 = range.end;
459         int m = src->rows;
460         size_t sstep = src->step, dstep = dst->step/sizeof(float);
461         AutoBuffer<int> _d(m);
462         int* d = _d;
463
464         for( i = i1; i < i2; i++ )
465         {
466             const uchar* sptr = src->data.ptr + i + (m-1)*sstep;
467             float* dptr = dst->data.fl + i;
468             int j, dist = m-1;
469
470             for( j = m-1; j >= 0; j--, sptr -= sstep )
471             {
472                 dist = (dist + 1) & (sptr[0] == 0 ? 0 : -1);
473                 d[j] = dist;
474             }
475
476             dist = m-1;
477             for( j = 0; j < m; j++, dptr += dstep )
478             {
479                 dist = dist + 1 - sat_tab[dist - d[j]];
480                 d[j] = dist;
481                 dptr[0] = sqr_tab[dist];
482             }
483         }
484     }
485
486     const CvMat* src;
487     CvMat* dst;
488     const int* sat_tab;
489     const float* sqr_tab;
490 };
491
492
493 struct DTRowInvoker : ParallelLoopBody
494 {
495     DTRowInvoker( CvMat* _dst, const float* _sqr_tab, const float* _inv_tab )
496     {
497         dst = _dst;
498         sqr_tab = _sqr_tab;
499         inv_tab = _inv_tab;
500     }
501
502     void operator()( const Range& range ) const
503     {
504         const float inf = 1e15f;
505         int i, i1 = range.start, i2 = range.end;
506         int n = dst->cols;
507         AutoBuffer<uchar> _buf((n+2)*2*sizeof(float) + (n+2)*sizeof(int));
508         float* f = (float*)(uchar*)_buf;
509         float* z = f + n;
510         int* v = alignPtr((int*)(z + n + 1), sizeof(int));
511
512         for( i = i1; i < i2; i++ )
513         {
514             float* d = (float*)(dst->data.ptr + i*dst->step);
515             int p, q, k;
516
517             v[0] = 0;
518             z[0] = -inf;
519             z[1] = inf;
520             f[0] = d[0];
521
522             for( q = 1, k = 0; q < n; q++ )
523             {
524                 float fq = d[q];
525                 f[q] = fq;
526
527                 for(;;k--)
528                 {
529                     p = v[k];
530                     float s = (fq + sqr_tab[q] - d[p] - sqr_tab[p])*inv_tab[q - p];
531                     if( s > z[k] )
532                     {
533                         k++;
534                         v[k] = q;
535                         z[k] = s;
536                         z[k+1] = inf;
537                         break;
538                     }
539                 }
540             }
541
542             for( q = 0, k = 0; q < n; q++ )
543             {
544                 while( z[k+1] < q )
545                     k++;
546                 p = v[k];
547                 d[q] = std::sqrt(sqr_tab[std::abs(q - p)] + f[p]);
548             }
549         }
550     }
551
552     CvMat* dst;
553     const float* sqr_tab;
554     const float* inv_tab;
555 };
556
557 }
558
559 static void
560 icvTrueDistTrans( const CvMat* src, CvMat* dst )
561 {
562     const float inf = 1e15f;
563
564     if( !CV_ARE_SIZES_EQ( src, dst ))
565         CV_Error( CV_StsUnmatchedSizes, "" );
566
567     if( CV_MAT_TYPE(src->type) != CV_8UC1 ||
568         CV_MAT_TYPE(dst->type) != CV_32FC1 )
569         CV_Error( CV_StsUnsupportedFormat,
570         "The input image must have 8uC1 type and the output one must have 32fC1 type" );
571
572     int i, m = src->rows, n = src->cols;
573
574     cv::AutoBuffer<uchar> _buf(std::max(m*2*sizeof(float) + (m*3+1)*sizeof(int), n*2*sizeof(float)));
575     // stage 1: compute 1d distance transform of each column
576     float* sqr_tab = (float*)(uchar*)_buf;
577     int* sat_tab = cv::alignPtr((int*)(sqr_tab + m*2), sizeof(int));
578     int shift = m*2;
579
580     for( i = 0; i < m; i++ )
581         sqr_tab[i] = (float)(i*i);
582     for( i = m; i < m*2; i++ )
583         sqr_tab[i] = inf;
584     for( i = 0; i < shift; i++ )
585         sat_tab[i] = 0;
586     for( ; i <= m*3; i++ )
587         sat_tab[i] = i - shift;
588
589     cv::parallel_for_(cv::Range(0, n), cv::DTColumnInvoker(src, dst, sat_tab, sqr_tab));
590
591     // stage 2: compute modified distance transform for each row
592     float* inv_tab = sqr_tab + n;
593
594     inv_tab[0] = sqr_tab[0] = 0.f;
595     for( i = 1; i < n; i++ )
596     {
597         inv_tab[i] = (float)(0.5/i);
598         sqr_tab[i] = (float)(i*i);
599     }
600
601     cv::parallel_for_(cv::Range(0, m), cv::DTRowInvoker(dst, sqr_tab, inv_tab));
602 }
603
604
605 /*********************************** IPP functions *********************************/
606
607 typedef CvStatus (CV_STDCALL * CvIPPDistTransFunc)( const uchar* src, int srcstep,
608                                                     void* dst, int dststep,
609                                                     CvSize size, const void* metrics );
610
611 typedef CvStatus (CV_STDCALL * CvIPPDistTransFunc2)( uchar* src, int srcstep,
612                                                      CvSize size, const int* metrics );
613
614 /***********************************************************************************/
615
616 typedef CvStatus (CV_STDCALL * CvDistTransFunc)( const uchar* src, int srcstep,
617                                                  int* temp, int tempstep,
618                                                  float* dst, int dststep,
619                                                  CvSize size, const float* metrics );
620
621
622 /****************************************************************************************\
623  Non-inplace and Inplace 8u->8u Distance Transform for CityBlock (a.k.a. L1) metric
624  (C) 2006 by Jay Stavinzky.
625 \****************************************************************************************/
626
627 //BEGIN ATS ADDITION
628 /* 8-bit grayscale distance transform function */
629 static void
630 icvDistanceATS_L1_8u( const CvMat* src, CvMat* dst )
631 {
632     int width = src->cols, height = src->rows;
633
634     int a;
635     uchar lut[256];
636     int x, y;
637
638     const uchar *sbase = src->data.ptr;
639     uchar *dbase = dst->data.ptr;
640     int srcstep = src->step;
641     int dststep = dst->step;
642
643     CV_Assert( CV_IS_MASK_ARR( src ) && CV_MAT_TYPE( dst->type ) == CV_8UC1 );
644     CV_Assert( CV_ARE_SIZES_EQ( src, dst ));
645
646     ////////////////////// forward scan ////////////////////////
647     for( x = 0; x < 256; x++ )
648         lut[x] = CV_CAST_8U(x+1);
649
650     //init first pixel to max (we're going to be skipping it)
651     dbase[0] = (uchar)(sbase[0] == 0 ? 0 : 255);
652
653     //first row (scan west only, skip first pixel)
654     for( x = 1; x < width; x++ )
655         dbase[x] = (uchar)(sbase[x] == 0 ? 0 : lut[dbase[x-1]]);
656
657     for( y = 1; y < height; y++ )
658     {
659         sbase += srcstep;
660         dbase += dststep;
661
662         //for left edge, scan north only
663         a = sbase[0] == 0 ? 0 : lut[dbase[-dststep]];
664         dbase[0] = (uchar)a;
665
666         for( x = 1; x < width; x++ )
667         {
668             a = sbase[x] == 0 ? 0 : lut[MIN(a, dbase[x - dststep])];
669             dbase[x] = (uchar)a;
670         }
671     }
672
673     ////////////////////// backward scan ///////////////////////
674
675     a = dbase[width-1];
676
677     // do last row east pixel scan here (skip bottom right pixel)
678     for( x = width - 2; x >= 0; x-- )
679     {
680         a = lut[a];
681         dbase[x] = (uchar)(CV_CALC_MIN_8U(a, dbase[x]));
682     }
683
684     // right edge is the only error case
685     for( y = height - 2; y >= 0; y-- )
686     {
687         dbase -= dststep;
688
689         // do right edge
690         a = lut[dbase[width-1+dststep]];
691         dbase[width-1] = (uchar)(MIN(a, dbase[width-1]));
692
693         for( x = width - 2; x >= 0; x-- )
694         {
695             int b = dbase[x+dststep];
696             a = lut[MIN(a, b)];
697             dbase[x] = (uchar)(MIN(a, dbase[x]));
698         }
699     }
700 }
701 //END ATS ADDITION
702
703
704 /* Wrapper function for distance transform group */
705 CV_IMPL void
706 cvDistTransform( const void* srcarr, void* dstarr,
707                  int distType, int maskSize,
708                  const float *mask,
709                  void* labelsarr, int labelType )
710 {
711     float _mask[5] = {0};
712     CvMat srcstub, *src = (CvMat*)srcarr;
713     CvMat dststub, *dst = (CvMat*)dstarr;
714     CvMat lstub, *labels = (CvMat*)labelsarr;
715
716     src = cvGetMat( src, &srcstub );
717     dst = cvGetMat( dst, &dststub );
718
719     if( !CV_IS_MASK_ARR( src ) || (CV_MAT_TYPE( dst->type ) != CV_32FC1 &&
720         (CV_MAT_TYPE(dst->type) != CV_8UC1 || distType != CV_DIST_L1 || labels)) )
721         CV_Error( CV_StsUnsupportedFormat,
722         "source image must be 8uC1 and the distance map must be 32fC1 "
723         "(or 8uC1 in case of simple L1 distance transform)" );
724
725     if( !CV_ARE_SIZES_EQ( src, dst ))
726         CV_Error( CV_StsUnmatchedSizes, "the source and the destination images must be of the same size" );
727
728     if( maskSize != CV_DIST_MASK_3 && maskSize != CV_DIST_MASK_5 && maskSize != CV_DIST_MASK_PRECISE )
729         CV_Error( CV_StsBadSize, "Mask size should be 3 or 5 or 0 (presize)" );
730
731     if( distType == CV_DIST_C || distType == CV_DIST_L1 )
732         maskSize = !labels ? CV_DIST_MASK_3 : CV_DIST_MASK_5;
733     else if( distType == CV_DIST_L2 && labels )
734         maskSize = CV_DIST_MASK_5;
735
736     if( maskSize == CV_DIST_MASK_PRECISE )
737     {
738         icvTrueDistTrans( src, dst );
739         return;
740     }
741
742     if( labels )
743     {
744         labels = cvGetMat( labels, &lstub );
745         if( CV_MAT_TYPE( labels->type ) != CV_32SC1 )
746             CV_Error( CV_StsUnsupportedFormat, "the output array of labels must be 32sC1" );
747
748         if( !CV_ARE_SIZES_EQ( labels, dst ))
749             CV_Error( CV_StsUnmatchedSizes, "the array of labels has a different size" );
750
751         if( maskSize == CV_DIST_MASK_3 )
752             CV_Error( CV_StsNotImplemented,
753             "3x3 mask can not be used for \"labeled\" distance transform. Use 5x5 mask" );
754     }
755
756     if( distType == CV_DIST_C || distType == CV_DIST_L1 || distType == CV_DIST_L2 )
757     {
758         icvGetDistanceTransformMask( (distType == CV_DIST_C ? 0 :
759             distType == CV_DIST_L1 ? 1 : 2) + maskSize*10, _mask );
760     }
761     else if( distType == CV_DIST_USER )
762     {
763         if( !mask )
764             CV_Error( CV_StsNullPtr, "" );
765
766         memcpy( _mask, mask, (maskSize/2 + 1)*sizeof(float));
767     }
768
769     CvSize size = cvGetMatSize(src);
770
771     if( CV_MAT_TYPE(dst->type) == CV_8UC1 )
772     {
773         icvDistanceATS_L1_8u( src, dst );
774     }
775     else
776     {
777         int border = maskSize == CV_DIST_MASK_3 ? 1 : 2;
778         cv::Ptr<CvMat> temp = cvCreateMat( size.height + border*2, size.width + border*2, CV_32SC1 );
779
780         if( !labels )
781         {
782             CvDistTransFunc func = maskSize == CV_DIST_MASK_3 ?
783                 icvDistanceTransform_3x3_C1R :
784                 icvDistanceTransform_5x5_C1R;
785
786             func( src->data.ptr, src->step, temp->data.i, temp->step,
787                   dst->data.fl, dst->step, size, _mask );
788         }
789         else
790         {
791             cvZero( labels );
792
793             if( labelType == CV_DIST_LABEL_CCOMP )
794             {
795                 CvSeq *contours = 0;
796                 cv::Ptr<CvMemStorage> st = cvCreateMemStorage();
797                 cv::Ptr<CvMat> src_copy = cvCreateMat( size.height+border*2, size.width+border*2, src->type );
798                 cvCopyMakeBorder(src, src_copy, cvPoint(border, border), IPL_BORDER_CONSTANT, cvScalarAll(255));
799                 cvCmpS( src_copy, 0, src_copy, CV_CMP_EQ );
800                 cvFindContours( src_copy, st, &contours, sizeof(CvContour),
801                                CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(-border, -border));
802
803                 for( int label = 1; contours != 0; contours = contours->h_next, label++ )
804                 {
805                     CvScalar area_color = cvScalarAll(label);
806                     cvDrawContours( labels, contours, area_color, area_color, -255, -1, 8 );
807                 }
808             }
809             else
810             {
811                 int k = 1;
812                 for( int i = 0; i < src->rows; i++ )
813                 {
814                     const uchar* srcptr = src->data.ptr + src->step*i;
815                     int* labelptr = (int*)(labels->data.ptr + labels->step*i);
816
817                     for( int j = 0; j < src->cols; j++ )
818                         if( srcptr[j] == 0 )
819                             labelptr[j] = k++;
820                 }
821             }
822
823             icvDistanceTransformEx_5x5_C1R( src->data.ptr, src->step, temp->data.i, temp->step,
824                         dst->data.fl, dst->step, labels->data.i, labels->step, size, _mask );
825         }
826     }
827 }
828
829 void cv::distanceTransform( InputArray _src, OutputArray _dst, OutputArray _labels,
830                             int distanceType, int maskSize, int labelType )
831 {
832     Mat src = _src.getMat();
833     _dst.create(src.size(), CV_32F);
834     _labels.create(src.size(), CV_32S);
835     CvMat c_src = src, c_dst = _dst.getMat(), c_labels = _labels.getMat();
836     cvDistTransform(&c_src, &c_dst, distanceType, maskSize, 0, &c_labels, labelType);
837 }
838
839 void cv::distanceTransform( InputArray _src, OutputArray _dst,
840                             int distanceType, int maskSize )
841 {
842     Mat src = _src.getMat();
843     _dst.create(src.size(), CV_32F);
844     Mat dst = _dst.getMat();
845     CvMat c_src = src, c_dst = _dst.getMat();
846     cvDistTransform(&c_src, &c_dst, distanceType, maskSize, 0, 0, -1);
847 }
848
849 /* End of file. */