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