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 ======================================================================-====*/
18 #ifndef VVIMAGEREADER_CXX
19 #define VVIMAGEREADER_CXX
21 #include <QApplication>
22 #include <itkImageFileReader.h>
23 #include "vvImageReader.h"
24 #include "vvImageReader.txx"
25 #include "clitkTransformUtilities.h"
27 //------------------------------------------------------------------------------
28 vvImageReader::vvImageReader()
31 mInputFilenames.resize(0);
33 mType = UNDEFINEDIMAGETYPE;
35 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 vvImageReader::~vvImageReader() { }
40 //------------------------------------------------------------------------------
43 //------------------------------------------------------------------------------
44 void vvImageReader::Update()
48 //------------------------------------------------------------------------------
51 //------------------------------------------------------------------------------
52 void vvImageReader::Update(LoadedImageType type)
54 itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
56 mLastError="Unable to read file.";
58 reader->SetFileName(mInputFilenames[0]);
59 reader->ReadImageInformation();
60 if (mInputFilenames.size() > 1)
61 Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),type);
63 Update(reader->GetNumberOfDimensions(),reader->GetComponentTypeAsString(reader->GetComponentType()),type);
66 //------------------------------------------------------------------------------
69 //------------------------------------------------------------------------------
70 void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
72 //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
75 mInputPixelType=inputPixelType;
76 this->start(); //Start heavy read operation in a separate thread
77 while (this->isRunning()) {
78 qApp->processEvents();
82 //------------------------------------------------------------------------------
85 //------------------------------------------------------------------------------
86 void vvImageReader::run()
90 UpdateWithDim<2>(mInputPixelType);
93 UpdateWithDim<3>(mInputPixelType);
96 UpdateWithDim<4>(mInputPixelType);
99 std::cerr << "dimension unknown in Update ! " << std::endl;
102 //------------------------------------------------------------------------------
105 //------------------------------------------------------------------------------
106 void vvImageReader::SetInputFilename(const std::string & filename)
108 mInputFilenames.resize(0);
109 mInputFilenames.push_back(filename);
111 //------------------------------------------------------------------------------
114 //------------------------------------------------------------------------------
115 void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
117 mInputFilenames = filenames;
119 //------------------------------------------------------------------------------
122 //------------------------------------------------------------------------------
123 //Read transformation in NKI format (Xdr, transposed, cm)
124 void vvImageReader::ReadNkiImageTransform()
127 typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
128 MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
129 readerTransfo->SetFileName(mInputFilenames[0]+".MACHINEORIENTATION");
131 readerTransfo->Update();
132 } catch( itk::ExceptionObject & err ) {
137 //Transpose matrix (NKI format)
138 for(int j=0; j<4; j++)
139 for(int i=0; i<4; i++)
140 mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]);
143 for(int i=0; i<3; i++)
144 mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
146 mImage->GetTransform()->Inverse();
147 mImage->UpdateReslice();
150 //------------------------------------------------------------------------------
153 //------------------------------------------------------------------------------
154 //Read transformation in ASCII format
155 void vvImageReader::ReadMatImageTransform()
157 std::string filename(mInputFilenames[0]+".mat");
158 std::ifstream f(filename.c_str());
162 itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(filename);
163 for(int j=0; j<4; j++)
164 for(int i=0; i<4; i++)
165 mImage->GetTransform()->GetMatrix()->SetElement(j,i,itkMat[j][i]);
166 mImage->UpdateReslice();
169 //------------------------------------------------------------------------------