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);
113 // the 2 following will be merged
114 virtual void PrintPubDict (std::ostream &os = std::cout);
115 virtual void PrintShaDict (std::ostream &os = std::cout);
118 gdcmDict *GetPubDict(void);
119 gdcmDict *GetShaDict(void);
120 bool SetShaDict(gdcmDict *dict);
121 bool SetShaDict(DictKey dictName);
123 // Informations contained in the parser
124 virtual bool IsReadable(void);
125 bool IsImplicitVRLittleEndianTransferSyntax(void);
126 bool IsExplicitVRLittleEndianTransferSyntax(void);
127 bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
128 bool IsExplicitVRBigEndianTransferSyntax(void);
129 FileType GetFileType(void);
131 // Read (used in gdcmFile, gdcmDicomDir)
132 FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
133 bool CloseFile(void);
135 // Write (used in gdcmFile, gdcmDicomDir)
136 virtual bool Write(FILE *, FileType);
137 virtual void WriteEntryTagVRLength(gdcmHeaderEntry *tag,
138 FILE *_fp, FileType type);
139 virtual void WriteEntryValue(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
140 virtual bool WriteEntry(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
141 virtual bool WriteEntries(FILE *_fp,FileType type);
142 void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
144 gdcmHeaderEntry * ReplaceOrCreateByNumber(std::string Value,
145 guint16 Group, guint16 Elem);
146 bool ReplaceIfExistByNumber (char *Value, guint16 Group, guint16 Elem);
149 guint16 SwapShort(guint16); // needed by gdcmFile
150 guint32 SwapLong(guint32); // needed by gdcmFile
151 guint16 UnswapShort(guint16); // needed by gdcmFile
152 guint32 UnswapLong(guint32); // needed by gdcmFile
155 // Constructor and destructor are protected to forbid end user
156 // to instanciate from this class gdcmParser (only gdcmHeader and
157 // gdcmDicomDir are meaningfull).
158 gdcmParser(bool exception_on_error = false);
159 gdcmParser(const char *inFilename,
160 bool exception_on_error = false,
161 bool enable_sequences = false,
162 bool ignore_shadow = false);
163 virtual ~gdcmParser(void);
165 int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
166 virtual std::string GetEntryByName (std::string tagName);
167 virtual std::string GetEntryVRByName (std::string tagName);
168 virtual std::string GetEntryByNumber (guint16 group, guint16 element);
169 virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
170 virtual int GetEntryLengthByNumber(guint16 group, guint16 element);
172 virtual bool SetEntryByName (std::string content, std::string tagName);
173 virtual bool SetEntryByNumber(std::string content,
174 guint16 group, guint16 element);
175 virtual bool SetEntryLengthByNumber(guint32 length,
176 guint16 group, guint16 element);
178 virtual size_t GetEntryOffsetByNumber (guint16 Group, guint16 Elem);
179 virtual void *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);
180 virtual void *LoadEntryVoidArea (guint16 Group, guint16 Element);
181 virtual bool SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
183 virtual void UpdateShaEntries(void);
186 gdcmHeaderEntry *GetHeaderEntryByNumber (guint16 group, guint16 element);
187 gdcmHeaderEntry *GetHeaderEntryByName (std::string Name);
188 IterHT GetHeaderEntrySameNumber(guint16 group, guint16 element);
189 // IterHT GetHeaderEntrySameName (std::string Name);
191 void LoadHeaderEntrySafe(gdcmHeaderEntry *);
193 void UpdateGroupLength(bool SkipSequence = false,
194 FileType type = ImplicitVR);
196 void AddHeaderEntry (gdcmHeaderEntry *);
201 bool LoadHeaderEntries(bool exception_on_error = false) throw(gdcmFormatError);
203 void LoadHeaderEntry (gdcmHeaderEntry *);
204 void FindHeaderEntryLength(gdcmHeaderEntry *);
205 void FindHeaderEntryVR (gdcmHeaderEntry *);
206 bool CheckHeaderEntryVR (gdcmHeaderEntry *, VRKey);
208 std::string GetHeaderEntryValue (gdcmHeaderEntry *);
209 std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
211 void SkipHeaderEntry (gdcmHeaderEntry *);
212 void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
213 bool IsHeaderEntryAnInteger (gdcmHeaderEntry *);
215 guint32 FindHeaderEntryLengthOB(void);
217 guint16 ReadInt16(void);
218 guint32 ReadInt32(void);
219 void SkipBytes(guint32);
221 void Initialise(void);
222 bool CheckSwap(void);
223 void SwitchSwapToBigEndian(void);
224 void SetMaxSizeLoadEntry(long);
225 void SetMaxSizePrintEntry(long);
227 // DictEntry related utilities
228 gdcmDictEntry *GetDictEntryByName (std::string Name);
229 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
230 gdcmDictEntry *NewVirtualDictEntry(guint16 group,
232 std::string vr = "unkn",
233 std::string fourth = "unkn",
234 std::string name = "unkn");
235 //gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *); // never defined
237 // HeaderEntry related utilities
239 gdcmHeaderEntry *ReadNextHeaderEntry (void);
240 gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group,
242 gdcmHeaderEntry *NewHeaderEntryByName (std::string Name);
244 // Deprecated (Not used) --> commented out
245 //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
248 guint32 GenerateFreeTagKeyInGroup(guint16 group);
252 /// Accessor to \ref printLevel
253 void SetPrintLevel(int level) { printLevel = level; };
255 /// Accessor to \ref filename
256 inline std::string GetFileName(void) {return filename;}
258 /// Accessor to \ref filename
259 inline void SetFileName(char* fileName) {filename = fileName;}
261 /// Accessor to \ref gdcmParser::tagHT
262 inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
264 /// Accessor to \ref gdcmParser::listEntries
265 inline ListTag &GetListEntry(void) { return listEntries; };
267 /// 'Swap code' accessor (see \ref sw )
268 inline int GetSwapCode(void) { return sw; }
271 //-----------------------------------------------------------------------------