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