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>
27 #include "itkVectorImageToImageAdaptor.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 (mLastError.size()==0) {
65 //ReadNkiImageTransform();
66 ReadMatImageTransform();
69 //----------------------------------------------------------------------------
72 //----------------------------------------------------------------------------
73 template<class InputPixelType, unsigned int VImageDimension>
74 void vvImageReader::UpdateWithDimAndInputPixelType()
76 if (mType == MERGEDWITHTIME) // In this case we can load the images
77 // one at the time to avoid excessive
80 mImage=vvImage::New();
82 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
83 typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
84 typedef itk::ImageFileReader<InputImageType> ReaderType;
85 typename ReaderType::Pointer reader = ReaderType::New();
86 reader->ReleaseDataFlagOn();
87 reader->SetFileName(*i);
89 mImage->AddItkImage<InputImageType>(reader->GetOutput());
90 mImage->ComputeScalarRangeBase<InputPixelType, VImageDimension-1>(reader->GetOutput());
91 } catch ( itk::ExceptionObject & err ) {
92 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
93 << " " << err << std::endl;
94 std::stringstream error;
96 mLastError = error.str();
100 } else if (mType == SLICED) {
101 mImage=vvImage::New();
102 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
103 typedef itk::ImageFileReader<InputImageType> ReaderType;
104 typename ReaderType::Pointer reader = ReaderType::New();
105 reader->SetFileName(mInputFilenames[0]);
106 reader->UpdateOutputInformation();
108 typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
109 typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
111 typename InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
112 typename InputImageType::SizeType inputSize = inputRegion.GetSize();
113 typename InputImageType::IndexType start = inputRegion.GetIndex();
114 typename InputImageType::SizeType extractedRegionSize = inputSize;
115 typename InputImageType::RegionType extractedRegion;
116 extractedRegionSize[VImageDimension - 1] = 0;
117 extractedRegion.SetSize(extractedRegionSize);
118 start[VImageDimension - 1] = mSlice;
119 extractedRegion.SetIndex(start);
121 typename FilterType::Pointer filter = FilterType::New();
122 filter->SetExtractionRegion(extractedRegion);
123 filter->SetInput(reader->GetOutput());
124 filter->ReleaseDataFlagOn();
125 filter->SetDirectionCollapseToSubmatrix();
127 mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
128 mImage->ComputeScalarRangeBase<InputPixelType, VImageDimension-1>(filter->GetOutput());
129 } catch ( itk::ExceptionObject & err ) {
130 std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
131 << "(slice #" << mSlice << ") " << err << std::endl;
134 } else if (mType == VECTORPIXELIMAGE) {
135 mImage=vvImage::New();
136 typedef itk::VectorImage< InputPixelType, VImageDimension-1 > InputImageType;
137 typedef itk::ImageFileReader<InputImageType> ReaderType;
138 typedef itk::Image<InputPixelType, VImageDimension> OutputImageType;
139 typename ReaderType::Pointer reader = ReaderType::New();
140 reader->SetFileName(mInputFilenames[0]);
142 typename InputImageType::Pointer input= reader->GetOutput();
144 typedef itk::VectorImageToImageAdaptor<InputPixelType, VImageDimension-1> ImageAdaptorType;
145 typename ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
146 typename OutputImageType::Pointer output = OutputImageType::New();
148 adaptor->SetExtractComponentIndex(0);
149 adaptor->SetImage(input);
152 typename OutputImageType::IndexType index;
154 typename OutputImageType::SizeType size;
155 size.Fill(input->GetNumberOfComponentsPerPixel());
156 typename OutputImageType::SpacingType spacing;
158 typename OutputImageType::PointType origin;
160 for (unsigned int pixelDim=0; pixelDim<VImageDimension-1; ++pixelDim)
162 size[pixelDim]=adaptor->GetLargestPossibleRegion().GetSize(pixelDim);
163 spacing[pixelDim]=input->GetSpacing()[pixelDim];
164 origin[pixelDim]=input->GetOrigin()[pixelDim];
166 typename OutputImageType::RegionType region;
167 region.SetSize(size);
168 region.SetIndex(index);
169 output->SetRegions(region);
170 output->SetOrigin(origin);
171 output->SetSpacing(spacing);
175 for (unsigned int pixelDim=0; pixelDim<input->GetNumberOfComponentsPerPixel(); ++pixelDim)
177 adaptor->SetExtractComponentIndex(pixelDim);
179 itk::ImageRegionIterator<InputImageType> imageIterator(input,input->GetLargestPossibleRegion());
181 while(!imageIterator.IsAtEnd())
183 typename OutputImageType::IndexType indexVector;
185 for (unsigned int indexDim=0; indexDim<VImageDimension-1; ++indexDim)
187 indexVector[indexDim]=imageIterator.GetIndex().GetElement(indexDim);
189 indexVector[VImageDimension-1]=pixelDim;
191 output->SetPixel(indexVector, adaptor->GetPixel(imageIterator.GetIndex()));
196 if (VImageDimension == 4)
197 mType == VECTORPIXELIMAGEWITHTIME;
199 mType == VECTORPIXELIMAGE;
202 mImage = vvImageFromITK<VImageDimension,InputPixelType>(output, mType == VECTORPIXELIMAGEWITHTIME);
203 mImage->ComputeScalarRangeBase<InputPixelType, VImageDimension>(output);
204 } catch ( itk::ExceptionObject & err ) {
205 std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
206 << " " << err << std::endl;
210 if (mInputFilenames.size() > 1) {
211 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
212 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
213 typename ReaderType::Pointer reader = ReaderType::New();
214 reader->SetFileNames(mInputFilenames);
215 reader->ReleaseDataFlagOn();
218 if (mType == IMAGEWITHTIME)
220 std::cerr << "We should never come here:" << std::endl
221 << " Calling vvImageReader with multiple images and IMAGEWITHTIME is undefined." << std::endl
222 << " You are probably looking for MERGEDWITHTIME Type." << std::endl;
226 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
227 } catch ( itk::ExceptionObject & err ) {
228 std::cerr << "Error while reading image series:" << err << std::endl;
229 std::stringstream error;
231 mLastError = error.str();
235 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
236 typedef itk::ImageFileReader<InputImageType> ReaderType;
237 typename ReaderType::Pointer reader = ReaderType::New();
238 reader->SetFileName(mInputFilenames[0]);
239 reader->ReleaseDataFlagOn();
242 mImage = vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(), mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
243 } catch ( itk::ExceptionObject & err ) {
244 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
245 << " " << err << std::endl;
246 std::stringstream error;
248 mLastError = error.str();
254 //----------------------------------------------------------------------------
256 //----------------------------------------------------------------------------
257 template<class InputPixelType, unsigned int VImageDimension>
258 void vvImageReader::UpdateWithDimAndInputVectorPixelType()
260 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
261 typename InputImageType::Pointer input;
263 if (mInputFilenames.size() > 1) {
264 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
265 typename ReaderType::Pointer reader = ReaderType::New();
266 reader->SetFileNames(mInputFilenames);
267 reader->ReleaseDataFlagOn();
270 input = reader->GetOutput();
271 } catch ( itk::ExceptionObject & err ) {
272 std::cerr << "Error while reading image series:" << err << std::endl;
273 std::stringstream error;
275 mLastError = error.str();
279 typedef itk::ImageFileReader<InputImageType> ReaderType;
280 typename ReaderType::Pointer reader = ReaderType::New();
281 reader->SetFileName(mInputFilenames[0]);
282 reader->ReleaseDataFlagOn();
285 input = reader->GetOutput();
286 } catch ( itk::ExceptionObject & err ) {
287 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
288 << " " << err << std::endl;
289 std::stringstream error;
291 mLastError = error.str();
296 typedef itk::Image< itk::Vector<float , 3>, VImageDimension > VectorImageType;
297 typedef itk::FlexibleVectorCastImageFilter<InputImageType, VectorImageType> CasterType;
298 typename VectorImageType::Pointer casted_input;
299 typename CasterType::Pointer caster = CasterType::New();
300 caster->SetInput(input);
302 casted_input = caster->GetOutput();
304 mImage = vvImageFromITK<VImageDimension, itk::Vector<float, 3> >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
306 //----------------------------------------------------------------------------
308 #endif /* end #define vvImageReader_TXX */