]> Creatis software - gdcm.git/blob - src/gdcmParser.h
ENH: Remove redundancie about GDCM_DICT stuff, now we only need to modify
[gdcm.git] / src / gdcmParser.h
1 // gdcmParser.h
2 //-----------------------------------------------------------------------------
3 #ifndef GDCMPARSER_H
4 #define GDCMPARSER_H
5
6 #include "gdcmCommon.h"
7
8 #include "gdcmVR.h"
9 #include "gdcmTS.h"
10 #include "gdcmException.h"
11 #include "gdcmDictSet.h"
12 #include "gdcmHeaderEntry.h"
13
14 #include <map>
15 #include <list>
16
17
18
19 //-----------------------------------------------------------------------------
20 typedef std::string VRKey;
21 typedef std::string VRAtr;
22 typedef std::map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
23
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;
29
30 typedef std::string GroupKey;
31 typedef std::map<GroupKey, int> GroupHT;
32
33 //-----------------------------------------------------------------------------
34 /**
35  * \brief used by both gdcmHeader and gdcmDicomDir
36  */
37 class GDCM_EXPORT gdcmParser
38 {
39 private:
40    /// Public dictionary used to parse this header
41    gdcmDict *RefPubDict;
42    
43    /// Optional "shadow dictionary" (private elements) used to parse
44    /// this header
45    gdcmDict *RefShaDict;
46
47    /// Equals 1 if a gdcmHeaderEntry was added post parsing 
48    int wasUpdated;
49    
50    /// Equals =1 if user wants to skip shadow groups while parsing
51    /// (to save space)
52    int ignoreShadow;
53
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;
59    
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;
64
65 protected:
66    /// Refering underlying filename.
67    std::string filename; 
68
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.
72    int sw;
73
74    /// File Pointer, opened during Header parsing.
75    FILE *fp;
76
77    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
78    FileType filetype;  
79
80    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
81    static const unsigned int HEADER_LENGTH_TO_READ; 
82
83    /// Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
84    /// are NOT loaded.
85    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
86
87    /// Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
88    /// are NOT printed.
89    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
90
91    /// Hash Table (multimap), to provide fast access
92    TagHeaderEntryHT tagHT; 
93
94    /// Chained list, to keep the 'spacial' ordering
95    ListTag listEntries; 
96
97    /// will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
98    int enableSequences;
99
100    /// Amount of printed details for each Header Entry (Dicom Element):
101    /// 0 : stands for the least detail level.
102    int printLevel;
103    
104 public:
105    
106
107 // Print
108    /// Canonical Printing method (see also gdcmParser::SetPrintLevel)
109    virtual void Print        (std::ostream &os = std::cout) 
110       {PrintEntry(os);};
111    virtual void PrintEntry      (std::ostream &os = std::cout);
112    virtual void PrintEntryNoSQ  (std::ostream &os = std::cout);
113    // the 2 following will be merged
114    virtual void PrintEntryNiceSQ(std::ostream &os = std::cout);
115    virtual void PrintPubDict (std::ostream &os = std::cout);
116    virtual void PrintShaDict (std::ostream &os = std::cout);
117
118 // Dictionnaries
119    gdcmDict *GetPubDict(void);
120    gdcmDict *GetShaDict(void);
121    bool SetShaDict(gdcmDict *dict);
122    bool SetShaDict(DictKey dictName);
123
124 // Informations contained in the parser
125    virtual bool IsReadable(void);
126    bool IsImplicitVRLittleEndianTransferSyntax(void);
127    bool IsExplicitVRLittleEndianTransferSyntax(void);
128    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
129    bool IsExplicitVRBigEndianTransferSyntax(void);
130    FileType GetFileType(void);
131
132 // Read (used in gdcmFile, gdcmDicomDir)
133    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
134    bool CloseFile(void);
135
136 // Write (used in gdcmFile, gdcmDicomDir)
137    virtual bool Write(FILE *, FileType);
138    virtual void WriteEntryTagVRLength(gdcmHeaderEntry *tag,
139                                        FILE *_fp, FileType type);
140    virtual void WriteEntryValue(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
141    virtual bool WriteEntry(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
142    virtual bool WriteEntries(FILE *_fp,FileType type);
143    void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
144
145    gdcmHeaderEntry * ReplaceOrCreateByNumber(std::string Value,
146                                              guint16 Group, guint16 Elem);
147    bool ReplaceIfExistByNumber (char *Value, guint16 Group, guint16 Elem);
148
149 // System access
150    guint16 SwapShort(guint16);   // needed by gdcmFile
151    guint32 SwapLong(guint32);    // needed by gdcmFile
152    guint16 UnswapShort(guint16); // needed by gdcmFile
153    guint32 UnswapLong(guint32);  // needed by gdcmFile
154
155 protected:
156    // Constructor and destructor are protected to forbid end user 
157    // to instanciate from this class gdcmParser (only gdcmHeader and
158    // gdcmDicomDir are meaningfull).
159    gdcmParser(bool exception_on_error  = false);
160    gdcmParser(const char *inFilename, 
161               bool  exception_on_error = false, 
162               bool  enable_sequences   = false,
163               bool  ignore_shadow      = false);
164    virtual ~gdcmParser(void);
165 // Entry
166    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
167    virtual std::string GetEntryByName    (std::string tagName);
168    virtual std::string GetEntryVRByName  (std::string tagName);
169    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
170    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
171    virtual int     GetEntryLengthByNumber(guint16 group, guint16 element);
172
173    virtual bool SetEntryByName  (std::string content, std::string tagName);
174    virtual bool SetEntryByNumber(std::string content,
175                                  guint16 group, guint16 element);
176    virtual bool SetEntryLengthByNumber(guint32 length,
177                                  guint16 group, guint16 element);
178
179    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
180    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
181    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
182    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
183
184    virtual void UpdateShaEntries(void);
185
186 // Header entry
187    gdcmHeaderEntry *GetHeaderEntryByNumber  (guint16 group, guint16 element); 
188    gdcmHeaderEntry *GetHeaderEntryByName    (std::string Name);
189    IterHT           GetHeaderEntrySameNumber(guint16 group, guint16 element); 
190 // IterHT           GetHeaderEntrySameName  (std::string Name); 
191
192    void LoadHeaderEntrySafe(gdcmHeaderEntry *);
193
194    void UpdateGroupLength(bool SkipSequence = false,
195                           FileType type = ImplicitVR);
196
197    void AddHeaderEntry       (gdcmHeaderEntry *);
198    
199       
200 private:
201    // Read
202    bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
203
204    void LoadHeaderEntries    (void);
205    void LoadHeaderEntry      (gdcmHeaderEntry *);
206    void FindHeaderEntryLength(gdcmHeaderEntry *);
207    void FindHeaderEntryVR    (gdcmHeaderEntry *);
208    bool CheckHeaderEntryVR   (gdcmHeaderEntry *, VRKey);
209
210    std::string GetHeaderEntryValue  (gdcmHeaderEntry *);
211    std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
212
213    void SkipHeaderEntry          (gdcmHeaderEntry *);
214    void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
215    bool IsHeaderEntryAnInteger   (gdcmHeaderEntry *);
216
217    guint32 FindHeaderEntryLengthOB(void);
218
219    guint16 ReadInt16(void);
220    guint32 ReadInt32(void);
221    void    SkipBytes(guint32);
222
223    void Initialise(void);
224    bool CheckSwap(void);
225    void SwitchSwapToBigEndian(void);
226    void SetMaxSizeLoadEntry(long);
227    void SetMaxSizePrintEntry(long);
228
229    // DictEntry  related utilities
230    gdcmDictEntry *GetDictEntryByName  (std::string Name);
231    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
232    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
233                                       guint16 element,
234                                       std::string vr     = "unkn",
235                                       std::string fourth = "unkn",
236                                       std::string name   = "unkn");
237    //gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *); // never defined
238    
239    // HeaderEntry related utilities
240    
241    gdcmHeaderEntry *ReadNextHeaderEntry   (void);
242    gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group, 
243                                            guint16 element);
244    gdcmHeaderEntry *NewHeaderEntryByName  (std::string Name);
245    
246    // Deprecated (Not used) --> commented out
247    //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
248    //                                               std::string VR);
249    
250    guint32 GenerateFreeTagKeyInGroup(guint16 group);
251
252 public:
253 // Accessors:
254    /// Accessor to \ref printLevel
255    void SetPrintLevel(int level) { printLevel = level; };
256
257    /// Accessor to \ref filename
258    inline std::string GetFileName(void) {return filename;}
259
260    /// Accessor to \ref filename
261    inline void SetFileName(char* fileName) {filename = fileName;}
262
263    /// Accessor to \ref gdcmParser::tagHT
264    inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
265
266    /// Accessor to \ref gdcmParser::listEntries
267    inline ListTag &GetListEntry(void) { return listEntries; };
268
269    /// 'Swap code' accessor (see \ref sw )
270    inline int GetSwapCode(void) { return sw; }
271 };
272
273 //-----------------------------------------------------------------------------
274 #endif