]> Creatis software - clitk.git/blob - common/clitkImageToImageGenericFilter.h
43a62df8aaaabc969842ba388860fe03e6daec20
[clitk.git] / common / clitkImageToImageGenericFilter.h
1 #ifndef CLITKIMAGETOIMAGEGENERICFILTER_H
2 #define CLITKIMAGETOIMAGEGENERICFILTER_H
3
4 /**
5  ===================================================================
6  * @file   clitkImageToImageGenericFilter.h
7  * @author David Sarrut <David.Sarrut@creatis.insa-lyon.fr>
8  * @date   05 May 2008 14:40:51
9
10  * @brief  
11
12  ===================================================================*/
13
14 // clitk include
15 #include "clitkCommon.h"
16 #include "clitkImageCommon.h"
17 #include <itkImage.h>
18
19 #include <vvImage.h>
20 #include <vvFromITK.h>
21 #include <vvToITK.h>
22 #include <list>
23
24 namespace clitk {
25   
26   class ImageToImageGenericFilter: public itk::Object {
27     
28   public: 
29     // constructor - destructor
30     ImageToImageGenericFilter();
31
32     // Types
33     typedef ImageToImageGenericFilter     Self;
34     typedef Object                        Superclass;
35     typedef itk::SmartPointer<Self>       Pointer;
36     typedef itk::SmartPointer<const Self> ConstPointer;
37
38     // Generic IO
39     /// Returns the dimension and pixel type of the *first* input
40     void GetInputImageDimensionAndPixelType(unsigned int& dim, std::string& pixeltype,unsigned int & components);
41
42     // File IO
43     void SetInputFilename(const std::string & filename);
44     void AddInputFilename(const std::string & filename);
45     void SetInputFilenames(const std::vector<std::string> & filenames);
46     void SetOutputFilename(const std::string & filename);
47     void AddOutputFilename(const std::string & filename);
48     void SetOutputFilenames(const std::vector<std::string> & filenames);
49     std::string GetOutputFilename();
50     void SetIOVerbose(bool b) { mIOVerbose = b; }
51
52     // VVImage IO
53     void SetInputVVImage (vvImage::Pointer input);
54     void SetInputVVImages (std::vector<vvImage::Pointer> input);
55     void AddInputVVImage (vvImage::Pointer input);
56     vvImage::Pointer GetOutputVVImage ();
57     std::vector<vvImage::Pointer> GetOutputVVImages ();
58
59     /// Main function to implement
60     virtual void Update() = 0;
61     
62   protected:
63     /// Call this function to dispatch an output towards the correct sink
64     template<class ImageType> 
65         void SetNextOutput(typename ImageType::Pointer output);
66     /// Call this function to get the nth itk input image, regardless of input source
67     template<class ImageType> 
68         typename ImageType::Pointer GetInput(unsigned int n);
69
70     std::vector<std::string> mInputFilenames;
71     std::list<std::string> mOutputFilenames;
72
73     bool mIOVerbose;
74     unsigned int mDim;
75     std::string mPixelTypeName;
76     unsigned int mNbOfComponents;
77
78     std::vector<vvImage::Pointer> mInputVVImages;
79     std::vector<vvImage::Pointer> mOutputVVImages;
80
81   }; // end class clitk::ImageToImageGenericFilter
82
83 } // end namespace
84
85 template<class ImageType> 
86 void clitk::ImageToImageGenericFilter::SetNextOutput(typename ImageType::Pointer output)
87 {
88     if (mOutputFilenames.size())
89     {
90         clitk::writeImage<ImageType>(output, mOutputFilenames.front(), mIOVerbose);
91         mOutputFilenames.pop_front();
92     }
93     if (mInputVVImages.size()) //We assume that if a vv image is set as input, we want one as the output
94         mOutputVVImages.push_back(vvImageFromITK<ImageType::ImageDimension,typename ImageType::PixelType>(output));
95 }
96 template<class ImageType> 
97 typename ImageType::Pointer clitk::ImageToImageGenericFilter::GetInput(unsigned int n)
98 {
99     if (mInputFilenames.size() > n)
100         return clitk::readImage<ImageType>(mInputFilenames[n], mIOVerbose);
101     else if (mInputVVImages.size() > n)
102         return typename ImageType::Pointer(const_cast<ImageType*>(vvImageToITK<ImageType>(mInputVVImages[n]).GetPointer()));
103     else
104         assert(false); //No input, this shouldn't happen
105 }
106 #endif /* end #define CLITKIMAGETOIMAGEGENERICFILTER_H */
107