]> Creatis software - gdcm.git/blob - src/gdcmParser.h
BUG: JP suggest me to do this change
[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    /// \brief 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    /// \brief Equals =1 if user wants to skip shadow groups while parsing
51    /// (to save space)
52    int ignoreShadow;
53
54    /// \brief Size threshold above which an element value will NOT be loaded
55    /// in 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    /// \brief Size threshold above which an element value will NOT be *printed*
61    /// in 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    /// \brief 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    /// \brief 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    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
88    /// are NOT printed.
89    /// \todo Currently not used since collides with #define in
90    ///       \ref gdcmHeaderEntry.cxx. See also
91    ///       \ref gdcmParser::SetMaxSizePrintEntry()
92    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
93
94    /// Hash Table (multimap), to provide fast access
95    TagHeaderEntryHT tagHT; 
96
97    /// Chained list, to keep the 'spacial' ordering
98    ListTag listEntries; 
99
100    /// Will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
101    int enableSequences;
102
103    /// \brief Amount of printed details for each Header Entry (Dicom Element):
104    /// 0 : stands for the least detail level.
105    int printLevel;
106    
107 public:
108    
109
110 // Print
111    /// Canonical Printing method (see also gdcmParser::SetPrintLevel)
112    virtual void Print        (std::ostream &os = std::cout) 
113       {PrintEntry(os);};
114    virtual void PrintEntry      (std::ostream &os = std::cout);
115
116    // the 2 following will be merged
117    virtual void PrintPubDict (std::ostream &os = std::cout);
118    virtual void PrintShaDict (std::ostream &os = std::cout);
119
120 // Dictionnaries
121    gdcmDict *GetPubDict(void);
122    gdcmDict *GetShaDict(void);
123    bool SetShaDict(gdcmDict *dict);
124    bool SetShaDict(DictKey dictName);
125
126 // Informations contained in the parser
127    virtual bool IsReadable(void);
128    bool IsImplicitVRLittleEndianTransferSyntax(void);
129    bool IsExplicitVRLittleEndianTransferSyntax(void);
130    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
131    bool IsExplicitVRBigEndianTransferSyntax(void);
132    FileType GetFileType(void);
133
134 // Read (used in gdcmFile, gdcmDicomDir)
135    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
136    bool CloseFile(void);
137
138 // Write (used in gdcmFile, gdcmDicomDir)
139    virtual bool Write(FILE *, FileType);
140    virtual void WriteEntryTagVRLength(gdcmHeaderEntry *tag,
141                                        FILE *_fp, FileType type);
142    virtual void WriteEntryValue(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
143    virtual bool WriteEntry(gdcmHeaderEntry *tag,FILE *_fp,FileType type);
144    virtual bool WriteEntries(FILE *_fp,FileType type);
145    void WriteEntriesDeprecated(FILE *_fp,FileType type); // JPR
146
147    gdcmHeaderEntry * ReplaceOrCreateByNumber(std::string Value,
148                                              guint16 Group, guint16 Elem);
149    bool ReplaceIfExistByNumber (char *Value, guint16 Group, guint16 Elem);
150
151 // System access
152    guint16 SwapShort(guint16);   // needed by gdcmFile
153    guint32 SwapLong(guint32);    // needed by gdcmFile
154    guint16 UnswapShort(guint16); // needed by gdcmFile
155    guint32 UnswapLong(guint32);  // needed by gdcmFile
156
157 protected:
158    // Constructor and destructor are protected to forbid end user 
159    // to instanciate from this class gdcmParser (only gdcmHeader and
160    // gdcmDicomDir are meaningfull).
161    gdcmParser(bool exception_on_error  = false);
162    gdcmParser(const char *inFilename, 
163               bool  exception_on_error = false, 
164               bool  enable_sequences   = false,
165               bool  ignore_shadow      = false);
166    virtual ~gdcmParser(void);
167 // Entry
168    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
169    virtual std::string GetEntryByName    (std::string tagName);
170    virtual std::string GetEntryVRByName  (std::string tagName);
171    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
172    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
173    virtual int     GetEntryLengthByNumber(guint16 group, guint16 element);
174
175    virtual bool SetEntryByName  (std::string content, std::string tagName);
176    virtual bool SetEntryByNumber(std::string content,
177                                  guint16 group, guint16 element);
178    virtual bool SetEntryLengthByNumber(guint32 length,
179                                  guint16 group, guint16 element);
180
181    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
182    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
183    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
184    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
185
186    virtual void UpdateShaEntries(void);
187
188 // Header entry
189    gdcmHeaderEntry *GetHeaderEntryByNumber  (guint16 group, guint16 element); 
190    gdcmHeaderEntry *GetHeaderEntryByName    (std::string Name);
191    IterHT           GetHeaderEntrySameNumber(guint16 group, guint16 element); 
192 // IterHT           GetHeaderEntrySameName  (std::string Name); 
193
194    void LoadHeaderEntrySafe(gdcmHeaderEntry *);
195
196    void UpdateGroupLength(bool SkipSequence = false,
197                           FileType type = gdcmImplicitVR);
198
199    void AddHeaderEntry       (gdcmHeaderEntry *);
200    
201       
202 private:
203    // Read
204    bool LoadHeaderEntries(bool exception_on_error = false) throw(gdcmFormatError);
205
206    void LoadHeaderEntry      (gdcmHeaderEntry *);
207    void FindHeaderEntryLength(gdcmHeaderEntry *);
208    void FindHeaderEntryVR    (gdcmHeaderEntry *);
209    bool CheckHeaderEntryVR   (gdcmHeaderEntry *, VRKey);
210
211    std::string GetHeaderEntryValue  (gdcmHeaderEntry *);
212    std::string GetHeaderEntryUnvalue(gdcmHeaderEntry *);
213
214    void SkipHeaderEntry          (gdcmHeaderEntry *);
215    void FixHeaderEntryFoundLength(gdcmHeaderEntry *, guint32);
216    bool IsHeaderEntryAnInteger   (gdcmHeaderEntry *);
217
218    guint32 FindHeaderEntryLengthOB(void);
219
220    guint16 ReadInt16(void);
221    guint32 ReadInt32(void);
222    void    SkipBytes(guint32);
223
224    void Initialise(void);
225    bool CheckSwap(void);
226    void SwitchSwapToBigEndian(void);
227    void SetMaxSizeLoadEntry(long);
228    void SetMaxSizePrintEntry(long);
229
230    // DictEntry  related utilities
231    gdcmDictEntry *GetDictEntryByName  (std::string Name);
232    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
233    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
234                                       guint16 element,
235                                       std::string vr     = "unkn",
236                                       std::string fourth = "unkn",
237                                       std::string name   = "unkn");
238    //gdcmDictEntry *NewVirtualDictEntry(gdcmHeaderEntry *); // never defined
239    
240    // HeaderEntry related utilities
241    
242    gdcmHeaderEntry *ReadNextHeaderEntry   (void);
243    gdcmHeaderEntry *NewHeaderEntryByNumber(guint16 group, 
244                                            guint16 element);
245    gdcmHeaderEntry *NewHeaderEntryByName  (std::string Name);
246    
247    // Deprecated (Not used) --> commented out
248    //gdcmHeaderEntry *NewManualHeaderEntryToPubDict(std::string NewTagName,
249    //                                               std::string VR);
250    
251    guint32 GenerateFreeTagKeyInGroup(guint16 group);
252
253 public:
254 // Accessors:
255    /// Accessor to \ref printLevel
256    void SetPrintLevel(int level) { printLevel = level; };
257
258    /// Accessor to \ref filename
259    inline std::string GetFileName(void) {return filename;}
260
261    /// Accessor to \ref filename
262    inline void SetFileName(char* fileName) {filename = fileName;}
263
264    /// Accessor to \ref gdcmParser::tagHT
265    inline TagHeaderEntryHT &GetEntry(void) { return tagHT; };
266
267    /// Accessor to \ref gdcmParser::listEntries
268    inline ListTag &GetListEntry(void) { return listEntries; };
269
270    /// 'Swap code' accessor (see \ref sw )
271    inline int GetSwapCode(void) { return sw; }
272 };
273
274 //-----------------------------------------------------------------------------
275 #endif