]> rtime.felk.cvut.cz Git - opencv.git/blobdiff - opencv/src/cv/cvhaar.cpp
fixed MSVC 2008 compile errors and warnings
[opencv.git] / opencv / src / cv / cvhaar.cpp
index 82e2bd06369a129dcf4606890272e51734649402..20ea15f23a2dcfd522ed794cc57eb90b1fda8af0 100644 (file)
 #include "_cv.h"
 #include <stdio.h>
 
+/*#if CV_SSE2
+#   if CV_SSE4 || defined __SSE4__
+#       include <smmintrin.h>
+#   else
+#       define _mm_blendv_pd(a, b, m) _mm_xor_pd(a, _mm_and_pd(_mm_xor_pd(b, a), m))
+#       define _mm_blendv_ps(a, b, m) _mm_xor_ps(a, _mm_and_ps(_mm_xor_ps(b, a), m))
+#   endif
+#if defined CV_ICC
+#   define CV_HAAR_USE_SSE 1
+#endif
+#endif*/
+
 /* these settings affect the quality of detection: change with care */
 #define CV_ADJUST_FEATURES 1
 #define CV_ADJUST_WEIGHTS  0
@@ -89,7 +101,7 @@ typedef struct CvHidHaarStageClassifier
     float threshold;
     CvHidHaarClassifier* classifier;
     int two_rects;
-    
+
     struct CvHidHaarStageClassifier* next;
     struct CvHidHaarStageClassifier* child;
     struct CvHidHaarStageClassifier* parent;
@@ -108,44 +120,65 @@ struct CvHidHaarClassifierCascade
     CvHidHaarStageClassifier* stage_classifier;
     sqsumtype *pq0, *pq1, *pq2, *pq3;
     sumtype *p0, *p1, *p2, *p3;
+
+    void** ipp_stages;
 };
 
 
+const int icv_object_win_border = 1;
+const float icv_stage_threshold_bias = 0.0001f;
+
 static CvHaarClassifierCascade*
 icvCreateHaarClassifierCascade( int stage_count )
 {
     CvHaarClassifierCascade* cascade = 0;
-    
-    CV_FUNCNAME( "icvCreateHaarClassifierCascade" );
-
-    __BEGIN__;
 
     int block_size = sizeof(*cascade) + stage_count*sizeof(*cascade->stage_classifier);
 
     if( stage_count <= 0 )
-        CV_ERROR( CV_StsOutOfRange, "Number of stages should be positive" );
+        CV_Error( CV_StsOutOfRange, "Number of stages should be positive" );
 
-    CV_CALL( cascade = (CvHaarClassifierCascade*)cvAlloc( block_size ));
+    cascade = (CvHaarClassifierCascade*)cvAlloc( block_size );
     memset( cascade, 0, block_size );
 
     cascade->stage_classifier = (CvHaarStageClassifier*)(cascade + 1);
     cascade->flags = CV_HAAR_MAGIC_VAL;
     cascade->count = stage_count;
 
-    __END__;
-
     return cascade;
 }
 
