From: dsarrut Date: Tue, 2 Mar 2010 13:02:52 +0000 (+0000) Subject: - filter base (with trial to improve compilation time by explicit template in cxx) X-Git-Tag: v1.2.0~788 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=decc1d09983198c5b2f1cc1a14e0716fea452f39;p=clitk.git - filter base (with trial to improve compilation time by explicit template in cxx) --- diff --git a/common/clitkImageToImageGenericFilterBase.cxx b/common/clitkImageToImageGenericFilterBase.cxx new file mode 100644 index 0000000..fb61bbf --- /dev/null +++ b/common/clitkImageToImageGenericFilterBase.cxx @@ -0,0 +1,297 @@ +/*========================================================================= + + Program: vv + Module: $RCSfile: clitkImageToImageGenericFilterBase.cxx,v $ + Language: C++ + Date: $Date: 2010/03/02 13:02:52 $ + Version: $Revision: 1.1 $ + Author : Joel Schaerer + David Sarrut + + Copyright (C) 2008 + Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + CREATIS-LRMN http://www.creatis.insa-lyon.fr + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 3 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + =========================================================================*/ + +#include "clitkImageToImageGenericFilterBase.h" +#include + +//-------------------------------------------------------------------- +clitk::ImageToImageGenericFilterBase::~ImageToImageGenericFilterBase() {} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string n) + :mIOVerbose(false) { + mFilterName = n; + mFailOnImageTypeError = true; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::SetInputFilenames(const std::vector & filenames) { + mInputFilenames=filenames; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::SetInputFilename(const std::string & filename) { + std::vector f; + f.push_back(filename); + SetInputFilenames(f); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::AddInputFilename(const std::string & filename) { + mInputFilenames.push_back(filename); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::SetOutputFilename(const std::string & filename) { + mOutputFilenames.clear(); + mOutputFilenames.push_back(filename); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::AddOutputFilename(const std::string & filename) +{ + mOutputFilenames.push_back(filename); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::SetOutputFilenames(const std::vector & filenames) +{ + mOutputFilenames.clear(); + std::copy(filenames.begin(),filenames.end(),mOutputFilenames.begin()); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +std::string clitk::ImageToImageGenericFilterBase::GetOutputFilename() +{ + assert(mOutputFilenames.size() == 1); + return mOutputFilenames.front(); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::GetInputImageDimensionAndPixelType(unsigned int& dim, \ + std::string& pixeltype,unsigned int& components) +{ + if (mInputFilenames.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(); + } + else + assert(false); //No input image, shouldn't happen + + if (mIOVerbose) { + std::cout << "Input is " << mDim << "D " << mPixelTypeName << "." << std::endl; + } +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +vvImage::Pointer clitk::ImageToImageGenericFilterBase::GetOutputVVImage () +{ + assert(mOutputVVImages.size()); + return mOutputVVImages[0]; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +std::vector clitk::ImageToImageGenericFilterBase::GetOutputVVImages() +{ + return mOutputVVImages; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::SetInputVVImage (vvImage::Pointer input) +{ + mInputVVImages.clear(); + mInputVVImages.push_back(input); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::AddInputVVImage (vvImage::Pointer input) +{ + mInputVVImages.push_back(input); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::SetInputVVImages (std::vector input) +{ + mInputVVImages=input; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::PrintAvailableImageTypes() { + std::cout << GetAvailableImageTypes(); +} +//-------------------------------------------------------------------- + + + +//-------------------------------------------------------------------- +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::SetImageTypeError() { + std::cerr << "TODO ! " << std::endl; + exit(0); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +const std::string & clitk::ImageToImageGenericFilterBase::GetFilterName() { + return mFilterName; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::SetFilterName(std::string & n) { + mFilterName = n; +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +void clitk::ImageToImageGenericFilterBase::SetIOVerbose(bool b) { + mIOVerbose = b; +} +//-------------------------------------------------------------------- + +#define DEF_SetNextOutput_And_GetInput(PixelType, Dim) \ + template \ +void clitk::ImageToImageGenericFilterBase::SetNextOutput >(itk::Image::Pointer output); \ + template \ + itk::Image::Pointer clitk::ImageToImageGenericFilterBase::GetInput >(unsigned int n); + +#define DEF_SetNextOutput_And_GetInput_WithCompo(Compo, Dim) \ + template \ + void clitk::ImageToImageGenericFilterBase::SetNextOutput, Dim> >(itk::Image,Dim>::Pointer output); \ + template \ + itk::Image, Dim>::Pointer clitk::ImageToImageGenericFilterBase::GetInput, Dim> >(unsigned int n); + +DEF_SetNextOutput_And_GetInput(char, 2); +DEF_SetNextOutput_And_GetInput(unsigned char, 2); +DEF_SetNextOutput_And_GetInput(short, 2); +DEF_SetNextOutput_And_GetInput(unsigned short, 2); +DEF_SetNextOutput_And_GetInput(int, 2); +DEF_SetNextOutput_And_GetInput(float, 2); +DEF_SetNextOutput_And_GetInput(double, 2); + +DEF_SetNextOutput_And_GetInput(char, 3); +DEF_SetNextOutput_And_GetInput(unsigned char, 3); +DEF_SetNextOutput_And_GetInput(short, 3); +DEF_SetNextOutput_And_GetInput(unsigned short, 3); +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(char, 4); +DEF_SetNextOutput_And_GetInput(unsigned char, 4); +DEF_SetNextOutput_And_GetInput(short, 4); +DEF_SetNextOutput_And_GetInput(unsigned short, 4); +DEF_SetNextOutput_And_GetInput(int, 4); +DEF_SetNextOutput_And_GetInput(float, 4); +DEF_SetNextOutput_And_GetInput(double, 4); + + +//-------------------------------------------------------------------- +template +void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output) { + if (mOutputFilenames.size()) + { + clitk::writeImage(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(output)); +} +//-------------------------------------------------------------------- + + +//-------------------------------------------------------------------- +template +typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n) { + if (mInputFilenames.size() > n) { + return clitk::readImage(mInputFilenames[n], mIOVerbose); + } + else if (mInputVVImages.size() > n) + return typename ImageType::Pointer(const_cast(vvImageToITK(mInputVVImages[n]).GetPointer())); + else + { + assert(false); //No input, this shouldn't happen + return typename ImageType::Pointer(NULL); + } +} +//-------------------------------------------------------------------- + + + diff --git a/common/clitkImageToImageGenericFilterBase.h b/common/clitkImageToImageGenericFilterBase.h new file mode 100644 index 0000000..007f40b --- /dev/null +++ b/common/clitkImageToImageGenericFilterBase.h @@ -0,0 +1,132 @@ +/*========================================================================= + + Program: clitk + Module: $RCSfile: clitkImageToImageGenericFilterBase.h,v $ + Language: C++ + Date: $Date: 2010/03/02 13:02:52 $ + Version: $Revision: 1.1 $ + Author : Joel Schaerer + David Sarrut + + Copyright (C) 2008 + Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + CREATIS-LRMN http://www.creatis.insa-lyon.fr + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 3 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + =========================================================================*/ + +#ifndef CLITKIMAGETOIMAGEGENERICFILTERBASE_H +#define CLITKIMAGETOIMAGEGENERICFILTERBASE_H + +// clitk include +#include "clitkCommon.h" +#include "clitkImageCommon.h" +#include "clitkCommonGenericFilter.h" + +// itk include +#include + +// vv include +#include "vvImage.h" +#include "vvFromITK.h" +#include "vvToITK.h" + +namespace clitk { + + //-------------------------------------------------------------------- + class ImageToImageGenericFilterBase: public itk::Object { + + public: + // constructor - destructor + ImageToImageGenericFilterBase(std::string filterName); + virtual ~ImageToImageGenericFilterBase(); + + // Types + typedef ImageToImageGenericFilterBase Self; + typedef Object Superclass; + typedef itk::SmartPointer Pointer; + typedef itk::SmartPointer ConstPointer; + + // Filters information + const std::string & GetFilterName(); + void SetFilterName(std::string & n); + + // Generic IO + /// Returns the dimension and pixel type of the *first* input + void GetInputImageDimensionAndPixelType(unsigned int& dim, std::string& pixeltype, + unsigned int & components); + + // File IO + void SetInputFilename(const std::string & filename); + void AddInputFilename(const std::string & filename); + void SetInputFilenames(const std::vector & filenames); + void SetOutputFilename(const std::string & filename); + void AddOutputFilename(const std::string & filename); + void SetOutputFilenames(const std::vector & filenames); + std::string GetOutputFilename(); + void SetIOVerbose(bool b); + + // VVImage IO + void SetInputVVImage (vvImage::Pointer input); + void SetInputVVImages (std::vector input); + void AddInputVVImage (vvImage::Pointer input); + vvImage::Pointer GetOutputVVImage (); + std::vector GetOutputVVImages (); + + // Information on available image types + void PrintAvailableImageTypes(); + virtual std::string GetAvailableImageTypes() = 0; + + virtual bool CheckImageType(unsigned int dim,unsigned int ncomp, std::string pixeltype) = 0; + virtual bool CheckImageType() = 0; + + // Main function to call for using the filter. + virtual bool Update() = 0; + + protected: + /// Call this function to dispatch an output towards the correct sink + template + void SetNextOutput(typename ImageType::Pointer output); + + /// Call this function to get the nth itk input image, regardless of input source + template + typename ImageType::Pointer GetInput(unsigned int n); + + std::vector mInputFilenames; + std::list mOutputFilenames; + + bool mIOVerbose; + unsigned int mDim; + std::string mPixelTypeName; + unsigned int mNbOfComponents; + std::string mFilterName; + + std::vector mInputVVImages; + std::vector mOutputVVImages; + + void ImageTypeError(); + void SetImageTypeError(); + bool mFailOnImageTypeError; + + }; // end class clitk::ImageToImageGenericFilter + +#define ADD_VEC_IMAGE_TYPE(DIM, COMP, PT) this->mImageTypesManager.template AddNewDimensionAndPixelType(); +#define ADD_IMAGE_TYPE(DIM, PT) this->mImageTypesManager.template AddNewDimensionAndPixelType(); + +#include "clitkImageToImageGenericFilterBase.txx" + +} // end namespace + +#endif /* end #define CLITKIMAGETOIMAGEGENERICFILTERBASE_H */ + diff --git a/common/clitkImageToImageGenericFilterBase.txx b/common/clitkImageToImageGenericFilterBase.txx new file mode 100644 index 0000000..d37635e --- /dev/null +++ b/common/clitkImageToImageGenericFilterBase.txx @@ -0,0 +1,59 @@ +/*========================================================================= + + Program: vv + Module: $RCSfile: clitkImageToImageGenericFilterBase.txx,v $ + Language: C++ + Date: $Date: 2010/03/02 13:02:52 $ + Version: $Revision: 1.1 $ + Author : Joel Schaerer + David Sarrut + + Copyright (C) 2008 + Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + CREATIS-LRMN http://www.creatis.insa-lyon.fr + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, version 3 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + =========================================================================*/ + +// //-------------------------------------------------------------------- +// template +// void clitk::ImageToImageGenericFilterBase::SetNextOutput(typename ImageType::Pointer output) { +// if (mOutputFilenames.size()) +// { +// clitk::writeImage(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(output)); +// } +// //-------------------------------------------------------------------- + + +// //-------------------------------------------------------------------- +// template +// typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsigned int n) { +// if (mInputFilenames.size() > n) { +// return clitk::readImage(mInputFilenames[n], mIOVerbose); +// } +// else if (mInputVVImages.size() > n) +// return typename ImageType::Pointer(const_cast(vvImageToITK(mInputVVImages[n]).GetPointer())); +// else +// { +// assert(false); //No input, this shouldn't happen +// return typename ImageType::Pointer(NULL); +// } +// } +// //-------------------------------------------------------------------- + +