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