+static void
+icvReleaseHidHaarClassifierCascade( CvHidHaarClassifierCascade** _cascade )
+{
+    if( _cascade && *_cascade )
+    {
+#ifdef HAVE_IPP
+        CvHidHaarClassifierCascade* cascade = *_cascade;
+        if( cascade->ipp_stages )
+        {
+            int i;
+            for( i = 0; i < cascade->count; i++ )
+            {
+                if( cascade->ipp_stages[i] )
+                    ippiHaarClassifierFree_32f( (IppiHaarClassifier_32f*)cascade->ipp_stages[i] );
+            }
+        }
+        cvFree( &cascade->ipp_stages );
+#endif
+        cvFree( _cascade );
+    }
+}
+
 /* create more efficient internal representation of haar classifier cascade */
 static CvHidHaarClassifierCascade*
 icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
 {
-    CvHidHaarClassifierCascade* out = 0;
-
-    CV_FUNCNAME( "icvCreateHidHaarClassifierCascade" );
+    CvRect* ipp_features = 0;
+    float *ipp_weights = 0, *ipp_thresholds = 0, *ipp_val1 = 0, *ipp_val2 = 0;
+    int* ipp_counts = 0;
 
-    __BEGIN__;
+    CvHidHaarClassifierCascade* out = 0;
 
     int i, j, k, l;
     int datasize;
@@ -156,21 +189,22 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
     CvHidHaarTreeNode* haar_node_ptr;
     CvSize orig_window_size;
     int has_tilted_features = 0;
+    int max_count = 0;
 
     if( !CV_IS_HAAR_CLASSIFIER(cascade) )
-        CV_ERROR( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
+        CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
 
     if( cascade->hid_cascade )
-        CV_ERROR( CV_StsError, "hid_cascade has been already created" );
+        CV_Error( CV_StsError, "hid_cascade has been already created" );
 
     if( !cascade->stage_classifier )
-        CV_ERROR( CV_StsNullPtr, "" );
+        CV_Error( CV_StsNullPtr, "" );
 
     if( cascade->count <= 0 )
-        CV_ERROR( CV_StsOutOfRange, "Negative number of cascade stages" );
+        CV_Error( CV_StsOutOfRange, "Negative number of cascade stages" );
 
     orig_window_size = cascade->orig_window_size;
-    
+
     /* check input structure correctness and calculate total memory size needed for
        internal representation of the classifier cascade */
     for( i = 0; i < cascade->count; i++ )
@@ -182,9 +216,10 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
         {
             sprintf( errorstr, "header of the stage classifier #%d is invalid "
                      "(has null pointers or non-positive classfier count)", i );
-            CV_ERROR( CV_StsError, errorstr );
+            CV_Error( CV_StsError, errorstr );
         }
 
+        max_count = MAX( max_count, stage_classifier->count );
         total_classifiers += stage_classifier->count;
 
         for( j = 0; j < stage_classifier->count; j++ )
@@ -196,7 +231,7 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
             {
                 for( k = 0; k < CV_HAAR_FEATURE_MAX; k++ )
                 {
-                    if( classifier->haar_feature[l].rect[k].weight )
+                    if( classifier->haar_feature[l].rect[k].r.width )
                     {
                         CvRect r = classifier->haar_feature[l].rect[k].r;
                         int tilted = classifier->haar_feature[l].tilted;
@@ -213,7 +248,7 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
                             sprintf( errorstr, "rectangle #%d of the classifier #%d of "
                                      "the stage classifier #%d is not inside "
                                      "the reference (original) cascade window", k, j, i );
-                            CV_ERROR( CV_StsNullPtr, errorstr );
+                            CV_Error( CV_StsNullPtr, errorstr );
                         }
                     }
                 }
@@ -228,7 +263,7 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
                sizeof(CvHidHaarTreeNode) * total_nodes +
                sizeof(void*)*(total_nodes + total_classifiers);
 
-    CV_CALL( out = (CvHidHaarClassifierCascade*)cvAlloc( datasize ));
+    out = (CvHidHaarClassifierCascade*)cvAlloc( datasize );
     memset( out, 0, sizeof(*out) );
 
     /* init header */
@@ -248,7 +283,7 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
         CvHidHaarStageClassifier* hid_stage_classifier = out->stage_classifier + i;
 
         hid_stage_classifier->count = stage_classifier->count;
-        hid_stage_classifier->threshold = stage_classifier->threshold;
+        hid_stage_classifier->threshold = stage_classifier->threshold - icv_stage_threshold_bias;
         hid_stage_classifier->classifier = haar_classifier_ptr;
         hid_stage_classifier->two_rects = 1;
         haar_classifier_ptr += stage_classifier->count;
@@ -259,7 +294,7 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
             ? NULL : out->stage_classifier + stage_classifier->next;
         hid_stage_classifier->child = (stage_classifier->child == -1)
             ? NULL : out->stage_classifier + stage_classifier->child;
-        
+
         out->is_tree |= hid_stage_classifier->next != NULL;
 
         for( j = 0; j < stage_classifier->count; j++ )
@@ -272,7 +307,7 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
             hid_classifier->count = node_count;
             hid_classifier->node = haar_node_ptr;
             hid_classifier->alpha = alpha_ptr;
-            
+
             for( l = 0; l < node_count; l++ )
             {
                 CvHidHaarTreeNode* node = hid_classifier->node + l;
@@ -282,7 +317,7 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
                 node->left = classifier->left[l];
                 node->right = classifier->right[l];
 
-                if( feature->rect[2].weight == 0 ||
+                if( fabs(feature->rect[2].weight) < DBL_EPSILON ||
                     feature->rect[2].r.width == 0 ||
                     feature->rect[2].r.height == 0 )
                     memset( &(node->feature.rect[2]), 0, sizeof(node->feature.rect[2]) );
@@ -298,13 +333,71 @@ icvCreateHidHaarClassifierCascade( CvHaarClassifierCascade* cascade )
         }
     }
 
+#ifdef HAVE_IPP
+    int can_use_ipp = !out->has_tilted_features && !out->is_tree && out->is_stump_based;
+
+    if( can_use_ipp )
+    {
+        int ipp_datasize = cascade->count*sizeof(out->ipp_stages[0]);
+        float ipp_weight_scale=(float)(1./((orig_window_size.width-icv_object_win_border*2)*
+            (orig_window_size.height-icv_object_win_border*2)));
+
+        out->ipp_stages = (void**)cvAlloc( ipp_datasize );
+        memset( out->ipp_stages, 0, ipp_datasize );
+
+        ipp_features = (CvRect*)cvAlloc( max_count*3*sizeof(ipp_features[0]) );
+        ipp_weights = (float*)cvAlloc( max_count*3*sizeof(ipp_weights[0]) );
+        ipp_thresholds = (float*)cvAlloc( max_count*sizeof(ipp_thresholds[0]) );
+        ipp_val1 = (float*)cvAlloc( max_count*sizeof(ipp_val1[0]) );
+        ipp_val2 = (float*)cvAlloc( max_count*sizeof(ipp_val2[0]) );
+        ipp_counts = (int*)cvAlloc( max_count*sizeof(ipp_counts[0]) );
+
+        for( i = 0; i < cascade->count; i++ )
+        {
+            CvHaarStageClassifier* stage_classifier = cascade->stage_classifier + i;
+            for( j = 0, k = 0; j < stage_classifier->count; j++ )
+            {
+                CvHaarClassifier* classifier = stage_classifier->classifier + j;
+                int rect_count = 2 + (classifier->haar_feature->rect[2].r.width != 0);
+
+                ipp_thresholds[j] = classifier->threshold[0];
+                ipp_val1[j] = classifier->alpha[0];
+                ipp_val2[j] = classifier->alpha[1];
+                ipp_counts[j] = rect_count;
+
+                for( l = 0; l < rect_count; l++, k++ )
+                {
+                    ipp_features[k] = classifier->haar_feature->rect[l].r;
+                    //ipp_features[k].y = orig_window_size.height - ipp_features[k].y - ipp_features[k].height;
+                    ipp_weights[k] = classifier->haar_feature->rect[l].weight*ipp_weight_scale;
+                }
+            }
+
+            if( ippiHaarClassifierInitAlloc_32f( (IppiHaarClassifier_32f**)&out->ipp_stages[i],
+                (const IppiRect*)ipp_features, ipp_weights, ipp_thresholds,
+                ipp_val1, ipp_val2, ipp_counts, stage_classifier->count ) < 0 )
+                break;
+        }
+
+        if( i < cascade->count )
+        {
+            for( j = 0; j < i; j++ )
+                if( out->ipp_stages[i] )
+                    ippiHaarClassifierFree_32f( (IppiHaarClassifier_32f*)out->ipp_stages[i] );
+            cvFree( &out->ipp_stages );
+        }
+    }
+#endif
+
     cascade->hid_cascade = out;
     assert( (char*)haar_node_ptr - (char*)out <= datasize );
 
-    __END__;
-
-    if( cvGetErrStatus() < 0 )
-        cvFree( (void**)&out );
+    cvFree( &ipp_features );
+    cvFree( &ipp_weights );
+    cvFree( &ipp_thresholds );
+    cvFree( &ipp_val1 );
+    cvFree( &ipp_val2 );
+    cvFree( &ipp_counts );
 
     return out;
 }
@@ -327,109 +420,105 @@ cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
                                      const CvArr* _tilted_sum,
                                      double scale )
 {
-    CV_FUNCNAME("cvSetImagesForHaarClassifierCascade");
-
-    __BEGIN__;
-
     CvMat sum_stub, *sum = (CvMat*)_sum;
     CvMat sqsum_stub, *sqsum = (CvMat*)_sqsum;
     CvMat tilted_stub, *tilted = (CvMat*)_tilted_sum;
     CvHidHaarClassifierCascade* cascade;
     int coi0 = 0, coi1 = 0;
-    int i, j, k, l;
-    CvRect equ_rect;
+    int i;
+    CvRect equRect;
     double weight_scale;
 
     if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
-        CV_ERROR( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
+        CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier pointer" );
 
     if( scale <= 0 )
-        CV_ERROR( CV_StsOutOfRange, "Scale must be positive" );
+        CV_Error( CV_StsOutOfRange, "Scale must be positive" );
 
-    CV_CALL( sum = cvGetMat( sum, &sum_stub, &coi0 ));
-    CV_CALL( sqsum = cvGetMat( sqsum, &sqsum_stub, &coi1 ));
+    sum = cvGetMat( sum, &sum_stub, &coi0 );
+    sqsum = cvGetMat( sqsum, &sqsum_stub, &coi1 );
 
     if( coi0 || coi1 )
-        CV_ERROR( CV_BadCOI, "COI is not supported" );
+        CV_Error( CV_BadCOI, "COI is not supported" );
 
     if( !CV_ARE_SIZES_EQ( sum, sqsum ))
-        CV_ERROR( CV_StsUnmatchedSizes, "All integral images must have the same size" );
+        CV_Error( CV_StsUnmatchedSizes, "All integral images must have the same size" );
 
     if( CV_MAT_TYPE(sqsum->type) != CV_64FC1 ||
         CV_MAT_TYPE(sum->type) != CV_32SC1 )
-        CV_ERROR( CV_StsUnsupportedFormat,
+        CV_Error( CV_StsUnsupportedFormat,
         "Only (32s, 64f, 32s) combination of (sum,sqsum,tilted_sum) formats is allowed" );
 
     if( !_cascade->hid_cascade )
-        CV_CALL( icvCreateHidHaarClassifierCascade(_cascade) );
+        icvCreateHidHaarClassifierCascade(_cascade);
 
     cascade = _cascade->hid_cascade;
 
     if( cascade->has_tilted_features )
     {
-        CV_CALL( tilted = cvGetMat( tilted, &tilted_stub, &coi1 ));
+        tilted = cvGetMat( tilted, &tilted_stub, &coi1 );
 
         if( CV_MAT_TYPE(tilted->type) != CV_32SC1 )
-            CV_ERROR( CV_StsUnsupportedFormat,
+            CV_Error( CV_StsUnsupportedFormat,
             "Only (32s, 64f, 32s) combination of (sum,sqsum,tilted_sum) formats is allowed" );
 
         if( sum->step != tilted->step )
-            CV_ERROR( CV_StsUnmatchedSizes,
+            CV_Error( CV_StsUnmatchedSizes,
             "Sum and tilted_sum must have the same stride (step, widthStep)" );
 
         if( !CV_ARE_SIZES_EQ( sum, tilted ))
-            CV_ERROR( CV_StsUnmatchedSizes, "All integral images must have the same size" );
+            CV_Error( CV_StsUnmatchedSizes, "All integral images must have the same size" );
         cascade->tilted = *tilted;
     }
-    
+
     _cascade->scale = scale;
     _cascade->real_window_size.width = cvRound( _cascade->orig_window_size.width * scale );
     _cascade->real_window_size.height = cvRound( _cascade->orig_window_size.height * scale );
 
     cascade->sum = *sum;
     cascade->sqsum = *sqsum;
-    
-    equ_rect.x = equ_rect.y = cvRound(scale);
-    equ_rect.width = cvRound((_cascade->orig_window_size.width-2)*scale);
-    equ_rect.height = cvRound((_cascade->orig_window_size.height-2)*scale);
-    weight_scale = 1./(equ_rect.width*equ_rect.height);
+
+    equRect.x = equRect.y = cvRound(scale);
+    equRect.width = cvRound((_cascade->orig_window_size.width-2)*scale);
+    equRect.height = cvRound((_cascade->orig_window_size.height-2)*scale);
+    weight_scale = 1./(equRect.width*equRect.height);
     cascade->inv_window_area = weight_scale;
 
-    cascade->p0 = sum_elem_ptr(*sum, equ_rect.y, equ_rect.x);
-    cascade->p1 = sum_elem_ptr(*sum, equ_rect.y, equ_rect.x + equ_rect.width );
-    cascade->p2 = sum_elem_ptr(*sum, equ_rect.y + equ_rect.height, equ_rect.x );
-    cascade->p3 = sum_elem_ptr(*sum, equ_rect.y + equ_rect.height,
-                                     equ_rect.x + equ_rect.width );
+    cascade->p0 = sum_elem_ptr(*sum, equRect.y, equRect.x);
+    cascade->p1 = sum_elem_ptr(*sum, equRect.y, equRect.x + equRect.width );
+    cascade->p2 = sum_elem_ptr(*sum, equRect.y + equRect.height, equRect.x );
+    cascade->p3 = sum_elem_ptr(*sum, equRect.y + equRect.height,
+                                     equRect.x + equRect.width );
 
-    cascade->pq0 = sqsum_elem_ptr(*sqsum, equ_rect.y, equ_rect.x);
-    cascade->pq1 = sqsum_elem_ptr(*sqsum, equ_rect.y, equ_rect.x + equ_rect.width );
-    cascade->pq2 = sqsum_elem_ptr(*sqsum, equ_rect.y + equ_rect.height, equ_rect.x );
-    cascade->pq3 = sqsum_elem_ptr(*sqsum, equ_rect.y + equ_rect.height,
-                                          equ_rect.x + equ_rect.width );
+    cascade->pq0 = sqsum_elem_ptr(*sqsum, equRect.y, equRect.x);
+    cascade->pq1 = sqsum_elem_ptr(*sqsum, equRect.y, equRect.x + equRect.width );
+    cascade->pq2 = sqsum_elem_ptr(*sqsum, equRect.y + equRect.height, equRect.x );
+    cascade->pq3 = sqsum_elem_ptr(*sqsum, equRect.y + equRect.height,
+                                          equRect.x + equRect.width );
 
     /* init pointers in haar features according to real window size and
        given image pointers */
     for( i = 0; i < _cascade->count; i++ )
     {
+        int j, k, l;
         for( j = 0; j < cascade->stage_classifier[i].count; j++ )
         {
             for( l = 0; l < cascade->stage_classifier[i].classifier[j].count; l++ )
             {
-                CvHaarFeature* feature = 
+                CvHaarFeature* feature =
                     &_cascade->stage_classifier[i].classifier[j].haar_feature[l];
                 /* CvHidHaarClassifier* classifier =
                     cascade->stage_classifier[i].classifier + j; */
-                CvHidHaarFeature* hidfeature = 
+                CvHidHaarFeature* hidfeature =
                     &cascade->stage_classifier[i].classifier[j].node[l].feature;
                 double sum0 = 0, area0 = 0;
                 CvRect r[3];
-#if CV_ADJUST_FEATURES
+
                 int base_w = -1, base_h = -1;
                 int new_base_w = 0, new_base_h = 0;
                 int kx, ky;
                 int flagx = 0, flagy = 0;
                 int x0 = 0, y0 = 0;
-#endif
                 int nr;
 
                 /* align blocks */
@@ -437,20 +526,17 @@ cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
                 {
                     if( !hidfeature->rect[k].p0 )
                         break;
-#if CV_ADJUST_FEATURES
                     r[k] = feature->rect[k].r;
                     base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].width-1) );
                     base_w = (int)CV_IMIN( (unsigned)base_w, (unsigned)(r[k].x - r[0].x-1) );
                     base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].height-1) );
                     base_h = (int)CV_IMIN( (unsigned)base_h, (unsigned)(r[k].y - r[0].y-1) );
-#endif
                 }
 
-                base_w += 1;
-                base_h += 1;
                 nr = k;
 
-#if CV_ADJUST_FEATURES
+                base_w += 1;
+                base_h += 1;
                 kx = r[0].width / base_w;
                 ky = r[0].height / base_h;
 
@@ -467,34 +553,29 @@ cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
                     new_base_h = cvRound( r[0].height * scale ) / ky;
                     y0 = cvRound( r[0].y * scale );
                 }
-#endif
-            
+
                 for( k = 0; k < nr; k++ )
                 {
                     CvRect tr;
-                    double correctionRatio;
-                
-#if CV_ADJUST_FEATURES
+                    double correction_ratio;
+
                     if( flagx )
                     {
                         tr.x = (r[k].x - r[0].x) * new_base_w / base_w + x0;
                         tr.width = r[k].width * new_base_w / base_w;
                     }
                     else
-#endif
                     {
                         tr.x = cvRound( r[k].x * scale );
                         tr.width = cvRound( r[k].width * scale );
                     }
 
-#if CV_ADJUST_FEATURES
                     if( flagy )
                     {
                         tr.y = (r[k].y - r[0].y) * new_base_h / base_h + y0;
                         tr.height = r[k].height * new_base_h / base_h;
                     }
                     else
-#endif
                     {
                         tr.y = cvRound( r[k].y * scale );
                         tr.height = cvRound( r[k].height * scale );
@@ -503,18 +584,18 @@ cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
 #if CV_ADJUST_WEIGHTS
                     {
                     // RAINER START
-                    const float orig_feature_size =  (float)(feature->rect[k].r.width)*feature->rect[k].r.height; 
-                    const float orig_norm_size = (float)(cascade->orig_window_size.width)*(cascade->orig_window_size.height);
+                    const float orig_feature_size =  (float)(feature->rect[k].r.width)*feature->rect[k].r.height;
+                    const float orig_norm_size = (float)(_cascade->orig_window_size.width)*(_cascade->orig_window_size.height);
                     const float feature_size = float(tr.width*tr.height);
-                    //const float normSize    = float(equ_rect.width*equ_rect.height);
-                    float targetRatio = orig_feature_size / origNormSize;
+                    //const float normSize    = float(equRect.width*equRect.height);
+                    float target_ratio = orig_feature_size / orig_norm_size;
                     //float isRatio = featureSize / normSize;
                     //correctionRatio = targetRatio / isRatio / normSize;
-                    correctionRatio = targetRatio / featureSize;
+                    correction_ratio = target_ratio / feature_size;
                     // RAINER END
                     }
 #else
-                    correctionRatio = weight_scale * (!feature->tilted ? 1 : 0.5);
+                    correction_ratio = weight_scale * (!feature->tilted ? 1 : 0.5);
 #endif
 
                     if( !feature->tilted )
@@ -533,7 +614,7 @@ cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
                         hidfeature->rect[k].p1 = sum_elem_ptr(*tilted, tr.y + tr.height, tr.x - tr.height);
                     }
 
-                    hidfeature->rect[k].weight = (float)(feature->rect[k].weight * correctionRatio);
+                    hidfeature->rect[k].weight = (float)(feature->rect[k].weight * correction_ratio);
 
                     if( k == 0 )
                         area0 = tr.width * tr.height;
@@ -545,8 +626,6 @@ cvSetImagesForHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
             } /* l */
         } /* j */
     }
-
-    __END__;
 }
 
 
