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)
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>();
54 std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
56 if (CLITK_EXPERIMENTAL && mLastError.size()==0) {
57 ReadNkiImageTransform();
58 ReadMatImageTransform();
61 //----------------------------------------------------------------------------
64 //----------------------------------------------------------------------------
65 template<class InputPixelType, unsigned int VImageDimension>
66 void vvImageReader::UpdateWithDimAndInputPixelType()
71 if (mType == MERGEDWITHTIME) // In this case we can load the images
72 // one at the time to avoid excessive
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);
87 } catch ( itk::ExceptionObject & err ) {
88 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
89 << " " << err << std::endl;
90 std::stringstream error;
92 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);
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);
118 } catch ( itk::ExceptionObject & err ) {
119 std::cerr << "Error while reading image series:" << err << std::endl;
120 std::stringstream error;
122 mLastError = error.str();
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]);
132 if (mType == IMAGEWITHTIME)
133 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
135 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
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);
146 } catch ( itk::ExceptionObject & err ) {
147 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
148 << " " << err << std::endl;
149 std::stringstream error;
151 mLastError = error.str();
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]);
163 if (mType == IMAGEWITHTIME)
164 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
166 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
170 //----------------------------------------------------------------------------
173 #endif /* end #define vvImageReader_TXX */