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