1 /*=========================================================================
4 Module: $RCSfile: gdcmSerieHelper.h,v $
6 Date: $Date: 2006/03/30 16:51:29 $
7 Version: $Revision: 1.38 $
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.
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.
17 =========================================================================*/
19 #ifndef GDCMSERIEHELPER_H
20 #define GDCMSERIEHELPER_H
22 #include "gdcmRefCounter.h"
23 #include "gdcmTagKey.h"
24 #include "gdcmDebug.h" // for LEGACY
34 typedef std::vector<File* > FileList;
35 #ifndef GDCM_LEGACY_REMOVE
36 typedef std::vector<File* > GdcmFileList;
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;
45 typedef bool (*BOOL_FUNCTION_PFILE_PFILE_POINTER)(File *, File *);
47 //-----------------------------------------------------------------------------
50 * This class should be used for a stack of 2D dicom images.
52 * - It allows to explore (recursively or not) a directory and
53 * makes a set of 'Single SerieUID Filesets'
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)
61 class GDCM_EXPORT SerieHelper : public RefCounter
63 gdcmTypeMacro(SerieHelper);
66 /// SingleSerieUIDFileSetmap replaces the former CoherentFileListmap
67 /// ( List were actually std::vectors, and where no coherent at all :
68 /// They were only Single SeriesInstanceUID File sets)
69 typedef std::map<std::string, FileList *> SingleSerieUIDFileSetmap;
71 typedef std::vector<File* > FileVector;
73 /// \brief Constructs a SerieHelper with a RefCounter
74 static SerieHelper *New() {return new SerieHelper();}
76 virtual ~SerieHelper();
77 void Print(std::ostream &os = std::cout, std::string const &indent = "" );
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); }
86 void SetDirectory(std::string const &dir, bool recursive=false);
87 bool IsCoherent(FileList *fileSet);
88 void OrderFileList(FileList *fileSet);
89 void Clear() { ClearAll(); }
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; }
98 GDCM_LEGACY( FileList *GetFirstCoherentFileList() );
99 GDCM_LEGACY( FileList *GetNextCoherentFileList() );
100 GDCM_LEGACY( FileList *GetCoherentFileList(std::string serieUID) );
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; }
109 /// \brief returns the distance between the 2 first -adjacent- slices,
110 /// along the slice normal; -1.0 if process failed
111 /// Computed during ImagePositionPatientOrdering() process.
112 /// To be use *immediately after* ImagePositionPatientOrdering()
113 /// (it's a member of the FileHelper, *not* of the FileList !)
114 double GetZSpacing() { return ZSpacing; }
116 /// All the following allow user to restrict DICOM file to be part
117 /// of a particular serie
118 /// \todo : find a trick to allow user to say the restrictions are ored
120 /// ex : keep the images whose SerieNumber is 101 or 102 or 103.
121 void AddRestriction(TagKey const &key);
122 void AddRestriction(TagKey const &key, std::string const &value, int op);
123 void AddRestriction(uint16_t group, uint16_t elem, std::string const &value,
126 /// \brief Use additional series information such as ProtocolName
127 /// and SeriesName to identify when a single SeriesUID contains
128 /// multiple 3D volumes - as can occur with perfusion and DTI imaging
129 void SetUseSeriesDetails( bool useSeriesDetails )
131 m_UseSeriesDetails = useSeriesDetails;
133 bool GetUseSeriesDetails()
135 return m_UseSeriesDetails;
137 /// \brief This function will add the following DICOM tag as being part of a
138 /// 'fake' uid. This is usefull when the Serie UID is not enough to disseminate
139 /// into multiple sub serie when needed:
140 /// 0020 0011 Series Number
141 /// 0018 0024 Sequence Name
142 /// 0018 0050 Slice Thickness
144 /// 0028 0011 Columns
145 void CreateDefaultUniqueSeriesIdentifier();
147 void AddSeriesDetail(uint16_t group, uint16_t elem, bool convert);
149 std::string CreateUserDefinedFileIdentifier( File * inFile );
151 /// \brief Create a string that uniquely identifies a series. By default
152 // uses the SeriesUID. If UseSeriesDetails(true) has been called,
153 // then additional identifying information is used.
154 std::string CreateUniqueSeriesIdentifier( File * inFile );
157 * \brief Sets the LoadMode as a boolean string.
158 * LD_NOSEQ, LD_NOSHADOW, LD_NOSHADOWSEQ
159 * ... (nothing more, right now)
160 * WARNING : before using LD_NOSHADOW, be sure *all* your files
161 * contain accurate values in the 0x0000 element (if any)
162 * of *each* Shadow Group. The parser will fail if the size is wrong !
163 * @param mode Load mode to be used
165 void SetLoadMode (int mode) { LoadMode = mode; }
167 /// Brief User wants the files to be sorted Direct Order (default value)
168 void SetSortOrderToDirect() { DirectOrder = true; }
170 /// Brief User wants the files to be sorted Reverse Order
171 void SetSortOrderToReverse() { DirectOrder = false; }
173 /// to allow user to give is own comparison function
174 void SetUserLessThanFunction( BOOL_FUNCTION_PFILE_PFILE_POINTER userFunc )
175 { UserLessThanFunction = userFunc; }
177 XCoherentFileSetmap SplitOnOrientation(FileList *fileSet);
178 XCoherentFileSetmap SplitOnPosition(FileList *fileSet);
179 XCoherentFileSetmap SplitOnTagValue(FileList *fileSet,
180 uint16_t group, uint16_t element);
186 bool UserOrdering(FileList *fileSet);
187 bool ImagePositionPatientOrdering(FileList *fileSet);
188 bool ImageNumberOrdering(FileList *fileSet);
189 bool FileNameOrdering(FileList *fileSet);
191 static bool ImageNumberLessThan(File *file1, File *file2);
192 static bool ImageNumberGreaterThan(File *file1, File *file2);
193 static bool FileNameLessThan(File *file1, File *file2);
194 static bool FileNameGreaterThan(File *file1, File *file2);
198 SingleSerieUIDFileSetmap SingleSerieUIDFileSetHT;
199 SingleSerieUIDFileSetmap::iterator ItFileSetHt;
201 #ifndef GDCM_LEGACY_REMOVE
202 typedef std::pair<TagKey, std::string> Rule;
203 typedef std::vector<Rule> SerieRestrictions;
204 SerieRestrictions Restrictions;
207 // New style for (extented) Rules
214 typedef std::vector<ExRule> SerieExRestrictions;
215 SerieExRestrictions ExRestrictions;
216 SerieExRestrictions ExRefine;
223 typedef std::vector<ExDetail> SeriesExDetails;
224 SeriesExDetails ExDetails;
227 /// \brief Bit string integer (each one considered as a boolean)
228 /// Bit 0 : Skip Sequences, if possible
229 /// Bit 1 : Skip Shadow Groups if possible
230 /// Probabely, some more to add
233 /// \brief whether we want to sort in direct order or not (reverse order).
234 /// To be used by aware user only
237 /// \brief If user knows more about his images than gdcm does,
238 /// he may supply his own comparison function.
239 BOOL_FUNCTION_PFILE_PFILE_POINTER UserLessThanFunction;
241 void Sort(FileList *fileList, bool (*pt2Func)( File *file1, File *file2) );
243 bool m_UseSeriesDetails;
244 /// \brief distance between the 2 first -adjancent- slices, along the slice normal
245 /// Computed during ImagePositionPatientOrdering() process.
246 /// (we need to sort the whole image set to do that)
247 /// To be used *immediately after* ImagePositionPatientOrdering()
248 /// (it's a member of the FileHelper, *not* of the FileList !)
252 } // end namespace gdcm
254 //-----------------------------------------------------------------------------