@@ -556,7 +635,7 @@ double icvEvalHidHaarClassifier( CvHidHaarClassifier* classifier,
                                  size_t p_offset )
 {
     int idx = 0;
-    do 
+    do
     {
         CvHidHaarTreeNode* node = classifier->node + idx;
         double t = node->threshold * variance_norm_factor;
@@ -575,13 +654,10 @@ double icvEvalHidHaarClassifier( CvHidHaarClassifier* classifier,
 
 
 CV_IMPL int
-cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
+cvRunHaarClassifierCascade( const CvHaarClassifierCascade* _cascade,
                             CvPoint pt, int start_stage )
 {
     int result = -1;
-    CV_FUNCNAME("cvRunHaarClassifierCascade");
-
-    __BEGIN__;
 
     int p_offset, pq_offset;
     int i, j;
@@ -589,17 +665,17 @@ cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
     CvHidHaarClassifierCascade* cascade;
 
     if( !CV_IS_HAAR_CLASSIFIER(_cascade) )
-        CV_ERROR( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid cascade pointer" );
+        CV_Error( !_cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid cascade pointer" );
 
     cascade = _cascade->hid_cascade;
     if( !cascade )
-        CV_ERROR( CV_StsNullPtr, "Hidden cascade has not been created.\n"
+        CV_Error( CV_StsNullPtr, "Hidden cascade has not been created.\n"
             "Use cvSetImagesForHaarClassifierCascade" );
 
     if( pt.x < 0 || pt.y < 0 ||
         pt.x + _cascade->real_window_size.width >= cascade->sum.width-2 ||
         pt.y + _cascade->real_window_size.height >= cascade->sum.height-2 )
-        EXIT;
+        return -1;
 
     p_offset = pt.y * (cascade->sum.step/sizeof(sumtype)) + pt.x;
     pq_offset = pt.y * (cascade->sqsum.step/sizeof(sqsumtype)) + pt.x;
@@ -630,7 +706,7 @@ cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
                     variance_norm_factor, p_offset );
             }
 
-            if( stage_sum >= ptr->threshold - 0.0001 )
+            if( stage_sum >= ptr->threshold )
             {
                 ptr = ptr->child;
             }
@@ -638,10 +714,7 @@ cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
             {
                 while( ptr && ptr->next == NULL ) ptr = ptr->parent;
                 if( ptr == NULL )
-                {
-                    result = 0;
-                    EXIT;
-                }
+                    return 0;
                 ptr = ptr->next;
             }
         }
@@ -650,7 +723,11 @@ cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
     {
         for( i = start_stage; i < cascade->count; i++ )
         {
+#ifndef CV_HAAR_USE_SSE
             double stage_sum = 0;
+#else
+            __m128d stage_sum = _mm_setzero_pd();
+#endif
 
             if( cascade->stage_classifier[i].two_rects )
             {
@@ -658,14 +735,21 @@ cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
                 {
                     CvHidHaarClassifier* classifier = cascade->stage_classifier[i].classifier + j;
                     CvHidHaarTreeNode* node = classifier->node;
-                    double sum, t = node->threshold*variance_norm_factor, a, b;
-
-                    sum = calc_sum(node->feature.rect[0],p_offset) * node->feature.rect[0].weight;
+#ifndef CV_HAAR_USE_SSE
+                    double t = node->threshold*variance_norm_factor;
+                    double sum = calc_sum(node->feature.rect[0],p_offset) * node->feature.rect[0].weight;
                     sum += calc_sum(node->feature.rect[1],p_offset) * node->feature.rect[1].weight;
-
-                    a = classifier->alpha[0];
-                    b = classifier->alpha[1];
-                    stage_sum += sum < t ? a : b;
+                    stage_sum += classifier->alpha[sum >= t];
+#else
+                    // ayasin - NHM perf optim. Avoid use of costly flaky jcc
+                    __m128d t = _mm_set_sd(node->threshold*variance_norm_factor);
+                    __m128d a = _mm_set_sd(classifier->alpha[0]);
+                    __m128d b = _mm_set_sd(classifier->alpha[1]);
+                    __m128d sum = _mm_set_sd(calc_sum(node->feature.rect[0],p_offset) * node->feature.rect[0].weight +
+                                             calc_sum(node->feature.rect[1],p_offset) * node->feature.rect[1].weight);
+                    t = _mm_cmpgt_sd(t, sum);
+                    stage_sum = _mm_add_sd(stage_sum, _mm_blendv_pd(b, a, t));
+#endif
                 }
             }
             else
@@ -674,25 +758,38 @@ cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
                 {
                     CvHidHaarClassifier* classifier = cascade->stage_classifier[i].classifier + j;
                     CvHidHaarTreeNode* node = classifier->node;
-                    double sum, t = node->threshold*variance_norm_factor, a, b;
-
-                    sum = calc_sum(node->feature.rect[0],p_offset) * node->feature.rect[0].weight;
+#ifndef CV_HAAR_USE_SSE
+                    double t = node->threshold*variance_norm_factor;
+                    double sum = calc_sum(node->feature.rect[0],p_offset) * node->feature.rect[0].weight;
                     sum += calc_sum(node->feature.rect[1],p_offset) * node->feature.rect[1].weight;
-
                     if( node->feature.rect[2].p0 )
                         sum += calc_sum(node->feature.rect[2],p_offset) * node->feature.rect[2].weight;
-
-                    a = classifier->alpha[0];
-                    b = classifier->alpha[1];
-                    stage_sum += sum < t ? a : b;
+                    
+                    stage_sum += classifier->alpha[sum >= t];
+#else
+                    // ayasin - NHM perf optim. Avoid use of costly flaky jcc
+                    __m128d t = _mm_set_sd(node->threshold*variance_norm_factor);
+                    __m128d a = _mm_set_sd(classifier->alpha[0]);
+                    __m128d b = _mm_set_sd(classifier->alpha[1]);
+                    double _sum = calc_sum(node->feature.rect[0],p_offset) * node->feature.rect[0].weight;
+                    _sum += calc_sum(node->feature.rect[1],p_offset) * node->feature.rect[1].weight;
+                    if( node->feature.rect[2].p0 )
+                        _sum += calc_sum(node->feature.rect[2],p_offset) * node->feature.rect[2].weight;
+                    __m128d sum = _mm_set_sd(_sum);
+                    
+                    t = _mm_cmpgt_sd(t, sum);
+                    stage_sum = _mm_add_sd(stage_sum, _mm_blendv_pd(b, a, t));
+#endif
                 }
             }
 
-            if( stage_sum < cascade->stage_classifier[i].threshold - 0.0001 )
-            {
-                result = -i;
-                EXIT;
-            }
+#ifndef CV_HAAR_USE_SSE
+            if( stage_sum < cascade->stage_classifier[i].threshold )
+#else
+            __m128d i_threshold = _mm_set_sd(cascade->stage_classifier[i].threshold);
+            if( _mm_comilt_sd(stage_sum, i_threshold) )
+#endif
+                return -i;
         }
     }
     else
@@ -708,313 +805,471 @@ cvRunHaarClassifierCascade( CvHaarClassifierCascade* _cascade,
                     variance_norm_factor, p_offset );
             }
 
-            if( stage_sum < cascade->stage_classifier[i].threshold - 0.0001 )
-            {
-                result = -i;
-                EXIT;
-            }
+            if( stage_sum < cascade->stage_classifier[i].threshold )
+                return -i;
         }
     }
 
-    result = 1;
+    return 1;
+}
 
-    __END__;
 
-    return result;
-}
+namespace cv
+{
 
+struct HaarDetectObjects_ScaleImage_Invoker
+{
+    HaarDetectObjects_ScaleImage_Invoker( const CvHaarClassifierCascade* _cascade,
+                                          int _stripSize, double _factor,
+                                          const Mat& _sum1, const Mat& _sqsum1, Mat* _norm1,
+                                          Mat* _mask1, Rect _equRect, ConcurrentRectVector& _vec )
+    {
+        cascade = _cascade;
+        stripSize = _stripSize;
+        factor = _factor;
+        sum1 = _sum1;
+        sqsum1 = _sqsum1;
+        norm1 = _norm1;
+        mask1 = _mask1;
+        equRect = _equRect;
+        vec = &_vec;
+    }
+    
+    void operator()( const BlockedRange& range ) const
+    {
+        Size winSize0 = cascade->orig_window_size;
+        Size winSize(cvRound(winSize0.width*factor), cvRound(winSize0.height*factor));
+        int y1 = range.begin()*stripSize, y2 = min(range.end()*stripSize, sum1.rows - 1 - winSize0.height);
+        Size ssz(sum1.cols - 1 - winSize0.width, y2 - y1);
+        int x, y, ystep = factor > 2 ? 1 : 2;
+        
+    #ifdef HAVE_IPP
+        if( cascade->hid_cascade->ipp_stages )
+        {
+            IppiRect iequRect = {equRect.x, equRect.y, equRect.width, equRect.height};
+            ippiRectStdDev_32f_C1R(sum1.ptr<float>(y1), sum1.step,
+                                   sqsum1.ptr<double>(y1), sqsum1.step,
+                                   norm1->ptr<float>(y1), norm1->step,
+                                   ippiSize(ssz.width, ssz.height), iequRect );
+            
+            int positive = (ssz.width/ystep)*((ssz.height + ystep-1)/ystep);
 
-static int is_equal( const void* _r1, const void* _r2, void* )
+            if( ystep == 1 )
+                (*mask1) = Scalar::all(1);
+            else
+                for( y = y1; y < y2; y++ )
+                {
+                    uchar* mask1row = mask1->ptr(y);
+                    memset( mask1row, 0, ssz.width );
+                    
+                    if( y % ystep == 0 )
+                        for( x = 0; x < ssz.width; x += ystep )
+                            mask1row[x] = (uchar)1;
+                }
+            
+            for( int j = 0; j < cascade->count; j++ )
+            {
+                if( ippiApplyHaarClassifier_32f_C1R(
+                            sum1.ptr<float>(y1), sum1.step,
+                            norm1->ptr<float>(y1), norm1->step,
+                            mask1->ptr<uchar>(y1), mask1->step,
+                            ippiSize(ssz.width, ssz.height), &positive,
+                            cascade->hid_cascade->stage_classifier[j].threshold,
+                            (IppiHaarClassifier_32f*)cascade->hid_cascade->ipp_stages[j]) < 0 )
+                    positive = 0;
+                if( positive <= 0 )
+                    break;
+            }
+            
+            if( positive > 0 )
+                for( y = y1; y < y2; y += ystep )
+                {
+                    uchar* mask1row = mask1->ptr(y);
+                    for( x = 0; x < ssz.width; x += ystep )
+                        if( mask1row[x] != 0 )
+                        {
+                            vec->push_back(Rect(cvRound(x*factor), cvRound(y*factor),
+                                                winSize.width, winSize.height));
+                            if( --positive == 0 )
+                                break;
+                        }
+                    if( positive == 0 )
+                        break;
+                }
+        }
+        else
+#endif
+            for( y = y1; y < y2; y += ystep )
+                for( x = 0; x < ssz.width; x += ystep )
+                {
+                    if( cvRunHaarClassifierCascade( cascade, cvPoint(x,y), 0 ) > 0 )
+                        vec->push_back(Rect(cvRound(x*factor), cvRound(y*factor),
+                                            winSize.width, winSize.height)); 
+                }
+    }
+    
+    const CvHaarClassifierCascade* cascade;
+    int stripSize;
+    double factor;
+    Mat sum1, sqsum1, *norm1, *mask1;
+    Rect equRect;
+    ConcurrentRectVector* vec;
+};
+    
+
+struct HaarDetectObjects_ScaleCascade_Invoker
 {
-    const CvRect* r1 = (const CvRect*)_r1;
-    const CvRect* r2 = (const CvRect*)_r2;
-    int distance = cvRound(r1->width*0.2);
-
-    return r2->x <= r1->x + distance &&
-           r2->x >= r1->x - distance &&
-           r2->y <= r1->y + distance &&
-           r2->y >= r1->y - distance &&
-           r2->width <= cvRound( r1->width * 1.2 ) &&
-           cvRound( r2->width * 1.2 ) >= r1->width;
+    HaarDetectObjects_ScaleCascade_Invoker( const CvHaarClassifierCascade* _cascade,
+                                            Size _winsize, const Range& _xrange, double _ystep,
+                                            size_t _sumstep, const int** _p, const int** _pq,
+                                            ConcurrentRectVector& _vec )
+    {
+        cascade = _cascade;
+        winsize = _winsize;
+        xrange = _xrange;
+        ystep = _ystep;
+        sumstep = _sumstep;
+        p = _p; pq = _pq;
+        vec = &_vec;
+    }
+    
+    void operator()( const BlockedRange& range ) const
+    {
+        int iy, startY = range.begin(), endY = range.end();
+        const int *p0 = p[0], *p1 = p[1], *p2 = p[2], *p3 = p[3];
+        const int *pq0 = pq[0], *pq1 = pq[1], *pq2 = pq[2], *pq3 = pq[3];
+        bool doCannyPruning = p0 != 0;
+        int sstep = sumstep/sizeof(p0[0]);
+        
+        for( iy = startY; iy < endY; iy++ )
+        {
+            int ix, y = cvRound(iy*ystep), ixstep = 1;
+            for( ix = xrange.start; ix < xrange.end; ix += ixstep )
+            {
+                int x = cvRound(ix*ystep); // it should really be ystep, not ixstep
+                
+                if( doCannyPruning )
+                {
+                    int offset = y*sstep + x;
+                    int s = p0[offset] - p1[offset] - p2[offset] + p3[offset];
+                    int sq = pq0[offset] - pq1[offset] - pq2[offset] + pq3[offset];
+                    if( s < 100 || sq < 20 )
+                    {
+                        ixstep = 2;
+                        continue;
+                    }
+                }
+                
+                int result = cvRunHaarClassifierCascade( cascade, cvPoint(x, y), 0 );
+                if( result > 0 )
+                    vec->push_back(Rect(x, y, winsize.width, winsize.height));
+                ixstep = result != 0 ? 1 : 2;
+            }
+        }
+    }
+    
+    const CvHaarClassifierCascade* cascade;
+    double ystep;
+    size_t sumstep;
+    Size winsize;
+    Range xrange;
+    const int** p;
+    const int** pq;
+    ConcurrentRectVector* vec;
+};
+    
+    
 }
