]> Creatis software - clitk.git/blob - vv/vvImageReader.txx
29b4f63567811b3f720c4fef6dd36b0f4bd9814c
[clitk.git] / vv / vvImageReader.txx
1 #ifndef vvImageReader_TXX
2 #define vvImageReader_TXX
3 #include<string>
4
5 #include <itkImageFileReader.h>
6 #include <itkImageSeriesReader.h>
7
8 #include "clitkCommon.h"
9 #include "itkImageToVTKImageFilter.h"
10 #include "vvFromITK.h"
11 #include "vvConstants.h"
12
13 template<unsigned int VImageDimension>
14 void vvImageReader::UpdateWithDim(std::string InputPixelType)
15 {
16     if (mType == VECTORFIELD)
17         UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
18     else if (InputPixelType == "short")
19         UpdateWithDimAndInputPixelType<short,VImageDimension>();
20     else if (InputPixelType == "unsigned_short")
21         UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
22     else if (InputPixelType == "char")
23         UpdateWithDimAndInputPixelType<char,VImageDimension>();
24     else if (InputPixelType == "unsigned_char")
25         UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
26     else if (InputPixelType == "int")
27         UpdateWithDimAndInputPixelType<int,VImageDimension>();
28     else if (InputPixelType == "double")
29         UpdateWithDimAndInputPixelType<double,VImageDimension>();
30     else if (InputPixelType == "float")
31         UpdateWithDimAndInputPixelType<float,VImageDimension>();
32     else
33         std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
34 }
35 //====================================================================
36
37 template<class InputPixelType, unsigned int VImageDimension>
38 void vvImageReader::UpdateWithDimAndInputPixelType()
39 {
40     if (mType == MERGEDWITHTIME)   // In this case we can load the images
41                                    // one at the time to avoid excessive
42                                    // memory use
43     {
44         typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
45         typedef itk::ImageFileReader<InputImageType> ReaderType;
46         typename ReaderType::Pointer reader = ReaderType::New();
47         typedef itk::ImageToVTKImageFilter <InputImageType> ConnectorType;
48         typename ConnectorType::Pointer connector = ConnectorType::New();
49         connector->SetInput(reader->GetOutput());
50         mImage=vvImage::New();
51         for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
52         {
53             std::cout << (*i) << std::endl;
54             reader->SetFileName(*i);
55             try {
56                 reader->Update();
57             }
58             catch ( itk::ExceptionObject & err ) {
59                 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
60                     << " " << err << std::endl;
61                 std::stringstream error;
62                 error << err;
63                 mLastError = error.str();
64                 return;
65             }
66             try {
67                 connector->Update();
68             }
69             catch ( itk::ExceptionObject & err ) {
70                 std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
71                     << " " << err << std::endl;
72             }
73             vtkImageData *image = vtkImageData::New();
74             image->DeepCopy(connector->GetOutput());
75             mImage->AddImage(image);
76         }
77     }
78     else
79     {
80         if (mInputFilenames.size() > 1)
81         {
82             typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
83             typedef itk::ImageSeriesReader<InputImageType> ReaderType;
84             typename ReaderType::Pointer reader = ReaderType::New();
85             for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
86                 std::cout << (*i) << std::endl;
87             reader->SetFileNames(mInputFilenames);
88             //if (mUseAnObserver) {
89                 //reader->AddObserver(itk::ProgressEvent(), mObserver);
90             //}
91             try {
92                 reader->Update();
93             }
94             catch ( itk::ExceptionObject & err ) {
95                 std::cerr << "Error while reading image series:" << err << std::endl;
96                 std::stringstream error;
97                 error << err;
98                 mLastError = error.str();
99                 return;
100             }
101             if (mType == IMAGEWITHTIME)
102                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
103             else
104                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
105         }
106         else
107         {
108             typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
109             typedef itk::ImageFileReader<InputImageType> ReaderType;
110             typename ReaderType::Pointer reader = ReaderType::New();
111             reader->SetFileName(mInputFilenames[0]);
112             //if (mUseAnObserver) {
113                 //reader->AddObserver(itk::ProgressEvent(), mObserver);
114             //}
115             try {
116                 reader->Update();
117             }
118             catch ( itk::ExceptionObject & err ) {
119                 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
120                     << " " << err << std::endl;
121                 std::stringstream error;
122                 error << err;
123                 mLastError = error.str();
124                 return;
125             }
126             if (mType == IMAGEWITHTIME)
127                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
128             else
129                 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
130         }
131     }
132 }
133 //====================================================================
134
135
136 #endif /* end #define vvImageReader_TXX */
137