2 //-----------------------------------------------------------------------------
6 #include "gdcmCommon.h"
10 #include "gdcmException.h"
11 #include "gdcmDictSet.h"
12 #include "gdcmHeaderEntry.h"
19 //-----------------------------------------------------------------------------
20 typedef std::string VRKey;
21 typedef std::string VRAtr;
22 typedef std::map<VRKey, VRAtr> VRHT; // Value Representation Hash Table
24 typedef std::multimap<TagKey, gdcmHeaderEntry *> TagHeaderEntryHT;
25 typedef std::pair<TagKey, gdcmHeaderEntry *> PairHT;
26 typedef std::pair<TagHeaderEntryHT::iterator,TagHeaderEntryHT::iterator> IterHT;
27 /// for linking together the Elements
28 typedef std::list<gdcmHeaderEntry *> ListTag;
30 typedef std::string GroupKey;
31 typedef std::map<GroupKey, int> GroupHT;
33 //-----------------------------------------------------------------------------
35 * \brief used by both gdcmHeader and gdcmDicomDir
37 class GDCM_EXPORT gdcmParser
40 /// Public dictionary used to parse this header
43 /// Optional "shadow dictionary" (private elements) used to parse
47 /// Equals 1 if a gdcmHeaderEntry was added post parsing
50 /// Equals =1 if user wants to skip shadow groups while parsing
54 /// Size threshold above which an element value will NOT be loaded in
55 /// memory (to avoid loading the image/volume itself). By default,
56 /// this upper bound is fixed to 1024 bytes (which might look reasonable
57 /// when one considers the definition of the various VR contents).
58 guint32 MaxSizeLoadEntry;
60 /// Size threshold above which an element value will NOT be *printed* in
61 /// order no to polute the screen output. By default, this upper bound
62 /// is fixed to 64 bytes.
63 guint32 MaxSizePrintEntry;
66 /// Refering underlying filename.
69 /// SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
70 /// Bad Little Endian) according to the processor Endianity and
71 /// what is written on disc.
74 /// File Pointer, opened during Header parsing.
77 /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
80 /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
81 static const unsigned int HEADER_LENGTH_TO_READ;
83 /// Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
85 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
87 /// Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
89 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
91 /// Hash Table (multimap), to provide fast access
92 TagHeaderEntryHT tagHT;
94 /// Chained list, to keep the 'spacial' ordering
97 /// will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
100 /// Amount of printed details for each Header Entry (Dicom Element):
101 /// 0 : stands for the least detail level.
108 /// Canonical Printing method (see also gdcmParser::SetPrintLevel)
109 virtual void Print (std::ostream &os = std::cout)
111 virtual void PrintEntry (std::ostream &os = std::cout);
112 virtual void PrintEntryNoSQ (std::ostream &os = std::cout);
113 // the 2 following will be merged
114 virtual void PrintEntryNiceSQ(std::ostream &os = std::cout);
115 virtual void PrintPubDict (std::ostream &os = std::cout);
116 virtual void PrintShaDict (std::ostream &os = std::cout);
119 gdcmDict *GetPubDict(void);
120 gdcmDict *GetShaDict(void);
121 bool SetShaDict(gdcmDict *dict);
122 bool SetShaDict(DictKey dictName);
124 // Informations contained in the parser
125 virtual bool IsReadable(void);
126 bool IsImplicitVRLittleEndianTransferSyntax(void);
127 bool IsExplicitVRLittleEndianTransferSyntax(void);
128 bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
129 bool IsExplicitVRBigEndianTransferSyntax(void);
130 FileType GetFileType(void);
132 // Read (used in gdcmFile, gdcmDicomDir)
133 FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
134 bool CloseFile(void);
136 // Write (used in gdcmFile, gdcmDicomDir)
137 virtual bool Write(FILE *, FileType);
138 virtual void WriteEntryTagVRLength(gdcmHeaderEntry *tag,
139 FILE *_fp, FileType type);
140 virtual void WriteEntryValue(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
141 virtual bool WriteEntry(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
142 virtual bool WriteEntries(FILE *_fp,FileType type);
143 void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
145 gdcmHeaderEntry * ReplaceOrCreateByNumber(std::string Value,
146 guint16 Group, guint16 Elem);
147 bool ReplaceIfExistByNumber (char *Value, guint16 Group, guint16 Elem);
150 guint16 SwapShort(guint16); // needed by gdcmFile
151 guint32 SwapLong(guint32); // needed by gdcmFile
152 guint16 UnswapShort(guint16); // needed by gdcmFile
153 guint32 UnswapLong(guint32); // needed by gdcmFile
156 // Constructor and destructor are protected to forbid end user
157 // to instanciate from this class gdcmParser (only gdcmHeader and
158 // gdcmDicomDir are meaningfull).
159 gdcmParser(bool exception_on_error = false);
160 gdcmParser(const char *inFilename,
161 bool exception_on_error = false,
162 bool enable_sequences = false,
163 bool ignore_shadow = false);
164 virtual ~gdcmParser(void);
166 int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
167 virtual std::string GetEntryByName (std::string tagName);
168 virtual std::string GetEntryVRByName (std::string tagName);
169 virtual std::string GetEntryByNumber (guint16 group, guint16 element);
170 virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
171 virtual int GetEntryLengthByNumber(guint16 group, guint16 element);
173 virtual bool SetEntryByName (std::string content, std::string tagName);
174 virtual bool SetEntryByNumber(std::string content,
175 guint16 group, guint16 element);
176 virtual bool SetEntryLengthByNumber(guint32 length,
177 guint16 group, guint16 element);
179 virtual size_t GetEntryOffsetByNumber (guint16 Group, guint16 Elem);
180 virtual void *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);
181 virtual void *LoadEntryVoidArea (guint16 Group, guint16 Element);
182 virtual bool SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
184 virtual void UpdateShaEntries(void);
187 gdcmHeaderEntry *GetHeaderEntryByNumber (guint16 group, guint16 element);
188 gdcmHeaderEntry *GetHeaderEntryByName (std::string Name);
189 IterHT GetHeaderEntrySameNumber(guint16 group, guint16 element);
190 // IterHT GetHeaderEntrySameName (std::string Name);
192 void LoadHeaderEntrySafe(gdcmHeaderEntry *);
194 void UpdateGroupLength(bool SkipSequence = false,
195 FileType type = ImplicitVR);
197 void AddHeaderEntry (gdcmHeaderEntry *);
202 bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
204 void LoadHeaderEntries (void);
205 void LoadHeaderEntry (gdcmHeaderEntry *);
206 void FindHeaderEntryLength(gdcmHeaderEntry *);
207 void FindHeaderEntryVR (gdcmHeaderEntry *);
208 bool CheckHeaderEntryVR (gdcmHeaderEntry *, VRKey);
210 std::string GetHeaderEntryValue (gdcmHeaderEntry *);
211 std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
213 void SkipHeaderEntry (gdcmHeaderEntry *);
214 void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
215 bool IsHeaderEntryAnInteger (gdcmHeaderEntry *);
217 guint32 FindHeaderEntryLengthOB(void);
219 guint16 ReadInt16(void);
220 guint32 ReadInt32(void);
221 void SkipBytes(guint32);
223 void Initialise(void);
224 bool CheckSwap(void);
225 void SwitchSwapToBigEndian(void);
226 void SetMaxSizeLoadEntry(long);
227 void SetMaxSizePrintEntry(long);
229 // DictEntry related utilities
230 gdcmDictEntry *GetDictEntryByName (std::string Name);
231 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
232 gdcmDictEntry *NewVirtualDictEntry(guint16 group,
234 std::string vr = "unkn",
235 std::string fourth = "unkn",
236 std::string name = "unkn");
237 //gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *); // never defined
239 // HeaderEntry related utilities
241 gdcmHeaderEntry *ReadNextHeaderEntry (void);
242 gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group,
244 gdcmHeaderEntry *NewHeaderEntryByName (std::string Name);
246 // Deprecated (Not used) --> commented out
247 //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
250 guint32 GenerateFreeTagKeyInGroup(guint16 group);
254 /// Accessor to \ref printLevel
255 void SetPrintLevel(int level) { printLevel = level; };
257 /// Accessor to \ref filename
258 inline std::string GetFileName(void) {return filename;}
260 /// Accessor to \ref filename
261 inline void SetFileName(char* fileName) {filename = fileName;}
263 /// Accessor to \ref gdcmParser::tagHT
264 inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
266 /// Accessor to \ref gdcmParser::listEntries
267 inline ListTag &GetListEntry(void) { return listEntries; };
269 /// 'Swap code' accessor (see \ref sw )
270 inline int GetSwapCode(void) { return sw; }
273 //-----------------------------------------------------------------------------