-
+    
 
 CV_IMPL CvSeq*
 cvHaarDetectObjects( const CvArr* _img,
                      CvHaarClassifierCascade* cascade,
-                     CvMemStorage* storage, double scale_factor,
-                     int min_neighbors, int flags, CvSize min_size )
+                     CvMemStorage* storage, double scaleFactor,
+                     int minNeighbors, int flags, CvSize minSize )
 {
-    int split_stage = 2;
+    const double GROUP_EPS = 0.2;
     CvMat stub, *img = (CvMat*)_img;
-    CvMat *temp = 0, *sum = 0, *tilted = 0, *sqsum = 0, *sumcanny = 0;
-    CvSeq* seq = 0;
-    CvSeq* seq2 = 0;
-    CvSeq* idx_seq = 0;
+    cv::Ptr<CvMat> temp, sum, tilted, sqsum, normImg, sumcanny, imgSmall;
     CvSeq* result_seq = 0;
-    CvMemStorage* temp_storage = 0;
-    CvAvgComp* comps = 0;
-    
-    CV_FUNCNAME( "cvHaarDetectObjects" );
-
-    __BEGIN__;
+    cv::Ptr<CvMemStorage> temp_storage;
 
+    cv::ConcurrentRectVector allCandidates;
+    std::vector<cv::Rect> rectList;
+    std::vector<int> rweights;
     double factor;
-    int i, npass = 2, coi;
-    int do_canny_pruning = flags & CV_HAAR_DO_CANNY_PRUNING;
+    int coi;
+    bool doCannyPruning = (flags & CV_HAAR_DO_CANNY_PRUNING) != 0;
+    bool findBiggestObject = (flags & CV_HAAR_FIND_BIGGEST_OBJECT) != 0;
+    bool roughSearch = (flags & CV_HAAR_DO_ROUGH_SEARCH) != 0;
 
     if( !CV_IS_HAAR_CLASSIFIER(cascade) )
-        CV_ERROR( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier cascade" );
+        CV_Error( !cascade ? CV_StsNullPtr : CV_StsBadArg, "Invalid classifier cascade" );
 
     if( !storage )
-        CV_ERROR( CV_StsNullPtr, "Null storage pointer" );
+        CV_Error( CV_StsNullPtr, "Null storage pointer" );
 
-    CV_CALL( img = cvGetMat( img, &stub, &coi ));
+    img = cvGetMat( img, &stub, &coi );
     if( coi )
-        CV_ERROR( CV_BadCOI, "COI is not supported" );
+        CV_Error( CV_BadCOI, "COI is not supported" );
 
     if( CV_MAT_DEPTH(img->type) != CV_8U )
-        CV_ERROR( CV_StsUnsupportedFormat, "Only 8-bit images are supported" );
+        CV_Error( CV_StsUnsupportedFormat, "Only 8-bit images are supported" );
+    
+    if( scaleFactor <= 1 )
+        CV_Error( CV_StsOutOfRange, "scale factor must be > 1" );
+
+    if( findBiggestObject )
+        flags &= ~CV_HAAR_SCALE_IMAGE;
 
     temp = cvCreateMat( img->rows, img->cols, CV_8UC1 );
     sum = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 );
     sqsum = cvCreateMat( img->rows + 1, img->cols + 1, CV_64FC1 );
-    temp_storage = cvCreateChildMemStorage( storage );
 
     if( !cascade->hid_cascade )
-        CV_CALL( icvCreateHidHaarClassifierCascade(cascade) );
+        icvCreateHidHaarClassifierCascade(cascade);
 
     if( cascade->hid_cascade->has_tilted_features )
         tilted = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 );
 
-    seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvRect), temp_storage );
-    seq2 = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvAvgComp), temp_storage );
     result_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvAvgComp), storage );
 
-    if( min_neighbors == 0 )
-        seq = result_seq;
-
     if( CV_MAT_CN(img->type) > 1 )
     {
         cvCvtColor( img, temp, CV_BGR2GRAY );
         img = temp;
     }
-    
-    cvIntegral( img, sum, sqsum, tilted );
-    
-    if( do_canny_pruning )
+
+    if( findBiggestObject )
+        flags &= ~(CV_HAAR_SCALE_IMAGE|CV_HAAR_DO_CANNY_PRUNING);
+
+    if( flags & CV_HAAR_SCALE_IMAGE )
     {
-        sumcanny = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 );
-        cvCanny( img, temp, 0, 50, 3 );
-        cvIntegral( temp, sumcanny );
+        CvSize winSize0 = cascade->orig_window_size;
+#ifdef HAVE_IPP
+        int use_ipp = cascade->hid_cascade->ipp_stages != 0;
+
+        if( use_ipp )
+            normImg = cvCreateMat( img->rows, img->cols, CV_32FC1 );
+#endif
+        imgSmall = cvCreateMat( img->rows + 1, img->cols + 1, CV_8UC1 );
+
+        for( factor = 1; ; factor *= scaleFactor )
+        {
+            CvSize winSize = { cvRound(winSize0.width*factor),
+                                cvRound(winSize0.height*factor) };
+            CvSize sz = { cvRound( img->cols/factor ), cvRound( img->rows/factor ) };
+            CvSize sz1 = { sz.width - winSize0.width, sz.height - winSize0.height };
+
+            CvRect equRect = { icv_object_win_border, icv_object_win_border,
+                winSize0.width - icv_object_win_border*2,
+                winSize0.height - icv_object_win_border*2 };
+
+            CvMat img1, sum1, sqsum1, norm1, tilted1, mask1;
+            CvMat* _tilted = 0;
+
+            if( sz1.width <= 0 || sz1.height <= 0 )
+                break;
+            if( winSize.width < minSize.width || winSize.height < minSize.height )
+                continue;
+
+            img1 = cvMat( sz.height, sz.width, CV_8UC1, imgSmall->data.ptr );
+            sum1 = cvMat( sz.height+1, sz.width+1, CV_32SC1, sum->data.ptr );
+            sqsum1 = cvMat( sz.height+1, sz.width+1, CV_64FC1, sqsum->data.ptr );
+            if( tilted )
+            {
+                tilted1 = cvMat( sz.height+1, sz.width+1, CV_32SC1, tilted->data.ptr );
+                _tilted = &tilted1;
+            }
+            norm1 = cvMat( sz1.height, sz1.width, CV_32FC1, normImg ? normImg->data.ptr : 0 );
+            mask1 = cvMat( sz1.height, sz1.width, CV_8UC1, temp->data.ptr );
+
+            cvResize( img, &img1, CV_INTER_LINEAR );
+            cvIntegral( &img1, &sum1, &sqsum1, _tilted );
+
+            int ystep = factor > 2 ? 1 : 2;
+        #ifdef HAVE_TBB
+            const int LOCS_PER_THREAD = 1000;
+            int stripCount = ((sz1.width/ystep)*(sz1.height + ystep-1)/ystep + LOCS_PER_THREAD/2)/LOCS_PER_THREAD;
+            stripCount = std::min(std::max(stripCount, 1), 100);
+        #else
+            const int stripCount = 1;
+        #endif
+            
+#ifdef HAVE_IPP
+            if( use_ipp )
+            {
+                cv::Mat fsum(sum1.rows, sum1.cols, CV_32F, sum1.data.ptr, sum1.step);
+                cv::Mat(&sum1).convertTo(fsum, CV_32F, 1, -(1<<24));
+            }
+            else
+#endif
+                cvSetImagesForHaarClassifierCascade( cascade, &sum1, &sqsum1, _tilted, 1. );            
+            
+            cv::Mat _norm1(&norm1), _mask1(&mask1);
+            cv::parallel_for(cv::BlockedRange(0, stripCount),
+                         cv::HaarDetectObjects_ScaleImage_Invoker(cascade,
+                                (((sz1.height + stripCount - 1)/stripCount + ystep-1)/ystep)*ystep,
+                                factor, cv::Mat(&sum1), cv::Mat(&sqsum1), &_norm1, &_mask1,
+                                cv::Rect(equRect), allCandidates));
+        }
     }
-    
-    if( (unsigned)split_stage >= (unsigned)cascade->count ||
-        cascade->hid_cascade->is_tree )
+    else
     {
-        split_stage = cascade->count;
-        npass = 1;
-    }
+        int n_factors = 0;
+        cv::Rect scanROI;
 
