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