2 //-----------------------------------------------------------------------------
6 #include "gdcmCommon.h"
9 #include "gdcmException.h"
10 #include "gdcmDictSet.h"
11 #include "gdcmHeaderEntry.h"
14 #include <list> // for linking together *all* the Dicom Elements
16 //-----------------------------------------------------------------------------
17 typedef std::string VRKey;
18 typedef std::string VRAtr;
19 typedef std::map<VRKey, VRAtr> VRHT; // Value Representation Hash Table
21 typedef std::multimap<TagKey, gdcmHeaderEntry *> TagHeaderEntryHT;
22 typedef std::pair<TagKey, gdcmHeaderEntry *> PairHT;
23 typedef std::pair<TagHeaderEntryHT::iterator,TagHeaderEntryHT::iterator> IterHT;
25 typedef std::list<gdcmHeaderEntry *> ListTag; // for linking together the Elements
27 typedef std::string GroupKey;
28 typedef std::map<GroupKey, int> GroupHT;
30 //-----------------------------------------------------------------------------
32 * \defgroup gdcmParser
33 * \brief used by both gdcmHeader and gdcmDicomDir
35 class GDCM_EXPORT gdcmParser
38 gdcmParser(bool exception_on_error = false);
39 gdcmParser(const char *filename,
40 bool exception_on_error = false,
41 bool enable_sequences = false,
42 bool ignore_shadow = false);
43 virtual ~gdcmParser(void);
48 * \brief Sets the print level for the Dicom Header
49 * \note 0 for Light Print; 1 for 'medium' Print, 2 for Heavy
51 void SetPrintLevel(int level) { printLevel = level; };
52 virtual void PrintEntry (std::ostream &os = std::cout);
53 virtual void PrintPubDict (std::ostream &os = std::cout);
54 virtual void PrintShaDict (std::ostream &os = std::cout);
57 inline std::string GetFileName(void) {return filename;}
60 gdcmDict *GetPubDict(void);
61 gdcmDict *GetShaDict(void);
62 bool SetShaDict(gdcmDict *dict);
63 bool SetShaDict(DictKey dictName);
65 // Informations contained in the parser
66 virtual bool IsReadable(void);
67 bool IsImplicitVRLittleEndianTransferSyntax(void);
68 bool IsExplicitVRLittleEndianTransferSyntax(void);
69 bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
70 bool IsExplicitVRBigEndianTransferSyntax(void);
71 FileType GetFileType(void);
76 * \brief returns a ref to the Dicom Header H table (multimap)
77 * return the Dicom Header H table
79 inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
83 * \brief returns a ref to the Dicom Header chained list
84 * return the Dicom Header chained list
86 inline ListTag &GetListEntry(void) { return listEntries; };
88 // Read (used in gdcmFile)
89 FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
92 // Write (used in gdcmFile)
93 virtual bool Write(FILE *, FileType);
95 bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
96 bool ReplaceOrCreateByNumber( char *Value, guint16 Group, guint16 Elem);
97 bool ReplaceIfExistByNumber ( char *Value, guint16 Group, guint16 Elem);
100 inline int GetSwapCode(void) { return sw; }
101 guint16 SwapShort(guint16); // needed by gdcmFile
102 guint32 SwapLong(guint32); // needed by gdcmFile
103 guint16 UnswapShort(guint16); // needed by gdcmFile
104 guint32 UnswapLong(guint32); // needed by gdcmFile
108 int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
109 virtual std::string GetEntryByName (std::string tagName);
110 virtual std::string GetEntryVRByName (std::string tagName);
111 virtual std::string GetEntryByNumber (guint16 group, guint16 element);
112 virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
114 virtual bool SetEntryByName (std::string content, std::string tagName);
115 virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
116 virtual bool SetEntryLengthByNumber(guint32 l, guint16 group, guint16 element);
118 virtual size_t GetEntryOffsetByNumber (guint16 Group, guint16 Elem);
119 virtual void *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);
120 virtual void *LoadEntryVoidArea (guint16 Group, guint16 Element);
121 virtual bool SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
123 virtual void UpdateShaEntries(void);
126 gdcmHeaderEntry *GetHeaderEntryByNumber (guint16 group, guint16 element);
127 gdcmHeaderEntry *GetHeaderEntryByName (std::string Name);
128 IterHT GetHeaderEntrySameNumber(guint16 group, guint16 element);
129 // IterHT GetHeaderEntrySameName (std::string Name);
131 void LoadHeaderEntrySafe(gdcmHeaderEntry *);
133 void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
134 void WriteEntries(FileType type, FILE *);
138 FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
140 static const unsigned int HEADER_LENGTH_TO_READ;
141 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
147 TagHeaderEntryHT tagHT; // H Table (multimap), to provide fast access
148 ListTag listEntries; // chained list, to keep the 'spacial' ordering
152 void Parse(bool exception_on_error = false) throw(gdcmFormatError);
154 void LoadHeaderEntries (void);
155 void LoadHeaderEntry (gdcmHeaderEntry *);
156 void AddHeaderEntry (gdcmHeaderEntry *);
157 void FindHeaderEntryLength(gdcmHeaderEntry *);
158 void FindHeaderEntryVR (gdcmHeaderEntry *);
159 bool CheckHeaderEntryVR (gdcmHeaderEntry *, VRKey);
161 std::string GetHeaderEntryValue (gdcmHeaderEntry *);
162 std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
164 void SkipHeaderEntry (gdcmHeaderEntry *);
165 void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
166 bool IsHeaderEntryAnInteger (gdcmHeaderEntry *);
168 guint32 FindHeaderEntryLengthOB(void);
170 guint16 ReadInt16(void);
171 guint32 ReadInt32(void);
172 void SkipBytes(guint32);
174 void Initialise(void);
175 void CheckSwap(void);
176 void SwitchSwapToBigEndian(void);
177 void SetMaxSizeLoadEntry(long);
179 // DictEntry related utilities
180 gdcmDictEntry *GetDictEntryByName (std::string Name);
181 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
182 gdcmDictEntry *NewVirtualDictEntry(guint16 group,
184 std::string vr = "Unknown",
185 std::string fourth = "Unknown",
186 std::string name = "Unknown");
187 gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *);
189 // HeaderEntry related utilities
190 gdcmHeaderEntry *ReadNextHeaderEntry (void);
191 gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group,
193 gdcmHeaderEntry *NewHeaderEntryByName (std::string Name);
196 // Deprecated (Not used)
197 gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
199 guint32 GenerateFreeTagKeyInGroup(guint16 group);
201 // Refering underlying filename.
202 std::string filename;
204 // Public dictionary used to parse this header
205 gdcmDict *RefPubDict;
206 // Optional "shadow dictionary" (private elements) used to parse this header
207 gdcmDict *RefShaDict;
209 // = 1 if a gdcmHeaderEntry was added post parsing
212 // =1 if user wants to skip shadow groups while parsing (to save space)
215 // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
216 // this code is not fixed during header parsing.
219 // Size treshold above which an element value will NOT be loaded in
220 // memory (to avoid loading the image/volume itself). By default,
221 // this upper bound is fixed to 1024 bytes (which might look reasonable
222 // when one considers the definition of the various VR contents).
223 guint32 MaxSizeLoadEntry;
226 //-----------------------------------------------------------------------------