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