-    for( factor = 1; factor*cascade->orig_window_size.width < img->cols - 10 &&
-                     factor*cascade->orig_window_size.height < img->rows - 10;
-         factor *= scale_factor )
-    {
-        const double ystep = MAX( 2, factor );
-        CvSize win_size = { cvRound( cascade->orig_window_size.width * factor ),
-                            cvRound( cascade->orig_window_size.height * factor )};
-        CvRect equ_rect = { 0, 0, 0, 0 };
-        int *p0 = 0, *p1 = 0, *p2 = 0, *p3 = 0;
-        int *pq0 = 0, *pq1 = 0, *pq2 = 0, *pq3 = 0;
-        int pass, stage_offset = 0;
-        int stop_height = cvRound((img->rows - win_size.height) / ystep);
-
-        if( win_size.width < min_size.width || win_size.height < min_size.height )
-            continue;
-
-        cvSetImagesForHaarClassifierCascade( cascade, sum, sqsum, tilted, factor );
-        cvZero( temp );
-
-        if( do_canny_pruning )
+        cvIntegral( img, sum, sqsum, tilted );
+
+        if( doCannyPruning )
         {
-            equ_rect.x = cvRound(win_size.width*0.3);
-            equ_rect.y = cvRound(win_size.height*0.3);
-            equ_rect.width = cvRound(win_size.width*0.7);
-            equ_rect.height = cvRound(win_size.height*0.7);
-
-            p0 = (int*)(sumcanny->data.ptr + equ_rect.y*sumcanny->step) + equ_rect.x;
-            p1 = (int*)(sumcanny->data.ptr + equ_rect.y*sumcanny->step)
-                        + equ_rect.x + equ_rect.width;
-            p2 = (int*)(sumcanny->data.ptr + (equ_rect.y + equ_rect.height)*sumcanny->step) + equ_rect.x;
-            p3 = (int*)(sumcanny->data.ptr + (equ_rect.y + equ_rect.height)*sumcanny->step)
-                        + equ_rect.x + equ_rect.width;
-
-            pq0 = (int*)(sum->data.ptr + equ_rect.y*sum->step) + equ_rect.x;
-            pq1 = (int*)(sum->data.ptr + equ_rect.y*sum->step)
-                        + equ_rect.x + equ_rect.width;
-            pq2 = (int*)(sum->data.ptr + (equ_rect.y + equ_rect.height)*sum->step) + equ_rect.x;
-            pq3 = (int*)(sum->data.ptr + (equ_rect.y + equ_rect.height)*sum->step)
-                        + equ_rect.x + equ_rect.width;
+            sumcanny = cvCreateMat( img->rows + 1, img->cols + 1, CV_32SC1 );
+            cvCanny( img, temp, 0, 50, 3 );
+            cvIntegral( temp, sumcanny );
         }
 
-        cascade->hid_cascade->count = split_stage;
+        for( n_factors = 0, factor = 1;
+             factor*cascade->orig_window_size.width < img->cols - 10 &&
+             factor*cascade->orig_window_size.height < img->rows - 10;
+             n_factors++, factor *= scaleFactor )
+            ;
 
-        for( pass = 0; pass < npass; pass++ )
+        if( findBiggestObject )
         {
-#ifdef _OPENMP
-    #pragma omp parallel for shared(hid_cascade, stop_height, seq, ystep, temp, size, win_size, pass, npass, sum, p0, p1, p2 ,p3, pq0, pq1, pq2, pq3, stage_offset) 
-#endif // _OPENMP
+            scaleFactor = 1./scaleFactor;
+            factor *= scaleFactor;
+        }
+        else
+            factor = 1;
 
-            for( int _iy = 0; _iy < stop_height; _iy++ )
+        for( ; n_factors-- > 0; factor *= scaleFactor )
+        {
+            const double ystep = std::max( 2., factor );
+            CvSize winSize = { cvRound( cascade->orig_window_size.width * factor ),
+                                cvRound( cascade->orig_window_size.height * factor )};
+            CvRect equRect = { 0, 0, 0, 0 };
+            int *p[4] = {0,0,0,0};
+            int *pq[4] = {0,0,0,0};
+            int startX = 0, startY = 0;
+            int endX = cvRound((img->cols - winSize.width) / ystep);
+            int endY = cvRound((img->rows - winSize.height) / ystep);
+
+            if( winSize.width < minSize.width || winSize.height < minSize.height )
             {
-                int iy = cvRound(_iy*ystep);
-                int _ix, _xstep = 1;
-                int stop_width = cvRound((img->cols - win_size.width) / ystep);
-                uchar* mask_row = temp->data.ptr + temp->step * iy;
+                if( findBiggestObject )
+                    break;
+                continue;
+            }
 
-                for( _ix = 0; _ix < stop_width; _ix += _xstep )
-                {
-                    int ix = cvRound(_ix*ystep); // it really should be ystep
-                    
-                    if( pass == 0 )
-                    {
-                        int result;
-                        _xstep = 2;
+            cvSetImagesForHaarClassifierCascade( cascade, sum, sqsum, tilted, factor );
+            cvZero( temp );
 
-                        if( do_canny_pruning )
-                        {
-                            int offset;
-                            int s, sq;
-                        
-                            offset = iy*(sum->step/sizeof(p0[0])) + ix;
-                            s = p0[offset] - p1[offset] - p2[offset] + p3[offset];
-                            sq = pq0[offset] - pq1[offset] - pq2[offset] + pq3[offset];
-                            if( s < 100 || sq < 20 )
-                                continue;
-                        }
+            if( doCannyPruning )
+            {
+                equRect.x = cvRound(winSize.width*0.15);
+                equRect.y = cvRound(winSize.height*0.15);
+                equRect.width = cvRound(winSize.width*0.7);
+                equRect.height = cvRound(winSize.height*0.7);
+
+                p[0] = (int*)(sumcanny->data.ptr + equRect.y*sumcanny->step) + equRect.x;
+                p[1] = (int*)(sumcanny->data.ptr + equRect.y*sumcanny->step)
+                            + equRect.x + equRect.width;
+                p[2] = (int*)(sumcanny->data.ptr + (equRect.y + equRect.height)*sumcanny->step) + equRect.x;
+                p[3] = (int*)(sumcanny->data.ptr + (equRect.y + equRect.height)*sumcanny->step)
+                            + equRect.x + equRect.width;
+
+                pq[0] = (int*)(sum->data.ptr + equRect.y*sum->step) + equRect.x;
+                pq[1] = (int*)(sum->data.ptr + equRect.y*sum->step)
+                            + equRect.x + equRect.width;
+                pq[2] = (int*)(sum->data.ptr + (equRect.y + equRect.height)*sum->step) + equRect.x;
+                pq[3] = (int*)(sum->data.ptr + (equRect.y + equRect.height)*sum->step)
+                            + equRect.x + equRect.width;
+            }
 
-                        result = cvRunHaarClassifierCascade( cascade, cvPoint(ix,iy), 0 );
-#ifdef _OPENMP
-#pragma omp critical
-#endif
-                        if( result > 0 )
-                        {
-                            if( pass < npass - 1 )
-                                mask_row[ix] = 1;
-                            else
-                            {
-                                CvRect rect = cvRect(ix,iy,win_size.width,win_size.height);
-                                cvSeqPush( seq, &rect );
-                            }
-                        }
-                        if( result < 0 )
-                            _xstep = 1;
-                    }
-                    else if( mask_row[ix] )
+            if( scanROI.area() > 0 )
+            {
+                //adjust start_height and stop_height
+                startY = cvRound(scanROI.y / ystep);
+                endY = cvRound((scanROI.y + scanROI.height - winSize.height) / ystep);
+
+                startX = cvRound(scanROI.x / ystep);
+                endX = cvRound((scanROI.x + scanROI.width - winSize.width) / ystep);
+            }
+
+            cv::parallel_for(cv::BlockedRange(startY, endY),
+                cv::HaarDetectObjects_ScaleCascade_Invoker(cascade, winSize, cv::Range(startX, endX),
+                                                           ystep, sum->step, (const int**)p,
+                                                           (const int**)pq, allCandidates ));
+
+            if( findBiggestObject && !allCandidates.empty() && scanROI.area() == 0 )
+            {
+                rectList.resize(allCandidates.size());
+                std::copy(allCandidates.begin(), allCandidates.end(), rectList.begin());
+                
+                groupRectangles(rectList, std::max(minNeighbors, 1), GROUP_EPS);
+                
+                if( !rectList.empty() )
+                {
+                    size_t i, sz = rectList.size();
+                    cv::Rect maxRect;
+                    
+                    for( i = 0; i < sz; i++ )
                     {
-                        int result = cvRunHaarClassifierCascade( cascade, cvPoint(ix,iy),
-                                                                 stage_offset );
-#ifdef _OPENMP
-#pragma omp critical
-#endif
-                        if( result > 0 )
-                        {
-                            if( pass == npass - 1 )
-                            {
-                                CvRect rect = cvRect(ix,iy,win_size.width,win_size.height);
-                                cvSeqPush( seq, &rect );
-                            }
-                        }
-                        else
-                            mask_row[ix] = 0;
+                        if( rectList[i].area() > maxRect.area() )
+                            maxRect = rectList[i];
                     }
+                    
+                    allCandidates.push_back(maxRect);
+                    
+                    scanROI = maxRect;
+                    int dx = cvRound(maxRect.width*GROUP_EPS);
+                    int dy = cvRound(maxRect.height*GROUP_EPS);
+                    scanROI.x = std::max(scanROI.x - dx, 0);
+                    scanROI.y = std::max(scanROI.y - dy, 0);
+                    scanROI.width = std::min(scanROI.width + dx*2, img->cols-1-scanROI.x);
+                    scanROI.height = std::min(scanROI.height + dy*2, img->rows-1-scanROI.y);
+                
+                    double minScale = roughSearch ? 0.6 : 0.4;
+                    minSize.width = cvRound(maxRect.width*minScale);
+                    minSize.height = cvRound(maxRect.height*minScale);
                 }
             }
-            stage_offset = cascade->hid_cascade->count;
-            cascade->hid_cascade->count = cascade->count;
         }
     }
 
-    if( min_neighbors != 0 )
+    rectList.resize(allCandidates.size());
+    if(!allCandidates.empty())
+        std::copy(allCandidates.begin(), allCandidates.end(), rectList.begin());
+    
+    if( minNeighbors != 0 || findBiggestObject )
+        groupRectangles(rectList, rweights, std::max(minNeighbors, 1), GROUP_EPS);
+        
+    if( findBiggestObject && rectList.size() )
     {
-        // group retrieved rectangles in order to filter out noise 
-        int ncomp = cvSeqPartition( seq, 0, &idx_seq, is_equal, 0 );
-        CV_CALL( comps = (CvAvgComp*)cvAlloc( (ncomp+1)*sizeof(comps[0])));
-        memset( comps, 0, (ncomp+1)*sizeof(comps[0]));
-
-        // count number of neighbors
-        for( i = 0; i < seq->total; i++ )
-        {
-            CvRect r1 = *(CvRect*)cvGetSeqElem( seq, i );
-            int idx = *(int*)cvGetSeqElem( idx_seq, i );
-            assert( (unsigned)idx < (unsigned)ncomp );
-
-            comps[idx].neighbors++;
-             
-            comps[idx].rect.x += r1.x;
-            comps[idx].rect.y += r1.y;
-            comps[idx].rect.width += r1.width;
-            comps[idx].rect.height += r1.height;
-        }
-
-        // calculate average bounding box
-        for( i = 0; i < ncomp; i++ )
+        CvAvgComp result_comp = {{0,0,0,0},0};
+        
+        for( size_t i = 0; i < rectList.size(); i++ )
         {
-            int n = comps[i].neighbors;
-            if( n >= min_neighbors )
+            cv::Rect r = rectList[i];
+            if( r.area() > cv::Rect(result_comp.rect).area() )
             {
-                CvAvgComp comp;
-                comp.rect.x = (comps[i].rect.x*2 + n)/(2*n);
-                comp.rect.y = (comps[i].rect.y*2 + n)/(2*n);
-                comp.rect.width = (comps[i].rect.width*2 + n)/(2*n);
-                comp.rect.height = (comps[i].rect.height*2 + n)/(2*n);
-                comp.neighbors = comps[i].neighbors;
-
-                cvSeqPush( seq2, &comp );
+                result_comp.rect = r;
+                result_comp.neighbors = rweights[i];
             }
         }
-
-        // filter out small face rectangles inside large face rectangles
-        for( i = 0; i < seq2->total; i++ )
+        cvSeqPush( result_seq, &result_comp );
+    }
+    else
+    {
+        for( size_t i = 0; i < rectList.size(); i++ )
         {
-            CvAvgComp r1 = *(CvAvgComp*)cvGetSeqElem( seq2, i );
-            int j, flag = 1;
-
-            for( j = 0; j < seq2->total; j++ )
-            {
-                CvAvgComp r2 = *(CvAvgComp*)cvGetSeqElem( seq2, j );
-                int distance = cvRound( r2.rect.width * 0.2 );
-            
-                if( i != j &&
-                    r1.rect.x >= r2.rect.x - distance &&
-                    r1.rect.y >= r2.rect.y - distance &&
-                    r1.rect.x + r1.rect.width <= r2.rect.x + r2.rect.width + distance &&
-                    r1.rect.y + r1.rect.height <= r2.rect.y + r2.rect.height + distance &&
-                    (r2.neighbors > MAX( 3, r1.neighbors ) || r1.neighbors < 3) )
-                {
-                    flag = 0;
-                    break;
-                }
-            }
-
-            if( flag )
-            {
-                cvSeqPush( result_seq, &r1 );
-                /* cvSeqPush( result_seq, &r1.rect ); */
-            }
+            CvAvgComp c;
+            c.rect = rectList[i];
+            c.neighbors = rweights[i];
+            cvSeqPush( result_seq, &c );
         }
     }
 
-    __END__;
-
-    cvReleaseMemStorage( &temp_storage );
-    cvReleaseMat( &sum );
-    cvReleaseMat( &sqsum );
-    cvReleaseMat( &tilted );
-    cvReleaseMat( &temp );
-    cvReleaseMat( &sumcanny );
-    cvFree( (void**)&comps );
-
     return result_seq;
 }
 
@@ -1039,7 +1294,7 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
 
         sscanf( stage, "%d%n", &count, &dl );
         stage += dl;
-        
+
         assert( count > 0 );
         cascade->stage_classifier[i].count = count;
         cascade->stage_classifier[i].classifier =
@@ -1050,11 +1305,11 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
             CvHaarClassifier* classifier = cascade->stage_classifier[i].classifier + j;
             int k, rects = 0;
             char str[100];
-            
+
             sscanf( stage, "%d%n", &classifier->count, &dl );
             stage += dl;
 
