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