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