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 *inFilename,
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; };
54 * \brief canonical Printer
57 virtual void Print (std::ostream &os = std::cout) {PrintEntry(os);};
58 virtual void PrintEntry (std::ostream &os = std::cout);
59 virtual void PrintPubDict (std::ostream &os = std::cout);
60 virtual void PrintShaDict (std::ostream &os = std::cout);
65 * \brief Gets the external File Name
67 inline std::string GetFileName(void) {return filename;}
70 gdcmDict *GetPubDict(void);
71 gdcmDict *GetShaDict(void);
72 bool SetShaDict(gdcmDict *dict);
73 bool SetShaDict(DictKey dictName);
75 // Informations contained in the parser
76 virtual bool IsReadable(void);
77 bool IsImplicitVRLittleEndianTransferSyntax(void);
78 bool IsExplicitVRLittleEndianTransferSyntax(void);
79 bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
80 bool IsExplicitVRBigEndianTransferSyntax(void);
81 FileType GetFileType(void);
86 * \brief returns a ref to the Dicom Header H table (multimap)
87 * return the Dicom Header H table
89 inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
93 * \brief returns a ref to the Dicom Header chained list
94 * return the Dicom Header chained list
96 inline ListTag &GetListEntry(void) { return listEntries; };
98 // Read (used in gdcmFile, gdcmDicomDir)
99 FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
100 bool CloseFile(void);
102 // Write (used in gdcmFile, gdcmDicomDir)
103 virtual bool Write(FILE *, FileType);
105 bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
106 bool ReplaceOrCreateByNumber( char *Value, guint16 Group, guint16 Elem);
107 bool ReplaceIfExistByNumber ( char *Value, guint16 Group, guint16 Elem);
111 * \ingroup gdcmHeader
112 * \brief returns the 'swap code'
113 * (Big Endian, Little Endian,
114 * Bad Big Endian, Bad Little Endian)
115 * according to the processor Endianity and what's written on disc
118 inline int GetSwapCode(void) { return sw; }
120 guint16 SwapShort(guint16); // needed by gdcmFile
121 guint32 SwapLong(guint32); // needed by gdcmFile
122 guint16 UnswapShort(guint16); // needed by gdcmFile
123 guint32 UnswapLong(guint32); // needed by gdcmFile
127 int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
128 virtual std::string GetEntryByName (std::string tagName);
129 virtual std::string GetEntryVRByName (std::string tagName);
130 virtual std::string GetEntryByNumber (guint16 group, guint16 element);
131 virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
132 virtual int GetEntryLengthByNumber(guint16 group, guint16 element);
134 virtual bool SetEntryByName (std::string content, std::string tagName);
135 virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
136 virtual bool SetEntryLengthByNumber(guint32 length, guint16 group, guint16 element);
138 virtual size_t GetEntryOffsetByNumber (guint16 Group, guint16 Elem);
139 virtual void *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);
140 virtual void *LoadEntryVoidArea (guint16 Group, guint16 Element);
141 virtual bool SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
143 virtual void UpdateShaEntries(void);
146 gdcmHeaderEntry *GetHeaderEntryByNumber (guint16 group, guint16 element);
147 gdcmHeaderEntry *GetHeaderEntryByName (std::string Name);
148 IterHT GetHeaderEntrySameNumber(guint16 group, guint16 element);
149 // IterHT GetHeaderEntrySameName (std::string Name);
151 void LoadHeaderEntrySafe(gdcmHeaderEntry *);
153 void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
154 void WriteEntries(FILE *_fp,FileType type);
155 void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
157 void AddHeaderEntry (gdcmHeaderEntry *);
161 * \brief File Pointer, open during Header parsing
165 * \brief ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
169 static const unsigned int HEADER_LENGTH_TO_READ;
170 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
171 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
175 * \brief H Table (multimap), to provide fast access
177 TagHeaderEntryHT tagHT;
179 * \brief chained list, to keep the 'spacial' ordering
183 * \brief will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
187 * \brief amount of printed details for each Header Entry (Dicom Element)
193 * \brief For some ACR-NEMA images, it's *not* 7fe0 ...
198 * \brief For some ACR-NEMA images, it's *not* 0010 ...
202 * \brief some files may contain icons; GrPixel,NumPixel appears several times
203 * Let's remember how many times!
209 bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
211 void LoadHeaderEntries (void);
212 void LoadHeaderEntry (gdcmHeaderEntry *);
213 void FindHeaderEntryLength(gdcmHeaderEntry *);
214 void FindHeaderEntryVR (gdcmHeaderEntry *);
215 bool CheckHeaderEntryVR (gdcmHeaderEntry *, VRKey);
217 std::string GetHeaderEntryValue (gdcmHeaderEntry *);
218 std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
220 void SkipHeaderEntry (gdcmHeaderEntry *);
221 void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
222 bool IsHeaderEntryAnInteger (gdcmHeaderEntry *);
224 guint32 FindHeaderEntryLengthOB(void);
226 guint16 ReadInt16(void);
227 guint32 ReadInt32(void);
228 void SkipBytes(guint32);
230 void Initialise(void);
231 bool CheckSwap(void);
232 void SwitchSwapToBigEndian(void);
233 void SetMaxSizeLoadEntry(long);
234 void SetMaxSizePrintEntry(long);
236 // DictEntry related utilities
237 gdcmDictEntry *GetDictEntryByName (std::string Name);
238 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
239 gdcmDictEntry *NewVirtualDictEntry(guint16 group,
241 std::string vr = "unkn",
242 std::string fourth = "unkn",
243 std::string name = "unkn");
244 gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *);
246 // HeaderEntry related utilities
247 gdcmHeaderEntry *ReadNextHeaderEntry (void);
248 gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group,
250 gdcmHeaderEntry *NewHeaderEntryByName (std::string Name);
253 // Deprecated (Not used) --> commented out
254 //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
256 guint32 GenerateFreeTagKeyInGroup(guint16 group);
258 // Refering underlying filename.
259 std::string filename;
261 // Public dictionary used to parse this header
262 gdcmDict *RefPubDict;
263 // Optional "shadow dictionary" (private elements) used to parse this header
264 gdcmDict *RefShaDict;
266 // = 1 if a gdcmHeaderEntry was added post parsing
269 // =1 if user wants to skip shadow groups while parsing (to save space)
272 // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
273 // this code is not fixed during header parsing.
276 // Size treshold above which an element value will NOT be loaded in
277 // memory (to avoid loading the image/volume itself). By default,
278 // this upper bound is fixed to 1024 bytes (which might look reasonable
279 // when one considers the definition of the various VR contents).
280 guint32 MaxSizeLoadEntry;
281 // Size treshold above which an element value will NOT be *printed* in
282 // order no to polute the screen output. By default,
283 // this upper bound is fixed to 64 bytes.
284 guint32 MaxSizePrintEntry;
288 //-----------------------------------------------------------------------------