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 Print (std::ostream &os = std::cout) {PrintEntry(os);};
53 virtual void PrintEntry (std::ostream &os = std::cout);
54 virtual void PrintPubDict (std::ostream &os = std::cout);
55 virtual void PrintShaDict (std::ostream &os = std::cout);
58 inline std::string GetFileName(void) {return filename;}
61 gdcmDict *GetPubDict(void);
62 gdcmDict *GetShaDict(void);
63 bool SetShaDict(gdcmDict *dict);
64 bool SetShaDict(DictKey dictName);
66 // Informations contained in the parser
67 virtual bool IsReadable(void);
68 bool IsImplicitVRLittleEndianTransferSyntax(void);
69 bool IsExplicitVRLittleEndianTransferSyntax(void);
70 bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
71 bool IsExplicitVRBigEndianTransferSyntax(void);
72 FileType GetFileType(void);
77 * \brief returns a ref to the Dicom Header H table (multimap)
78 * return the Dicom Header H table
80 inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
84 * \brief returns a ref to the Dicom Header chained list
85 * return the Dicom Header chained list
87 inline ListTag &GetListEntry(void) { return listEntries; };
89 // Read (used in gdcmFile)
90 FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
93 // Write (used in gdcmFile)
94 virtual bool Write(FILE *, FileType);
96 bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
97 bool ReplaceOrCreateByNumber( char *Value, guint16 Group, guint16 Elem);
98 bool ReplaceIfExistByNumber ( char *Value, guint16 Group, guint16 Elem);
101 inline int GetSwapCode(void) { return sw; }
102 guint16 GetGrPixel(void) {return GrPixel;}
103 guint16 GetNumPixel(void) {return NumPixel;}
105 guint16 SwapShort(guint16); // needed by gdcmFile
106 guint32 SwapLong(guint32); // needed by gdcmFile
107 guint16 UnswapShort(guint16); // needed by gdcmFile
108 guint32 UnswapLong(guint32); // needed by gdcmFile
112 int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
113 virtual std::string GetEntryByName (std::string tagName);
114 virtual std::string GetEntryVRByName (std::string tagName);
115 virtual std::string GetEntryByNumber (guint16 group, guint16 element);
116 virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
118 virtual bool SetEntryByName (std::string content, std::string tagName);
119 virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
120 virtual bool SetEntryLengthByNumber(guint32 length, guint16 group, guint16 element);
122 virtual size_t GetEntryOffsetByNumber (guint16 Group, guint16 Elem);
123 virtual void *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);
124 virtual void *LoadEntryVoidArea (guint16 Group, guint16 Element);
125 virtual bool SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
127 virtual void UpdateShaEntries(void);
130 gdcmHeaderEntry *GetHeaderEntryByNumber (guint16 group, guint16 element);
131 gdcmHeaderEntry *GetHeaderEntryByName (std::string Name);
132 IterHT GetHeaderEntrySameNumber(guint16 group, guint16 element);
133 // IterHT GetHeaderEntrySameName (std::string Name);
135 void LoadHeaderEntrySafe(gdcmHeaderEntry *);
137 void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
138 void WriteEntries(FILE *_fp,FileType type);
142 FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
144 static const unsigned int HEADER_LENGTH_TO_READ;
145 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
146 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
149 TagHeaderEntryHT tagHT; // H Table (multimap), to provide fast access
150 ListTag listEntries; // chained list, to keep the 'spacial' ordering
155 // For some ACR-NEMA images, it's *not* 7fe0, 0010 ...
158 // some files may contain icons; GrPixel,NumPixel appears several times
159 // Let's remember how many times!
164 bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
166 void LoadHeaderEntries (void);
167 void LoadHeaderEntry (gdcmHeaderEntry *);
168 void AddHeaderEntry (gdcmHeaderEntry *);
169 void FindHeaderEntryLength(gdcmHeaderEntry *);
170 void FindHeaderEntryVR (gdcmHeaderEntry *);
171 bool CheckHeaderEntryVR (gdcmHeaderEntry *, VRKey);
173 std::string GetHeaderEntryValue (gdcmHeaderEntry *);
174 std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
176 void SkipHeaderEntry (gdcmHeaderEntry *);
177 void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
178 bool IsHeaderEntryAnInteger (gdcmHeaderEntry *);
180 guint32 FindHeaderEntryLengthOB(void);
182 guint16 ReadInt16(void);
183 guint32 ReadInt32(void);
184 void SkipBytes(guint32);
186 void Initialise(void);
187 bool CheckSwap(void);
188 void SwitchSwapToBigEndian(void);
189 void SetMaxSizeLoadEntry(long);
190 void SetMaxSizePrintEntry(long);
192 // DictEntry related utilities
193 gdcmDictEntry *GetDictEntryByName (std::string Name);
194 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
195 gdcmDictEntry *NewVirtualDictEntry(guint16 group,
197 std::string vr = "unkn",
198 std::string fourth = "unkn",
199 std::string name = "unkn");
200 gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *);
202 // HeaderEntry related utilities
203 gdcmHeaderEntry *ReadNextHeaderEntry (void);
204 gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group,
206 gdcmHeaderEntry *NewHeaderEntryByName (std::string Name);
209 // Deprecated (Not used) --> commented out
210 //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
212 guint32 GenerateFreeTagKeyInGroup(guint16 group);
214 // Refering underlying filename.
215 std::string filename;
217 // Public dictionary used to parse this header
218 gdcmDict *RefPubDict;
219 // Optional "shadow dictionary" (private elements) used to parse this header
220 gdcmDict *RefShaDict;
222 // = 1 if a gdcmHeaderEntry was added post parsing
225 // =1 if user wants to skip shadow groups while parsing (to save space)
228 // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
229 // this code is not fixed during header parsing.
232 // Size treshold above which an element value will NOT be loaded in
233 // memory (to avoid loading the image/volume itself). By default,
234 // this upper bound is fixed to 1024 bytes (which might look reasonable
235 // when one considers the definition of the various VR contents).
236 guint32 MaxSizeLoadEntry;
237 // Size treshold above which an element value will NOT be *printed* in
238 // order no to polute the screen output. By default,
239 // this upper bound is fixed to 64 bytes.
240 guint32 MaxSizePrintEntry;
244 //-----------------------------------------------------------------------------