]> Creatis software - bbtk.git/blob - packages/gdcmvtk/src/bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
#2951 BBTK Feature New Normal - Replace String box with vector functionality
[bbtk.git] / packages / gdcmvtk / src / bbgdcmvtkGetXCoherentInfoGdcmReader.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 "bbgdcmvtkGetXCoherentInfoGdcmReader.h"
28 #include "bbgdcmvtkPackage.h"
29
30 #include "gdcmFile.h"
31 #include "vtkImageData.h"
32 #include <vtkIndent.h>
33
34 #if defined USE_GDCM
35         #include "gdcmFileHelper.h"
36 #endif
37
38 #if defined USE_GDCM2
39         #include <gdcmReader.h>
40         #include <gdcmImageHelper.h>
41         #include <gdcmIPPSorter.h>
42         #include <vtkGDCMImageReader.h>
43         #include "vtkStringArray.h"
44 #endif
45
46 namespace bbgdcmvtk
47 {
48
49 BBTK_ADD_BLACK_BOX_TO_PACKAGE(gdcmvtk,GetXCoherentInfoGdcmReader)
50 BBTK_BLACK_BOX_IMPLEMENTATION(GetXCoherentInfoGdcmReader,bbtk::AtomicBlackBox);
51 #if defined USE_GDCM
52 void GetXCoherentInfoGdcmReader::Process()
53 {
54  // Read the *first* image file (a SET of file names is given as input) 
55    f = GDCM_NAME_SPACE::File::New();
56    f->SetFileName( bbGetInputIn()[0] );
57    bool res = f->Load();  
58    if ( !res )
59    {
60       f->Delete();
61       bbSetOutputOut(0);
62       return;
63    }
64  // Get info from the first image file
65    int i;
66    std::vector<double> v_iop;
67    float iop[6];
68    f->GetImageOrientationPatient(iop);
69    
70    for(i=0; i< 6; i++)
71       v_iop.push_back(iop[i]);
72    bbSetOutputIOP(v_iop );
73  
74    std::vector<double> v_ipp;
75    float ipp[3];
76    f->GetImagePositionPatient(ipp);
77    
78    for(i=0; i< 3; i++)
79       v_ipp.push_back(ipp[i]);
80    bbSetOutputIPP(v_ipp );
81
82 // Add *all the files* to the SerieHelper
83    sh = GDCM_NAME_SPACE::SerieHelper::New();
84    std::vector<std::string> gii = bbGetInputIn();
85    
86    for(std::vector<std::string>::iterator it = gii.begin();
87                                           it != gii.end();
88                                         ++it)
89    {
90       sh->AddFileName(*it);
91    }
92
93    GDCM_NAME_SPACE::FileList::const_iterator it;
94    GDCM_NAME_SPACE::FileList *l;
95
96    // Should only contain one!
97    l = sh->GetFirstSingleSerieUIDFileSet();
98
99    int nbFiles;
100    double zspacing = 0.;
101    nbFiles = l->size() ;
102    sh->OrderFileList(l); // this one should compute the *actual* Z Spacing!
103    zspacing = sh->GetZSpacing();
104    std::vector<double> v_pixelspacing;
105    v_pixelspacing.push_back( f->GetXSpacing() );
106    v_pixelspacing.push_back( f->GetYSpacing() );
107    v_pixelspacing.push_back( zspacing );
108 //   if (f->GetZSize() != 1) {
109 //      v_pixelspacing.push_back(f->GetZSpacing());
110 //   }
111    bbSetOutputPixelSpacing(v_pixelspacing);
112
113    if (reader!=NULL)
114    { 
115       reader->Delete();
116       reader=NULL;
117    }
118    reader = vtkGdcmReader::New();
119         
120         //EED 21 mars 2012  FLIP probleme  ..PLOP..
121         reader->SetFlipY(false);
122
123         
124    //reader->SetFileName( bbGetInputIn().c_str() );
125    reader->SetCoherentFileList(l);
126    reader->Update();
127    reader->GetOutput();
128
129    bbSetOutputOut( reader->GetOutput() );
130 }
131 #endif
132 // endif USE_GDCM
133
134 #if defined USE_GDCM2
135 void GetXCoherentInfoGdcmReader::Process()
136 {
137  // Read the *first* image file (a SET of file names is given as input) 
138         gdcm::Reader *read = new gdcm::Reader();
139         reader->SetFileName( bbGetInputIn()[0].c_str() );
140   
141         bool res = read->Read();  
142         if ( !res )
143         {
144         delete read;
145                 printf("EED GetXCoherentInfoGdcmReader::Process resultImage NULL\n");
146         bbSetOutputOut(NULL);
147         return;
148         }
149     
150
151
152  // Get info from the first image file
153    const gdcm::File &f = read->GetFile();
154    int i;
155    std::vector<double> v_iop;
156    const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
157    std::stringstream ss;
158    deIop.GetValue().Print(ss);
159    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
160    iop.Read( ss );
161    for(i=0; i< 6; i++)
162       v_iop.push_back((float)(iop[i]));
163    bbSetOutputIOP(v_iop );
164  
165   std::vector<double> v_ipp;
166    const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
167    deIpp.GetValue().Print(ss);
168    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
169    ipp.Read( ss );
170           for(i=0; i< 3; i++)
171                 v_ipp.push_back((float)(ipp[i]));
172    bbSetOutputIPP(v_ipp );
173
174 // Add *all the files* to the IPPsorter
175      gdcm::IPPSorter s;
176          s.SetComputeZSpacing( true );
177      s.SetZSpacingTolerance( 1e-3 );
178      std::vector<std::string> gii = bbGetInputIn();
179      s.Sort(gii);
180
181          //l = sh->GetFirstSingleSerieUIDFileSet();
182          // no Test if we have multiple series
183          //s.GetFilenames();
184          //int nbFiles;
185
186         double zspacing = 0.;
187         zspacing = s.GetZSpacing();
188         std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
189         v_pixelspacing.push_back( v_pixelspacing[0] );
190         v_pixelspacing.push_back( v_pixelspacing[1] );
191         v_pixelspacing.push_back( zspacing );
192         bbSetOutputPixelSpacing(v_pixelspacing);
193
194    if (reader!=NULL)
195    { 
196       reader->Delete();
197       reader=NULL;
198    }
199    reader = vtkGDCMImageReader::New();
200    vtkStringArray *files = vtkStringArray::New();
201    std::vector< std::string >::const_iterator it = s.GetFilenames().begin();
202    for( ; it != s.GetFilenames().end(); ++it)
203    {
204           const std::string &f = *it;
205       files->InsertNextValue( f.c_str() );
206    }
207    reader->SetFileNames(files);
208    reader->Update();
209    bbSetOutputOut( reader->GetOutput() );
210 }
211 #endif
212 // endif USE_GDCM2
213
214 void GetXCoherentInfoGdcmReader::bbUserSetDefaultValues()
215 {
216    std::vector<std::string> init;
217    init.push_back("");
218    bbSetInputIn(init);  
219    //reader=NULL;   /// \TODO fixme JPR
220 }
221
222 void GetXCoherentInfoGdcmReader::bbUserInitializeProcessing()
223 {
224         
225 }
226
227 #if defined USE_GDCM
228 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
229 {
230    if(reader)
231       reader->Delete();
232    if(f)
233       f->Delete();
234    if(sh)
235       sh->Delete();      
236 }
237 #endif
238
239 #if defined USE_GDCM2
240 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
241 {
242    if(reader)
243       reader->Delete();
244 }
245 #endif
246
247 }
248 // EO namespace bbgdcmvtk