]> Creatis software - clitk.git/blob - vv/vvImageReader.txx
Implemented new vvImage scheme: the itk to vtk image converter is now kept as a class...
[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       std::cout << (*i) << std::endl;
85       reader->SetFileName(*i);
86       try {
87         reader->Update();
88       } catch ( itk::ExceptionObject & err ) {
89         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
90                   << " " << err << std::endl;
91         std::stringstream error;
92         error << err;
93         mLastError = error.str();
94         return;
95       }
96       mImage->AddItkImage<InputImageType>(reader->GetOutput());
97     }
98   } else {
99     if (mInputFilenames.size() > 1) {
100       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
101       typedef itk::ImageSeriesReader<InputImageType> ReaderType;
102       typename ReaderType::Pointer reader = ReaderType::New();
103       for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++)
104         std::cout << (*i) << std::endl;
105       reader->SetFileNames(mInputFilenames);
106       //if (mUseAnObserver) {
107       //reader->AddObserver(itk::ProgressEvent(), mObserver);
108       //}
109       try {
110         reader->Update();
111       } catch ( itk::ExceptionObject & err ) {
112         std::cerr << "Error while reading image series:" << err << std::endl;
113         std::stringstream error;
114         error << err;
115         mLastError = error.str();
116         return;
117       }
118
119       // DD(reader->GetOutput()->GetImageDimension());
120       //           DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
121       //           for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
122       //             DD(reader->GetOutput()->GetSpacing()[i]);
123       //           }
124
125       if (mType == IMAGEWITHTIME)
126         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
127       else
128         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
129     } else {
130       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
131       typedef itk::ImageFileReader<InputImageType> ReaderType;
132       typename ReaderType::Pointer reader = ReaderType::New();
133       reader->SetFileName(mInputFilenames[0]);
134       //if (mUseAnObserver) {
135       //reader->AddObserver(itk::ProgressEvent(), mObserver);
136       //}
137       try {
138         reader->Update();
139       } catch ( itk::ExceptionObject & err ) {
140         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
141                   << " " << err << std::endl;
142         std::stringstream error;
143         error << err;
144         mLastError = error.str();
145         return;
146       }
147
148       // DD(reader->GetOutput()->GetImageDimension());
149       //           DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
150       //           for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
151       //             DD(reader->GetOutput()->GetSpacing()[i]);
152       //             DD(reader->GetOutput()->GetOrigin()[i]);
153       //           }
154
155
156       if (mType == IMAGEWITHTIME)
157         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
158       else
159         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
160     }
161   }
162 }
163 //----------------------------------------------------------------------------
164
165
166 #endif /* end #define vvImageReader_TXX */
167