]> Creatis software - clitk.git/blob - common/vvImageReader.txx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk into VTK6_Qt5
[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
28 #include <vtkTransform.h>
29
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)
36 {
37   if (mType == VECTORFIELD || mType == VECTORFIELDWITHTIME)
38   {
39     if (VImageDimension == 4)
40       UpdateWithDimAndInputVectorPixelType<itk::Vector<float,3>,VImageDimension>();
41     else
42       UpdateWithDimAndInputVectorPixelType<itk::Vector<float,VImageDimension>,VImageDimension>();
43   }
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>();
60   else
61     std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
62
63   if (mLastError.size()==0) {
64     //ReadNkiImageTransform();
65     ReadMatImageTransform();
66   }
67 }
68 //----------------------------------------------------------------------------
69
70
71 //----------------------------------------------------------------------------
72 template<class InputPixelType, unsigned int VImageDimension>
73 void vvImageReader::UpdateWithDimAndInputPixelType()
74 {
75   if (mType == MERGEDWITHTIME)   // In this case we can load the images
76     // one at the time to avoid excessive
77     // memory use
78   {
79     mImage=vvImage::New();
80
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);
87       try {
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;
94         error << err;
95         mLastError = error.str();
96         return;
97       }
98     }
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();
106
107     typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
108     typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
109
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);
119
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();
126 #endif
127     try {
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;
133       return;
134     }
135   } else {
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();
142
143       try {
144         if (mType == IMAGEWITHTIME)
145         {
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;
149           return;
150         }
151         else
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;
156         error << err;
157         mLastError = error.str();
158         return;
159       }
160     } else {
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();
166
167       try {
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;
173         error << err;
174         mLastError = error.str();
175         return;
176       }
177     }
178   }
179 }
180 //----------------------------------------------------------------------------
181
182 //----------------------------------------------------------------------------
183 template<class InputPixelType, unsigned int VImageDimension>
184 void vvImageReader::UpdateWithDimAndInputVectorPixelType()
185 {
186   typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
187   typename InputImageType::Pointer input;
188
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();
194     try {
195       reader->Update();
196       input = reader->GetOutput();
197     } catch ( itk::ExceptionObject & err ) {
198       std::cerr << "Error while reading image series:" << err << std::endl;
199       std::stringstream error;
200       error << err;
201       mLastError = error.str();
202       return;
203     }
204   } else {
205     typedef itk::ImageFileReader<InputImageType> ReaderType;
206     typename ReaderType::Pointer reader = ReaderType::New();
207     reader->SetFileName(mInputFilenames[0]);
208     reader->ReleaseDataFlagOn();
209     try {
210       reader->Update();
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;
216       error << err;
217       mLastError = error.str();
218       return;
219     }
220   }
221   
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);
227   caster->Update();
228   casted_input = caster->GetOutput();
229   
230   mImage = vvImageFromITK<VImageDimension, itk::Vector<float, 3> >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
231 }
232 //----------------------------------------------------------------------------
233
234 #endif /* end #define vvImageReader_TXX */
235