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