]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
STYLE: minor clean up
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2005/01/11 17:19:39 $
7   Version:   $Revision: 1.81 $
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
26 #include <map>
27 #include <list>
28 #include <fstream>
29
30 namespace gdcm 
31 {
32 class ValEntry;
33 class BinEntry;
34 class SeqEntry;
35 class Dict;
36 class RLEFramesInfo;
37 class JPEGFragmentsInfo;
38
39 //-----------------------------------------------------------------------------
40 /**
41  * \brief Derived by both Header and DicomDir
42  */
43 class GDCM_EXPORT Document : public ElementSet
44 {
45 public:
46 // Informations contained in the parser
47    virtual bool IsReadable();
48    FileType GetFileType();
49
50    std::string GetTransferSyntax();
51
52    bool IsDicomV3();
53
54    RLEFramesInfo *GetRLEInfo() { return RLEInfo; }
55    JPEGFragmentsInfo *GetJPEGInfo() { return JPEGInfo; }
56
57 // Dictionnaries
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 File
71    uint32_t SwapLong(uint32_t);    // needed by File
72    uint16_t UnswapShort(uint16_t); // needed by File
73    uint32_t UnswapLong(uint32_t);  // needed by File
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 bool SetEntry(std::string const &content,
92                          uint16_t group, uint16_t element);
93    virtual bool SetEntry(uint8_t *content, int lgth,
94                          uint16_t group, uint16_t element);
95    virtual bool SetEntry(std::string const &content, ValEntry *entry);
96    virtual bool SetEntry(uint8_t *content, int lgth, BinEntry *entry);
97
98    virtual void *GetEntryBinArea(uint16_t group, uint16_t elem);   
99
100    virtual std::string GetEntry  (uint16_t group, uint16_t elem);
101    virtual std::string GetEntryVR(uint16_t group, uint16_t elem);
102    virtual int GetEntryLength(uint16_t group, uint16_t elem);
103
104    DocEntry *GetDocEntry(uint16_t group, uint16_t element); 
105    ValEntry *GetValEntry(uint16_t group, uint16_t element); 
106    BinEntry *GetBinEntry(uint16_t group, uint16_t element); 
107
108    ValEntry *ReplaceOrCreate(std::string const &value,
109                              uint16_t group, uint16_t elem,
110                              TagName const &vr = GDCM_UNKNOWN);
111    BinEntry *ReplaceOrCreate(uint8_t* binArea, int lgth,
112                              uint16_t group, uint16_t elem,
113                              TagName const &vr = GDCM_UNKNOWN);
114    SeqEntry *ReplaceOrCreate(uint16_t group, uint16_t elem);
115
116    bool ReplaceIfExist(std::string const &value,
117                        uint16_t group, uint16_t elem );
118    
119    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
120    virtual void LoadEntryBinArea(BinEntry *entry);
121
122    void LoadDocEntrySafe(DocEntry *entry);
123    TagDocEntryHT *BuildFlatHashTable();
124       
125 protected:
126 // Methods
127    // Constructor and destructor are protected to forbid end user 
128    // to instanciate from this class Document (only Header and
129    // DicomDir are meaningfull).
130    Document();
131    Document( std::string const &filename );
132    virtual ~Document();
133    
134    void ReadAndSkipEncapsulatedBasicOffsetTable();
135    void ComputeRLEInfo();
136    void ComputeJPEGFragmentInfo();
137    // Entry
138    bool CheckIfEntryExist(uint16_t group, uint16_t elem );
139
140    int ComputeGroup0002Length( FileType filetype );
141
142 // Variables
143    /// Refering underlying filename.
144    std::string Filename;
145
146    /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
147    /// Bad Little Endian) according to the processor Endianity and
148    /// what is written on disc.
149    int SwapCode;
150
151    ///\brief whether we already parsed group 0002
152    bool Group0002Parsed;
153
154    ///\brief whether file has a DCM Preamble
155    bool HasDCMPreamble;
156
157    /// File Pointer, opened during Header parsing.
158    std::ifstream *Fp;
159
160    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
161    FileType Filetype;  
162
163    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
164    static const unsigned int HEADER_LENGTH_TO_READ; 
165
166    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
167    /// are NOT loaded.
168    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
169
170    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
171    /// are NOT printed.
172    /// \todo Currently not used since collides with #define in
173    ///       class DocEntry . See also
174    ///       method ref Document::SetMaxSizePrintEntry()
175    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
176
177    /// Store the RLE frames info obtained during parsing of pixels.
178    RLEFramesInfo *RLEInfo;
179
180    /// Store the JPEG fragments info obtained during parsing of pixels.
181    JPEGFragmentsInfo *JPEGInfo;
182
183 private:
184 // Methods
185    // Read
186    void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
187    void ParseSQ (SeqEntry *seq,   long offset, long l_max, bool delim_mode);
188
189    void LoadDocEntry         (DocEntry *e);
190    void FindDocEntryLength   (DocEntry *e) throw ( FormatError );
191    uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
192    std::string FindDocEntryVR();
193    bool CheckDocEntryVR      (VRKey k);
194
195    std::string GetDocEntryValue  (DocEntry *entry);
196    std::string GetDocEntryUnvalue(DocEntry *entry);
197
198
199    void SkipDocEntry          (DocEntry *entry);
200    void SkipToNextDocEntry    (DocEntry *entry);
201
202    void FixDocEntryFoundLength(DocEntry *entry,uint32_t l);
203    bool IsDocEntryAnInteger   (DocEntry *entry);
204
205    uint16_t ReadInt16() throw ( FormatError );
206    uint32_t ReadInt32() throw ( FormatError );
207    void     SkipBytes(uint32_t);
208    bool     ReadTag(uint16_t, uint16_t);
209    uint32_t ReadTagLength(uint16_t, uint16_t);
210
211    void Initialise();
212    bool CheckSwap();
213    void SwitchByteSwapCode();
214    void SetMaxSizeLoadEntry(long);
215    void SetMaxSizePrintEntry(long);
216
217    // DocEntry related utilities
218    DocEntry *ReadNextDocEntry();
219
220    uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
221    void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
222                                    DocEntrySet* set );
223
224    void HandleBrokenEndian(uint16_t  group, uint16_t  elem);
225    void HandleOutOfGroup0002(uint16_t group);
226
227 // Variables
228    /// Public dictionary used to parse this header
229    Dict *RefPubDict;
230    
231    /// \brief Optional "shadow dictionary" (private elements) used to parse
232    /// this header
233    Dict *RefShaDict;
234
235    /// \brief Size threshold above which an element value will NOT be loaded
236    /// in memory (to avoid loading the image/volume itself). By default,
237    /// this upper bound is fixed to 1024 bytes (which might look reasonable
238    /// when one considers the definition of the various VR contents).
239    uint32_t MaxSizeLoadEntry;
240    
241    /// \brief Size threshold above which an element value will NOT be *printed*
242    /// in order no to polute the screen output. By default, this upper bound
243    /// is fixed to 64 bytes.
244    uint32_t MaxSizePrintEntry;   
245
246 private:
247    friend class File;
248 };
249 } // end namespace gdcm
250
251 //-----------------------------------------------------------------------------
252 #endif