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