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