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