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 "clitkCommon.h"
26 #include "itkImageToVTKImageFilter.h"
27 #include "vvFromITK.h"
28 #include "vvConstants.h"
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>();
50 std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
52 //----------------------------------------------------------------------------
55 //----------------------------------------------------------------------------
56 template<class InputPixelType, unsigned int VImageDimension>
57 void vvImageReader::UpdateWithDimAndInputPixelType() {
61 if (mType == MERGEDWITHTIME) // In this case we can load the images
62 // one at the time to avoid excessive
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++)
74 std::cout << (*i) << std::endl;
75 reader->SetFileName(*i);
79 catch ( itk::ExceptionObject & err ) {
80 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
81 << " " << err << std::endl;
82 std::stringstream error;
84 mLastError = error.str();
90 catch ( itk::ExceptionObject & err ) {
91 std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
92 << " " << err << std::endl;
94 vtkImageData *image = vtkImageData::New();
95 image->DeepCopy(connector->GetOutput());
96 mImage->AddImage(image);
101 if (mInputFilenames.size() > 1)
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);
115 catch ( itk::ExceptionObject & err ) {
116 std::cerr << "Error while reading image series:" << err << std::endl;
117 std::stringstream error;
119 mLastError = error.str();
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]);
129 if (mType == IMAGEWITHTIME)
130 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
132 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
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);
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 */