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 "clitkConfiguration.h"
31 #include "vvFromITK.h"
32 #include "vvConstants.h"
34 //----------------------------------------------------------------------------
35 template<unsigned int VImageDimension>
36 void vvImageReader::UpdateWithDim(std::string InputPixelType)
38 if (mType == VECTORFIELD)
39 UpdateWithDimAndInputPixelType<itk::Vector<float,3>,VImageDimension>();
40 else if (InputPixelType == "short")
41 UpdateWithDimAndInputPixelType<short,VImageDimension>();
42 else if (InputPixelType == "unsigned_short")
43 UpdateWithDimAndInputPixelType<unsigned short,VImageDimension>();
44 else if (InputPixelType == "char")
45 UpdateWithDimAndInputPixelType<char,VImageDimension>();
46 else if (InputPixelType == "unsigned_char")
47 UpdateWithDimAndInputPixelType<unsigned char,VImageDimension>();
48 else if (InputPixelType == "int")
49 UpdateWithDimAndInputPixelType<int,VImageDimension>();
50 else if (InputPixelType == "unsigned_int")
51 UpdateWithDimAndInputPixelType<unsigned int,VImageDimension>();
52 else if (InputPixelType == "double")
53 UpdateWithDimAndInputPixelType<double,VImageDimension>();
54 else if (InputPixelType == "float")
55 UpdateWithDimAndInputPixelType<float,VImageDimension>();
57 std::cerr << "Error, input pixel type : " << InputPixelType << " unknown !" << std::endl;
59 if (CLITK_EXPERIMENTAL && mLastError.size()==0) {
60 ReadNkiImageTransform();
61 ReadMatImageTransform();
64 //----------------------------------------------------------------------------
67 //----------------------------------------------------------------------------
68 template<class InputPixelType, unsigned int VImageDimension>
69 void vvImageReader::UpdateWithDimAndInputPixelType()
74 if (mType == MERGEDWITHTIME) // In this case we can load the images
75 // one at the time to avoid excessive
78 mImage=vvImage::New();
79 for (std::vector<std::string>::const_iterator i=mInputFilenames.begin(); i!=mInputFilenames.end(); i++) {
80 typedef itk::Image< InputPixelType, VImageDimension-1 > InputImageType;
81 typedef itk::ImageFileReader<InputImageType> ReaderType;
82 typename ReaderType::Pointer reader = ReaderType::New();
83 reader->ReleaseDataFlagOn();
84 reader->SetFileName(*i);
86 mImage->AddItkImage<InputImageType>(reader->GetOutput());
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();
96 } else if (mType == SLICED) {
97 mImage=vvImage::New();
98 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
99 typedef itk::ImageFileReader<InputImageType> ReaderType;
100 typename ReaderType::Pointer reader = ReaderType::New();
101 reader->SetFileName(mInputFilenames[0]);
102 reader->UpdateOutputInformation();
104 typedef itk::Image< InputPixelType, VImageDimension-1 > SlicedImageType;
105 typedef itk::ExtractImageFilter<InputImageType, SlicedImageType> FilterType;
107 typename InputImageType::RegionType inputRegion = reader->GetOutput()->GetLargestPossibleRegion();
108 typename InputImageType::SizeType inputSize = inputRegion.GetSize();
109 typename InputImageType::IndexType start = inputRegion.GetIndex();
110 typename InputImageType::SizeType extractedRegionSize = inputSize;
111 typename InputImageType::RegionType extractedRegion;
112 extractedRegionSize[VImageDimension - 1] = 0;
113 extractedRegion.SetSize(extractedRegionSize);
114 start[VImageDimension - 1] = mSlice;
115 extractedRegion.SetIndex(start);
117 typename FilterType::Pointer filter = FilterType::New();
118 filter->SetExtractionRegion(extractedRegion);
119 filter->SetInput(reader->GetOutput());
120 filter->ReleaseDataFlagOn();
122 mImage->AddItkImage<SlicedImageType>(filter->GetOutput());
124 catch ( itk::ExceptionObject & err ) {
125 std::cerr << "Error while slicing " << mInputFilenames[0].c_str()
126 << "(slice #" << mSlice << ") " << err << std::endl;
130 if (mInputFilenames.size() > 1) {
131 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
132 typedef itk::ImageSeriesReader<InputImageType> ReaderType;
133 typename ReaderType::Pointer reader = ReaderType::New();
134 reader->SetFileNames(mInputFilenames);
135 reader->ReleaseDataFlagOn();
138 if (mType == IMAGEWITHTIME)
139 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
141 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
142 } catch ( itk::ExceptionObject & err ) {
143 std::cerr << "Error while reading image series:" << err << std::endl;
144 std::stringstream error;
146 mLastError = error.str();
150 typedef itk::Image< InputPixelType, VImageDimension > InputImageType;
151 typedef itk::ImageFileReader<InputImageType> ReaderType;
152 typename ReaderType::Pointer reader = ReaderType::New();
153 reader->SetFileName(mInputFilenames[0]);
154 reader->ReleaseDataFlagOn();
157 if (mType == IMAGEWITHTIME)
158 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput(),true);
160 mImage=vvImageFromITK<VImageDimension,InputPixelType>(reader->GetOutput());
161 } catch ( itk::ExceptionObject & err ) {
162 std::cerr << "Error while reading " << mInputFilenames[0].c_str()
163 << " " << err << std::endl;
164 std::stringstream error;
166 mLastError = error.str();
172 //----------------------------------------------------------------------------
175 #endif /* end #define vvImageReader_TXX */