]> Creatis software - gdcm.git/blob - src/gdcmSerieHelper.cxx
Doxygenation
[gdcm.git] / src / gdcmSerieHelper.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSerieHelper.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/21 09:46:27 $
7   Version:   $Revision: 1.35 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmSerieHelper.h"
20 #include "gdcmDirList.h"
21 #include "gdcmFile.h"
22 #include "gdcmDictEntry.h" // for TranslateToKey
23 #include "gdcmDebug.h"
24 #include "gdcmUtil.h"
25
26 #include <math.h>
27 #include <vector>
28 #include <map>
29 #include <algorithm>
30 #include <stdio.h>  //for sscanf
31
32 namespace gdcm 
33 {
34 //-----------------------------------------------------------------------------
35
36 //-----------------------------------------------------------------------------
37 // Constructor / Destructor
38 /**
39  * \brief   Constructor from a given SerieHelper
40  */
41 SerieHelper::SerieHelper()
42 {
43    ClearAll();
44    UserLessThanFunction = 0;
45    DirectOrder = true;
46 }
47
48 /**
49  * \brief   Canonical destructor.
50  */
51 SerieHelper::~SerieHelper()
52 {
53    ClearAll();
54 }
55
56 /**
57  * \brief  Preventively, clear everything at constructor time.
58  *         ( use it at destructor time.)
59  */
60 void SerieHelper::ClearAll()
61 {
62    // For all the 'Single SerieUID' Filesets that may already exist 
63    FileList *l = GetFirstSingleSerieUIDFileSet();
64    while (l)
65    { 
66       // For all the gdcm::File of a File set
67       for (gdcm::FileList::iterator it  = l->begin();
68                                     it != l->end(); 
69                                   ++it)
70       {
71          (*it)->Delete(); // remove each entry
72       }
73       l->clear();
74       delete l;     // remove the container
75       l = GetNextSingleSerieUIDFileSet();
76    }
77 }
78
79 //-----------------------------------------------------------------------------
80
81 //-----------------------------------------------------------------------------
82
83 // Public
84 /**
85  * \brief add a gdcm::File to the Fileset corresponding to its Serie UID
86  * @param   filename Name of the file to deal with
87  */
88 void SerieHelper::AddFileName(std::string const &filename)
89 {
90    // Create a DICOM file
91    File *header = File::New();
92    header->SetLoadMode(LoadMode);
93    header->SetFileName( filename ); 
94    header->Load();
95
96    if ( header->IsReadable() )
97    {
98       int allrules = 1;
99       // First step : the user defined a set of rules for the DICOM file
100       // he is looking for.
101       // Make sure the file corresponds to his set of rules:
102
103       std::string s;
104       for(SerieExRestrictions::iterator it2 = ExRestrictions.begin();
105           it2 != ExRestrictions.end();
106           ++it2)
107       {
108          const ExRule &r = *it2;
109          s = header->GetEntryString( r.group, r.elem );
110          if ( !Util::CompareDicomString(s, r.value.c_str(), r.op) )
111          {
112            // Argh ! This rule is unmatched; let's just quit
113
114            allrules = 0;
115            break;
116          }
117       }
118
119       if ( allrules ) // all rules are respected:
120       {
121          // Allright! we have a found a DICOM that matches the user expectation. 
122          // Let's add it!
123
124          // 0020 000e UI REL Series Instance UID
125          const std::string &uid = header->GetEntryString(0x0020, 0x000e);
126          // if uid == GDCM_UNFOUND then consistently we should find GDCM_UNFOUND
127          // no need here to do anything special
128
129
130          if ( SingleSerieUIDFileSetHT.count(uid) == 0 )
131          {
132             gdcmDebugMacro(" New Serie UID :[" << uid << "]");
133             // create a std::list in 'uid' position
134             SingleSerieUIDFileSetHT[uid] = new FileList;
135          }
136          // Current Serie UID and DICOM header seems to match; add the file:
137          SingleSerieUIDFileSetHT[uid]->push_back( header );
138       }
139       else
140       {
141          // at least one rule was unmatched we need to deallocate the file:
142          header->Delete();
143       }
144    }
145    else
146    {
147       gdcmWarningMacro("Could not read file: " << filename );
148       header->Delete();
149    }
150 }
151
152 /**
153  * \brief add a gdcm::File to the first (and supposed to be unique) file set
154  *        of the gdcm::SerieHelper.
155  * \warning : this method should be used by aware users only!
156  *           Passing a gdcm::File* has the same effect than passing a file name!
157  * \todo : decide which one is wrong (the method, or the commentary)!
158  *           the following comment doesn't match the method :-(
159  *            User is supposed to know the files he want to deal with
160  *           and consider them they belong to the same Serie
161  *           (even if their Serie UID is different)
162  *           user will probabely OrderFileList() this list (actually, ordering
163  *           user choosen gdm::File is the sole interest of this method)
164  *           Moreover, using vtkGdcmReader::SetCoherentFileList() will avoid
165  *           vtkGdcmReader parsing twice the same files. 
166  *           *no* coherence check is performed, but those specified
167  *           by SerieHelper::AddRestriction()
168  * @param   header gdcm::File* of the file to deal with
169  */
170 void SerieHelper::AddGdcmFile(File *header)
171 {
172       int allrules = 1;
173       // First step the user has defined a set of rules for the DICOM 
174       // he is looking for.
175       // make sure the file correspond to his set of rules:
176       for(SerieRestrictions::iterator it =  Restrictions.begin();
177                                       it != Restrictions.end();
178                                     ++it)
179       {
180          const Rule &r = *it;
181          const std::string s;// = header->GetEntryValue( r.first );
182          if ( !Util::DicomStringEqual(s, r.second.c_str()) )
183          {
184            // Argh ! This rule is unmatch let's just quit
185            allrules = 0;
186            break;
187          }
188       }
189       if ( allrules ) // all rules are respected:
190       {
191          // Allright ! we have a found a DICOM that match the user expectation. 
192          // Let's add it !
193
194          const std::string &uid = "0";
195          // Serie UID of the gdcm::File* may be different.
196          // User is supposed to know what he wants
197
198          if ( SingleSerieUIDFileSetHT.count(uid) == 0 )
199          {
200             gdcmDebugMacro(" New Serie UID :[" << uid << "]");
201             // create a std::list in 'uid' position
202             SingleSerieUIDFileSetHT[uid] = new FileList;
203          }
204          // Current Serie UID and DICOM header seems to match; add the file:
205          SingleSerieUIDFileSetHT[uid]->push_back( header );
206       }
207          // Even if a rule was unmatch we don't deallocate the gdcm::File:
208 }
209
210 /**
211  * \brief add a rules for restricting a DICOM file to be in the serie we are
212  * trying to find. For example you can select only the DICOM file from a
213  * directory which would have a particular EchoTime==4.0.
214  * This method is a user level, value is not required to be formatted as a DICOM
215  * string
216  * @param   key  Target tag we want restrict on a given value
217  * @param value value to be checked to exclude File
218  * @param op  operator we want to use to check
219  */
220 void SerieHelper::AddRestriction(TagKey const &key, 
221                                  std::string const &value, int op)
222 {
223    ExRule r;
224    r.group = key[0];
225    r.elem  = key[1];
226    r.value = value;
227    r.op    = op;
228    ExRestrictions.push_back( r ); 
229 }
230
231 /**
232  * \brief Sets the root Directory
233  * @param   dir Name of the directory to deal with
234  * @param recursive whether we want explore recursively the root Directory
235  */
236 void SerieHelper::SetDirectory(std::string const &dir, bool recursive)
237 {
238    DirList dirList(dir, recursive); // OS specific
239   
240    DirListType filenames_list = dirList.GetFilenames();
241    for( DirListType::const_iterator it = filenames_list.begin(); 
242         it != filenames_list.end(); ++it)
243    {
244       AddFileName( *it );
245    }
246 }
247
248 /**
249  * \brief Sorts the given Fileset
250  * \warning This could be implemented in a 'Strategy Pattern' approach
251  *          But as I don't know how to do it, I leave it this way
252  *          BTW, this is also a Strategy, I don't know this is 
253  *          the best approach :)
254  */
255 void SerieHelper::OrderFileList(FileList *fileSet)
256 {
257
258    if ( SerieHelper::UserLessThanFunction )
259    {
260       UserOrdering( fileSet );
261       return; 
262    }
263    else if ( ImagePositionPatientOrdering( fileSet ) )
264    {
265       return ;
266    }
267    else if ( ImageNumberOrdering(fileSet ) )
268    {
269       return ;
270    }
271    else  
272    {
273       FileNameOrdering(fileSet );
274    }
275 }
276
277 /**
278  * \brief Elementary coherence checking of the files with the same Serie UID
279  * Only sizes and pixel type are checked right now ...
280  */ 
281 bool SerieHelper::IsCoherent(FileList *fileSet)
282 {
283    if(fileSet->size() == 1)
284    return true;
285
286    FileList::const_iterator it = fileSet->begin();
287
288    int nX =               (*it)->GetXSize();
289    int nY =               (*it)->GetYSize();
290    int pixelSize =        (*it)->GetPixelSize();
291    bool signedPixelData = (*it)->IsSignedPixelData();
292    it ++;
293    for ( ;
294          it != fileSet->end();
295        ++it)
296    {
297       if ( (*it)->GetXSize() != nX )
298          return false;
299       if ( (*it)->GetYSize() != nY )
300          return false;
301       if ( (*it)->GetPixelSize() != pixelSize )
302          return false;
303       if ( (*it)->IsSignedPixelData() != signedPixelData )
304          return false;
305       // probabely more is to be checked (?)      
306    }
307    return true;
308 }
309
310 #ifndef GDCM_LEGACY_REMOVE
311
312 FileList *SerieHelper::GetFirstCoherentFileList()
313 {
314    ItFileSetHt = SingleSerieUIDFileSetHT.begin();
315    if ( ItFileSetHt != SingleSerieUIDFileSetHT.end() )
316       return ItFileSetHt->second;
317    return NULL;
318 }
319
320
321 FileList *SerieHelper::GetNextCoherentFileList()
322 {
323    gdcmAssertMacro (ItFileSetHt != SingleSerieUIDFileSetHT.end());
324   
325    ++ItFileSetHt;
326    if ( ItFileSetHt != SingleSerieUIDFileSetHT.end() )
327       return ItFileSetHt->second;
328    return NULL;
329 }
330
331
332 FileList *SerieHelper::GetCoherentFileList(std::string SerieUID)
333 {
334    if ( SingleSerieUIDFileSetHT.count(SerieUID) == 0 )
335       return 0;     
336    return SingleSerieUIDFileSetHT[SerieUID];
337 }
338 #endif
339
340
341 /**
342  * \brief   Get the first Fileset while visiting the SingleSerieUIDFileSetmap
343  * @return  The first FileList (SingleSerieUIDFileSet) if found, otherwhise 0
344  */
345 FileList *SerieHelper::GetFirstSingleSerieUIDFileSet()
346 {
347    ItFileSetHt = SingleSerieUIDFileSetHT.begin();
348    if ( ItFileSetHt != SingleSerieUIDFileSetHT.end() )
349       return ItFileSetHt->second;
350    return NULL;
351 }
352
353 /**
354  * \brief   Get the next Fileset while visiting the SingleSerieUIDFileSetmap
355  * \note : meaningfull only if GetNextSingleSerieUIDFileSet() already called 
356  * @return  The next FileList (SingleSerieUIDFileSet) if found, otherwhise 0
357  */
358 FileList *SerieHelper::GetNextSingleSerieUIDFileSet()
359 {
360    gdcmAssertMacro (ItFileSetHt != SingleSerieUIDFileSetHT.end());
361   
362    ++ItFileSetHt;
363    if ( ItFileSetHt != SingleSerieUIDFileSetHT.end() )
364       return ItFileSetHt->second;
365    return NULL;
366 }
367
368 /**
369  * \brief   Get the SingleSerieUIDFileSet according to its Serie UID
370  * @param SerieUID SerieUID to retrieve
371  * \return pointer to the FileList (SingleSerieUIDFileSet) if found, otherwhise 0
372  */
373 FileList *SerieHelper::GetSingleSerieUIDFileSet(std::string SerieUID)
374 {
375    if ( SingleSerieUIDFileSetHT.count(SerieUID) == 0 )
376       return 0;     
377    return SingleSerieUIDFileSetHT[SerieUID];
378 }
379
380 /**
381  * \brief   Splits a Single SerieUID Fileset according to the Orientations
382  * @param fileSet File Set to be splitted
383  * \return  std::map of 'Xcoherent' File sets
384  */
385
386 XCoherentFileSetmap SerieHelper::SplitOnOrientation(FileList *fileSet)
387 {
388    XCoherentFileSetmap CoherentFileSet;
389
390    int nb = fileSet->size();
391    if (nb == 0 )
392       return CoherentFileSet;
393    float iop[6];
394
395    std::string strOrient;
396    std::ostringstream ossOrient;   
397    FileList::const_iterator it = fileSet->begin();
398    it ++;
399    for ( ;
400          it != fileSet->end();
401        ++it)
402    {     
403       // Information is in :      
404       // 0020 0037 : Image Orientation (Patient) or
405       // 0020 0035 : Image Orientation (RET)
406
407       // Let's build again the 'cosines' string, to be sure of it's format      
408       (*it)->GetImageOrientationPatient(iop);
409
410       ossOrient << iop[0];      
411       for (int i = 1; i < 6; i++)
412       {
413         ossOrient << "\\";
414         ossOrient << iop[i]; 
415       }      
416       strOrient = ossOrient.str();
417       ossOrient.str("");
418       // FIXME : is it a 'cleaner' way to initialize an ostringstream? 
419
420       if ( CoherentFileSet.count(strOrient) == 0 )
421       {
422          gdcmDebugMacro(" New Orientation :[" << strOrient << "]");
423          // create a File set in 'orientation' position
424          CoherentFileSet[strOrient] = new FileList;
425       }
426       // Current Orientation and DICOM header match; add the file:
427       CoherentFileSet[strOrient]->push_back( (*it) );
428    } 
429    return CoherentFileSet;
430 }
431
432 /**
433  * \brief   Splits a 'Single SerieUID' Fileset according to the Positions
434  * @param fileSet File Set to be splitted
435  * \return  std::map of 'Xcoherent' File sets
436  */
437
438 XCoherentFileSetmap SerieHelper::SplitOnPosition(FileList *fileSet)
439 {
440    XCoherentFileSetmap CoherentFileSet;
441
442    int nb = fileSet->size();
443    if (nb == 0 )
444       return CoherentFileSet;
445    float pos[3];
446    std::string strImPos;  // read on disc
447    std::ostringstream ossPosition;
448    std::string strPosition; // re computed
449    FileList::const_iterator it = fileSet->begin();
450    it ++;
451    for ( ;
452          it != fileSet->end();
453        ++it)
454    {     
455       // Information is in :      
456       // 0020,0032 : Image Position Patient
457       // 0020,0030 : Image Position (RET)
458
459       strImPos = (*it)->GetEntryString(0x0020,0x0032);
460       if ( strImPos == GDCM_UNFOUND)
461       {
462          gdcmWarningMacro( "Unfound Image Position Patient (0020,0032)");
463          strImPos = (*it)->GetEntryString(0x0020,0x0030); // For ACR-NEMA images
464          if ( strImPos == GDCM_UNFOUND )
465          {
466             gdcmWarningMacro( "Unfound Image Position (RET) (0020,0030)");
467             // User wants to split on the 'Position'
468             // No 'Position' info found.
469             // We return an empty Htable !
470             return CoherentFileSet;
471          }  
472       }
473
474       if ( sscanf( strImPos.c_str(), "%f \\%f \\%f ", 
475                                               &pos[0], &pos[1], &pos[2]) != 3 )
476       {
477             gdcmWarningMacro( "Wrong number for Position : ["
478                        << strImPos << "]" );
479              return CoherentFileSet;
480       }
481
482       // Let's build again the 'position' string, to be sure of it's format      
483
484       ossPosition << pos[0];      
485       for (int i = 1; i < 3; i++)
486       {
487         ossPosition << "\\";
488         ossPosition << pos[i]; 
489       }      
490       strPosition = ossPosition.str();
491       ossPosition.str("");
492             
493       if ( CoherentFileSet.count(strPosition) == 0 )
494       {
495          gdcmDebugMacro(" New Position :[" << strPosition << "]");
496          // create a File set in 'position' position
497          CoherentFileSet[strPosition] = new FileList;
498       }
499       // Current Position and DICOM header match; add the file:
500       CoherentFileSet[strPosition]->push_back( (*it) );
501    }   
502    return CoherentFileSet;
503 }
504
505 /**
506  * \brief   Splits a 'Single SerieUID' File set Coherent according to the
507  *          value of a given Tag
508  * @param fileSet File Set to be splitted
509  * @param   group  group number of the target Element
510  * @param   elem element number of the target Element
511  * \return  std::map of 'Xcoherent' File sets
512  */
513
514 XCoherentFileSetmap SerieHelper::SplitOnTagValue(FileList *fileSet, 
515                                                uint16_t group, uint16_t elem)
516 {
517    XCoherentFileSetmap CoherentFileSet;
518
519    int nb = fileSet->size();
520    if (nb == 0 )
521       return CoherentFileSet;
522
523    std::string strTagValue;  // read on disc
524
525    FileList::const_iterator it = fileSet->begin();
526    it ++;
527    for ( ;
528          it != fileSet->end();
529        ++it)
530    {     
531       // Information is in :      
532       // 0020,0032 : Image Position Patient
533       // 0020,0030 : Image Position (RET)
534
535       strTagValue = (*it)->GetEntryString(group,elem);
536       
537       if ( CoherentFileSet.count(strTagValue) == 0 )
538       {
539          gdcmDebugMacro(" New Tag Value :[" << strTagValue << "]");
540          // create a File set in 'position' position
541          CoherentFileSet[strTagValue] = new FileList;
542       }
543       // Current Tag value and DICOM header match; add the file:
544       CoherentFileSet[strTagValue]->push_back( (*it) );
545    }
546    return CoherentFileSet;
547 }
548
549 //-----------------------------------------------------------------------------
550 // Protected
551
552 //-----------------------------------------------------------------------------
553 // Private
554 /**
555  * \brief sorts the images, according to their Patient Position.
556  *
557  *  We may order, considering :
558  *   -# Image Position Patient
559  *   -# Image Number
560  *   -# file name
561  *   -# More to come :-)
562  * \note : FileList = std::vector<File* >
563  * @param fileList Coherent File list (same Serie UID) to sort
564  * @return false only if the header is bugged !
565  */
566 bool SerieHelper::ImagePositionPatientOrdering( FileList *fileList )
567 //based on Jolinda Smith's algorithm
568 {
569    //iop is calculated based on the file file
570    float cosines[6];
571    float normal[3];
572    float ipp[3];
573    float dist;
574    float min = 0, max = 0;
575    bool first = true;
576    int n=0;
577    std::vector<float> distlist;
578
579    //!\todo rewrite this for loop.
580    for ( FileList::const_iterator 
581          it = fileList->begin();
582          it != fileList->end(); ++it )
583    {
584       if ( first ) 
585       {
586          (*it)->GetImageOrientationPatient( cosines );
587       
588          // You only have to do this once for all slices in the volume. Next, 
589          // for each slice, calculate the distance along the slice normal 
590          // using the IPP ("Image Position Patient") tag.
591          // ("dist" is initialized to zero before reading the first slice) :
592          normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
593          normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
594          normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
595   
596          ipp[0] = (*it)->GetXOrigin();
597          ipp[1] = (*it)->GetYOrigin();
598          ipp[2] = (*it)->GetZOrigin();
599
600          dist = 0;
601          for ( int i = 0; i < 3; ++i )
602          {
603             dist += normal[i]*ipp[i];
604          }
605     
606          distlist.push_back( dist );
607
608          max = min = dist;
609          first = false;
610       }
611       else 
612       {
613          ipp[0] = (*it)->GetXOrigin();
614          ipp[1] = (*it)->GetYOrigin();
615          ipp[2] = (*it)->GetZOrigin();
616   
617          dist = 0;
618          for ( int i = 0; i < 3; ++i )
619          {
620             dist += normal[i]*ipp[i];
621          }
622
623          distlist.push_back( dist );
624
625          min = (min < dist) ? min : dist;
626          max = (max > dist) ? max : dist;
627       }
628       ++n;
629    }
630
631    // Then I order the slices according to the value "dist". Finally, once
632    // I've read in all the slices, I calculate the z-spacing as the difference
633    // between the "dist" values for the first two slices.
634    FileVector CoherentFileVector(n);
635    // CoherentFileVector.reserve( n );
636    CoherentFileVector.resize( n );
637    // gdcmAssertMacro( CoherentFileVector.capacity() >= n );
638
639    // Find out if min/max are coherent
640    if ( min == max )
641    {
642      gdcmWarningMacro("Looks like all images have the exact same image position"
643                       << ". No PositionPatientOrdering sort performed" );
644      return false;
645    }
646
647    float step = (max - min)/(n - 1);
648    int pos;
649    n = 0;
650     
651    //VC++ don't understand what scope is !! it -> it2
652    for (FileList::const_iterator it2  = fileList->begin();
653         it2 != fileList->end(); ++it2, ++n)
654    {
655       //2*n sort algo !!
656       //Assumption: all files are present (no one missing)
657       pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
658
659       // a Dicom 'Serie' may contain scout views
660       // and images may have differents directions
661       // -> More than one may have the same 'pos'
662       // Sorting has then NO meaning !
663       if (CoherentFileVector[pos]==NULL)
664          CoherentFileVector[pos] = *it2;
665       else
666       {
667          gdcmWarningMacro( "At least 2 files with same position."
668                         << " No PositionPatientOrdering sort performed");
669          return false;
670       }
671    }
672
673    fileList->clear();  // doesn't delete list elements, only nodes
674
675    if (DirectOrder)
676    {  
677       //VC++ don't understand what scope is !! it -> it3
678       for (FileVector::const_iterator it3  = CoherentFileVector.begin();
679            it3 != CoherentFileVector.end(); ++it3)
680       {
681          fileList->push_back( *it3 );
682       }
683    }
684    else // user asked for reverse order
685    {
686       FileVector::const_iterator it4;
687       it4 = CoherentFileVector.end();
688       do
689       {
690          it4--;
691          fileList->push_back( *it4 );
692       } while (it4 != CoherentFileVector.begin() );
693    } 
694
695    distlist.clear();
696    CoherentFileVector.clear();
697
698    return true;
699 }
700
701 bool SerieHelper::ImageNumberLessThan(File *file1, File *file2)
702 {
703   return file1->GetImageNumber() < file2->GetImageNumber();
704 }
705
706 bool SerieHelper::ImageNumberGreaterThan(File *file1, File *file2)
707 {
708   return file1->GetImageNumber() > file2->GetImageNumber();
709 }
710
711 /**
712  * \brief sorts the images, according to their Image Number
713  * \note Works only on bona fide files  (i.e image number is a character string
714  *                                      corresponding to an integer)
715  *             within a bona fide serie (i.e image numbers are consecutive)
716  * @param fileList  File set (same Serie UID) to sort 
717  * @return false if non bona fide stuff encountered
718  */
719 bool SerieHelper::ImageNumberOrdering(FileList *fileList) 
720 {
721    int min, max, pos;
722    int n = fileList->size();
723
724    FileList::const_iterator it = fileList->begin();
725    min = max = (*it)->GetImageNumber();
726
727    for (; it != fileList->end(); ++it, ++n)
728    {
729       pos = (*it)->GetImageNumber();
730       min = (min < pos) ? min : pos;
731       max = (max > pos) ? max : pos;
732    }
733
734    // Find out if image numbers are coherent (consecutive)
735    if ( min == max || max == 0 || max >= (n+min) )
736    {
737       gdcmWarningMacro( " 'Image numbers' not coherent. "
738                         << " No ImageNumberOrdering sort performed.");
739       return false;
740    }
741    if (DirectOrder) 
742       std::sort(fileList->begin(), fileList->end(), 
743                                           SerieHelper::ImageNumberLessThan );
744    else
745       std::sort(fileList->begin(), fileList->end(),
746                                           SerieHelper::ImageNumberGreaterThan );
747
748    return true;
749 }
750
751 bool SerieHelper::FileNameLessThan(File *file1, File *file2)
752 {
753    return file1->GetFileName() < file2->GetFileName();
754 }
755
756 bool SerieHelper::FileNameGreaterThan(File *file1, File *file2)
757 {
758    return file1->GetFileName() > file2->GetFileName();
759 }
760 /**
761  * \brief sorts the images, according to their File Name
762  * @param fileList Coherent File list (same Serie UID) to sort
763  * @return false only if the header is bugged !
764  */
765 bool SerieHelper::FileNameOrdering(FileList *fileList)
766 {
767    if (DirectOrder) 
768       std::sort(fileList->begin(), fileList->end(), 
769                                        SerieHelper::FileNameLessThan);
770    else
771       std::sort(fileList->begin(), fileList->end(), 
772                                        SerieHelper::FileNameGreaterThan);
773
774    return true;
775 }
776
777 /**
778  * \brief sorts the images, according to user supplied function
779  * @param fileList Coherent File list (same Serie UID) to sort
780  * @return false only if the header is bugged !
781  */
782 bool SerieHelper::UserOrdering(FileList *fileList)
783 {
784    std::sort(fileList->begin(), fileList->end(), 
785                                     SerieHelper::UserLessThanFunction);
786    if (!DirectOrder) 
787    {
788       std::reverse(fileList->begin(), fileList->end());
789    }
790    return true;
791 }
792
793 //-----------------------------------------------------------------------------
794 // Print
795 /**
796  * \brief   Canonical printer.
797  */
798 void SerieHelper::Print(std::ostream &os, std::string const &indent)
799 {
800    // For all the Coherent File lists of the gdcm::Serie
801    SingleSerieUIDFileSetmap::iterator itl = SingleSerieUIDFileSetHT.begin();
802    if ( itl == SingleSerieUIDFileSetHT.end() )
803    {
804       gdcmWarningMacro( "No SingleSerieUID File set found" );
805       return;
806    }
807    while (itl != SingleSerieUIDFileSetHT.end())
808    { 
809       os << "Serie UID :[" << itl->first << "]" << std::endl;
810
811       // For all the files of a SingleSerieUID File set
812       for (FileList::iterator it =  (itl->second)->begin();
813                                   it != (itl->second)->end(); 
814                                 ++it)
815       {
816          os << indent << " --- " << (*it)->GetFileName() << std::endl;
817       }
818       ++itl;
819    }
820 }
821
822 //-----------------------------------------------------------------------------
823 } // end namespace gdcm