]> Creatis software - gdcm.git/blob - src/gdcmParser.h
054c158a6d5ce80f7da0680e77d90aa48eab9c70
[gdcm.git] / src / gdcmParser.h
1 // gdcmParser.h
2 //-----------------------------------------------------------------------------
3 #ifndef GDCMPARSER_H
4 #define GDCMPARSER_H
5
6 #include "gdcmCommon.h"
7 #include "gdcmVR.h"
8 #include "gdcmTS.h"
9 #include "gdcmException.h"
10 #include "gdcmDictSet.h"
11 #include "gdcmHeaderEntry.h"
12
13 #include <map>
14 #include <list>       // for linking together *all* the Dicom Elements
15
16 //-----------------------------------------------------------------------------
17 typedef std::string VRKey;
18 typedef std::string VRAtr;
19 typedef std::map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
20
21 typedef std::multimap<TagKey, gdcmHeaderEntry*> TagHeaderEntryHT;
22 typedef std::pair<TagKey, gdcmHeaderEntry*> PairHT;
23 typedef std::pair<TagHeaderEntryHT::iterator,TagHeaderEntryHT::iterator> IterHT; 
24
25 typedef std::list<gdcmHeaderEntry*> ListTag; // for linking together the Elements
26
27 typedef std::string GroupKey;
28 typedef std::map<GroupKey, int> GroupHT;
29
30 //-----------------------------------------------------------------------------
31 /*
32  * \defgroup gdcmHeader
33  * \brief
34  */
35 class GDCM_EXPORT gdcmParser
36 {
37 public:
38    gdcmParser(bool exception_on_error = false);
39    gdcmParser(const char *filename, 
40               bool  exception_on_error = false, 
41               bool  enable_sequences   = false);
42    virtual ~gdcmParser(void);
43
44 // Print
45    /**
46     * \ingroup gdcmParser
47     * \brief   Sets the print level for the Dicom Header 
48     * \note    0 for Light Print; 1 for 'medium' Print, 2 for Heavy
49     */
50    void SetPrintLevel(int level) { printLevel = level; };
51    virtual void PrintPubEntry(std::ostream & os = std::cout);
52    virtual void PrintPubDict (std::ostream & os = std::cout);
53    virtual void PrintShaDict (std::ostream & os = std::cout);
54
55 // Standard values and informations contained in the header
56    inline std::string GetFileName(void) {return filename;}
57
58    bool IsReadable(void);
59    bool IsImplicitVRLittleEndianTransferSyntax(void);
60    bool IsExplicitVRLittleEndianTransferSyntax(void);
61    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
62    bool IsExplicitVRBigEndianTransferSyntax(void);
63    FileType GetFileType(void);
64
65 // Entries
66    /**
67     * \ingroup gdcmHeader
68     * \brief   returns a ref to the Dicom Header H table (multimap)
69     * return the Dicom Header H table
70     */
71    inline TagHeaderEntryHT &GetPubEntry(void) { return tagHT; };
72
73    /**
74     * \ingroup gdcmHeader
75     * \brief   returns a ref to the Dicom Header chained list
76     * return the Dicom Header chained list
77     */
78    inline ListTag &GetPubListEntry(void) { return listEntries; };
79
80 // Read (used in gdcmFile)
81    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
82    bool CloseFile(void);
83    virtual void Parse(bool exception_on_error = false) throw(gdcmFormatError);
84
85 // Write (used in gdcmFile)
86    virtual bool Write(FILE *, FileType);
87
88    bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
89    bool ReplaceOrCreateByNumber(     char  *Value, guint16 Group, guint16 Elem);
90    bool ReplaceIfExistByNumber (     char  *Value, guint16 Group, guint16 Elem);
91
92 // System access
93    inline int GetSwapCode(void) { return sw; }
94    guint16 SwapShort(guint16); // needed by gdcmFile
95    guint32 SwapLong(guint32);  // for JPEG Files
96
97 protected:
98 // Entry
99    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
100    virtual std::string GetEntryByName    (std::string tagName);
101    virtual std::string GetEntryVRByName  (std::string tagName);
102    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
103    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
104
105    virtual bool SetEntryByName  (std::string content, std::string tagName);
106    virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
107    virtual bool SetEntryLengthByNumber(guint32 l, guint16 group, guint16 element);
108
109    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
110    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
111    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
112    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
113
114 // Header entry
115    gdcmHeaderEntry *GetHeaderEntryByName  (std::string Name);
116    gdcmHeaderEntry *GetHeaderEntryByNumber(guint16 group, guint16 element); 
117
118    void LoadHeaderEntrySafe  (gdcmHeaderEntry *);
119
120    void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
121    void WriteEntries(FileType type, FILE *);
122
123 // Variables
124    FILE *fp;
125    FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
126
127    static const unsigned int HEADER_LENGTH_TO_READ; 
128    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
129
130 private:
131    // Read
132    void LoadHeaderEntries    (void);
133    void LoadHeaderEntry      (gdcmHeaderEntry *);
134    void AddHeaderEntry       (gdcmHeaderEntry *);
135    void FindHeaderEntryLength(gdcmHeaderEntry *);
136    void FindHeaderEntryVR    (gdcmHeaderEntry *);
137
138    void SkipHeaderEntry          (gdcmHeaderEntry *);
139    void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
140    bool IsHeaderEntryAnInteger   (gdcmHeaderEntry *);
141
142    guint32 FindHeaderEntryLengthOB(void);
143
144    guint16 ReadInt16(void);
145    guint32 ReadInt32(void);
146    void    SkipBytes(guint32);
147
148    void Initialise(void);
149    void CheckSwap(void);
150    void SwitchSwapToBigEndian(void);
151    void SetMaxSizeLoadEntry(long);
152
153    // Dict
154    gdcmDictEntry *GetDictEntryByName  (std::string Name);
155    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
156
157    // HeaderEntry related utilities
158    gdcmHeaderEntry *ReadNextHeaderEntry   (void);
159    gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group, guint16 element);
160    gdcmHeaderEntry *NewHeaderEntryByName  (std::string Name);
161
162    // Deprecated (Not used)
163    gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
164                                                   std::string VR);
165    guint32 GenerateFreeTagKeyInGroup(guint16 group);
166
167    // Refering underlying filename.
168    std::string filename; 
169
170    // Pointer to the Value Representation Hash Table which contains all
171    // the VR of the DICOM version3 public dictionary. 
172    gdcmVR *dicom_vr;     // Not a class member for thread-safety reasons
173    
174    // Pointer to the Transfert Syntax Hash Table which contains all
175    // the TS of the DICOM version3 public dictionary. 
176    gdcmTS *dicom_ts;     // Not a class member for thread-safety reasons 
177      
178    // Pointer to global dictionary container
179    gdcmDictSet *Dicts;   // Not a class member for thread-safety reasons
180    
181    // Public dictionary used to parse this header
182    gdcmDict *RefPubDict;
183    
184    // Optional "shadow dictionary" (private elements) used to parse this
185    // header
186    gdcmDict *RefShaDict;
187
188    TagHeaderEntryHT tagHT; // H Table (multimap), to provide fast access
189    ListTag listEntries;    // chained list, to keep the 'spacial' ordering 
190
191    // true if a gdcmHeaderEntry was added post parsing 
192    int wasUpdated;
193    // for PrintHeader
194    int printLevel;
195
196    int enableSequences;
197
198    // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
199    // this code is not fixed during header parsing.
200    int sw;
201
202    // Size treshold above which an element value will NOT be loaded in 
203    // memory (to avoid loading the image/volume itself). By default,
204    // this upper bound is fixed to 1024 bytes (which might look reasonable
205    // when one considers the definition of the various VR contents).
206    guint32 MaxSizeLoadEntry;
207 };
208
209 //-----------------------------------------------------------------------------
210 #endif