]> Creatis software - gdcm.git/blob - src/gdcmParser.h
adding testDICICOMDIR.cxx to check DICOMDIR utilities
[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 PrintEntry(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
56    inline std::string GetFileName(void) {return filename;}
57
58 // Dictionnaries
59    gdcmDict *GetPubDict(void);
60    gdcmDict *GetShaDict(void);
61    bool SetShaDict(gdcmDict *dict);
62    bool SetShaDict(DictKey dictName);
63
64 // Informations contained in the parser
65    bool IsReadable(void);
66    bool IsImplicitVRLittleEndianTransferSyntax(void);
67    bool IsExplicitVRLittleEndianTransferSyntax(void);
68    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
69    bool IsExplicitVRBigEndianTransferSyntax(void);
70    FileType GetFileType(void);
71
72 // Entries
73    /**
74     * \ingroup gdcmHeader
75     * \brief   returns a ref to the Dicom Header H table (multimap)
76     * return the Dicom Header H table
77     */
78    inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
79
80    /**
81     * \ingroup gdcmHeader
82     * \brief   returns a ref to the Dicom Header chained list
83     * return the Dicom Header chained list
84     */
85    inline ListTag &GetListEntry(void) { return listEntries; };
86
87 // Read (used in gdcmFile)
88    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
89    bool CloseFile(void);
90
91 // Write (used in gdcmFile)
92    virtual bool Write(FILE *, FileType);
93
94    bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
95    bool ReplaceOrCreateByNumber(     char  *Value, guint16 Group, guint16 Elem);
96    bool ReplaceIfExistByNumber (     char  *Value, guint16 Group, guint16 Elem);
97
98 // System access
99    inline int GetSwapCode(void) { return sw; }
100    guint16 SwapShort(guint16); // needed by gdcmFile
101    guint32 SwapLong(guint32);  // needed by gdcmFile
102    guint16 UnswapShort(guint16); // needed by gdcmFile
103    guint32 UnswapLong(guint32);  // needed by gdcmFile
104
105 protected:
106 // Entry
107    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
108    virtual std::string GetEntryByName    (std::string tagName);
109    virtual std::string GetEntryVRByName  (std::string tagName);
110    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
111    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
112
113    virtual bool SetEntryByName  (std::string content, std::string tagName);
114    virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
115    virtual bool SetEntryLengthByNumber(guint32 l, guint16 group, guint16 element);
116
117    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
118    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
119    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
120    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
121
122    virtual void UpdateShaEntries(void);
123
124 // Header entry
125    gdcmHeaderEntry *GetHeaderEntryByName  (std::string Name);
126    gdcmHeaderEntry *GetHeaderEntryByNumber(guint16 group, guint16 element); 
127
128    void LoadHeaderEntrySafe(gdcmHeaderEntry *);
129
130    void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
131    void WriteEntries(FileType type, FILE *);
132
133 // Variables
134    FILE *fp;
135    FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
136
137    static const unsigned int HEADER_LENGTH_TO_READ; 
138    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
139 protected:
140    int enableSequences;
141 private:
142    // Read
143    void Parse(bool exception_on_error = false) throw(gdcmFormatError);
144
145    void LoadHeaderEntries    (void);
146    void LoadHeaderEntry      (gdcmHeaderEntry *);
147    void AddHeaderEntry       (gdcmHeaderEntry *);
148    void FindHeaderEntryLength(gdcmHeaderEntry *);
149    void FindHeaderEntryVR    (gdcmHeaderEntry *);
150    bool CheckHeaderEntryVR   (gdcmHeaderEntry *, VRKey);
151
152    std::string GetHeaderEntryValue  (gdcmHeaderEntry *);
153    std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
154
155    void SkipHeaderEntry          (gdcmHeaderEntry *);
156    void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
157    bool IsHeaderEntryAnInteger   (gdcmHeaderEntry *);
158
159    guint32 FindHeaderEntryLengthOB(void);
160
161    guint16 ReadInt16(void);
162    guint32 ReadInt32(void);
163    void    SkipBytes(guint32);
164
165    void Initialise(void);
166    void CheckSwap(void);
167    void SwitchSwapToBigEndian(void);
168    void SetMaxSizeLoadEntry(long);
169
170    // Dict
171    gdcmDictEntry *GetDictEntryByName  (std::string Name);
172    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
173
174    // HeaderEntry related utilities
175    gdcmHeaderEntry *ReadNextHeaderEntry   (void);
176    gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group, guint16 element);
177    gdcmHeaderEntry *NewHeaderEntryByName  (std::string Name);
178    gdcmDictEntry *NewVirtualDictEntry(guint16 group, guint16 element,
179                                       std::string vr = "Unknown",
180                                       std::string fourth = "Unknown",
181                                       std::string name   = "Unknown");
182    gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *);
183
184    // Deprecated (Not used)
185    gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
186                                                   std::string VR);
187    guint32 GenerateFreeTagKeyInGroup(guint16 group);
188
189    // Refering underlying filename.
190    std::string filename; 
191
192    // Public dictionary used to parse this header
193    gdcmDict *RefPubDict;
194    // Optional "shadow dictionary" (private elements) used to parse this header
195    gdcmDict *RefShaDict;
196
197    TagHeaderEntryHT tagHT; // H Table (multimap), to provide fast access
198    ListTag listEntries;    // chained list, to keep the 'spacial' ordering 
199
200
201    // true if a gdcmHeaderEntry was added post parsing 
202    int wasUpdated;
203
204    // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
205    // this code is not fixed during header parsing.
206    int sw;
207
208    // Size treshold above which an element value will NOT be loaded in 
209    // memory (to avoid loading the image/volume itself). By default,
210    // this upper bound is fixed to 1024 bytes (which might look reasonable
211    // when one considers the definition of the various VR contents).
212    guint32 MaxSizeLoadEntry;
213
214    // for PrintHeader
215    int printLevel;
216 };
217
218 //-----------------------------------------------------------------------------
219 #endif