]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
Cosmetics
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2005/01/31 12:19:34 $
7   Version:   $Revision: 1.103 $
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 // Dictionaries
47    virtual void PrintPubDict (std::ostream &os = std::cout);
48    virtual void PrintShaDict (std::ostream &os = std::cout);
49
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 // Ordering of Documents
78    bool operator<(Document &document);
79
80 // File I/O
81    /// Accessor to \ref Filename
82    const std::string &GetFileName() const { return Filename; }
83    /// Accessor to \ref Filename
84    void SetFileName(std::string const &fileName) { Filename = fileName; }
85
86    std::ifstream *OpenFile();
87    bool CloseFile();
88    void WriteContent( std::ofstream *fp, FileType type );
89
90 // Content entries
91
92    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
93    virtual void LoadEntryBinArea(BinEntry *entry);
94
95    void LoadDocEntrySafe(DocEntry *entry);
96
97 protected:
98 // Methods
99    // Constructor and destructor are protected to forbid end user 
100    // to instanciate from this class Document (only gdcm::File and
101    // gdcm::DicomDir are meaningfull).
102    Document();
103    Document( std::string const &filename );
104    virtual ~Document();
105    
106    uint16_t ReadInt16() throw ( FormatError );
107    uint32_t ReadInt32() throw ( FormatError );
108    void     SkipBytes(uint32_t);
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    void SkipDocEntry          (DocEntry *entry);
166    void SkipToNextDocEntry    (DocEntry *entry);
167
168    void FixDocEntryFoundLength(DocEntry *entry,uint32_t l);
169    bool IsDocEntryAnInteger   (DocEntry *entry);
170
171    void Initialize();
172    bool CheckSwap();
173    void SwitchByteSwapCode();
174    void SetMaxSizeLoadEntry(long);
175    void SetMaxSizePrintEntry(long);
176
177    // DocEntry related utilities
178    DocEntry *ReadNextDocEntry();
179
180    void HandleBrokenEndian  (uint16_t &group, uint16_t &elem);
181    void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
182
183 // Variables
184    /// Public dictionary used to parse this header
185    Dict *RefPubDict;
186    /// \brief Optional "shadow dictionary" (private elements) used to parse
187    /// this header
188    Dict *RefShaDict;
189
190    /// \brief Size threshold above which an element value will NOT be loaded
191    /// in memory (to avoid loading the image/volume itself). By default,
192    /// this upper bound is fixed to 1024 bytes (which might look reasonable
193    /// when one considers the definition of the various VR contents).
194    uint32_t MaxSizeLoadEntry;
195    
196    /// \brief Size threshold above which an element value will NOT be *printed*
197    /// in order no to polute the screen output. By default, this upper bound
198    /// is fixed to 64 bytes.
199    uint32_t MaxSizePrintEntry;   
200
201 //  uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
202 //  void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
203 //                                   DocEntrySet *set );
204 };
205
206 } // end namespace gdcm
207
208 //-----------------------------------------------------------------------------
209 #endif