arLabeling.c
Go to the documentation of this file.
00001 /*******************************************************
00002  *
00003  * Author: Hirokazu Kato
00004  *
00005  *         kato@sys.im.hiroshima-cu.ac.jp
00006  *
00007  * Revision: 3.1
00008  * Date: 01/12/07
00009  *
00010  *
00011  * modified by Thomas Pintaric [tp], Vienna University of Technology
00012  **********************************************************************/
00013 
00014 #include <stdlib.h>
00015 #include <stdio.h>
00016 #include <AR/ar.h>
00017 
00018 #ifdef _WIN32
00019 #  include <windows.h>
00020 #  define put_zero(p,s) ZeroMemory(p, s)
00021 #else
00022 #  include <string.h>
00023 #  define put_zero(p,s) memset((void *)p, 0, s)
00024 #endif
00025 
00026 #define USE_OPTIMIZATIONS
00027 #define WORK_SIZE   1024*32
00028 
00029 /*****************************************************************************/
00030 // BUG in ARToolkit 2.65
00031 // Hardcoded buffer (600*500) is too small for full-size DV-PAL/NTSC resolutions of
00032 // 720x576 and 720x480, respectively. Results in segment faults.
00033 /*
00034 static ARInt16      l_imageL[640*500];
00035 static ARInt16      l_imageR[640*500];
00036 */
00037 
00038 #define HARDCODED_BUFFER_WIDTH  1024
00039 #define HARDCODED_BUFFER_HEIGHT 1024
00040 
00041 static ARInt16      l_imageL[HARDCODED_BUFFER_WIDTH*HARDCODED_BUFFER_HEIGHT];
00042 static ARInt16      l_imageR[HARDCODED_BUFFER_WIDTH*HARDCODED_BUFFER_HEIGHT];
00043 /*****************************************************************************/
00044 
00045 
00046 static int          workL[WORK_SIZE];
00047 static int          workR[WORK_SIZE];
00048 static int          work2L[WORK_SIZE*7];
00049 static int          work2R[WORK_SIZE*7];
00050 
00051 static int          wlabel_numL;
00052 static int          wlabel_numR;
00053 static int          wareaL[WORK_SIZE];
00054 static int          wareaR[WORK_SIZE];
00055 static int          wclipL[WORK_SIZE*4];
00056 static int          wclipR[WORK_SIZE*4];
00057 static double       wposL[WORK_SIZE*2];
00058 static double       wposR[WORK_SIZE*2];
00059 
00060 static ARInt16 *labeling2( ARUint8 *image, int thresh,
00061                            int *label_num, int **area, double **pos, int **clip,
00062                            int **label_ref, int LorR );
00063 static ARInt16 *labeling3( ARUint8 *image, int thresh,
00064                            int *label_num, int **area, double **pos, int **clip,
00065                            int **label_ref, int LorR );
00066 
00067 void arGetImgFeature( int *num, int **area, int **clip, double **pos )
00068 {
00069     *num  = wlabel_numL;
00070     *area = wareaL;
00071     *clip = wclipL;
00072     *pos  = wposL;
00073 
00074     return;
00075 }
00076 
00077 ARInt16 *arLabeling( ARUint8 *image, int thresh,
00078                      int *label_num, int **area, double **pos, int **clip,
00079                      int **label_ref )
00080 {
00081     if( arDebug ) {
00082         return( labeling3(image, thresh, label_num,
00083                           area, pos, clip, label_ref, 1) );
00084     } else {
00085         return( labeling2(image, thresh, label_num,
00086                           area, pos, clip, label_ref, 1) );
00087     }
00088 }
00089 
00090 void arsGetImgFeature( int *num, int **area, int **clip, double **pos, int LorR )
00091 {
00092     if (LorR) {
00093         *num  = wlabel_numL;
00094         *area = wareaL;
00095         *clip = wclipL;
00096         *pos  = wposL;
00097     } else {
00098         *num  = wlabel_numR;
00099         *area = wareaR;
00100         *clip = wclipR;
00101         *pos  = wposR;
00102     }
00103 
00104     return;
00105 }
00106 
00107 ARInt16 *arsLabeling( ARUint8 *image, int thresh,
00108                       int *label_num, int **area, double **pos, int **clip,
00109                       int **label_ref, int LorR )
00110 {
00111     if( arDebug ) {
00112         return( labeling3(image, thresh, label_num,
00113                           area, pos, clip, label_ref, LorR) );
00114     } else {
00115         return( labeling2(image, thresh, label_num,
00116                           area, pos, clip, label_ref, LorR) );
00117     }
00118 }
00119 
00120 static ARInt16 *labeling2( ARUint8 *image, int thresh,
00121                            int *label_num, int **area, double **pos, int **clip,
00122                            int **label_ref, int LorR )
00123 {
00124     ARUint8   *pnt;                     /*  image pointer       */
00125     ARInt16   *pnt1, *pnt2;             /*  image pointer       */
00126     int       *wk;                      /*  pointer for work    */
00127     int       wk_max;                   /*  work                */
00128     int       m,n;                      /*  work                */
00129     int       i,j,k;                    /*  for loop            */
00130     int       lxsize, lysize;
00131     int       poff;
00132     ARInt16   *l_image;
00133     int       *work, *work2;
00134     int       *wlabel_num;
00135     int       *warea;
00136     int       *wclip;
00137     double    *wpos;
00138 #ifdef USE_OPTIMIZATIONS
00139         int               pnt2_index;   // [tp]
00140 #endif
00141         int               thresht3 = thresh * 3;
00142 
00143         if (LorR) {
00144         l_image = &l_imageL[0];
00145         work    = &workL[0];
00146         work2   = &work2L[0];
00147         wlabel_num = &wlabel_numL;
00148         warea   = &wareaL[0];
00149         wclip   = &wclipL[0];
00150         wpos    = &wposL[0];
00151     } else {
00152         l_image = &l_imageR[0];
00153         work    = &workR[0];
00154         work2   = &work2R[0];
00155         wlabel_num = &wlabel_numR;
00156         warea   = &wareaR[0];
00157         wclip   = &wclipR[0];
00158         wpos    = &wposR[0];
00159     }
00160 
00161     if (arImageProcMode == AR_IMAGE_PROC_IN_HALF) {
00162         lxsize = arImXsize / 2;
00163         lysize = arImYsize / 2;
00164     } else {
00165         lxsize = arImXsize;
00166         lysize = arImYsize;
00167     }
00168 
00169     pnt1 = &l_image[0]; // Leftmost pixel of top row of image.
00170     pnt2 = &l_image[(lysize - 1)*lxsize]; // Leftmost pixel of bottom row of image.
00171 
00172 #ifndef USE_OPTIMIZATIONS
00173         for(i = 0; i < lxsize; i++) {
00174         *(pnt1++) = *(pnt2++) = 0;
00175     }
00176 #else
00177 // 4x loop unrolling
00178         for (i = 0; i < lxsize - (lxsize%4); i += 4) {
00179         *(pnt1++) = *(pnt2++) = 0;
00180         *(pnt1++) = *(pnt2++) = 0;
00181         *(pnt1++) = *(pnt2++) = 0;
00182         *(pnt1++) = *(pnt2++) = 0;
00183     }
00184 #endif
00185     pnt1 = &l_image[0]; // Leftmost pixel of top row of image.
00186     pnt2 = &l_image[lxsize - 1]; // Rightmost pixel of top row of image.
00187 
00188 #ifndef USE_OPTIMIZATIONS
00189     for(i = 0; i < lysize; i++) {
00190         *pnt1 = *pnt2 = 0;
00191         pnt1 += lxsize;
00192         pnt2 += lxsize;
00193     }
00194 #else
00195 // 4x loop unrolling
00196     for (i = 0; i < lysize - (lysize%4); i += 4) {
00197                 *pnt1 = *pnt2 = 0;
00198         pnt1 += lxsize;
00199         pnt2 += lxsize;
00200 
00201                 *pnt1 = *pnt2 = 0;
00202         pnt1 += lxsize;
00203         pnt2 += lxsize;
00204 
00205                 *pnt1 = *pnt2 = 0;
00206         pnt1 += lxsize;
00207         pnt2 += lxsize;
00208 
00209                 *pnt1 = *pnt2 = 0;
00210         pnt1 += lxsize;
00211         pnt2 += lxsize;
00212     }
00213 #endif
00214 
00215     wk_max = 0;
00216     pnt2 = &(l_image[lxsize+1]);
00217     if (arImageProcMode == AR_IMAGE_PROC_IN_HALF) {
00218         pnt = &(image[(arImXsize*2+2)*AR_PIX_SIZE_DEFAULT]);
00219         poff = AR_PIX_SIZE_DEFAULT*2;
00220     } else {
00221         pnt = &(image[(arImXsize+1)*AR_PIX_SIZE_DEFAULT]);
00222         poff = AR_PIX_SIZE_DEFAULT;
00223     }
00224     for (j = 1; j < lysize - 1; j++, pnt += poff*2, pnt2 += 2) {
00225         for(i = 1; i < lxsize-1; i++, pnt+=poff, pnt2++) {
00226 #if (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_ARGB)
00227             if( *(pnt+1) + *(pnt+2) + *(pnt+3) <= thresht3 )
00228 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_ABGR)
00229             if( *(pnt+1) + *(pnt+2) + *(pnt+3) <= thresht3 )
00230 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGRA)
00231             if( *(pnt+0) + *(pnt+1) + *(pnt+2) <= thresht3 )
00232 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGR)
00233             if( *(pnt+0) + *(pnt+1) + *(pnt+2) <= thresht3 )
00234 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGBA)
00235             if( *(pnt+0) + *(pnt+1) + *(pnt+2) <= thresht3 )
00236 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGB)
00237             if( *(pnt+0) + *(pnt+1) + *(pnt+2) <= thresht3 )
00238 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_MONO)
00239                         if( *(pnt) <= thresh )
00240 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_2vuy)
00241                         if( *(pnt+1) <= thresh )
00242 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_yuvs)
00243                         if( *(pnt+0) <= thresh )
00244 #else
00245 #  error Unknown default pixel format defined in config.h
00246 #endif
00247                         {
00248                 pnt1 = &(pnt2[-lxsize]);
00249                 if( *pnt1 > 0 ) {
00250                     *pnt2 = *pnt1;
00251 
00252 #ifndef USE_OPTIMIZATIONS
00253                                         // ORIGINAL CODE
00254                                         work2[((*pnt2)-1)*7+0] ++;
00255                     work2[((*pnt2)-1)*7+1] += i;
00256                     work2[((*pnt2)-1)*7+2] += j;
00257                     work2[((*pnt2)-1)*7+6] = j;
00258 #else
00259                                         // OPTIMIZED CODE [tp]
00260                                         // ((*pnt2)-1)*7 should be treated as constant, since
00261                                         //  work2[n] (n=0..xsize*ysize) cannot overwrite (*pnt2)
00262                                         pnt2_index = ((*pnt2)-1) * 7;
00263                     work2[pnt2_index+0]++;
00264                     work2[pnt2_index+1]+= i;
00265                     work2[pnt2_index+2]+= j;
00266                     work2[pnt2_index+6] = j;
00267                                         // --------------------------------
00268 #endif
00269                 }
00270                 else if( *(pnt1+1) > 0 ) {
00271                     if( *(pnt1-1) > 0 ) {
00272                         m = work[*(pnt1+1)-1];
00273                         n = work[*(pnt1-1)-1];
00274                         if( m > n ) {
00275                             *pnt2 = n;
00276                             wk = &(work[0]);
00277                             for(k = 0; k < wk_max; k++) {
00278                                 if( *wk == m ) *wk = n;
00279                                 wk++;
00280                             }
00281                         }
00282                         else if( m < n ) {
00283                             *pnt2 = m;
00284                             wk = &(work[0]);
00285                             for(k = 0; k < wk_max; k++) {
00286                                 if( *wk == n ) *wk = m;
00287                                 wk++;
00288                             }
00289                         }
00290                         else *pnt2 = m;
00291 
00292 #ifndef USE_OPTIMIZATIONS
00293                                                 // ORIGINAL CODE
00294                                                 work2[((*pnt2)-1)*7+0] ++;
00295                         work2[((*pnt2)-1)*7+1] += i;
00296                         work2[((*pnt2)-1)*7+2] += j;
00297                         work2[((*pnt2)-1)*7+6] = j;
00298 #else
00299                                                 // PERFORMANCE OPTIMIZATION:
00300                                                 pnt2_index = ((*pnt2)-1) * 7;
00301                                                 work2[pnt2_index+0]++;
00302                                                 work2[pnt2_index+1]+= i;
00303                                                 work2[pnt2_index+2]+= j;
00304                                                 work2[pnt2_index+6] = j;
00305 #endif
00306 
00307                     }
00308                     else if( *(pnt2-1) > 0 ) {
00309                         m = work[*(pnt1+1)-1];
00310                         n = work[*(pnt2-1)-1];
00311                         if( m > n ) {
00312                             *pnt2 = n;
00313                             wk = &(work[0]);
00314                             for(k = 0; k < wk_max; k++) {
00315                                 if( *wk == m ) *wk = n;
00316                                 wk++;
00317                             }
00318                         }
00319                         else if( m < n ) {
00320                             *pnt2 = m;
00321                             wk = &(work[0]);
00322                             for(k = 0; k < wk_max; k++) {
00323                                 if( *wk == n ) *wk = m;
00324                                 wk++;
00325                             }
00326                         }
00327                         else *pnt2 = m;
00328 
00329 #ifndef USE_OPTIMIZATIONS
00330                                                 // ORIGINAL CODE
00331                         work2[((*pnt2)-1)*7+0] ++;
00332                         work2[((*pnt2)-1)*7+1] += i;
00333                         work2[((*pnt2)-1)*7+2] += j;
00334 #else
00335                                                 // PERFORMANCE OPTIMIZATION:
00336                                                 pnt2_index = ((*pnt2)-1) * 7;
00337                                                 work2[pnt2_index+0]++;
00338                                                 work2[pnt2_index+1]+= i;
00339                                                 work2[pnt2_index+2]+= j;
00340 #endif
00341 
00342                     }
00343                     else {
00344                         *pnt2 = *(pnt1+1);
00345 
00346 #ifndef USE_OPTIMIZATIONS
00347                                                 // ORIGINAL CODE
00348                         work2[((*pnt2)-1)*7+0] ++;
00349                         work2[((*pnt2)-1)*7+1] += i;
00350                         work2[((*pnt2)-1)*7+2] += j;
00351                         if( work2[((*pnt2)-1)*7+3] > i ) work2[((*pnt2)-1)*7+3] = i;
00352                         work2[((*pnt2)-1)*7+6] = j;
00353 #else
00354                                                 // PERFORMANCE OPTIMIZATION:
00355                                                 pnt2_index = ((*pnt2)-1) * 7;
00356                                                 work2[pnt2_index+0]++;
00357                                                 work2[pnt2_index+1]+= i;
00358                                                 work2[pnt2_index+2]+= j;
00359                         if( work2[pnt2_index+3] > i ) work2[pnt2_index+3] = i;
00360                                                 work2[pnt2_index+6] = j;
00361 #endif
00362                     }
00363                 }
00364                 else if( *(pnt1-1) > 0 ) {
00365                     *pnt2 = *(pnt1-1);
00366 
00367 #ifndef USE_OPTIMIZATIONS
00368                                                 // ORIGINAL CODE
00369                     work2[((*pnt2)-1)*7+0] ++;
00370                     work2[((*pnt2)-1)*7+1] += i;
00371                     work2[((*pnt2)-1)*7+2] += j;
00372                     if( work2[((*pnt2)-1)*7+4] < i ) work2[((*pnt2)-1)*7+4] = i;
00373                     work2[((*pnt2)-1)*7+6] = j;
00374 #else
00375                                         // PERFORMANCE OPTIMIZATION:
00376                                         pnt2_index = ((*pnt2)-1) * 7;
00377                                         work2[pnt2_index+0]++;
00378                                         work2[pnt2_index+1]+= i;
00379                                         work2[pnt2_index+2]+= j;
00380                     if( work2[pnt2_index+4] < i ) work2[pnt2_index+4] = i;
00381                                         work2[pnt2_index+6] = j;
00382 #endif
00383                 }
00384                 else if( *(pnt2-1) > 0) {
00385                     *pnt2 = *(pnt2-1);
00386 
00387 #ifndef USE_OPTIMIZATIONS
00388                                                 // ORIGINAL CODE
00389                     work2[((*pnt2)-1)*7+0] ++;
00390                     work2[((*pnt2)-1)*7+1] += i;
00391                     work2[((*pnt2)-1)*7+2] += j;
00392                     if( work2[((*pnt2)-1)*7+4] < i ) work2[((*pnt2)-1)*7+4] = i;
00393 #else
00394                                         // PERFORMANCE OPTIMIZATION:
00395                                         pnt2_index = ((*pnt2)-1) * 7;
00396                                         work2[pnt2_index+0]++;
00397                                         work2[pnt2_index+1]+= i;
00398                                         work2[pnt2_index+2]+= j;
00399                     if( work2[pnt2_index+4] < i ) work2[pnt2_index+4] = i;
00400 #endif
00401                                 }
00402                 else {
00403                     wk_max++;
00404                     if( wk_max > WORK_SIZE ) {
00405                         return(0);
00406                     }
00407                     work[wk_max-1] = *pnt2 = wk_max;
00408                     work2[(wk_max-1)*7+0] = 1;
00409                     work2[(wk_max-1)*7+1] = i;
00410                     work2[(wk_max-1)*7+2] = j;
00411                     work2[(wk_max-1)*7+3] = i;
00412                     work2[(wk_max-1)*7+4] = i;
00413                     work2[(wk_max-1)*7+5] = j;
00414                     work2[(wk_max-1)*7+6] = j;
00415                 }
00416             }
00417             else {
00418                 *pnt2 = 0;
00419             }
00420         }
00421         if (arImageProcMode == AR_IMAGE_PROC_IN_HALF) pnt += arImXsize*AR_PIX_SIZE_DEFAULT;
00422     }
00423 
00424     j = 1;
00425     wk = &(work[0]);
00426     for(i = 1; i <= wk_max; i++, wk++) {
00427         *wk = (*wk==i)? j++: work[(*wk)-1];
00428     }
00429     *label_num = *wlabel_num = j - 1;
00430     if( *label_num == 0 ) {
00431         return( l_image );
00432     }
00433 
00434     put_zero( (ARUint8 *)warea, *label_num *     sizeof(int) );
00435     put_zero( (ARUint8 *)wpos,  *label_num * 2 * sizeof(double) );
00436     for(i = 0; i < *label_num; i++) {
00437         wclip[i*4+0] = lxsize;
00438         wclip[i*4+1] = 0;
00439         wclip[i*4+2] = lysize;
00440         wclip[i*4+3] = 0;
00441     }
00442     for(i = 0; i < wk_max; i++) {
00443         j = work[i] - 1;
00444         warea[j]    += work2[i*7+0];
00445         wpos[j*2+0] += work2[i*7+1];
00446         wpos[j*2+1] += work2[i*7+2];
00447         if( wclip[j*4+0] > work2[i*7+3] ) wclip[j*4+0] = work2[i*7+3];
00448         if( wclip[j*4+1] < work2[i*7+4] ) wclip[j*4+1] = work2[i*7+4];
00449         if( wclip[j*4+2] > work2[i*7+5] ) wclip[j*4+2] = work2[i*7+5];
00450         if( wclip[j*4+3] < work2[i*7+6] ) wclip[j*4+3] = work2[i*7+6];
00451     }
00452 
00453     for( i = 0; i < *label_num; i++ ) {
00454         wpos[i*2+0] /= warea[i];
00455         wpos[i*2+1] /= warea[i];
00456     }
00457 
00458     *label_ref = work;
00459     *area      = warea;
00460     *pos       = wpos;
00461     *clip      = wclip;
00462     return (l_image);
00463 }
00464 
00465 static ARInt16 *labeling3( ARUint8 *image, int thresh,
00466                            int *label_num, int **area, double **pos, int **clip,
00467                            int **label_ref, int LorR )
00468 {
00469     ARUint8   *pnt;                     /*  image pointer       */
00470     ARInt16   *pnt1, *pnt2;             /*  image pointer       */
00471     int       *wk;                      /*  pointer for work    */
00472     int       wk_max;                   /*  work                */
00473     int       m,n;                      /*  work                */
00474     int       i,j,k;                    /*  for loop            */
00475     int       lxsize, lysize;
00476     int       poff;
00477     ARUint8   *dpnt;
00478     ARInt16   *l_image;
00479     int       *work, *work2;
00480     int       *wlabel_num;
00481     int       *warea;
00482     int       *wclip;
00483     double    *wpos;
00484         int               thresht3 = thresh * 3;
00485         static int imageProcModePrev = -1;
00486         static int imXsizePrev = -1;
00487         static int imYsizePrev = -1;
00488 
00489         // Ensure that the debug image is correct size.
00490         // If size has changed, debug image will need to be re-allocated.
00491         if (imageProcModePrev != arImageProcMode || imXsizePrev != arImXsize || imYsizePrev != arImYsize) {
00492                 arLabelingCleanup();
00493                 imageProcModePrev = arImageProcMode;
00494                 imXsizePrev = arImXsize;
00495                 imYsizePrev = arImYsize;
00496         }
00497 
00498     if( arImageProcMode == AR_IMAGE_PROC_IN_HALF ) {
00499         lxsize = arImXsize / 2;
00500         lysize = arImYsize / 2;
00501     }
00502     else {
00503         lxsize = arImXsize;
00504         lysize = arImYsize;
00505     }
00506 
00507     if( LorR ) {
00508         l_image = &l_imageL[0];
00509         work    = &workL[0];
00510         work2   = &work2L[0];
00511         wlabel_num = &wlabel_numL;
00512         warea   = &wareaL[0];
00513         wclip   = &wclipL[0];
00514         wpos    = &wposL[0];
00515         if( arImageL == NULL ) {
00516 #if 0
00517             int texXsize = 1;
00518             int texYsize = 1;
00519             while( texXsize < arImXsize ) texXsize *= 2;
00520             if( texXsize > 512 ) texXsize = 512;
00521             while( texYsize < arImYsize ) texYsize *= 2;
00522             arMalloc( arImageL, ARUint8, texXsize*texYsize*AR_PIX_SIZE_DEFAULT );
00523 #else
00524             arMalloc( arImageL, ARUint8, arImXsize*arImYsize*AR_PIX_SIZE_DEFAULT );
00525 #endif
00526             put_zero( arImageL, lxsize*lysize*AR_PIX_SIZE_DEFAULT );
00527             arImage = arImageL;
00528         }
00529     }
00530     else {
00531         l_image = &l_imageR[0];
00532         work    = &workR[0];
00533         work2   = &work2R[0];
00534         wlabel_num = &wlabel_numR;
00535         warea   = &wareaR[0];
00536         wclip   = &wclipR[0];
00537         wpos    = &wposR[0];
00538         if( arImageR == NULL ) {
00539 #if 0
00540             int texXsize = 1;
00541             int texYsize = 1;
00542             while( texXsize < arImXsize ) texXsize *= 2;
00543             if( texXsize > 512 ) texXsize = 512;
00544             while( texYsize < arImYsize ) texYsize *= 2;
00545             arMalloc( arImageR, ARUint8, texXsize*texYsize*AR_PIX_SIZE_DEFAULT );
00546 #else
00547             arMalloc( arImageR, ARUint8, arImXsize*arImYsize*AR_PIX_SIZE_DEFAULT );
00548 #endif
00549             put_zero( arImageR, lxsize*lysize*AR_PIX_SIZE_DEFAULT );
00550         }
00551     }
00552 
00553     pnt1 = &l_image[0];
00554     pnt2 = &l_image[(lysize-1)*lxsize];
00555     for(i = 0; i < lxsize; i++) {
00556         *(pnt1++) = *(pnt2++) = 0;
00557     }
00558 
00559     pnt1 = &l_image[0];
00560     pnt2 = &l_image[lxsize-1];
00561     for(i = 0; i < lysize; i++) {
00562         *pnt1 = *pnt2 = 0;
00563         pnt1 += lxsize;
00564         pnt2 += lxsize;
00565     }
00566 
00567     wk_max = 0;
00568     pnt2 = &(l_image[lxsize+1]);
00569     if( LorR ) dpnt = &(arImageL[(lxsize+1)*AR_PIX_SIZE_DEFAULT]);
00570     else       dpnt = &(arImageR[(lxsize+1)*AR_PIX_SIZE_DEFAULT]);
00571     if( arImageProcMode == AR_IMAGE_PROC_IN_HALF ) {
00572         pnt = &(image[(arImXsize*2+2)*AR_PIX_SIZE_DEFAULT]);
00573         poff = AR_PIX_SIZE_DEFAULT*2;
00574     }
00575     else {
00576         pnt = &(image[(arImXsize+1)*AR_PIX_SIZE_DEFAULT]);
00577         poff = AR_PIX_SIZE_DEFAULT;
00578     }
00579     for(j = 1; j < lysize-1; j++, pnt+=poff*2, pnt2+=2, dpnt+=AR_PIX_SIZE_DEFAULT*2) {
00580         for(i = 1; i < lxsize-1; i++, pnt+=poff, pnt2++, dpnt+=AR_PIX_SIZE_DEFAULT) {
00581 #if (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_ARGB)
00582             if( *(pnt+1) + *(pnt+2) + *(pnt+3) <= thresht3 ) {
00583                 *(dpnt+1) = *(dpnt+2) = *(dpnt+3) = 255;
00584 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_ABGR)
00585             if( *(pnt+1) + *(pnt+2) + *(pnt+3) <= thresht3 ) {
00586                 *(dpnt+1) = *(dpnt+2) = *(dpnt+3) = 255;
00587 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGRA)
00588             if( *(pnt+0) + *(pnt+1) + *(pnt+2) <= thresht3 ) {
00589                 *(dpnt+0) = *(dpnt+1) = *(dpnt+2) = 255;
00590 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGR)
00591             if( *(pnt+0) + *(pnt+1) + *(pnt+2) <= thresht3 ) {
00592                 *(dpnt+0) = *(dpnt+1) = *(dpnt+2) = 255;
00593 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGBA)
00594             if( *(pnt+0) + *(pnt+1) + *(pnt+2) <= thresht3 ) {
00595                 *(dpnt+0) = *(dpnt+1) = *(dpnt+2) = 255;
00596 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGB)
00597             if( *(pnt+0) + *(pnt+1) + *(pnt+2) <= thresht3 ) {
00598                 *(dpnt+0) = *(dpnt+1) = *(dpnt+2) = 255;
00599 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_MONO)
00600                         if( *(pnt) <= thresh ) {
00601                                 *(dpnt) = 255;
00602 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_2vuy)
00603                         if( *(pnt+1) <= thresh ) {
00604                                 *(dpnt+0) = 128; *(dpnt+1) = 235; // *(dpnt+0) is chroma, set to 128 to maintain black & white debug image.
00605 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_yuvs)
00606                         if( *(pnt+0) <= thresh ) {
00607                                 *(dpnt+0) = 235; *(dpnt+1) = 128; // *(dpnt+1) is chroma, set to 128 to maintain black & white debug image.
00608 #else
00609 #  error Unknown default pixel format defined in config.h
00610 #endif
00611                                                 pnt1 = &(pnt2[-lxsize]);
00612                 if( *pnt1 > 0 ) {
00613                     *pnt2 = *pnt1;
00614                     work2[((*pnt2)-1)*7+0] ++;
00615                     work2[((*pnt2)-1)*7+1] += i;
00616                     work2[((*pnt2)-1)*7+2] += j;
00617                     work2[((*pnt2)-1)*7+6] = j;
00618                 }
00619                 else if( *(pnt1+1) > 0 ) {
00620                     if( *(pnt1-1) > 0 ) {
00621                         m = work[*(pnt1+1)-1];
00622                         n = work[*(pnt1-1)-1];
00623                         if( m > n ) {
00624                             *pnt2 = n;
00625                             wk = &(work[0]);
00626                             for(k = 0; k < wk_max; k++) {
00627                                 if( *wk == m ) *wk = n;
00628                                 wk++;
00629                             }
00630                         }
00631                         else if( m < n ) {
00632                             *pnt2 = m;
00633                             wk = &(work[0]);
00634                             for(k = 0; k < wk_max; k++) {
00635                                 if( *wk == n ) *wk = m;
00636                                 wk++;
00637                             }
00638                         }
00639                         else *pnt2 = m;
00640                         work2[((*pnt2)-1)*7+0] ++;
00641                         work2[((*pnt2)-1)*7+1] += i;
00642                         work2[((*pnt2)-1)*7+2] += j;
00643                         work2[((*pnt2)-1)*7+6] = j;
00644                     }
00645                     else if( *(pnt2-1) > 0 ) {
00646                         m = work[*(pnt1+1)-1];
00647                         n = work[*(pnt2-1)-1];
00648                         if( m > n ) {
00649                             *pnt2 = n;
00650                             wk = &(work[0]);
00651                             for(k = 0; k < wk_max; k++) {
00652                                 if( *wk == m ) *wk = n;
00653                                 wk++;
00654                             }
00655                         }
00656                         else if( m < n ) {
00657                             *pnt2 = m;
00658                             wk = &(work[0]);
00659                             for(k = 0; k < wk_max; k++) {
00660                                 if( *wk == n ) *wk = m;
00661                                 wk++;
00662                             }
00663                         }
00664                         else *pnt2 = m;
00665                         work2[((*pnt2)-1)*7+0] ++;
00666                         work2[((*pnt2)-1)*7+1] += i;
00667                         work2[((*pnt2)-1)*7+2] += j;
00668                     }
00669                     else {
00670                         *pnt2 = *(pnt1+1);
00671                         work2[((*pnt2)-1)*7+0] ++;
00672                         work2[((*pnt2)-1)*7+1] += i;
00673                         work2[((*pnt2)-1)*7+2] += j;
00674                         if( work2[((*pnt2)-1)*7+3] > i ) work2[((*pnt2)-1)*7+3] = i;
00675                         work2[((*pnt2)-1)*7+6] = j;
00676                     }
00677                 }
00678                 else if( *(pnt1-1) > 0 ) {
00679                     *pnt2 = *(pnt1-1);
00680                     work2[((*pnt2)-1)*7+0] ++;
00681                     work2[((*pnt2)-1)*7+1] += i;
00682                     work2[((*pnt2)-1)*7+2] += j;
00683                     if( work2[((*pnt2)-1)*7+4] < i ) work2[((*pnt2)-1)*7+4] = i;
00684                     work2[((*pnt2)-1)*7+6] = j;
00685                 }
00686                 else if( *(pnt2-1) > 0) {
00687                     *pnt2 = *(pnt2-1);
00688                     work2[((*pnt2)-1)*7+0] ++;
00689                     work2[((*pnt2)-1)*7+1] += i;
00690                     work2[((*pnt2)-1)*7+2] += j;
00691                     if( work2[((*pnt2)-1)*7+4] < i ) work2[((*pnt2)-1)*7+4] = i;
00692                 }
00693                 else {
00694                     wk_max++;
00695                     if( wk_max > WORK_SIZE ) {
00696                         return(0);
00697                     }
00698                     work[wk_max-1] = *pnt2 = wk_max;
00699                     work2[(wk_max-1)*7+0] = 1;
00700                     work2[(wk_max-1)*7+1] = i;
00701                     work2[(wk_max-1)*7+2] = j;
00702                     work2[(wk_max-1)*7+3] = i;
00703                     work2[(wk_max-1)*7+4] = i;
00704                     work2[(wk_max-1)*7+5] = j;
00705                     work2[(wk_max-1)*7+6] = j;
00706                 }
00707             }
00708             else {
00709                 *pnt2 = 0;
00710 #if (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_ARGB)
00711                 *(dpnt+1) = *(dpnt+2) = *(dpnt+3) = 0; }
00712 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_ABGR)
00713                 *(dpnt+1) = *(dpnt+2) = *(dpnt+3) = 0; }
00714 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGRA)
00715                 *(dpnt+0) = *(dpnt+1) = *(dpnt+2) = 0; }
00716 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_BGR)
00717                 *(dpnt+0) = *(dpnt+1) = *(dpnt+2) = 0; }
00718 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGBA)
00719                 *(dpnt+0) = *(dpnt+1) = *(dpnt+2) = 0; }
00720 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_RGB)
00721                 *(dpnt+0) = *(dpnt+1) = *(dpnt+2) = 0; }
00722 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_MONO)
00723                 *(dpnt) = 0; }
00724 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_2vuy)
00725                 *(dpnt+0) = 128; *(dpnt+1) = 16; } // *(dpnt+0) is chroma, set to 128 to maintain black & white debug image.
00726 #elif (AR_DEFAULT_PIXEL_FORMAT == AR_PIXEL_FORMAT_yuvs)
00727                 *(dpnt+0) = 16; *(dpnt+1) = 128; } // *(dpnt+1) is chroma, set to 128 to maintain black & white debug image.
00728 #else
00729 #  error Unknown default pixel format defined in config.h
00730 #endif
00731         }
00732         if (arImageProcMode == AR_IMAGE_PROC_IN_HALF) pnt += arImXsize*AR_PIX_SIZE_DEFAULT;
00733     }
00734 
00735     j = 1;
00736     wk = &(work[0]);
00737     for(i = 1; i <= wk_max; i++, wk++) {
00738         *wk = (*wk==i)? j++: work[(*wk)-1];
00739     }
00740     *label_num = *wlabel_num = j - 1;
00741     if( *label_num == 0 ) {
00742         return( l_image );
00743     }
00744 
00745     put_zero( (ARUint8 *)warea, *label_num *     sizeof(int) );
00746     put_zero( (ARUint8 *)wpos,  *label_num * 2 * sizeof(double) );
00747     for(i = 0; i < *label_num; i++) {
00748         wclip[i*4+0] = lxsize;
00749         wclip[i*4+1] = 0;
00750         wclip[i*4+2] = lysize;
00751         wclip[i*4+3] = 0;
00752     }
00753     for(i = 0; i < wk_max; i++) {
00754         j = work[i] - 1;
00755         warea[j]    += work2[i*7+0];
00756         wpos[j*2+0] += work2[i*7+1];
00757         wpos[j*2+1] += work2[i*7+2];
00758         if( wclip[j*4+0] > work2[i*7+3] ) wclip[j*4+0] = work2[i*7+3];
00759         if( wclip[j*4+1] < work2[i*7+4] ) wclip[j*4+1] = work2[i*7+4];
00760         if( wclip[j*4+2] > work2[i*7+5] ) wclip[j*4+2] = work2[i*7+5];
00761         if( wclip[j*4+3] < work2[i*7+6] ) wclip[j*4+3] = work2[i*7+6];
00762     }
00763 
00764     for( i = 0; i < *label_num; i++ ) {
00765         wpos[i*2+0] /= warea[i];
00766         wpos[i*2+1] /= warea[i];
00767     }
00768 
00769     *label_ref = work;
00770     *area      = warea;
00771     *pos       = wpos;
00772     *clip      = wclip;
00773     return( l_image );
00774 }
00775 
00776 void arLabelingCleanup(void)
00777 {
00778         if (arImageL) {
00779                 free (arImageL);
00780                 arImageL = NULL;
00781                 arImage = NULL;
00782         }
00783         if (arImageR) {
00784                 free (arImageR);
00785                 arImageR = NULL;
00786         }
00787 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


ar_recog
Author(s): Graylin Trevor Jay and Christopher Crick
autogenerated on Fri Jan 25 2013 12:14:59