]> Creatis software - gdcm.git/blob - src/gdcmDocument.h
* src/gdcmDocument.[h|cxx] : set the Transfert Syntax values to the header
[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 13:12:02 $
7   Version:   $Revision: 1.63 $
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 static const char *TransferSyntaxStrings[] =  {
64   // Implicit VR Little Endian
65   "1.2.840.10008.1.2",
66   // Implicit VR Little Endian DLX G.E?
67   "1.2.840.113619.5.2",
68   // Explicit VR Little Endian
69   "1.2.840.10008.1.2.1",
70   // Deflated Explicit VR Little Endian
71   "1.2.840.10008.1.2.1.99",
72   // Explicit VR Big Endian
73   "1.2.840.10008.1.2.2",
74   // JPEG Baseline (Process 1)
75   "1.2.840.10008.1.2.4.50",
76   // JPEG Extended (Process 2 & 4)
77   "1.2.840.10008.1.2.4.51",
78   // JPEG Extended (Process 3 & 5)
79   "1.2.840.10008.1.2.4.52",
80   // JPEG Spectral Selection, Non-Hierarchical (Process 6 & 8)
81   "1.2.840.10008.1.2.4.53",
82   // JPEG Full Progression, Non-Hierarchical (Process 10 & 12)
83   "1.2.840.10008.1.2.4.55",
84   // JPEG Lossless, Non-Hierarchical (Process 14)
85   "1.2.840.10008.1.2.4.57",
86   // JPEG Lossless, Hierarchical, First-Order Prediction (Process 14, [Selection Value 1])
87   "1.2.840.10008.1.2.4.70",
88   // JPEG 2000 Lossless
89   "1.2.840.10008.1.2.4.90",
90   // JPEG 2000
91   "1.2.840.10008.1.2.4.91",
92   // RLE Lossless
93   "1.2.840.10008.1.2.5",
94   // Unknown
95   "Unknown Transfer Syntax"
96 };
97
98 //-----------------------------------------------------------------------------
99 /**
100  * \brief Derived by both Header and DicomDir
101  */
102 class GDCM_EXPORT Document : public ElementSet
103 {
104 friend class File;
105 private:
106    /// Public dictionary used to parse this header
107    Dict* RefPubDict;
108    
109    /// \brief Optional "shadow dictionary" (private elements) used to parse
110    /// this header
111    Dict* RefShaDict;
112
113    /// \brief Size threshold above which an element value will NOT be loaded
114    /// in memory (to avoid loading the image/volume itself). By default,
115    /// this upper bound is fixed to 1024 bytes (which might look reasonable
116    /// when one considers the definition of the various VR contents).
117    uint32_t MaxSizeLoadEntry;
118    
119    /// \brief Size threshold above which an element value will NOT be *printed*
120    /// in order no to polute the screen output. By default, this upper bound
121    /// is fixed to 64 bytes.
122    uint32_t MaxSizePrintEntry;   
123
124 protected:
125    /// Refering underlying filename.
126    std::string Filename;
127
128    /// \brief SWap code (e.g. Big Endian, Little Endian, Bad Big Endian,
129    /// Bad Little Endian) according to the processor Endianity and
130    /// what is written on disc.
131    int SwapCode;
132
133    /// File Pointer, opened during Header parsing.
134    std::ifstream* Fp;
135
136    /// ACR, ACR_LIBIDO, ExplicitVR, ImplicitVR, Unknown
137    FileType Filetype;  
138
139    /// After opening the file, we read HEADER_LENGTH_TO_READ bytes.
140    static const unsigned int HEADER_LENGTH_TO_READ; 
141
142    /// \brief Elements whose value is longer than MAX_SIZE_LOAD_ELEMENT_VALUE
143    /// are NOT loaded.
144    static const unsigned int MAX_SIZE_LOAD_ELEMENT_VALUE;
145
146    /// \brief Elements whose value is longer than  MAX_SIZE_PRINT_ELEMENT_VALUE
147    /// are NOT printed.
148    /// \todo Currently not used since collides with #define in
149    ///       class DocEntry . See also
150    ///       method ref Document::SetMaxSizePrintEntry()
151    static const unsigned int MAX_SIZE_PRINT_ELEMENT_VALUE;
152
153    /// Store the RLE frames info obtained during parsing of pixels.
154    RLEFramesInfo* RLEInfo;
155
156    /// Store the JPEG fragments info obtained during parsing of pixels.
157    JPEGFragmentsInfo* JPEGInfo;
158
159    /// \brief Amount of printed details for each Header Entry (Dicom Element):
160    /// 0 : stands for the least detail level.
161    int PrintLevel;
162    
163 public:
164 // the 2 following will be merged
165    virtual void PrintPubDict (std::ostream &os = std::cout);
166    virtual void PrintShaDict (std::ostream &os = std::cout);
167
168 // Dictionnaries
169    Dict* GetPubDict();
170    Dict* GetShaDict();
171    bool SetShaDict(Dict* dict);
172    bool SetShaDict(DictKey const & dictName);
173
174 // Informations contained in the parser
175    virtual bool IsReadable();
176    TransferSyntaxType GetTransferSyntax();
177    bool IsJPEGLossless();
178    bool IsJPEG2000();
179    bool IsJPEG();
180    bool IsEncapsulate();
181    bool IsDicomV3();
182
183    FileType GetFileType();
184
185    std::ifstream * OpenFile();
186    bool CloseFile();
187
188    void Write( std::ofstream* fp, FileType type );
189
190    ValEntry* ReplaceOrCreateByNumber(std::string const & value,
191                                      uint16_t group, uint16_t elem,
192                                      TagName const & vr = "unkn");
193    
194    BinEntry* ReplaceOrCreateByNumber(uint8_t* binArea, int lgth,
195                                      uint16_t group, uint16_t elem,
196                                      TagName const & vr = "unkn");
197
198    SeqEntry* ReplaceOrCreateByNumber(uint16_t group, uint16_t elem);
199
200    bool ReplaceIfExistByNumber ( std::string const & value,
201                                  uint16_t group, uint16_t elem );
202    
203    virtual void LoadEntryBinArea(uint16_t group, uint16_t elem);
204    virtual void LoadEntryBinArea(BinEntry* entry);
205       
206    // System access (meaning endian related !?)
207    uint16_t SwapShort(uint16_t);   // needed by File
208    uint32_t SwapLong(uint32_t);    // needed by File
209    uint16_t UnswapShort(uint16_t); // needed by File
210    uint32_t UnswapLong(uint32_t);  // needed by File
211
212 protected:
213    // Constructor and destructor are protected to forbid end user 
214    // to instanciate from this class Document (only Header and
215    // DicomDir are meaningfull).
216    Document();
217    Document( std::string const & filename );
218    virtual ~Document();
219    
220    void ReadAndSkipEncapsulatedBasicOffsetTable();
221    void ComputeRLEInfo();
222    void ComputeJPEGFragmentInfo();
223    // Entry
224    bool CheckIfEntryExistByNumber(uint16_t group, uint16_t elem );
225 public:
226    virtual std::string GetEntryByName    (TagName const & tagName);
227    virtual std::string GetEntryVRByName  (TagName const & tagName);
228    virtual std::string GetEntryByNumber  (uint16_t group, uint16_t elem);
229    virtual std::string GetEntryVRByNumber(uint16_t group, uint16_t elem);
230    virtual int     GetEntryLengthByNumber(uint16_t group, uint16_t elem);
231 //protected:
232    virtual bool SetEntryByName  ( std::string const & content, 
233                                   TagName const & tagName );
234    virtual bool SetEntryByNumber(std::string const & content,
235                                  uint16_t group, uint16_t element);
236    virtual bool SetEntryByNumber(uint8_t* content, int lgth,
237                                  uint16_t group, uint16_t element);
238    virtual bool SetEntryLengthByNumber(uint32_t length,
239                                        uint16_t group, uint16_t element);
240
241    virtual size_t GetEntryOffsetByNumber(uint16_t group, uint16_t elem);
242    virtual void* GetEntryBinAreaByNumber(uint16_t group, uint16_t elem);   
243    virtual bool  SetEntryBinAreaByNumber(uint8_t* a, uint16_t group,
244                                                    uint16_t elem);
245
246    virtual void UpdateShaEntries();
247
248    // Header entry
249    DocEntry* GetDocEntryByNumber(uint16_t group, uint16_t element); 
250    DocEntry* GetDocEntryByName  (TagName const & tagName);
251
252    ValEntry* GetValEntryByNumber(uint16_t group, uint16_t element); 
253    //BinEntry* GetBinEntryByNumber(uint16_t group, uint16_t element); 
254    RLEFramesInfo* GetRLEInfo() { return RLEInfo; }
255    JPEGFragmentsInfo* GetJPEGInfo() { return JPEGInfo; }
256
257    void LoadDocEntrySafe(DocEntry* entry);
258    TagDocEntryHT* BuildFlatHashTable();
259
260 private:
261    // Read
262    void ParseDES(DocEntrySet *set,long offset, long l_max, bool delim_mode);
263    void ParseSQ (SeqEntry *seq,   long offset, long l_max, bool delim_mode);
264
265    void LoadDocEntry      (DocEntry *);
266    void FindDocEntryLength(DocEntry *) throw ( FormatError );
267    void FindDocEntryVR    (DocEntry *);
268    bool CheckDocEntryVR   (DocEntry *, VRKey);
269
270    std::string GetDocEntryValue  (DocEntry *);
271    std::string GetDocEntryUnvalue(DocEntry *);
272
273    void SkipDocEntry          (DocEntry *);
274    void SkipToNextDocEntry    (DocEntry *);
275
276    void FixDocEntryFoundLength(DocEntry *, uint32_t);
277    bool IsDocEntryAnInteger   (DocEntry *);
278
279    uint32_t FindDocEntryLengthOB() throw( FormatUnexpected );
280
281    uint16_t ReadInt16() throw ( FormatError );
282    uint32_t ReadInt32() throw ( FormatError );
283    void     SkipBytes(uint32_t);
284    bool     ReadTag(uint16_t, uint16_t);
285    uint32_t ReadTagLength(uint16_t, uint16_t);
286
287    void Initialise();
288    bool CheckSwap();
289    void SwitchSwapToBigEndian();
290    void SetMaxSizeLoadEntry(long);
291    void SetMaxSizePrintEntry(long);
292
293    // DocEntry related utilities
294    DocEntry* ReadNextDocEntry();
295
296    uint32_t GenerateFreeTagKeyInGroup(uint16_t group);
297    void BuildFlatHashTableRecurse( TagDocEntryHT& builtHT,
298                                    DocEntrySet* set );
299
300    void HandleBrokenEndian(uint16_t  group, uint16_t  elem);
301 public:
302 // Accessors:
303    /// Accessor to \ref PrintLevel
304    void SetPrintLevel(int level) { PrintLevel = level; }
305
306    /// Accessor to \ref Filename
307    const std::string &GetFileName() const { return Filename; }
308
309    /// Accessor to \ref Filename
310    void SetFileName(std::string const & fileName) { Filename = fileName; }
311
312    /// 'Swap code' accessor (see \ref SwapCode )
313    int GetSwapCode() { return SwapCode; }
314    
315    bool operator<(Document &document);
316
317 };
318 } // end namespace gdcm
319
320 //-----------------------------------------------------------------------------
321 #endif