]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
fix compilation warnings for the gdcm::Document::TransferSyntaxStrings
[gdcm.git] / src / gdcmDocument.h
1 /*=========================================================================
2  
3   Program:   gdcm
4   Module:    $RCSfile: gdcmDocument.h,v $
5   Language:  C++
6   Date:      $Date: 2004/11/25 16:35:17 $
7   Version:   $Revision: 1.65 $
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 "gdcmJPEGFragmentsInfo.h"
30 #include "gdcmDocEntrySet.h"
31 #include "gdcmElementSet.h"
32
33 class ValEntry;
34 class BinEntry;
35 class SeqEntry;
36
37 #include <map>
38 #include <list>
39 #include <fstream>
40
41 namespace gdcm 
42 {
43
44 enum TransferSyntaxType {
45   ImplicitVRLittleEndian = 0,
46   ImplicitVRLittleEndianDLXGE,
47   ExplicitVRLittleEndian,
48   DeflatedExplicitVRLittleEndian,
49   ExplicitVRBigEndian,
50   JPEGBaselineProcess1,
51   JPEGExtendedProcess2_4,
52   JPEGExtendedProcess3_5,
53   JPEGSpectralSelectionProcess6_8,
54   JPEGFullProgressionProcess10_12,
55   JPEGLosslessProcess14,
56   JPEGLosslessProcess14_1,
57   JPEG2000Lossless,
58   JPEG2000,
59   RLELossless,
60   UnknownTS
61 };
62
63 //-----------------------------------------------------------------------------
64 /**
65  * \brief Derived by both Header and DicomDir
66  */
67 class GDCM_EXPORT Document : public ElementSet
68 {
69 friend class File;
70 private:
71    /// Public dictionary used to parse this header
72    Dict* RefPubDict;
73    
74    /// \brief Optional "shadow dictionary" (private elements) used to parse
75    /// this header
76    Dict* RefShaDict;
77
78    /// \brief Size threshold above which an element value will NOT be loaded
79    /// in memory (to avoid loading the image/volume itself). By default,
80    /// this upper bound is fixed to 1024 bytes (which might look reasonable
81    /// when one considers the definition of the various VR contents).
82    uint32_t MaxSizeLoadEntry;
83    
84    /// \brief Size threshold above which an element value will NOT be *printed*
85    /// in order no to polute the screen output. By default, this upper bound
86    /// is fixed to 64 bytes.
87    uint32_t MaxSizePrintEntry;   
88
89 protected:
90    /// Refering underlying filename.
91    std::string Filename;
92
93    /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
94    /// Bad Little Endian) according to the processor Endianity and
95    /// what is written on disc.
96    int SwapCode;
97
98    /// File Pointer, opened during Header parsing.
99    std::ifstream* Fp;
100
101    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
102    FileType Filetype;  
103
104    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
105    static const unsigned int HEADER_LENGTH_TO_READ; 
106
107    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
108    /// are NOT loaded.
109    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
110
111    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
112    /// are NOT printed.
113    /// \todo Currently not used since collides with #define in
114    ///       class DocEntry . See also
115    ///       method ref Document::SetMaxSizePrintEntry()
116    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
117
118    /// Store the RLE frames info obtained during parsing of pixels.
119    RLEFramesInfo* RLEInfo;
120
121    /// Store the JPEG fragments info obtained during parsing of pixels.
122    JPEGFragmentsInfo* JPEGInfo;
123
124    /// \brief Amount of printed details for each Header Entry (Dicom Element):
125    /// 0 : stands for the least detail level.
126    int PrintLevel;
127    
128 public:
129 // the 2 following will be merged
130    virtual void PrintPubDict (std::ostream &os = std::cout);
131    virtual void PrintShaDict (std::ostream &os = std::cout);
132
133 // Dictionnaries
134    Dict* GetPubDict();
135    Dict* GetShaDict();
136    bool SetShaDict(Dict* dict);
137    bool SetShaDict(DictKey const & dictName);
138
139 // Informations contained in the parser
140    virtual bool IsReadable();
141    TransferSyntaxType GetTransferSyntax();
142    bool IsJPEGLossless();
143    bool IsJPEG2000();
144    bool IsJPEG();
145    bool IsEncapsulate();
146    bool IsDicomV3();
147
148    FileType GetFileType();
149
150    std::ifstream * OpenFile();
151    bool CloseFile();
152
153    void WriteContent( std::ofstream* fp, FileType type );
154
155    ValEntry* ReplaceOrCreateByNumber(std::string const & value,
156                                      uint16_t group, uint16_t elem,
157                                      TagName const & vr = "unkn");
158    
159    BinEntry* ReplaceOrCreateByNumber(uint8_t* binArea, int lgth,
160                                      uint16_t group, uint16_t elem,
161                                      TagName const & vr = "unkn");
162
163    SeqEntry* ReplaceOrCreateByNumber(uint16_t group, uint16_t elem);
164
165    bool ReplaceIfExistByNumber ( std::string const & value,
166                                  uint16_t group, uint16_t elem );
167    
168    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
169    virtual void LoadEntryBinArea(BinEntry* entry);
170       
171    // System access (meaning endian related !?)
172    uint16_t SwapShort(uint16_t);   // needed by File
173    uint32_t SwapLong(uint32_t);    // needed by File
174    uint16_t UnswapShort(uint16_t); // needed by File
175    uint32_t UnswapLong(uint32_t);  // needed by File
176
177    static std::string GetTransferSyntaxValue(TransferSyntaxType type);
178
179 protected:
180    // Constructor and destructor are protected to forbid end user 
181    // to instanciate from this class Document (only Header and
182    // DicomDir are meaningfull).
183    Document();
184    Document( std::string const & filename );
185    virtual ~Document();
186    
187    void ReadAndSkipEncapsulatedBasicOffsetTable();
188    void ComputeRLEInfo();
189    void ComputeJPEGFragmentInfo();
190    // Entry
191    bool CheckIfEntryExistByNumber(uint16_t group, uint16_t elem );
192 public:
193    virtual std::string GetEntryByName    (TagName const & tagName);
194    virtual std::string GetEntryVRByName  (TagName const & tagName);
195    virtual std::string GetEntryByNumber  (uint16_t group, uint16_t elem);
196    virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
197    virtual int     GetEntryLengthByNumber(uint16_t group, uint16_t elem);
198 //protected:
199    virtual bool SetEntryByName  ( std::string const & content, 
200                                   TagName const & tagName );
201    virtual bool SetEntryByNumber(std::string const & content,
202                                  uint16_t group, uint16_t element);
203    virtual bool SetEntryByNumber(uint8_t* content, int lgth,
204                                  uint16_t group, uint16_t element);
205    virtual bool SetEntryLengthByNumber(uint32_t length,
206                                        uint16_t group, uint16_t element);
207
208    virtual size_t GetEntryOffsetByNumber(uint16_t group, uint16_t elem);
209    virtual void* GetEntryBinAreaByNumber(uint16_t group, uint16_t elem);   
210    virtual bool  SetEntryBinAreaByNumber(uint8_t* a, uint16_t group,
211                                                    uint16_t elem);
212
213    virtual void UpdateShaEntries();
214
215    // Header entry
216    DocEntry* GetDocEntryByNumber(uint16_t group, uint16_t element); 
217    DocEntry* GetDocEntryByName  (TagName const & tagName);
218
219    ValEntry* GetValEntryByNumber(uint16_t group, uint16_t element); 
220    //BinEntry* GetBinEntryByNumber(uint16_t group, uint16_t element); 
221    RLEFramesInfo* GetRLEInfo() { return RLEInfo; }
222    JPEGFragmentsInfo* GetJPEGInfo() { return JPEGInfo; }
223
224    void LoadDocEntrySafe(DocEntry* entry);
225    TagDocEntryHT* BuildFlatHashTable();
226
227 private:
228    // Read
229    void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
230    void ParseSQ (SeqEntry *seq,   long offset, long l_max, bool delim_mode);
231
232    void LoadDocEntry      (DocEntry *);
233    void FindDocEntryLength(DocEntry *) throw ( FormatError );
234    void FindDocEntryVR    (DocEntry *);
235    bool CheckDocEntryVR   (DocEntry *, VRKey);
236
237    std::string GetDocEntryValue  (DocEntry *);
238    std::string GetDocEntryUnvalue(DocEntry *);
239
240    void SkipDocEntry          (DocEntry *);
241    void SkipToNextDocEntry    (DocEntry *);
242
243    void FixDocEntryFoundLength(DocEntry *, uint32_t);
244    bool IsDocEntryAnInteger   (DocEntry *);
245
246    uint32_t FindDocEntryLengthOB() throw( FormatUnexpected );
247
248    uint16_t ReadInt16() throw ( FormatError );
249    uint32_t ReadInt32() throw ( FormatError );
250    void     SkipBytes(uint32_t);
251    bool     ReadTag(uint16_t, uint16_t);
252    uint32_t ReadTagLength(uint16_t, uint16_t);
253
254    void Initialise();
255    bool CheckSwap();
256    void SwitchSwapToBigEndian();
257    void SetMaxSizeLoadEntry(long);
258    void SetMaxSizePrintEntry(long);
259
260    // DocEntry related utilities
261    DocEntry* ReadNextDocEntry();
262
263    uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
264    void BuildFlatHashTableRecurse( TagDocEntryHT& builtHT,
265                                    DocEntrySet* set );
266
267    void HandleBrokenEndian(uint16_t  group, uint16_t  elem);
268 public:
269 // Accessors:
270    /// Accessor to \ref PrintLevel
271    void SetPrintLevel(int level) { PrintLevel = level; }
272
273    /// Accessor to \ref Filename
274    const std::string &GetFileName() const { return Filename; }
275
276    /// Accessor to \ref Filename
277    void SetFileName(std::string const & fileName) { Filename = fileName; }
278
279    /// 'Swap code' accessor (see \ref SwapCode )
280    int GetSwapCode() { return SwapCode; }
281    
282    bool operator<(Document &document);
283
284 };
285 } // end namespace gdcm
286
287 //-----------------------------------------------------------------------------
288 #endif