1 /*=========================================================================
5 Author : Pierre Seroul (pierre.seroul@gmail.com)
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 =========================================================================*/
24 #ifndef vvImageReader_TXX
25 #define vvImageReader_TXX
28 #include <itkImageFileReader.h>
29 #include <itkImageSeriesReader.h>
31 #include "clitkCommon.h"
32 #include "itkImageToVTKImageFilter.h"
33 #include "vvFromITK.h"
34 #include "vvConstants.h"
36 template<unsigned int VImageDimension>
37 void vvImageReader::UpdateWithDim(std::string InputPixelType)
39 if (mType == VECTORFIELD)
40 UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
41 else if (InputPixelType == "short")
42 UpdateWithDimAndInputPixelType<short,VImageDimension>();
43 else if (InputPixelType == "unsigned_short")
44 UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
45 else if (InputPixelType == "char")
46 UpdateWithDimAndInputPixelType<char,VImageDimension>();
47 else if (InputPixelType == "unsigned_char")
48 UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
49 else if (InputPixelType == "int")
50 UpdateWithDimAndInputPixelType<int,VImageDimension>();
51 else if (InputPixelType == "double")
52 UpdateWithDimAndInputPixelType<double,VImageDimension>();
53 else if (InputPixelType == "float")
54 UpdateWithDimAndInputPixelType<float,VImageDimension>();
56 std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
58 //====================================================================
60 template<class InputPixelType, unsigned int VImageDimension>
61 void vvImageReader::UpdateWithDimAndInputPixelType()
63 if (mType == MERGEDWITHTIME) // In this case we can load the images
64 // one at the time to avoid excessive
67 typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
68 typedef itk::ImageFileReader<InputImageType> ReaderType;
69 typename ReaderType::Pointer reader = ReaderType::New();
70 typedef itk::ImageToVTKImageFilter <InputImageType> ConnectorType;
71 typename ConnectorType::Pointer connector = ConnectorType::New();
72 connector->SetInput(reader->GetOutput());
73 mImage=vvImage::New();
74 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
76 std::cout << (*i) << std::endl;
77 reader->SetFileName(*i);
81 catch ( itk::ExceptionObject & err ) {
82 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
83 << " " << err << std::endl;
84 std::stringstream error;
86 mLastError = error.str();
92 catch ( itk::ExceptionObject & err ) {
93 std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
94 << " " << err << std::endl;
96 vtkImageData *image = vtkImageData::New();
97 image->DeepCopy(connector->GetOutput());
98 mImage->AddImage(image);
103 if (mInputFilenames.size() > 1)
105 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
106 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
107 typename ReaderType::Pointer reader = ReaderType::New();
108 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
109 std::cout << (*i) << std::endl;
110 reader->SetFileNames(mInputFilenames);
111 //if (mUseAnObserver) {
112 //reader->AddObserver(itk::ProgressEvent(), mObserver);
117 catch ( itk::ExceptionObject & err ) {
118 std::cerr << "Error while reading image series:" << err << std::endl;
119 std::stringstream error;
121 mLastError = error.str();
124 if (mType == IMAGEWITHTIME)
125 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
127 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
131 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
132 typedef itk::ImageFileReader<InputImageType> ReaderType;
133 typename ReaderType::Pointer reader = ReaderType::New();
134 reader->SetFileName(mInputFilenames[0]);
135 //if (mUseAnObserver) {
136 //reader->AddObserver(itk::ProgressEvent(), mObserver);
141 catch ( itk::ExceptionObject & err ) {
142 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
143 << " " << err << std::endl;
144 std::stringstream error;
146 mLastError = error.str();
149 if (mType == IMAGEWITHTIME)
150 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
152 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
156 //====================================================================
159 #endif /* end #define vvImageReader_TXX */