-            classifier->haar_feature = (CvHaarFeature*) cvAlloc( 
+            classifier->haar_feature = (CvHaarFeature*) cvAlloc(
                 classifier->count * ( sizeof( *classifier->haar_feature ) +
                                       sizeof( *classifier->threshold ) +
                                       sizeof( *classifier->left ) +
@@ -1064,7 +1319,7 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
             classifier->left = (int*) (classifier->threshold + classifier->count);
             classifier->right = (int*) (classifier->left + classifier->count);
             classifier->alpha = (float*) (classifier->right + classifier->count);
-            
+
             for( l = 0; l < classifier->count; l++ )
             {
                 sscanf( stage, "%d%n", &rects, &dl );
@@ -1084,16 +1339,16 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
                 }
                 sscanf( stage, "%s%n", str, &dl );
                 stage += dl;
-            
+
                 classifier->haar_feature[l].tilted = strncmp( str, "tilted", 6 ) == 0;
-            
+
                 for( k = rects; k < CV_HAAR_FEATURE_MAX; k++ )
                 {
                     memset( classifier->haar_feature[l].rect + k, 0,
                             sizeof(classifier->haar_feature[l].rect[k]) );
                 }
-                
-                sscanf( stage, "%f%d%d%n", &(classifier->threshold[l]), 
+
+                sscanf( stage, "%f%d%d%n", &(classifier->threshold[l]),
                                        &(classifier->left[l]),
                                        &(classifier->right[l]), &dl );
                 stage += dl;
@@ -1104,7 +1359,7 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
                 stage += dl;
             }
         }
-       
+
         sscanf( stage, "%f%n", &threshold, &dl );
         stage += dl;
 
@@ -1138,13 +1393,9 @@ icvLoadCascadeCART( const char** input_cascade, int n, CvSize orig_window_size )
 CV_IMPL CvHaarClassifierCascade*
 cvLoadHaarClassifierCascade( const char* directory, CvSize orig_window_size )
 {
-    const char** input_cascade = 0; 
+    const char** input_cascade = 0;
     CvHaarClassifierCascade *cascade = 0;
 
-    CV_FUNCNAME( "cvLoadHaarClassifierCascade" );
-
-    __BEGIN__;
-
     int i, n;
     const char* slash;
     char name[_MAX_PATH];
@@ -1152,9 +1403,9 @@ cvLoadHaarClassifierCascade( const char* directory, CvSize orig_window_size )
     char* ptr = 0;
 
     if( !directory )
-        CV_ERROR( CV_StsNullPtr, "Null path is passed" );
+        CV_Error( CV_StsNullPtr, "Null path is passed" );
 
-    n = strlen(directory)-1;
+    n = (int)strlen(directory)-1;
     slash = directory[n] == '\\' || directory[n] == '/' ? "" : "/";
 
     /* try to read the classifier from directory */
@@ -1170,15 +1421,13 @@ cvLoadHaarClassifierCascade( const char* directory, CvSize orig_window_size )
     }
 
     if( n == 0 && slash[0] )
-    {
-        CV_CALL( cascade = (CvHaarClassifierCascade*)cvLoad( directory ));
-        EXIT;
-    }
-    else if( n == 0 )
-        CV_ERROR( CV_StsBadArg, "Invalid path" );
-    
+        return (CvHaarClassifierCascade*)cvLoad( directory );
+
+    if( n == 0 )
+        CV_Error( CV_StsBadArg, "Invalid path" );
+
     size += (n+1)*sizeof(char*);
-    CV_CALL( input_cascade = (const char**)cvAlloc( size ));
+    input_cascade = (const char**)cvAlloc( size );
     ptr = (char*)(input_cascade + n + 1);
 
     for( i = 0; i < n; i++ )
@@ -1186,7 +1435,7 @@ cvLoadHaarClassifierCascade( const char* directory, CvSize orig_window_size )
         sprintf( name, "%s/%d/AdaBoostCARTHaarClassifier.txt", directory, i );
         FILE* f = fopen( name, "rb" );
         if( !f )
-            CV_ERROR( CV_StsError, "" );
+            CV_Error( CV_StsError, "" );
         fseek( f, 0, SEEK_END );
         size = ftell( f );
         fseek( f, 0, SEEK_SET );
@@ -1200,37 +1449,29 @@ cvLoadHaarClassifierCascade( const char* directory, CvSize orig_window_size )
     input_cascade[n] = 0;
     cascade = icvLoadCascadeCART( input_cascade, n, orig_window_size );
 
-    __END__;
-
     if( input_cascade )
-        cvFree( (void**)&input_cascade );
-
-    if( cvGetErrStatus() < 0 )
-        cvReleaseHaarClassifierCascade( &cascade );
+        cvFree( &input_cascade );
 
     return cascade;
 }
 
 
 CV_IMPL void
-cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** cascade )
+cvReleaseHaarClassifierCascade( CvHaarClassifierCascade** _cascade )
 {
-    if( cascade && *cascade )
+    if( _cascade && *_cascade )
     {
-        int i;
-        int j;
+        int i, j;
+        CvHaarClassifierCascade* cascade = *_cascade;
 
-        for( i = 0; i < cascade[0]->count; i++ )
+        for( i = 0; i < cascade->count; i++ )
         {
-            for( j = 0; j < cascade[0]->stage_classifier[i].count; j++ )
-            {
-                cvFree( (void**)
-                    &(cascade[0]->stage_classifier[i].classifier[j].haar_feature) );
-            }
-            cvFree( (void**) &(cascade[0]->stage_classifier[i].classifier) );
+            for( j = 0; j < cascade->stage_classifier[i].count; j++ )
+                cvFree( &cascade->stage_classifier[i].classifier[j].haar_feature );
+            cvFree( &cascade->stage_classifier[i].classifier );
         }
-        cvFree( (void**)&(cascade[0]->hid_cascade) );
-        cvFree( (void**)cascade );
+        icvReleaseHidHaarClassifierCascade( &cascade->hid_cascade );
+        cvFree( _cascade );
     }
 }
 
@@ -1267,10 +1508,6 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
 {
     CvHaarClassifierCascade* cascade = NULL;
 
-    CV_FUNCNAME( "cvReadHaarClassifier" );
-
-    __BEGIN__;
-
     char buf[256];
     CvFileNode* seq_fn = NULL; /* sequence */
     CvFileNode* fn = NULL;
@@ -1280,27 +1517,27 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
     int i, j, k, l;
     int parent, next;
 
-    CV_CALL( stages_fn = cvGetFileNodeByName( fs, node, ICV_HAAR_STAGES_NAME ) );
+    stages_fn = cvGetFileNodeByName( fs, node, ICV_HAAR_STAGES_NAME );
     if( !stages_fn || !CV_NODE_IS_SEQ( stages_fn->tag) )
-        CV_ERROR( CV_StsError, "Invalid stages node" );
+        CV_Error( CV_StsError, "Invalid stages node" );
 
     n = stages_fn->data.seq->total;
-    CV_CALL( cascade = icvCreateHaarClassifierCascade(n) );
+    cascade = icvCreateHaarClassifierCascade(n);
 
     /* read size */
-    CV_CALL( seq_fn = cvGetFileNodeByName( fs, node, ICV_HAAR_SIZE_NAME ) );
+    seq_fn = cvGetFileNodeByName( fs, node, ICV_HAAR_SIZE_NAME );
     if( !seq_fn || !CV_NODE_IS_SEQ( seq_fn->tag ) || seq_fn->data.seq->total != 2 )
-        CV_ERROR( CV_StsError, "size node is not a valid sequence." );
-    CV_CALL( fn = (CvFileNode*) cvGetSeqElem( seq_fn->data.seq, 0 ) );
+        CV_Error( CV_StsError, "size node is not a valid sequence." );
+    fn = (CvFileNode*) cvGetSeqElem( seq_fn->data.seq, 0 );
     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= 0 )
-        CV_ERROR( CV_StsError, "Invalid size node: width must be positive integer" );
+        CV_Error( CV_StsError, "Invalid size node: width must be positive integer" );
     cascade->orig_window_size.width = fn->data.i;
-    CV_CALL( fn = (CvFileNode*) cvGetSeqElem( seq_fn->data.seq, 1 ) );
+    fn = (CvFileNode*) cvGetSeqElem( seq_fn->data.seq, 1 );
     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= 0 )
-        CV_ERROR( CV_StsError, "Invalid size node: height must be positive integer" );
+        CV_Error( CV_StsError, "Invalid size node: height must be positive integer" );
     cascade->orig_window_size.height = fn->data.i;
 
-    CV_CALL( cvStartReadSeq( stages_fn->data.seq, &stages_reader ) );
+    cvStartReadSeq( stages_fn->data.seq, &stages_reader );
     for( i = 0; i < n; ++i )
     {
         CvFileNode* stage_fn;
@@ -1311,27 +1548,27 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
         if( !CV_NODE_IS_MAP( stage_fn->tag ) )
         {
             sprintf( buf, "Invalid stage %d", i );
-            CV_ERROR( CV_StsError, buf );
+            CV_Error( CV_StsError, buf );
         }
 
-        CV_CALL( trees_fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_TREES_NAME ) );
+        trees_fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_TREES_NAME );
         if( !trees_fn || !CV_NODE_IS_SEQ( trees_fn->tag )
             || trees_fn->data.seq->total <= 0 )
         {
             sprintf( buf, "Trees node is not a valid sequence. (stage %d)", i );
-            CV_ERROR( CV_StsError, buf );
+            CV_Error( CV_StsError, buf );
         }
 
-        CV_CALL( cascade->stage_classifier[i].classifier =
+        cascade->stage_classifier[i].classifier =
             (CvHaarClassifier*) cvAlloc( trees_fn->data.seq->total
-                * sizeof( cascade->stage_classifier[i].classifier[0] ) ) );
+                * sizeof( cascade->stage_classifier[i].classifier[0] ) );
         for( j = 0; j < trees_fn->data.seq->total; ++j )
         {
             cascade->stage_classifier[i].classifier[j].haar_feature = NULL;
         }
         cascade->stage_classifier[i].count = trees_fn->data.seq->total;
 
