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
42 * \brief Sets the print level for the Dicom Header
43 * \note 0 for Light Print; 1 for 'medium' Print, 2 for Heavy
45 void SetPrintLevel(int level)
46 { printLevel = level; };
49 * \brief canonical Printer
52 virtual void Print (std::ostream &os = std::cout)
54 virtual void PrintEntry (std::ostream &os = std::cout);
55 virtual void PrintPubDict (std::ostream &os = std::cout);
56 virtual void PrintShaDict (std::ostream &os = std::cout);
61 * \brief Gets the external File Name
63 inline std::string GetFileName(void)
67 gdcmDict *GetPubDict(void);
68 gdcmDict *GetShaDict(void);
69 bool SetShaDict(gdcmDict *dict);
70 bool SetShaDict(DictKey dictName);
72 // Informations contained in the parser
73 virtual bool IsReadable(void);
74 bool IsImplicitVRLittleEndianTransferSyntax(void);
75 bool IsExplicitVRLittleEndianTransferSyntax(void);
76 bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
77 bool IsExplicitVRBigEndianTransferSyntax(void);
78 FileType GetFileType(void);
83 * \brief returns a ref to the Dicom Header H table (multimap)
84 * return the Dicom Header H table
86 inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
90 * \brief returns a ref to the Dicom Header chained list
91 * return the Dicom Header chained list
93 inline ListTag &GetListEntry(void) { return listEntries; };
95 // Read (used in gdcmFile, gdcmDicomDir)
96 FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
99 // Write (used in gdcmFile, gdcmDicomDir)
100 virtual bool Write(FILE *, FileType);
102 gdcmHeaderEntry * ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
103 gdcmHeaderEntry * ReplaceOrCreateByNumber( char *Value, guint16 Group, guint16 Elem);
104 bool ReplaceIfExistByNumber ( char *Value, guint16 Group, guint16 Elem);
108 * \ingroup gdcmHeader
109 * \brief returns the 'swap code'
110 * (Big Endian, Little Endian,
111 * Bad Big Endian, Bad Little Endian)
112 * according to the processor Endianity and what's written on disc
115 inline int GetSwapCode(void) { return sw; }
117 guint16 SwapShort(guint16); // needed by gdcmFile
118 guint32 SwapLong(guint32); // needed by gdcmFile
119 guint16 UnswapShort(guint16); // needed by gdcmFile
120 guint32 UnswapLong(guint32); // needed by gdcmFile
123 // constructor and destructor are protected to forbid end user to instanciate
124 // class gdcmParser (only gdcmHeader and gdcmDicomDir are meaningfull)
125 gdcmParser(bool exception_on_error = false);
126 gdcmParser(const char *inFilename,
127 bool exception_on_error = false,
128 bool enable_sequences = false,
129 bool ignore_shadow = false);
130 virtual ~gdcmParser(void);
132 int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
133 virtual std::string GetEntryByName (std::string tagName);
134 virtual std::string GetEntryVRByName (std::string tagName);
135 virtual std::string GetEntryByNumber (guint16 group, guint16 element);
136 virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
137 virtual int GetEntryLengthByNumber(guint16 group, guint16 element);
139 virtual bool SetEntryByName (std::string content, std::string tagName);
140 virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
141 virtual bool SetEntryLengthByNumber(guint32 length, guint16 group, guint16 element);
143 virtual size_t GetEntryOffsetByNumber (guint16 Group, guint16 Elem);
144 virtual void *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);
145 virtual void *LoadEntryVoidArea (guint16 Group, guint16 Element);
146 virtual bool SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
148 virtual void UpdateShaEntries(void);
151 gdcmHeaderEntry *GetHeaderEntryByNumber (guint16 group, guint16 element);
152 gdcmHeaderEntry *GetHeaderEntryByName (std::string Name);
153 IterHT GetHeaderEntrySameNumber(guint16 group, guint16 element);
154 // IterHT GetHeaderEntrySameName (std::string Name);
156 void LoadHeaderEntrySafe(gdcmHeaderEntry *);
158 void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
159 void WriteEntry(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
160 void WriteEntries(FILE *_fp,FileType type);
161 void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
163 void AddHeaderEntry (gdcmHeaderEntry *);
166 * \ingroup gdcmParser
167 * \brief Set the external File Name
169 inline void SetFileName(char* fileName)
170 {filename = fileName;}
174 * \brief File Pointer, open during Header parsing
178 * \brief ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
182 /// after opening the file, we read HEADER_LENGTH_TO_READ bytes.
183 static const unsigned int HEADER_LENGTH_TO_READ;
184 /// Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE are NOT loaded
185 static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
186 /// Elements whose value is longer than MAX_SIZE_PRINT_ELEMENT_VALUE are NOT printed
187 static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
191 * \brief H Table (multimap), to provide fast access
193 TagHeaderEntryHT tagHT;
195 * \brief chained list, to keep the 'spacial' ordering
199 * \brief will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
203 * \brief amount of printed details for each Header Entry (Dicom Element)
209 * \brief For some ACR-NEMA images, it's *not* 7fe0 ...
214 * \brief For some ACR-NEMA images, it's *not* 0010 ...
218 * \brief some files may contain icons; GrPixel,NumPixel appears several times
219 * Let's remember how many times!
223 * \brief = true when the 'pixel Element' is reached during writting process
225 bool itsTimeToWritePixels;
229 bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
231 void LoadHeaderEntries (void);
232 void LoadHeaderEntry (gdcmHeaderEntry *);
233 void FindHeaderEntryLength(gdcmHeaderEntry *);
234 void FindHeaderEntryVR (gdcmHeaderEntry *);
235 bool CheckHeaderEntryVR (gdcmHeaderEntry *, VRKey);
237 std::string GetHeaderEntryValue (gdcmHeaderEntry *);
238 std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
240 void SkipHeaderEntry (gdcmHeaderEntry *);
241 void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
242 bool IsHeaderEntryAnInteger (gdcmHeaderEntry *);
244 guint32 FindHeaderEntryLengthOB(void);
246 guint16 ReadInt16(void);
247 guint32 ReadInt32(void);
248 void SkipBytes(guint32);
250 void Initialise(void);
251 bool CheckSwap(void);
252 void SwitchSwapToBigEndian(void);
253 void SetMaxSizeLoadEntry(long);
254 void SetMaxSizePrintEntry(long);
256 // DictEntry related utilities
257 gdcmDictEntry *GetDictEntryByName (std::string Name);
258 gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
259 gdcmDictEntry *NewVirtualDictEntry(guint16 group,
261 std::string vr = "unkn",
262 std::string fourth = "unkn",
263 std::string name = "unkn");
264 //gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *); // never defined
266 // HeaderEntry related utilities
268 gdcmHeaderEntry *ReadNextHeaderEntry (void);
269 gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group,
271 gdcmHeaderEntry *NewHeaderEntryByName (std::string Name);
273 // Deprecated (Not used) --> commented out
274 //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
277 guint32 GenerateFreeTagKeyInGroup(guint16 group);
280 * \brief Refering underlying filename.
282 std::string filename;
285 * \brief Public dictionary used to parse this header
287 gdcmDict *RefPubDict;
290 * \brief Optional "shadow dictionary" (private elements) used to parse this header
292 gdcmDict *RefShaDict;
295 * \brief = 1 if a gdcmHeaderEntry was added post parsing
300 * \brief =1 if user wants to skip shadow groups while parsing (to save space)
305 * \brief Swap code e.g. little, big, bad-big, bad-little endian).
306 * \warning : this code is not fixed during header parsing.
310 * \brief Size threshold above which an element value will NOT be loaded in
311 * memory (to avoid loading the image/volume itself). By default,
312 * this upper bound is fixed to 1024 bytes (which might look reasonable
313 * when one considers the definition of the various VR contents).
315 guint32 MaxSizeLoadEntry;
318 * \brief Size threshold above which an element value will NOT be *printed* in
319 * order no to polute the screen output.
320 * By default, this upper bound is fixed to 64 bytes.
322 guint32 MaxSizePrintEntry;
326 //-----------------------------------------------------------------------------