]> Creatis software - clitk.git/blobdiff - common/clitkImageToImageGenericFilterBase.cxx
Change N formula
[clitk.git] / common / clitkImageToImageGenericFilterBase.cxx
index 2d1679f748def5299a5c847c757b360bf5557f22..c538ac0d196a3773508918f110b093785a8be7e8 100644 (file)
@@ -3,7 +3,7 @@
 
   Authors belong to:
   - University of LYON              http://www.universite-lyon.fr/
-  - Léon Bérard cancer center       http://oncora1.lyon.fnclcc.fr
+  - Léon Bérard cancer center       http://www.centreleonberard.fr
   - CREATIS CNRS laboratory         http://www.creatis.insa-lyon.fr
 
   This software is distributed WITHOUT ANY WARRANTY; without even
 
   - BSD        See included LICENSE.txt file
   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
-======================================================================-====*/
+===========================================================================**/
+
+// clitk
 #include "clitkImageToImageGenericFilterBase.h"
+#include "clitkExceptionObject.h"
+
+// itk
 #include <itkImage.h>
 
 //--------------------------------------------------------------------
@@ -30,7 +35,11 @@ clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string
   m_FilterName = n;
   m_FailOnImageTypeError = true;
   m_ReadOnDisk = true;
-  m_LastError = "";
+  m_WriteOnDisk = true;
+  m_WriteCompression = false;
+  // m_LastError = "";
+  // StopOnErrorOn();
+  SetFilterBase(NULL);
 }
 //--------------------------------------------------------------------
 
@@ -39,6 +48,11 @@ clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string
 void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames)
 {
   m_InputFilenames = filenames;
+  // in this case, assume by default that we DO want to write/read on
+  // disk (rather a command line tool, but not a vvTool. Can be
+  // changed with EnableReadOnDisk and EnableWriteOnDisk)
+  EnableReadOnDisk(true);
+  EnableWriteOnDisk(true);
 }
 //--------------------------------------------------------------------
 
@@ -51,12 +65,33 @@ void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b)
 //--------------------------------------------------------------------
 
 
+//--------------------------------------------------------------------
+void clitk::ImageToImageGenericFilterBase::EnableWriteOnDisk(bool b)
+{
+  m_WriteOnDisk = b;
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+void clitk::ImageToImageGenericFilterBase::EnableWriteCompression(bool b)
+{
+  m_WriteCompression = b;
+}
+//--------------------------------------------------------------------
+
+
 //--------------------------------------------------------------------
 void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename)
 {
   std::vector<std::string> f;
   f.push_back(filename);
   SetInputFilenames(f);
+  // in this case, assume by default that we DO want to write/read on
+  // disk (rather a command line tool, but not a vvTool. Can be
+  // changed with EnableReadOnDisk and EnableWriteOnDisk)
+  EnableReadOnDisk(true);
+  EnableWriteOnDisk(true);
 }
 //--------------------------------------------------------------------
 
@@ -65,6 +100,11 @@ void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string &
 void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename)
 {
   m_InputFilenames.push_back(filename);
+  // in this case, assume by default that we DO want to write/read on
+  // disk (rather a command line tool, but not a vvTool. Can be
+  // changed with EnableReadOnDisk and EnableWriteOnDisk)
+  EnableReadOnDisk(true);
+  EnableWriteOnDisk(true);
 }
 //--------------------------------------------------------------------
 
@@ -79,7 +119,7 @@ void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string &
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string filename)
+void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string filename)
 {
   m_OutputFilenames.push_back(filename);
 }
