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