]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* Rename the NO_SEQ, NO_SHADOW, NO_SHADOWSEQ to
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2005/08/30 14:40:33 $
7   Version:   $Revision: 1.121 $
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 namespace gdcm 
32 {
33 class ValEntry;
34 class BinEntry;
35 class SeqEntry;
36 class Dict;
37
38 //-----------------------------------------------------------------------------
39 /**
40  * \brief Derived by both gdcm::File and gdcm::DicomDir
41  */
42 class GDCM_EXPORT Document : public ElementSet
43 {
44 public:
45
46 typedef std::list<Element> ListElements;
47
48 // Loading
49    //Deprecated : use SetFileName() + Load()
50    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    virtual bool IsReadable();
61    bool IsDicomV3();
62    bool IsPapyrus();
63    FileType GetFileType();
64    std::string GetTransferSyntax();
65    /// Return the Transfer Syntax as a string
66    std::string GetTransferSyntaxName();
67
68 // Swap code
69    /// 'Swap code' accessor (see \ref SwapCode )
70    int GetSwapCode() { return SwapCode; }
71    // System access (meaning endian related !?)
72    uint16_t SwapShort(uint16_t);
73    uint32_t SwapLong(uint32_t);
74    /// \brief  Unswaps back the bytes of 2-bytes long integer 
75    ///         so they agree with the processor order.
76    uint16_t UnswapShort(uint16_t a) { return SwapShort(a);}
77    /// \brief  Unswaps back the bytes of 4-byte long integer 
78    ///         so they agree with the processor order.
79    uint32_t UnswapLong(uint32_t a) { return SwapLong(a);}
80    
81 // File I/O
82    /// Accessor to \ref Filename
83    const std::string &GetFileName() const { return Filename; }
84    /// Accessor to \ref Filename
85    virtual void SetFileName(std::string const &fileName) 
86                    { if (Filename != fileName)
87                         Filename = fileName, IsDocumentModified = true; }
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    void AddForceLoadElement(uint16_t group, uint16_t elem);
99  
100 // Ordering of Documents
101    bool operator<(Document &document);
102
103 /**
104  * \brief Sets the LoadMode as a boolean string. 
105  *        LD_NOSEQ, LD_NOSHADOW, LD_NOSHADOWSEQ
106  ... (nothing more, right now)
107  *        WARNING : before using NO_SHADOW, be sure *all* your files
108  *        contain accurate values in the 0x0000 element (if any) 
109  *        of *each* Shadow Group. The parser will fail if the size is wrong !
110  * @param   mode Load mode to be used    
111  */
112    void SetLoadMode (int mode) { if (LoadMode != mode) 
113                                      LoadMode=mode, IsDocumentModified = true; }
114
115 protected:
116 // Methods
117    // Constructor and destructor are protected to forbid end user 
118    // to instanciate from this class Document (only gdcm::File and
119    // gdcm::DicomDir are meaningfull).
120    Document();
121    Document( std::string const &filename );
122    virtual ~Document();
123    
124    uint16_t ReadInt16() throw ( FormatError );
125    uint32_t ReadInt32() throw ( FormatError );
126    void     SkipBytes(uint32_t);
127    int ComputeGroup0002Length( FileType filetype );
128
129 // Variables
130    /// Refering underlying filename.
131    std::string Filename;
132
133    /// \brief Swap code gives an information on the byte order of a 
134    ///  supposed to be an int32, as it's read on disc 
135    /// (depending on the image Transfer Syntax *and* on the processor endianess)
136    /// as opposed as it should in memory to be dealt as an int32.
137    /// For instance :
138    /// - a 'Little Endian' image, read with a little endian processor
139    /// will have a SwapCode= 1234 (the order is OK; nothing to do)
140    /// - a 'Little Endian' image, read with a big endian procesor
141    /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
142    /// swapped)
143    /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
144    /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
145    int SwapCode;
146
147    ///\brief whether we already parsed group 0002 (Meta Elements)
148    bool Group0002Parsed;
149
150    ///\brief whether file has a DCM Preamble
151    bool HasDCMPreamble;
152
153    /// File Pointer, opened during Document parsing.
154    std::ifstream *Fp;
155
156    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
157    FileType Filetype;  
158
159    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
160    static const unsigned int HEADER_LENGTH_TO_READ; 
161    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
162    /// are NOT loaded.
163    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
164
165    /// User supplied list of elements to Anonymize
166    ListElements UserAnonymizeList;
167
168    /// User supplied list of elements to force Load
169    ListElements UserForceLoadList;
170
171    /// \brief Bit string integer (each one considered as a boolean)
172    ///        Bit 0 : Skip Sequences,    if possible
173    ///        Bit 1 : Skip Shadow Groups if possible
174    ///        Probabely, some more to add
175    int LoadMode;
176    
177    /// \brief Whether the gdcm::Document is already parsed/loaded :
178    /// False from the creation of the gdcm::Document untill 
179    ///   gdcm::Document:Load()
180    bool IsDocumentAlreadyLoaded; // FIXME : probabely useless now
181
182    /// Whether the gdcm::Document was modified since the last Load()
183    bool IsDocumentModified;
184
185 private:
186 // Methods
187    void Initialize();
188    bool DoTheLoadingDocumentJob();
189    // Read
190    void ParseDES(DocEntrySet *set, long offset, long l_max, bool delim_mode);
191    void ParseSQ (SeqEntry *seq,    long offset, long l_max, bool delim_mode);
192
193    void LoadDocEntry         (DocEntry *e, bool forceLoad = false);
194    void FindDocEntryLength   (DocEntry *e) throw ( FormatError );
195    uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
196    std::string FindDocEntryVR();
197    bool CheckDocEntryVR      (VRKey k);
198
199    std::string GetDocEntryValue  (DocEntry *entry);
200    std::string GetDocEntryUnvalue(DocEntry *entry);
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 //  uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
233 //  void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
234 //                                  DocEntrySet *set );
235
236 };
237
238 } // end namespace gdcm
239
240 //-----------------------------------------------------------------------------
241 #endif