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