]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* src/gdcmDictEntry.h : now, the IsVRUnknown is correct
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2005/01/06 13:35:38 $
7   Version:   $Revision: 1.72 $
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    virtual bool SetEntryByName  (std::string const & content, 
114                                  TagName const & tagName );
115    virtual bool SetEntryByNumber(std::string const & content,
116                                  uint16_t group, uint16_t element);
117    virtual bool SetEntryByNumber(uint8_t* content, int lgth,
118                                  uint16_t group, uint16_t element);
119    virtual void* GetEntryBinAreaByNumber(uint16_t group, uint16_t elem);   
120
121    virtual std::string GetEntryByName    (TagName const & tagName);
122    virtual std::string GetEntryVRByName  (TagName const & tagName);
123    virtual std::string GetEntryByNumber  (uint16_t group, uint16_t elem);
124    virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
125    virtual int GetEntryLengthByNumber(uint16_t group, uint16_t elem);
126
127    DocEntry* GetDocEntryByNumber(uint16_t group, uint16_t element); 
128    DocEntry* GetDocEntryByName  (TagName const & tagName);
129    ValEntry* GetValEntryByNumber(uint16_t group, uint16_t element); 
130    //BinEntry* GetBinEntryByNumber(uint16_t group, uint16_t element); 
131
132    ValEntry* ReplaceOrCreateByNumber(std::string const & value,
133                                      uint16_t group, uint16_t elem,
134                                      TagName const & vr = GDCM_UNKNOWN);
135    BinEntry* ReplaceOrCreateByNumber(uint8_t* binArea, int lgth,
136                                      uint16_t group, uint16_t elem,
137                                      TagName const & vr = GDCM_UNKNOWN);
138    SeqEntry* ReplaceOrCreateByNumber(uint16_t group, uint16_t elem);
139
140    bool ReplaceIfExistByNumber ( std::string const & value,
141                                  uint16_t group, uint16_t elem );
142    
143    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
144    virtual void LoadEntryBinArea(BinEntry* entry);
145
146    void LoadDocEntrySafe(DocEntry* entry);
147    TagDocEntryHT* BuildFlatHashTable();
148       
149 // Divers
150    static std::string GetTransferSyntaxValue(TransferSyntaxType type);
151
152 protected:
153 // Methods
154    // Constructor and destructor are protected to forbid end user 
155    // to instanciate from this class Document (only Header and
156    // DicomDir are meaningfull).
157    Document();
158    Document( std::string const & filename );
159    virtual ~Document();
160    
161    void ReadAndSkipEncapsulatedBasicOffsetTable();
162    void ComputeRLEInfo();
163    void ComputeJPEGFragmentInfo();
164    // Entry
165    bool CheckIfEntryExistByNumber(uint16_t group, uint16_t elem );
166
167    int ComputeGroup0002Length( FileType filetype );
168
169 // Variables
170    /// Refering underlying filename.
171    std::string Filename;
172
173    /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
174    /// Bad Little Endian) according to the processor Endianity and
175    /// what is written on disc.
176    int SwapCode;
177
178    /// File Pointer, opened during Header parsing.
179    std::ifstream* Fp;
180
181    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
182    FileType Filetype;  
183
184    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
185    static const unsigned int HEADER_LENGTH_TO_READ; 
186
187    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
188    /// are NOT loaded.
189    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
190
191    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
192    /// are NOT printed.
193    /// \todo Currently not used since collides with #define in
194    ///       class DocEntry . See also
195    ///       method ref Document::SetMaxSizePrintEntry()
196    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
197
198    /// Store the RLE frames info obtained during parsing of pixels.
199    RLEFramesInfo* RLEInfo;
200
201    /// Store the JPEG fragments info obtained during parsing of pixels.
202    JPEGFragmentsInfo* JPEGInfo;
203
204 private:
205 // Methods
206    // Read
207    void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
208    void ParseSQ (SeqEntry *seq,   long offset, long l_max, bool delim_mode);
209
210    void LoadDocEntry         (DocEntry *);
211    void FindDocEntryLength   (DocEntry *) throw ( FormatError );
212    std::string FindDocEntryVR();
213    bool CheckDocEntryVR      (VRKey);
214
215    std::string GetDocEntryValue  (DocEntry *);
216    std::string GetDocEntryUnvalue(DocEntry *);
217
218    void SkipDocEntry          (DocEntry *);
219    void SkipToNextDocEntry    (DocEntry *);
220
221    void FixDocEntryFoundLength(DocEntry *, uint32_t);
222    bool IsDocEntryAnInteger   (DocEntry *);
223
224    uint32_t FindDocEntryLengthOB() throw( FormatUnexpected );
225
226    uint16_t ReadInt16() throw ( FormatError );
227    uint32_t ReadInt32() throw ( FormatError );
228    void     SkipBytes(uint32_t);
229    bool     ReadTag(uint16_t, uint16_t);
230    uint32_t ReadTagLength(uint16_t, uint16_t);
231
232    void Initialise();
233    bool CheckSwap();
234    void SwitchSwapToBigEndian();
235    void SetMaxSizeLoadEntry(long);
236    void SetMaxSizePrintEntry(long);
237
238    // DocEntry related utilities
239    DocEntry* ReadNextDocEntry();
240
241    uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
242    void BuildFlatHashTableRecurse( TagDocEntryHT& builtHT,
243                                    DocEntrySet* set );
244
245    void HandleBrokenEndian(uint16_t  group, uint16_t  elem);
246
247 // Variables
248    /// Public dictionary used to parse this header
249    Dict* RefPubDict;
250    
251    /// \brief Optional "shadow dictionary" (private elements) used to parse
252    /// this header
253    Dict* RefShaDict;
254
255    /// \brief Size threshold above which an element value will NOT be loaded
256    /// in memory (to avoid loading the image/volume itself). By default,
257    /// this upper bound is fixed to 1024 bytes (which might look reasonable
258    /// when one considers the definition of the various VR contents).
259    uint32_t MaxSizeLoadEntry;
260    
261    /// \brief Size threshold above which an element value will NOT be *printed*
262    /// in order no to polute the screen output. By default, this upper bound
263    /// is fixed to 64 bytes.
264    uint32_t MaxSizePrintEntry;   
265
266 private:
267    friend class File;
268 };
269 } // end namespace gdcm
270
271 //-----------------------------------------------------------------------------
272 #endif