]> Creatis software - clitk.git/blob - vv/vvImageReader.txx
Implemented alternative to former split: split on open.
[clitk.git] / vv / 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://oncora1.lyon.fnclcc.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
27 #include <vtkTransform.h>
28
29 #include "clitkCommon.h"
30 #include "clitkConfiguration.h"
31 #include "vvFromITK.h"
32 #include "vvConstants.h"
33
34 //----------------------------------------------------------------------------
35 template<unsigned int VImageDimension>
36 void vvImageReader::UpdateWithDim(std::string InputPixelType)
37 {
38   if (mType == VECTORFIELD)
39     UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
40   else if (InputPixelType == "short")
41     UpdateWithDimAndInputPixelType<short,VImageDimension>();
42   else if (InputPixelType == "unsigned_short")
43     UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
44   else if (InputPixelType == "char")
45     UpdateWithDimAndInputPixelType<char,VImageDimension>();
46   else if (InputPixelType == "unsigned_char")
47     UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
48   else if (InputPixelType == "int")
49     UpdateWithDimAndInputPixelType<int,VImageDimension>();
50   else if (InputPixelType == "unsigned_int")
51     UpdateWithDimAndInputPixelType<unsigned int,VImageDimension>();
52   else if (InputPixelType == "double")
53     UpdateWithDimAndInputPixelType<double,VImageDimension>();
54   else if (InputPixelType == "float")
55     UpdateWithDimAndInputPixelType<float,VImageDimension>();
56   else
57     std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
58
59   if (CLITK_EXPERIMENTAL && mLastError.size()==0) {
60     ReadNkiImageTransform();
61     ReadMatImageTransform();
62   }
63 }
64 //----------------------------------------------------------------------------
65
66
67 //----------------------------------------------------------------------------
68 template<class InputPixelType, unsigned int VImageDimension>
69 void vvImageReader::UpdateWithDimAndInputPixelType()
70 {
71
72   //  DD(mType);
73
74   if (mType == MERGEDWITHTIME)   // In this case we can load the images
75     // one at the time to avoid excessive
76     // memory use
77   {
78     mImage=vvImage::New();
79     for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
80       typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
81       typedef itk::ImageFileReader<InputImageType> ReaderType;
82       typename ReaderType::Pointer reader = ReaderType::New();
83       reader->ReleaseDataFlagOn();
84       reader->SetFileName(*i);
85       try {
86         reader->Update();
87       } catch ( itk::ExceptionObject & err ) {
88         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
89                   << " " << err << std::endl;
90         std::stringstream error;
91         error << err;
92         mLastError = error.str();
93         return;
94       }
95       mImage->AddItkImage<InputImageType>(reader->GetOutput());
96     }
97   } else if (mType == SLICED) {
98     mImage=vvImage::New();
99     typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
100     typedef itk::ImageFileReader<InputImageType> ReaderType;
101     typename ReaderType::Pointer reader = ReaderType::New();
102     reader->SetFileName(mInputFilenames[0]);
103     reader->UpdateOutputInformation();
104
105     typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
106     typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
107
108     typename InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
109     typename InputImageType::SizeType inputSize = inputRegion.GetSize();
110     typename InputImageType::IndexType start = inputRegion.GetIndex();
111     typename InputImageType::SizeType extractedRegionSize = inputSize;
112     typename InputImageType::RegionType extractedRegion;
113     extractedRegionSize[VImageDimension - 1] = 0;
114     extractedRegion.SetSize(extractedRegionSize);
115     start[VImageDimension - 1] = mSlice;
116     extractedRegion.SetIndex(start);
117
118     typename FilterType::Pointer filter = FilterType::New();
119     filter->SetExtractionRegion(extractedRegion);
120     filter->SetInput(reader->GetOutput());
121     filter->ReleaseDataFlagOn();
122     try {
123       filter->Update();
124     }
125     catch ( itk::ExceptionObject & err ) {
126       std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
127                 << "(slice #" << mSlice << ") " << err << std::endl;
128       return;
129     }
130     mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
131   } else {
132     if (mInputFilenames.size() > 1) {
133       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
134       typedef itk::ImageSeriesReader<InputImageType> ReaderType;
135       typename ReaderType::Pointer reader = ReaderType::New();
136       for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++)
137         std::cout << (*i) << std::endl;
138       reader->SetFileNames(mInputFilenames);
139       //if (mUseAnObserver) {
140       //reader->AddObserver(itk::ProgressEvent(), mObserver);
141       //}
142       try {
143         reader->Update();
144       } catch ( itk::ExceptionObject & err ) {
145         std::cerr << "Error while reading image series:" << err << std::endl;
146         std::stringstream error;
147         error << err;
148         mLastError = error.str();
149         return;
150       }
151
152       // DD(reader->GetOutput()->GetImageDimension());
153       //           DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
154       //           for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
155       //             DD(reader->GetOutput()->GetSpacing()[i]);
156       //           }
157
158       if (mType == IMAGEWITHTIME)
159         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
160       else
161         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
162     } else {
163       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
164       typedef itk::ImageFileReader<InputImageType> ReaderType;
165       typename ReaderType::Pointer reader = ReaderType::New();
166       reader->SetFileName(mInputFilenames[0]);
167       //if (mUseAnObserver) {
168       //reader->AddObserver(itk::ProgressEvent(), mObserver);
169       //}
170       try {
171         reader->Update();
172       } catch ( itk::ExceptionObject & err ) {
173         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
174                   << " " << err << std::endl;
175         std::stringstream error;
176         error << err;
177         mLastError = error.str();
178         return;
179       }
180
181       // DD(reader->GetOutput()->GetImageDimension());
182       //           DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
183       //           for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
184       //             DD(reader->GetOutput()->GetSpacing()[i]);
185       //             DD(reader->GetOutput()->GetOrigin()[i]);
186       //           }
187
188
189       if (mType == IMAGEWITHTIME)
190         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
191       else
192         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
193     }
194   }
195 }
196 //----------------------------------------------------------------------------
197
198
199 #endif /* end #define vvImageReader_TXX */
200