]> Creatis software - clitk.git/blob - vv/vvImageReader.cxx
5c6eba3ef40d7ffb3e8da0b5dc3b2a2ae54a2709
[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
26 //------------------------------------------------------------------------------
27 vvImageReader::vvImageReader()
28 {
29   mImage = NULL;
30   mInputFilenames.resize(0);
31   mLastError = "";
32   mType = UNDEFINEDIMAGETYPE;
33 }
34 //------------------------------------------------------------------------------
35
36
37 //------------------------------------------------------------------------------
38 vvImageReader::~vvImageReader() { }
39 //------------------------------------------------------------------------------
40
41
42 //------------------------------------------------------------------------------
43 void vvImageReader::Update()
44 {
45   Update(mType);
46 }
47 //------------------------------------------------------------------------------
48
49
50 //------------------------------------------------------------------------------
51 void vvImageReader::Update(LoadedImageType type)
52 {
53   itk::ImageIOBase::Pointer reader = itk::ImageIOFactory::CreateImageIO(mInputFilenames[0].c_str(), itk::ImageIOFactory::ReadMode);
54   if (!reader) {
55     mLastError="Unable to read file.";
56   }
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   //CALL_FOR_ALL_DIMS(dim,UpdateWithDim,inputPixelType);
72   mType = type;
73   mDim = dim;
74   mInputPixelType=inputPixelType;
75   this->start(); //Start heavy read operation in a separate thread
76   while (this->isRunning())
77     {
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   mInputFilenames = filenames;
117 }
118 //------------------------------------------------------------------------------
119
120
121 //------------------------------------------------------------------------------
122 //Read transformation in NKI format (Xdr, transposed, cm)
123 void vvImageReader::ReadNkiImageTransform()
124 {
125     bool bRead=true;
126     typedef itk::ImageFileReader< itk::Image< double, 2 > > MatrixReaderType;
127     MatrixReaderType::Pointer readerTransfo = MatrixReaderType::New();
128     readerTransfo->SetFileName(mInputFilenames[0]+".MACHINEORIENTATION");
129     try
130     {   readerTransfo->Update();
131     }
132     catch( itk::ExceptionObject & err )
133     {   bRead=false;
134     }
135         
136     if (bRead)
137     {   double mat[16];
138
139         //Transpose matrix (NKI format)
140         for(int j=0; j<4; j++)
141             for(int i=0; i<4; i++)
142                 mat[4*j+i]=readerTransfo->GetOutput()->GetBufferPointer()[4*i+j];
143
144         //From cm to mm
145         for(int i=0; i<3; i++)
146             mat[4*i+3]*=10;
147
148         //Set Transformation
149         vtkSmartPointer<vtkTransform> pt = vtkSmartPointer<vtkTransform>::New();
150         pt->SetMatrix( mat );
151         pt->Inverse();
152         mImage->SetTransform( pt );
153     }
154 }
155 //------------------------------------------------------------------------------
156 #endif
157