]> Creatis software - gdcm.git/blob - src/gdcmSerieHelper.h
ENH: Try to sync gdcm CVS and gdcm 1.2. ~2000 lines of changes, please be gentle...
[gdcm.git] / src / gdcmSerieHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSerieHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2006/02/16 20:06:15 $
7   Version:   $Revision: 1.37 $
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 "gdcmRefCounter.h"
23 #include "gdcmTagKey.h" 
24 #include "gdcmDebug.h"  // for LEGACY
25  
26 #include <vector>
27 #include <iostream>
28 #include <map>
29
30 namespace gdcm 
31 {
32 class File;
33
34    typedef std::vector<File* > FileList;
35 #ifndef GDCM_LEGACY_REMOVE
36 typedef std::vector<File* > GdcmFileList;
37 #endif
38
39    /// \brief XCoherent stands for 'Extra Coherent',
40    ///        (The name 'Coherent' would be enough but it was used before;
41    ///        I don't want to put a bomb in the code)
42    ///        Any 'better name' is welcome !
43    typedef std::map<std::string, FileList *> XCoherentFileSetmap;
44
45    typedef bool (*BOOL_FUNCTION_PFILE_PFILE_POINTER)(File *, File *);
46
47 //-----------------------------------------------------------------------------
48 /**
49  * \brief  
50  *  This class should be used for a stack of 2D dicom images.
51  *
52  *   - It allows to explore (recursively or not) a directory and 
53  *   makes a set of 'Single SerieUID Filesets' 
54  *   - It allows :
55  *   - - to sort any of the 'Single SerieUID Fileset' on the image position.
56  *   - - to split any of the Single SerieUID Filesets (better use this name than
57  *   'CoherentFileList' : it's NOT a std::list, files are NOT coherent ...)
58  *    into several XCoherent Filesets 
59  *   XCoherent stands for 'Extra Coherent' (same orientation, or same position)
60  */
61 class GDCM_EXPORT SerieHelper  : public RefCounter
62 {
63    gdcmTypeMacro(SerieHelper);
64    
65 public:
66    /// SingleSerieUIDFileSetmap replaces the former CoherentFileListmap
67    /// ( List were actually std::vectors, and wher no coherent at all :
68    ///   They were only Single SeriesInstanceUID File sets)
69    typedef std::map<std::string, FileList *> SingleSerieUIDFileSetmap;
70
71    typedef std::vector<File* > FileVector;
72
73 /// \brief Constructs a SerieHelper with a RefCounter
74     static SerieHelper *New() {return new SerieHelper();}
75      
76    virtual ~SerieHelper();
77    void Print(std::ostream &os = std::cout, std::string const &indent = "" );
78
79    /// \todo should return bool or throw error ?
80    void AddFileName(std::string const &filename);
81    bool AddFile(File *header);
82 #ifndef GDCM_LEGACY_REMOVE
83    bool AddGdcmFile(File* header) { return AddFile(header); }
84 #endif
85
86    void SetDirectory(std::string const &dir, bool recursive=false);
87    bool IsCoherent(FileList *fileSet);
88    void OrderFileList(FileList *fileSet);
89    void Clear() { ClearAll(); }
90
91    /// \brief Gets the FIRST Single SerieUID Fileset.
92    ///        Deprecated; kept not to break the API
93    /// \note Caller must call OrderFileList first
94    /// @return the (first) Single SerieUID Fileset
95    const FileList &GetFileList()
96                            { return *SingleSerieUIDFileSetHT.begin()->second; }
97   
98    GDCM_LEGACY(   FileList *GetFirstCoherentFileList()  );
99    GDCM_LEGACY(   FileList *GetNextCoherentFileList()   );
100    GDCM_LEGACY(   FileList *GetCoherentFileList(std::string serieUID)  );
101
102    FileList *GetFirstSingleSerieUIDFileSet();
103    FileList *GetNextSingleSerieUIDFileSet();
104    FileList *GetSingleSerieUIDFileSet(std::string serieUID);
105    /// brief returns the 'Series Instance UID' Single SerieUID FileSet
106    std::string GetCurrentSerieUIDFileSetUID()
107                              { return  (*ItFileSetHt).first; }
108    /// All the following allow user to restrict DICOM file to be part
109    /// of a particular serie
110    /// \todo : find a trick to allow user to say the retrictetons are ored
111    ///         (not only anded) 
112    ///         ex : keep the images whose SerieNumber is 101 or 102 or 103.
113    void AddRestriction(TagKey const &key);
114    void AddRestriction(TagKey const &key, std::string const &value, int op);
115    void AddRestriction(uint16_t group, uint16_t elem, std::string const &value,
116                                                                     int op);
117
118    /// \brief Use additional series information such as ProtocolName
119    ///        and SeriesName to identify when a single SeriesUID contains
120    ///        multiple 3D volumes - as can occur with perfusion and DTI imaging
121    void SetUseSeriesDetails( bool useSeriesDetails )
122      {
123      m_UseSeriesDetails = useSeriesDetails;
124      }
125    bool GetUseSeriesDetails()
126      {
127      return m_UseSeriesDetails;
128      }
129    /// \brief This function will add the following DICOM tag as being part of a
130    /// 'fake' uid. This is usefull when the Serie UID is not enough to disseminate
131    /// into multiple sub serie when needed:
132    /// 0020 0011 Series Number
133    /// 0018 0024 Sequence Name
134    /// 0018 0050 Slice Thickness
135    /// 0028 0010 Rows
136    /// 0028 0011 Columns
137    void CreateDefaultUniqueSeriesIdentifier();
138
139    void AddSeriesDetail(uint16_t group, uint16_t elem, bool convert);
140
141    std::string CreateUserDefinedFileIdentifier( File * inFile );
142
143    /// \brief Create a string that uniquely identifies a series.   By default
144    //         uses the SeriesUID.   If UseSeriesDetails(true) has been called,
145    //         then additional identifying information is used.
146    std::string CreateUniqueSeriesIdentifier( File * inFile );
147  
148 /**
149  * \brief Sets the LoadMode as a boolean string. 
150  *        LD_NOSEQ, LD_NOSHADOW, LD_NOSHADOWSEQ
151  *        ... (nothing more, right now)
152  *        WARNING : before using LD_NOSHADOW, be sure *all* your files
153  *        contain accurate values in the 0x0000 element (if any) 
154  *        of *each* Shadow Group. The parser will fail if the size is wrong !
155  * @param   mode Load mode to be used    
156  */
157    void SetLoadMode (int mode) { LoadMode = mode; }
158
159 /// Brief User wants the files to be sorted Direct Order (default value)
160    void SetSortOrderToDirect()  { DirectOrder = true;  }
161
162 /// Brief User wants the files to be sorted Reverse Order 
163    void SetSortOrderToReverse() { DirectOrder = false; }
164
165    /// to allow user to give is own comparison function
166    void SetUserLessThanFunction( BOOL_FUNCTION_PFILE_PFILE_POINTER userFunc ) 
167                         { UserLessThanFunction = userFunc; }  
168
169    XCoherentFileSetmap SplitOnOrientation(FileList *fileSet); 
170    XCoherentFileSetmap SplitOnPosition(FileList *fileSet); 
171    XCoherentFileSetmap SplitOnTagValue(FileList *fileSet,
172                                                uint16_t group, uint16_t element);
173 protected :
174    SerieHelper();
175    
176 private:
177    void ClearAll();
178    bool UserOrdering(FileList *fileSet);
179    bool ImagePositionPatientOrdering(FileList *fileSet);
180    bool ImageNumberOrdering(FileList *fileSet);
181    bool FileNameOrdering(FileList *fileSet);
182    
183    static bool ImageNumberLessThan(File *file1, File *file2);
184    static bool ImageNumberGreaterThan(File *file1, File *file2);
185    static bool FileNameLessThan(File *file1, File *file2);
186    static bool FileNameGreaterThan(File *file1, File *file2);
187
188 //Attributes:
189    
190    SingleSerieUIDFileSetmap SingleSerieUIDFileSetHT;
191    SingleSerieUIDFileSetmap::iterator ItFileSetHt;
192
193 #ifndef GDCM_LEGACY_REMOVE
194    typedef std::pair<TagKey, std::string> Rule;
195    typedef std::vector<Rule> SerieRestrictions;
196    SerieRestrictions Restrictions;
197 #endif
198
199    // New style for (extented) Rules
200    typedef struct {
201       uint16_t group;
202       uint16_t elem;
203       std::string value;
204       int op;
205    } ExRule;
206    typedef std::vector<ExRule> SerieExRestrictions;
207    SerieExRestrictions ExRestrictions;
208    SerieExRestrictions ExRefine;
209
210    typedef struct {
211       uint16_t group;
212       uint16_t elem;
213       bool convert;
214    } ExDetail;
215    typedef std::vector<ExDetail> SeriesExDetails; 
216    SeriesExDetails ExDetails;
217     
218    
219    /// \brief Bit string integer (each one considered as a boolean)
220    ///        Bit 0 : Skip Sequences,    if possible
221    ///        Bit 1 : Skip Shadow Groups if possible
222    ///        Probabely, some more to add
223    int LoadMode;
224
225    /// \brief whether we want to sort in direct order or not (reverse order).
226    ///        To be used by aware user only
227    bool DirectOrder;
228
229    /// \brief If user knows more about his images than gdcm does,
230    ///        he may supply his own comparison function.
231    BOOL_FUNCTION_PFILE_PFILE_POINTER UserLessThanFunction;
232
233    void Sort(FileList *fileList, bool (*pt2Func)( File *file1, File *file2) );
234
235    bool m_UseSeriesDetails;
236 };
237
238 } // end namespace gdcm
239
240 //-----------------------------------------------------------------------------
241 #endif