]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* src/gdcmDocument.[h|cxx]:
[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 gdcmValEntry;
14 class gdcmBinEntry;
15 class gdcmSeqEntry;
16
17 #include "gdcmDocEntrySet.h"
18 #include "gdcmElementSet.h"
19
20 #include <map>
21 #include <list>
22
23 //-----------------------------------------------------------------------------
24 typedef std::string VRKey;
25 typedef std::string VRAtr;
26 typedef std::map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
27
28 //-----------------------------------------------------------------------------
29 /**
30  * \brief Derived by both gdcmHeader and gdcmDicomDir
31  */
32 class GDCM_EXPORT gdcmDocument : public gdcmElementSet
33 {
34 friend class gdcmFile;
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 // the 2 following will be merged
96    virtual void PrintPubDict (std::ostream &os = std::cout);
97    virtual void PrintShaDict (std::ostream &os = std::cout);
98
99 // Dictionnaries
100    gdcmDict *GetPubDict(void);
101    gdcmDict *GetShaDict(void);
102    bool SetShaDict(gdcmDict *dict);
103    bool SetShaDict(DictKey dictName);
104
105 // Informations contained in the parser
106    virtual bool IsReadable(void);
107    bool IsGivenTransferSyntax(const std::string & SyntaxToCheck);
108    bool IsImplicitVRLittleEndianTransferSyntax(void);
109    bool IsExplicitVRLittleEndianTransferSyntax(void);
110    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
111    bool IsExplicitVRBigEndianTransferSyntax(void);
112    bool IsJPEGBaseLineProcess1TransferSyntax(void);
113    bool IsJPEGExtendedProcess2_4TransferSyntax(void);
114    bool IsJPEGExtendedProcess3_5TransferSyntax(void);
115    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void);
116    bool IsRLELossLessTransferSyntax(void);
117    bool IsJPEGLossless(void);
118    bool IsJPEG2000(void);
119    bool IsDicomV3(void);
120
121    FileType GetFileType(void);
122
123 // Read (used in gdcmFile, gdcmDicomDir)
124    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
125    bool CloseFile(void);
126
127 // Write (used in gdcmFile, gdcmDicomDir)
128    virtual bool Write(FILE *, FileType);
129    virtual void WriteEntryTagVRLength(gdcmDocEntry *tag,
130                                        FILE *_fp, FileType type);
131    virtual void WriteEntryValue(gdcmDocEntry *tag,FILE *_fp,FileType type);
132    virtual bool WriteEntry(gdcmDocEntry *tag,FILE *_fp,FileType type);
133    virtual bool WriteEntries(FILE *_fp,FileType type);
134
135    gdcmValEntry * ReplaceOrCreateByNumber(std::string Value,
136                                              guint16 Group, guint16 Elem);
137                                                                                                                         
138    gdcmBinEntry * ReplaceOrCreateByNumber(void *voidArea, int lgth,
139                                              guint16 Group, guint16 Elem);                                                                                                                      
140    bool ReplaceIfExistByNumber (char *Value, guint16 Group, guint16 Elem);
141    
142    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
143    void           LoadVLEntry             (gdcmDocEntry *entry);
144       
145 // System access
146    guint16 SwapShort(guint16);   // needed by gdcmFile
147    guint32 SwapLong(guint32);    // needed by gdcmFile
148    guint16 UnswapShort(guint16); // needed by gdcmFile
149    guint32 UnswapLong(guint32);  // needed by gdcmFile
150
151 protected:
152    // Constructor and destructor are protected to forbid end user 
153    // to instanciate from this class gdcmDocument (only gdcmHeader and
154    // gdcmDicomDir are meaningfull).
155    gdcmDocument(bool exception_on_error  = false);
156    gdcmDocument(const char *inFilename, 
157                 bool  exception_on_error = false, 
158                 bool  enable_sequences   = false,
159                 bool  ignore_shadow      = false);
160    virtual ~gdcmDocument(void);
161    
162    void gdcmDocument::Parse7FE0 (void);   
163 // Entry
164    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
165 public:
166    virtual std::string GetEntryByName    (std::string tagName);
167    virtual std::string GetEntryVRByName  (std::string tagName);
168    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
169    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
170    virtual int     GetEntryLengthByNumber(guint16 group, guint16 element);
171 protected:
172    virtual bool SetEntryByName  (std::string content, std::string tagName);
173    virtual bool SetEntryByNumber(std::string content,
174                                  guint16 group, guint16 element);
175    virtual bool SetEntryByNumber(void *content, int lgth,
176                                  guint16 group, guint16 element);                                                                                       
177    virtual bool SetEntryLengthByNumber(guint32 length,
178                                  guint16 group, guint16 element);
179
180    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
181    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
182    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
183
184    virtual void UpdateShaEntries(void);
185
186 // Header entry
187    gdcmDocEntry *GetDocEntryByNumber  (guint16 group, guint16 element); 
188    gdcmDocEntry *GetDocEntryByName    (std::string Name);
189         
190    gdcmValEntry *GetValEntryByNumber  (guint16 group, guint16 element); 
191    gdcmBinEntry *GetBinEntryByNumber  (guint16 group, guint16 element); 
192
193    void LoadDocEntrySafe(gdcmDocEntry *);
194
195 private:
196 // Read
197    long ParseDES(gdcmDocEntrySet *set, long offset, long l_max,bool delim_mode);
198    long ParseSQ(gdcmSeqEntry *seq, long offset, long l_max, bool delim_mode); 
199    
200    void LoadDocEntry      (gdcmDocEntry *);
201    void FindDocEntryLength(gdcmDocEntry *);
202    void FindDocEntryVR    (gdcmDocEntry *);
203    bool CheckDocEntryVR   (gdcmDocEntry *, VRKey);
204
205    std::string GetDocEntryValue  (gdcmDocEntry *);
206    std::string GetDocEntryUnvalue(gdcmDocEntry *);
207
208    void SkipDocEntry          (gdcmDocEntry *);
209    void SkipToNextDocEntry    (gdcmDocEntry *);
210
211    void FixDocEntryFoundLength(gdcmDocEntry *, guint32);
212    bool IsDocEntryAnInteger   (gdcmDocEntry *);
213
214    guint32 FindDocEntryLengthOB(void);
215
216    guint16 ReadInt16(void);
217    guint32 ReadInt32(void);
218    void    SkipBytes(guint32);
219    guint32 ReadTagLength(guint16, guint16);
220    guint32 ReadItemTagLength(void);
221    guint32 ReadSequenceDelimiterTagLength(void);
222
223    void Initialise(void);
224    bool CheckSwap(void);
225    void SwitchSwapToBigEndian(void);
226    void SetMaxSizeLoadEntry(long);
227    void SetMaxSizePrintEntry(long);
228
229   // DictEntry  related utilities
230    
231    gdcmDictEntry *GetDictEntryByName  (std::string Name);
232    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
233    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
234                                       guint16 element,
235                                       std::string vr     = "unkn",
236                                       std::string fourth = "unkn",
237                                       std::string name   = "unkn");
238    // DocEntry related utilities
239    gdcmDocEntry *ReadNextDocEntry   (void);
240    gdcmDocEntry *NewDocEntryByNumber(guint16 group, 
241                                      guint16 element);
242    gdcmDocEntry *NewDocEntryByName  (std::string Name);
243
244    gdcmValEntry *NewValEntryByNumber(guint16 group, 
245                                      guint16 element); 
246    gdcmBinEntry *NewBinEntryByNumber(guint16 group, 
247                                      guint16 element);                                                                                             
248    guint32 GenerateFreeTagKeyInGroup(guint16 group);
249
250 public:
251 // Accessors:
252    /// Accessor to \ref printLevel
253    void SetPrintLevel(int level) { printLevel = level; };
254
255    /// Accessor to \ref filename
256    inline std::string GetFileName(void) {return filename;}
257
258    /// Accessor to \ref filename
259    inline void SetFileName(char* fileName) {filename = fileName;}
260
261    /// Accessor to \ref gdcmElementSet::tagHT
262    inline TagDocEntryHT &GetEntry(void) { return tagHT; };
263
264    /// 'Swap code' accessor (see \ref sw )
265    inline int GetSwapCode(void) { return sw; }
266    
267    /// File pointer
268    inline FILE * GetFP(void) { return fp; }
269
270    bool operator<(gdcmDocument &document);
271
272 };
273
274 //-----------------------------------------------------------------------------
275 #endif