]> Creatis software - clitk.git/blob - vv/vvImageReader.cxx
Implemented alternative to former split: split on open.
[clitk.git] / vv / vvImageReader.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
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
20
21 #include <QApplication>
22 #include <itkImageFileReader.h>
23 #include "vvImageReader.h"
24 #include "vvImageReader.txx"
25 #include "clitkTransformUtilities.h"
26
27 //------------------------------------------------------------------------------
28 vvImageReader::vvImageReader()
29 {
30   mImage = NULL;
31   mInputFilenames.resize(0);
32   mLastError = "";
33   mType = UNDEFINEDIMAGETYPE;
34   mSlice = 0;
35 }
36 //------------------------------------------------------------------------------
37
38
39 //------------------------------------------------------------------------------
40 vvImageReader::~vvImageReader() { }
41 //------------------------------------------------------------------------------
42
43
44 //------------------------------------------------------------------------------
45 void vvImageReader::Update()
46 {
47   Update(mType);
48 }
49 //------------------------------------------------------------------------------
50
51
52 //------------------------------------------------------------------------------
53 void vvImageReader::Update(LoadedImageType type)
54 {
55   itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
56   if (!reader) {
57     mLastError="Unable to read file.";
58   } else {
59     reader->SetFileName(mInputFilenames[0]);
60     reader->ReadImageInformation();
61     if (mInputFilenames.size() > 1)
62       Update(reader->GetNumberOfDimensions()+1,reader->GetComponentTypeAsString(reader->GetComponentType()),type);
63     else
64       Update(reader->GetNumberOfDimensions(),reader->GetComponentTypeAsString(reader->GetComponentType()),type);
65   }
66 }
67 //------------------------------------------------------------------------------
68
69
70 //------------------------------------------------------------------------------
71 void vvImageReader::Update(int dim,std::string inputPixelType, LoadedImageType type)
72 {
73   //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
74   mType = type;
75   mDim = dim;
76   mInputPixelType=inputPixelType;
77   this->start(); //Start heavy read operation in a separate thread
78   while (this->isRunning()) {
79     qApp->processEvents();
80     this->wait(50);
81   }
82 }
83 //------------------------------------------------------------------------------
84
85
86 //------------------------------------------------------------------------------
87 void vvImageReader::run()
88 {
89   switch(mDim)     {
90   case 2:
91     UpdateWithDim<2>(mInputPixelType);
92     break;;
93   case 3:
94     UpdateWithDim<3>(mInputPixelType);
95     break;;
96   case 4:
97     UpdateWithDim<4>(mInputPixelType);
98     break;;
99   default:
100     std::cerr << "dimension unknown in Update ! " << std::endl;
101   }
102 }
103 //------------------------------------------------------------------------------
104
105
106 //------------------------------------------------------------------------------
107 void vvImageReader::SetInputFilename(const std::string & filename)
108 {
109   mInputFilenames.resize(0);
110   mInputFilenames.push_back(filename);
111 }
112 //------------------------------------------------------------------------------
113
114
115 //------------------------------------------------------------------------------
116 void vvImageReader::SetInputFilenames(const std::vector<std::string> & filenames)
117 {
118   mInputFilenames = filenames;
119 }
120 //------------------------------------------------------------------------------
121
122
123 //------------------------------------------------------------------------------
124 //Read transformation in NKI format (Xdr, transposed, cm)
125 void vvImageReader::ReadNkiImageTransform()
126 {
127   bool bRead=true;
128   typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
129   MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
130   readerTransfo->SetFileName(mInputFilenames[0]+".MACHINEORIENTATION");
131   try {
132     readerTransfo->Update();
133   } catch( itk::ExceptionObject & err ) {
134     bRead=false;
135   }
136
137   if (bRead) {
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]);
142
143     //From cm to mm
144     for(int i=0; i<3; i++)
145       mImage->GetTransform()->GetMatrix()->SetElement(i,3,10*mImage->GetTransform()->GetMatrix()->GetElement(i,3));
146
147     mImage->GetTransform()->Inverse();
148     mImage->UpdateReslice();
149   }
150 }
151 //------------------------------------------------------------------------------
152
153
154 //------------------------------------------------------------------------------
155 //Read transformation in ASCII format
156 void vvImageReader::ReadMatImageTransform()
157 {
158   std::string filename(mInputFilenames[0]+".mat");
159   std::ifstream f(filename.c_str());
160   if(f.is_open()) {
161     f.close();
162     
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();
168   }
169 }
170 //------------------------------------------------------------------------------
171 #endif
172