]> Creatis software - gdcm.git/blob - src/gdcmParser.h
2ab4cce6eb2ab9339a89527729d2735e17807152
[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 gdcmParser
33  * \brief used by both gdcmHeader and gdcmDicomDir
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               bool  ignore_shadow      = false);
43    virtual ~gdcmParser(void);
44
45 // Print
46    /**
47     * \ingroup gdcmParser
48     * \brief   Sets the print level for the Dicom Header 
49     * \note    0 for Light Print; 1 for 'medium' Print, 2 for Heavy
50     */
51    void SetPrintLevel(int level) { printLevel = level; };
52    virtual void PrintEntry   (std::ostream &os = std::cout);
53    virtual void PrintPubDict (std::ostream &os = std::cout);
54    virtual void PrintShaDict (std::ostream &os = std::cout);
55
56 // Standard values
57    inline std::string GetFileName(void) {return filename;}
58
59 // Dictionnaries
60    gdcmDict *GetPubDict(void);
61    gdcmDict *GetShaDict(void);
62    bool SetShaDict(gdcmDict *dict);
63    bool SetShaDict(DictKey dictName);
64
65 // Informations contained in the parser
66    virtual bool IsReadable(void);
67    bool IsImplicitVRLittleEndianTransferSyntax(void);
68    bool IsExplicitVRLittleEndianTransferSyntax(void);
69    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
70    bool IsExplicitVRBigEndianTransferSyntax(void);
71    FileType GetFileType(void);
72
73 // Entries
74    /**
75     * \ingroup gdcmHeader
76     * \brief   returns a ref to the Dicom Header H table (multimap)
77     * return the Dicom Header H table
78     */
79    inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
80
81    /**
82     * \ingroup gdcmHeader
83     * \brief   returns a ref to the Dicom Header chained list
84     * return the Dicom Header chained list
85     */
86    inline ListTag &GetListEntry(void) { return listEntries; };
87
88 // Read (used in gdcmFile)
89    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
90    bool CloseFile(void);
91
92 // Write (used in gdcmFile)
93    virtual bool Write(FILE *, FileType);
94
95    bool ReplaceOrCreateByNumber(std::string Value, guint16 Group, guint16 Elem);
96    bool ReplaceOrCreateByNumber(     char  *Value, guint16 Group, guint16 Elem);
97    bool ReplaceIfExistByNumber (     char  *Value, guint16 Group, guint16 Elem);
98
99 // System access
100    inline int GetSwapCode(void) { return sw; }
101    guint16 GetGrPixel(void) {return GrPixel;}
102    guint16 GetNumPixel(void) {return NumPixel;}
103    
104    guint16 SwapShort(guint16);   // needed by gdcmFile
105    guint32 SwapLong(guint32);    // needed by gdcmFile
106    guint16 UnswapShort(guint16); // needed by gdcmFile
107    guint32 UnswapLong(guint32);  // needed by gdcmFile
108
109 protected:
110 // Entry
111    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
112    virtual std::string GetEntryByName    (std::string tagName);
113    virtual std::string GetEntryVRByName  (std::string tagName);
114    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
115    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
116
117    virtual bool SetEntryByName  (std::string content, std::string tagName);
118    virtual bool SetEntryByNumber(std::string content, guint16 group, guint16 element);
119    virtual bool SetEntryLengthByNumber(guint32 l, guint16 group, guint16 element);
120
121    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
122    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
123    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
124    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
125
126    virtual void UpdateShaEntries(void);
127
128 // Header entry
129    gdcmHeaderEntry *GetHeaderEntryByNumber  (guint16 group, guint16 element); 
130    gdcmHeaderEntry *GetHeaderEntryByName    (std::string Name);
131    IterHT           GetHeaderEntrySameNumber(guint16 group, guint16 element); 
132 // IterHT           GetHeaderEntrySameName  (std::string Name); 
133
134    void LoadHeaderEntrySafe(gdcmHeaderEntry *);
135
136    void UpdateGroupLength(bool SkipSequence = false, FileType type = ImplicitVR);
137    void WriteEntries(FileType type, FILE *);
138
139 // Variables
140    FILE *fp;
141    FileType filetype; // ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
142
143    static const unsigned int HEADER_LENGTH_TO_READ; 
144    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
145    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
146
147 protected:
148    TagHeaderEntryHT tagHT; // H Table (multimap), to provide fast access
149    ListTag listEntries;    // chained list, to keep the 'spacial' ordering
150     
151    int enableSequences;
152    int printLevel;
153    
154    // For some ACR-NEMA images, it's *not* 7fe0, 0010 ...    
155    guint16 GrPixel;
156    guint16 NumPixel;
157    // some files may contain icons; GrPixel,NumPixel appears several times
158    // Let's remember how many times!
159    int countGrPixel;
160       
161 private:
162    // Read
163    bool ParseHeader(bool exception_on_error = false) throw(gdcmFormatError);
164
165    void LoadHeaderEntries    (void);
166    void LoadHeaderEntry      (gdcmHeaderEntry *);
167    void AddHeaderEntry       (gdcmHeaderEntry *);
168    void FindHeaderEntryLength(gdcmHeaderEntry *);
169    void FindHeaderEntryVR    (gdcmHeaderEntry *);
170    bool CheckHeaderEntryVR   (gdcmHeaderEntry *, VRKey);
171
172    std::string GetHeaderEntryValue  (gdcmHeaderEntry *);
173    std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
174
175    void SkipHeaderEntry          (gdcmHeaderEntry *);
176    void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
177    bool IsHeaderEntryAnInteger   (gdcmHeaderEntry *);
178
179    guint32 FindHeaderEntryLengthOB(void);
180
181    guint16 ReadInt16(void);
182    guint32 ReadInt32(void);
183    void    SkipBytes(guint32);
184
185    void Initialise(void);
186    bool CheckSwap(void);
187    void SwitchSwapToBigEndian(void);
188    void SetMaxSizeLoadEntry(long);
189    void SetMaxSizePrintEntry(long);
190
191    // DictEntry  related utilities
192    gdcmDictEntry *GetDictEntryByName  (std::string Name);
193    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
194    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
195                                       guint16 element,
196                                       std::string vr     = "Unknown",
197                                       std::string fourth = "Unknown",
198                                       std::string name   = "Unknown");
199    gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *);
200    
201    // HeaderEntry related utilities
202    gdcmHeaderEntry *ReadNextHeaderEntry   (void);
203    gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group, 
204                                            guint16 element);
205    gdcmHeaderEntry *NewHeaderEntryByName  (std::string Name);
206
207
208    // Deprecated (Not used) --> commented out
209    //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
210    //                                               std::string VR);
211    guint32 GenerateFreeTagKeyInGroup(guint16 group);
212
213    // Refering underlying filename.
214    std::string filename; 
215
216    // Public dictionary used to parse this header
217    gdcmDict *RefPubDict;
218    // Optional "shadow dictionary" (private elements) used to parse this header
219    gdcmDict *RefShaDict;
220
221    // = 1 if a gdcmHeaderEntry was added post parsing 
222    int wasUpdated;
223    
224    // =1 if user wants to skip shadow groups while parsing (to save space)
225    int ignoreShadow;
226
227    // Swap code e.g. little, big, bad-big, bad-little endian). Warning:
228    // this code is not fixed during header parsing.
229    int sw;
230
231    // Size treshold above which an element value will NOT be loaded in 
232    // memory (to avoid loading the image/volume itself). By default,
233    // this upper bound is fixed to 1024 bytes (which might look reasonable
234    // when one considers the definition of the various VR contents).
235    guint32 MaxSizeLoadEntry;
236    // Size treshold above which an element value will NOT be *printed* in 
237    // order no to polute the screen output. By default,
238    // this upper bound is fixed to 64 bytes.   
239    guint32 MaxSizePrintEntry;
240    
241 };
242
243 //-----------------------------------------------------------------------------
244 #endif