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