From 05e7c29de5fc4864a8812d8f8ab92e89ae6343dc Mon Sep 17 00:00:00 2001
From: Vadim Pisarevsky <vadim.pisarevsky@gmail.com>
Date: Sun, 10 Aug 2014 00:10:05 +0400
Subject: [PATCH] fixed various warnings and obvious errors reported by clang
 compiler and the coverity tool.

---
 modules/core/src/dxt.cpp                | 4 ++--
 modules/core/src/ocl.cpp                | 7 +++----
 modules/features2d/src/freak.cpp        | 1 -
 modules/imgproc/src/templmatch.cpp      | 1 -
 modules/ml/src/ann_mlp.cpp              | 2 +-
 modules/ml/src/data.cpp                 | 8 ++++----
 modules/ml/src/svm.cpp                  | 4 ++--
 modules/shape/test/test_emdl1.cpp       | 2 --
 modules/shape/test/test_shape.cpp       | 2 --
 modules/video/test/test_tvl1optflow.cpp | 4 +++-
 modules/videoio/src/cap_qtkit.mm        | 1 -
 11 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/modules/core/src/dxt.cpp b/modules/core/src/dxt.cpp
index c74d87dcba..ed96a0b80c 100644
--- a/modules/core/src/dxt.cpp
+++ b/modules/core/src/dxt.cpp
@@ -1801,11 +1801,11 @@ private:
     UMat twiddles;
     String buildOptions;
     int thread_count;
-    bool status;
     int dft_size;
+    bool status;
 
 public:
-    OCL_FftPlan(int _size): dft_size(_size), status(true)
+    OCL_FftPlan(int _size) : dft_size(_size), status(true)
     {
         int min_radix;
         std::vector<int> radixes, blocks;
diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp
index 433249a2ea..837d16ee83 100644
--- a/modules/core/src/ocl.cpp
+++ b/modules/core/src/ocl.cpp
@@ -1324,6 +1324,9 @@ OCL_FUNC(cl_int, clReleaseEvent, (cl_event event), (event))
 
 #endif
 
+#ifdef _DEBUG
+#define CV_OclDbgAssert CV_DbgAssert
+#else
 static bool isRaiseError()
 {
     static bool initialized = false;
@@ -1335,10 +1338,6 @@ static bool isRaiseError()
     }
     return value;
 }
-
-#ifdef _DEBUG
-#define CV_OclDbgAssert CV_DbgAssert
-#else
 #define CV_OclDbgAssert(expr) do { if (isRaiseError()) { CV_Assert(expr); } else { (void)(expr); } } while ((void)0, 0)
 #endif
 
