From a5910ac06887146119f186843e36bdc01d99018d Mon Sep 17 00:00:00 2001
From: Anatoly Baksheev <no@email>
Date: Fri, 24 Sep 2010 16:41:34 +0000
Subject: [PATCH] modified according to NPP for CUDA 3.2 API updates.

---
 modules/gpu/include/opencv2/gpu/gpu.hpp      |  7 +++----
 modules/gpu/src/arithm.cpp                   | 15 ++++++++++----
 modules/gpu/src/{npp_error.cpp => error.cpp} | 21 +++++++++++++++-----
 modules/gpu/src/precomp.cpp                  | 14 +------------
 modules/gpu/src/precomp.hpp                  |  1 +
 5 files changed, 32 insertions(+), 26 deletions(-)
 rename modules/gpu/src/{npp_error.cpp => error.cpp} (85%)

diff --git a/modules/gpu/include/opencv2/gpu/gpu.hpp b/modules/gpu/include/opencv2/gpu/gpu.hpp
index 38edb9da8..a58d4b0b1 100644
--- a/modules/gpu/include/opencv2/gpu/gpu.hpp
+++ b/modules/gpu/include/opencv2/gpu/gpu.hpp
@@ -372,6 +372,7 @@ namespace cv
         //! computes norm of array
         //! Supports NORM_INF, NORM_L1, NORM_L2
         CV_EXPORTS double norm(const GpuMat& src1, int normType=NORM_L2);
+        
         //! computes norm of the difference between two arrays
         //! Supports NORM_INF, NORM_L1, NORM_L2
         CV_EXPORTS double norm(const GpuMat& src1, const GpuMat& src2, int normType=NORM_L2);
@@ -475,11 +476,9 @@ namespace cv
 
         //! smooths the image using the normalized box filter
         CV_EXPORTS void boxFilter(const GpuMat& src, GpuMat& dst, Size ksize, Point anchor = Point(-1,-1));
+
         //! a synonym for normalized box filter
-        static inline void blur(const GpuMat& src, GpuMat& dst, Size ksize, Point anchor = Point(-1,-1))
-        {
-            boxFilter(src, dst, ksize, anchor);
-        }
+        static inline void blur(const GpuMat& src, GpuMat& dst, Size ksize, Point anchor = Point(-1,-1)) { boxFilter(src, dst, ksize, anchor); }
 
         //! erodes the image (applies the local minimum operator)
         CV_EXPORTS void erode( const GpuMat& src, GpuMat& dst, const Mat& kernel, Point anchor, int iterations);
diff --git a/modules/gpu/src/arithm.cpp b/modules/gpu/src/arithm.cpp
index a21acbbf2..7aa81a92c 100644
--- a/modules/gpu/src/arithm.cpp
+++ b/modules/gpu/src/arithm.cpp
@@ -310,18 +310,25 @@ Scalar cv::gpu::sum(const GpuMat& src)
     CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC4);
     
     Scalar res;
+    
 
     NppiSize sz;
     sz.width  = src.cols;
     sz.height = src.rows;
 
+    int bufsz;
+    
     if (src.type() == CV_8UC1)
