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