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 } catch ( itk::ExceptionObject & err ) {
90 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
91 << " " << err << std::endl;
92 std::stringstream error;
94 mLastError = error.str();
98 } else if (mType == SLICED) {
99 mImage=vvImage::New();
100 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
101 typedef itk::ImageFileReader<InputImageType> ReaderType;
102 typename ReaderType::Pointer reader = ReaderType::New();
103 reader->SetFileName(mInputFilenames[0]);
104 reader->UpdateOutputInformation();
106 typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
107 typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
109 typename InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
110 typename InputImageType::SizeType inputSize = inputRegion.GetSize();
111 typename InputImageType::IndexType start = inputRegion.GetIndex();
112 typename InputImageType::SizeType extractedRegionSize = inputSize;
113 typename InputImageType::RegionType extractedRegion;
114 extractedRegionSize[VImageDimension - 1] = 0;
115 extractedRegion.SetSize(extractedRegionSize);
116 start[VImageDimension - 1] = mSlice;
117 extractedRegion.SetIndex(start);
119 typename FilterType::Pointer filter = FilterType::New();
120 filter->SetExtractionRegion(extractedRegion);
121 filter->SetInput(reader->GetOutput());
122 filter->ReleaseDataFlagOn();
123 #if ITK_VERSION_MAJOR == 4
124 filter->SetDirectionCollapseToSubmatrix();
127 mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
128 } catch ( itk::ExceptionObject & err ) {
129 std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
130 << "(slice #" << mSlice << ") " << err << std::endl;
134 if (mInputFilenames.size() > 1) {
135 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
136 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
137 typename ReaderType::Pointer reader = ReaderType::New();
138 reader->SetFileNames(mInputFilenames);
139 reader->ReleaseDataFlagOn();
142 if (mType == IMAGEWITHTIME)
144 std::cerr << "We should never come here:" << std::endl
145 << " Calling vvImageReader with multiple images and IMAGEWITHTIME is undefined." << std::endl
146 << " You are probably looking for MERGEDWITHTIME Type." << std::endl;
150 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
151 } catch ( itk::ExceptionObject & err ) {
152 std::cerr << "Error while reading image series:" << err << std::endl;
153 std::stringstream error;
155 mLastError = error.str();
159 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
160 typedef itk::ImageFileReader<InputImageType> ReaderType;
161 typename ReaderType::Pointer reader = ReaderType::New();
162 reader->SetFileName(mInputFilenames[0]);
163 reader->ReleaseDataFlagOn();
166 mImage = vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(), mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
167 } catch ( itk::ExceptionObject & err ) {
168 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
169 << " " << err << std::endl;
170 std::stringstream error;
172 mLastError = error.str();
178 //----------------------------------------------------------------------------
180 //----------------------------------------------------------------------------
181 template<class InputPixelType, unsigned int VImageDimension>
182 void vvImageReader::UpdateWithDimAndInputVectorPixelType()
184 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
185 typename InputImageType::Pointer input;
187 if (mInputFilenames.size() > 1) {
188 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
189 typename ReaderType::Pointer reader = ReaderType::New();
190 reader->SetFileNames(mInputFilenames);
191 reader->ReleaseDataFlagOn();
194 input = reader->GetOutput();
195 } catch ( itk::ExceptionObject & err ) {
196 std::cerr << "Error while reading image series:" << err << std::endl;
197 std::stringstream error;
199 mLastError = error.str();
203 typedef itk::ImageFileReader<InputImageType> ReaderType;
204 typename ReaderType::Pointer reader = ReaderType::New();
205 reader->SetFileName(mInputFilenames[0]);
206 reader->ReleaseDataFlagOn();
209 input = reader->GetOutput();
210 } catch ( itk::ExceptionObject & err ) {
211 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
212 << " " << err << std::endl;
213 std::stringstream error;
215 mLastError = error.str();
220 typedef itk::Image< itk::Vector<float , 3>, VImageDimension > VectorImageType;
221 typedef itk::FlexibleVectorCastImageFilter<InputImageType, VectorImageType> CasterType;
222 typename VectorImageType::Pointer casted_input;
223 typename CasterType::Pointer caster = CasterType::New();
224 caster->SetInput(input);
226 casted_input = caster->GetOutput();
228 mImage = vvImageFromITK<VImageDimension, itk::Vector<float, 3> >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
230 //----------------------------------------------------------------------------
232 #endif /* end #define vvImageReader_TXX */