]> Creatis software - bbtk.git/blob - packages/gdcmvtk/src/bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
#3107 BBTK Bug New Normal - branch vtk7itk4 compilation with vtk7
[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
52
53 vtkImageData* GetXCoherentInfoGdcmReader::CreateDefaultImage()
54 {
55         int i;
56         int sizeX, sizeY, sizeZ;
57         sizeX = 200;
58         sizeY = sizeX;
59         sizeZ = 1;
60         vtkImageData *newImage = vtkImageData::New();
61         newImage->Initialize();
62         newImage->SetSpacing( 1,1,1 );
63         newImage->SetDimensions(  sizeX,sizeY,sizeZ );
64         newImage->SetExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
65
66 //EED 2017-01-01 Migration VTK7
67 #if VTK_MAJOR_VERSION <= 5
68         newImage->SetWholeExtent(0,  sizeX-1,0,sizeY-1,0,sizeZ-1 );
69         newImage->SetScalarTypeToUnsignedChar();
70         newImage->SetNumberOfScalarComponents(1);
71         newImage->AllocateScalars();
72         newImage->Update();
73 #else
74         newImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
75 #endif
76
77         memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
78         for (i=0; i<sizeX; i++)
79         {
80                 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
81                 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
82         } // for i
83         return newImage;
84
85
86
87 #if defined USE_GDCM
88 void GetXCoherentInfoGdcmReader::Process()
89 {
90
91  // Read the *first* image file (a SET of file names is given as input) 
92    f = GDCM_NAME_SPACE::File::New();
93
94         if ( bbGetInputIn().size()!=0 )
95         {
96                 f->SetFileName( bbGetInputIn()[0] );
97         } else {
98                 f->SetFileName( "" );
99         } // if
100    
101    bool res = f->Load();  
102    if ( !res )
103    {
104                 printf("EED GetXCoherentInfoGdcmReader::Process resultImage NULL\n");
105         f->Delete();
106         bbSetOutputOut( CreateDefaultImage() );
107         return;
108    } // if res
109  // Get info from the first image file
110    int i;
111    std::vector<double> v_iop;
112    float iop[6];
113    f->GetImageOrientationPatient(iop);
114    
115    for(i=0; i< 6; i++)
116         {
117       v_iop.push_back(iop[i]);
118         } // for i
119    bbSetOutputIOP(v_iop );
120  
121    std::vector<double> v_ipp;
122    float ipp[3];
123    f->GetImagePositionPatient(ipp);
124    
125    for(i=0; i< 3; i++)
126         {
127       v_ipp.push_back(ipp[i]);
128         } // for i
129    bbSetOutputIPP(v_ipp );
130
131 // Add *all the files* to the SerieHelper
132    sh = GDCM_NAME_SPACE::SerieHelper::New();
133    std::vector<std::string> gii = bbGetInputIn();
134    
135    for(std::vector<std::string>::iterator it = gii.begin();
136                                           it != gii.end();
137                                         ++it)
138    {
139       sh->AddFileName(*it);
140    } //for vector
141
142
143    GDCM_NAME_SPACE::FileList::const_iterator it;
144    GDCM_NAME_SPACE::FileList *l;
145
146    // Should only contain one!
147    l = sh->GetFirstSingleSerieUIDFileSet();
148
149    int nbFiles;
150    double zspacing      = 0.;
151    nbFiles                      = l->size() ;
152    sh->OrderFileList(l); // this one should compute the *actual* Z Spacing!
153    zspacing             = sh->GetZSpacing();
154    std::vector<double> v_pixelspacing;
155    v_pixelspacing.push_back( f->GetXSpacing() );
156    v_pixelspacing.push_back( f->GetYSpacing() );
157    v_pixelspacing.push_back( zspacing );
158 //   if (f->GetZSize() != 1) {
159 //      v_pixelspacing.push_back(f->GetZSpacing());
160 //   }
161    bbSetOutputPixelSpacing(v_pixelspacing);
162    if (reader!=NULL)
163    { 
164       reader->Delete();
165       reader = NULL;
166    }
167    reader = vtkGdcmReader::New();
168         //EED 21 mars 2012  FLIP probleme  ..PLOP..
169         reader->SetFlipY(false);
170    //reader->SetFileName( bbGetInputIn().c_str() );
171    reader->SetCoherentFileList(l);
172    reader->Update();
173    reader->GetOutput();
174
175 printf("EED GetXCoherentInfoGdcmReader::Process \n");
176    bbSetOutputOut( reader->GetOutput() );
177 }
178 #endif
179 // endif USE_GDCM
180
181 #if defined USE_GDCM2
182 void GetXCoherentInfoGdcmReader::Process()
183 {
184  // Read the *first* image file (a SET of file names is given as input) 
185         gdcm::Reader *read = new gdcm::Reader();
186         reader->SetFileName( bbGetInputIn()[0].c_str() );
187   
188         bool res = read->Read();  
189         if ( !res )
190         {
191         delete read;
192         bbSetOutputOut(NULL);
193         return;
194         }
195     
196
197
198  // Get info from the first image file
199    const gdcm::File &f = read->GetFile();
200    int i;
201    std::vector<double> v_iop;
202    const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
203    std::stringstream ss;
204    deIop.GetValue().Print(ss);
205    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
206    iop.Read( ss );
207    for(i=0; i< 6; i++)
208         {
209         v_iop.push_back((float)(iop[i]));
210         }
211    bbSetOutputIOP(v_iop );
212  
213   std::vector<double> v_ipp;
214    const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
215    deIpp.GetValue().Print(ss);
216    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
217    ipp.Read( ss );
218         for(i=0; i< 3; i++)
219         {
220                 v_ipp.push_back((float)(ipp[i]));
221         } // for 3
222    bbSetOutputIPP(v_ipp );
223
224 // Add *all the files* to the IPPsorter
225      gdcm::IPPSorter s;
226          s.SetComputeZSpacing( true );
227      s.SetZSpacingTolerance( 1e-3 );
228      std::vector<std::string> gii = bbGetInputIn();
229      s.Sort(gii);
230
231          //l = sh->GetFirstSingleSerieUIDFileSet();
232          // no Test if we have multiple series
233          //s.GetFilenames();
234          //int nbFiles;
235
236         double zspacing = 0.;
237         zspacing = s.GetZSpacing();
238         std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
239         v_pixelspacing.push_back( v_pixelspacing[0] );
240         v_pixelspacing.push_back( v_pixelspacing[1] );
241         v_pixelspacing.push_back( zspacing );
242         bbSetOutputPixelSpacing(v_pixelspacing);
243
244    if (reader!=NULL)
245    { 
246       reader->Delete();
247       reader=NULL;
248    }
249    reader = vtkGDCMImageReader::New();
250    vtkStringArray *files = vtkStringArray::New();
251    std::vector< std::string >::const_iterator it = s.GetFilenames().begin();
252    for( ; it != s.GetFilenames().end(); ++it)
253    {
254           const std::string &f = *it;
255       files->InsertNextValue( f.c_str() );
256    }
257    reader->SetFileNames(files);
258    reader->Update();
259    bbSetOutputOut( reader->GetOutput() );
260 }
261 #endif
262 // endif USE_GDCM2
263
264 void GetXCoherentInfoGdcmReader::bbUserSetDefaultValues()
265 {
266    std::vector<std::string> init;
267    init.push_back("");
268    bbSetInputIn(init);  
269    //reader=NULL;   /// \TODO fixme JPR
270 }
271
272 void GetXCoherentInfoGdcmReader::bbUserInitializeProcessing()
273 {
274         
275 }
276
277 #if defined USE_GDCM
278 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
279 {
280    if(reader)
281         {
282       reader->Delete();
283         } // if reader
284    if(f)
285         {
286       f->Delete();
287         } // if f
288    if(sh)
289         {
290       sh->Delete();      
291         } // if sh
292 }
293 #endif
294
295 #if defined USE_GDCM2
296 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
297 {
298    if(reader)
299         {
300       reader->Delete();
301         }
302 }
303 #endif
304
305 }
306 // EO namespace bbgdcmvtk