]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
Sub-minor fixes on std inclusion... JPR + Frog
[gdcm.git] / src / gdcmDocument.h
1 // gdcmDocument.h
2 //-----------------------------------------------------------------------------
3 #ifndef GDCMDOCUMENT_H
4 #define GDCMDOCUMENT_H
5
6 #include "gdcmCommon.h"
7 #include "gdcmVR.h"
8 #include "gdcmTS.h"
9 #include "gdcmException.h"
10 #include "gdcmDictSet.h"
11 #include "gdcmDocEntry.h"
12
13 class gdcmSeqEntry;
14
15 #include "gdcmDocEntrySet.h"
16 #include "gdcmElementSet.h"
17
18 #include <map>
19 #include <list>
20
21 //-----------------------------------------------------------------------------
22 typedef std::string VRKey;
23 typedef std::string VRAtr;
24 typedef std::map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
25
26 //-----------------------------------------------------------------------------
27 /**
28  * \brief used by both gdcmHeader and gdcmDicomDir
29  */
30 class GDCM_EXPORT gdcmDocument : public gdcmElementSet
31 {
32 private:
33    /// Public dictionary used to parse this header
34    gdcmDict *RefPubDict;
35    
36    /// \brief Optional "shadow dictionary" (private elements) used to parse
37    /// this header
38    gdcmDict *RefShaDict;
39    
40    /// \brief Equals =1 if user wants to skip shadow groups while parsing
41    /// (to save space)
42    int ignoreShadow;
43
44    /// \brief Size threshold above which an element value will NOT be loaded
45    /// in memory (to avoid loading the image/volume itself). By default,
46    /// this upper bound is fixed to 1024 bytes (which might look reasonable
47    /// when one considers the definition of the various VR contents).
48    guint32 MaxSizeLoadEntry;
49    
50    /// \brief Size threshold above which an element value will NOT be *printed*
51    /// in order no to polute the screen output. By default, this upper bound
52    /// is fixed to 64 bytes.
53    guint32 MaxSizePrintEntry;   
54
55 protected:
56    /// Refering underlying filename.
57    std::string filename;
58
59    /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
60    /// Bad Little Endian) according to the processor Endianity and
61    /// what is written on disc.
62    int sw;
63
64    /// File Pointer, opened during Header parsing.
65    FILE *fp;
66
67    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
68    FileType filetype;  
69
70    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
71    static const unsigned int HEADER_LENGTH_TO_READ; 
72
73    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
74    /// are NOT loaded.
75    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
76
77    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
78    /// are NOT printed.
79    /// \todo Currently not used since collides with #define in
80    ///       \ref gdcmDocEntry.cxx. See also
81    ///       \ref gdcmDocument::SetMaxSizePrintEntry()
82    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
83
84    /// Will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
85    int enableSequences;
86
87    /// \brief Amount of printed details for each Header Entry (Dicom Element):
88    /// 0 : stands for the least detail level.
89    int printLevel;
90    
91 public:
92    
93
94 // Print
95   // Canonical Printing method (see also gdcmDocument::SetPrintLevel)
96   // virtual void Print        (std::ostream &os = std::cout);    
97  //     {PrintEntry(os);};
98  
99  // no more Print method for gdcmDocument (inherits from gdcmElementSet
100  // virtual void PrintEntry      (std::ostream &os = std::cout)
101  //               { return Print(os);};
102
103    // the 2 following will be merged
104    virtual void PrintPubDict (std::ostream &os = std::cout);
105    virtual void PrintShaDict (std::ostream &os = std::cout);
106
107 // Dictionnaries
108    gdcmDict *GetPubDict(void);
109    gdcmDict *GetShaDict(void);
110    bool SetShaDict(gdcmDict *dict);
111    bool SetShaDict(DictKey dictName);
112
113 // Informations contained in the parser
114    virtual bool IsReadable(void);
115    bool IsImplicitVRLittleEndianTransferSyntax(void);
116    bool IsExplicitVRLittleEndianTransferSyntax(void);
117    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
118    bool IsExplicitVRBigEndianTransferSyntax(void);
119    FileType GetFileType(void);
120
121 // Read (used in gdcmFile, gdcmDicomDir)
122    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
123    bool CloseFile(void);
124
125 // Write (used in gdcmFile, gdcmDicomDir)
126    virtual bool Write(FILE *, FileType);
127    virtual void WriteEntryTagVRLength(gdcmDocEntry *tag,
128                                        FILE *_fp, FileType type);
129    virtual void WriteEntryValue(gdcmDocEntry *tag,FILE *_fp,FileType type);
130    virtual bool WriteEntry(gdcmDocEntry *tag,FILE *_fp,FileType type);
131    virtual bool WriteEntries(FILE *_fp,FileType type);
132
133    gdcmDocEntry * ReplaceOrCreateByNumber(std::string Value,
134                                              guint16 Group, guint16 Elem);
135    bool ReplaceIfExistByNumber (char *Value, guint16 Group, guint16 Elem);
136    
137    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
138    void           LoadVLEntry             (gdcmDocEntry *entry);
139       
140 // System access
141    guint16 SwapShort(guint16);   // needed by gdcmFile
142    guint32 SwapLong(guint32);    // needed by gdcmFile
143    guint16 UnswapShort(guint16); // needed by gdcmFile
144    guint32 UnswapLong(guint32);  // needed by gdcmFile
145
146 protected:
147    // Constructor and destructor are protected to forbid end user 
148    // to instanciate from this class gdcmDocument (only gdcmHeader and
149    // gdcmDicomDir are meaningfull).
150    gdcmDocument(bool exception_on_error  = false);
151    gdcmDocument(const char *inFilename, 
152               bool  exception_on_error = false, 
153               bool  enable_sequences   = false,
154               bool  ignore_shadow      = false);
155    virtual ~gdcmDocument(void);
156    
157    void gdcmDocument::Parse7FE0 (void);   
158 // Entry
159    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
160    virtual std::string GetEntryByName    (std::string tagName);
161    virtual std::string GetEntryVRByName  (std::string tagName);
162    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
163    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
164    virtual int     GetEntryLengthByNumber(guint16 group, guint16 element);
165
166    virtual bool SetEntryByName  (std::string content, std::string tagName);
167    virtual bool SetEntryByNumber(std::string content,
168                                  guint16 group, guint16 element);
169    virtual bool SetEntryLengthByNumber(guint32 length,
170                                  guint16 group, guint16 element);
171
172    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
173    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
174    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
175
176    virtual void UpdateShaEntries(void);
177
178 // Header entry
179    gdcmDocEntry *GetDocEntryByNumber  (guint16 group, guint16 element); 
180    gdcmDocEntry *GetDocEntryByName    (std::string Name);
181
182    void LoadDocEntrySafe(gdcmDocEntry *);
183
184    // Probabely useless
185    //void UpdateGroupLength(bool SkipSequence = false,
186    //                       FileType type = ImplicitVR);
187
188    //void AddDocEntry       (gdcmDocEntry *);
189    
190       
191 private:
192    // Read
193  //bool LoadHeaderEntries(bool exception_on_error = false) throw(gdcmFormatError);
194    // remplacé par ParseDES.
195    // What about exception_on_error ?
196    
197    long ParseDES(gdcmDocEntrySet *set, long offset, long l_max, bool delim_mode);
198   // long ParseSQ(gdcmDocEntrySet *set, long offset, long l_max, bool delim_mode);
199   long ParseSQ(gdcmSeqEntry *seq, long offset, long l_max, bool delim_mode); 
200    
201    void LoadDocEntry      (gdcmDocEntry *);
202    void FindDocEntryLength(gdcmDocEntry *);
203    void FindDocEntryVR    (gdcmDocEntry *);
204    bool CheckDocEntryVR   (gdcmDocEntry *, VRKey);
205
206    std::string GetDocEntryValue  (gdcmDocEntry *);
207    std::string GetDocEntryUnvalue(gdcmDocEntry *);
208
209    void SkipDocEntry          (gdcmDocEntry *);
210    void SkipToNextDocEntry    (gdcmDocEntry *);
211
212    void FixDocEntryFoundLength(gdcmDocEntry *, guint32);
213    bool IsDocEntryAnInteger   (gdcmDocEntry *);
214
215    guint32 FindDocEntryLengthOB(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    
229    gdcmDictEntry *GetDictEntryByName  (std::string Name);
230    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
231    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
232                                       guint16 element,
233                                       std::string vr     = "unkn",
234                                       std::string fourth = "unkn",
235                                       std::string name   = "unkn");
236    //gdcmDictEntry *NewVirtualDictEntry(gdcmDocEntry *); // never defined 
237    
238    // DocEntry related utilities
239    
240    gdcmDocEntry *ReadNextDocEntry   (void);
241    gdcmDocEntry *NewDocEntryByNumber(guint16 group, 
242                                            guint16 element);
243    gdcmDocEntry *NewDocEntryByName  (std::string Name);
244    
245    // Deprecated (Not used) --> commented out
246    //gdcmDocEntry *NewManualDocEntryToPubDict(std::string NewTagName,
247    //                                               std::string VR);
248    
249    guint32 GenerateFreeTagKeyInGroup(guint16 group);
250
251 public:
252 // Accessors:
253    /// Accessor to \ref printLevel
254    void SetPrintLevel(int level) { printLevel = level; };
255
256    /// Accessor to \ref filename
257    inline std::string GetFileName(void) {return filename;}
258
259    /// Accessor to \ref filename
260    inline void SetFileName(char* fileName) {filename = fileName;}
261
262    /// Accessor to \ref gdcmElementSet::tagHT
263    inline TagDocEntryHT &GetEntry(void) { return tagHT; };
264
265    /// Accessor to \ref gdcmDocument::listEntries
266    //inline ListTag &GetListEntry(void) { return listEntries; };
267
268    /// 'Swap code' accessor (see \ref sw )
269    inline int GetSwapCode(void) { return sw; }
270    
271    /// File pointer
272    inline FILE * GetFP(void) { return fp; }
273
274 };
275
276 //-----------------------------------------------------------------------------
277 #endif