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 # ------------------------------------------------------------------------
29 #include <creaImageIOOutputModel.h>
30 #include <boost/filesystem.hpp>
31 #include "boost/algorithm/string.hpp"
34 #include <gdcmGlobal.h>
36 #include <gdcmSerieHelper.h>
40 #if defined(USE_GDCM2)
42 #include <gdcmDicts.h>
43 #include <gdcmGlobal.h>
46 #if defined(USE_XERCES)
47 #include <xercesc/dom/DOM.hpp>
48 #include <xercesc/dom/DOMDocument.hpp>
49 #include <xercesc/util/XMLString.hpp>
50 #include <xercesc/util/PlatformUtils.hpp>
51 using namespace xercesc;
58 OutputModel::~OutputModel()
63 double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
66 typedef std::vector<GDCM_NAME_SPACE::File* > FileList;
68 //GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
69 GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
70 std::vector<std::string> lstAux;
71 std::vector<std::string>::iterator it;
72 for (it=im.begin(); it!=im.end(); ++it)
74 ///\TODO liberer les GDCM_NAME_SPACE::File a la fin! // JPR
75 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
80 fileVector.push_back(f);
82 lstAux.push_back(*it);
86 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
88 sh->OrderFileList(&fileVector);
89 spacing= sh->GetZSpacing();
92 for (i=0; i<fileVector.size(); i++)
94 im.push_back( (fileVector[i])->GetFileName() );
96 for (i=0; i<lstAux.size(); i++)
98 im.push_back( lstAux[i] );
101 std::sort( im.begin(), im.end() );
109 #if defined(USE_GDCM2)
111 double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
117 #if defined(USE_XERCES)
119 OutputModel::OutputModel(OutputModelParser *i_outparser) : m_outparser(i_outparser)
125 //OutputModel::setOutputModel(std::vector< std::map<std::string, std::string>> i_outputs)
127 // std::vector< std::map<std::string, std::string>>::iterator it = i_outputs.begin();
128 // for(; it != i_outputs.end(); it++)
130 // checkModel((*it));
134 bool OutputModel::checkModel(std::map<std::string, std::string> i_model, const std::string i_val)
137 if( i_model.find(i_val) != i_model.end() )
145 OutputModel::~OutputModel()
148 void OutputModel::setDB(const std::string i_db, const std::string i_table)
155 const std::string OutputModel::getTag()
164 #if defined(USE_GDCM)
165 sprintf(key,"D%04x_%04x", m_tag1.GetGroup(), m_tag1.GetElement());
167 #if defined(USE_GDCM2)
168 sprintf(key,"D%04x_%04x", m_tag2.GetGroup(), m_tag2.GetElement());
173 /* void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort)
175 std::vector<int> values;
180 getDBValues<int>(i_filenames, values);
184 getValues<int>(i_filenames, values);
189 for(int i = 0; i <i_filenames.size(); i++)
193 for(int index = tag_begin; index <= tag_end; index += tag_step)
195 std::vector<int>::iterator it_val = values.begin();
196 std::vector<std::string>::const_iterator it = i_filenames.begin();
197 for(;it != i_filenames.end(); it_val++, it++)
199 if((*it_val) == index)
201 o_sort.push_back((*it));
207 m_model->sort(i_filenames, o_sort);
210 void OutputModel::getDBValues(const std::vector<std::string> i_filenames, const std::string i_stag, std::map<std::string , T> &o_val)
215 //template<typename T>
216 //T OutputModel::getTypeTag()
218 // GDCM_NAME_SPACE::DictEntry* entry = GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
219 // entry->GetVR().GetVRType( entry->GetVR());
224 void OutputModel::getValues(const std::vector<std::string> i_filenames,const std::string i_tag, std::map< std::string, T> &o_val)
226 #if defined(USE_GDCM)
227 getReadValues(i_filenames, o_val);
229 #if defined(USE_GDCM2)
230 getScanValues(i_filenames, i_tag,o_val);
234 #if defined(USE_GDCM2)
235 // TO DO if some file don't provide this value, we are lost so return a map!
237 void OutputModel::getScanValues(const std::vector<std :: string> i_filenames, const std::string i_stag, std::map<std::string,T> &o_val)
240 sscanf(i_stag.c_str(),"D%04hx_%04hx ",&gr,&el);
241 gdcm::Tag tag(gr, el);
242 if (!tag.IsIllegal())
244 std::vector<std :: string> names(i_filenames);
249 std::vector<std :: string>::iterator it = names.begin();
250 for(;it != names.end(); it++)
251 boost::algorithm::replace_all((*it),"\\", "/");
252 scan.Scan(i_filenames);
253 // const gdcm::Scanner::TagToValue &mapping =
254 std::vector<std::string>::const_iterator it_file = i_filenames.begin();
255 for(; it_file != i_filenames.end(); it++)
257 if( scan.GetMapping((*it_file).c_str()).begin() != scan.GetMapping((*it_file).c_str()).end())
259 o_val[(*it)] = scan.GetMapping((*it_file).c_str()).begin()->second;
269 //const gdcm::VR::VRType OutputModel::getType(const std::string i_tag)
272 // sscanf(itag.c_str(),"D%04hx_%04hx ",&gr,&el);
273 // const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
274 // const gdcm::Dicts &dicts = g.GetDicts();
275 // const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
276 // gdcm::DictEntry dictentry = dict.GetDictEntry(gdcm::Tag(gr, el));
277 // return dictentry.GetVR();
282 #if defined(USE_GDCM)
283 double OutputModel::orderFiles(std::vector<std::string> im, std::vector<std::string> &out)
286 std::vector<boost::shared_ptr<GDCM_NAME_SPACE::File> > fileVector;
287 std::vector<std::string> lstAux;
288 GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
289 std::vector<std::string>::iterator it = im.begin();
290 for (; it!=im.end(); ++it)
292 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
297 fileVector.push_back(f);
301 lstAux.push_back((*it));
304 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
306 sh->OrderFileList(&fileVector);
307 spacing= sh->GetZSpacing();
310 for (i=0; i<fileVector.size(); i++)
312 out.push_back( (fileVector[i])->GetFileName() );
314 for (i=0; i<lstAux.size(); i++)
316 out.push_back( lstAux[i] );
321 std::sort( im.begin(), im.end() );
328 void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort, int level)
330 #if defined(USE_XERCES)
334 int tag_end = i_filenames.size();
336 std::map<std::string, std::string> model = m_outparser->getLevel(level);
337 std::map<std::string, std::string> values;
339 if( checkModel(model, OUTPUTMODEL_TAG(3)) && !b_db)
341 #if defined (USE_GDCM2)
343 //OutputSort< getType(model[OUTPUTMODEL_TAG(3)] > osort;
345 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
350 OutputSort<std::string> osort;
351 for(int i = 0; i < 3; i++)
353 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
355 osort.setTag(model[OUTPUTMODEL_TAG(i)],OUTPUTMODEL_TAG(i));
361 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
365 for(int i = 0; i <i_filenames.size(); i++)
366 values[i_filenames[i]] = i;
367 osort.sort(values,o_sort);
374 for(int i = 1; i < 4; i++)
376 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
378 sscanf(model[OUTPUTMODEL_TAG(i)].c_str(), "%d", tags[i]);
382 if( checkModel(model, OUTPUTMODEL_TAG(3)) )
386 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
390 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
395 for(int i = 0; i <i_filenames.size(); i++)
396 values[i_filenames[i]] = i;
399 for(int index = tag_begin; index <= tag_end; index += tag_step)
401 std::map<std::string, std::string>::iterator it_val = values.begin();
402 for(;it_val != values.end(); it_val++)
404 if(it_val->second.c_str() )// == index)
406 o_sort.push_back(it_val->first.c_str());
411 if(checkModel(model, OUTPUTMODEL_TAG(4)))
414 sscanf(model[OUTPUTMODEL_TAG(4)].c_str(), "%d", lv);
415 sort(i_filenames,o_sort,lv);