]> Creatis software - bbtk.git/blob - packages/gdcmvtk/src/bbgdcmvtkGetInfoGdcmReader.cxx
87012ecd6b44ead87b82ddfb823e19f52a60ad29
[bbtk.git] / packages / gdcmvtk / src / bbgdcmvtkGetInfoGdcmReader.cxx
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la SantÈ)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and
11 #  abiding by the rules of distribution of free software. You can  use,
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B
13 #  license as circulated by CEA, CNRS and INRIA at the following URL
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability.
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------ */
26
27 #include "bbgdcmvtkGetInfoGdcmReader.h"
28 #include "bbgdcmvtkPackage.h"
29
30 #if defined USE_GDCM
31         #include "gdcmFile.h"
32         #include "gdcmFileHelper.h"
33         #include "vtkImageData.h"
34         #include "vtkGdcmReader.h"
35 #endif
36 #if defined USE_GDCM2
37         #include <gdcmReader.h>
38 //      #include "vtkImageData.h"       
39         #include <gdcmImageHelper.h>
40         #include <vtkGDCMImageReader.h>
41 #endif
42 #include <vtkIndent.h>
43
44 namespace bbgdcmvtk
45 {
46
47 BBTK_ADD_BLACK_BOX_TO_PACKAGE(gdcmvtk,GetInfoGdcmReader)
48 BBTK_BLACK_BOX_IMPLEMENTATION(GetInfoGdcmReader,bbtk::AtomicBlackBox);
49
50 #if defined USE_GDCM
51 void GetInfoGdcmReader::Process()
52 {
53         // Reset de reader, f
54         bbUserFinalizeProcessing();
55         
56    f = GDCM_NAME_SPACE::File::New();
57    f->SetFileName( bbGetInputIn() );
58    bool res = f->Load();  
59    if ( !res )
60    {
61       f->Delete();
62       bbSetOutputOut(0);
63       return;
64    }
65  // Get info from THE image file (only *one* as input)
66    int i;
67    std::vector<double> v_iop;
68    float iop[6];
69    f->GetImageOrientationPatient(iop);
70    
71    for(i=0; i< 6; i++)
72       v_iop.push_back(iop[i]);
73    bbSetOutputIOP(v_iop );
74  
75    std::vector<double> v_ipp;
76    float ipp[3];
77    f->GetImagePositionPatient(ipp);
78    
79    for(i=0; i< 3; i++)
80       v_ipp.push_back(ipp[i]);
81    bbSetOutputIPP(v_ipp );
82
83    std::vector<double> v_pixelspacing;
84    v_pixelspacing.push_back(f->GetXSpacing());
85    v_pixelspacing.push_back(f->GetYSpacing());
86    
87    if (f->GetZSize() != 1) 
88    {
89       v_pixelspacing.push_back(f->GetZSpacing());
90    }
91    bbSetOutputPixelSpacing(v_pixelspacing);
92    
93    //double interslice;
94     bbSetOutputInterSlice(f->GetZSpacing());
95    
96    reader = vtkGdcmReader::New();
97    reader->SetFileName( bbGetInputIn().c_str() );
98    reader->Update();
99    reader->GetOutput();
100
101    vtkIndent indent ;
102 //   reader->GetOutput()->PrintSelf(std::cout, indent);
103    bbSetOutputOut( reader->GetOutput() );
104 }
105 #endif
106
107 #if defined USE_GDCM2
108 void GetInfoGdcmReader::Process()
109 {
110         // Reset de reader, f
111         bbUserFinalizeProcessing();
112         gdcm::Reader *read = new gdcm::Reader();
113         read->SetFileName( bbGetInputIn().c_str());
114   
115         bool res = read->Read();  
116    if ( !res )
117    {
118       delete read;
119       bbSetOutputOut(0);
120       return;
121    }
122    
123  // Get info from THE image file (only *one* as input)
124    const gdcm::File &f = read->GetFile();
125    
126    // Image Orientation (Patient)
127    int i;
128    std::vector<double> v_iop;
129    const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
130    std::stringstream ss;
131    deIop.GetValue().Print(ss);
132    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
133    iop.Read( ss );
134    for(i=0; i< 6; i++)
135       v_iop.push_back((float)(iop[i]));
136    bbSetOutputIOP(v_iop );
137  
138    // Image Position (Patient)
139    std::vector<double> v_ipp;
140    const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
141    deIpp.GetValue().Print(ss);
142    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
143    ipp.Read( ss );
144           for(i=0; i< 3; i++)
145                 v_ipp.push_back((float)(ipp[i]));
146    bbSetOutputIPP(v_ipp );
147
148    std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
149    bbSetOutputPixelSpacing(v_pixelspacing);
150    
151    //double interslice; ?? ==1 NO?
152     bbSetOutputInterSlice(v_pixelspacing[2]);
153    
154    reader = vtkGDCMImageReader::New();
155    reader->SetFileName( bbGetInputIn().c_str() );
156    reader->Update();
157    reader->GetOutput();
158    vtkIndent indent ;
159    reader->GetOutput()->PrintSelf(std::cout, indent);
160    bbSetOutputOut( reader->GetOutput() );
161 }
162 #endif
163
164
165 void GetInfoGdcmReader::bbUserSetDefaultValues()
166 {
167 #if defined USE_GDCM2
168    reader=NULL;
169 #endif   
170 #if defined USE_GDCM
171    reader=NULL;
172    f=NULL;
173 #endif
174    bbSetInputIn("");  
175 }
176
177
178 void GetInfoGdcmReader::bbUserInitializeProcessing()
179 {
180 }
181
182
183 void GetInfoGdcmReader::bbUserFinalizeProcessing()
184 {
185 #if defined USE_GDCM2
186    if( reader )
187    {
188       reader->Delete();
189       reader=NULL;
190    }
191 #endif
192
193 #if defined USE_GDCM    
194    if( reader )
195    {
196       reader->Delete();
197       reader=NULL;
198    }   
199
200    if(f)
201    {
202       f->Delete();
203       f=NULL;
204    }    
205 #endif
206 }
207 }
208
209 // EO namespace bbgdcmvtk
210
211