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 <itkFlexibleVectorCastImageFilter.h>
28 #include <vtkTransform.h>
30 #include "clitkCommon.h"
31 #include "clitkConfiguration.h"
32 #include "vvFromITK.h"
33 //----------------------------------------------------------------------------
34 template<unsigned int VImageDimension>
35 void vvImageReader::UpdateWithDim(std::string InputPixelType)
37 if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME)
39 if (VImageDimension == 4)
40 UpdateWithDimAndInputVectorPixelType<itk::Vector<float,3>,VImageDimension>();
42 UpdateWithDimAndInputVectorPixelType<itk::Vector<float,VImageDimension>,VImageDimension>();
44 else if (InputPixelType == "short")
45 UpdateWithDimAndInputPixelType<short,VImageDimension>();
46 else if (InputPixelType == "unsigned_short")
47 UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
48 else if (InputPixelType == "char")
49 UpdateWithDimAndInputPixelType<char,VImageDimension>();
50 else if (InputPixelType == "unsigned_char")
51 UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
52 else if (InputPixelType == "int")
53 UpdateWithDimAndInputPixelType<int,VImageDimension>();
54 else if (InputPixelType == "unsigned_int")
55 UpdateWithDimAndInputPixelType<unsigned int,VImageDimension>();
56 else if (InputPixelType == "double")
57 UpdateWithDimAndInputPixelType<double,VImageDimension>();
58 else if (InputPixelType == "float")
59 UpdateWithDimAndInputPixelType<float,VImageDimension>();
61 std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
63 if (CLITK_EXPERIMENTAL && mLastError.size()==0) {
64 //ReadNkiImageTransform();
65 ReadMatImageTransform();
68 //----------------------------------------------------------------------------
71 //----------------------------------------------------------------------------
72 template<class InputPixelType, unsigned int VImageDimension>
73 void vvImageReader::UpdateWithDimAndInputPixelType()
75 if (mType == MERGEDWITHTIME) // In this case we can load the images
76 // one at the time to avoid excessive
79 mImage=vvImage::New();
81 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
82 typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
83 typedef itk::ImageFileReader<InputImageType> ReaderType;
84 typename ReaderType::Pointer reader = ReaderType::New();
85 reader->ReleaseDataFlagOn();
86 reader->SetFileName(*i);
88 mImage->AddItkImage<InputImageType>(reader->GetOutput());
89 mImage->ComputeScalarRangeBase<InputPixelType, VImageDimension-1>(reader->GetOutput());
90 } catch ( itk::ExceptionObject & err ) {
91 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
92 << " " << err << std::endl;
93 std::stringstream error;
95 mLastError = error.str();
99 } else if (mType == SLICED) {
100 mImage=vvImage::New();
101 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
102 typedef itk::ImageFileReader<InputImageType> ReaderType;
103 typename ReaderType::Pointer reader = ReaderType::New();
104 reader->SetFileName(mInputFilenames[0]);
105 reader->UpdateOutputInformation();
107 typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
108 typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
110 typename InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
111 typename InputImageType::SizeType inputSize = inputRegion.GetSize();
112 typename InputImageType::IndexType start = inputRegion.GetIndex();
113 typename InputImageType::SizeType extractedRegionSize = inputSize;
114 typename InputImageType::RegionType extractedRegion;
115 extractedRegionSize[VImageDimension - 1] = 0;
116 extractedRegion.SetSize(extractedRegionSize);
117 start[VImageDimension - 1] = mSlice;
118 extractedRegion.SetIndex(start);
120 typename FilterType::Pointer filter = FilterType::New();
121 filter->SetExtractionRegion(extractedRegion);
122 filter->SetInput(reader->GetOutput());
123 filter->ReleaseDataFlagOn();
124 #if ITK_VERSION_MAJOR == 4
125 filter->SetDirectionCollapseToSubmatrix();
128 mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
129 mImage->ComputeScalarRangeBase<InputPixelType, VImageDimension-1>(filter->GetOutput());
130 } catch ( itk::ExceptionObject & err ) {
131 std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
132 << "(slice #" << mSlice << ") " << err << std::endl;
136 if (mInputFilenames.size() > 1) {
137 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
138 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
139 typename ReaderType::Pointer reader = ReaderType::New();
140 reader->SetFileNames(mInputFilenames);
141 reader->ReleaseDataFlagOn();
144 if (mType == IMAGEWITHTIME)
146 std::cerr << "We should never come here:" << std::endl
147 << " Calling vvImageReader with multiple images and IMAGEWITHTIME is undefined." << std::endl
148 << " You are probably looking for MERGEDWITHTIME Type." << std::endl;
152 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
153 } catch ( itk::ExceptionObject & err ) {
154 std::cerr << "Error while reading image series:" << err << std::endl;
155 std::stringstream error;
157 mLastError = error.str();
161 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
162 typedef itk::ImageFileReader<InputImageType> ReaderType;
163 typename ReaderType::Pointer reader = ReaderType::New();
164 reader->SetFileName(mInputFilenames[0]);
165 reader->ReleaseDataFlagOn();
168 mImage = vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(), mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
169 } catch ( itk::ExceptionObject & err ) {
170 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
171 << " " << err << std::endl;
172 std::stringstream error;
174 mLastError = error.str();
180 //----------------------------------------------------------------------------
182 //----------------------------------------------------------------------------
183 template<class InputPixelType, unsigned int VImageDimension>
184 void vvImageReader::UpdateWithDimAndInputVectorPixelType()
186 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
187 typename InputImageType::Pointer input;
189 if (mInputFilenames.size() > 1) {
190 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
191 typename ReaderType::Pointer reader = ReaderType::New();
192 reader->SetFileNames(mInputFilenames);
193 reader->ReleaseDataFlagOn();
196 input = reader->GetOutput();
197 } catch ( itk::ExceptionObject & err ) {
198 std::cerr << "Error while reading image series:" << err << std::endl;
199 std::stringstream error;
201 mLastError = error.str();
205 typedef itk::ImageFileReader<InputImageType> ReaderType;
206 typename ReaderType::Pointer reader = ReaderType::New();
207 reader->SetFileName(mInputFilenames[0]);
208 reader->ReleaseDataFlagOn();
211 input = reader->GetOutput();
212 } catch ( itk::ExceptionObject & err ) {
213 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
214 << " " << err << std::endl;
215 std::stringstream error;
217 mLastError = error.str();
222 typedef itk::Image< itk::Vector<float , 3>, VImageDimension > VectorImageType;
223 typedef itk::FlexibleVectorCastImageFilter<InputImageType, VectorImageType> CasterType;
224 typename VectorImageType::Pointer casted_input;
225 typename CasterType::Pointer caster = CasterType::New();
226 caster->SetInput(input);
228 casted_input = caster->GetOutput();
230 mImage = vvImageFromITK<VImageDimension, itk::Vector<float, 3> >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
232 //----------------------------------------------------------------------------
234 #endif /* end #define vvImageReader_TXX */