]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* gdcmPython/CMakeLists.txt: SWIG_FLAGS doesn't declare includeall
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2004/08/02 14:06:58 $
7   Version:   $Revision: 1.29 $
8  
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.htm for details.
12  
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16  
17 =========================================================================*/
18
19 #ifndef GDCMDOCUMENT_H
20 #define GDCMDOCUMENT_H
21
22 #include "gdcmCommon.h"
23 #include "gdcmVR.h"
24 #include "gdcmTS.h"
25 #include "gdcmException.h"
26 #include "gdcmDictSet.h"
27 #include "gdcmDocEntry.h"
28
29 class gdcmValEntry;
30 class gdcmBinEntry;
31 class gdcmSeqEntry;
32
33 #include "gdcmDocEntrySet.h"
34 #include "gdcmElementSet.h"
35
36 #include <map>
37 #include <list>
38
39 //-----------------------------------------------------------------------------
40 /**
41  * \brief Derived by both gdcmHeader and gdcmDicomDir
42  */
43 class GDCM_EXPORT gdcmDocument : public gdcmElementSet
44 {
45 friend class gdcmFile;
46 private:
47    /// Public dictionary used to parse this header
48    gdcmDict *RefPubDict;
49    
50    /// \brief Optional "shadow dictionary" (private elements) used to parse
51    /// this header
52    gdcmDict *RefShaDict;
53    
54    /// \brief Equals =1 if user wants to skip shadow groups while parsing
55    /// (to save space)
56    int IgnoreShadow;
57
58    /// \brief Size threshold above which an element value will NOT be loaded
59    /// in memory (to avoid loading the image/volume itself). By default,
60    /// this upper bound is fixed to 1024 bytes (which might look reasonable
61    /// when one considers the definition of the various VR contents).
62    uint32_t MaxSizeLoadEntry;
63    
64    /// \brief Size threshold above which an element value will NOT be *printed*
65    /// in order no to polute the screen output. By default, this upper bound
66    /// is fixed to 64 bytes.
67    uint32_t MaxSizePrintEntry;   
68
69 protected:
70    /// Refering underlying filename.
71    std::string Filename;
72
73    /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
74    /// Bad Little Endian) according to the processor Endianity and
75    /// what is written on disc.
76    int SwapCode;
77
78    /// File Pointer, opened during Header parsing.
79    FILE *Fp;
80
81    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
82    FileType Filetype;  
83
84    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
85    static const unsigned int HEADER_LENGTH_TO_READ; 
86
87    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
88    /// are NOT loaded.
89    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
90
91    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
92    /// are NOT printed.
93    /// \todo Currently not used since collides with #define in
94    ///       \ref gdcmDocEntry.cxx. See also
95    ///       \ref gdcmDocument::SetMaxSizePrintEntry()
96    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
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 // 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();
112    gdcmDict *GetShaDict();
113    bool SetShaDict(gdcmDict *dict);
114    bool SetShaDict(DictKey const & dictName);
115
116 // Informations contained in the parser
117    virtual bool IsReadable();
118    bool IsGivenTransferSyntax(std::string const & syntaxToCheck);
119    bool IsImplicitVRLittleEndianTransferSyntax();
120    bool IsExplicitVRLittleEndianTransferSyntax();
121    bool IsDeflatedExplicitVRLittleEndianTransferSyntax();
122    bool IsExplicitVRBigEndianTransferSyntax();
123    bool IsJPEGBaseLineProcess1TransferSyntax();
124    bool IsJPEGExtendedProcess2_4TransferSyntax();
125    bool IsJPEGExtendedProcess3_5TransferSyntax();
126    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax();
127    bool IsRLELossLessTransferSyntax();
128    bool IsJPEGLossless();
129    bool IsJPEG2000();
130    bool IsDicomV3();
131
132    FileType GetFileType();
133
134    FILE* OpenFile(bool exception_on_error = false) throw( gdcmFileError );
135    bool CloseFile();
136
137    void Write(FILE* fp, FileType type);
138
139    gdcmValEntry* ReplaceOrCreateByNumber(std::string const & value,
140                                          uint16_t group, uint16_t elem);
141
142    gdcmBinEntry* ReplaceOrCreateByNumber(void *voidArea, int lgth,
143                                          uint16_t group, uint16_t elem);
144    bool ReplaceIfExistByNumber (std::string const & value, uint16_t group, uint16_t elem);
145    
146    virtual void* LoadEntryVoidArea(uint16_t group, uint16_t elem);
147    virtual void* LoadEntryVoidArea(gdcmBinEntry* entry);
148       
149    // System access
150    uint16_t SwapShort(uint16_t);   // needed by gdcmFile
151    uint32_t SwapLong(uint32_t);    // needed by gdcmFile
152    uint16_t UnswapShort(uint16_t); // needed by gdcmFile
153    uint32_t UnswapLong(uint32_t);  // needed by gdcmFile
154
155 protected:
156    // Constructor and destructor are protected to forbid end user 
157    // to instanciate from this class gdcmDocument (only gdcmHeader and
158    // gdcmDicomDir are meaningfull).
159    gdcmDocument(bool exception_on_error  = false);
160    gdcmDocument(std::string const & filename, 
161                 bool  exception_on_error = false);
162    virtual ~gdcmDocument();
163    
164    void Parse7FE0 ();   
165    // Entry
166    int CheckIfEntryExistByNumber(uint16_t group, uint16_t elem ); // int !
167 public:
168    virtual std::string GetEntryByName    (TagName const & tagName);
169    virtual std::string GetEntryVRByName  (TagName const & tagName);
170    virtual std::string GetEntryByNumber  (uint16_t group, uint16_t elem);
171    virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
172    virtual int     GetEntryLengthByNumber(uint16_t group, uint16_t elem);
173 protected:
174    virtual bool SetEntryByName  (std::string const & content, 
175                                  std::string const & tagName);
176    virtual bool SetEntryByNumber(std::string const & content,
177                                  uint16_t group, uint16_t element);
178    virtual bool SetEntryByNumber(void *content, int lgth,
179                                  uint16_t group, uint16_t element);
180    virtual bool SetEntryLengthByNumber(uint32_t length,
181                                        uint16_t group, uint16_t element);
182
183    virtual size_t GetEntryOffsetByNumber(uint16_t group, uint16_t elem);
184    virtual void* GetEntryVoidAreaByNumber(uint16_t group, uint16_t elem);   
185    virtual bool  SetEntryVoidAreaByNumber(void* a, uint16_t group,
186                                                    uint16_t elem);
187
188    virtual void UpdateShaEntries();
189
190    // Header entry
191    gdcmDocEntry* GetDocEntryByNumber(uint16_t group, uint16_t element); 
192    gdcmDocEntry* GetDocEntryByName  (std::string const & tagName);
193
194    gdcmValEntry* GetValEntryByNumber(uint16_t group, uint16_t element); 
195    gdcmBinEntry* GetBinEntryByNumber(uint16_t group, uint16_t element); 
196
197    void LoadDocEntrySafe(gdcmDocEntry* entry);
198
199 private:
200    // Read
201    long ParseDES(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 *) throw ( gdcmFormatError );
206    void FindDocEntryVR    (gdcmDocEntry *);
207    bool CheckDocEntryVR   (gdcmDocEntry *, gdcmVRKey);
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 *, uint32_t);
216    bool IsDocEntryAnInteger   (gdcmDocEntry *);
217
218    uint32_t FindDocEntryLengthOB() throw( gdcmFormatUnexpected );
219
220    uint16_t ReadInt16() throw ( gdcmFormatError );
221    uint32_t ReadInt32() throw ( gdcmFormatError );
222    void     SkipBytes(uint32_t);
223    bool     ReadTag(uint16_t, uint16_t);
224    uint32_t ReadTagLength(uint16_t, uint16_t);
225
226    void Initialise();
227    bool CheckSwap();
228    void SwitchSwapToBigEndian();
229    void SetMaxSizeLoadEntry(long);
230    void SetMaxSizePrintEntry(long);
231
232    // DocEntry related utilities
233    gdcmDocEntry* ReadNextDocEntry();
234
235    uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
236
237 public:
238 // Accessors:
239    /// Accessor to \ref printLevel
240    void SetPrintLevel(int level) { PrintLevel = level; }
241
242    /// Accessor to \ref Filename
243    const std::string &GetFileName() { return Filename; }
244
245    /// Accessor to \ref Filename
246    void SetFileName(std::string const & fileName) { Filename = fileName; }
247
248    /// 'Swap code' accessor (see \ref sw )
249    int GetSwapCode() { return SwapCode; }
250    
251    /// File pointer
252    FILE * GetFP() { return Fp; }
253
254    bool operator<(gdcmDocument &document);
255
256 };
257
258 //-----------------------------------------------------------------------------
259 #endif