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