]> Creatis software - clitk.git/blob - vv/vvImageReader.txx
move clitkConfiguration.h out of clitkCommon to avoid costly recompilations
[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     typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
79     typedef itk::ImageFileReader<InputImageType> ReaderType;
80     typename ReaderType::Pointer reader = ReaderType::New();
81     typedef itk::ImageToVTKImageFilter <InputImageType> ConnectorType;
82     typename ConnectorType::Pointer connector = ConnectorType::New();
83     connector->SetInput(reader->GetOutput());
84     mImage=vvImage::New();
85     for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
86       std::cout << (*i) << std::endl;
87       reader->SetFileName(*i);
88       try {
89         reader->Update();
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       try {
99         connector->Update();
100       } catch ( itk::ExceptionObject & err ) {
101         std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
102                   << " " << err << std::endl;
103       }
104       vtkImageData *image = vtkImageData::New();
105       image->ShallowCopy(connector->GetOutput());
106       mImage->AddImage(image);
107     }
108   } else {
109     if (mInputFilenames.size() > 1) {
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       } catch ( itk::ExceptionObject & err ) {
122         std::cerr << "Error while reading image series:" << err << std::endl;
123         std::stringstream error;
124         error << err;
125         mLastError = error.str();
126         return;
127       }
128
129       // DD(reader->GetOutput()->GetImageDimension());
130       //           DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
131       //           for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
132       //             DD(reader->GetOutput()->GetSpacing()[i]);
133       //           }
134
135       if (mType == IMAGEWITHTIME)
136         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
137       else
138         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
139     } else {
140       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
141       typedef itk::ImageFileReader<InputImageType> ReaderType;
142       typename ReaderType::Pointer reader = ReaderType::New();
143       reader->SetFileName(mInputFilenames[0]);
144       //if (mUseAnObserver) {
145       //reader->AddObserver(itk::ProgressEvent(), mObserver);
146       //}
147       try {
148         reader->Update();
149       } catch ( itk::ExceptionObject & err ) {
150         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
151                   << " " << err << std::endl;
152         std::stringstream error;
153         error << err;
154         mLastError = error.str();
155         return;
156       }
157
158       // DD(reader->GetOutput()->GetImageDimension());
159       //           DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
160       //           for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
161       //             DD(reader->GetOutput()->GetSpacing()[i]);
162       //             DD(reader->GetOutput()->GetOrigin()[i]);
163       //           }
164
165
166       if (mType == IMAGEWITHTIME)
167         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
168       else
169         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
170     }
171   }
172 }
173 //----------------------------------------------------------------------------
174
175
176 #endif /* end #define vvImageReader_TXX */
177