2 # ---------------------------------------------------------------------
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
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
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.
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
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 # ------------------------------------------------------------------------ */
27 #include "bbgdcmvtkGetXCoherentInfoGdcmReader.h"
28 #include "bbgdcmvtkPackage.h"
31 #include "vtkImageData.h"
32 #include <vtkIndent.h>
35 #include "gdcmFileHelper.h"
39 #include <gdcmReader.h>
40 #include <gdcmImageHelper.h>
41 #include <gdcmIPPSorter.h>
42 #include <vtkGDCMImageReader.h>
43 #include "vtkStringArray.h"
49 BBTK_ADD_BLACK_BOX_TO_PACKAGE(gdcmvtk,GetXCoherentInfoGdcmReader)
50 BBTK_BLACK_BOX_IMPLEMENTATION(GetXCoherentInfoGdcmReader,bbtk::AtomicBlackBox);
53 vtkImageData* GetXCoherentInfoGdcmReader::CreateDefaultImage()
56 int sizeX, sizeY, sizeZ;
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 );
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();
74 newImage->AllocateScalars(VTK_UNSIGNED_CHAR,1);
77 memset ( (void*)newImage->GetScalarPointer(), 0, sizeX*sizeY*1 );
78 for (i=0; i<sizeX; i++)
80 newImage->SetScalarComponentFromDouble(i,i,0, 0, 255 );
81 newImage->SetScalarComponentFromDouble(i,sizeY-1-i,0, 0, 255 );
88 void GetXCoherentInfoGdcmReader::Process()
90 printf("EED GetXCoherentInfoGdcmReader::Process Start\n");
91 // Read the *first* image file (a SET of file names is given as input)
92 f = GDCM_NAME_SPACE::File::New();
94 if ( bbGetInputIn().size()!=0 )
96 f->SetFileName( bbGetInputIn()[0] );
100 printf("EED GetXCoherentInfoGdcmReader::Process 1\n");
102 bool res = f->Load();
105 printf("EED GetXCoherentInfoGdcmReader::Process resultImage NULL\n");
107 bbSetOutputOut( CreateDefaultImage() );
112 // Get info from the first image file
114 std::vector<double> v_iop;
116 f->GetImageOrientationPatient(iop);
120 v_iop.push_back(iop[i]);
122 bbSetOutputIOP(v_iop );
123 printf("EED GetXCoherentInfoGdcmReader::Process 2\n");
125 std::vector<double> v_ipp;
127 f->GetImagePositionPatient(ipp);
131 v_ipp.push_back(ipp[i]);
133 bbSetOutputIPP(v_ipp );
135 // Add *all the files* to the SerieHelper
136 sh = GDCM_NAME_SPACE::SerieHelper::New();
137 std::vector<std::string> gii = bbGetInputIn();
139 printf("EED GetXCoherentInfoGdcmReader::Process 3\n");
141 for(std::vector<std::string>::iterator it = gii.begin();
145 sh->AddFileName(*it);
148 GDCM_NAME_SPACE::FileList::const_iterator it;
149 GDCM_NAME_SPACE::FileList *l;
151 printf("EED GetXCoherentInfoGdcmReader::Process 4\n");
152 // Should only contain one!
153 l = sh->GetFirstSingleSerieUIDFileSet();
154 printf("EED GetXCoherentInfoGdcmReader::Process 5\n");
158 printf("EED GetXCoherentInfoGdcmReader::Process Warnning (1) not coherent list/file \n");
161 printf("EED GetXCoherentInfoGdcmReader::Process 6\n");
163 double zspacing = 0.;
164 sh->OrderFileList(l); // this one should compute the *actual* Z Spacing!
165 zspacing = sh->GetZSpacing();
166 std::vector<double> v_pixelspacing;
167 v_pixelspacing.push_back( f->GetXSpacing() );
168 v_pixelspacing.push_back( f->GetYSpacing() );
169 v_pixelspacing.push_back( zspacing );
170 // if (f->GetZSize() != 1) {
171 // v_pixelspacing.push_back(f->GetZSpacing());
173 bbSetOutputPixelSpacing(v_pixelspacing);
175 VectorMapInfoDicom vectormapinfodicom;
179 std::string strTagValue; // read on disc
180 int iTag,sizeDicomTagsVector=bbGetInputDicomTags().size();
181 GDCM_NAME_SPACE::FileList::const_iterator iitt = l->begin();
184 for ( ; iitt != l->end(); ++iitt)
186 MapInfoDicom mapinfodicom;
187 for (iTag=0; iTag<sizeDicomTagsVector; iTag++)
189 key = bbGetInputDicomTags()[iTag];
191 if ( (key.size()==10) && (key[0] == 'D') && (key[5] == '_') )
193 sscanf(key.c_str(),"D%04hx_%04hx ",&group,&elem);
195 strTagValue = (*iitt)->GetEntryString(group,elem);
196 mapinfodicom.insert ( std::pair<std::string,std::string>(key,strTagValue) );
199 vectormapinfodicom.push_back( mapinfodicom );
201 bbSetOutputDicomInfo( vectormapinfodicom );
208 reader = vtkGdcmReader::New();
209 //EED 21 mars 2012 FLIP probleme ..PLOP..
210 reader->SetFlipY(false);
211 //reader->SetFileName( bbGetInputIn().c_str() );
212 reader->SetCoherentFileList(l);
215 bbSetOutputOut( reader->GetOutput() );
217 printf("EED GetXCoherentInfoGdcmReader::Process Warnning (2) not coherent list/file \n");
225 #if defined USE_GDCM2
226 void GetXCoherentInfoGdcmReader::Process()
228 // Read the *first* image file (a SET of file names is given as input)
229 gdcm::Reader *read = new gdcm::Reader();
230 reader->SetFileName( bbGetInputIn()[0].c_str() );
231 bool res = read->Read();
235 bbSetOutputOut(NULL);
239 // Get info from the first image file
240 const gdcm::File &f = read->GetFile();
242 std::vector<double> v_iop;
243 const gdcm::DataElement &deIop = f.GetDataSet().GetDataElement(gdcm::Tag(0x0020, 0x0037));
244 std::stringstream ss;
245 deIop.GetValue().Print(ss);
246 gdcm::Element<gdcm::VR::DS,gdcm::VM::VM6> iop;
250 v_iop.push_back((float)(iop[i]));
252 bbSetOutputIOP(v_iop );
254 std::vector<double> v_ipp;
255 const gdcm::DataElement &deIpp = f.GetDataSet().GetDataElement(gdcm::Tag((0x0020,0x0032)));
256 deIpp.GetValue().Print(ss);
257 gdcm::Element<gdcm::VR::DS,gdcm::VM::VM3> ipp;
261 v_ipp.push_back((float)(ipp[i]));
263 bbSetOutputIPP(v_ipp );
265 // Add *all the files* to the IPPsorter
267 s.SetComputeZSpacing( true );
268 s.SetZSpacingTolerance( 1e-3 );
269 std::vector<std::string> gii = bbGetInputIn();
272 //l = sh->GetFirstSingleSerieUIDFileSet();
273 // no Test if we have multiple series
277 double zspacing = 0.;
278 zspacing = s.GetZSpacing();
279 std::vector<double> v_pixelspacing = gdcm::ImageHelper::GetSpacingValue(f);
280 v_pixelspacing.push_back( v_pixelspacing[0] );
281 v_pixelspacing.push_back( v_pixelspacing[1] );
282 v_pixelspacing.push_back( zspacing );
283 bbSetOutputPixelSpacing(v_pixelspacing);
290 reader = vtkGDCMImageReader::New();
291 vtkStringArray *files = vtkStringArray::New();
292 std::vector< std::string >::const_iterator it = s.GetFilenames().begin();
293 for( ; it != s.GetFilenames().end(); ++it)
295 const std::string &f = *it;
296 files->InsertNextValue( f.c_str() );
298 reader->SetFileNames(files);
300 bbSetOutputOut( reader->GetOutput() );
305 void GetXCoherentInfoGdcmReader::bbUserSetDefaultValues()
308 std::vector<std::string> init;
311 //reader=NULL; /// \TODO fixme JPR
314 void GetXCoherentInfoGdcmReader::bbUserInitializeProcessing()
320 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
337 #if defined USE_GDCM2
338 void GetXCoherentInfoGdcmReader::bbUserFinalizeProcessing()
348 // EO namespace bbgdcmvtk