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