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