]> Creatis software - clitk.git/blob - vv/vvImageReader.txx
Reformatted using new coding style
[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)
57     if (mLastError.size()==0)
58       ReadNkiImageTransform();
59 }
60 //----------------------------------------------------------------------------
61
62
63 //----------------------------------------------------------------------------
64 template<class InputPixelType, unsigned int VImageDimension>
65 void vvImageReader::UpdateWithDimAndInputPixelType()
66 {
67
68   //  DD(mType);
69
70   if (mType == MERGEDWITHTIME)   // In this case we can load the images
71     // one at the time to avoid excessive
72     // memory use
73   {
74     typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
75     typedef itk::ImageFileReader<InputImageType> ReaderType;
76     typename ReaderType::Pointer reader = ReaderType::New();
77     typedef itk::ImageToVTKImageFilter <InputImageType> ConnectorType;
78     typename ConnectorType::Pointer connector = ConnectorType::New();
79     connector->SetInput(reader->GetOutput());
80     mImage=vvImage::New();
81     for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
82       std::cout << (*i) << std::endl;
83       reader->SetFileName(*i);
84       try {
85         reader->Update();
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       } catch ( itk::ExceptionObject & err ) {
97         std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
98                   << " " << err << std::endl;
99       }
100       vtkImageData *image = vtkImageData::New();
101       image->DeepCopy(connector->GetOutput());
102       mImage->AddImage(image);
103     }
104   } else {
105     if (mInputFilenames.size() > 1) {
106       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
107       typedef itk::ImageSeriesReader<InputImageType> ReaderType;
108       typename ReaderType::Pointer reader = ReaderType::New();
109       for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++)
110         std::cout << (*i) << std::endl;
111       reader->SetFileNames(mInputFilenames);
112       //if (mUseAnObserver) {
113       //reader->AddObserver(itk::ProgressEvent(), mObserver);
114       //}
115       try {
116         reader->Update();
117       } catch ( itk::ExceptionObject & err ) {
118         std::cerr << "Error while reading image series:" << err << std::endl;
119         std::stringstream error;
120         error << err;
121         mLastError = error.str();
122         return;
123       }
124
125       // DD(reader->GetOutput()->GetImageDimension());
126       //           DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
127       //           for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
128       //             DD(reader->GetOutput()->GetSpacing()[i]);
129       //           }
130
131       if (mType == IMAGEWITHTIME)
132         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
133       else
134         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
135     } else {
136       typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
137       typedef itk::ImageFileReader<InputImageType> ReaderType;
138       typename ReaderType::Pointer reader = ReaderType::New();
139       reader->SetFileName(mInputFilenames[0]);
140       //if (mUseAnObserver) {
141       //reader->AddObserver(itk::ProgressEvent(), mObserver);
142       //}
143       try {
144         reader->Update();
145       } catch ( itk::ExceptionObject & err ) {
146         std::cerr << "Error while reading " << mInputFilenames[0].c_str()
147                   << " " << err << std::endl;
148         std::stringstream error;
149         error << err;
150         mLastError = error.str();
151         return;
152       }
153
154       // DD(reader->GetOutput()->GetImageDimension());
155       //           DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
156       //           for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
157       //             DD(reader->GetOutput()->GetSpacing()[i]);
158       //             DD(reader->GetOutput()->GetOrigin()[i]);
159       //           }
160
161
162       if (mType == IMAGEWITHTIME)
163         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
164       else
165         mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
166     }
167   }
168 }
169 //----------------------------------------------------------------------------
170
171
172 #endif /* end #define vvImageReader_TXX */
173