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