-        CV_CALL( cvStartReadSeq( trees_fn->data.seq, &trees_reader ) );
+        cvStartReadSeq( trees_fn->data.seq, &trees_reader );
         for( j = 0; j < trees_fn->data.seq->total; ++j )
         {
             CvFileNode* tree_fn;
@@ -1345,22 +1582,22 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
             {
                 sprintf( buf, "Tree node is not a valid sequence."
                          " (stage %d, tree %d)", i, j );
-                CV_ERROR( CV_StsError, buf );
+                CV_Error( CV_StsError, buf );
             }
 
             classifier->count = tree_fn->data.seq->total;
-            CV_CALL( classifier->haar_feature = (CvHaarFeature*) cvAlloc( 
+            classifier->haar_feature = (CvHaarFeature*) cvAlloc(
                 classifier->count * ( sizeof( *classifier->haar_feature ) +
                                       sizeof( *classifier->threshold ) +
                                       sizeof( *classifier->left ) +
                                       sizeof( *classifier->right ) ) +
-                (classifier->count + 1) * sizeof( *classifier->alpha ) ) );
+                (classifier->count + 1) * sizeof( *classifier->alpha ) );
             classifier->threshold = (float*) (classifier->haar_feature+classifier->count);
             classifier->left = (int*) (classifier->threshold + classifier->count);
             classifier->right = (int*) (classifier->left + classifier->count);
             classifier->alpha = (float*) (classifier->right + classifier->count);
 
-            CV_CALL( cvStartReadSeq( tree_fn->data.seq, &tree_reader ) );
+            cvStartReadSeq( tree_fn->data.seq, &tree_reader );
             for( k = 0, last_idx = 0; k < tree_fn->data.seq->total; ++k )
             {
                 CvFileNode* node_fn;
@@ -1373,27 +1610,25 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
                 {
                     sprintf( buf, "Tree node %d is not a valid map. (stage %d, tree %d)",
                              k, i, j );
-                    CV_ERROR( CV_StsError, buf );
+                    CV_Error( CV_StsError, buf );
                 }
-                CV_CALL( feature_fn = cvGetFileNodeByName( fs, node_fn,
-                    ICV_HAAR_FEATURE_NAME ) );
+                feature_fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_FEATURE_NAME );
                 if( !feature_fn || !CV_NODE_IS_MAP( feature_fn->tag ) )
                 {
                     sprintf( buf, "Feature node is not a valid map. "
                              "(stage %d, tree %d, node %d)", i, j, k );
-                    CV_ERROR( CV_StsError, buf );
+                    CV_Error( CV_StsError, buf );
                 }
-                CV_CALL( rects_fn = cvGetFileNodeByName( fs, feature_fn,
-                    ICV_HAAR_RECTS_NAME ) );
+                rects_fn = cvGetFileNodeByName( fs, feature_fn, ICV_HAAR_RECTS_NAME );
                 if( !rects_fn || !CV_NODE_IS_SEQ( rects_fn->tag )
                     || rects_fn->data.seq->total < 1
                     || rects_fn->data.seq->total > CV_HAAR_FEATURE_MAX )
                 {
                     sprintf( buf, "Rects node is not a valid sequence. "
                              "(stage %d, tree %d, node %d)", i, j, k );
-                    CV_ERROR( CV_StsError, buf );
+                    CV_Error( CV_StsError, buf );
                 }
-                CV_CALL( cvStartReadSeq( rects_fn->data.seq, &rects_reader ) );
+                cvStartReadSeq( rects_fn->data.seq, &rects_reader );
                 for( l = 0; l < rects_fn->data.seq->total; ++l )
                 {
                     CvFileNode* rect_fn;
@@ -1404,15 +1639,15 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
                     {
                         sprintf( buf, "Rect %d is not a valid sequence. "
                                  "(stage %d, tree %d, node %d)", l, i, j, k );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
-                    
+
                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 0 );
                     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i < 0 )
                     {
                         sprintf( buf, "x coordinate must be non-negative integer. "
                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     r.x = fn->data.i;
                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 1 );
@@ -1420,7 +1655,7 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
                     {
                         sprintf( buf, "y coordinate must be non-negative integer. "
                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     r.y = fn->data.i;
                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 2 );
@@ -1430,7 +1665,7 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
                         sprintf( buf, "width must be positive integer and "
                                  "(x + width) must not exceed window width. "
                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     r.width = fn->data.i;
                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 3 );
@@ -1440,7 +1675,7 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
                         sprintf( buf, "height must be positive integer and "
                                  "(y + height) must not exceed window height. "
                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     r.height = fn->data.i;
                     fn = CV_SEQ_ELEM( rect_fn->data.seq, CvFileNode, 4 );
@@ -1448,7 +1683,7 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
                     {
                         sprintf( buf, "weight must be real number. "
                                  "(stage %d, tree %d, node %d, rect %d)", i, j, k, l );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
 
                     classifier->haar_feature[k].rect[l].weight = (float) fn->data.f;
@@ -1462,23 +1697,23 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
                     classifier->haar_feature[k].rect[l].r = cvRect( 0, 0, 0, 0 );
                 }
 
-                CV_CALL( fn = cvGetFileNodeByName( fs, feature_fn, ICV_HAAR_TILTED_NAME));
+                fn = cvGetFileNodeByName( fs, feature_fn, ICV_HAAR_TILTED_NAME);
                 if( !fn || !CV_NODE_IS_INT( fn->tag ) )
                 {
                     sprintf( buf, "tilted must be 0 or 1. "
                              "(stage %d, tree %d, node %d)", i, j, k );
-                    CV_ERROR( CV_StsError, buf );
+                    CV_Error( CV_StsError, buf );
                 }
                 classifier->haar_feature[k].tilted = ( fn->data.i != 0 );
-                CV_CALL( fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_THRESHOLD_NAME));
+                fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_THRESHOLD_NAME);
                 if( !fn || !CV_NODE_IS_REAL( fn->tag ) )
                 {
                     sprintf( buf, "threshold must be real number. "
                              "(stage %d, tree %d, node %d)", i, j, k );
-                    CV_ERROR( CV_StsError, buf );
+                    CV_Error( CV_StsError, buf );
                 }
                 classifier->threshold[k] = (float) fn->data.f;
-                CV_CALL( fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_LEFT_NODE_NAME));
+                fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_LEFT_NODE_NAME);
                 if( fn )
                 {
                     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= k
@@ -1486,38 +1721,37 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
                     {
                         sprintf( buf, "left node must be valid node number. "
                                  "(stage %d, tree %d, node %d)", i, j, k );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     /* left node */
                     classifier->left[k] = fn->data.i;
                 }
                 else
                 {
-                    CV_CALL( fn = cvGetFileNodeByName( fs, node_fn,
-                        ICV_HAAR_LEFT_VAL_NAME ) );
+                    fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_LEFT_VAL_NAME );
                     if( !fn )
                     {
                         sprintf( buf, "left node or left value must be specified. "
                                  "(stage %d, tree %d, node %d)", i, j, k );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     if( !CV_NODE_IS_REAL( fn->tag ) )
                     {
                         sprintf( buf, "left value must be real number. "
                                  "(stage %d, tree %d, node %d)", i, j, k );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     /* left value */
                     if( last_idx >= classifier->count + 1 )
                     {
                         sprintf( buf, "Tree structure is broken: too many values. "
                                  "(stage %d, tree %d, node %d)", i, j, k );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     classifier->left[k] = -last_idx;
                     classifier->alpha[last_idx++] = (float) fn->data.f;
                 }
-                CV_CALL( fn = cvGetFileNodeByName( fs, node_fn,ICV_HAAR_RIGHT_NODE_NAME));
+                fn = cvGetFileNodeByName( fs, node_fn,ICV_HAAR_RIGHT_NODE_NAME);
                 if( fn )
                 {
                     if( !CV_NODE_IS_INT( fn->tag ) || fn->data.i <= k
@@ -1525,33 +1759,32 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
                     {
                         sprintf( buf, "right node must be valid node number. "
                                  "(stage %d, tree %d, node %d)", i, j, k );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     /* right node */
                     classifier->right[k] = fn->data.i;
                 }
                 else
                 {
-                    CV_CALL( fn = cvGetFileNodeByName( fs, node_fn,
-                        ICV_HAAR_RIGHT_VAL_NAME ) );
+                    fn = cvGetFileNodeByName( fs, node_fn, ICV_HAAR_RIGHT_VAL_NAME );
                     if( !fn )
                     {
                         sprintf( buf, "right node or right value must be specified. "
                                  "(stage %d, tree %d, node %d)", i, j, k );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     if( !CV_NODE_IS_REAL( fn->tag ) )
                     {
                         sprintf( buf, "right value must be real number. "
                                  "(stage %d, tree %d, node %d)", i, j, k );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     /* right value */
                     if( last_idx >= classifier->count + 1 )
                     {
                         sprintf( buf, "Tree structure is broken: too many values. "
                                  "(stage %d, tree %d, node %d)", i, j, k );
-                        CV_ERROR( CV_StsError, buf );
+                        CV_Error( CV_StsError, buf );
                     }
                     classifier->right[k] = -last_idx;
                     classifier->alpha[last_idx++] = (float) fn->data.f;
@@ -1563,37 +1796,37 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
             {
                 sprintf( buf, "Tree structure is broken: too few values. "
                          "(stage %d, tree %d)", i, j );
-                CV_ERROR( CV_StsError, buf );
+                CV_Error( CV_StsError, buf );
             }
 
             CV_NEXT_SEQ_ELEM( sizeof( *tree_fn ), trees_reader );
         } /* for each tree */
 
-        CV_CALL( fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_STAGE_THRESHOLD_NAME));
+        fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_STAGE_THRESHOLD_NAME);
         if( !fn || !CV_NODE_IS_REAL( fn->tag ) )
         {
             sprintf( buf, "stage threshold must be real number. (stage %d)", i );
-            CV_ERROR( CV_StsError, buf );
+            CV_Error( CV_StsError, buf );
         }
         cascade->stage_classifier[i].threshold = (float) fn->data.f;
 
         parent = i - 1;
         next = -1;
 
-        CV_CALL( fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_PARENT_NAME ) );
+        fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_PARENT_NAME );
         if( !fn || !CV_NODE_IS_INT( fn->tag )
-            || fn->data.i < -1 || fn->data.i >= cascade->stage_classifier[i].count )
+            || fn->data.i < -1 || fn->data.i >= cascade->count )
         {
             sprintf( buf, "parent must be integer number. (stage %d)", i );
-            CV_ERROR( CV_StsError, buf );
+            CV_Error( CV_StsError, buf );
         }
         parent = fn->data.i;
-        CV_CALL( fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_NEXT_NAME ) );
+        fn = cvGetFileNodeByName( fs, stage_fn, ICV_HAAR_NEXT_NAME );
         if( !fn || !CV_NODE_IS_INT( fn->tag )
-            || fn->data.i < -1 || fn->data.i >= cascade->stage_classifier[i].count )
+            || fn->data.i < -1 || fn->data.i >= cascade->count )
         {
             sprintf( buf, "next must be integer number. (stage %d)", i );
-            CV_ERROR( CV_StsError, buf );
+            CV_Error( CV_StsError, buf );
         }
         next = fn->data.i;
 
@@ -1605,18 +1838,10 @@ icvReadHaarClassifier( CvFileStorage* fs, CvFileNode* node )
         {
             cascade->stage_classifier[parent].child = i;
         }
-        
+
         CV_NEXT_SEQ_ELEM( sizeof( *stage_fn ), stages_reader );
     } /* for each stage */
 
-    __END__;
-
-    if( cvGetErrStatus() < 0 )
-    {
-        cvReleaseHaarClassifierCascade( &cascade );
-        cascade = NULL;
-    }
-
     return cascade;
 }
 
@@ -1624,45 +1849,41 @@ static void
 icvWriteHaarClassifier( CvFileStorage* fs, const char* name, const void* struct_ptr,
                         CvAttrList attributes )
 {
-    CV_FUNCNAME( "cvWriteHaarClassifier" );
-
-    __BEGIN__;
-
     int i, j, k, l;
     char buf[256];
     const CvHaarClassifierCascade* cascade = (const CvHaarClassifierCascade*) struct_ptr;
 
     /* TODO: parameters check */
 
-    CV_CALL( cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_HAAR, attributes ) );
-    
-    CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_SIZE_NAME, CV_NODE_SEQ | CV_NODE_FLOW ) );
-    CV_CALL( cvWriteInt( fs, NULL, cascade->orig_window_size.width ) );
-    CV_CALL( cvWriteInt( fs, NULL, cascade->orig_window_size.height ) );
-    CV_CALL( cvEndWriteStruct( fs ) ); /* size */
-    
-    CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_STAGES_NAME, CV_NODE_SEQ ) );    
+    cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_HAAR, attributes );
+
+    cvStartWriteStruct( fs, ICV_HAAR_SIZE_NAME, CV_NODE_SEQ | CV_NODE_FLOW );
+    cvWriteInt( fs, NULL, cascade->orig_window_size.width );
+    cvWriteInt( fs, NULL, cascade->orig_window_size.height );
+    cvEndWriteStruct( fs ); /* size */
+
+    cvStartWriteStruct( fs, ICV_HAAR_STAGES_NAME, CV_NODE_SEQ );
     for( i = 0; i < cascade->count; ++i )
     {
-        CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_MAP ) );
+        cvStartWriteStruct( fs, NULL, CV_NODE_MAP );
         sprintf( buf, "stage %d", i );
-        CV_CALL( cvWriteComment( fs, buf, 1 ) );
-        
-        CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_TREES_NAME, CV_NODE_SEQ ) );
+        cvWriteComment( fs, buf, 1 );
+
+        cvStartWriteStruct( fs, ICV_HAAR_TREES_NAME, CV_NODE_SEQ );
 
         for( j = 0; j < cascade->stage_classifier[i].count; ++j )
         {
             CvHaarClassifier* tree = &cascade->stage_classifier[i].classifier[j];
 
-            CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_SEQ ) );
+            cvStartWriteStruct( fs, NULL, CV_NODE_SEQ );
             sprintf( buf, "tree %d", j );
