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