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