]> Creatis software - clitk.git/blob - common/vvImageReader.txx
Merge branch 'master' into extentSimon
[clitk.git] / common / vvImageReader.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
15   - BSD        See included LICENSE.txt file
16   - CeCILL-B   http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17   ===========================================================================**/
18
19 #ifndef VVIMAGEREADER_TXX
20 #define VVIMAGEREADER_TXX
21
22 #include <string>
23 #include <itkImageFileReader.h>
24 #include <itkImageSeriesReader.h>
25 #include <itkImageToVTKImageFilter.h>
26 #include <itkFlexibleVectorCastImageFilter.h>
27 #include "itkVectorImageToImageAdaptor.h"
28
29 #include <vtkTransform.h>
30
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)
37 {
38   if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME)
39   {
40     if (VImageDimension == 4)
41       UpdateWithDimAndInputVectorPixelType<itk::Vector<float,3>,VImageDimension>();
42     else
43       UpdateWithDimAndInputVectorPixelType<itk::Vector<float,VImageDimension>,VImageDimension>();
44   }
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>();
61   else
62     std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
63
64   if (mLastError.size()==0) {
65     //ReadNkiImageTransform();
66     ReadMatImageTransform();
67   }
68 }
69 //----------------------------------------------------------------------------
70
71
72 //----------------------------------------------------------------------------
73 template<class InputPixelType, unsigned int VImageDimension>
74 void vvImageReader::UpdateWithDimAndInputPixelType()
75 {
76   if (mType == MERGEDWITHTIME)   // In this case we can load the images
77     // one at the time to avoid excessive
78     // memory use
79   {
80     mImage=vvImage::New();
81
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);
88       try {
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;
95         error << err;
96         mLastError = error.str();
97         return;
98       }
99     }
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();
107
108     typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
109     typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
110
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);
120
121     typename FilterType::Pointer filter = FilterType::New();
122     filter->SetExtractionRegion(extractedRegion);
123     filter->SetInput(reader->GetOutput());
124     filter->ReleaseDataFlagOn();
125     filter->SetDirectionCollapseToSubmatrix();
126     try {
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;
132       return;
133     }
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]);
141     reader->Update();
142     typename InputImageType::Pointer input= reader->GetOutput();
143
144     typedef itk::VectorImageToImageAdaptor<InputPixelType, VImageDimension-1> ImageAdaptorType;
145     typename ImageAdaptorType::Pointer adaptor = ImageAdaptorType::New();
146     typename OutputImageType::Pointer output = OutputImageType::New();
147
148     adaptor->SetExtractComponentIndex(0);
149     adaptor->SetImage(input);
150
151     //Create the output
152     typename OutputImageType::IndexType index;
153     index.Fill(0);
154     typename OutputImageType::SizeType size;
155     size.Fill(input->GetNumberOfComponentsPerPixel());
156     typename OutputImageType::SpacingType spacing;
157     spacing.Fill(1);
158     typename OutputImageType::PointType origin;
159     origin.Fill(0);
160     for (unsigned int pixelDim=0; pixelDim<VImageDimension-1; ++pixelDim)
161     {
162       size[pixelDim]=adaptor->GetLargestPossibleRegion().GetSize(pixelDim);
163       spacing[pixelDim]=input->GetSpacing()[pixelDim];
164       origin[pixelDim]=input->GetOrigin()[pixelDim];
165     }
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);
172     output->Allocate();
173
174     //Copy each channel
175     for (unsigned int pixelDim=0; pixelDim<input->GetNumberOfComponentsPerPixel(); ++pixelDim)
176     {
177       adaptor->SetExtractComponentIndex(pixelDim);
178
179       itk::ImageRegionIterator<InputImageType> imageIterator(input,input->GetLargestPossibleRegion());
180
181       while(!imageIterator.IsAtEnd())
182       {
183         typename OutputImageType::IndexType indexVector;
184         indexVector.Fill(0);
185         for (unsigned int indexDim=0; indexDim<VImageDimension-1; ++indexDim)
186         {
187           indexVector[indexDim]=imageIterator.GetIndex().GetElement(indexDim);
188         }
189         indexVector[VImageDimension-1]=pixelDim;
190
191         output->SetPixel(indexVector, adaptor->GetPixel(imageIterator.GetIndex()));
192         ++imageIterator;
193       }
194     }
195
196     if (VImageDimension == 4)
197       mType == VECTORPIXELIMAGEWITHTIME;
198     else
199       mType == VECTORPIXELIMAGE;
200
201     try {
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;
207       return;
208     }
209   } else {
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();
216
217       try {
218         if (mType == IMAGEWITHTIME)
219         {
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;
223           return;
224         }
225         else
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;
230         error << err;
231         mLastError = error.str();
232         return;
233       }
234     } else {
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();
240
241       try {
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;
247         error << err;
248         mLastError = error.str();
249         return;
250       }
251     }
252   }
253 }
254 //----------------------------------------------------------------------------
255
256 //----------------------------------------------------------------------------
257 template<class InputPixelType, unsigned int VImageDimension>
258 void vvImageReader::UpdateWithDimAndInputVectorPixelType()
259 {
260   typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
261   typename InputImageType::Pointer input;
262
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();
268     try {
269       reader->Update();
270       input = reader->GetOutput();
271     } catch ( itk::ExceptionObject & err ) {
272       std::cerr << "Error while reading image series:" << err << std::endl;
273       std::stringstream error;
274       error << err;
275       mLastError = error.str();
276       return;
277     }
278   } else {
279     typedef itk::ImageFileReader<InputImageType> ReaderType;
280     typename ReaderType::Pointer reader = ReaderType::New();
281     reader->SetFileName(mInputFilenames[0]);
282     reader->ReleaseDataFlagOn();
283     try {
284       reader->Update();
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;
290       error << err;
291       mLastError = error.str();
292       return;
293     }
294   }
295   
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);
301   caster->Update();
302   casted_input = caster->GetOutput();
303   
304   mImage = vvImageFromITK<VImageDimension, itk::Vector<float, 3> >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
305 }
306 //----------------------------------------------------------------------------
307
308 #endif /* end #define vvImageReader_TXX */
309