@@ -115,11 +155,13 @@ void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(un
     dim=dim_temp;
   } else {
     if (m_InputVVImages.size()) {
-      pixeltype=m_InputVVImages[0]->GetScalarTypeAsString();
-      dim=m_InputVVImages[0]->GetNumberOfDimensions();
-      components=m_InputVVImages[0]->GetNumberOfScalarComponents();
-    } else
+      pixeltype = m_InputVVImages[0]->GetScalarTypeAsITKString();
+      dim = m_InputVVImages[0]->GetNumberOfDimensions();
+      components = m_InputVVImages[0]->GetNumberOfScalarComponents();
+    } else {
+      clitkExceptionMacro("No input given in this ImageToImageGenericFilter.");
       assert(false); //No input image, shouldn't happen
+    }
   }
   if (m_IOVerbose) {
     std::cout << "Input is " << m_Dim << "D " << m_PixelTypeName << "." << std::endl;
@@ -150,6 +192,11 @@ void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer inp
 {
   m_InputVVImages.clear();
   m_InputVVImages.push_back(input);
+  // in this case, assume by default that we do not want to write/read
+  // on disk (not a command line tool, but rather a vvTool. Can be
+  // changed with EnableReadOnDisk and EnableWriteOnDisk)
+  EnableReadOnDisk(false);
+  EnableWriteOnDisk(false);
 }
 //--------------------------------------------------------------------
 
@@ -166,6 +213,11 @@ void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer inp
 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
 {
   m_InputVVImages=input;
+  // in this case, assume by default that we do not want to write/read
+  // on disk (not a command line tool, but rather a vvTool. Can be
+  // changed with EnableReadOnDisk and EnableWriteOnDisk)
+  EnableReadOnDisk(false);
+  EnableWriteOnDisk(false);
 }
 //--------------------------------------------------------------------
 
@@ -182,12 +234,14 @@ void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes()
 //--------------------------------------------------------------------
 void clitk::ImageToImageGenericFilterBase::ImageTypeError()
 {
-  std::cerr << "**Error** The filter <" << m_FilterName << "> is not available for "
-            << m_Dim << "D images with pixel="
-            << m_PixelTypeName << " and "
-            << m_NbOfComponents << " component." << std::endl;
-  std::cerr << GetAvailableImageTypes();
-  exit(0);
+  std::ostringstream os;
+  os << "**Error** The filter <" << m_FilterName << "> is not available for "
+     << m_Dim << "D images with pixel="
+     << m_PixelTypeName << " and "
+     << m_NbOfComponents << " component." << std::endl;
+  os << GetAvailableImageTypes();
+  clitkExceptionMacro(os.str());
+  //  exit(0);
 }
 //--------------------------------------------------------------------
 
@@ -230,11 +284,11 @@ void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<PixelType, D
   template \
    itk::Image<PixelType, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<PixelType, Dim> >(unsigned int n);
 
-#define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim)   \
+#define DEF_SetNextOutput_And_GetInput_WithCompo(PixelType, Compo, Dim)   \
   template \
-  void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<float, Compo>, Dim> >(itk::Image<itk::Vector<float, Compo>,Dim>::Pointer output); \
+  void clitk::ImageToImageGenericFilterBase::SetNextOutput<itk::Image<itk::Vector<PixelType, Compo>, Dim> >(itk::Image<itk::Vector<PixelType, Compo>,Dim>::Pointer output); \
   template \
-  itk::Image<itk::Vector<float,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<float, Compo>, Dim> >(unsigned int n);
+  itk::Image<itk::Vector<PixelType,Compo>, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput<itk::Image<itk::Vector<PixelType, Compo>, Dim> >(unsigned int n);
 
 DEF_SetNextOutput_And_GetInput(char, 2);
 DEF_SetNextOutput_And_GetInput(unsigned char, 2);
@@ -252,15 +306,77 @@ DEF_SetNextOutput_And_GetInput(int, 3);
 DEF_SetNextOutput_And_GetInput(float, 3);
 DEF_SetNextOutput_And_GetInput(double, 3);
 
-DEF_SetNextOutput_And_GetInput_WithCompo(2, 2);
-DEF_SetNextOutput_And_GetInput_WithCompo(2, 3);
-DEF_SetNextOutput_And_GetInput_WithCompo(2, 4);
-DEF_SetNextOutput_And_GetInput_WithCompo(3, 2);
-DEF_SetNextOutput_And_GetInput_WithCompo(3, 3);
-DEF_SetNextOutput_And_GetInput_WithCompo(3, 4);
-DEF_SetNextOutput_And_GetInput_WithCompo(4, 2);
-DEF_SetNextOutput_And_GetInput_WithCompo(4, 3);
-DEF_SetNextOutput_And_GetInput_WithCompo(4, 4);
+/*
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 2, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 2, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 2, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 3, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 3, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 3, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 4, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 4, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned char, 4, 4);
+
+DEF_SetNextOutput_And_GetInput_WithCompo(char, 2, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(char, 2, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(char, 2, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(char, 3, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(char, 3, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(char, 3, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(char, 4, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(char, 4, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(char, 4, 4);
+
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 2, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 2, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 2, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 3, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 3, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 3, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 4, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 4, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(unsigned short, 4, 4);
+
+DEF_SetNextOutput_And_GetInput_WithCompo(short, 2, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(short, 2, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(short, 2, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(short, 3, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(short, 3, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(short, 3, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(short, 4, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(short, 4, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(short, 4, 4);
+
+DEF_SetNextOutput_And_GetInput_WithCompo(int, 2, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(int, 2, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(int, 2, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(int, 3, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(int, 3, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(int, 3, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(int, 4, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(int, 4, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(int, 4, 4);
+*/
+
+DEF_SetNextOutput_And_GetInput_WithCompo(float, 2, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(float, 2, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(float, 2, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(float, 3, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(float, 3, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(float, 3, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(float, 4, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(float, 4, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(float, 4, 4);
+
+DEF_SetNextOutput_And_GetInput_WithCompo(double, 2, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(double, 2, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(double, 2, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(double, 3, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(double, 3, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(double, 3, 4);
+DEF_SetNextOutput_And_GetInput_WithCompo(double, 4, 2);
+DEF_SetNextOutput_And_GetInput_WithCompo(double, 4, 3);
+DEF_SetNextOutput_And_GetInput_WithCompo(double, 4, 4);
 
 DEF_SetNextOutput_And_GetInput(char, 4);
 DEF_SetNextOutput_And_GetInput(unsigned char, 4);
@@ -275,8 +391,8 @@ DEF_SetNextOutput_And_GetInput(double, 4);
 template<class ImageType>
 void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output)
 {
-  if (m_OutputFilenames.size()) {
-    clitk::writeImage<ImageType>(output, m_OutputFilenames.front(), m_IOVerbose);
+  if (m_WriteOnDisk && m_OutputFilenames.size()) {
+    clitk::writeImage<ImageType>(output, m_OutputFilenames.front(), m_IOVerbose, m_WriteCompression);
     m_OutputFilenames.pop_front();
   }
   if (m_InputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
@@ -296,11 +412,33 @@ typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsig
       return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(m_InputVVImages[n]).GetPointer()));
     else {
       assert(false); //No input, this shouldn't happen
-      return typename ImageType::Pointer(NULL);
+      return typename ImageType::Pointer((ImageType*)ITK_NULLPTR);
     }
   }
 }
 //--------------------------------------------------------------------
 
 
+//--------------------------------------------------------------------
+// void clitk::ImageToImageGenericFilterBase::MustStop()
+// {
+//   if (m_FilterBase != NULL) {
+//     m_FilterBase->SetMustStop(true);
+//   }
+// }
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+void clitk::ImageToImageGenericFilterBase::DeleteLastOutputImage()
+{
+  if (m_OutputVVImages.size()>1) {
+   m_OutputVVImages.pop_back();
+  }
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+