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://www.centreleonberard.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 "clitkConfiguration.h"
31 #include "vvFromITK.h"
32 //----------------------------------------------------------------------------
33 template<unsigned int VImageDimension>
34 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 == "unsigned_int")
49 UpdateWithDimAndInputPixelType<unsigned int,VImageDimension>();
50 else if (InputPixelType == "double")
51 UpdateWithDimAndInputPixelType<double,VImageDimension>();
52 else if (InputPixelType == "float")
53 UpdateWithDimAndInputPixelType<float,VImageDimension>();
55 std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
57 if (CLITK_EXPERIMENTAL && mLastError.size()==0) {
58 //ReadNkiImageTransform();
59 ReadMatImageTransform();
62 //----------------------------------------------------------------------------
65 //----------------------------------------------------------------------------
66 template<class InputPixelType, unsigned int VImageDimension>
67 void vvImageReader::UpdateWithDimAndInputPixelType()
69 if (mType == MERGEDWITHTIME) // In this case we can load the images
70 // one at the time to avoid excessive
73 mImage=vvImage::New();
75 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
76 typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
77 typedef itk::ImageFileReader<InputImageType> ReaderType;
78 typename ReaderType::Pointer reader = ReaderType::New();
79 reader->ReleaseDataFlagOn();
80 reader->SetFileName(*i);
82 mImage->AddItkImage<InputImageType>(reader->GetOutput());
83 } catch ( itk::ExceptionObject & err ) {
84 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
85 << " " << err << std::endl;
86 std::stringstream error;
88 mLastError = error.str();
92 } else if (mType == SLICED) {
93 mImage=vvImage::New();
94 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
95 typedef itk::ImageFileReader<InputImageType> ReaderType;
96 typename ReaderType::Pointer reader = ReaderType::New();
97 reader->SetFileName(mInputFilenames[0]);
98 reader->UpdateOutputInformation();
100 typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
101 typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
103 typename InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
104 typename InputImageType::SizeType inputSize = inputRegion.GetSize();
105 typename InputImageType::IndexType start = inputRegion.GetIndex();
106 typename InputImageType::SizeType extractedRegionSize = inputSize;
107 typename InputImageType::RegionType extractedRegion;
108 extractedRegionSize[VImageDimension - 1] = 0;
109 extractedRegion.SetSize(extractedRegionSize);
110 start[VImageDimension - 1] = mSlice;
111 extractedRegion.SetIndex(start);
113 typename FilterType::Pointer filter = FilterType::New();
114 filter->SetExtractionRegion(extractedRegion);
115 filter->SetInput(reader->GetOutput());
116 filter->ReleaseDataFlagOn();
118 mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
120 catch ( itk::ExceptionObject & err ) {
121 std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
122 << "(slice #" << mSlice << ") " << err << std::endl;
126 if (mInputFilenames.size() > 1) {
127 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
128 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
129 typename ReaderType::Pointer reader = ReaderType::New();
130 reader->SetFileNames(mInputFilenames);
131 reader->ReleaseDataFlagOn();
134 if (mType == IMAGEWITHTIME)
135 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
137 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
138 } catch ( itk::ExceptionObject & err ) {
139 std::cerr << "Error while reading image series:" << err << std::endl;
140 std::stringstream error;
142 mLastError = error.str();
146 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
147 typedef itk::ImageFileReader<InputImageType> ReaderType;
148 typename ReaderType::Pointer reader = ReaderType::New();
149 reader->SetFileName(mInputFilenames[0]);
150 reader->ReleaseDataFlagOn();
153 if (mType == IMAGEWITHTIME)
154 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
156 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
157 } catch ( itk::ExceptionObject & err ) {
158 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
159 << " " << err << std::endl;
160 std::stringstream error;
162 mLastError = error.str();
168 //----------------------------------------------------------------------------
171 #endif /* end #define vvImageReader_TXX */