1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
19 #ifndef VVIMAGEREADER_TXX
20 #define VVIMAGEREADER_TXX
23 #include <itkImageFileReader.h>
24 #include <itkImageSeriesReader.h>
25 #include <itkImageToVTKImageFilter.h>
26 #include <itkAnalyzeImageIO.h>
27 #include <itkFlexibleVectorCastImageFilter.h>
29 #include <vtkTransform.h>
31 #include "clitkCommon.h"
32 #include "clitkConfiguration.h"
33 #include "vvFromITK.h"
34 //----------------------------------------------------------------------------
35 template<unsigned int VImageDimension>
36 void vvImageReader::UpdateWithDim(std::string InputPixelType)
38 if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME)
40 if (VImageDimension == 4)
41 UpdateWithDimAndInputVectorPixelType<itk::Vector<float,3>,VImageDimension>();
43 UpdateWithDimAndInputVectorPixelType<itk::Vector<float,VImageDimension>,VImageDimension>();
45 else if (InputPixelType == "short")
46 UpdateWithDimAndInputPixelType<short,VImageDimension>();
47 else if (InputPixelType == "unsigned_short")
48 UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
49 else if (InputPixelType == "char")
50 UpdateWithDimAndInputPixelType<char,VImageDimension>();
51 else if (InputPixelType == "unsigned_char")
52 UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
53 else if (InputPixelType == "int")
54 UpdateWithDimAndInputPixelType<int,VImageDimension>();
55 else if (InputPixelType == "unsigned_int")
56 UpdateWithDimAndInputPixelType<unsigned int,VImageDimension>();
57 else if (InputPixelType == "double")
58 UpdateWithDimAndInputPixelType<double,VImageDimension>();
59 else if (InputPixelType == "float")
60 UpdateWithDimAndInputPixelType<float,VImageDimension>();
62 std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
64 if (CLITK_EXPERIMENTAL && mLastError.size()==0) {
65 //ReadNkiImageTransform();
66 ReadMatImageTransform();
69 //----------------------------------------------------------------------------
72 //----------------------------------------------------------------------------
73 template<class InputPixelType, unsigned int VImageDimension>
74 void vvImageReader::UpdateWithDimAndInputPixelType()
76 itk::AnalyzeImageIO *analyzeImageIO = NULL;
78 if (mType == MERGEDWITHTIME) // In this case we can load the images
79 // one at the time to avoid excessive
82 mImage=vvImage::New();
84 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
85 typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
86 typedef itk::ImageFileReader<InputImageType> ReaderType;
87 typename ReaderType::Pointer reader = ReaderType::New();
88 reader->ReleaseDataFlagOn();
89 reader->SetFileName(*i);
91 mImage->AddItkImage<InputImageType>(reader->GetOutput());
92 } catch ( itk::ExceptionObject & err ) {
93 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
94 << " " << err << std::endl;
95 std::stringstream error;
97 mLastError = error.str();
100 analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
102 } else if (mType == SLICED) {
103 mImage=vvImage::New();
104 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
105 typedef itk::ImageFileReader<InputImageType> ReaderType;
106 typename ReaderType::Pointer reader = ReaderType::New();
107 reader->SetFileName(mInputFilenames[0]);
108 reader->UpdateOutputInformation();
110 typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
111 typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
113 typename InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
114 typename InputImageType::SizeType inputSize = inputRegion.GetSize();
115 typename InputImageType::IndexType start = inputRegion.GetIndex();
116 typename InputImageType::SizeType extractedRegionSize = inputSize;
117 typename InputImageType::RegionType extractedRegion;
118 extractedRegionSize[VImageDimension - 1] = 0;
119 extractedRegion.SetSize(extractedRegionSize);
120 start[VImageDimension - 1] = mSlice;
121 extractedRegion.SetIndex(start);
123 typename FilterType::Pointer filter = FilterType::New();
124 filter->SetExtractionRegion(extractedRegion);
125 filter->SetInput(reader->GetOutput());
126 filter->ReleaseDataFlagOn();
127 #if ITK_VERSION_MAJOR == 4
128 filter->SetDirectionCollapseToSubmatrix();
131 mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
132 } catch ( itk::ExceptionObject & err ) {
133 std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
134 << "(slice #" << mSlice << ") " << err << std::endl;
137 analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
139 if (mInputFilenames.size() > 1) {
140 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
141 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
142 typename ReaderType::Pointer reader = ReaderType::New();
143 reader->SetFileNames(mInputFilenames);
144 reader->ReleaseDataFlagOn();
147 if (mType == IMAGEWITHTIME)
149 std::cerr << "We should never come here:" << std::endl
150 << " Calling vvImageReader with multiple images and IMAGEWITHTIME is undefined." << std::endl
151 << " You are probably looking for MERGEDWITHTIME Type." << std::endl;
155 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
156 } catch ( itk::ExceptionObject & err ) {
157 std::cerr << "Error while reading image series:" << err << std::endl;
158 std::stringstream error;
160 mLastError = error.str();
164 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
165 typedef itk::ImageFileReader<InputImageType> ReaderType;
166 typename ReaderType::Pointer reader = ReaderType::New();
167 reader->SetFileName(mInputFilenames[0]);
168 reader->ReleaseDataFlagOn();
171 mImage = vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(), mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
172 } catch ( itk::ExceptionObject & err ) {
173 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
174 << " " << err << std::endl;
175 std::stringstream error;
177 mLastError = error.str();
180 analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
184 // For unknown analyze orientations, we set identity
186 const double m[16] = {1.,0.,0.,0.,
190 // TODO SR and BP: check on the list of transforms and not the first only
192 for(i=0; i<16 && m[i]==mImage->GetTransform()[0]->GetMatrix()->GetElement(i%4, i/4); i++);
194 itkWarningMacro(<< "Analyze image file format detected with unknown orientation. "
195 << "Forcing identity orientation, use other file format if not ok.");
196 mImage->GetTransform()[0]->Identity();
200 //----------------------------------------------------------------------------
202 //----------------------------------------------------------------------------
203 template<class InputPixelType, unsigned int VImageDimension>
204 void vvImageReader::UpdateWithDimAndInputVectorPixelType()
206 itk::AnalyzeImageIO *analyzeImageIO = NULL;
208 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
209 typename InputImageType::Pointer input;
211 if (mInputFilenames.size() > 1) {
212 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
213 typename ReaderType::Pointer reader = ReaderType::New();
214 reader->SetFileNames(mInputFilenames);
215 reader->ReleaseDataFlagOn();
218 input = reader->GetOutput();
219 } catch ( itk::ExceptionObject & err ) {
220 std::cerr << "Error while reading image series:" << err << std::endl;
221 std::stringstream error;
223 mLastError = error.str();
227 typedef itk::ImageFileReader<InputImageType> ReaderType;
228 typename ReaderType::Pointer reader = ReaderType::New();
229 reader->SetFileName(mInputFilenames[0]);
230 reader->ReleaseDataFlagOn();
233 input = reader->GetOutput();
234 } catch ( itk::ExceptionObject & err ) {
235 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
236 << " " << err << std::endl;
237 std::stringstream error;
239 mLastError = error.str();
242 analyzeImageIO = dynamic_cast<itk::AnalyzeImageIO*>( reader->GetImageIO() );
245 typedef itk::Image< itk::Vector<float , 3>, VImageDimension > VectorImageType;
246 typedef itk::FlexibleVectorCastImageFilter<InputImageType, VectorImageType> CasterType;
247 typename VectorImageType::Pointer casted_input;
248 typename CasterType::Pointer caster = CasterType::New();
249 caster->SetInput(input);
251 casted_input = caster->GetOutput();
253 mImage = vvImageFromITK<VImageDimension, itk::Vector<float, 3> >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
255 // For unknown analyze orientations, we set identity
258 const double m[16] = {1.,0.,0.,0.,
263 for (i = 0; i < 16 && m[i] == mImage->GetTransform()[0]->GetMatrix()->GetElement(i % 4, i / 4); i++)
267 itkWarningMacro(<< "Analyze image file format detected with unknown orientation. "
268 << "Forcing identity orientation, use other file format if not ok.");
269 mImage->GetTransform()[0]->Identity();
273 //----------------------------------------------------------------------------
275 #endif /* end #define vvImageReader_TXX */