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 gdcmHeaderEntry * ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
109 gdcmHeaderEntry * 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 *);
164 * \ingroup gdcmParser
165 * \brief Set the external File Name
167 inline void SetFileName(char* fileName)
168 {filename = fileName;}
172 * \brief File Pointer, open during Header parsing
176 * \brief ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
180 static const unsigned int HEADER_LENGTH_TO_READ;
181 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
182 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
186 * \brief H Table (multimap), to provide fast access
188 TagHeaderEntryHT tagHT;
190 * \brief chained list, to keep the 'spacial' ordering
194 * \brief will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
198 * \brief amount of printed details for each Header Entry (Dicom Element)
204 * \brief For some ACR-NEMA images, it's *not* 7fe0 ...
209 * \brief For some ACR-NEMA images, it's *not* 0010 ...
213 * \brief some files may contain icons; GrPixel,NumPixel appears several times
214 * Let's remember how many times!
218 * \brief = true when the 'pixel Element' is reached during writting process
220 bool itsTimeToWritePixels;
224 bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
226 void LoadHeaderEntries (void);
227 void LoadHeaderEntry (gdcmHeaderEntry *);
228 void FindHeaderEntryLength(gdcmHeaderEntry *);
229 void FindHeaderEntryVR (gdcmHeaderEntry *);
230 bool CheckHeaderEntryVR (gdcmHeaderEntry *, VRKey);
232 std::string GetHeaderEntryValue (gdcmHeaderEntry *);
233 std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
235 void SkipHeaderEntry (gdcmHeaderEntry *);
236 void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
237 bool IsHeaderEntryAnInteger (gdcmHeaderEntry *);
239 guint32 FindHeaderEntryLengthOB(void);
241 guint16 ReadInt16(void);
242 guint32 ReadInt32(void);
243 void SkipBytes(guint32);
245 void Initialise(void);
246 bool CheckSwap(void);
247 void SwitchSwapToBigEndian(void);
248 void SetMaxSizeLoadEntry(long);
249 void SetMaxSizePrintEntry(long);
251 // DictEntry related utilities
252 gdcmDictEntry *GetDictEntryByName (std::string Name);
253 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
254 gdcmDictEntry *NewVirtualDictEntry(guint16 group,
256 std::string vr = "unkn",
257 std::string fourth = "unkn",
258 std::string name = "unkn");
259 //gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *); // never defined
261 // HeaderEntry related utilities
263 gdcmHeaderEntry *ReadNextHeaderEntry (void);
264 gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group,
266 gdcmHeaderEntry *NewHeaderEntryByName (std::string Name);
268 // Deprecated (Not used) --> commented out
269 //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
272 guint32 GenerateFreeTagKeyInGroup(guint16 group);
275 * \brief Refering underlying filename.
277 std::string filename;
280 * \brief Public dictionary used to parse this header
282 gdcmDict *RefPubDict;
285 * \brief Optional "shadow dictionary" (private elements) used to parse this header
287 gdcmDict *RefShaDict;
290 * \brief = 1 if a gdcmHeaderEntry was added post parsing
295 * \brief =1 if user wants to skip shadow groups while parsing (to save space)
300 * \brief Swap code e.g. little, big, bad-big, bad-little endian).
301 * \warning : this code is not fixed during header parsing.
305 * \brief Size threshold above which an element value will NOT be loaded in
306 * memory (to avoid loading the image/volume itself). By default,
307 * this upper bound is fixed to 1024 bytes (which might look reasonable
308 * when one considers the definition of the various VR contents).
310 guint32 MaxSizeLoadEntry;
313 * \brief Size threshold above which an element value will NOT be *printed* in
314 * order no to polute the screen output.
315 * By default, this upper bound is fixed to 64 bytes.
317 guint32 MaxSizePrintEntry;
321 //-----------------------------------------------------------------------------