1 /*=========================================================================
4 Module: $RCSfile: vvImageReader.txx,v $
6 Date: $Date: 2010/01/06 13:31:57 $
7 Version: $Revision: 1.1 $
8 Author : Pierre Seroul (pierre.seroul@gmail.com)
11 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
12 CREATIS-LRMN http://www.creatis.insa-lyon.fr
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, version 3 of the License.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 =========================================================================*/
27 #ifndef vvImageReader_TXX
28 #define vvImageReader_TXX
31 #include <itkImageFileReader.h>
32 #include <itkImageSeriesReader.h>
34 #include "clitkCommon.h"
35 #include "itkImageToVTKImageFilter.h"
36 #include "vvFromITK.h"
37 #include "vvConstants.h"
39 template<unsigned int VImageDimension>
40 void vvImageReader::UpdateWithDim(std::string InputPixelType)
42 if (mType == VECTORFIELD)
43 UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
44 else if (InputPixelType == "short")
45 UpdateWithDimAndInputPixelType<short,VImageDimension>();
46 else if (InputPixelType == "unsigned_short")
47 UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
48 else if (InputPixelType == "char")
49 UpdateWithDimAndInputPixelType<char,VImageDimension>();
50 else if (InputPixelType == "unsigned_char")
51 UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
52 else if (InputPixelType == "int")
53 UpdateWithDimAndInputPixelType<int,VImageDimension>();
54 else if (InputPixelType == "double")
55 UpdateWithDimAndInputPixelType<double,VImageDimension>();
56 else if (InputPixelType == "float")
57 UpdateWithDimAndInputPixelType<float,VImageDimension>();
59 std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
61 //====================================================================
63 template<class InputPixelType, unsigned int VImageDimension>
64 void vvImageReader::UpdateWithDimAndInputPixelType()
66 if (mType == MERGEDWITHTIME) // In this case we can load the images
67 // one at the time to avoid excessive
70 typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
71 typedef itk::ImageFileReader<InputImageType> ReaderType;
72 typename ReaderType::Pointer reader = ReaderType::New();
73 typedef itk::ImageToVTKImageFilter <InputImageType> ConnectorType;
74 typename ConnectorType::Pointer connector = ConnectorType::New();
75 connector->SetInput(reader->GetOutput());
76 mImage=vvImage::New();
77 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
79 std::cout << (*i) << std::endl;
80 reader->SetFileName(*i);
84 catch ( itk::ExceptionObject & err ) {
85 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
86 << " " << err << std::endl;
87 std::stringstream error;
89 mLastError = error.str();
95 catch ( itk::ExceptionObject & err ) {
96 std::cerr << "Error while setting vvImage from ITK (MERGEDWITHTIME)"
97 << " " << err << std::endl;
99 vtkImageData *image = vtkImageData::New();
100 image->DeepCopy(connector->GetOutput());
101 mImage->AddImage(image);
106 if (mInputFilenames.size() > 1)
108 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
109 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
110 typename ReaderType::Pointer reader = ReaderType::New();
111 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin();i!=mInputFilenames.end();i++)
112 std::cout << (*i) << std::endl;
113 reader->SetFileNames(mInputFilenames);
114 //if (mUseAnObserver) {
115 //reader->AddObserver(itk::ProgressEvent(), mObserver);
120 catch ( itk::ExceptionObject & err ) {
121 std::cerr << "Error while reading image series:" << err << std::endl;
122 std::stringstream error;
124 mLastError = error.str();
127 if (mType == IMAGEWITHTIME)
128 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
130 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
134 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
135 typedef itk::ImageFileReader<InputImageType> ReaderType;
136 typename ReaderType::Pointer reader = ReaderType::New();
137 reader->SetFileName(mInputFilenames[0]);
138 //if (mUseAnObserver) {
139 //reader->AddObserver(itk::ProgressEvent(), mObserver);
144 catch ( itk::ExceptionObject & err ) {
145 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
146 << " " << err << std::endl;
147 std::stringstream error;
149 mLastError = error.str();
152 if (mType == IMAGEWITHTIME)
153 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
155 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
159 //====================================================================
162 #endif /* end #define vvImageReader_TXX */