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