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)
52 { printLevel = level; };
55 * \brief canonical Printer
58 virtual void Print (std::ostream &os = std::cout)
60 virtual void PrintEntry (std::ostream &os = std::cout);
61 virtual void PrintPubDict (std::ostream &os = std::cout);
62 virtual void PrintShaDict (std::ostream &os = std::cout);
67 * \brief Gets the external File Name
69 inline std::string GetFileName(void)
73 gdcmDict *GetPubDict(void);
74 gdcmDict *GetShaDict(void);
75 bool SetShaDict(gdcmDict *dict);
76 bool SetShaDict(DictKey dictName);
78 // Informations contained in the parser
79 virtual bool IsReadable(void);
80 bool IsImplicitVRLittleEndianTransferSyntax(void);
81 bool IsExplicitVRLittleEndianTransferSyntax(void);
82 bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
83 bool IsExplicitVRBigEndianTransferSyntax(void);
84 FileType GetFileType(void);
89 * \brief returns a ref to the Dicom Header H table (multimap)
90 * return the Dicom Header H table
92 inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
96 * \brief returns a ref to the Dicom Header chained list
97 * return the Dicom Header chained list
99 inline ListTag &GetListEntry(void) { return listEntries; };
101 // Read (used in gdcmFile, gdcmDicomDir)
102 FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
103 bool CloseFile(void);
105 // Write (used in gdcmFile, gdcmDicomDir)
106 virtual bool Write(FILE *, FileType);
108 bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
109 bool ReplaceOrCreateByNumber( char *Value, guint16 Group, guint16 Elem);
110 bool ReplaceIfExistByNumber ( char *Value, guint16 Group, guint16 Elem);
114 * \ingroup gdcmHeader
115 * \brief returns the 'swap code'
116 * (Big Endian, Little Endian,
117 * Bad Big Endian, Bad Little Endian)
118 * according to the processor Endianity and what's written on disc
121 inline int GetSwapCode(void) { return sw; }
123 guint16 SwapShort(guint16); // needed by gdcmFile
124 guint32 SwapLong(guint32); // needed by gdcmFile
125 guint16 UnswapShort(guint16); // needed by gdcmFile
126 guint32 UnswapLong(guint32); // needed by gdcmFile
130 int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
131 virtual std::string GetEntryByName (std::string tagName);
132 virtual std::string GetEntryVRByName (std::string tagName);
133 virtual std::string GetEntryByNumber (guint16 group, guint16 element);
134 virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
135 virtual int GetEntryLengthByNumber(guint16 group, guint16 element);
137 virtual bool SetEntryByName (std::string content, std::string tagName);
138 virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
139 virtual bool SetEntryLengthByNumber(guint32 length, guint16 group, guint16 element);
141 virtual size_t GetEntryOffsetByNumber (guint16 Group, guint16 Elem);
142 virtual void *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);
143 virtual void *LoadEntryVoidArea (guint16 Group, guint16 Element);
144 virtual bool SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
146 virtual void UpdateShaEntries(void);
149 gdcmHeaderEntry *GetHeaderEntryByNumber (guint16 group, guint16 element);
150 gdcmHeaderEntry *GetHeaderEntryByName (std::string Name);
151 IterHT GetHeaderEntrySameNumber(guint16 group, guint16 element);
152 // IterHT GetHeaderEntrySameName (std::string Name);
154 void LoadHeaderEntrySafe(gdcmHeaderEntry *);
156 void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
157 void WriteEntry(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
158 void WriteEntries(FILE *_fp,FileType type);
159 void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
161 void AddHeaderEntry (gdcmHeaderEntry *);
165 * \brief File Pointer, open during Header parsing
169 * \brief ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
173 static const unsigned int HEADER_LENGTH_TO_READ;
174 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
175 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
179 * \brief H Table (multimap), to provide fast access
181 TagHeaderEntryHT tagHT;
183 * \brief chained list, to keep the 'spacial' ordering
187 * \brief will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
191 * \brief amount of printed details for each Header Entry (Dicom Element)
197 * \brief For some ACR-NEMA images, it's *not* 7fe0 ...
202 * \brief For some ACR-NEMA images, it's *not* 0010 ...
206 * \brief some files may contain icons; GrPixel,NumPixel appears several times
207 * Let's remember how many times!
211 * \brief = true when the 'pixel Element' is reached during writting process
213 bool itsTimeToWritePixels;
217 bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
219 void LoadHeaderEntries (void);
220 void LoadHeaderEntry (gdcmHeaderEntry *);
221 void FindHeaderEntryLength(gdcmHeaderEntry *);
222 void FindHeaderEntryVR (gdcmHeaderEntry *);
223 bool CheckHeaderEntryVR (gdcmHeaderEntry *, VRKey);
225 std::string GetHeaderEntryValue (gdcmHeaderEntry *);
226 std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
228 void SkipHeaderEntry (gdcmHeaderEntry *);
229 void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
230 bool IsHeaderEntryAnInteger (gdcmHeaderEntry *);
232 guint32 FindHeaderEntryLengthOB(void);
234 guint16 ReadInt16(void);
235 guint32 ReadInt32(void);
236 void SkipBytes(guint32);
238 void Initialise(void);
239 bool CheckSwap(void);
240 void SwitchSwapToBigEndian(void);
241 void SetMaxSizeLoadEntry(long);
242 void SetMaxSizePrintEntry(long);
244 // DictEntry related utilities
245 gdcmDictEntry *GetDictEntryByName (std::string Name);
246 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
247 gdcmDictEntry *NewVirtualDictEntry(guint16 group,
249 std::string vr = "unkn",
250 std::string fourth = "unkn",
251 std::string name = "unkn");
252 //gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *); // never defined
254 // HeaderEntry related utilities
256 gdcmHeaderEntry *ReadNextHeaderEntry (void);
257 gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group,
259 gdcmHeaderEntry *NewHeaderEntryByName (std::string Name);
262 // Deprecated (Not used) --> commented out
263 //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
266 guint32 GenerateFreeTagKeyInGroup(guint16 group);
269 * \brief Refering underlying filename.
271 std::string filename;
274 * \brief Public dictionary used to parse this header
276 gdcmDict *RefPubDict;
279 * \brief Optional "shadow dictionary" (private elements) used to parse this header
281 gdcmDict *RefShaDict;
284 * \brief = 1 if a gdcmHeaderEntry was added post parsing
289 * \brief =1 if user wants to skip shadow groups while parsing (to save space)
294 * \brief Swap code e.g. little, big, bad-big, bad-little endian).
295 * \warning : this code is not fixed during header parsing.
299 * \brief Size treshold above which an element value will NOT be loaded in
300 * memory (to avoid loading the image/volume itself). By default,
301 * this upper bound is fixed to 1024 bytes (which might look reasonable
302 * when one considers the definition of the various VR contents).
304 guint32 MaxSizeLoadEntry;
307 * \brief Size treshold above which an element value will NOT be *printed* in
308 * order no to polute the screen output.
309 * By default, this upper bound is fixed to 64 bytes.
311 guint32 MaxSizePrintEntry;
315 //-----------------------------------------------------------------------------