From 4996423419494a128cf6cd7659eba3d70f3f621c Mon Sep 17 00:00:00 2001
From: Yannick TANGUY <yannick.tanguy@cnes.fr>
Date: Fri, 14 Dec 2018 10:08:12 +0000
Subject: [PATCH]  REFAC : replaces the UnaryImageFunctor with the new
 FunctorFilter (apply log function to the input image) in otbDynamicConvert

---
 .../AppImageUtils/app/otbConvert.cxx          |  6 +--
 .../AppImageUtils/app/otbDynamicConvert.cxx   | 50 ++++++++-----------
 2 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/Modules/Applications/AppImageUtils/app/otbConvert.cxx b/Modules/Applications/AppImageUtils/app/otbConvert.cxx
index 5ae0648621..05575df851 100644
--- a/Modules/Applications/AppImageUtils/app/otbConvert.cxx
+++ b/Modules/Applications/AppImageUtils/app/otbConvert.cxx
@@ -273,13 +273,13 @@ private:
       if ( rescaleType == "log2")
         {
 	  // define lambda function that applies a log to all bands of the input pixel
-	  auto logFunction = [](const FloatVectorImageType::PixelType & vectorIn) {
-	    FloatVectorImageType::PixelType vectorOut(vectorIn.Size());
+	  auto logFunction = [](FloatVectorImageType::PixelType & vectorOut, const FloatVectorImageType::PixelType & vectorIn) {
+	    assert(vectorOut.Size() == vectorIn.Size() && "Input vector types don't have the same size");
 
 	    for (unsigned int i = 0; i < vectorIn.Size() ; i++) {
 	      vectorOut[i] = std::log(vectorIn[i]);
 	    }
-	    return vectorOut;
+	    
 	  };
 	  // creates functor filter
 	  auto transferLogFilter = NewFunctorFilter(logFunction,tempImage->GetNumberOfComponentsPerPixel(),{{0,0}});
diff --git a/Modules/Applications/AppImageUtils/app/otbDynamicConvert.cxx b/Modules/Applications/AppImageUtils/app/otbDynamicConvert.cxx
index 35487cae38..aba074743b 100644
--- a/Modules/Applications/AppImageUtils/app/otbDynamicConvert.cxx
+++ b/Modules/Applications/AppImageUtils/app/otbDynamicConvert.cxx
@@ -22,7 +22,7 @@
 #include "otbWrapperApplicationFactory.h"
 
 #include "otbVectorRescaleIntensityImageFilter.h"
-#include "otbUnaryImageFunctorWithVectorImageFilter.h"
+#include "otbFunctorImageFilter.h"
 #include "otbStreamingShrinkImageFilter.h"
 #include "itkListSample.h"
 #include "otbListSampleToHistogramListGenerator.h"
@@ -39,20 +39,6 @@ namespace otb
 namespace Wrapper
 {
 
-namespace Functor
-{
-  template< class TScalar >
-class ITK_EXPORT LogFunctor
-{
-public:
-  TScalar operator() (const TScalar& v) const
-  {
-    return std::log(v);
-  }
-};
-} // end namespace Functor
-
-
 
 class DynamicConvert : public Application
 {
@@ -80,11 +66,6 @@ public:
 
   typedef StreamingShrinkImageFilter<UInt8ImageType, UInt8ImageType> UInt8ShrinkFilterType;
 
-  typedef Functor::LogFunctor<FloatVectorImageType::InternalPixelType> TransferLogFunctor;
-  typedef UnaryImageFunctorWithVectorImageFilter<FloatVectorImageType,
-    FloatVectorImageType,
-    TransferLogFunctor> TransferLogType;
-
 private:
 
   void DoInit() override
@@ -273,13 +254,26 @@ private:
 
     if ( rescaleType == "log2")
     {
-      //define the transfer log
-      m_TransferLog = TransferLogType::New();
-      m_TransferLog->SetInput(tempImage);
-      m_TransferLog->UpdateOutputInformation();
-
-      shrinkFilter->SetInput(m_TransferLog->GetOutput());
-      rescaler->SetInput(m_TransferLog->GetOutput());
+      // define lambda function that applies a log to all bands of the input pixel
+      auto logFunction = [](FloatVectorImageType::PixelType & vectorOut, const FloatVectorImageType::PixelType & vectorIn) {
+	assert(vectorOut.Size() == vectorIn.Size() && "Input vector types don't have the same size");
+	
+	for (unsigned int i = 0; i < vectorIn.Size() ; i++) {
+	  vectorOut[i] = std::log(vectorIn[i]);
+	}
+	
+      };
+// creates functor filter
+      auto transferLogFilter = NewFunctorFilter(logFunction,tempImage->GetNumberOfComponentsPerPixel(),{{0,0}});
+      
+      // save a reference to the functor
+      m_Filters.push_back(transferLogFilter.GetPointer());
+      
+      transferLogFilter->SetVariadicInputs(tempImage);
+      transferLogFilter->UpdateOutputInformation();
+      
+      shrinkFilter->SetInput(transferLogFilter->GetOutput());
+      rescaler->SetInput(transferLogFilter->GetOutput());
       shrinkFilter->Update();
     }
     else
@@ -524,8 +518,6 @@ private:
     }
   }
 
-  itk::ProcessObject::Pointer m_TmpFilter;
-  TransferLogType::Pointer m_TransferLog;
   std::vector<itk::LightObject::Pointer> m_Filters;
 };
 
-- 
GitLab