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