]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
remove some now useless stuff
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2006/02/09 10:48:05 $
7   Version:   $Revision: 1.139 $
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 #include "gdcmCommandManager.h"
28
29 #include <map>
30 #include <list>
31 #include <fstream>
32
33 namespace gdcm 
34 {
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    gdcmTypeMacro(Document);
45
46 public:
47    typedef std::list<DicomElement> ListElements;
48
49 // Loading
50    //Deprecated : use SetFileName() + Load()
51    //GDCM_LEGACY(  virtual bool Load( std::string const &filename )  ); 
52    //virtual bool Load( std::string const &filename );
53    virtual bool Load( ); 
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    bool IsParsable();
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    
75 // File I/O
76    /// Accessor to \ref Filename
77    const std::string &GetFileName() const { return Filename; }
78    /// Accessor to \ref Filename
79    virtual void SetFileName(std::string const &fileName) 
80                    { if (Filename != fileName)
81                         Filename = fileName, IsDocumentModified = true; }
82
83    std::ifstream *OpenFile();
84    bool CloseFile();
85    void WriteContent( std::ofstream *fp, FileType type );
86
87 // Data entries
88    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
89    virtual void LoadEntryBinArea(DataEntry *entry);
90
91    //void LoadDocEntrySafe(DocEntry *entry);
92    void AddForceLoadElement(uint16_t group, uint16_t elem);
93  
94 // Ordering of Documents
95    bool operator<(Document &document);
96
97 /**
98  * \brief Sets the LoadMode as a boolean string. 
99  *        LD_NOSEQ, LD_NOSHADOW, LD_NOSHADOWSEQ
100  ... (nothing more, right now)
101  *        WARNING : before using NO_SHADOW, be sure *all* your files
102  *        contain accurate values in the 0x0000 element (if any) 
103  *        of *each* Shadow Group. The parser will fail if the size is wrong !
104  * @param   mode Load mode to be used    
105  */
106    void SetLoadMode (int mode) { if (LoadMode != mode) 
107                                      LoadMode=mode, IsDocumentModified = true; }
108
109 protected:
110 // Methods
111    // Constructor and destructor are protected to forbid end user 
112    // to instanciate from this class Document (only gdcm::File and
113    // gdcm::DicomDir are meaningfull).
114    Document();
115    virtual ~Document();
116
117    virtual void CallStartMethod();
118    virtual void CallProgressMethod();
119    virtual void CallEndMethod();
120       
121    uint16_t ReadInt16() throw ( FormatError );
122    uint32_t ReadInt32() throw ( FormatError );
123    
124    /// \brief skips bytes inside the source file
125    void     SkipBytes(uint32_t nBytes) { Fp->seekg((long)nBytes, std::ios::cur);} 
126    int ComputeGroup0002Length( );
127
128 // Variables
129    /// Refering underlying filename.
130    std::string Filename;
131
132    /// \brief Swap code gives an information on the byte order of a 
133    ///  supposed to be an int32, as it's read on disc 
134    /// (depending on the image Transfer Syntax *and* on the processor endianess)
135    /// as opposed as it should in memory to be dealt as an int32.
136    /// For instance :
137    /// - a 'Little Endian' image, read with a little endian processor
138    /// will have a SwapCode= 1234 (the order is OK; nothing to do)
139    /// - a 'Little Endian' image, read with a big endian procesor
140    /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
141    /// swapped)
142    /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
143    /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
144    int SwapCode;
145
146    ///\brief whether we already parsed group 0002 (Meta Elements)
147    bool Group0002Parsed;
148
149    ///\brief whether file has a DCM Preamble
150    bool HasDCMPreamble;
151
152    /// File Pointer, opened during Document parsing.
153    std::ifstream *Fp;
154
155    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
156    FileType Filetype;  
157
158    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
159    static const unsigned int HEADER_LENGTH_TO_READ; 
160    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
161    /// are NOT loaded.
162    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
163
164    /// User supplied list of elements to Anonymize
165    ListElements UserAnonymizeList;
166
167    /// User supplied list of elements to force Load
168    ListElements UserForceLoadList;
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      
189       // System access (meaning endian related !?)
190    uint16_t SwapShort(uint16_t);
191    uint32_t SwapLong(uint32_t);
192    double SwapDouble(double);
193    /// \brief  Unswaps back the bytes of 2-bytes long integer 
194    ///         so they agree with the processor order.
195    uint16_t UnswapShort(uint16_t a) { return SwapShort(a);}
196    /// \brief  Unswaps back the bytes of 4-byte long integer 
197    ///         so they agree with the processor order.
198    uint32_t UnswapLong(uint32_t a) { return SwapLong(a);}
199    
200    // Read
201    void ParseDES(DocEntrySet *set, long offset, long l_max, bool delim_mode);
202    void ParseSQ (SeqEntry *seq,    long offset, long l_max, bool delim_mode);
203
204    void LoadDocEntry         (DocEntry *e, bool forceLoad = false);
205    void FindDocEntryLength   (DocEntry *e) throw ( FormatError );
206    uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
207    VRKey FindDocEntryVR();
208    bool CheckDocEntryVR      (const VRKey &k);
209
210    void SkipDocEntry          (DocEntry *entry);
211    void SkipToNextDocEntry    (DocEntry *entry);
212
213    void FixDocEntryFoundLength(DocEntry *entry, uint32_t l);
214    bool IsDocEntryAnInteger   (DocEntry *entry);
215
216    bool CheckSwap();
217    void SwitchByteSwapCode();
218    void SetMaxSizeLoadEntry(long);
219
220    // DocEntry related utilities
221    DocEntry *ReadNextDocEntry();
222
223    void HandleBrokenEndian  (uint16_t &group, uint16_t &elem);
224    void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
225    DocEntry *Backtrack(DocEntry *docEntry);
226
227 // Variables
228 protected:
229    /// value of the ??? for any progress bar
230    float Progress;
231    mutable bool Abort;
232    
233    /// Public dictionary used to parse this header
234    Dict *RefPubDict;
235    /// \brief Optional "shadow dictionary" (private elements) used to parse
236    /// this header
237    Dict *RefShaDict;
238
239    /// \brief Size threshold above which an element value will NOT be loaded
240    /// in memory (to avoid loading the image/volume itself). By default,
241    /// this upper bound is fixed to 1024 bytes (which might look reasonable
242    /// when one considers the definition of the various VR contents).
243    uint32_t MaxSizeLoadEntry;
244
245    /// \brief to allow any inner method to know current tag Group number 
246    uint16_t CurrentGroup;
247    /// \brief to allow any inner method to know current tag Element number 
248    uint16_t CurrentElem; 
249      
250 //  uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
251 //  void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
252 //                                  DocEntrySet *set );
253
254 };
255
256 } // end namespace gdcm
257
258 //-----------------------------------------------------------------------------
259 #endif