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;
36 //------------------------------------------------------------------------------
39 //------------------------------------------------------------------------------
40 vvImageReader::~vvImageReader() { }
41 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
45 void vvImageReader::Update()
49 //------------------------------------------------------------------------------
52 //------------------------------------------------------------------------------
53 void vvImageReader::Update(LoadedImageType type)
55 itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
57 mLastError="Unable to read file.";
59 reader->SetFileName(mInputFilenames[0]);
60 reader->ReadImageInformation();
61 if (mInputFilenames.size() > 1)
62 Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),type);
64 Update(reader->GetNumberOfDimensions(),reader->GetComponentTypeAsString(reader->GetComponentType()),type);
67 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------------------
71 void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
73 //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
76 mInputPixelType=inputPixelType;
77 this->start(); //Start heavy read operation in a separate thread
78 while (this->isRunning()) {
79 qApp->processEvents();
83 //------------------------------------------------------------------------------
86 //------------------------------------------------------------------------------
87 void vvImageReader::run()
91 UpdateWithDim<2>(mInputPixelType);
94 UpdateWithDim<3>(mInputPixelType);
97 UpdateWithDim<4>(mInputPixelType);
100 std::cerr << "dimension unknown in Update ! " << std::endl;
103 //------------------------------------------------------------------------------
106 //------------------------------------------------------------------------------
107 void vvImageReader::SetInputFilename(const std::string & filename)
109 mInputFilenames.resize(0);
110 mInputFilenames.push_back(filename);
112 //------------------------------------------------------------------------------
115 //------------------------------------------------------------------------------
116 void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
118 mInputFilenames = filenames;
120 //------------------------------------------------------------------------------
123 //------------------------------------------------------------------------------
124 //Read transformation in NKI format (Xdr, transposed, cm)
125 void vvImageReader::ReadNkiImageTransform()
128 typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
129 MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
130 readerTransfo->SetFileName(mInputFilenames[0]+".MACHINEORIENTATION");
132 readerTransfo->Update();
133 } catch( itk::ExceptionObject & err ) {
138 //Transpose matrix (NKI format)
139 for(int j=0; j<4; j++)
140 for(int i=0; i<4; i++)
141 mImage->GetTransform()->GetMatrix()->SetElement(j,i,readerTransfo->GetOutput()->GetBufferPointer()[4*i+j]);
144 for(int i=0; i<3; i++)
145 mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
147 mImage->GetTransform()->Inverse();
148 mImage->UpdateReslice();
151 //------------------------------------------------------------------------------
154 //------------------------------------------------------------------------------
155 //Read transformation in ASCII format
156 void vvImageReader::ReadMatImageTransform()
158 std::string filename(mInputFilenames[0]+".mat");
159 std::ifstream f(filename.c_str());
163 itk::Matrix<double, 4, 4> itkMat = clitk::ReadMatrix3D(filename);
164 for(int j=0; j<4; j++)
165 for(int i=0; i<4; i++)
166 mImage->GetTransform()->GetMatrix()->SetElement(j,i,itkMat[j][i]);
167 mImage->UpdateReslice();
170 //------------------------------------------------------------------------------