]> Creatis software - gdcm.git/blob - src/gdcmSerieHelper.h
SerieHelper uses RefCounter.
[gdcm.git] / src / gdcmSerieHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSerieHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2005/11/14 15:50:33 $
7   Version:   $Revision: 1.28 $
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 #ifndef GDCMSERIEHELPER_H
20 #define GDCMSERIEHELPER_H
21
22 #include "gdcmCommon.h" 
23 #include "gdcmTagKey.h" 
24 #include "gdcmDebug.h"  // for LEGACY
25 #include "gdcmRefCounter.h"
26  
27 #include <vector>
28 #include <iostream>
29 #include <map>
30
31 namespace gdcm 
32 {
33 class File;
34
35
36    typedef std::vector<File* > FileList;
37    
38    /// \brief XCoherent stands for 'Extra Coherent', 
39    ///        (The name 'Coherent' would be enough but it was used before;
40    ///        I don't want to put a bomb in the code)
41    ///        Any 'better name' is welcome !
42    typedef std::map<std::string, FileList *> XCoherentFileSetmap; 
43       
44    typedef bool (*BOOL_FUNCTION_PFILE_PFILE_POINTER)(File *, File *);
45
46 //-----------------------------------------------------------------------------
47 /**
48  * \brief  
49  * - This class should be used for a stack of 2D dicom images.
50  *   It allows to explore (recursively or not) a directory and 
51  *   makes a set of 'Single SerieUID Filesets' 
52  *   It allows :
53  *   - to sort any of the 'Single SerieUID Fileset' on the image position.
54  *   - to split any of the Single SerieUID Filesets (better use this name than
55  *   'Coherent File List' : it's NOT a std::list, files are NOT coherent ...)
56  *    into several XCoherent Filesets 
57  *   XCoherent stands for 'Extra Coherent' (same orientation, or same position)
58  */
59 class GDCM_EXPORT SerieHelper  : public RefCounter
60 {
61    gdcmTypeMacro(SerieHelper);
62    
63 public:
64    /// SingleSerieUIDFileSetmap replaces the former CoherentFileListmap
65    /// ( List were actually std::vectors, and wher no coherent at all :
66    ///   They were only Single SeriesInstanceUID File sets)
67    typedef std::map<std::string, FileList *> SingleSerieUIDFileSetmap;
68
69    typedef std::vector<File* > FileVector;
70
71     static SerieHelper *New() {return new SerieHelper();}
72      
73    virtual ~SerieHelper();
74    void Print(std::ostream &os = std::cout, std::string const &indent = "" );
75
76    /// \todo should return bool or throw error ?
77    void AddFileName(std::string const &filename);
78    void AddGdcmFile(File *header);
79
80    void SetDirectory(std::string const &dir, bool recursive=false);
81    bool IsCoherent(FileList *fileSet);
82    void OrderFileList(FileList *fileSet);
83    
84    /// \brief Gets the FIRST Single SerieUID Fileset.
85    ///        Deprecated; kept not to break the API
86    /// \note Caller must call OrderFileList first
87    /// @return the (first) Single SerieUID Fileset
88    const FileList &GetFileList()
89                            { return *SingleSerieUIDFileSetHT.begin()->second; }
90   
91    GDCM_LEGACY(   FileList *GetFirstCoherentFileList()  );
92    GDCM_LEGACY(   FileList *GetNextCoherentFileList()   );
93    GDCM_LEGACY(   FileList *GetCoherentFileList(std::string serieUID)  );
94
95    FileList *GetFirstSingleSerieUIDFileSet();
96    FileList *GetNextSingleSerieUIDFileSet();
97    FileList *GetSingleSerieUIDFileSet(std::string serieUID);
98    /// brief returns the 'Series Instance UID' Single SerieUID FileSet
99    std::string GetCurrentSerieUIDFileSetUID()
100                              { return  (*ItFileSetHt).first; }
101    /// All the following allow user to restrict DICOM file to be part
102    /// of a particular serie
103    GDCM_LEGACY( void AddRestriction(TagKey const &key, std::string const &value) );
104    void AddRestriction(TagKey const &key, std::string const &value, int op);
105  
106 /**
107  * \brief Sets the LoadMode as a boolean string. 
108  *        LD_NOSEQ, LD_NOSHADOW, LD_NOSHADOWSEQ
109  *        ... (nothing more, right now)
110  *        WARNING : before using LD_NOSHADOW, be sure *all* your files
111  *        contain accurate values in the 0x0000 element (if any) 
112  *        of *each* Shadow Group. The parser will fail if the size is wrong !
113  * @param   mode Load mode to be used    
114  */
115    void SetLoadMode (int mode) { LoadMode = mode; }
116
117 /// Brief User wants the files to be sorted Direct Order (default value)
118    void SetSortOrderToDirect()  { DirectOrder = true;  }
119
120 /// Brief User wants the files to be sorted Reverse Order 
121    void SetSortOrderToReverse() { DirectOrder = false; }
122
123    /// to allow user to give is own comparison function
124    void SetUserLessThanFunction( BOOL_FUNCTION_PFILE_PFILE_POINTER userFunc ) 
125                         { UserLessThanFunction = userFunc; }  
126
127    XCoherentFileSetmap SplitOnOrientation(FileList *fileSet); 
128    XCoherentFileSetmap SplitOnPosition(FileList *fileSet); 
129    XCoherentFileSetmap SplitOnTagValue(FileList *fileSet,
130                                                uint16_t group, uint16_t elem);
131 protected :
132    SerieHelper();
133    
134 private:
135    void ClearAll();
136    bool UserOrdering(FileList *fileSet);
137    bool ImagePositionPatientOrdering(FileList *fileSet);
138    bool ImageNumberOrdering(FileList *fileSet);
139    bool FileNameOrdering(FileList *fileSet);
140    
141    static bool ImageNumberLessThan(File *file1, File *file2);
142    static bool ImageNumberGreaterThan(File *file1, File *file2);
143    static bool FileNameLessThan(File *file1, File *file2);
144    static bool FileNameGreaterThan(File *file1, File *file2);
145
146 //Attributes:
147    
148    SingleSerieUIDFileSetmap SingleSerieUIDFileSetHT;
149    SingleSerieUIDFileSetmap::iterator ItFileSetHt;
150    
151    typedef std::pair<TagKey, std::string> Rule;
152    typedef std::vector<Rule> SerieRestrictions;
153    SerieRestrictions Restrictions;
154    
155    // New style for (extented) Rules (Moreover old one doesn't compile)
156    typedef struct {
157       uint16_t group;
158       uint16_t elem;
159       std::string value;
160       int op;
161    } ExRule;
162    typedef std::vector<ExRule> SerieExRestrictions;
163    SerieExRestrictions ExRestrictions;
164
165    /// \brief Bit string integer (each one considered as a boolean)
166    ///        Bit 0 : Skip Sequences,    if possible
167    ///        Bit 1 : Skip Shadow Groups if possible
168    ///        Probabely, some more to add
169    int LoadMode;
170
171    /// \brief whether we want to sort in direct order or not (reverse order).
172    ///        To be used by aware user only
173    bool DirectOrder;
174
175    /// \brief If user knows more about his images than gdcm does,
176    ///        he may supply his own comparison function.
177     BOOL_FUNCTION_PFILE_PFILE_POINTER UserLessThanFunction;
178
179 };
180
181 } // end namespace gdcm
182
183 //-----------------------------------------------------------------------------
184 #endif