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