1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
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
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.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #ifndef VVIMAGEREADER_TXX
20 #define VVIMAGEREADER_TXX
23 #include <itkImageFileReader.h>
24 #include <itkImageSeriesReader.h>
25 #include <itkImageToVTKImageFilter.h>
27 #include <vtkTransform.h>
29 #include "clitkCommon.h"
30 #include "vvFromITK.h"
31 #include "vvConstants.h"
33 //----------------------------------------------------------------------------
34 template<unsigned int VImageDimension>
35 void vvImageReader::UpdateWithDim(std::string InputPixelType) {
36 if (mType == VECTORFIELD)
37 UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
38 else if (InputPixelType == "short")
39 UpdateWithDimAndInputPixelType<short,VImageDimension>();
40 else if (InputPixelType == "unsigned_short")
41 UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
42 else if (InputPixelType == "char")
43 UpdateWithDimAndInputPixelType<char,VImageDimension>();
44 else if (InputPixelType == "unsigned_char")
45 UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
46 else if (InputPixelType == "int")
47 UpdateWithDimAndInputPixelType<int,VImageDimension>();
48 else if (InputPixelType == "double")
49 UpdateWithDimAndInputPixelType<double,VImageDimension>();
50 else if (InputPixelType == "float")
51 UpdateWithDimAndInputPixelType<float,VImageDimension>();
53 std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
55 if (CLITK_EXPERIMENTAL)
56 if (mLastError.size()==0)
57 ReadNkiImageTransform();
59 //----------------------------------------------------------------------------
62 //----------------------------------------------------------------------------
63 template<class InputPixelType, unsigned int VImageDimension>
64 void vvImageReader::UpdateWithDimAndInputPixelType() {
68 if (mType == MERGEDWITHTIME) // In this case we can load the images
69 // one at the time to avoid excessive
72 typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
73 typedef itk::ImageFileReader<InputImageType> ReaderType;
74 typename ReaderType::Pointer reader = ReaderType::New();
75 typedef itk::ImageToVTKImageFilter <InputImageType> ConnectorType;
76 typename ConnectorType::Pointer connector = ConnectorType::New();
77 connector->SetInput(reader->GetOutput());
78 mImage=vvImage::New();
79 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
81 std::cout << (*i) << std::endl;
82 reader->SetFileName(*i);
86 catch ( itk::ExceptionObject & err ) {
87 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
88 << " " << err << std::endl;
89 std::stringstream error;
91 mLastError = error.str();
97 catch ( itk::ExceptionObject & err ) {
98 std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
99 << " " << err << std::endl;
101 vtkImageData *image = vtkImageData::New();
102 image->DeepCopy(connector->GetOutput());
103 mImage->AddImage(image);
108 if (mInputFilenames.size() > 1)
110 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
111 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
112 typename ReaderType::Pointer reader = ReaderType::New();
113 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
114 std::cout << (*i) << std::endl;
115 reader->SetFileNames(mInputFilenames);
116 //if (mUseAnObserver) {
117 //reader->AddObserver(itk::ProgressEvent(), mObserver);
122 catch ( itk::ExceptionObject & err ) {
123 std::cerr << "Error while reading image series:" << err << std::endl;
124 std::stringstream error;
126 mLastError = error.str();
130 // DD(reader->GetOutput()->GetImageDimension());
131 // DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
132 // for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
133 // DD(reader->GetOutput()->GetSpacing()[i]);
136 if (mType == IMAGEWITHTIME)
137 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
139 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
143 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
144 typedef itk::ImageFileReader<InputImageType> ReaderType;
145 typename ReaderType::Pointer reader = ReaderType::New();
146 reader->SetFileName(mInputFilenames[0]);
147 //if (mUseAnObserver) {
148 //reader->AddObserver(itk::ProgressEvent(), mObserver);
153 catch ( itk::ExceptionObject & err ) {
154 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
155 << " " << err << std::endl;
156 std::stringstream error;
158 mLastError = error.str();
162 // DD(reader->GetOutput()->GetImageDimension());
163 // DD(reader->GetOutput()->GetNumberOfComponentsPerPixel());
164 // for(unsigned int i=0; i <reader->GetOutput()->GetImageDimension(); i++) {
165 // DD(reader->GetOutput()->GetSpacing()[i]);
166 // DD(reader->GetOutput()->GetOrigin()[i]);
170 if (mType == IMAGEWITHTIME)
171 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
173 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
177 //----------------------------------------------------------------------------
180 #endif /* end #define vvImageReader_TXX */