]> Creatis software - clitk.git/blob - common/vvImageReader.txx
551e42626ba7313a84bb687f2fe8322c78d93962
[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     filter->SetDirectionCollapseToSubmatrix();
125     try {
126       mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
127       mImage->ComputeScalarRangeBase<InputPixelType, VImageDimension-1>(filter->GetOutput());
128     } catch ( itk::ExceptionObject & err ) {
129       std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
130                 << "(slice #" << mSlice << ") " << err << std::endl;
131       return;
132     }
133   } else {
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();
140
141       try {
142         if (mType == IMAGEWITHTIME)
143         {
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;
147           return;
148         }
149         else
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;
154         error << err;
155         mLastError = error.str();
156         return;
157       }
158     } else {
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();
164
165       try {
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;
171         error << err;
172         mLastError = error.str();
173         return;
174       }
175     }
176   }
177 }
178 //----------------------------------------------------------------------------
179
180 //----------------------------------------------------------------------------
181 template<class InputPixelType, unsigned int VImageDimension>
182 void vvImageReader::UpdateWithDimAndInputVectorPixelType()
183 {
184   typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
185   typename InputImageType::Pointer input;
186
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();
192     try {
193       reader->Update();
194       input = reader->GetOutput();
195     } catch ( itk::ExceptionObject & err ) {
196       std::cerr << "Error while reading image series:" << err << std::endl;
197       std::stringstream error;
198       error << err;
199       mLastError = error.str();
200       return;
201     }
202   } else {
203     typedef itk::ImageFileReader<InputImageType> ReaderType;
204     typename ReaderType::Pointer reader = ReaderType::New();
205     reader->SetFileName(mInputFilenames[0]);
206     reader->ReleaseDataFlagOn();
207     try {
208       reader->Update();
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;
214       error << err;
215       mLastError = error.str();
216       return;
217     }
218   }
219   
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);
225   caster->Update();
226   casted_input = caster->GetOutput();
227   
228   mImage = vvImageFromITK<VImageDimension, itk::Vector<float, 3> >(casted_input, mType == IMAGEWITHTIME || mType == VECTORFIELDWITHTIME);
229 }
230 //----------------------------------------------------------------------------
231
232 #endif /* end #define vvImageReader_TXX */
233