]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
According to Benoit's suggestion, and without any objection from anybody
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2005/01/08 15:03:59 $
7   Version:   $Revision: 1.78 $
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 enum TransferSyntaxType {
40   ImplicitVRLittleEndian = 0,
41   ImplicitVRLittleEndianDLXGE,
42   ExplicitVRLittleEndian,
43   DeflatedExplicitVRLittleEndian,
44   ExplicitVRBigEndian,
45   JPEGBaselineProcess1,
46   JPEGExtendedProcess2_4,
47   JPEGExtendedProcess3_5,
48   JPEGSpectralSelectionProcess6_8,
49   JPEGFullProgressionProcess10_12,
50   JPEGLosslessProcess14,
51   JPEGLosslessProcess14_1,
52   JPEG2000Lossless,
53   JPEG2000,
54   RLELossless,
55   UnknownTS
56 };
57
58 //-----------------------------------------------------------------------------
59 /**
60  * \brief Derived by both Header and DicomDir
61  */
62 class GDCM_EXPORT Document : public ElementSet
63 {
64 public:
65 // Informations contained in the parser
66    virtual bool IsReadable();
67    FileType GetFileType();
68
69    TransferSyntaxType GetTransferSyntax();
70
71    bool IsJPEGLossless();
72    bool IsJPEG2000();
73    bool IsJPEG();
74    bool IsEncapsulate();
75    bool IsDicomV3();
76
77    RLEFramesInfo *GetRLEInfo() { return RLEInfo; }
78    JPEGFragmentsInfo *GetJPEGInfo() { return JPEGInfo; }
79
80 // Dictionnaries
81    virtual void PrintPubDict (std::ostream &os = std::cout);
82    virtual void PrintShaDict (std::ostream &os = std::cout);
83
84    Dict* GetPubDict();
85    Dict* GetShaDict();
86    bool SetShaDict(Dict* dict);
87    bool SetShaDict(DictKey const &dictName);
88
89 // Swap code
90    /// 'Swap code' accessor (see \ref SwapCode )
91    int GetSwapCode() { return SwapCode; }
92    // System access (meaning endian related !?)
93    uint16_t SwapShort(uint16_t);   // needed by File
94    uint32_t SwapLong(uint32_t);    // needed by File
95    uint16_t UnswapShort(uint16_t); // needed by File
96    uint32_t UnswapLong(uint32_t);  // needed by File
97    
98 // Ordering of Documents
99    bool operator<(Document &document);
100
101 public:
102 // File I/O
103    /// Accessor to \ref Filename
104    const std::string &GetFileName() const { return Filename; }
105    /// Accessor to \ref Filename
106    void SetFileName(std::string const &fileName) { Filename = fileName; }
107
108    std::ifstream *OpenFile();
109    bool CloseFile();
110    void WriteContent( std::ofstream *fp, FileType type );
111
112 // Content entries
113
114    virtual bool SetEntry(std::string const &content,
115                          uint16_t group, uint16_t element);
116    virtual bool SetEntry(uint8_t *content, int lgth,
117                          uint16_t group, uint16_t element);
118    virtual bool SetEntry(std::string const &content,ValEntry *entry);
119    virtual bool SetEntry(uint8_t *content, int lgth,BinEntry *entry);
120
121    virtual void *GetEntryBinArea(uint16_t group, uint16_t elem);   
122
123    virtual std::string GetEntry  (uint16_t group, uint16_t elem);
124    virtual std::string GetEntryVR(uint16_t group, uint16_t elem);
125    virtual int GetEntryLength(uint16_t group, uint16_t elem);
126
127    DocEntry *GetDocEntry(uint16_t group, uint16_t element); 
128    ValEntry *GetValEntry(uint16_t group, uint16_t element); 
129    BinEntry *GetBinEntry(uint16_t group, uint16_t element); 
130
131    ValEntry *ReplaceOrCreate(std::string const &value,
132                              uint16_t group, uint16_t elem,
133                              TagName const &vr = GDCM_UNKNOWN);
134    BinEntry *ReplaceOrCreate(uint8_t* binArea, int lgth,
135                              uint16_t group, uint16_t elem,
136                              TagName const &vr = GDCM_UNKNOWN);
137    SeqEntry *ReplaceOrCreate(uint16_t group, uint16_t elem);
138
139    bool ReplaceIfExist ( std::string const &value,
140                          uint16_t group, uint16_t elem );
141    
142    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
143    virtual void LoadEntryBinArea(BinEntry *entry);
144
145    void LoadDocEntrySafe(DocEntry *entry);
146    TagDocEntryHT *BuildFlatHashTable();
147       
148 // Divers
149    static std::string GetTransferSyntaxValue(TransferSyntaxType type);
150
151 protected:
152 // Methods
153    // Constructor and destructor are protected to forbid end user 
154    // to instanciate from this class Document (only Header and
155    // DicomDir are meaningfull).
156    Document();
157    Document( std::string const &filename );
158    virtual ~Document();
159    
160    void ReadAndSkipEncapsulatedBasicOffsetTable();
161    void ComputeRLEInfo();
162    void ComputeJPEGFragmentInfo();
163    // Entry
164    bool CheckIfEntryExist(uint16_t group, uint16_t elem );
165
166    int ComputeGroup0002Length( FileType filetype );
167
168 // Variables
169    /// Refering underlying filename.
170    std::string Filename;
171
172    /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
173    /// Bad Little Endian) according to the processor Endianity and
174    /// what is written on disc.
175    int SwapCode;
176
177    /// File Pointer, opened during Header parsing.
178    std::ifstream *Fp;
179
180    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
181    FileType Filetype;  
182
183    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
184    static const unsigned int HEADER_LENGTH_TO_READ; 
185
186    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
187    /// are NOT loaded.
188    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
189
190    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
191    /// are NOT printed.
192    /// \todo Currently not used since collides with #define in
193    ///       class DocEntry . See also
194    ///       method ref Document::SetMaxSizePrintEntry()
195    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
196
197    /// Store the RLE frames info obtained during parsing of pixels.
198    RLEFramesInfo *RLEInfo;
199
200    /// Store the JPEG fragments info obtained during parsing of pixels.
201    JPEGFragmentsInfo *JPEGInfo;
202
203 private:
204 // Methods
205    // Read
206    void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
207    void ParseSQ (SeqEntry *seq,   long offset, long l_max, bool delim_mode);
208
209    void LoadDocEntry         (DocEntry *e);
210    void FindDocEntryLength   (DocEntry *e) throw ( FormatError );
211    uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
212    std::string FindDocEntryVR();
213    bool CheckDocEntryVR      (VRKey k);
214
215    std::string GetDocEntryValue  (DocEntry *entry);
216    std::string GetDocEntryUnvalue(DocEntry *entry);
217
218
219    void SkipDocEntry          (DocEntry *entry);
220    void SkipToNextDocEntry    (DocEntry *entry);
221
222    void FixDocEntryFoundLength(DocEntry *entry,uint32_t l);
223    bool IsDocEntryAnInteger   (DocEntry *entry);
224
225    uint16_t ReadInt16() throw ( FormatError );
226    uint32_t ReadInt32() throw ( FormatError );
227    void     SkipBytes(uint32_t);
228    bool     ReadTag(uint16_t, uint16_t);
229    uint32_t ReadTagLength(uint16_t, uint16_t);
230
231    void Initialise();
232    bool CheckSwap();
233    void SwitchSwapToBigEndian();
234    void SetMaxSizeLoadEntry(long);
235    void SetMaxSizePrintEntry(long);
236
237    // DocEntry related utilities
238    DocEntry *ReadNextDocEntry();
239
240    uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
241    void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
242                                    DocEntrySet* set );
243
244    void HandleBrokenEndian(uint16_t  group, uint16_t  elem);
245
246 // Variables
247    /// Public dictionary used to parse this header
248    Dict *RefPubDict;
249    
250    /// \brief Optional "shadow dictionary" (private elements) used to parse
251    /// this header
252    Dict *RefShaDict;
253
254    /// \brief Size threshold above which an element value will NOT be loaded
255    /// in memory (to avoid loading the image/volume itself). By default,
256    /// this upper bound is fixed to 1024 bytes (which might look reasonable
257    /// when one considers the definition of the various VR contents).
258    uint32_t MaxSizeLoadEntry;
259    
260    /// \brief Size threshold above which an element value will NOT be *printed*
261    /// in order no to polute the screen output. By default, this upper bound
262    /// is fixed to 64 bytes.
263    uint32_t MaxSizePrintEntry;   
264
265 private:
266    friend class File;
267 };
268 } // end namespace gdcm
269
270 //-----------------------------------------------------------------------------
271 #endif