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