]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* Valgrind note: after Mathieu Malaterre teached me how to read
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2004/06/18 00:11:45 $
7   Version:   $Revision: 1.10 $
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.htm 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 // gdcmDocument.h
20 //-----------------------------------------------------------------------------
21 #ifndef GDCMDOCUMENT_H
22 #define GDCMDOCUMENT_H
23
24 #include "gdcmCommon.h"
25 #include "gdcmVR.h"
26 #include "gdcmTS.h"
27 #include "gdcmException.h"
28 #include "gdcmDictSet.h"
29 #include "gdcmDocEntry.h"
30
31 class gdcmValEntry;
32 class gdcmBinEntry;
33 class gdcmSeqEntry;
34
35 #include "gdcmDocEntrySet.h"
36 #include "gdcmElementSet.h"
37
38 #include <map>
39 #include <list>
40
41 //-----------------------------------------------------------------------------
42 typedef std::string VRKey;
43 typedef std::string VRAtr;
44 typedef std::map<VRKey, VRAtr> VRHT;    // Value Representation Hash Table
45
46 //-----------------------------------------------------------------------------
47 /**
48  * \brief Derived by both gdcmHeader and gdcmDicomDir
49  */
50 class GDCM_EXPORT gdcmDocument : public gdcmElementSet
51 {
52 friend class gdcmFile;
53 private:
54    /// Public dictionary used to parse this header
55    gdcmDict *RefPubDict;
56    
57    /// \brief Optional "shadow dictionary" (private elements) used to parse
58    /// this header
59    gdcmDict *RefShaDict;
60    
61    /// \brief Equals =1 if user wants to skip shadow groups while parsing
62    /// (to save space)
63    int ignoreShadow;
64
65    /// \brief Size threshold above which an element value will NOT be loaded
66    /// in memory (to avoid loading the image/volume itself). By default,
67    /// this upper bound is fixed to 1024 bytes (which might look reasonable
68    /// when one considers the definition of the various VR contents).
69    guint32 MaxSizeLoadEntry;
70    
71    /// \brief Size threshold above which an element value will NOT be *printed*
72    /// in order no to polute the screen output. By default, this upper bound
73    /// is fixed to 64 bytes.
74    guint32 MaxSizePrintEntry;   
75
76 protected:
77    /// Refering underlying filename.
78    std::string filename;
79
80    /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
81    /// Bad Little Endian) according to the processor Endianity and
82    /// what is written on disc.
83    int sw;
84
85    /// File Pointer, opened during Header parsing.
86    FILE *fp;
87
88    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
89    FileType filetype;  
90
91    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
92    static const unsigned int HEADER_LENGTH_TO_READ; 
93
94    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
95    /// are NOT loaded.
96    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
97
98    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
99    /// are NOT printed.
100    /// \todo Currently not used since collides with #define in
101    ///       \ref gdcmDocEntry.cxx. See also
102    ///       \ref gdcmDocument::SetMaxSizePrintEntry()
103    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
104
105    /// Will be set 1 if user asks to 'go inside' the 'sequences' (VR = "SQ")
106    int enableSequences;
107
108    /// \brief Amount of printed details for each Header Entry (Dicom Element):
109    /// 0 : stands for the least detail level.
110    int printLevel;
111    
112 public:
113 // the 2 following will be merged
114    virtual void PrintPubDict (std::ostream &os = std::cout);
115    virtual void PrintShaDict (std::ostream &os = std::cout);
116
117 // Dictionnaries
118    gdcmDict *GetPubDict(void);
119    gdcmDict *GetShaDict(void);
120    bool SetShaDict(gdcmDict *dict);
121    bool SetShaDict(DictKey dictName);
122
123 // Informations contained in the parser
124    virtual bool IsReadable(void);
125    bool IsGivenTransferSyntax(const std::string & SyntaxToCheck);
126    bool IsImplicitVRLittleEndianTransferSyntax(void);
127    bool IsExplicitVRLittleEndianTransferSyntax(void);
128    bool IsDeflatedExplicitVRLittleEndianTransferSyntax(void);
129    bool IsExplicitVRBigEndianTransferSyntax(void);
130    bool IsJPEGBaseLineProcess1TransferSyntax(void);
131    bool IsJPEGExtendedProcess2_4TransferSyntax(void);
132    bool IsJPEGExtendedProcess3_5TransferSyntax(void);
133    bool IsJPEGSpectralSelectionProcess6_8TransferSyntax(void);
134    bool IsRLELossLessTransferSyntax(void);
135    bool IsJPEGLossless(void);
136    bool IsJPEG2000(void);
137    bool IsDicomV3(void);
138
139    FileType GetFileType(void);
140
141 // Read (used in gdcmFile, gdcmDicomDir)
142    FILE *OpenFile(bool exception_on_error = false) throw(gdcmFileError);
143    bool CloseFile(void);
144
145 // Write (used in gdcmFile, gdcmDicomDir)
146    virtual bool Write(FILE *, FileType);
147    virtual void WriteEntryTagVRLength(gdcmDocEntry *tag,
148                                        FILE *_fp, FileType type);
149    virtual void WriteEntryValue(gdcmDocEntry *tag,FILE *_fp,FileType type);
150    virtual bool WriteEntry(gdcmDocEntry *tag,FILE *_fp,FileType type);
151    virtual bool WriteEntries(FILE *_fp,FileType type);
152
153    gdcmValEntry * ReplaceOrCreateByNumber(std::string Value,
154                                              guint16 Group, guint16 Elem);
155                                                                                                                         
156    gdcmBinEntry * ReplaceOrCreateByNumber(void *voidArea, int lgth,
157                                              guint16 Group, guint16 Elem);                                                                                                                      
158    bool ReplaceIfExistByNumber (char *Value, guint16 Group, guint16 Elem);
159    
160    virtual void  *LoadEntryVoidArea       (guint16 Group, guint16 Element);
161    void           LoadVLEntry             (gdcmDocEntry *entry);
162       
163 // System access
164    guint16 SwapShort(guint16);   // needed by gdcmFile
165    guint32 SwapLong(guint32);    // needed by gdcmFile
166    guint16 UnswapShort(guint16); // needed by gdcmFile
167    guint32 UnswapLong(guint32);  // needed by gdcmFile
168
169 protected:
170    // Constructor and destructor are protected to forbid end user 
171    // to instanciate from this class gdcmDocument (only gdcmHeader and
172    // gdcmDicomDir are meaningfull).
173    gdcmDocument(bool exception_on_error  = false);
174    gdcmDocument(const char *inFilename, 
175                 bool  exception_on_error = false, 
176                 bool  enable_sequences   = false,
177                 bool  ignore_shadow      = false);
178    virtual ~gdcmDocument(void);
179    
180    void gdcmDocument::Parse7FE0 (void);   
181 // Entry
182    int CheckIfEntryExistByNumber(guint16 Group, guint16 Elem ); // int !
183 public:
184    virtual std::string GetEntryByName    (std::string tagName);
185    virtual std::string GetEntryVRByName  (std::string tagName);
186    virtual std::string GetEntryByNumber  (guint16 group, guint16 element);
187    virtual std::string GetEntryVRByNumber(guint16 group, guint16 element);
188    virtual int     GetEntryLengthByNumber(guint16 group, guint16 element);
189 protected:
190    virtual bool SetEntryByName  (std::string content, std::string tagName);
191    virtual bool SetEntryByNumber(std::string content,
192                                  guint16 group, guint16 element);
193    virtual bool SetEntryByNumber(void *content, int lgth,
194                                  guint16 group, guint16 element);                                                                                       
195    virtual bool SetEntryLengthByNumber(guint32 length,
196                                  guint16 group, guint16 element);
197
198    virtual size_t GetEntryOffsetByNumber  (guint16 Group, guint16 Elem);
199    virtual void  *GetEntryVoidAreaByNumber(guint16 Group, guint16 Elem);   
200    virtual bool   SetEntryVoidAreaByNumber(void *a, guint16 Group, guint16 Elem);
201
202    virtual void UpdateShaEntries(void);
203
204 // Header entry
205    gdcmDocEntry *GetDocEntryByNumber  (guint16 group, guint16 element); 
206    gdcmDocEntry *GetDocEntryByName    (std::string Name);
207         
208    gdcmValEntry *GetValEntryByNumber  (guint16 group, guint16 element); 
209    gdcmBinEntry *GetBinEntryByNumber  (guint16 group, guint16 element); 
210
211    void LoadDocEntrySafe(gdcmDocEntry *);
212
213 private:
214 // Read
215    long ParseDES(gdcmDocEntrySet *set, long offset, long l_max,bool delim_mode);
216    long ParseSQ(gdcmSeqEntry *seq, long offset, long l_max, bool delim_mode); 
217    
218    void LoadDocEntry      (gdcmDocEntry *);
219    void FindDocEntryLength(gdcmDocEntry *);
220    void FindDocEntryVR    (gdcmDocEntry *);
221    bool CheckDocEntryVR   (gdcmDocEntry *, VRKey);
222
223    std::string GetDocEntryValue  (gdcmDocEntry *);
224    std::string GetDocEntryUnvalue(gdcmDocEntry *);
225
226    void SkipDocEntry          (gdcmDocEntry *);
227    void SkipToNextDocEntry    (gdcmDocEntry *);
228
229    void FixDocEntryFoundLength(gdcmDocEntry *, guint32);
230    bool IsDocEntryAnInteger   (gdcmDocEntry *);
231
232    guint32 FindDocEntryLengthOB(void);
233
234    guint16 ReadInt16(void);
235    guint32 ReadInt32(void);
236    void    SkipBytes(guint32);
237    guint32 ReadTagLength(guint16, guint16);
238    guint32 ReadItemTagLength(void);
239    guint32 ReadSequenceDelimiterTagLength(void);
240
241    void Initialise(void);
242    bool CheckSwap(void);
243    void SwitchSwapToBigEndian(void);
244    void SetMaxSizeLoadEntry(long);
245    void SetMaxSizePrintEntry(long);
246
247   // DictEntry  related utilities
248    
249    gdcmDictEntry *GetDictEntryByName  (std::string Name);
250    gdcmDictEntry *GetDictEntryByNumber(guint16, guint16);
251    gdcmDictEntry *NewVirtualDictEntry(guint16 group, 
252                                       guint16 element,
253                                       std::string vr     = "unkn",
254                                       std::string fourth = "unkn",
255                                       std::string name   = "unkn");
256    // DocEntry related utilities
257    gdcmDocEntry *ReadNextDocEntry   (void);
258    gdcmDocEntry *NewDocEntryByNumber(guint16 group, 
259                                      guint16 element);
260    gdcmDocEntry *NewDocEntryByName  (std::string Name);
261
262    gdcmValEntry *NewValEntryByNumber(guint16 group, 
263                                      guint16 element); 
264    gdcmBinEntry *NewBinEntryByNumber(guint16 group, 
265                                      guint16 element);                                                                                             
266    guint32 GenerateFreeTagKeyInGroup(guint16 group);
267
268 public:
269 // Accessors:
270    /// Accessor to \ref printLevel
271    void SetPrintLevel(int level) { printLevel = level; };
272
273    /// Accessor to \ref filename
274    inline std::string GetFileName(void) {return filename;}
275
276    /// Accessor to \ref filename
277    inline void SetFileName(char* fileName) {filename = fileName;}
278
279    /// Accessor to \ref gdcmElementSet::tagHT
280    inline TagDocEntryHT &GetEntry(void) { return tagHT; };
281
282    /// 'Swap code' accessor (see \ref sw )
283    inline int GetSwapCode(void) { return sw; }
284    
285    /// File pointer
286    inline FILE * GetFP(void) { return fp; }
287
288    bool operator<(gdcmDocument &document);
289
290 };
291
292 //-----------------------------------------------------------------------------
293 #endif