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