-    {
-        nppSafeCall( nppiSum_8u_C1R(src.ptr<Npp8u>(), src.step, sz, res.val) );
+    {        
+        nppiReductionGetBufferHostSize_8u_C1R(sz, &bufsz);
+        GpuMat buf(1, bufsz, CV_32S);
+        nppSafeCall( nppiSum_8u_C1R(src.ptr<Npp8u>(), src.step, sz, buf.ptr<Npp32s>(), res.val) );
     }
     else
-    {
-        nppSafeCall( nppiSum_8u_C4R(src.ptr<Npp8u>(), src.step, sz, res.val) );
+    {                
+        nppiReductionGetBufferHostSize_8u_C4R(sz, &bufsz);
+        GpuMat buf(1, bufsz, CV_32S);
+        nppSafeCall( nppiSum_8u_C4R(src.ptr<Npp8u>(), src.step, sz, buf.ptr<Npp32s>(), res.val) );
     }
 
     return res;
diff --git a/modules/gpu/src/npp_error.cpp b/modules/gpu/src/error.cpp
similarity index 85%
rename from modules/gpu/src/npp_error.cpp
rename to modules/gpu/src/error.cpp
index df22b80c9..eeaf86d79 100644
--- a/modules/gpu/src/npp_error.cpp
+++ b/modules/gpu/src/error.cpp
@@ -42,6 +42,7 @@
 
 #include "precomp.hpp"
 
+
 using namespace cv;
 using namespace cv::gpu;
 
@@ -56,7 +57,7 @@ namespace
     struct NppError
     {
         int error;
-        const char* str;
+        string str;
     } 
     npp_errors [] = 
     {
@@ -95,8 +96,9 @@ namespace
         { NPP_WRONG_INTERSECTION_QUAD_WARNING, "NPP_WRONG_INTERSECTION_QUAD_WARNING" },
         { NPP_MISALIGNED_DST_ROI_WARNING, "NPP_MISALIGNED_DST_ROI_WARNING" },
         { NPP_AFFINE_QUAD_INCORRECT_WARNING, "NPP_AFFINE_QUAD_INCORRECT_WARNING" },
-        { NPP_AFFINE_QUAD_CHANGED_WARNING, "NPP_AFFINE_QUAD_CHANGED_WARNING" },
-        { NPP_ADJUSTED_ROI_SIZE_WARNING, "NPP_ADJUSTED_ROI_SIZE_WARNING" },
+        //disabled in NPP for cuda 3.2-rc
+        //{ NPP_AFFINE_QUAD_CHANGED_WARNING, "NPP_AFFINE_QUAD_CHANGED_WARNING" },
+        //{ NPP_ADJUSTED_ROI_SIZE_WARNING, "NPP_ADJUSTED_ROI_SIZE_WARNING" },
         { NPP_DOUBLE_SIZE_WARNING, "NPP_DOUBLE_SIZE_WARNING" },
         { NPP_ODD_ROI_WARNING, "NPP_ODD_ROI_WARNING" }
     };
@@ -116,17 +118,26 @@ namespace cv
 {
     namespace gpu
     {
-        extern "C" const char* getNppErrorString( int err )
+        const string getNppErrorString( int err )
         {
             int idx = std::find_if(npp_errors, npp_errors + error_num, Searcher(err)) - npp_errors;
+            const string& msg = (idx != error_num) ? npp_errors[idx].str : string("Unknown error code");
 
-            return (idx != error_num) ? npp_errors[idx].str : "";             
+            std::stringstream interpreter;
+            interpreter << "<" << err << "> " << msg;
+
+            return interpreter.str();
         }
 
         extern "C" void npp_error( int err, const char *file, const int line, const char *func)
         {                    
             cv::error( cv::Exception(CV_GpuNppCallError, getNppErrorString(err), func, file, line) );                
         }
+
+        extern "C" void error(const char *error_string, const char *file, const int line, const char *func)
+        {                       
+            cv::error( cv::Exception(CV_GpuApiCallError, error_string, func, file, line) );
+        }
     }
 }
 
diff --git a/modules/gpu/src/precomp.cpp b/modules/gpu/src/precomp.cpp
index d0216dbb7..2bf93e651 100644
--- a/modules/gpu/src/precomp.cpp
+++ b/modules/gpu/src/precomp.cpp
@@ -41,16 +41,4 @@
 
 #include "precomp.hpp"
 
-/* End of file. */
-
-
-namespace cv
-{
-    namespace gpu
-    {
-        extern "C" void error(const char *error_string, const char *file, const int line, const char *func)
-        {                       
-            cv::error( cv::Exception(CV_GpuApiCallError, error_string, func, file, line) );
-        }
-    }
-}
+/* End of file. */
\ No newline at end of file
diff --git a/modules/gpu/src/precomp.hpp b/modules/gpu/src/precomp.hpp
index 43d2985f9..d9a7a9152 100644
--- a/modules/gpu/src/precomp.hpp
+++ b/modules/gpu/src/precomp.hpp
@@ -54,6 +54,7 @@
 #include <limits>
 #include <vector>
 #include <algorithm>
+#include <sstream>
 
 #include "opencv2/gpu/gpu.hpp"
 #include "opencv2/imgproc/imgproc.hpp"