]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* Add method File::AnonymizeNoLoad, to allow anomymizing files
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2005/02/06 14:39:35 $
7   Version:   $Revision: 1.105 $
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 "gdcmCommon.h"
27
28 #include <map>
29 #include <list>
30 #include <fstream>
31
32 namespace gdcm 
33 {
34 class ValEntry;
35 class BinEntry;
36 class SeqEntry;
37 class Dict;
38
39 //-----------------------------------------------------------------------------
40 /**
41  * \brief Derived by both gdcm::File and gdcm::DicomDir
42  */
43 class GDCM_EXPORT Document : public ElementSet
44 {
45 public:
46
47 typedef std::list<Element> ListElements;
48
49 // Dictionaries
50    Dict *GetPubDict();
51    Dict *GetShaDict();
52    bool SetShaDict(Dict *dict);
53    bool SetShaDict(DictKey const &dictName);
54
55 // Informations contained in the gdcm::Document
56    virtual bool IsReadable();
57    bool IsDicomV3();
58    bool IsPapyrus();
59    FileType GetFileType();
60    std::string GetTransferSyntax();
61    /// Return the Transfer Syntax as a string
62    std::string GetTransferSyntaxName();
63
64 // Swap code
65    /// 'Swap code' accessor (see \ref SwapCode )
66    int GetSwapCode() { return SwapCode; }
67    // System access (meaning endian related !?)
68    uint16_t SwapShort(uint16_t);
69    uint32_t SwapLong(uint32_t);
70    /// \brief  Unswaps back the bytes of 2-bytes long integer 
71    ///         so they agree with the processor order.
72    uint16_t UnswapShort(uint16_t a) { return SwapShort(a);}
73    /// \brief  Unswaps back the bytes of 4-byte long integer 
74    ///         so they agree with the processor order.
75    uint32_t UnswapLong(uint32_t a) { return SwapLong(a);}
76    
77 // File I/O
78    /// Accessor to \ref Filename
79    const std::string &GetFileName() const { return Filename; }
80    /// Accessor to \ref Filename
81    void SetFileName(std::string const &fileName) { Filename = fileName; }
82
83    std::ifstream *OpenFile();
84    bool CloseFile();
85    void WriteContent( std::ofstream *fp, FileType type );
86
87 // Content entries
88    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
89    virtual void LoadEntryBinArea(BinEntry *entry);
90
91    void LoadDocEntrySafe(DocEntry *entry);
92  
93 // Ordering of Documents
94    bool operator<(Document &document);
95
96 protected:
97 // Methods
98    // Constructor and destructor are protected to forbid end user 
99    // to instanciate from this class Document (only gdcm::File and
100    // gdcm::DicomDir are meaningfull).
101    Document();
102    Document( std::string const &filename );
103    virtual ~Document();
104    
105    uint16_t ReadInt16() throw ( FormatError );
106    uint32_t ReadInt32() throw ( FormatError );
107    void     SkipBytes(uint32_t);
108    int ComputeGroup0002Length( FileType filetype );
109
110 // Variables
111    /// Refering underlying filename.
112    std::string Filename;
113
114    /// \brief Swap code gives an information on the byte order of a 
115    ///  supposed to be an int32, as it's read on disc 
116    /// (depending on the image Transfer Syntax *and* on the processor endianess)
117    /// as opposed as it should in memory to be dealt as an int32.
118    /// For instance :
119    /// - a 'Little Endian' image, read with a little endian processor
120    /// will have a SwapCode= 1234 (the order is OK; nothing to do)
121    /// - a 'Little Endian' image, read with a big endian procesor
122    /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
123    /// swapped)
124    /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
125    /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
126    int SwapCode;
127
128    ///\brief whether we already parsed group 0002 (Meta Elements)
129    bool Group0002Parsed;
130
131    ///\brief whether file has a DCM Preamble
132    bool HasDCMPreamble;
133
134    /// File Pointer, opened during Document parsing.
135    std::ifstream *Fp;
136
137    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
138    FileType Filetype;  
139
140    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
141    static const unsigned int HEADER_LENGTH_TO_READ; 
142    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
143    /// are NOT loaded.
144    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
145    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
146    /// are NOT printed.
147    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
148
149    /// List of element to Anonymize
150    ListElements AnonymizeList;
151
152 private:
153 // Methods
154    void Initialize();
155
156    // Read
157    void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
158    void ParseSQ (SeqEntry *seq,   long offset, long l_max, bool delim_mode);
159
160    void LoadDocEntry         (DocEntry *e);
161    void FindDocEntryLength   (DocEntry *e) throw ( FormatError );
162    uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
163    std::string FindDocEntryVR();
164    bool CheckDocEntryVR      (VRKey k);
165
166    std::string GetDocEntryValue  (DocEntry *entry);
167    std::string GetDocEntryUnvalue(DocEntry *entry);
168
169    void SkipDocEntry          (DocEntry *entry);
170    void SkipToNextDocEntry    (DocEntry *entry);
171
172    void FixDocEntryFoundLength(DocEntry *entry,uint32_t l);
173    bool IsDocEntryAnInteger   (DocEntry *entry);
174
175    bool CheckSwap();
176    void SwitchByteSwapCode();
177    void SetMaxSizeLoadEntry(long);
178    void SetMaxSizePrintEntry(long);
179
180    // DocEntry related utilities
181    DocEntry *ReadNextDocEntry();
182
183    void HandleBrokenEndian  (uint16_t &group, uint16_t &elem);
184    void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
185
186 // Variables
187    /// Public dictionary used to parse this header
188    Dict *RefPubDict;
189    /// \brief Optional "shadow dictionary" (private elements) used to parse
190    /// this header
191    Dict *RefShaDict;
192
193    /// \brief Size threshold above which an element value will NOT be loaded
194    /// in memory (to avoid loading the image/volume itself). By default,
195    /// this upper bound is fixed to 1024 bytes (which might look reasonable
196    /// when one considers the definition of the various VR contents).
197    uint32_t MaxSizeLoadEntry;
198    
199    /// \brief Size threshold above which an element value will NOT be *printed*
200    /// in order no to polute the screen output. By default, this upper bound
201    /// is fixed to 64 bytes.
202    uint32_t MaxSizePrintEntry;   
203
204
205 //  uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
206 //  void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
207 //                                   DocEntrySet *set );
208
209 };
210
211 } // end namespace gdcm
212
213 //-----------------------------------------------------------------------------
214 #endif