]> Creatis software - bbtk.git/blob - packages/gdcmvtk/src/bbgdcmvtkGetXCoherentInfoGdcmReader.cxx
#3326 BBTK Feature New Normal - gdcm vtk GetXCoherentInfoGdcmReader box option...
[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  // Read the *first* image file (a SET of file names is given as input) 
91    f = GDCM_NAME_SPACE::File::New();
92
93         if ( bbGetInputIn().size()!=0 )
94         {
95                 f->SetFileName( bbGetInputIn()[0] );
96         } else {
97                 f->SetFileName( "" );
98         } // if
99
100    bool res = f->Load();  
101    if ( !res )
102    {
103                 printf("EED GetXCoherentInfoGdcmReader::Process resultImage NULL\n");
104         f->Delete();
105         bbSetOutputOut( CreateDefaultImage() );
106         return;
107    } // if res
108
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    GDCM_NAME_SPACE::FileList::const_iterator it;
143    GDCM_NAME_SPACE::FileList *l;
144
145    // Should only contain one!
146    l = sh->GetFirstSingleSerieUIDFileSet();
147
148         if (l==NULL)
149         {       
150                 return;
151         }
152
153    double zspacing = 0.;
154    sh->OrderFileList(l); // this one should compute the *actual* Z Spacing!
155    zspacing             = sh->GetZSpacing();
156    std::vector<double> v_pixelspacing;
157    v_pixelspacing.push_back( f->GetXSpacing() );
158    v_pixelspacing.push_back( f->GetYSpacing() );
159    v_pixelspacing.push_back( zspacing );
160 //   if (f->GetZSize() != 1) {
161 //      v_pixelspacing.push_back(f->GetZSpacing());
162 //   }
163    bbSetOutputPixelSpacing(v_pixelspacing);
164
165         VectorMapInfoDicom vectormapinfodicom;
166         uint16_t group;
167         uint16_t elem;
168         std::string key;
169         std::string strTagValue;  // read on disc
170         int iTag,sizeDicomTagsVector=bbGetInputDicomTags().size();
171         GDCM_NAME_SPACE::FileList::const_iterator iitt = l->begin();
172         //iitt ++;
173
174         for ( ; iitt != l->end(); ++iitt)
175         {
176                 MapInfoDicom mapinfodicom;
177                 for (iTag=0; iTag<sizeDicomTagsVector; iTag++)
178                 {
179                         key = bbGetInputDicomTags()[iTag];
180                         group = elem = 0;
181                         if ( (key.size()==10) && (key[0] == 'D') && (key[5] == '_') )
182                         {
183                                 sscanf(key.c_str(),"D%04hx_%04hx ",&group,&elem);  
184                         }// if key
185                         strTagValue = (*iitt)->GetEntryString(group,elem);
186                         mapinfodicom.insert ( std::pair<std::string,std::string>(key,strTagValue) );
187                 } // for iTag
188
189                 vectormapinfodicom.push_back( mapinfodicom );
190    } // for iitt
191    bbSetOutputDicomInfo( vectormapinfodicom );
192
193         if (bbGetInputReadRaw()==true)
194         {
195            if (reader!=NULL)
196            { 
197                   reader->Delete();
198                   reader = NULL;
199            }
200            reader = vtkGdcmReader::New();
201                 //EED 21 mars 2012  FLIP probleme  ..PLOP..
202                 reader->SetFlipY(false);
203            //reader->SetFileName( bbGetInputIn().c_str() );
204            reader->SetCoherentFileList(l);
205            reader->Update();
206            reader->GetOutput();
207            bbSetOutputOut( reader->GetOutput() );
208         } // if bGetInputReadRaw
209 }
210 #endif
211 // endif USE_GDCM
212
213 #if defined USE_GDCM2
214 void GetXCoherentInfoGdcmReader::Process()
215 {
216  // Read the *first* image file (a SET of file names is given as input) 
217         gdcm::Reader *read = new gdcm::Reader();
218         reader->SetFileName( bbGetInputIn()[0].c_str() );
219         bool res = read->Read();  
220         if ( !res )
221         {
222         delete read;
223         bbSetOutputOut(NULL);
224         return;
225         }
226     
227  // Get info from the first image file
228    const gdcm::File &f = read->GetFile();
229    int i;
230    std::vector<double> v_iop;
231    const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
232    std::stringstream ss;
233    deIop.GetValue().Print(ss);
234    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
235    iop.Read( ss );
236    for(i=0; i< 6; i++)
237    {
238         v_iop.push_back((float)(iop[i]));
239    }
240    bbSetOutputIOP(v_iop );
241  
242    std::vector<double> v_ipp;
243    const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
244    deIpp.GetValue().Print(ss);
245    gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
246    ipp.Read( ss );
247    for(i=0; i< 3; i++)
248         {
249                 v_ipp.push_back((float)(ipp[i]));
250         } // for 3
251    bbSetOutputIPP(v_ipp );
252
253 // Add *all the files* to the IPPsorter
254      gdcm::IPPSorter s;
255          s.SetComputeZSpacing( true );
256      s.SetZSpacingTolerance( 1e-3 );
257      std::vector<std::string> gii = bbGetInputIn();
258      s.Sort(gii);
259
260          //l = sh->GetFirstSingleSerieUIDFileSet();
261          // no Test if we have multiple series
262          //s.GetFilenames();
263          //int nbFiles;
264
265         double zspacing = 0.;
266         zspacing = s.GetZSpacing();
267         std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
268         v_pixelspacing.push_back( v_pixelspacing[0] );
269         v_pixelspacing.push_back( v_pixelspacing[1] );
270         v_pixelspacing.push_back( zspacing );
271         bbSetOutputPixelSpacing(v_pixelspacing);
272
273    if (reader!=NULL)
274    { 
275       reader->Delete();
276       reader=NULL;
277    }
278    reader = vtkGDCMImageReader::New();
279    vtkStringArray *files = vtkStringArray::New();
280    std::vector< std::string >::const_iterator it = s.GetFilenames().begin();
281    for( ; it != s.GetFilenames().end(); ++it)
282    {
283           const std::string &f = *it;
284       files->InsertNextValue( f.c_str() );
285    }
286    reader->SetFileNames(files);
287    reader->Update();
288    bbSetOutputOut( reader->GetOutput() );
289 }
290 #endif
291 // endif USE_GDCM2
292
293 void GetXCoherentInfoGdcmReader::bbUserSetDefaultValues()
294 {
295    reader=NULL;
296    std::vector<std::string> init;
297    init.push_back("");
298    bbSetInputIn(init);  
299    bbSetInputReadRaw(true);  
300    //reader=NULL;   /// \TODO fixme JPR
301 }
302
303 void GetXCoherentInfoGdcmReader::bbUserInitializeProcessing()
304 {
305         
306 }
307
308 #if defined USE_GDCM
309 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
310 {
311    if(reader)
312         {
313       reader->Delete();
314         } // if reader
315    if(f)
316         {
317       f->Delete();
318         } // if f
319    if(sh)
320         {
321       sh->Delete();      
322         } // if sh
323 }
324 #endif
325
326 #if defined USE_GDCM2
327 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
328 {
329    if(reader)
330         {
331       reader->Delete();
332         }
333 }
334 #endif
335
336 }
337 // EO namespace bbgdcmvtk