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 sh->SetDropDuplicatePositions(true);
71 std::vector<std::string> lstAux;
72 std::vector<std::string>::iterator it;
73 for (it=im.begin(); it!=im.end(); ++it)
75 ///\TODO liberer les GDCM_NAME_SPACE::File a la fin! // JPR
76 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
81 fileVector.push_back(f);
83 lstAux.push_back(*it);
87 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
89 sh->OrderFileList(&fileVector);
90 spacing= sh->GetZSpacing();
93 for (i=0; i<fileVector.size(); i++)
95 im.push_back( (fileVector[i])->GetFileName() );
97 for (i=0; i<lstAux.size(); i++)
99 im.push_back( lstAux[i] );
102 std::sort( im.begin(), im.end() );
110 #if defined(USE_GDCM2)
112 double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
118 #if defined(USE_XERCES)
120 OutputModel::OutputModel(OutputModelParser *i_outparser) : m_outparser(i_outparser)
126 //OutputModel::setOutputModel(std::vector< std::map<std::string, std::string>> i_outputs)
128 // std::vector< std::map<std::string, std::string>>::iterator it = i_outputs.begin();
129 // for(; it != i_outputs.end(); it++)
131 // checkModel((*it));
135 bool OutputModel::checkModel(std::map<std::string, std::string> i_model, const std::string i_val)
138 if( i_model.find(i_val) != i_model.end() )
146 OutputModel::~OutputModel()
149 void OutputModel::setDB(const std::string i_db, const std::string i_table)
156 const std::string OutputModel::getTag()
165 #if defined(USE_GDCM)
166 sprintf(key,"D%04x_%04x", m_tag1.GetGroup(), m_tag1.GetElement());
168 #if defined(USE_GDCM2)
169 sprintf(key,"D%04x_%04x", m_tag2.GetGroup(), m_tag2.GetElement());
174 /* void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort)
176 std::vector<int> values;
181 getDBValues<int>(i_filenames, values);
185 getValues<int>(i_filenames, values);
190 for(int i = 0; i <i_filenames.size(); i++)
194 for(int index = tag_begin; index <= tag_end; index += tag_step)
196 std::vector<int>::iterator it_val = values.begin();
197 std::vector<std::string>::const_iterator it = i_filenames.begin();
198 for(;it != i_filenames.end(); it_val++, it++)
200 if((*it_val) == index)
202 o_sort.push_back((*it));
208 m_model->sort(i_filenames, o_sort);
211 void OutputModel::getDBValues(const std::vector<std::string> i_filenames, const std::string i_stag, std::map<std::string , T> &o_val)
216 //template<typename T>
217 //T OutputModel::getTypeTag()
219 // GDCM_NAME_SPACE::DictEntry* entry = GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
220 // entry->GetVR().GetVRType( entry->GetVR());
225 void OutputModel::getValues(const std::vector<std::string> i_filenames,const std::string i_tag, std::map< std::string, T> &o_val)
227 #if defined(USE_GDCM)
228 getReadValues(i_filenames, o_val);
230 #if defined(USE_GDCM2)
231 getScanValues(i_filenames, i_tag,o_val);
235 #if defined(USE_GDCM2)
236 // TO DO if some file don't provide this value, we are lost so return a map!
238 void OutputModel::getScanValues(const std::vector<std :: string> i_filenames, const std::string i_stag, std::map<std::string,T> &o_val)
241 sscanf(i_stag.c_str(),"D%04hx_%04hx ",&gr,&el);
242 gdcm::Tag tag(gr, el);
243 if (!tag.IsIllegal())
245 std::vector<std :: string> names(i_filenames);
250 std::vector<std :: string>::iterator it = names.begin();
251 for(;it != names.end(); it++)
252 boost::algorithm::replace_all((*it),"\\", "/");
253 scan.Scan(i_filenames);
254 // const gdcm::Scanner::TagToValue &mapping =
255 std::vector<std::string>::const_iterator it_file = i_filenames.begin();
256 for(; it_file != i_filenames.end(); it++)
258 if( scan.GetMapping((*it_file).c_str()).begin() != scan.GetMapping((*it_file).c_str()).end())
260 o_val[(*it)] = scan.GetMapping((*it_file).c_str()).begin()->second;
270 //const gdcm::VR::VRType OutputModel::getType(const std::string i_tag)
273 // sscanf(itag.c_str(),"D%04hx_%04hx ",&gr,&el);
274 // const gdcm::Global& g = gdcm::Global::GetInstance(); // sum of all knowledge !
275 // const gdcm::Dicts &dicts = g.GetDicts();
276 // const gdcm::Dict &dict = dicts.GetPublicDict(); // Part 6
277 // gdcm::DictEntry dictentry = dict.GetDictEntry(gdcm::Tag(gr, el));
278 // return dictentry.GetVR();
283 #if defined(USE_GDCM)
284 double OutputModel::orderFiles(std::vector<std::string> im, std::vector<std::string> &out)
287 std::vector<boost::shared_ptr<GDCM_NAME_SPACE::File> > fileVector;
288 std::vector<std::string> lstAux;
289 GDCM_NAME_SPACE::SerieHelper *sh = GDCM_NAME_SPACE::SerieHelper::New();
290 std::vector<std::string>::iterator it = im.begin();
291 for (; it!=im.end(); ++it)
293 GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
298 fileVector.push_back(f);
302 lstAux.push_back((*it));
305 if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
307 sh->OrderFileList(&fileVector);
308 spacing= sh->GetZSpacing();
311 for (i=0; i<fileVector.size(); i++)
313 out.push_back( (fileVector[i])->GetFileName() );
315 for (i=0; i<lstAux.size(); i++)
317 out.push_back( lstAux[i] );
322 std::sort( im.begin(), im.end() );
329 void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort, int level)
331 #if defined(USE_XERCES)
335 int tag_end = i_filenames.size();
337 std::map<std::string, std::string> model = m_outparser->getLevel(level);
338 std::map<std::string, std::string> values;
340 if( checkModel(model, OUTPUTMODEL_TAG(3)) && !b_db)
342 #if defined (USE_GDCM2)
344 //OutputSort< getType(model[OUTPUTMODEL_TAG(3)] > osort;
346 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
351 OutputSort<std::string> osort;
352 for(int i = 0; i < 3; i++)
354 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
356 osort.setTag(model[OUTPUTMODEL_TAG(i)],OUTPUTMODEL_TAG(i));
362 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
366 for(int i = 0; i <i_filenames.size(); i++)
367 values[i_filenames[i]] = i;
368 osort.sort(values,o_sort);
375 for(int i = 1; i < 4; i++)
377 if ( checkModel(model, OUTPUTMODEL_TAG(i)))
379 sscanf(model[OUTPUTMODEL_TAG(i)].c_str(), "%d", tags[i]);
383 if( checkModel(model, OUTPUTMODEL_TAG(3)) )
387 getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
391 getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
396 for(int i = 0; i <i_filenames.size(); i++)
397 values[i_filenames[i]] = i;
400 for(int index = tag_begin; index <= tag_end; index += tag_step)
402 std::map<std::string, std::string>::iterator it_val = values.begin();
403 for(;it_val != values.end(); it_val++)
405 if(it_val->second.c_str() )// == index)
407 o_sort.push_back(it_val->first.c_str());
412 if(checkModel(model, OUTPUTMODEL_TAG(4)))
415 sscanf(model[OUTPUTMODEL_TAG(4)].c_str(), "%d", lv);
416 sort(i_filenames,o_sort,lv);