]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* src/gdcmHeader.[cxx|h]:
[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 Derived by both gdcmHeader and gdcmDicomDir
29  */
30 class GDCM_EXPORT gdcmDocument : public gdcmElementSet
31 {
32 friend class gdcmFile;
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 // the 2 following will be merged
94    virtual void PrintPubDict (std::ostream &os = std::cout);
95    virtual void PrintShaDict (std::ostream &os = std::cout);
96
97 // Dictionnaries
98    gdcmDict *GetPubDict(void);
99    gdcmDict *GetShaDict(void);
100    bool SetShaDict(gdcmDict *dict);
101    bool SetShaDict(DictKey dictName);
102
103 // Informations contained in the parser
104    virtual bool IsReadable(void);
105    bool IsImplicitVRLittleEndianTransferSyntax(void);
106    bool IsExplicitVRLittleEndianTransferSyntax(void);
107    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
108    bool IsExplicitVRBigEndianTransferSyntax(void);
109    bool IsJPEGBaseLineProcess1TransferSyntax(void);
110    bool IsJPEGExtendedProcess2_4TransferSyntax(void);
111    bool IsJPEGExtendedProcess3_5TransferSyntax(void);
112    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void);
113    bool IsRLELossLessTransferSyntax(void);
114    bool IsJPEGLossless(void);
115    bool IsJPEG2000(void);
116    bool IsDicomV3(void);
117
118    FileType GetFileType(void);
119
120 // Read (used in gdcmFile, gdcmDicomDir)
121    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
122    bool CloseFile(void);
123
124 // Write (used in gdcmFile, gdcmDicomDir)
125    virtual bool Write(FILE *, FileType);
126    virtual void WriteEntryTagVRLength(gdcmDocEntry *tag,
127                                        FILE *_fp, FileType type);
128    virtual void WriteEntryValue(gdcmDocEntry *tag,FILE *_fp,FileType type);
129    virtual bool WriteEntry(gdcmDocEntry *tag,FILE *_fp,FileType type);
130    virtual bool WriteEntries(FILE *_fp,FileType type);
131
132    gdcmDocEntry * ReplaceOrCreateByNumber(std::string Value,
133                                              guint16 Group, guint16 Elem);
134    bool ReplaceIfExistByNumber (char *Value, guint16 Group, guint16 Elem);
135    
136    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
137    void           LoadVLEntry             (gdcmDocEntry *entry);
138       
139 // System access
140    guint16 SwapShort(guint16);   // needed by gdcmFile
141    guint32 SwapLong(guint32);    // needed by gdcmFile
142    guint16 UnswapShort(guint16); // needed by gdcmFile
143    guint32 UnswapLong(guint32);  // needed by gdcmFile
144
145 protected:
146    // Constructor and destructor are protected to forbid end user 
147    // to instanciate from this class gdcmDocument (only gdcmHeader and
148    // gdcmDicomDir are meaningfull).
149    gdcmDocument(bool exception_on_error  = false);
150    gdcmDocument(const char *inFilename, 
151                 bool  exception_on_error = false, 
152                 bool  enable_sequences   = false,
153                 bool  ignore_shadow      = false);
154    virtual ~gdcmDocument(void);
155    
156    void gdcmDocument::Parse7FE0 (void);   
157 // Entry
158    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
159 public:
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 protected:
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 private:
185 // Read
186    long ParseDES(gdcmDocEntrySet *set, long offset, long l_max,bool delim_mode);
187    long ParseSQ(gdcmSeqEntry *seq, long offset, long l_max, bool delim_mode); 
188    
189    void LoadDocEntry      (gdcmDocEntry *);
190    void FindDocEntryLength(gdcmDocEntry *);
191    void FindDocEntryVR    (gdcmDocEntry *);
192    bool CheckDocEntryVR   (gdcmDocEntry *, VRKey);
193
194    std::string GetDocEntryValue  (gdcmDocEntry *);
195    std::string GetDocEntryUnvalue(gdcmDocEntry *);
196
197    void SkipDocEntry          (gdcmDocEntry *);
198    void SkipToNextDocEntry    (gdcmDocEntry *);
199
200    void FixDocEntryFoundLength(gdcmDocEntry *, guint32);
201    bool IsDocEntryAnInteger   (gdcmDocEntry *);
202
203    guint32 FindDocEntryLengthOB(void);
204
205    guint16 ReadInt16(void);
206    guint32 ReadInt32(void);
207    void    SkipBytes(guint32);
208
209    void Initialise(void);
210    bool CheckSwap(void);
211    void SwitchSwapToBigEndian(void);
212    void SetMaxSizeLoadEntry(long);
213    void SetMaxSizePrintEntry(long);
214
215   // DictEntry  related utilities
216    
217    gdcmDictEntry *GetDictEntryByName  (std::string Name);
218    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
219    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
220                                       guint16 element,
221                                       std::string vr     = "unkn",
222                                       std::string fourth = "unkn",
223                                       std::string name   = "unkn");
224    // DocEntry related utilities
225    gdcmDocEntry *ReadNextDocEntry   (void);
226    gdcmDocEntry *NewDocEntryByNumber(guint16 group, 
227                                            guint16 element);
228    gdcmDocEntry *NewDocEntryByName  (std::string Name);
229    
230    guint32 GenerateFreeTagKeyInGroup(guint16 group);
231
232 public:
233 // Accessors:
234    /// Accessor to \ref printLevel
235    void SetPrintLevel(int level) { printLevel = level; };
236
237    /// Accessor to \ref filename
238    inline std::string GetFileName(void) {return filename;}
239
240    /// Accessor to \ref filename
241    inline void SetFileName(char* fileName) {filename = fileName;}
242
243    /// Accessor to \ref gdcmElementSet::tagHT
244    inline TagDocEntryHT &GetEntry(void) { return tagHT; };
245
246    /// 'Swap code' accessor (see \ref sw )
247    inline int GetSwapCode(void) { return sw; }
248    
249    /// File pointer
250    inline FILE * GetFP(void) { return fp; }
251
252    bool operator<(gdcmDocument &document);
253
254 };
255
256 //-----------------------------------------------------------------------------
257 #endif