]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
Add warning about Load Mode
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2005/06/02 13:42:18 $
7   Version:   $Revision: 1.111 $
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.html 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 "gdcmVR.h"
23 #include "gdcmDict.h"
24 #include "gdcmElementSet.h"
25 #include "gdcmException.h"
26
27 #include <map>
28 #include <list>
29 #include <fstream>
30
31 #define NO_SEQ    0x0000001
32 #define NO_SHADOW 0x00000002
33
34 namespace gdcm 
35 {
36 class ValEntry;
37 class BinEntry;
38 class SeqEntry;
39 class Dict;
40
41 //-----------------------------------------------------------------------------
42 /**
43  * \brief Derived by both gdcm::File and gdcm::DicomDir
44  */
45 class GDCM_EXPORT Document : public ElementSet
46 {
47 public:
48
49 typedef std::list<Element> ListElements;
50
51 // Loading
52    virtual bool Load( std::string const &filename ); 
53
54 // Dictionaries
55    Dict *GetPubDict();
56    Dict *GetShaDict();
57    bool SetShaDict(Dict *dict);
58    bool SetShaDict(DictKey const &dictName);
59
60 // Informations contained in the gdcm::Document
61    virtual bool IsReadable();
62    bool IsDicomV3();
63    bool IsPapyrus();
64    FileType GetFileType();
65    std::string GetTransferSyntax();
66    /// Return the Transfer Syntax as a string
67    std::string GetTransferSyntaxName();
68
69 // Swap code
70    /// 'Swap code' accessor (see \ref SwapCode )
71    int GetSwapCode() { return SwapCode; }
72    // System access (meaning endian related !?)
73    uint16_t SwapShort(uint16_t);
74    uint32_t SwapLong(uint32_t);
75    /// \brief  Unswaps back the bytes of 2-bytes long integer 
76    ///         so they agree with the processor order.
77    uint16_t UnswapShort(uint16_t a) { return SwapShort(a);}
78    /// \brief  Unswaps back the bytes of 4-byte long integer 
79    ///         so they agree with the processor order.
80    uint32_t UnswapLong(uint32_t a) { return SwapLong(a);}
81    
82 // File I/O
83    /// Accessor to \ref Filename
84    const std::string &GetFileName() const { return Filename; }
85    /// Accessor to \ref Filename
86    void SetFileName(std::string const &fileName) { Filename = fileName; }
87
88    std::ifstream *OpenFile();
89    bool CloseFile();
90    void WriteContent( std::ofstream *fp, FileType type );
91
92 // Content entries
93    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
94    virtual void LoadEntryBinArea(BinEntry *entry);
95
96    void LoadDocEntrySafe(DocEntry *entry);
97  
98 // Ordering of Documents
99    bool operator<(Document &document);
100
101 /**
102  * \brief \brief Sets the LoadMode as a boolean string. 
103  *        NO_SEQ, NO_SHADOW, ... (nothing more, right now)
104  *        WARNING : before using NO_SHADOW, be sure *all* your files
105  *        contain accurate values in the 0x0000 element 
106  *        of each Shadow Group. The paser will fail if the size is wrong !
107  * @param   mode Load mode to be used    
108  */
109    void SetLoadMode (int mode) { LoadMode = mode; }
110
111 protected:
112 // Methods
113    // Constructor and destructor are protected to forbid end user 
114    // to instanciate from this class Document (only gdcm::File and
115    // gdcm::DicomDir are meaningfull).
116    Document();
117    Document( std::string const &filename );
118    virtual ~Document();
119    
120    uint16_t ReadInt16() throw ( FormatError );
121    uint32_t ReadInt32() throw ( FormatError );
122    void     SkipBytes(uint32_t);
123    int ComputeGroup0002Length( FileType filetype );
124
125 // Variables
126    /// Refering underlying filename.
127    std::string Filename;
128
129    /// \brief Swap code gives an information on the byte order of a 
130    ///  supposed to be an int32, as it's read on disc 
131    /// (depending on the image Transfer Syntax *and* on the processor endianess)
132    /// as opposed as it should in memory to be dealt as an int32.
133    /// For instance :
134    /// - a 'Little Endian' image, read with a little endian processor
135    /// will have a SwapCode= 1234 (the order is OK; nothing to do)
136    /// - a 'Little Endian' image, read with a big endian procesor
137    /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
138    /// swapped)
139    /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
140    /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
141    int SwapCode;
142
143    ///\brief whether we already parsed group 0002 (Meta Elements)
144    bool Group0002Parsed;
145
146    ///\brief whether file has a DCM Preamble
147    bool HasDCMPreamble;
148
149    /// File Pointer, opened during Document parsing.
150    std::ifstream *Fp;
151
152    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
153    FileType Filetype;  
154
155    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
156    static const unsigned int HEADER_LENGTH_TO_READ; 
157    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
158    /// are NOT loaded.
159    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
160
161    /// List of elements to Anonymize
162    ListElements AnonymizeList;
163
164    /// \brief Bit string integer (each one considered as a boolean)
165    ///        Bit 0 : Skip Sequences,    if possible
166    ///        Bit 1 : Skip Shadow Groups if possible
167    ///        Some more to add
168    int LoadMode;
169    
170    /// Whether the gdcm::Document is already parsed/loaded
171    /// - False from the creation of the gdcm::Document untill 
172    ///   gdcm::Document:Load()
173    bool IsDocumentAlreadyLoaded;
174
175 private:
176 // Methods
177    void Initialize();
178
179    // Read
180    void ParseDES(DocEntrySet *set, long offset, long l_max, bool delim_mode);
181    void ParseSQ (SeqEntry *seq,    long offset, long l_max, bool delim_mode);
182
183    void LoadDocEntry         (DocEntry *e);
184    void FindDocEntryLength   (DocEntry *e) throw ( FormatError );
185    uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
186    std::string FindDocEntryVR();
187    bool CheckDocEntryVR      (VRKey k);
188
189    std::string GetDocEntryValue  (DocEntry *entry);
190    std::string GetDocEntryUnvalue(DocEntry *entry);
191
192    void SkipDocEntry          (DocEntry *entry);
193    void SkipToNextDocEntry    (DocEntry *entry);
194
195    void FixDocEntryFoundLength(DocEntry *entry, uint32_t l);
196    bool IsDocEntryAnInteger   (DocEntry *entry);
197
198    bool CheckSwap();
199    void SwitchByteSwapCode();
200    void SetMaxSizeLoadEntry(long);
201
202    // DocEntry related utilities
203    DocEntry *ReadNextDocEntry();
204
205    void HandleBrokenEndian  (uint16_t &group, uint16_t &elem);
206    void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
207
208 // Variables
209    /// Public dictionary used to parse this header
210    Dict *RefPubDict;
211    /// \brief Optional "shadow dictionary" (private elements) used to parse
212    /// this header
213    Dict *RefShaDict;
214
215    /// \brief Size threshold above which an element value will NOT be loaded
216    /// in memory (to avoid loading the image/volume itself). By default,
217    /// this upper bound is fixed to 1024 bytes (which might look reasonable
218    /// when one considers the definition of the various VR contents).
219    uint32_t MaxSizeLoadEntry;
220    
221 //  uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
222 //  void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
223 //                                  DocEntrySet *set );
224
225 };
226
227 } // end namespace gdcm
228
229 //-----------------------------------------------------------------------------
230 #endif