creaImageIO_lib
creaImageIOOutputModel.cpp
Go to the documentation of this file.
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 
28 
29 #include <creaImageIOOutputModel.h>
30 #include <boost/filesystem.hpp>
31 #include "boost/algorithm/string.hpp"
32 
33 #if defined(USE_GDCM)
34 #include <gdcmGlobal.h>
35 #include <gdcmFile.h>
36 #include <gdcmSerieHelper.h>
37 #include <gdcmFile.h>
38 #endif
39 
40 #if defined(USE_GDCM2)
41 #include <gdcmDict.h>
42 #include <gdcmDicts.h>
43 #include <gdcmGlobal.h>
44 #endif
45 
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;
52 #endif
53 
54 
55 namespace creaImageIO
56 {
57 
58  OutputModel::~OutputModel()
59  {
60  }
61 
62 #if defined(USE_GDCM)
63  double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
64  {
65  double spacing=1;
66  typedef std::vector<GDCM_NAME_SPACE::File* > FileList;
67  FileList fileVector;
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)
73  {
75  GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
76  f->SetFileName(*it);
77  f->Load();
78  if (f->IsReadable())
79  {
80  fileVector.push_back(f);
81  } else {
82  lstAux.push_back(*it);
83  }
84  } // for
85 
86  if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
87  {
88  sh->OrderFileList(&fileVector);
89  spacing= sh->GetZSpacing();
90  im.clear();
91  int i;
92  for (i=0; i<fileVector.size(); i++)
93  {
94  im.push_back( (fileVector[i])->GetFileName() );
95  }
96  for (i=0; i<lstAux.size(); i++)
97  {
98  im.push_back( lstAux[i] );
99  }
100  }else {
101  std::sort( im.begin(), im.end() );
102  }
103 
104  return spacing;
105  }
106 
107 #endif
108 
109 #if defined(USE_GDCM2)
110  // TO DO
111  double OutputModel::orderFilesWithZspacing(std::vector<std::string> &im)
112  {
113  return 1;
114  }
115 #endif
116 
117 #if defined(USE_XERCES)
118 
119  OutputModel::OutputModel(OutputModelParser *i_outparser) : m_outparser(i_outparser)
120  {
121  // Init
122  b_db = false;
123  }
124 
125  //OutputModel::setOutputModel(std::vector< std::map<std::string, std::string>> i_outputs)
126  //{
127  // std::vector< std::map<std::string, std::string>>::iterator it = i_outputs.begin();
128  // for(; it != i_outputs.end(); it++)
129  // {
130  // checkModel((*it));
131  // }
132  //}
133 
134  bool OutputModel::checkModel(std::map<std::string, std::string> i_model, const std::string i_val)
135  {
136  bool bres = false;
137  if( i_model.find(i_val) != i_model.end() )
138  {
139  bres = true;
140  }
141 
142  return bres;
143  }
144 
146  {
147  }
148  void OutputModel::setDB(const std::string i_db, const std::string i_table)
149  {
150  b_db = true;
151  m_db = i_db;
152  m_table = i_table;
153  }
154 
155  const std::string OutputModel::getTag()
156  {
157  char key[12];
158  if (!m_tag.empty())
159  {
160  return m_tag;
161  }
162  else
163  {
164 #if defined(USE_GDCM)
165  sprintf(key,"D%04x_%04x", m_tag1.GetGroup(), m_tag1.GetElement());
166 #endif
167 #if defined(USE_GDCM2)
168  sprintf(key,"D%04x_%04x", m_tag2.GetGroup(), m_tag2.GetElement());
169 #endif
170  return key;
171  }
172  }
173 /* void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort)
174  {
175  std::vector<int> values;
176  if(bDicom)
177  {
178  if(b_db)
179  {
180  getDBValues<int>(i_filenames, values);
181  }
182  else
183  {
184  getValues<int>(i_filenames, values);
185  }
186  }
187  else
188  {
189  for(int i = 0; i <i_filenames.size(); i++)
190  values.push_back(i);
191  }
192 
193  for(int index = tag_begin; index <= tag_end; index += tag_step)
194  {
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++)
198  {
199  if((*it_val) == index)
200  {
201  o_sort.push_back((*it));
202  break;
203  }
204  }
205  }
206  if(m_model != NULL)
207  m_model->sort(i_filenames, o_sort);
208  }*/
209  template<typename T>
210  void OutputModel::getDBValues(const std::vector<std::string> i_filenames, const std::string i_stag, std::map<std::string , T> &o_val)
211  {
212  }
213 
214 
215  //template<typename T>
216  //T OutputModel::getTypeTag()
217  //{
218  // GDCM_NAME_SPACE::DictEntry* entry = GDCM_NAME_SPACE::Global::GetDicts()->GetDefaultPubDict()->GetEntry(GetGroup(),GetElement());
219  // entry->GetVR().GetVRType( entry->GetVR());
220  //
221  //}
222 
223  template<typename T>
224  void OutputModel::getValues(const std::vector<std::string> i_filenames,const std::string i_tag, std::map< std::string, T> &o_val)
225  {
226 #if defined(USE_GDCM)
227  getReadValues(i_filenames, o_val);
228 #endif
229 #if defined(USE_GDCM2)
230  getScanValues(i_filenames, i_tag,o_val);
231 #endif
232  }
233 
234 #if defined(USE_GDCM2)
235  // TO DO if some file don't provide this value, we are lost so return a map!
236  template<typename T>
237  void OutputModel::getScanValues(const std::vector<std :: string> i_filenames, const std::string i_stag, std::map<std::string,T> &o_val)
238  {
239  uint16_t gr, el;
240  sscanf(i_stag.c_str(),"D%04hx_%04hx ",&gr,&el);
241  gdcm::Tag tag(gr, el);
242  if (!tag.IsIllegal())
243  {
244  std::vector<std :: string> names(i_filenames);
245  gdcm::Scanner scan;
246  scan.ClearTags();
247  scan.AddTag(tag);
248 
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++)
256  {
257  if( scan.GetMapping((*it_file).c_str()).begin() != scan.GetMapping((*it_file).c_str()).end())
258  {
259  o_val[(*it)] = scan.GetMapping((*it_file).c_str()).begin()->second;
260  }
261  else
262  {
263  o_val[(*it)] = "";
264  }
265  }
266  }
267  }
268 
269  //const gdcm::VR::VRType OutputModel::getType(const std::string i_tag)
270  //{
271  // uint16_t gr, el;
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();
278  //}
279 #endif
280 
281 
282 #if defined(USE_GDCM)
283  double OutputModel::orderFiles(std::vector<std::string> im, std::vector<std::string> &out)
284 {
285  double spacing = 1;
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)
291  {
292  GDCM_NAME_SPACE::File *f = GDCM_NAME_SPACE::File::New();
293  f->SetFileName(*it);
294  f->Load();
295  if (f->IsReadable())
296  {
297  fileVector.push_back(f);
298  }
299  else
300  {
301  lstAux.push_back((*it));
302  }
303  }
304  if ((fileVector.size()>1) && (sh->IsCoherent( &fileVector )))
305  {
306  sh->OrderFileList(&fileVector);
307  spacing= sh->GetZSpacing();
308  out.clear();
309  int i;
310  for (i=0; i<fileVector.size(); i++)
311  {
312  out.push_back( (fileVector[i])->GetFileName() );
313  }
314  for (i=0; i<lstAux.size(); i++)
315  {
316  out.push_back( lstAux[i] );
317  }
318  }
319  else
320  {
321  std::sort( im.begin(), im.end() );
322  }
323  return spacing;
324  }
325 #endif
326 
327 
328  void OutputModel::sort(const std::vector<std::string> i_filenames, std::vector<std::string> &o_sort, int level)
329  {
330 #if defined(USE_XERCES)
331  int tags[3];
332  tags[0] = 0;
333 
334  int tag_end = i_filenames.size();
335  int tag_step = 1;
336  std::map<std::string, std::string> model = m_outparser->getLevel(level);
337  std::map<std::string, std::string> values;
338 
339  if( checkModel(model, OUTPUTMODEL_TAG(3)) && !b_db)
340  {
341 #if defined (USE_GDCM2)
342 
343  //OutputSort< getType(model[OUTPUTMODEL_TAG(3)] > osort;
344 
345  getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
346 #endif
347  }
348  else
349  {
350  OutputSort<std::string> osort;
351  for(int i = 0; i < 3; i++)
352  {
353  if ( checkModel(model, OUTPUTMODEL_TAG(i)))
354  {
355  osort.setTag(model[OUTPUTMODEL_TAG(i)],OUTPUTMODEL_TAG(i));
356  }
357  }
358 
359  if(b_db)
360  {
361  getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
362  }
363  else
364  {
365  for(int i = 0; i <i_filenames.size(); i++)
366  values[i_filenames[i]] = i;
367  osort.sort(values,o_sort);
368  }
369  }
370 
371 
372 
373 
374  for(int i = 1; i < 4; i++)
375  {
376  if ( checkModel(model, OUTPUTMODEL_TAG(i)))
377  {
378  sscanf(model[OUTPUTMODEL_TAG(i)].c_str(), "%d", tags[i]);
379  }
380  }
381 
382  if( checkModel(model, OUTPUTMODEL_TAG(3)) )
383  {
384  if(b_db)
385  {
386  getDBValues<std::string>(i_filenames,model[OUTPUTMODEL_TAG(3)], values);
387  }
388  else
389  {
390  getValues<std::string>(i_filenames, model[OUTPUTMODEL_TAG(3)], values);
391  }
392  }
393  else
394  {
395  for(int i = 0; i <i_filenames.size(); i++)
396  values[i_filenames[i]] = i;
397  }
398 
399  for(int index = tag_begin; index <= tag_end; index += tag_step)
400  {
401  std::map<std::string, std::string>::iterator it_val = values.begin();
402  for(;it_val != values.end(); it_val++)
403  {
404  if(it_val->second.c_str() )// == index)
405  {
406  o_sort.push_back(it_val->first.c_str());
407  break;
408  }
409  }
410  }
411  if(checkModel(model, OUTPUTMODEL_TAG(4)))
412  {
413  int lv;
414  sscanf(model[OUTPUTMODEL_TAG(4)].c_str(), "%d", lv);
415  sort(i_filenames,o_sort,lv);
416  }
417 #endif
418  }
419 #endif
420 
421 }