-            CV_CALL( cvWriteComment( fs, buf, 1 ) );
+            cvWriteComment( fs, buf, 1 );
 
             for( k = 0; k < tree->count; ++k )
             {
                 CvHaarFeature* feature = &tree->haar_feature[k];
 
-                CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_MAP ) );
+                cvStartWriteStruct( fs, NULL, CV_NODE_MAP );
                 if( k )
                 {
                     sprintf( buf, "node %d", k );
@@ -1671,70 +1892,64 @@ icvWriteHaarClassifier( CvFileStorage* fs, const char* name, const void* struct_
                 {
                     sprintf( buf, "root node" );
                 }
-                CV_CALL( cvWriteComment( fs, buf, 1 ) );
+                cvWriteComment( fs, buf, 1 );
 
-                CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_FEATURE_NAME, CV_NODE_MAP ) );
-                
-                CV_CALL( cvStartWriteStruct( fs, ICV_HAAR_RECTS_NAME, CV_NODE_SEQ ) );
-                for( l = 0; l < CV_HAAR_FEATURE_MAX && feature->rect[l].weight != 0; ++l )
+                cvStartWriteStruct( fs, ICV_HAAR_FEATURE_NAME, CV_NODE_MAP );
+
+                cvStartWriteStruct( fs, ICV_HAAR_RECTS_NAME, CV_NODE_SEQ );
+                for( l = 0; l < CV_HAAR_FEATURE_MAX && feature->rect[l].r.width != 0; ++l )
                 {
-                    CV_CALL( cvStartWriteStruct( fs, NULL, CV_NODE_SEQ | CV_NODE_FLOW ) );
-                    CV_CALL( cvWriteInt(  fs, NULL, feature->rect[l].r.x ) );
-                    CV_CALL( cvWriteInt(  fs, NULL, feature->rect[l].r.y ) );
-                    CV_CALL( cvWriteInt(  fs, NULL, feature->rect[l].r.width ) );
-                    CV_CALL( cvWriteInt(  fs, NULL, feature->rect[l].r.height ) );
-                    CV_CALL( cvWriteReal( fs, NULL, feature->rect[l].weight ) );
-                    CV_CALL( cvEndWriteStruct( fs ) ); /* rect */
+                    cvStartWriteStruct( fs, NULL, CV_NODE_SEQ | CV_NODE_FLOW );
+                    cvWriteInt(  fs, NULL, feature->rect[l].r.x );
+                    cvWriteInt(  fs, NULL, feature->rect[l].r.y );
+                    cvWriteInt(  fs, NULL, feature->rect[l].r.width );
+                    cvWriteInt(  fs, NULL, feature->rect[l].r.height );
+                    cvWriteReal( fs, NULL, feature->rect[l].weight );
+                    cvEndWriteStruct( fs ); /* rect */
                 }
-                CV_CALL( cvEndWriteStruct( fs ) ); /* rects */
-                CV_CALL( cvWriteInt( fs, ICV_HAAR_TILTED_NAME, feature->tilted ) );
-                CV_CALL( cvEndWriteStruct( fs ) ); /* feature */
-                
-                CV_CALL( cvWriteReal( fs, ICV_HAAR_THRESHOLD_NAME, tree->threshold[k]) );
+                cvEndWriteStruct( fs ); /* rects */
+                cvWriteInt( fs, ICV_HAAR_TILTED_NAME, feature->tilted );
+                cvEndWriteStruct( fs ); /* feature */
+
+                cvWriteReal( fs, ICV_HAAR_THRESHOLD_NAME, tree->threshold[k]);
 
                 if( tree->left[k] > 0 )
                 {
-                    CV_CALL( cvWriteInt( fs, ICV_HAAR_LEFT_NODE_NAME, tree->left[k] ) );
+                    cvWriteInt( fs, ICV_HAAR_LEFT_NODE_NAME, tree->left[k] );
                 }
                 else
                 {
-                    CV_CALL( cvWriteReal( fs, ICV_HAAR_LEFT_VAL_NAME,
-                        tree->alpha[-tree->left[k]] ) );
+                    cvWriteReal( fs, ICV_HAAR_LEFT_VAL_NAME,
+                        tree->alpha[-tree->left[k]] );
                 }
 
                 if( tree->right[k] > 0 )
                 {
-                    CV_CALL( cvWriteInt( fs, ICV_HAAR_RIGHT_NODE_NAME, tree->right[k] ) );
+                    cvWriteInt( fs, ICV_HAAR_RIGHT_NODE_NAME, tree->right[k] );
                 }
                 else
                 {
-                    CV_CALL( cvWriteReal( fs, ICV_HAAR_RIGHT_VAL_NAME,
-                        tree->alpha[-tree->right[k]] ) );
+                    cvWriteReal( fs, ICV_HAAR_RIGHT_VAL_NAME,
+                        tree->alpha[-tree->right[k]] );
                 }
 
-                CV_CALL( cvEndWriteStruct( fs ) ); /* split */
+                cvEndWriteStruct( fs ); /* split */
             }
 
-            CV_CALL( cvEndWriteStruct( fs ) ); /* tree */
+            cvEndWriteStruct( fs ); /* tree */
         }
 
-        CV_CALL( cvEndWriteStruct( fs ) ); /* trees */
-
-        CV_CALL( cvWriteReal( fs, ICV_HAAR_STAGE_THRESHOLD_NAME,
-                              cascade->stage_classifier[i].threshold) );
+        cvEndWriteStruct( fs ); /* trees */
 
-        CV_CALL( cvWriteInt( fs, ICV_HAAR_PARENT_NAME,
-                              cascade->stage_classifier[i].parent ) );
-        CV_CALL( cvWriteInt( fs, ICV_HAAR_NEXT_NAME,
-                              cascade->stage_classifier[i].next ) );
+        cvWriteReal( fs, ICV_HAAR_STAGE_THRESHOLD_NAME, cascade->stage_classifier[i].threshold);
+        cvWriteInt( fs, ICV_HAAR_PARENT_NAME, cascade->stage_classifier[i].parent );
+        cvWriteInt( fs, ICV_HAAR_NEXT_NAME, cascade->stage_classifier[i].next );
 
-        CV_CALL( cvEndWriteStruct( fs ) ); /* stage */
+        cvEndWriteStruct( fs ); /* stage */
     } /* for each stage */
-    
-    CV_CALL( cvEndWriteStruct( fs ) ); /* stages */
-    CV_CALL( cvEndWriteStruct( fs ) ); /* root */
 
-    __END__;
+    cvEndWriteStruct( fs ); /* stages */
+    cvEndWriteStruct( fs ); /* root */
 }
 
 static void*
@@ -1742,16 +1957,12 @@ icvCloneHaarClassifier( const void* struct_ptr )
 {
     CvHaarClassifierCascade* cascade = NULL;
 
-    CV_FUNCNAME( "cvCloneHaarClassifier" );
-
-    __BEGIN__;
-
     int i, j, k, n;
     const CvHaarClassifierCascade* cascade_src =
         (const CvHaarClassifierCascade*) struct_ptr;
 
     n = cascade_src->count;
-    CV_CALL( cascade = icvCreateHaarClassifierCascade(n) );
+    cascade = icvCreateHaarClassifierCascade(n);
     cascade->orig_window_size = cascade_src->orig_window_size;
 
     for( i = 0; i < n; ++i )
@@ -1762,31 +1973,29 @@ icvCloneHaarClassifier( const void* struct_ptr )
         cascade->stage_classifier[i].threshold = cascade_src->stage_classifier[i].threshold;
 
         cascade->stage_classifier[i].count = 0;
-        CV_CALL( cascade->stage_classifier[i].classifier =
+        cascade->stage_classifier[i].classifier =
             (CvHaarClassifier*) cvAlloc( cascade_src->stage_classifier[i].count
-                * sizeof( cascade->stage_classifier[i].classifier[0] ) ) );
-        
+                * sizeof( cascade->stage_classifier[i].classifier[0] ) );
+
         cascade->stage_classifier[i].count = cascade_src->stage_classifier[i].count;
 
         for( j = 0; j < cascade->stage_classifier[i].count; ++j )
-        {
             cascade->stage_classifier[i].classifier[j].haar_feature = NULL;
-        }
 
         for( j = 0; j < cascade->stage_classifier[i].count; ++j )
         {
-            const CvHaarClassifier* classifier_src = 
+            const CvHaarClassifier* classifier_src =
                 &cascade_src->stage_classifier[i].classifier[j];
-            CvHaarClassifier* classifier = 
+            CvHaarClassifier* classifier =
                 &cascade->stage_classifier[i].classifier[j];
 
             classifier->count = classifier_src->count;
-            CV_CALL( classifier->haar_feature = (CvHaarFeature*) cvAlloc( 
+            classifier->haar_feature = (CvHaarFeature*) cvAlloc(
                 classifier->count * ( sizeof( *classifier->haar_feature ) +
                                       sizeof( *classifier->threshold ) +
                                       sizeof( *classifier->left ) +
                                       sizeof( *classifier->right ) ) +
-                (classifier->count + 1) * sizeof( *classifier->alpha ) ) );
+                (classifier->count + 1) * sizeof( *classifier->alpha ) );
             classifier->threshold = (float*) (classifier->haar_feature+classifier->count);
             classifier->left = (int*) (classifier->threshold + classifier->count);
             classifier->right = (int*) (classifier->left + classifier->count);
@@ -1799,39 +2008,59 @@ icvCloneHaarClassifier( const void* struct_ptr )
                 classifier->right[k] = classifier_src->right[k];
                 classifier->alpha[k] = classifier_src->alpha[k];
             }
-            classifier->alpha[classifier->count] = 
+            classifier->alpha[classifier->count] =
                 classifier_src->alpha[classifier->count];
         }
     }
 
-    __END__;
-
     return cascade;
 }
 
-static int
-icvRegisterHaarClassifierType()
-{
-    CV_FUNCNAME( "icvRegisterHaarClassifierType" );
 
-    __BEGIN__;
+CvType haar_type( CV_TYPE_NAME_HAAR, icvIsHaarClassifier,
+                  (CvReleaseFunc)cvReleaseHaarClassifierCascade,
+                  icvReadHaarClassifier, icvWriteHaarClassifier,
+                  icvCloneHaarClassifier );
 
-    CvTypeInfo info;
+#if 0
+namespace cv
+{
 
-    info.header_size = sizeof( info );
-    info.is_instance = icvIsHaarClassifier;
-    info.release = (CvReleaseFunc) cvReleaseHaarClassifierCascade;
-    info.read = icvReadHaarClassifier;
-    info.write = icvWriteHaarClassifier;
-    info.clone = icvCloneHaarClassifier;
-    info.type_name = CV_TYPE_NAME_HAAR;
-    CV_CALL( cvRegisterType( &info ) );
+HaarClassifierCascade::HaarClassifierCascade() {}
+HaarClassifierCascade::HaarClassifierCascade(const String& filename)
+{ load(filename); }
+    
+bool HaarClassifierCascade::load(const String& filename)
+{
+    cascade = Ptr<CvHaarClassifierCascade>((CvHaarClassifierCascade*)cvLoad(filename.c_str(), 0, 0, 0));
+    return (CvHaarClassifierCascade*)cascade != 0;
+}
 
-    __END__;
+void HaarClassifierCascade::detectMultiScale( const Mat& image,
+                       Vector<Rect>& objects, double scaleFactor,
+                       int minNeighbors, int flags,
+                       Size minSize )
+{
+    MemStorage storage(cvCreateMemStorage(0));
+    CvMat _image = image;
+    CvSeq* _objects = cvHaarDetectObjects( &_image, cascade, storage, scaleFactor,
+                                           minNeighbors, flags, minSize );
+    Seq<Rect>(_objects).copyTo(objects);
+}
 
-    return 1;
+int HaarClassifierCascade::runAt(Point pt, int startStage, int) const
+{
+    return cvRunHaarClassifierCascade(cascade, pt, startStage);
 }
 
-static int icv_register_haar_classifier_type = icvRegisterHaarClassifierType();
+void HaarClassifierCascade::setImages( const Mat& sum, const Mat& sqsum,
+                                       const Mat& tilted, double scale )
+{
+    CvMat _sum = sum, _sqsum = sqsum, _tilted = tilted;
+    cvSetImagesForHaarClassifierCascade( cascade, &_sum, &_sqsum, &_tilted, scale );
+}
+
+}
+#endif
 
 /* End of file. */