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 /// \brief Optional "shadow dictionary" (private elements) used to parse
47 /// Equals 1 if a gdcmHeaderEntry was added post parsing
50 /// \brief Equals =1 if user wants to skip shadow groups while parsing
54 /// \brief Size threshold above which an element value will NOT be loaded
55 /// in 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 /// \brief Size threshold above which an element value will NOT be *printed*
61 /// in 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 /// \brief 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 /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
85 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
87 /// \brief Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE
89 /// \todo Currently not used since collides with #define in
90 /// \ref gdcmHeaderEntry.cxx. See also
91 /// \ref gdcmParser::SetMaxSizePrintEntry()
92 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
94 /// Hash Table (multimap), to provide fast access
95 TagHeaderEntryHT tagHT;
97 /// Chained list, to keep the 'spacial' ordering
100 /// Will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
103 /// \brief Amount of printed details for each Header Entry (Dicom Element):
104 /// 0 : stands for the least detail level.
111 /// Canonical Printing method (see also gdcmParser::SetPrintLevel)
112 virtual void Print (std::ostream &os = std::cout)
114 virtual void PrintEntry (std::ostream &os = std::cout);
116 // the 2 following will be merged
117 virtual void PrintPubDict (std::ostream &os = std::cout);
118 virtual void PrintShaDict (std::ostream &os = std::cout);
121 gdcmDict *GetPubDict(void);
122 gdcmDict *GetShaDict(void);
123 bool SetShaDict(gdcmDict *dict);
124 bool SetShaDict(DictKey dictName);
126 // Informations contained in the parser
127 virtual bool IsReadable(void);
128 bool IsImplicitVRLittleEndianTransferSyntax(void);
129 bool IsExplicitVRLittleEndianTransferSyntax(void);
130 bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
131 bool IsExplicitVRBigEndianTransferSyntax(void);
132 FileType GetFileType(void);
134 // Read (used in gdcmFile, gdcmDicomDir)
135 FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
136 bool CloseFile(void);
138 // Write (used in gdcmFile, gdcmDicomDir)
139 virtual bool Write(FILE *, FileType);
140 virtual void WriteEntryTagVRLength(gdcmHeaderEntry *tag,
141 FILE *_fp, FileType type);
142 virtual void WriteEntryValue(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
143 virtual bool WriteEntry(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
144 virtual bool WriteEntries(FILE *_fp,FileType type);
145 void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
147 gdcmHeaderEntry * ReplaceOrCreateByNumber(std::string Value,
148 guint16 Group, guint16 Elem);
149 bool ReplaceIfExistByNumber (char *Value, guint16 Group, guint16 Elem);
152 guint16 SwapShort(guint16); // needed by gdcmFile
153 guint32 SwapLong(guint32); // needed by gdcmFile
154 guint16 UnswapShort(guint16); // needed by gdcmFile
155 guint32 UnswapLong(guint32); // needed by gdcmFile
158 // Constructor and destructor are protected to forbid end user
159 // to instanciate from this class gdcmParser (only gdcmHeader and
160 // gdcmDicomDir are meaningfull).
161 gdcmParser(bool exception_on_error = false);
162 gdcmParser(const char *inFilename,
163 bool exception_on_error = false,
164 bool enable_sequences = false,
165 bool ignore_shadow = false);
166 virtual ~gdcmParser(void);
168 int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
169 virtual std::string GetEntryByName (std::string tagName);
170 virtual std::string GetEntryVRByName (std::string tagName);
171 virtual std::string GetEntryByNumber (guint16 group, guint16 element);
172 virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
173 virtual int GetEntryLengthByNumber(guint16 group, guint16 element);
175 virtual bool SetEntryByName (std::string content, std::string tagName);
176 virtual bool SetEntryByNumber(std::string content,
177 guint16 group, guint16 element);
178 virtual bool SetEntryLengthByNumber(guint32 length,
179 guint16 group, guint16 element);
181 virtual size_t GetEntryOffsetByNumber (guint16 Group, guint16 Elem);
182 virtual void *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);
183 virtual void *LoadEntryVoidArea (guint16 Group, guint16 Element);
184 virtual bool SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
186 virtual void UpdateShaEntries(void);
189 gdcmHeaderEntry *GetHeaderEntryByNumber (guint16 group, guint16 element);
190 gdcmHeaderEntry *GetHeaderEntryByName (std::string Name);
191 IterHT GetHeaderEntrySameNumber(guint16 group, guint16 element);
192 // IterHT GetHeaderEntrySameName (std::string Name);
194 void LoadHeaderEntrySafe(gdcmHeaderEntry *);
196 void UpdateGroupLength(bool SkipSequence = false,
197 FileType type = gdcmImplicitVR);
199 void AddHeaderEntry (gdcmHeaderEntry *);
204 bool LoadHeaderEntries(bool exception_on_error = false) throw(gdcmFormatError);
206 void LoadHeaderEntry (gdcmHeaderEntry *);
207 void FindHeaderEntryLength(gdcmHeaderEntry *);
208 void FindHeaderEntryVR (gdcmHeaderEntry *);
209 bool CheckHeaderEntryVR (gdcmHeaderEntry *, VRKey);
211 std::string GetHeaderEntryValue (gdcmHeaderEntry *);
212 std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
214 void SkipHeaderEntry (gdcmHeaderEntry *);
215 void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
216 bool IsHeaderEntryAnInteger (gdcmHeaderEntry *);
218 guint32 FindHeaderEntryLengthOB(void);
220 guint16 ReadInt16(void);
221 guint32 ReadInt32(void);
222 void SkipBytes(guint32);
224 void Initialise(void);
225 bool CheckSwap(void);
226 void SwitchSwapToBigEndian(void);
227 void SetMaxSizeLoadEntry(long);
228 void SetMaxSizePrintEntry(long);
230 // DictEntry related utilities
231 gdcmDictEntry *GetDictEntryByName (std::string Name);
232 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
233 gdcmDictEntry *NewVirtualDictEntry(guint16 group,
235 std::string vr = "unkn",
236 std::string fourth = "unkn",
237 std::string name = "unkn");
238 //gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *); // never defined
240 // HeaderEntry related utilities
242 gdcmHeaderEntry *ReadNextHeaderEntry (void);
243 gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group,
245 gdcmHeaderEntry *NewHeaderEntryByName (std::string Name);
247 // Deprecated (Not used) --> commented out
248 //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
251 guint32 GenerateFreeTagKeyInGroup(guint16 group);
255 /// Accessor to \ref printLevel
256 void SetPrintLevel(int level) { printLevel = level; };
258 /// Accessor to \ref filename
259 inline std::string GetFileName(void) {return filename;}
261 /// Accessor to \ref filename
262 inline void SetFileName(char* fileName) {filename = fileName;}
264 /// Accessor to \ref gdcmParser::tagHT
265 inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
267 /// Accessor to \ref gdcmParser::listEntries
268 inline ListTag &GetListEntry(void) { return listEntries; };
270 /// 'Swap code' accessor (see \ref sw )
271 inline int GetSwapCode(void) { return sw; }
274 //-----------------------------------------------------------------------------