diff --git a/modules/features2d/src/freak.cpp b/modules/features2d/src/freak.cpp
index 00c0e35ae8..58c1fe11e2 100644
--- a/modules/features2d/src/freak.cpp
+++ b/modules/features2d/src/freak.cpp
@@ -45,7 +45,6 @@ namespace cv
 {
 
 static const double FREAK_SQRT2 = 1.4142135623731;
-static const double FREAK_INV_SQRT2 = 1.0 / FREAK_SQRT2;
 static const double FREAK_LOG2 = 0.693147180559945;
 static const int FREAK_NB_ORIENTATION = 256;
 static const int FREAK_NB_POINTS = 43;
diff --git a/modules/imgproc/src/templmatch.cpp b/modules/imgproc/src/templmatch.cpp
index 33c1e15bd6..6919d7a3d0 100644
--- a/modules/imgproc/src/templmatch.cpp
+++ b/modules/imgproc/src/templmatch.cpp
@@ -450,7 +450,6 @@ static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArr
 
     UMat templ  = _templ.getUMat();
     UMat result = _result.getUMat();
-    Size tsize = templ.size();
 
     if (cn==1)
     {
diff --git a/modules/ml/src/ann_mlp.cpp b/modules/ml/src/ann_mlp.cpp
index 73af1ae94d..b0e1447dcc 100644
--- a/modules/ml/src/ann_mlp.cpp
+++ b/modules/ml/src/ann_mlp.cpp
@@ -431,7 +431,7 @@ public:
                 break;
 
             case GAUSSIAN:
-                for( i = 0; i < n; j++ )
+                for( i = 0; i < n; i++ )
                 {
                     double* data = sums.ptr<double>(i);
                     for( j = 0; j < cols; j++ )
diff --git a/modules/ml/src/data.cpp b/modules/ml/src/data.cpp
index b5d0527985..c9a323a43a 100644
--- a/modules/ml/src/data.cpp
+++ b/modules/ml/src/data.cpp
@@ -861,9 +861,9 @@ public:
     void getValues( int vi, InputArray _sidx, float* values ) const
     {
         Mat sidx = _sidx.getMat();
-        int i, n, nsamples = getNSamples();
+        int i, n = sidx.checkVector(1, CV_32S), nsamples = getNSamples();
         CV_Assert( 0 <= vi && vi < getNAllVars() );
-        CV_Assert( (n = sidx.checkVector(1, CV_32S)) >= 0 );
+        CV_Assert( n >= 0 );
         const int* s = n > 0 ? sidx.ptr<int>() : 0;
         if( n == 0 )
             n = nsamples;
@@ -938,8 +938,8 @@ public:
     {
         CV_Assert(buf != 0 && 0 <= sidx && sidx < getNSamples());
         Mat vidx = _vidx.getMat();
-        int i, n, nvars = getNAllVars();
-        CV_Assert( (n = vidx.checkVector(1, CV_32S)) >= 0 );
+        int i, n = vidx.checkVector(1, CV_32S), nvars = getNAllVars();
+        CV_Assert( n >= 0 );
         const int* vptr = n > 0 ? vidx.ptr<int>() : 0;
         if( n == 0 )
             n = nvars;
diff --git a/modules/ml/src/svm.cpp b/modules/ml/src/svm.cpp
index 985cc62520..49e5c0200a 100644
--- a/modules/ml/src/svm.cpp
+++ b/modules/ml/src/svm.cpp
@@ -1335,9 +1335,9 @@ public:
                 _responses.convertTo(_yf, CV_32F);
 
             bool ok =
-            (svmType == ONE_CLASS ? Solver::solve_one_class( _samples, params.nu, kernel, _alpha, sinfo, termCrit ) :
+            svmType == ONE_CLASS ? Solver::solve_one_class( _samples, params.nu, kernel, _alpha, sinfo, termCrit ) :
             svmType == EPS_SVR ? Solver::solve_eps_svr( _samples, _yf, params.p, params.C, kernel, _alpha, sinfo, termCrit ) :
-            svmType == NU_SVR ? Solver::solve_nu_svr( _samples, _yf, params.nu, params.C, kernel, _alpha, sinfo, termCrit ) : false);
+            svmType == NU_SVR ? Solver::solve_nu_svr( _samples, _yf, params.nu, params.C, kernel, _alpha, sinfo, termCrit ) : false;
 
             if( !ok )
                 return false;
diff --git a/modules/shape/test/test_emdl1.cpp b/modules/shape/test/test_emdl1.cpp
index fc677fc5b0..e52351bcf6 100644
--- a/modules/shape/test/test_emdl1.cpp
+++ b/modules/shape/test/test_emdl1.cpp
@@ -50,8 +50,6 @@ const float minRad=0.2f;
 const float maxRad=2;
 const int NSN=5;//10;//20; //number of shapes per class
 const int NP=100; //number of points sympliying the contour
-const float outlierWeight=0.1f;
-const int numOutliers=20;
 const float CURRENT_MAX_ACCUR=95; //98% and 99% reached in several tests, 95 is fixed as minimum boundary
 
 class CV_ShapeEMDTest : public cvtest::BaseTest
diff --git a/modules/shape/test/test_shape.cpp b/modules/shape/test/test_shape.cpp
index 737e0473ae..04e89fe6b9 100644
--- a/modules/shape/test/test_shape.cpp
+++ b/modules/shape/test/test_shape.cpp
@@ -50,8 +50,6 @@ const float minRad=0.2f;
 const float maxRad=2;
 const int NSN=5;//10;//20; //number of shapes per class
 const int NP=120; //number of points sympliying the contour
-const float outlierWeight=0.1f;
-const int numOutliers=20;
 const float CURRENT_MAX_ACCUR=95; //99% and 100% reached in several tests, 95 is fixed as minimum boundary
 
 class CV_ShapeTest : public cvtest::BaseTest
diff --git a/modules/video/test/test_tvl1optflow.cpp b/modules/video/test/test_tvl1optflow.cpp
index 274c13e65d..4772f0fb6e 100644
--- a/modules/video/test/test_tvl1optflow.cpp
+++ b/modules/video/test/test_tvl1optflow.cpp
@@ -52,12 +52,13 @@ namespace
 {
     // first four bytes, should be the same in little endian
     const float FLO_TAG_FLOAT = 202021.25f;  // check for this when READING the file
-    const char FLO_TAG_STRING[] = "PIEH";    // use this when WRITING the file
 
+#ifdef DUMP
     // binary file format for flow data specified here:
     // http://vision.middlebury.edu/flow/data/
     void writeOpticalFlowToFile(const Mat_<Point2f>& flow, const string& fileName)
     {
+        const char FLO_TAG_STRING[] = "PIEH";    // use this when WRITING the file
         ofstream file(fileName.c_str(), ios_base::binary);
 
         file << FLO_TAG_STRING;
@@ -76,6 +77,7 @@ namespace
             }
         }
     }
+#endif
 
     // binary file format for flow data specified here:
     // http://vision.middlebury.edu/flow/data/
diff --git a/modules/videoio/src/cap_qtkit.mm b/modules/videoio/src/cap_qtkit.mm
index 461bc1f33c..a0b10cd090 100644
--- a/modules/videoio/src/cap_qtkit.mm
+++ b/modules/videoio/src/cap_qtkit.mm
@@ -199,7 +199,6 @@ public:
 private:
     IplImage* argbimage;
     QTMovie* mMovie;
-    unsigned char* imagedata;
 
     NSString* path;
     NSString* codec;