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