/*========================================================================= 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 ===========================================================================**/ #ifndef CLITKIMAGECONVERTGENERICFILTER_H #define CLITKIMAGECONVERTGENERICFILTER_H /** =================================================================== * @file clitkImageConvertGenericFilter.h * @author David Sarrut * @date 05 May 2008 10:40:24 * @brief ===================================================================*/ // clitk include #include "clitkImageToImageGenericFilter.h" // itk include #include "itkCastImageFilter.h" #if ( ITK_VERSION_MAJOR < 5 ) #include "itkVectorCastImageFilter.h" #endif namespace clitk { template class ImageConvertTraits { public: enum { IS_VECTOR = false }; ImageConvertTraits() { TPixel p = "SCALAR"; } }; template < class TPixel, unsigned int Comp > class ImageConvertTraits< itk::Vector > { public: enum { IS_VECTOR = true }; }; class ImageConvertGenericFilter: public clitk::ImageToImageGenericFilter { public: // constructor - destructor ImageConvertGenericFilter(); // Types typedef ImageConvertGenericFilter Self; typedef itk::SmartPointer Pointer; typedef itk::SmartPointer ConstPointer; // New itkNewMacro(Self); // Members functions std::string GetInputPixelTypeName() { return m_PixelTypeName; } std::string GetOutputPixelTypeName() { return mOutputPixelTypeName; } void SetOutputPixelType(std::string p) { mOutputPixelTypeName = p; } void SetVV(bool b) { mVV = b; } void SetNoNiiMeta(bool b) { mNoNiiMeta = b; } bool IsWarningOccur() { return mWarningOccur; } std::string & GetWarning() { return mWarning; } void EnableDisplayWarning(bool b) { mDisplayWarning = b; } void SetCorrectNegativeSpacingFlag(bool b) { mCorrectNegativeSpacingFlag = b; } //-------------------------------------------------------------------- // Main function called each time the filter is updated template void UpdateWithInputImageType(); template void CheckTypes(std::string inType, std::string outType); protected: template void InitializeImageType(); std::string mOutputPixelTypeName; std::string mWarning; bool mWarningOccur; bool mDisplayWarning; bool mVV; bool mNoNiiMeta; bool mCorrectNegativeSpacingFlag; private: template class UpdateWithSelectiveOutputType { public: static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType) { if (IsSameType(outputPixelType)) UpdateWithOutputType(filter); else if (IsSameType(outputPixelType)) UpdateWithOutputType(filter); else if (IsSameType(outputPixelType)) UpdateWithOutputType(filter); else if (IsSameType(outputPixelType)) UpdateWithOutputType(filter); else if (IsSameType(outputPixelType)) UpdateWithOutputType(filter); else if (IsSameType(outputPixelType)) UpdateWithOutputType(filter); else if (IsSameType(outputPixelType)) UpdateWithOutputType(filter); else { std::string list = CreateListOfTypes(); std::cerr << "Error, I don't know the vector output type '" << outputPixelType << "'. " << std::endl << "Known types are " << list << "." << std::endl; return false; } return true; } private: template static void UpdateWithOutputType(ImageConvertGenericFilter& filter) { // Read typename InputImageType::Pointer input =filter.template GetInput(0); // Typedef typedef typename InputImageType::PixelType PixelType; // Warning filter.CheckTypes(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName()); // Cast typedef itk::Image OutputImageType; typedef itk::CastImageFilter FilterType; typename FilterType::Pointer cast_filter = FilterType::New(); cast_filter->SetInput(input); cast_filter->Update(); // Write filter.SetNextOutput(cast_filter->GetOutput()); } }; template class UpdateWithSelectiveOutputType { public: static bool Run(ImageConvertGenericFilter& filter, std::string outputPixelType) { /* // RP: future conversions? if (IsSameType(outputPixelType)) UpdateWithOutputVectorType(); else if (IsSameType(outputPixelType)) UpdateWithOutputVectorType(); else if (IsSameType(outputPixelType)) UpdateWithOutputVectorType(); else if (IsSameType(outputPixelType)) UpdateWithOutputVectorType(); else if (IsSameType(outputPixelType)) UpdateWithOutputVectorType(); else */ if (IsSameType(outputPixelType)) UpdateWithOutputVectorType(filter); else if (IsSameType(outputPixelType)) UpdateWithOutputVectorType(filter); else { std::string list = CreateListOfTypes(); std::cerr << "Error, I don't know the vector output type '" << outputPixelType << "'. " << std::endl << "Known types are " << list << "." << std::endl; return false; } return true; } private: template static void UpdateWithOutputVectorType(ImageConvertGenericFilter& filter) { // Read typename InputImageType::Pointer input =filter.template GetInput(0); // Typedef typedef typename InputImageType::PixelType::ValueType PixelType; // Warning filter.CheckTypes(filter.GetInputPixelTypeName(), filter.GetOutputPixelTypeName()); // Cast typedef itk::Image, InputImageType::ImageDimension> OutputImageType; #if ( ITK_VERSION_MAJOR < 5 ) typedef itk::VectorCastImageFilter FilterType; #else typedef itk::CastImageFilter FilterType; #endif typename FilterType::Pointer cast_filter = FilterType::New(); cast_filter->SetInput(input); cast_filter->Update(); // Write filter.SetNextOutput(cast_filter->GetOutput()); } }; }; // end class ImageConvertGenericFilter //#include "clitkImageConvertGenericFilter.txx" } // end namespace #endif /* end #define CLITKIMAGECONVERTGENERICFILTER_H */