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