]> Creatis software - clitk.git/blobdiff - common/clitkImageToImageGenericFilterBase.cxx
changes in license header
[clitk.git] / common / clitkImageToImageGenericFilterBase.cxx
index a72ff964e231a613fe64f8d824d61d6f29a1c97d..8b0505d7bb2ec214db028ef0fba9a1211640a5f3 100644 (file)
@@ -1,4 +1,26 @@
+/*=========================================================================
+  Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
+
+  Authors belong to:
+  - University of LYON              http://www.universite-lyon.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
+  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+  PURPOSE.  See the copyright notices for more information.
+
+  It is distributed under dual licence
+
+  - 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>
 
 //--------------------------------------------------------------------
@@ -8,56 +30,89 @@ clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {}
 
 //--------------------------------------------------------------------
 clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n)
-  :mIOVerbose(false) {
-  mFilterName = n;
-  mFailOnImageTypeError = true;
-  mReadOnDisk = true;
+  :m_IOVerbose(false)
+{
+  m_FilterName = n;
+  m_FailOnImageTypeError = true;
+  m_ReadOnDisk = true;
+  m_WriteOnDisk = true;
+  // m_LastError = "";
+  // StopOnErrorOn();
+  SetFilterBase(NULL);
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+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);
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector<std::string> & filenames) {
-    mInputFilenames=filenames;
+void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b)
+{
+  m_ReadOnDisk = b;
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::EnableReadOnDisk(bool b) {
-  mReadOnDisk = b;
+void clitk::ImageToImageGenericFilterBase::EnableWriteOnDisk(bool b)
+{
+  m_WriteOnDisk = b;
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename) {
+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);
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename) {
-  mInputFilenames.push_back(filename);
+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);
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename) {
-  mOutputFilenames.clear();
-  mOutputFilenames.push_back(filename);
+void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename)
+{
+  m_OutputFilenames.clear();
+  m_OutputFilenames.push_back(filename);
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string filename)
+void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string filename)
 {
-  mOutputFilenames.push_back(filename);
+  m_OutputFilenames.push_back(filename);
 }
 //--------------------------------------------------------------------
 
@@ -65,8 +120,8 @@ void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string &
 //--------------------------------------------------------------------
 void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<std::string> & filenames)
 {
-    mOutputFilenames.clear();
-    std::copy(filenames.begin(),filenames.end(),mOutputFilenames.begin());
+  m_OutputFilenames.clear();
+  std::copy(filenames.begin(),filenames.end(),m_OutputFilenames.begin());
 }
 //--------------------------------------------------------------------
 
@@ -74,32 +129,33 @@ void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector<
 //--------------------------------------------------------------------
 std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename()
 {
-    assert(mOutputFilenames.size() == 1);
-    return mOutputFilenames.front();
+  assert(m_OutputFilenames.size() == 1);
+  return m_OutputFilenames.front();
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
 void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \
-        std::string& pixeltype,unsigned int& components)
+    std::string& pixeltype,unsigned int& components)
 {
-  if (mReadOnDisk && mInputFilenames.size()) {
+  if (m_ReadOnDisk && m_InputFilenames.size()) {
     int comp_temp,dim_temp; //clitkCommonImage takes ints
-    ReadImageDimensionAndPixelType(mInputFilenames[0], dim_temp, pixeltype,comp_temp);
-    components=comp_temp; dim=dim_temp;
-  }
-  else {
-    if (mInputVVImages.size()) {
-      pixeltype=mInputVVImages[0]->GetScalarTypeAsString();
-      dim=mInputVVImages[0]->GetNumberOfDimensions();
-      components=mInputVVImages[0]->GetNumberOfScalarComponents();
+    ReadImageDimensionAndPixelType(m_InputFilenames[0], dim_temp, pixeltype,comp_temp);
+    components=comp_temp;
+    dim=dim_temp;
+  } else {
+    if (m_InputVVImages.size()) {
+      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
     }
-    else
-    assert(false); //No input image, shouldn't happen
   }
-  if (mIOVerbose) {
-    std::cout << "Input is " << mDim << "D " << mPixelTypeName << "." << std::endl;
+  if (m_IOVerbose) {
+    std::cout << "Input is " << m_Dim << "D " << m_PixelTypeName << "." << std::endl;
   }
 }
 //--------------------------------------------------------------------
@@ -108,8 +164,8 @@ void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(un
 //--------------------------------------------------------------------
 vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
 {
-    assert(mOutputVVImages.size());
-    return mOutputVVImages[0];
+  assert(m_OutputVVImages.size());
+  return m_OutputVVImages[0];
 }
 //--------------------------------------------------------------------
 
@@ -117,7 +173,7 @@ vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage ()
 //--------------------------------------------------------------------
 std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVImages()
 {
-    return mOutputVVImages;
+  return m_OutputVVImages;
 }
 //--------------------------------------------------------------------
 
@@ -125,8 +181,13 @@ std::vector<vvImage::Pointer> clitk::ImageToImageGenericFilterBase::GetOutputVVI
 //--------------------------------------------------------------------
 void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input)
 {
-    mInputVVImages.clear();
-    mInputVVImages.push_back(input);
+  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);
 }
 //--------------------------------------------------------------------
 
@@ -134,7 +195,7 @@ void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer inp
 //--------------------------------------------------------------------
 void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input)
 {
-    mInputVVImages.push_back(input);
+  m_InputVVImages.push_back(input);
 }
 //--------------------------------------------------------------------
 
@@ -142,13 +203,19 @@ void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer inp
 //--------------------------------------------------------------------
 void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector<vvImage::Pointer> input)
 {
-    mInputVVImages=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);
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes() {
+void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes()
+{
   std::cout << GetAvailableImageTypes();
 }
 //--------------------------------------------------------------------
@@ -156,19 +223,23 @@ void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes() {
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::ImageTypeError() {
-  std::cerr << "**Error** The filter <" << mFilterName << "> is not available for " 
-            << mDim << "D images with pixel=" 
-            << mPixelTypeName << " and "
-            << mNbOfComponents << " component." << std::endl;
-  std::cerr << GetAvailableImageTypes();
-  exit(0);
+void clitk::ImageToImageGenericFilterBase::ImageTypeError()
+{
+  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);
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::SetImageTypeError() {
+void clitk::ImageToImageGenericFilterBase::SetImageTypeError()
+{
   std::cerr << "TODO ! " << std::endl;
   exit(0);
 }
@@ -176,22 +247,25 @@ void clitk::ImageToImageGenericFilterBase::SetImageTypeError() {
 
 
 //--------------------------------------------------------------------
-const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName() { 
-  return mFilterName; 
+const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName()
+{
+  return m_FilterName;
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n) { 
-  mFilterName = n; 
+void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n)
+{
+  m_FilterName = n;
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b) { 
-  mIOVerbose = b; 
+void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b)
+{
+  m_IOVerbose = b;
 }
 //--------------------------------------------------------------------
 
@@ -243,36 +317,57 @@ DEF_SetNextOutput_And_GetInput(double, 4);
 
 
 //--------------------------------------------------------------------
-template<class ImageType> 
-void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output) {
-  if (mOutputFilenames.size())
-    {
-      clitk::writeImage<ImageType>(output, mOutputFilenames.front(), mIOVerbose);
-      mOutputFilenames.pop_front();
-    }
-  if (mInputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
-    mOutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
+template<class ImageType>
+void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output)
+{
+  if (m_WriteOnDisk && m_OutputFilenames.size()) {
+    clitk::writeImage<ImageType>(output, m_OutputFilenames.front(), m_IOVerbose);
+    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
+    m_OutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
 }
 //--------------------------------------------------------------------
 
 
 //--------------------------------------------------------------------
-template<class ImageType> 
-typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n) {
-  if (mReadOnDisk && mInputFilenames.size() > n) {
-    return clitk::readImage<ImageType>(mInputFilenames[n], mIOVerbose);
+template<class ImageType>
+typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n)
+{
+  if (m_ReadOnDisk && m_InputFilenames.size() > n) {
+    return clitk::readImage<ImageType>(m_InputFilenames[n], m_IOVerbose);
+  } else {
+    if (m_InputVVImages.size() > n)
+      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);
+    }
   }
-  else {
-    if (mInputVVImages.size() > n)
-      return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(mInputVVImages[n]).GetPointer()));
-    else
-      {
-        assert(false); //No input, this shouldn't happen
-        return typename ImageType::Pointer(NULL);
-      }
+}
+//--------------------------------------------------------------------
+
+
+//--------------------------------------------------------------------
+// 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();
   }
 }
 //--------------------------------------------------------------------
 
 
+//--------------------------------------------------------------------
+