]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* src/gdcmDebug.h : remove the Debug test in the gdcmErrorMacro
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2005/01/20 11:07:07 $
7   Version:   $Revision: 1.93 $
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 "gdcmVR.h"
23 #include "gdcmDict.h"
24 #include "gdcmElementSet.h"
25
26 #include <map>
27 #include <list>
28 #include <fstream>
29
30 namespace gdcm 
31 {
32 class ValEntry;
33 class BinEntry;
34 class SeqEntry;
35 class Dict;
36 class RLEFramesInfo;
37 class JPEGFragmentsInfo;
38
39 //-----------------------------------------------------------------------------
40 /**
41  * \brief Derived by both Header and DicomDir
42  */
43 class GDCM_EXPORT Document : public ElementSet
44 {
45 public:
46 // Informations contained in the parser
47    virtual bool IsReadable();
48    FileType GetFileType();
49
50    std::string GetTransferSyntax();
51  
52    RLEFramesInfo *GetRLEInfo() { return RLEInfo; }
53    JPEGFragmentsInfo *GetJPEGInfo() { return JPEGInfo; }
54
55 // Dictionaries
56    virtual void PrintPubDict (std::ostream &os = std::cout);
57    virtual void PrintShaDict (std::ostream &os = std::cout);
58
59    Dict* GetPubDict();
60    Dict* GetShaDict();
61    bool SetShaDict(Dict* dict);
62    bool SetShaDict(DictKey const &dictName);
63
64 // Swap code
65    /// 'Swap code' accessor (see \ref SwapCode )
66    int GetSwapCode() { return SwapCode; }
67    // System access (meaning endian related !?)
68    uint16_t SwapShort(uint16_t);   // needed by File
69    uint32_t SwapLong(uint32_t);    // needed by File
70    uint16_t UnswapShort(uint16_t); // needed by File
71    uint32_t UnswapLong(uint32_t);  // needed by File
72    
73 // Ordering of Documents
74    bool operator<(Document &document);
75
76 public:
77 // File I/O
78    /// Accessor to \ref Filename
79    const std::string &GetFileName() const { return Filename; }
80    /// Accessor to \ref Filename
81    void SetFileName(std::string const &fileName) { Filename = fileName; }
82
83    std::ifstream *OpenFile();
84    bool CloseFile();
85    void WriteContent( std::ofstream *fp, FileType type );
86
87 // Content entries
88
89    virtual bool SetEntry(std::string const &content,
90                          uint16_t group, uint16_t element);
91    virtual bool SetEntry(uint8_t *content, int lgth,
92                          uint16_t group, uint16_t element);
93    virtual bool SetEntry(std::string const &content, ValEntry *entry);
94    virtual bool SetEntry(uint8_t *content, int lgth, BinEntry *entry);
95
96    virtual void *GetEntryBinArea(uint16_t group, uint16_t elem);   
97
98    virtual std::string GetEntry  (uint16_t group, uint16_t elem);
99    virtual std::string GetEntryVR(uint16_t group, uint16_t elem);
100    virtual int GetEntryLength(uint16_t group, uint16_t elem);
101
102    DocEntry *GetDocEntry(uint16_t group, uint16_t element);
103    ValEntry *GetValEntry(uint16_t group, uint16_t element);
104    BinEntry *GetBinEntry(uint16_t group, uint16_t element);
105    SeqEntry *GetSeqEntry(uint16_t group, uint16_t element);
106
107    ValEntry *ReplaceOrCreate(std::string const &value,
108                              uint16_t group, uint16_t elem,
109                              TagName const &vr = GDCM_UNKNOWN);
110    BinEntry *ReplaceOrCreate(uint8_t* binArea, int lgth,
111                              uint16_t group, uint16_t elem,
112                              TagName const &vr = GDCM_UNKNOWN);
113    SeqEntry *ReplaceOrCreate(uint16_t group, uint16_t elem);
114
115    bool ReplaceIfExist(std::string const &value,
116                        uint16_t group, uint16_t elem );
117
118    bool CheckIfEntryExist(uint16_t group, uint16_t elem );
119    
120    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
121    virtual void LoadEntryBinArea(BinEntry *entry);
122
123    void LoadDocEntrySafe(DocEntry *entry);
124    /*TagDocEntryHT *BuildFlatHashTable();*/
125
126    /// Return the Transfer Syntax as a string
127    std::string GetTransferSyntaxName();
128
129    bool IsDicomV3();
130
131 protected:
132 // Methods
133    // Constructor and destructor are protected to forbid end user 
134    // to instanciate from this class Document (only Header and
135    // DicomDir are meaningfull).
136    Document();
137    Document( std::string const &filename );
138    virtual ~Document();
139    
140    void ReadAndSkipEncapsulatedBasicOffsetTable();
141    void ComputeRLEInfo();
142    void ComputeJPEGFragmentInfo();
143    // Entry
144
145    int ComputeGroup0002Length( FileType filetype );
146
147 // Variables
148    /// Refering underlying filename.
149    std::string Filename;
150
151    /// \brief Swap code gives an information on the byte order of a 
152    ///  supposed to be an int32, as it's read on disc 
153    /// (depending on the image Transfer Syntax *and* on the processor endianess)
154    /// as opposed as it should in memory to be dealt as an int32.
155    /// For instance :
156    /// - a 'Little Endian' image, read with a little endian processor
157    /// will have a SwapCode= 1234 (the order is OK; nothing to do)
158    /// - a 'Little Endian' image, read with a big endian procesor
159    /// will have a SwapCode= 4321 (the order is wrong; int32 an int16 must be
160    /// swapped)
161    /// note : values 2143, 4321, 3412 remain for the ACR-NEMA time, and
162    /// the well known 'Bad Big Endian' and 'Bad Little Endian' codes
163    int SwapCode;
164
165    ///\brief whether we already parsed group 0002
166    bool Group0002Parsed;
167
168    ///\brief whether file has a DCM Preamble
169    bool HasDCMPreamble;
170
171    /// File Pointer, opened during Header parsing.
172    std::ifstream *Fp;
173
174    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
175    FileType Filetype;  
176
177    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
178    static const unsigned int HEADER_LENGTH_TO_READ; 
179
180    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
181    /// are NOT loaded.
182    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
183
184    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
185    /// are NOT printed.
186    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
187
188    /// Store the RLE frames info obtained during parsing of pixels.
189    RLEFramesInfo *RLEInfo;
190
191    /// Store the JPEG fragments info obtained during parsing of pixels.
192    JPEGFragmentsInfo *JPEGInfo;
193
194 private:
195 // Methods
196    // Read
197    void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
198    void ParseSQ (SeqEntry *seq,   long offset, long l_max, bool delim_mode);
199
200    void LoadDocEntry         (DocEntry *e);
201    void FindDocEntryLength   (DocEntry *e) throw ( FormatError );
202    uint32_t FindDocEntryLengthOBOrOW() throw( FormatUnexpected );
203    std::string FindDocEntryVR();
204    bool CheckDocEntryVR      (VRKey k);
205
206    std::string GetDocEntryValue  (DocEntry *entry);
207    std::string GetDocEntryUnvalue(DocEntry *entry);
208
209
210    void SkipDocEntry          (DocEntry *entry);
211    void SkipToNextDocEntry    (DocEntry *entry);
212
213    void FixDocEntryFoundLength(DocEntry *entry,uint32_t l);
214    bool IsDocEntryAnInteger   (DocEntry *entry);
215
216    uint16_t ReadInt16() throw ( FormatError );
217    uint32_t ReadInt32() throw ( FormatError );
218    void     SkipBytes(uint32_t);
219    bool     ReadTag(uint16_t, uint16_t);
220    uint32_t ReadTagLength(uint16_t, uint16_t);
221
222    void Initialize();
223    bool CheckSwap();
224    void SwitchByteSwapCode();
225    void SetMaxSizeLoadEntry(long);
226    void SetMaxSizePrintEntry(long);
227
228    // DocEntry related utilities
229    DocEntry *ReadNextDocEntry();
230
231    uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
232 /*   void BuildFlatHashTableRecurse( TagDocEntryHT &builtHT,
233                                    DocEntrySet* set );*/
234
235    void HandleBrokenEndian(uint16_t &group, uint16_t &elem);
236    void HandleOutOfGroup0002(uint16_t &group, uint16_t &elem);
237
238 // Variables
239    /// Public dictionary used to parse this header
240    Dict *RefPubDict;
241    
242    /// \brief Optional "shadow dictionary" (private elements) used to parse
243    /// this header
244    Dict *RefShaDict;
245
246    /// \brief Size threshold above which an element value will NOT be loaded
247    /// in memory (to avoid loading the image/volume itself). By default,
248    /// this upper bound is fixed to 1024 bytes (which might look reasonable
249    /// when one considers the definition of the various VR contents).
250    uint32_t MaxSizeLoadEntry;
251    
252    /// \brief Size threshold above which an element value will NOT be *printed*
253    /// in order no to polute the screen output. By default, this upper bound
254    /// is fixed to 64 bytes.
255    uint32_t MaxSizePrintEntry;   
256
257 private:
258    friend class File;
259 };
260 } // end namespace gdcm
261
262 //-----------------------------------------------------------------------------
263 #endif