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